src/gui/util/qdesktopservices_x11.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qdesktopservices.h"
       
    43 
       
    44 #ifndef QT_NO_DESKTOPSERVICES
       
    45 
       
    46 #include <qprocess.h>
       
    47 #include <qurl.h>
       
    48 #include <qdir.h>
       
    49 #include <qfile.h>
       
    50 #include <qtextstream.h>
       
    51 #include <private/qt_x11_p.h>
       
    52 #include <qcoreapplication.h>
       
    53 #include <stdlib.h>
       
    54 
       
    55 QT_BEGIN_NAMESPACE
       
    56 
       
    57 inline static bool launch(const QUrl &url, const QString &client)
       
    58 {
       
    59 #if !defined(QT_NO_PROCESS)
       
    60     return (QProcess::startDetached(client + QLatin1Char(' ') + QString::fromLatin1(url.toEncoded().constData())));
       
    61 #else
       
    62     return (::system((client + QLatin1Char(' ') + QString::fromLatin1(url.toEncoded().constData())).toLocal8Bit().constData()) != -1);
       
    63 #endif
       
    64 }
       
    65 
       
    66 static bool openDocument(const QUrl &url)
       
    67 {
       
    68     if (!url.isValid())
       
    69         return false;
       
    70 
       
    71     if (launch(url, QLatin1String("xdg-open")))
       
    72         return true;
       
    73 
       
    74     if (X11->desktopEnvironment == DE_GNOME && launch(url, QLatin1String("gnome-open"))) {
       
    75         return true;
       
    76     } else {
       
    77         if (X11->desktopEnvironment == DE_KDE && launch(url, QLatin1String("kfmclient exec")))
       
    78             return true;
       
    79     }
       
    80 
       
    81     if (launch(url, QLatin1String("firefox")))
       
    82         return true;
       
    83     if (launch(url, QLatin1String("mozilla")))
       
    84         return true;
       
    85     if (launch(url, QLatin1String("netscape")))
       
    86         return true;
       
    87     if (launch(url, QLatin1String("opera")))
       
    88         return true;
       
    89 
       
    90     return false;
       
    91 }
       
    92 
       
    93 static bool launchWebBrowser(const QUrl &url)
       
    94 {
       
    95     if (!url.isValid())
       
    96         return false;
       
    97     if (url.scheme() == QLatin1String("mailto"))
       
    98         return openDocument(url);
       
    99 
       
   100     if (launch(url, QLatin1String("xdg-open")))
       
   101         return true;
       
   102     if (launch(url, QString::fromLocal8Bit(getenv("DEFAULT_BROWSER"))))
       
   103         return true;
       
   104     if (launch(url, QString::fromLocal8Bit(getenv("BROWSER"))))
       
   105         return true;
       
   106 
       
   107     if (X11->desktopEnvironment == DE_GNOME && launch(url, QLatin1String("gnome-open"))) {
       
   108         return true;
       
   109     } else {
       
   110         if (X11->desktopEnvironment == DE_KDE && launch(url, QLatin1String("kfmclient openURL")))
       
   111             return true;
       
   112     }
       
   113 
       
   114     if (launch(url, QLatin1String("firefox")))
       
   115         return true;
       
   116     if (launch(url, QLatin1String("mozilla")))
       
   117         return true;
       
   118     if (launch(url, QLatin1String("netscape")))
       
   119         return true;
       
   120     if (launch(url, QLatin1String("opera")))
       
   121         return true;
       
   122     return false;
       
   123 }
       
   124 
       
   125 
       
   126 
       
   127 QString QDesktopServices::storageLocation(StandardLocation type)
       
   128 {
       
   129     if (type == QDesktopServices::HomeLocation)
       
   130         return QDir::homePath();
       
   131     if (type == QDesktopServices::TempLocation)
       
   132         return QDir::tempPath();
       
   133 
       
   134     // http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
       
   135     if (type == QDesktopServices::CacheLocation) {
       
   136         QString xdgCacheHome = QLatin1String(qgetenv("XDG_CACHE_HOME"));
       
   137         if (xdgCacheHome.isEmpty())
       
   138             xdgCacheHome = QDir::homePath() + QLatin1String("/.cache");
       
   139         xdgCacheHome += QLatin1Char('/') + QCoreApplication::organizationName()
       
   140                     + QLatin1Char('/') + QCoreApplication::applicationName();
       
   141         return xdgCacheHome;
       
   142     }
       
   143 
       
   144     if (type == QDesktopServices::DataLocation) {
       
   145         QString xdgDataHome = QLatin1String(qgetenv("XDG_DATA_HOME"));
       
   146         if (xdgDataHome.isEmpty())
       
   147             xdgDataHome = QDir::homePath() + QLatin1String("/.local/share");
       
   148         xdgDataHome += QLatin1String("/data/")
       
   149                     + QCoreApplication::organizationName() + QLatin1Char('/')
       
   150                     + QCoreApplication::applicationName();
       
   151         return xdgDataHome;
       
   152     }
       
   153 
       
   154     // http://www.freedesktop.org/wiki/Software/xdg-user-dirs
       
   155     QString xdgConfigHome = QLatin1String(qgetenv("XDG_CONFIG_HOME"));
       
   156     if (xdgConfigHome.isEmpty())
       
   157         xdgConfigHome = QDir::homePath() + QLatin1String("/.config");
       
   158     QFile file(xdgConfigHome + QLatin1String("/user-dirs.dirs"));
       
   159     if (file.exists() && file.open(QIODevice::ReadOnly)) {
       
   160         QHash<QString, QString> lines;
       
   161         QTextStream stream(&file);
       
   162         // Only look for lines like: XDG_DESKTOP_DIR="$HOME/Desktop"
       
   163         QRegExp exp(QLatin1String("^XDG_(.*)_DIR=(.*)$"));
       
   164         while (!stream.atEnd()) {
       
   165             QString line = stream.readLine();
       
   166             if (exp.indexIn(line) != -1) {
       
   167                 QStringList lst = exp.capturedTexts();
       
   168                 QString key = lst.at(1);
       
   169                 QString value = lst.at(2);
       
   170                 if (value.length() > 2
       
   171                     && value.startsWith(QLatin1Char('\"'))
       
   172                     && value.endsWith(QLatin1Char('\"')))
       
   173                     value = value.mid(1, value.length() - 2);
       
   174                 // Store the key and value: "DESKTOP", "$HOME/Desktop"
       
   175                 lines[key] = value;
       
   176             }
       
   177         }
       
   178 
       
   179         QString key;
       
   180         switch (type) {
       
   181         case DesktopLocation: key = QLatin1String("DESKTOP"); break;
       
   182         case DocumentsLocation: key = QLatin1String("DOCUMENTS"); break;
       
   183         case PicturesLocation: key = QLatin1String("PICTURES"); break;
       
   184         case MusicLocation: key = QLatin1String("MUSIC"); break;
       
   185         case MoviesLocation: key = QLatin1String("VIDEOS"); break;
       
   186         default: break;
       
   187         }
       
   188         if (!key.isEmpty() && lines.contains(key)) {
       
   189             QString value = lines[key];
       
   190             // value can start with $HOME
       
   191             if (value.startsWith(QLatin1String("$HOME")))
       
   192                 value = QDir::homePath() + value.mid(5);
       
   193             return value;
       
   194         }
       
   195     }
       
   196 
       
   197     QDir emptyDir;
       
   198     QString path;
       
   199     switch (type) {
       
   200     case DesktopLocation:
       
   201         path = QDir::homePath() + QLatin1String("/Desktop");
       
   202         break;
       
   203     case DocumentsLocation:
       
   204         path = QDir::homePath() + QLatin1String("/Documents");
       
   205        break;
       
   206     case PicturesLocation:
       
   207         path = QDir::homePath() + QLatin1String("/Pictures");
       
   208         break;
       
   209 
       
   210     case FontsLocation:
       
   211         path = QDir::homePath() + QLatin1String("/.fonts");
       
   212         break;
       
   213 
       
   214     case MusicLocation:
       
   215         path = QDir::homePath() + QLatin1String("/Music");
       
   216         break;
       
   217 
       
   218     case MoviesLocation:
       
   219         path = QDir::homePath() + QLatin1String("/Videos");
       
   220         break;
       
   221 
       
   222     case ApplicationsLocation:
       
   223     default:
       
   224         break;
       
   225     }
       
   226 
       
   227     return path;
       
   228 }
       
   229 
       
   230 QString QDesktopServices::displayName(StandardLocation type)
       
   231 {
       
   232     Q_UNUSED(type);
       
   233     return QString();
       
   234 }
       
   235 
       
   236 QT_END_NAMESPACE
       
   237 
       
   238 #endif // QT_NO_DESKTOPSERVICES