source: trunk/src/gui/util/qdesktopservices_x11.cpp@ 1168

Last change on this file since 1168 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 8.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
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
55QT_BEGIN_NAMESPACE
56
57inline 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
66static 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
95static 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
131QString 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
234QString QDesktopServices::displayName(StandardLocation type)
235{
236 Q_UNUSED(type);
237 return QString();
238}
239
240QT_END_NAMESPACE
241
242#endif // QT_NO_DESKTOPSERVICES
Note: See TracBrowser for help on using the repository browser.