PageRenderTime 68ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/desktoputil.cpp

https://github.com/pontus/psilibnotify
C++ | 118 lines | 62 code | 10 blank | 46 comment | 9 complexity | 2fb9447b00f55385fe8706d713a1845f MD5 | raw file
Possible License(s): AGPL-1.0
  1. /*
  2. * desktoputil.cpp - url-opening routines
  3. * Copyright (C) 2007 Maciej Niedzielski, Michail Pishchagin
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include "desktoputil.h"
  21. #include <QDesktopServices>
  22. #include <QUrl>
  23. #include <QSettings>
  24. #include <QFileInfo>
  25. #include <QProcess>
  26. #include <QSysInfo>
  27. #include <QSet>
  28. #ifdef Q_WS_WIN
  29. #include <windows.h>
  30. QString defaultBrowser()
  31. {
  32. QSettings settings("HKEY_CLASSES_ROOT\\HTTP\\shell\\open\\command", QSettings::NativeFormat);
  33. QString command = settings.value(".").toString();
  34. QRegExp rx("\"(.+)\"");
  35. if (rx.indexIn(command) != -1)
  36. return rx.capturedTexts()[1];
  37. return command;
  38. }
  39. #endif
  40. static QSet<QString> handlers;
  41. static bool doOpenUrl(const QUrl& url)
  42. {
  43. #ifdef Q_WS_WIN
  44. if (!handlers.contains(url.scheme())) {
  45. // on Vista it always returns iexplore.exe as default browser
  46. bool oldStyleDefaultBrowserInfo = QSysInfo::WindowsVersion < QSysInfo::WV_VISTA;
  47. QFileInfo browserFileInfo(defaultBrowser());
  48. if (oldStyleDefaultBrowserInfo && browserFileInfo.fileName() == "iexplore.exe") {
  49. return QProcess::startDetached(browserFileInfo.absoluteFilePath(),
  50. QStringList() << "-new" << url.toEncoded());
  51. }
  52. else {
  53. // FIXME: This is necessary for Qt 4.3.3 to handle all URLs correctly
  54. QT_WA(
  55. ShellExecuteW(0, 0, (WCHAR *)QString(url.toEncoded()).utf16(), 0, 0, SW_SHOWNORMAL);
  56. ,
  57. QByteArray a = QString(url.toEncoded()).toLocal8Bit(); // must not call constData() of a temp object
  58. ShellExecuteA(0, 0, (CHAR *)a.constData(), 0, 0, SW_SHOWNORMAL);
  59. )
  60. return true;
  61. }
  62. }
  63. #endif
  64. return QDesktopServices::openUrl(url);
  65. }
  66. /**
  67. * \brief Opens URL using OS default handler
  68. * \param url the url to open
  69. *
  70. * \a url may be either percent encoded or not.
  71. * If it contains only ASCII characters, it is decoded,
  72. * else it is converted to QUrl in QUrl::TolerantMode mode.
  73. * Resulting QUrl object is passed to QDesktopServices::openUrl().
  74. *
  75. * \sa QDesktopServices::openUrl()
  76. */
  77. bool DesktopUtil::openUrl(const QString& url)
  78. {
  79. QByteArray ascii = url.toAscii();
  80. if (ascii == url)
  81. return doOpenUrl(QUrl::fromEncoded(ascii));
  82. else
  83. return doOpenUrl(QUrl(url, QUrl::TolerantMode));
  84. }
  85. /**
  86. * \brief Sets the handler for the given \a scheme to be the \a handler method provided by the \a receiver object.
  87. *
  88. * Handler is set in QDesktopServices and DesktopUtil will always use QDesktopServices to open URLs of this type.
  89. *
  90. * Note: Always manage handlers via DesktopUtil. Using QDestopServices directly may lead to unexpected behavior.
  91. */
  92. void DesktopUtil::setUrlHandler(const QString& scheme, QObject* receiver, const char* method)
  93. {
  94. QDesktopServices::setUrlHandler(scheme, receiver, method);
  95. handlers.insert(scheme);
  96. }
  97. /**
  98. * Removes a previously set URL handler for the specified \a scheme.
  99. *
  100. * Handler is unset in QDesktopServices.
  101. *
  102. * Note: Always manage handlers via DesktopUtil. Using QDestopServices directly may lead to unexpected behavior.
  103. */
  104. void DesktopUtil::unsetUrlHandler(const QString& scheme)
  105. {
  106. handlers.remove(scheme);
  107. QDesktopServices::unsetUrlHandler(scheme);
  108. }