qdesktopservices.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/util/qdesktopservices.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
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:LGPL21$-
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** $QT_END_LICENSE$-
31**-
32****************************************************************************/-
33-
34#include "qdesktopservices.h"-
35-
36#ifndef QT_NO_DESKTOPSERVICES-
37-
38#include <qdebug.h>-
39-
40#include <qstandardpaths.h>-
41#include <qhash.h>-
42#include <qobject.h>-
43#include <qcoreapplication.h>-
44#include <private/qguiapplication_p.h>-
45#include <qurl.h>-
46#include <qmutex.h>-
47#include <qpa/qplatformservices.h>-
48#include <qpa/qplatformintegration.h>-
49#include <qdir.h>-
50-
51QT_BEGIN_NAMESPACE-
52-
53class QOpenUrlHandlerRegistry : public QObject-
54{-
55 Q_OBJECT-
56public:-
57 inline QOpenUrlHandlerRegistry() : mutex(QMutex::Recursive) {}
never executed: end of block
0
58-
59 QMutex mutex;-
60-
61 struct Handler-
62 {-
63 QObject *receiver;-
64 QByteArray name;-
65 };-
66 typedef QHash<QString, Handler> HandlerHash;-
67 HandlerHash handlers;-
68-
69public Q_SLOTS:-
70 void handlerDestroyed(QObject *handler);-
71-
72};-
73-
74Q_GLOBAL_STATIC(QOpenUrlHandlerRegistry, handlerRegistry)
never executed: end of block
never executed: guard.store(QtGlobalStatic::Destroyed);
never executed: return &holder.value;
guard.load() =...c::InitializedDescription
TRUEnever evaluated
FALSEnever evaluated
0
75-
76void QOpenUrlHandlerRegistry::handlerDestroyed(QObject *handler)-
77{-
78 HandlerHash::Iterator it = handlers.begin();-
79 while (it != handlers.end()) {
it != handlers.end()Description
TRUEnever evaluated
FALSEnever evaluated
0
80 if (it->receiver == handler) {
it->receiver == handlerDescription
TRUEnever evaluated
FALSEnever evaluated
0
81 it = handlers.erase(it);-
82 } else {
never executed: end of block
0
83 ++it;-
84 }
never executed: end of block
0
85 }-
86}
never executed: end of block
0
87-
88/*!-
89 \class QDesktopServices-
90 \brief The QDesktopServices class provides methods for accessing common desktop services.-
91 \since 4.2-
92 \ingroup desktop-
93 \inmodule QtGui-
94-
95 Many desktop environments provide services that can be used by applications to-
96 perform common tasks, such as opening a web page, in a way that is both consistent-
97 and takes into account the user's application preferences.-
98-
99 This class contains functions that provide simple interfaces to these services-
100 that indicate whether they succeeded or failed.-
101-
102 The openUrl() function is used to open files located at arbitrary URLs in external-
103 applications. For URLs that correspond to resources on the local filing system-
104 (where the URL scheme is "file"), a suitable application will be used to open the-
105 file; otherwise, a web browser will be used to fetch and display the file.-
106-
107 The user's desktop settings control whether certain executable file types are-
108 opened for browsing, or if they are executed instead. Some desktop environments-
109 are configured to prevent users from executing files obtained from non-local URLs,-
110 or to ask the user's permission before doing so.-
111-
112 \section1 URL Handlers-
113-
114 The behavior of the openUrl() function can be customized for individual URL-
115 schemes to allow applications to override the default handling behavior for-
116 certain types of URLs.-
117-
118 The dispatch mechanism allows only one custom handler to be used for each URL-
119 scheme; this is set using the setUrlHandler() function. Each handler is-
120 implemented as a slot which accepts only a single QUrl argument.-
121-
122 The existing handlers for each scheme can be removed with the-
123 unsetUrlHandler() function. This returns the handling behavior for the given-
124 scheme to the default behavior.-
125-
126 This system makes it easy to implement a help system, for example. Help could be-
127 provided in labels and text browsers using \uicontrol{help://myapplication/mytopic}-
128 URLs, and by registering a handler it becomes possible to display the help text-
129 inside the application:-
130-
131 \snippet code/src_gui_util_qdesktopservices.cpp 0-
132-
133 If inside the handler you decide that you can't open the requested-
134 URL, you can just call QDesktopServices::openUrl() again with the-
135 same argument, and it will try to open the URL using the-
136 appropriate mechanism for the user's desktop environment.-
137-
138 \note Since Qt 5, storageLocation() and displayName() are replaced by functionality-
139 provided by the QStandardPaths class.-
140-
141 \sa QSystemTrayIcon, QProcess, QStandardPaths-
142*/-
143-
144/*!-
145 Opens the given \a url in the appropriate Web browser for the user's desktop-
146 environment, and returns \c true if successful; otherwise returns \c false.-
147-
148 If the URL is a reference to a local file (i.e., the URL scheme is "file") then-
149 it will be opened with a suitable application instead of a Web browser.-
150-
151 The following example opens a file on the Windows file system residing on a path-
152 that contains spaces:-
153-
154 \snippet code/src_gui_util_qdesktopservices.cpp 2-
155-
156 If a \c mailto URL is specified, the user's e-mail client will be used to open a-
157 composer window containing the options specified in the URL, similar to the way-
158 \c mailto links are handled by a Web browser.-
159-
160 For example, the following URL contains a recipient (\c{user@foo.com}), a-
161 subject (\c{Test}), and a message body (\c{Just a test}):-
162-
163 \snippet code/src_gui_util_qdesktopservices.cpp 1-
164-
165 \warning Although many e-mail clients can send attachments and are-
166 Unicode-aware, the user may have configured their client without these features.-
167 Also, certain e-mail clients (e.g., Lotus Notes) have problems with long URLs.-
168-
169 \warning A return value of \c true indicates that the application has successfully requested-
170 the operating system to open the URL in an external application. The external application may-
171 still fail to launch or fail to open the requested URL. This result will not be reported back-
172 to the application.-
173-
174 \sa setUrlHandler()-
175*/-
176bool QDesktopServices::openUrl(const QUrl &url)-
177{-
178 QOpenUrlHandlerRegistry *registry = handlerRegistry();-
179 QMutexLocker locker(&registry->mutex);-
180 static bool insideOpenUrlHandler = false;-
181-
182 if (!insideOpenUrlHandler) {
!insideOpenUrlHandlerDescription
TRUEnever evaluated
FALSEnever evaluated
0
183 QOpenUrlHandlerRegistry::HandlerHash::ConstIterator handler = registry->handlers.constFind(url.scheme());-
184 if (handler != registry->handlers.constEnd()) {
handler != reg...ers.constEnd()Description
TRUEnever evaluated
FALSEnever evaluated
0
185 insideOpenUrlHandler = true;-
186 bool result = QMetaObject::invokeMethod(handler->receiver, handler->name.constData(), Qt::DirectConnection, Q_ARG(QUrl, url));-
187 insideOpenUrlHandler = false;-
188 return result; // ### support bool slot return type
never executed: return result;
0
189 }-
190 }
never executed: end of block
0
191 if (!url.isValid())
!url.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
0
192 return false;
never executed: return false;
0
193-
194 QPlatformIntegration *platformIntegration = QGuiApplicationPrivate::platformIntegration();-
195 if (!platformIntegration)
!platformIntegrationDescription
TRUEnever evaluated
FALSEnever evaluated
0
196 return false;
never executed: return false;
0
197-
198 QPlatformServices *platformServices = platformIntegration->services();-
199 if (!platformServices) {
!platformServicesDescription
TRUEnever evaluated
FALSEnever evaluated
0
200 qWarning("The platform plugin does not support services.");-
201 return false;
never executed: return false;
0
202 }-
203 return url.scheme() == QLatin1String("file") ?
never executed: return url.scheme() == QLatin1String("file") ? platformServices->openDocument(url) : platformServices->openUrl(url);
url.scheme() =...String("file")Description
TRUEnever evaluated
FALSEnever evaluated
0
204 platformServices->openDocument(url) : platformServices->openUrl(url);
never executed: return url.scheme() == QLatin1String("file") ? platformServices->openDocument(url) : platformServices->openUrl(url);
0
205}-
206-
207/*!-
208 Sets the handler for the given \a scheme to be the handler \a method provided by-
209 the \a receiver object.-
210-
211 This function provides a way to customize the behavior of openUrl(). If openUrl()-
212 is called with a URL with the specified \a scheme then the given \a method on the-
213 \a receiver object is called instead of QDesktopServices launching an external-
214 application.-
215-
216 The provided method must be implemented as a slot that only accepts a single QUrl-
217 argument.-
218-
219 If setUrlHandler() is used to set a new handler for a scheme which already-
220 has a handler, the existing handler is simply replaced with the new one.-
221 Since QDesktopServices does not take ownership of handlers, no objects are-
222 deleted when a handler is replaced.-
223-
224 Note that the handler will always be called from within the same thread that-
225 calls QDesktopServices::openUrl().-
226-
227 \sa openUrl(), unsetUrlHandler()-
228*/-
229void QDesktopServices::setUrlHandler(const QString &scheme, QObject *receiver, const char *method)-
230{-
231 QOpenUrlHandlerRegistry *registry = handlerRegistry();-
232 QMutexLocker locker(&registry->mutex);-
233 if (!receiver) {
!receiverDescription
TRUEnever evaluated
FALSEnever evaluated
0
234 registry->handlers.remove(scheme.toLower());-
235 return;
never executed: return;
0
236 }-
237 QOpenUrlHandlerRegistry::Handler h;-
238 h.receiver = receiver;-
239 h.name = method;-
240 registry->handlers.insert(scheme.toLower(), h);-
241 QObject::connect(receiver, SIGNAL(destroyed(QObject*)),-
242 registry, SLOT(handlerDestroyed(QObject*)));-
243}
never executed: end of block
0
244-
245/*!-
246 Removes a previously set URL handler for the specified \a scheme.-
247-
248 \sa setUrlHandler()-
249*/-
250void QDesktopServices::unsetUrlHandler(const QString &scheme)-
251{-
252 setUrlHandler(scheme, 0, 0);-
253}
never executed: end of block
0
254-
255/*!-
256 \enum QDesktopServices::StandardLocation-
257 \since 4.4-
258 \obsolete-
259 Use QStandardPaths::StandardLocation (see storageLocation() for porting notes)-
260-
261 This enum describes the different locations that can be queried by-
262 QDesktopServices::storageLocation and QDesktopServices::displayName.-
263-
264 \value DesktopLocation Returns the user's desktop directory.-
265 \value DocumentsLocation Returns the user's document.-
266 \value FontsLocation Returns the user's fonts.-
267 \value ApplicationsLocation Returns the user's applications.-
268 \value MusicLocation Returns the users music.-
269 \value MoviesLocation Returns the user's movies.-
270 \value PicturesLocation Returns the user's pictures.-
271 \value TempLocation Returns the system's temporary directory.-
272 \value HomeLocation Returns the user's home directory.-
273 \value DataLocation Returns a directory location where persistent-
274 application data can be stored. QCoreApplication::applicationName-
275 and QCoreApplication::organizationName should work on all-
276 platforms.-
277 \value CacheLocation Returns a directory location where user-specific-
278 non-essential (cached) data should be written.-
279-
280 \sa storageLocation(), displayName()-
281*/-
282-
283/*!-
284 \fn QString QDesktopServices::storageLocation(StandardLocation type)-
285 \obsolete-
286 Use QStandardPaths::writableLocation()-
287-
288 \note when porting QDesktopServices::DataLocation to QStandardPaths::DataLocation,-
289 a different path will be returned.-
290-
291 \c{QDesktopServices::DataLocation} was \c{GenericDataLocation + "/data/organization/application"},-
292 while QStandardPaths::DataLocation is \c{GenericDataLocation + "/organization/application"}.-
293-
294 Also note that \c{application} could be empty in Qt 4, if QCoreApplication::setApplicationName()-
295 wasn't called, while in Qt 5 it defaults to the name of the executable.-
296-
297 Therefore, if you still need to access the Qt 4 path (for example for data migration to Qt 5), replace-
298 \code-
299 QDesktopServices::storageLocation(QDesktopServices::DataLocation)-
300 \endcode-
301 with-
302 \code-
303 QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) +-
304 "/data/organization/application"-
305 \endcode-
306 (assuming an organization name and an application name were set).-
307*/-
308-
309/*!-
310 \fn QString QDesktopServices::displayName(StandardLocation type)-
311 \obsolete-
312 Use QStandardPaths::displayName()-
313*/-
314-
315extern Q_CORE_EXPORT QString qt_applicationName_noFallback();-
316-
317QString QDesktopServices::storageLocationImpl(QStandardPaths::StandardLocation type)-
318{-
319 if (type == QStandardPaths::AppLocalDataLocation) {
type == QStand...alDataLocationDescription
TRUEnever evaluated
FALSEnever evaluated
0
320 // Preserve Qt 4 compatibility:-
321 // * QCoreApplication::applicationName() must default to empty-
322 // * Unix data location is under the "data/" subdirectory-
323 const QString compatAppName = qt_applicationName_noFallback();-
324 const QString baseDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);-
325#if defined(Q_OS_WIN) || defined(Q_OS_MAC)-
326 QString result = baseDir;-
327 if (!QCoreApplication::organizationName().isEmpty())-
328 result += QLatin1Char('/') + QCoreApplication::organizationName();-
329 if (!compatAppName.isEmpty())-
330 result += QLatin1Char('/') + compatAppName;-
331 return result;-
332#elif defined(Q_OS_UNIX)-
333 return baseDir + QLatin1String("/data/")
never executed: return baseDir + QLatin1String("/data/") + QCoreApplication::organizationName() + QLatin1Char('/') + compatAppName;
0
334 + QCoreApplication::organizationName() + QLatin1Char('/')
never executed: return baseDir + QLatin1String("/data/") + QCoreApplication::organizationName() + QLatin1Char('/') + compatAppName;
0
335 + compatAppName;
never executed: return baseDir + QLatin1String("/data/") + QCoreApplication::organizationName() + QLatin1Char('/') + compatAppName;
0
336#endif-
337 }-
338 return QStandardPaths::writableLocation(type);
never executed: return QStandardPaths::writableLocation(type);
0
339}-
340-
341QT_END_NAMESPACE-
342-
343#include "qdesktopservices.moc"-
344-
345#endif // QT_NO_DESKTOPSERVICES-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9