1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
launch(const QUrl & url,const QString & client)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 
openDocument(const QUrl & url)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     // Use the X11->desktopEnvironment value if X11 is non-NULL,
75     //  otherwise just attempt to launch command regardless of the desktop environment
76     if ((!X11 || (X11 && X11->desktopEnvironment == DE_GNOME)) && launch(url, QLatin1String("gnome-open"))) {
77         return true;
78     } else {
79         if ((!X11 || (X11 && X11->desktopEnvironment == DE_KDE)) && launch(url, QLatin1String("kfmclient exec")))
80             return true;
81     }
82 
83     if (launch(url, QLatin1String("firefox")))
84         return true;
85     if (launch(url, QLatin1String("mozilla")))
86         return true;
87     if (launch(url, QLatin1String("netscape")))
88         return true;
89     if (launch(url, QLatin1String("opera")))
90         return true;
91 
92     return false;
93 }
94 
launchWebBrowser(const QUrl & url)95 static bool launchWebBrowser(const QUrl &url)
96 {
97     if (!url.isValid())
98         return false;
99     if (url.scheme() == QLatin1String("mailto"))
100         return openDocument(url);
101 
102     if (launch(url, QLatin1String("xdg-open")))
103         return true;
104     if (launch(url, QString::fromLocal8Bit(getenv("DEFAULT_BROWSER"))))
105         return true;
106     if (launch(url, QString::fromLocal8Bit(getenv("BROWSER"))))
107         return true;
108 
109     // Use the X11->desktopEnvironment value if X11 is non-NULL,
110     //  otherwise just attempt to launch command regardless of the desktop environment
111     if ((!X11 || (X11 && X11->desktopEnvironment == DE_GNOME)) && launch(url, QLatin1String("gnome-open"))) {
112         return true;
113     } else {
114         if ((!X11 || (X11 && X11->desktopEnvironment == DE_KDE)) && launch(url, QLatin1String("kfmclient openURL")))
115             return true;
116     }
117 
118     if (launch(url, QLatin1String("firefox")))
119         return true;
120     if (launch(url, QLatin1String("mozilla")))
121         return true;
122     if (launch(url, QLatin1String("netscape")))
123         return true;
124     if (launch(url, QLatin1String("opera")))
125         return true;
126     return false;
127 }
128 
129 
130 
storageLocation(StandardLocation type)131 QString QDesktopServices::storageLocation(StandardLocation type)
132 {
133     if (type == QDesktopServices::HomeLocation)
134         return QDir::homePath();
135     if (type == QDesktopServices::TempLocation)
136         return QDir::tempPath();
137 
138     // http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
139     if (type == QDesktopServices::CacheLocation) {
140         QString xdgCacheHome = QLatin1String(qgetenv("XDG_CACHE_HOME"));
141         if (xdgCacheHome.isEmpty())
142             xdgCacheHome = QDir::homePath() + QLatin1String("/.cache");
143         xdgCacheHome += QLatin1Char('/') + QCoreApplication::organizationName()
144                     + QLatin1Char('/') + QCoreApplication::applicationName();
145         return xdgCacheHome;
146     }
147 
148     if (type == QDesktopServices::DataLocation) {
149         QString xdgDataHome = QLatin1String(qgetenv("XDG_DATA_HOME"));
150         if (xdgDataHome.isEmpty())
151             xdgDataHome = QDir::homePath() + QLatin1String("/.local/share");
152         xdgDataHome += QLatin1String("/data/")
153                     + QCoreApplication::organizationName() + QLatin1Char('/')
154                     + QCoreApplication::applicationName();
155         return xdgDataHome;
156     }
157 
158     // http://www.freedesktop.org/wiki/Software/xdg-user-dirs
159     QString xdgConfigHome = QLatin1String(qgetenv("XDG_CONFIG_HOME"));
160     if (xdgConfigHome.isEmpty())
161         xdgConfigHome = QDir::homePath() + QLatin1String("/.config");
162     QFile file(xdgConfigHome + QLatin1String("/user-dirs.dirs"));
163     if (file.exists() && file.open(QIODevice::ReadOnly)) {
164         QHash<QString, QString> lines;
165         QTextStream stream(&file);
166         // Only look for lines like: XDG_DESKTOP_DIR="$HOME/Desktop"
167         QRegExp exp(QLatin1String("^XDG_(.*)_DIR=(.*)$"));
168         while (!stream.atEnd()) {
169             QString line = stream.readLine();
170             if (exp.indexIn(line) != -1) {
171                 QStringList lst = exp.capturedTexts();
172                 QString key = lst.at(1);
173                 QString value = lst.at(2);
174                 if (value.length() > 2
175                     && value.startsWith(QLatin1Char('\"'))
176                     && value.endsWith(QLatin1Char('\"')))
177                     value = value.mid(1, value.length() - 2);
178                 // Store the key and value: "DESKTOP", "$HOME/Desktop"
179                 lines[key] = value;
180             }
181         }
182 
183         QString key;
184         switch (type) {
185         case DesktopLocation: key = QLatin1String("DESKTOP"); break;
186         case DocumentsLocation: key = QLatin1String("DOCUMENTS"); break;
187         case PicturesLocation: key = QLatin1String("PICTURES"); break;
188         case MusicLocation: key = QLatin1String("MUSIC"); break;
189         case MoviesLocation: key = QLatin1String("VIDEOS"); break;
190         default: break;
191         }
192         if (!key.isEmpty() && lines.contains(key)) {
193             QString value = lines[key];
194             // value can start with $HOME
195             if (value.startsWith(QLatin1String("$HOME")))
196                 value = QDir::homePath() + value.mid(5);
197             return value;
198         }
199     }
200 
201     QDir emptyDir;
202     QString path;
203     switch (type) {
204     case DesktopLocation:
205         path = QDir::homePath() + QLatin1String("/Desktop");
206         break;
207     case DocumentsLocation:
208         path = QDir::homePath() + QLatin1String("/Documents");
209        break;
210     case PicturesLocation:
211         path = QDir::homePath() + QLatin1String("/Pictures");
212         break;
213 
214     case FontsLocation:
215         path = QDir::homePath() + QLatin1String("/.fonts");
216         break;
217 
218     case MusicLocation:
219         path = QDir::homePath() + QLatin1String("/Music");
220         break;
221 
222     case MoviesLocation:
223         path = QDir::homePath() + QLatin1String("/Videos");
224         break;
225 
226     case ApplicationsLocation:
227     default:
228         break;
229     }
230 
231     return path;
232 }
233 
displayName(StandardLocation type)234 QString QDesktopServices::displayName(StandardLocation type)
235 {
236     Q_UNUSED(type);
237     return QString();
238 }
239 
240 QT_END_NAMESPACE
241 
242 #endif // QT_NO_DESKTOPSERVICES
243