PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/qtbase/src/gui/util/qdesktopservices.cpp

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