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

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

global: Updated year to 2010 in OS/2-specific headers.

File size: 9.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** Copyright (C) 2010 netlabs.org. OS/2 parts.
8**
9** This file is part of the QtGui module of the Qt Toolkit.
10**
11** $QT_BEGIN_LICENSE:LGPL$
12** Commercial Usage
13** Licensees holding valid Qt Commercial licenses may use this file in
14** accordance with the Qt Commercial License Agreement provided with the
15** Software or, alternatively, in accordance with the terms contained in
16** a written agreement between you and Nokia.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 2.1 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 2.1 requirements
24** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25**
26** In addition, as a special exception, Nokia gives you certain additional
27** rights. These rights are described in the Nokia Qt 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** If you have questions regarding the use of this file, please contact
39** Nokia at [email protected].
40** $QT_END_LICENSE$
41**
42****************************************************************************/
43
44#include <qsettings.h>
45#include <qdir.h>
46#include <qurl.h>
47#include <qstringlist.h>
48#include <qprocess.h>
49#include <qtemporaryfile.h>
50#include <qcoreapplication.h>
51
52#include <qt_os2.h>
53
54#ifndef QT_NO_DESKTOPSERVICES
55
56QT_BEGIN_NAMESPACE
57
58static bool openDocument(const QUrl &file)
59{
60 if (!file.isValid())
61 return false;
62
63 QString path = file.toLocalFile();
64 if (path.isEmpty())
65 return false;
66
67 path = QDir::toNativeSeparators(QDir::cleanPath(QDir::current()
68 .absoluteFilePath(path)));
69
70 HOBJECT hobj = WinQueryObject(QFile::encodeName(path));
71 if (hobj != NULLHANDLE)
72 return WinOpenObject(hobj, 0 /* OPEN_DEFAULT */, FALSE);
73
74 return false;
75}
76
77static QString openUrlParam(const char *key)
78{
79 QByteArray val;
80
81 ULONG len = 0;
82 if (PrfQueryProfileSize(HINI_USERPROFILE, "WPURLDEFAULTSETTINGS",
83 key, &len) && len) {
84 val.resize(len);
85 len = PrfQueryProfileString(HINI_USERPROFILE, "WPURLDEFAULTSETTINGS",
86 key, 0, val.data(), len);
87 --len; // excude terminating NULL
88 val.truncate(len);
89 }
90
91 return QString::fromLocal8Bit(val);
92}
93
94static bool launchWebBrowser(const QUrl &url)
95{
96 if (!url.isValid())
97 return false;
98
99 struct
100 {
101 const char *proto;
102 const char *exe;
103 const char *params;
104 const char *workDir;
105 }
106 Apps[] = {
107 { "mailto", "DefaultMailExe", "DefaultMailParameters", "DefaultMailWorkingDir" },
108 { "news", "DefaultNewsExe", "DefaultNewsParameters", "DefaultNewsWorkingDir" },
109 { "ftp", "DefaultFTPExe", "DefaultFTPParameters", "DefaultFTPWorkingDir" },
110 { "irc", "DefaultIRCExe", "DefaultIRCParameters", "DefaultIRCWorkingDir" },
111 // default: proto must be 0 and it must always come last
112 { 0, "DefaultBrowserExe", "DefaultParameters", "DefaultWorkingDir" },
113 };
114
115 for (size_t i = 0; i < sizeof(Apps)/sizeof(Apps[0]); ++i) {
116 if (!Apps[i].proto || url.scheme() == QLatin1String(Apps[i].proto)) {
117 QString exe = openUrlParam(Apps[i].exe);
118 if (!exe.isEmpty()) {
119 QString params = openUrlParam(Apps[i].params);
120 QString workDir = openUrlParam(Apps[i].workDir);
121 QStringList args;
122 args << params << url.toString();
123 QProcess process;
124 return process.startDetached(exe, args, workDir);
125 }
126 }
127 }
128
129 return false;
130}
131
132QString QDesktopServices::storageLocation(StandardLocation type)
133{
134 if (type == QDesktopServices::HomeLocation)
135 return QDir::homePath();
136 if (type == QDesktopServices::TempLocation)
137 return QDir::tempPath();
138
139 // http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
140 if (type == QDesktopServices::CacheLocation) {
141 QString xdgCacheHome = QFile::decodeName(qgetenv("XDG_CACHE_HOME"));
142 if (xdgCacheHome.isEmpty())
143 xdgCacheHome = QDir::homePath() + QLatin1String("/.cache");
144 xdgCacheHome += QLatin1Char('/') + QCoreApplication::organizationName()
145 + QLatin1Char('/') + QCoreApplication::applicationName();
146 return QDir::cleanPath(xdgCacheHome);
147 }
148
149 if (type == QDesktopServices::DataLocation) {
150 QString xdgDataHome = QFile::decodeName(qgetenv("XDG_DATA_HOME"));
151 if (xdgDataHome.isEmpty())
152 xdgDataHome = QDir::homePath() + QLatin1String("/.local/share");
153 xdgDataHome += QLatin1String("/data/")
154 + QCoreApplication::organizationName() + QLatin1Char('/')
155 + QCoreApplication::applicationName();
156 return QDir::cleanPath(xdgDataHome);
157 }
158
159 // http://www.freedesktop.org/wiki/Software/xdg-user-dirs
160 QString xdgConfigHome = QFile::decodeName(qgetenv("XDG_CONFIG_HOME"));
161 if (xdgConfigHome.isEmpty())
162 xdgConfigHome = QDir::homePath() + QLatin1String("/.config");
163 QFile file(xdgConfigHome + QLatin1String("/user-dirs.dirs"));
164 if (file.exists() && file.open(QIODevice::ReadOnly)) {
165 QHash<QString, QString> lines;
166 QTextStream stream(&file);
167 // Only look for lines like: XDG_DESKTOP_DIR="$HOME/Desktop"
168 QRegExp exp(QLatin1String("^XDG_(.*)_DIR=(.*)$"));
169 while (!stream.atEnd()) {
170 QString line = stream.readLine();
171 if (exp.indexIn(line) != -1) {
172 QStringList lst = exp.capturedTexts();
173 QString key = lst.at(1);
174 QString value = lst.at(2);
175 if (value.length() > 2
176 && value.startsWith(QLatin1String("\""))
177 && value.endsWith(QLatin1String("\"")))
178 value = value.mid(1, value.length() - 2);
179 // Store the key and value: "DESKTOP", "$HOME/Desktop"
180 lines[key] = value;
181 }
182 }
183
184 QString key;
185 switch (type) {
186 case DesktopLocation: key = QLatin1String("DESKTOP"); break;
187 case DocumentsLocation: key = QLatin1String("DOCUMENTS"); break;
188 case PicturesLocation: key = QLatin1String("PICTURES"); break;
189 case MusicLocation: key = QLatin1String("MUSIC"); break;
190 case MoviesLocation: key = QLatin1String("VIDEOS"); break;
191 default: break;
192 }
193 if (!key.isEmpty() && lines.contains(key)) {
194 QString value = lines[key];
195 // value can start with $HOME or %HOME%
196 if (value.startsWith(QLatin1String("$HOME")))
197 value = QDir::homePath() + value.mid(5);
198 else if (value.startsWith(QLatin1String("%HOME%")))
199 value = QDir::homePath() + value.mid(6);
200 return QDir::cleanPath(value);
201 }
202 }
203
204 QString path;
205 switch (type) {
206 case DesktopLocation: {
207 QByteArray buf(CCHMAXPATH, '\0');
208 WinQueryActiveDesktopPathname(buf.data(), buf.size());
209 buf.truncate(qstrlen(buf));
210 if (!buf.isEmpty())
211 path = QDir::cleanPath(QFile::decodeName(buf));
212 else
213 path = QDir::rootPath() + QLatin1String("Desktop");
214 break;
215 }
216 case DocumentsLocation:
217 path = QDir::homePath() + QLatin1String("/Documents");
218 break;
219 case PicturesLocation:
220 path = QDir::homePath() + QLatin1String("/Pictures");
221 break;
222 case MusicLocation:
223 path = QDir::homePath() + QLatin1String("/Music");
224 break;
225 case MoviesLocation:
226 path = QDir::homePath() + QLatin1String("/Videos");
227 break;
228
229 case ApplicationsLocation:
230 path = QDir::cleanPath(QFile::decodeName(qgetenv("PROGRAMS")));
231 if (path.isEmpty())
232 path = QDir::rootPath() + QLatin1String("PROGRAMS");
233 break;
234
235 case FontsLocation:
236 path = QDir::cleanPath(QFile::decodeName(qgetenv("FONTS")));
237 if (path.isEmpty())
238 path = QDir::rootPath() + QLatin1String("PSFONTS");
239 break;
240
241 default:
242 break;
243 }
244
245 return path;
246}
247
248QString QDesktopServices::displayName(StandardLocation type)
249{
250 Q_UNUSED(type);
251 return QString();
252}
253
254QT_END_NAMESPACE
255
256#endif // QT_NO_DESKTOPSERVICES
Note: See TracBrowser for help on using the repository browser.