/src/gui/util/qdesktopservices.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 312 lines · 91 code · 26 blank · 195 comment · 12 complexity · 56ca0b89c5de634870eb86749877ef94 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the QtGui module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qdesktopservices.h"
  42. #ifndef QT_NO_DESKTOPSERVICES
  43. #include <qdebug.h>
  44. #if defined(Q_WS_QWS) || defined(Q_WS_QPA)
  45. #include "qdesktopservices_qws.cpp"
  46. #elif defined(Q_WS_X11)
  47. #include "qdesktopservices_x11.cpp"
  48. #elif defined(Q_WS_WIN)
  49. #include "qdesktopservices_win.cpp"
  50. #elif defined(Q_WS_MAC)
  51. #include "qdesktopservices_mac.cpp"
  52. #elif defined(Q_OS_SYMBIAN)
  53. #include "qdesktopservices_s60.cpp"
  54. #endif
  55. #include <qhash.h>
  56. #include <qobject.h>
  57. #include <qcoreapplication.h>
  58. #include <qurl.h>
  59. #include <qmutex.h>
  60. QT_BEGIN_NAMESPACE
  61. class QOpenUrlHandlerRegistry : public QObject
  62. {
  63. Q_OBJECT
  64. public:
  65. inline QOpenUrlHandlerRegistry() : mutex(QMutex::Recursive) {}
  66. QMutex mutex;
  67. struct Handler
  68. {
  69. QObject *receiver;
  70. QByteArray name;
  71. };
  72. typedef QHash<QString, Handler> HandlerHash;
  73. HandlerHash handlers;
  74. public Q_SLOTS:
  75. void handlerDestroyed(QObject *handler);
  76. };
  77. Q_GLOBAL_STATIC(QOpenUrlHandlerRegistry, handlerRegistry)
  78. void QOpenUrlHandlerRegistry::handlerDestroyed(QObject *handler)
  79. {
  80. HandlerHash::Iterator it = handlers.begin();
  81. while (it != handlers.end()) {
  82. if (it->receiver == handler) {
  83. it = handlers.erase(it);
  84. } else {
  85. ++it;
  86. }
  87. }
  88. }
  89. /*!
  90. \class QDesktopServices
  91. \brief The QDesktopServices class provides methods for accessing common desktop services.
  92. \since 4.2
  93. \ingroup desktop
  94. Many desktop environments provide services that can be used by applications to
  95. perform common tasks, such as opening a web page, in a way that is both consistent
  96. and takes into account the user's application preferences.
  97. This class contains functions that provide simple interfaces to these services
  98. that indicate whether they succeeded or failed.
  99. The openUrl() function is used to open files located at arbitrary URLs in external
  100. applications. For URLs that correspond to resources on the local filing system
  101. (where the URL scheme is "file"), a suitable application will be used to open the
  102. file; otherwise, a web browser will be used to fetch and display the file.
  103. The user's desktop settings control whether certain executable file types are
  104. opened for browsing, or if they are executed instead. Some desktop environments
  105. are configured to prevent users from executing files obtained from non-local URLs,
  106. or to ask the user's permission before doing so.
  107. \section1 URL Handlers
  108. The behavior of the openUrl() function can be customized for individual URL
  109. schemes to allow applications to override the default handling behavior for
  110. certain types of URLs.
  111. The dispatch mechanism allows only one custom handler to be used for each URL
  112. scheme; this is set using the setUrlHandler() function. Each handler is
  113. implemented as a slot which accepts only a single QUrl argument.
  114. The existing handlers for each scheme can be removed with the
  115. unsetUrlHandler() function. This returns the handling behavior for the given
  116. scheme to the default behavior.
  117. This system makes it easy to implement a help system, for example. Help could be
  118. provided in labels and text browsers using \gui{help://myapplication/mytopic}
  119. URLs, and by registering a handler it becomes possible to display the help text
  120. inside the application:
  121. \snippet doc/src/snippets/code/src_gui_util_qdesktopservices.cpp 0
  122. If inside the handler you decide that you can't open the requested
  123. URL, you can just call QDesktopServices::openUrl() again with the
  124. same argument, and it will try to open the URL using the
  125. appropriate mechanism for the user's desktop environment.
  126. \sa QSystemTrayIcon, QProcess
  127. */
  128. /*!
  129. Opens the given \a url in the appropriate Web browser for the user's desktop
  130. environment, and returns true if successful; otherwise returns false.
  131. If the URL is a reference to a local file (i.e., the URL scheme is "file") then
  132. it will be opened with a suitable application instead of a Web browser.
  133. The following example opens a file on the Windows file system residing on a path
  134. that contains spaces:
  135. \snippet doc/src/snippets/code/src_gui_util_qdesktopservices.cpp 2
  136. If a \c mailto URL is specified, the user's e-mail client will be used to open a
  137. composer window containing the options specified in the URL, similar to the way
  138. \c mailto links are handled by a Web browser.
  139. For example, the following URL contains a recipient (\c{user@foo.com}), a
  140. subject (\c{Test}), and a message body (\c{Just a test}):
  141. \snippet doc/src/snippets/code/src_gui_util_qdesktopservices.cpp 1
  142. \warning Although many e-mail clients can send attachments and are
  143. Unicode-aware, the user may have configured their client without these features.
  144. Also, certain e-mail clients (e.g., Lotus Notes) have problems with long URLs.
  145. \note On Symbian OS, \c SwEvent capability is required to open the given \a url
  146. if the Web browser is already running.
  147. \sa setUrlHandler()
  148. */
  149. bool QDesktopServices::openUrl(const QUrl &url)
  150. {
  151. QOpenUrlHandlerRegistry *registry = handlerRegistry();
  152. QMutexLocker locker(&registry->mutex);
  153. static bool insideOpenUrlHandler = false;
  154. if (!insideOpenUrlHandler) {
  155. QOpenUrlHandlerRegistry::HandlerHash::ConstIterator handler = registry->handlers.constFind(url.scheme());
  156. if (handler != registry->handlers.constEnd()) {
  157. insideOpenUrlHandler = true;
  158. bool result = QMetaObject::invokeMethod(handler->receiver, handler->name.constData(), Qt::DirectConnection, Q_ARG(QUrl, url));
  159. insideOpenUrlHandler = false;
  160. return result; // ### support bool slot return type
  161. }
  162. }
  163. bool result;
  164. if (url.scheme() == QLatin1String("file"))
  165. result = openDocument(url);
  166. else
  167. result = launchWebBrowser(url);
  168. return result;
  169. }
  170. /*!
  171. Sets the handler for the given \a scheme to be the handler \a method provided by
  172. the \a receiver object.
  173. This function provides a way to customize the behavior of openUrl(). If openUrl()
  174. is called with a URL with the specified \a scheme then the given \a method on the
  175. \a receiver object is called instead of QDesktopServices launching an external
  176. application.
  177. The provided method must be implemented as a slot that only accepts a single QUrl
  178. argument.
  179. If setUrlHandler() is used to set a new handler for a scheme which already
  180. has a handler, the existing handler is simply replaced with the new one.
  181. Since QDesktopServices does not take ownership of handlers, no objects are
  182. deleted when a handler is replaced.
  183. Note that the handler will always be called from within the same thread that
  184. calls QDesktopServices::openUrl().
  185. \sa openUrl(), unsetUrlHandler()
  186. */
  187. void QDesktopServices::setUrlHandler(const QString &scheme, QObject *receiver, const char *method)
  188. {
  189. QOpenUrlHandlerRegistry *registry = handlerRegistry();
  190. QMutexLocker locker(&registry->mutex);
  191. if (!receiver) {
  192. registry->handlers.remove(scheme);
  193. return;
  194. }
  195. QOpenUrlHandlerRegistry::Handler h;
  196. h.receiver = receiver;
  197. h.name = method;
  198. registry->handlers.insert(scheme, h);
  199. QObject::connect(receiver, SIGNAL(destroyed(QObject*)),
  200. registry, SLOT(handlerDestroyed(QObject*)));
  201. }
  202. /*!
  203. Removes a previously set URL handler for the specified \a scheme.
  204. \sa setUrlHandler()
  205. */
  206. void QDesktopServices::unsetUrlHandler(const QString &scheme)
  207. {
  208. setUrlHandler(scheme, 0, 0);
  209. }
  210. /*!
  211. \enum QDesktopServices::StandardLocation
  212. \since 4.4
  213. This enum describes the different locations that can be queried by
  214. QDesktopServices::storageLocation and QDesktopServices::displayName.
  215. \value DesktopLocation Returns the user's desktop directory.
  216. \value DocumentsLocation Returns the user's document.
  217. \value FontsLocation Returns the user's fonts.
  218. \value ApplicationsLocation Returns the user's applications.
  219. \value MusicLocation Returns the users music.
  220. \value MoviesLocation Returns the user's movies.
  221. \value PicturesLocation Returns the user's pictures.
  222. \value TempLocation Returns the system's temporary directory.
  223. \value HomeLocation Returns the user's home directory.
  224. \value DataLocation Returns a directory location where persistent
  225. application data can be stored. QCoreApplication::applicationName
  226. and QCoreApplication::organizationName should work on all
  227. platforms.
  228. \value CacheLocation Returns a directory location where user-specific
  229. non-essential (cached) data should be written.
  230. \sa storageLocation() displayName()
  231. */
  232. /*!
  233. \fn QString QDesktopServices::storageLocation(StandardLocation type)
  234. \since 4.4
  235. Returns the default system directory where files of \a type belong, or an empty string
  236. if the location cannot be determined.
  237. \note The storage location returned can be a directory that does not exist; i.e., it
  238. may need to be created by the system or the user.
  239. \note On Symbian OS, ApplicationsLocation always point /sys/bin folder on the same drive
  240. with executable. FontsLocation always points to folder on ROM drive. Symbian OS does not
  241. have desktop concept, DesktopLocation returns same path as DocumentsLocation.
  242. Rest of the standard locations point to folder on same drive with executable, except
  243. that if executable is in ROM the folder from C drive is returned.
  244. */
  245. /*!
  246. \fn QString QDesktopServices::displayName(StandardLocation type)
  247. Returns a localized display name for the given location \a type or
  248. an empty QString if no relevant location can be found.
  249. */
  250. QT_END_NAMESPACE
  251. #include "qdesktopservices.moc"
  252. #endif // QT_NO_DESKTOPSERVICES