PageRenderTime 26ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/kadu-core/os/generic/url-opener.cpp

https://github.com/vogel/kadu
C++ | 117 lines | 88 code | 9 blank | 20 comment | 4 complexity | 016311f0c42b658c30c6baeef80a3ce8 MD5 | raw file
Possible License(s): CC-BY-3.0
  1. /*
  2. * %kadu copyright begin%
  3. * Copyright 2010, 2011 Piotr Galiszewski (piotr.galiszewski@kadu.im)
  4. * Copyright 2010, 2011, 2014 Bartosz Brachaczek (b.brachaczek@gmail.com)
  5. * Copyright 2010, 2011, 2013, 2014 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
  6. * %kadu copyright end%
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "url-opener.h"
  22. #include "url-opener.moc"
  23. #include "configuration/configuration.h"
  24. #include "configuration/deprecated-configuration-api.h"
  25. #include "icons/icons-manager.h"
  26. #include "windows/message-dialog.h"
  27. #include <QtCore/QProcess>
  28. #include <QtCore/QUrl>
  29. #include <QtGui/QDesktopServices>
  30. #include <QtWidgets/QApplication>
  31. UrlOpener::UrlOpener(QObject *parent) : QObject{parent}
  32. {
  33. }
  34. UrlOpener::~UrlOpener()
  35. {
  36. }
  37. void UrlOpener::setConfiguration(Configuration *configuration)
  38. {
  39. m_configuration = configuration;
  40. }
  41. void UrlOpener::setIconsManager(IconsManager *iconsManager)
  42. {
  43. m_iconsManager = iconsManager;
  44. }
  45. bool UrlOpener::openUrl(
  46. const QByteArray &urlForDesktopServices, const QByteArray &urlForApplication, const QString &application)
  47. {
  48. if (!application.isEmpty())
  49. {
  50. QProcess *process = new QProcess(qApp);
  51. QString launchLine = application;
  52. if (!launchLine.contains("%1"))
  53. launchLine.append(" \"" + QString::fromUtf8(urlForApplication) + '"');
  54. else
  55. launchLine.replace("%1", QString::fromUtf8(urlForApplication));
  56. process->start(launchLine);
  57. if (process->waitForStarted())
  58. return true;
  59. }
  60. return QDesktopServices::openUrl(QUrl::fromEncoded(urlForDesktopServices));
  61. }
  62. void UrlOpener::openUrl(const QByteArray &url)
  63. {
  64. QString browser;
  65. bool useDefaultWebBrowser = m_configuration->deprecatedApi()->readBoolEntry("Chat", "UseDefaultWebBrowser", true);
  66. if (!useDefaultWebBrowser)
  67. browser = m_configuration->deprecatedApi()->readEntry("Chat", "WebBrowser");
  68. if (!openUrl(url, url, browser))
  69. MessageDialog::show(
  70. m_iconsManager->iconByPath(KaduIcon("dialog-error")),
  71. QCoreApplication::translate("@default", QT_TR_NOOP("Kadu")),
  72. QCoreApplication::translate(
  73. "@default", QT_TR_NOOP("Could not spawn Web browser process. Check if the Web browser is functional")));
  74. }
  75. void UrlOpener::openEmail(const QByteArray &email)
  76. {
  77. QString client;
  78. bool useDefaultEMailClient = m_configuration->deprecatedApi()->readBoolEntry("Chat", "UseDefaultEMailClient", true);
  79. if (!useDefaultEMailClient)
  80. client = m_configuration->deprecatedApi()->readEntry("Chat", "MailClient");
  81. QByteArray urlForDesktopServices;
  82. QByteArray urlForApplication;
  83. if (email.startsWith("mailto:"))
  84. {
  85. urlForDesktopServices = email;
  86. urlForApplication = email;
  87. urlForApplication.remove(0, 7);
  88. }
  89. else
  90. {
  91. urlForDesktopServices = "mailto:" + email;
  92. urlForApplication = email;
  93. }
  94. if (!openUrl(urlForDesktopServices, urlForApplication, client))
  95. MessageDialog::show(
  96. m_iconsManager->iconByPath(KaduIcon("dialog-error")),
  97. QCoreApplication::translate("@default", QT_TR_NOOP("Kadu")),
  98. QCoreApplication::translate(
  99. "@default", QT_TR_NOOP("Could not spawn Mail client process. Check if the Mail client is functional")));
  100. }