Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qdesktopservices.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qdesktopservices.h"
5
6#ifndef QT_NO_DESKTOPSERVICES
7
8#include <qdebug.h>
9
10#include <qstandardpaths.h>
11#include <qhash.h>
12#include <qobject.h>
13#include <qcoreapplication.h>
14#include <private/qguiapplication_p.h>
15#include <qurl.h>
16#include <qmutex.h>
17#include <qpa/qplatformservices.h>
18#include <qpa/qplatformintegration.h>
19#include <qdir.h>
20
22
24{
25public:
27
29
35 typedef QHash<QString, Handler> HandlerHash;
37};
38
40
41
146bool QDesktopServices::openUrl(const QUrl &url)
147{
148 QOpenUrlHandlerRegistry *registry = handlerRegistry();
149 QMutexLocker locker(&registry->mutex);
150 static bool insideOpenUrlHandler = false;
151
152 if (!insideOpenUrlHandler) {
154 if (handler != registry->handlers.constEnd()) {
155 insideOpenUrlHandler = true;
156 bool result = QMetaObject::invokeMethod(handler->receiver, handler->name.constData(), Qt::DirectConnection, Q_ARG(QUrl, url));
157 insideOpenUrlHandler = false;
158 return result; // ### support bool slot return type
159 }
160 }
161 if (!url.isValid())
162 return false;
163
165 if (Q_UNLIKELY(!platformIntegration)) {
167 if (Q_UNLIKELY(!application))
168 qWarning("QDesktopServices::openUrl: Please instantiate the QGuiApplication object "
169 "first");
170 else if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(application)))
171 qWarning("QDesktopServices::openUrl: Application is not a GUI application");
172 return false;
173 }
174
175 QPlatformServices *platformServices = platformIntegration->services();
176 if (!platformServices) {
177 qWarning("The platform plugin does not support services.");
178 return false;
179 }
180 // We only use openDocument if there is no fragment for the URL to
181 // avoid it being lost when using openDocument
182 if (url.isLocalFile() && !url.hasFragment())
183 return platformServices->openDocument(url);
184 return platformServices->openUrl(url);
185}
186
270void QDesktopServices::setUrlHandler(const QString &scheme, QObject *receiver, const char *method)
271{
272 QOpenUrlHandlerRegistry *registry = handlerRegistry();
273 QMutexLocker locker(&registry->mutex);
274 if (!receiver) {
275 registry->handlers.remove(scheme.toLower());
276 return;
277 }
279 h.receiver = receiver;
280 h.name = method;
281 registry->handlers.insert(scheme.toLower(), h);
282}
283
294{
295 setUrlHandler(scheme, nullptr, nullptr);
296}
297
299
300#endif // QT_NO_DESKTOPSERVICES
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
The QDesktopServices class provides methods for accessing common desktop services.
static void unsetUrlHandler(const QString &scheme)
Removes a previously set URL handler for the specified scheme.
static void setUrlHandler(const QString &scheme, QObject *receiver, const char *method)
Sets the handler for the given scheme to be the handler method provided by the receiver object.
static QPlatformIntegration * platformIntegration()
bool remove(const Key &key)
Removes the item that has the key from the hash.
Definition qhash.h:958
const_iterator constFind(const Key &key) const noexcept
Definition qhash.h:1299
const_iterator constEnd() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
Definition qhash.h:1219
const_iterator ConstIterator
Qt-style synonym for QHash::const_iterator.
Definition qhash.h:1289
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1303
\inmodule QtCore
Definition qmutex.h:313
\inmodule QtCore
Definition qobject.h:103
QHash< QString, Handler > HandlerHash
QOpenUrlHandlerRegistry()=default
The QPlatformIntegration class is the entry for WindowSystem specific functionality.
virtual QPlatformServices * services() const
The QPlatformServices provides the backend for desktop-related functionality.
\inmodule QtCore
Definition qmutex.h:309
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString toLower() const &
Definition qstring.h:435
\inmodule QtCore
Definition qurl.h:94
bool isLocalFile() const
Definition qurl.cpp:3445
bool hasFragment() const
Definition qurl.cpp:2700
bool isValid() const
Returns true if the URL is non-empty and valid; otherwise returns false.
Definition qurl.cpp:1882
QString scheme() const
Returns the scheme of the URL.
Definition qurl.cpp:1991
Combined button and popup list for selecting options.
@ DirectConnection
#define Q_UNLIKELY(x)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char * method
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
#define qWarning
Definition qlogging.h:166
#define Q_ARG(Type, data)
Definition qobjectdefs.h:63
GLfloat GLfloat GLfloat GLfloat h
GLuint64EXT * result
[6]
QUrl url("example.com")
[constructor-url-reference]
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(nullptr), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
\threadsafe This is an overloaded member function, provided for convenience. It differs from the abov...