Qt 6.x
The Qt SDK
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
21#include <QtCore/private/qlocking_p.h>
22
24
26{
27public:
29
31
32 struct Handler
33 {
36 };
39
40#if QT_VERSION < QT_VERSION_CHECK(6, 6, 0)
42
43 void handlerDestroyed(QObject *handler);
44#endif
45
46};
47
49
50#if QT_VERSION < QT_VERSION_CHECK(6, 6, 0)
51void QOpenUrlHandlerRegistry::handlerDestroyed(QObject *handler)
52{
53 const auto lock = qt_scoped_lock(mutex);
55 while (it != handlers.end()) {
56 if (it->receiver == handler) {
58 qWarning("Please call QDesktopServices::unsetUrlHandler() before destroying a "
59 "registered URL handler object.\n"
60 "Support for destroying a registered URL handler object is deprecated, "
61 "and will be removed in Qt 6.6.");
62 } else {
63 ++it;
64 }
65 }
66}
67#endif
68
175{
176 QOpenUrlHandlerRegistry *registry = handlerRegistry();
177 QMutexLocker locker(&registry->mutex);
178 static bool insideOpenUrlHandler = false;
179
180 if (!insideOpenUrlHandler) {
182 if (handler != registry->handlers.constEnd()) {
183 insideOpenUrlHandler = true;
184 bool result = QMetaObject::invokeMethod(handler->receiver, handler->name.constData(), Qt::DirectConnection, Q_ARG(QUrl, url));
185 insideOpenUrlHandler = false;
186 return result; // ### support bool slot return type
187 }
188 }
189 if (!url.isValid())
190 return false;
191
193 if (Q_UNLIKELY(!platformIntegration)) {
195 if (Q_UNLIKELY(!application))
196 qWarning("QDesktopServices::openUrl: Please instantiate the QGuiApplication object "
197 "first");
198 else if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(application)))
199 qWarning("QDesktopServices::openUrl: Application is not a GUI application");
200 return false;
201 }
202
203 QPlatformServices *platformServices = platformIntegration->services();
204 if (!platformServices) {
205 qWarning("The platform plugin does not support services.");
206 return false;
207 }
208 // We only use openDocument if there is no fragment for the URL to
209 // avoid it being lost when using openDocument
210 if (url.isLocalFile() && !url.hasFragment())
211 return platformServices->openDocument(url);
212 return platformServices->openUrl(url);
213}
214
297void QDesktopServices::setUrlHandler(const QString &scheme, QObject *receiver, const char *method)
298{
299 QOpenUrlHandlerRegistry *registry = handlerRegistry();
300 QMutexLocker locker(&registry->mutex);
301 if (!receiver) {
302 registry->handlers.remove(scheme.toLower());
303 return;
304 }
306 h.receiver = receiver;
307 h.name = method;
308 registry->handlers.insert(scheme.toLower(), h);
309#if QT_VERSION < QT_VERSION_CHECK(6, 6, 0)
310 QObject::connect(receiver, &QObject::destroyed, &registry->context,
311 [registry](QObject *obj) { registry->handlerDestroyed(obj); },
313#endif
314}
315
326{
327 setUrlHandler(scheme, nullptr, nullptr);
328}
329
331
332#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.
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 bool openUrl(const QUrl &url)
Opens the given url in the appropriate Web browser for the user's desktop environment,...
static QPlatformIntegration * platformIntegration()
\inmodule QtCore
Definition qhash.h:818
bool remove(const Key &key)
Removes the item that has the key from the hash.
Definition qhash.h:956
iterator begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1202
const_iterator constFind(const Key &key) const noexcept
Definition qhash.h:1279
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:1209
iterator Iterator
Qt-style synonym for QHash::iterator.
Definition qhash.h:1255
iterator erase(const_iterator it)
Definition qhash.h:1223
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1206
const_iterator ConstIterator
Qt-style synonym for QHash::const_iterator.
Definition qhash.h:1256
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1283
\inmodule QtCore
Definition qmutex.h:317
\inmodule QtCore
Definition qobject.h:90
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
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.
virtual bool openDocument(const QUrl &url)
virtual bool openUrl(const QUrl &url)
\inmodule QtCore
Definition qmutex.h:313
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QString toLower() const &
Definition qstring.h:368
\inmodule QtCore
Definition qurl.h:94
bool isLocalFile() const
Definition qurl.cpp:3431
bool hasFragment() const
Definition qurl.cpp:2697
bool isValid() const
Returns true if the URL is non-empty and valid; otherwise returns false.
Definition qurl.cpp:1874
QString scheme() const
Returns the scheme of the URL.
Definition qurl.cpp:1983
QSet< QString >::iterator it
Combined button and popup list for selecting options.
@ DirectConnection
static void * context
#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:162
#define Q_ARG(Type, data)
Definition qobjectdefs.h:62
GLfloat GLfloat GLfloat GLfloat h
GLhandleARB obj
[2]
GLuint64EXT * result
[6]
QUrl url("example.com")
[constructor-url-reference]
QReadWriteLock lock
[0]
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...