Qt 4.8
qdesktopservices_x11.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
56 
57 inline static bool launch(const QUrl &url, const QString &client)
58 {
59 #if !defined(QT_NO_PROCESS)
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  // 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 
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 
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()
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/")
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)) {
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 
227  default:
228  break;
229  }
230 
231  return path;
232 }
233 
235 {
236  Q_UNUSED(type);
237  return QString();
238 }
239 
241 
242 #endif // QT_NO_DESKTOPSERVICES
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
static bool startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory, qint64 *pid=0)
Starts the program program with the arguments arguments in a new process, and detaches from it...
Definition: qprocess.cpp:2332
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
int type
Definition: qmetatype.cpp:239
bool isValid() const
Returns true if the URL is valid; otherwise returns false.
Definition: qurl.cpp:4303
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QString readLine(qint64 maxlen=0)
Reads one line of text from the stream, and returns it as a QString.
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
static QString storageLocation(StandardLocation type)
Returns the default system directory where files of type belong, or an empty string if the location c...
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition: qfile.cpp:1064
static QString displayName(StandardLocation type)
Returns a localized display name for the given location type or an empty QString if no relevant locat...
static bool openDocument(const QUrl &url)
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
StandardLocation
This enum describes the different locations that can be queried by QDesktopServices::storageLocation ...
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define X11
Definition: qt_x11_p.h:724
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
bool exists() const
Returns true if the file specified by fileName() exists; otherwise returns false. ...
Definition: qfile.cpp:626
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static FILE * stream
int indexIn(const QString &str, int offset=0, CaretMode caretMode=CaretAtZero) const
Attempts to find a match in str from position offset (0 by default).
Definition: qregexp.cpp:4136
QStringList capturedTexts() const
Returns a list of the captured text strings.
Definition: qregexp.cpp:4267
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
static bool launchWebBrowser(const QUrl &url)
static QString tempPath()
Returns the absolute path of the system&#39;s temporary directory.
Definition: qdir.cpp:1987
QByteArray toEncoded(FormattingOptions options=None) const
Returns the encoded representation of the URL if it&#39;s valid; otherwise an empty QByteArray is returne...
Definition: qurl.cpp:5949
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
QString scheme() const
Returns the scheme of the URL.
Definition: qurl.cpp:4550
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:73
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
int key
static bool launch(const QUrl &url, const QString &client)
static QString organizationName()
bool atEnd() const
Returns true if there is no more data to be read from the QTextStream; otherwise returns false...
static QString applicationName()
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
static QString homePath()
Returns the absolute path of the user&#39;s home directory.
Definition: qdir.cpp:1942