Qt 4.8
qdesktopservices.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qdesktopservices.h"
43 
44 #ifndef QT_NO_DESKTOPSERVICES
45 
46 #include <qdebug.h>
47 
48 #if defined(Q_OS_BLACKBERRY)
50 #elif defined(Q_WS_QWS) || defined(Q_WS_QPA)
51 #include "qdesktopservices_qws.cpp"
52 #elif defined(Q_WS_X11)
53 #include "qdesktopservices_x11.cpp"
54 #elif defined(Q_WS_WIN)
55 #include "qdesktopservices_win.cpp"
56 #elif defined(Q_WS_MAC)
57 #include "qdesktopservices_mac.cpp"
58 #elif defined(Q_OS_SYMBIAN)
59 #include "qdesktopservices_s60.cpp"
60 #endif
61 
62 #include <qhash.h>
63 #include <qobject.h>
64 #include <qcoreapplication.h>
65 #include <qurl.h>
66 #include <qmutex.h>
67 
69 
71 {
72  Q_OBJECT
73 public:
74  inline QOpenUrlHandlerRegistry() : mutex(QMutex::Recursive) {}
75 
77 
78  struct Handler
79  {
82  };
84  HandlerHash handlers;
85 
86 public Q_SLOTS:
88 
89 };
90 
92 
94 {
96  while (it != handlers.end()) {
97  if (it->receiver == handler) {
98  it = handlers.erase(it);
99  } else {
100  ++it;
101  }
102  }
103 }
104 
191 {
192  QOpenUrlHandlerRegistry *registry = handlerRegistry();
193  QMutexLocker locker(&registry->mutex);
194  static bool insideOpenUrlHandler = false;
195 
196  if (!insideOpenUrlHandler) {
198  if (handler != registry->handlers.constEnd()) {
199  insideOpenUrlHandler = true;
200  bool result = QMetaObject::invokeMethod(handler->receiver, handler->name.constData(), Qt::DirectConnection, Q_ARG(QUrl, url));
201  insideOpenUrlHandler = false;
202  return result; // ### support bool slot return type
203  }
204  }
205 
206  bool result;
207  if (url.scheme() == QLatin1String("file"))
208  result = openDocument(url);
209  else
210  result = launchWebBrowser(url);
211 
212  return result;
213 }
214 
237 void QDesktopServices::setUrlHandler(const QString &scheme, QObject *receiver, const char *method)
238 {
239  QOpenUrlHandlerRegistry *registry = handlerRegistry();
240  QMutexLocker locker(&registry->mutex);
241  if (!receiver) {
242  registry->handlers.remove(scheme);
243  return;
244  }
246  h.receiver = receiver;
247  h.name = method;
248  registry->handlers.insert(scheme, h);
250  registry, SLOT(handlerDestroyed(QObject*)));
251 }
252 
259 {
260  setUrlHandler(scheme, 0, 0);
261 }
262 
323 
324 #include "qdesktopservices.moc"
325 
326 #endif // QT_NO_DESKTOPSERVICES
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QMutex class provides access serialization between threads.
Definition: qmutex.h:60
int remove(const Key &key)
Removes all the items that have the key from the hash.
Definition: qhash.h:784
QHash< QString, Handler > HandlerHash
#define it(className, varName)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define SLOT(a)
Definition: qobjectdefs.h:226
const_iterator ConstIterator
Qt-style synonym for QHash::const_iterator.
Definition: qhash.h:474
static void unsetUrlHandler(const QString &scheme)
Removes a previously set URL handler for the specified scheme.
#define Q_ARG(type, data)
Definition: qobjectdefs.h:246
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define Q_SLOTS
Definition: qobjectdefs.h:71
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void destroyed(QObject *=0)
This signal is emitted immediately before the object obj is destroyed, and can not be blocked...
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
const_iterator constFind(const Key &key) const
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:859
void handlerDestroyed(QObject *handler)
static bool openDocument(const QUrl &file)
#define Q_OBJECT
Definition: qobjectdefs.h:157
static bool launchWebBrowser(const QUrl &url)
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:469
QString scheme() const
Returns the scheme of the URL.
Definition: qurl.cpp:4550
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
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...
iterator begin()
Returns an STL-style iterator pointing to the first item in the hash.
Definition: qhash.h:464
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(0), 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())
Invokes the member (a signal or a slot name) on the object obj.
QImageIOHandler * handler
iterator erase(iterator it)
Removes the (key, value) pair associated with the iterator pos from the hash, and returns an iterator...
Definition: qhash.h:827
iterator Iterator
Qt-style synonym for QHash::iterator.
Definition: qhash.h:473
static bool openUrl(const QUrl &url)
Opens the given url in the appropriate Web browser for the user&#39;s desktop environment, and returns true if successful; otherwise returns false.