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

Last change on this file since 123 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

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