PageRenderTime 88ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C++ | 88 lines | 63 code | 5 blank | 20 comment | 4 complexity | 501adb005c47fa2bcd7b2fdb45521994 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, GPL-2.0, AGPL-1.0, LGPL-2.1, GPL-3.0, AGPL-3.0
  1. /*
  2. * %kadu copyright begin%
  3. * Copyright 2010 Piotr Galiszewski (piotr.galiszewski@kadu.im)
  4. * Copyright 2010 Bartosz Brachaczek (b.brachaczek@gmail.com)
  5. * Copyright 2010 Rafa?‚ 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 <QtCore/QProcess>
  22. #include <QtCore/QUrl>
  23. #include <QtGui/QApplication>
  24. #include <QtGui/QDesktopServices>
  25. #include "configuration/configuration-file.h"
  26. #include "gui/windows/message-dialog.h"
  27. #include "url-opener.h"
  28. bool UrlOpener::openUrl(const QByteArray &urlForDesktopServices, const QByteArray &urlForApplication, const QString &application)
  29. {
  30. if (!application.isEmpty())
  31. {
  32. QProcess *process = new QProcess(qApp);
  33. QString launchLine = application;
  34. if (!launchLine.contains("%1"))
  35. launchLine.append(" \"" + QString::fromUtf8(urlForApplication) + '"');
  36. else
  37. launchLine.replace("%1", QString::fromUtf8(urlForApplication));
  38. process->start(launchLine);
  39. if (process->waitForStarted())
  40. return true;
  41. }
  42. return QDesktopServices::openUrl(QUrl::fromEncoded(urlForDesktopServices));
  43. }
  44. void UrlOpener::openUrl(const QByteArray &url)
  45. {
  46. QString browser;
  47. bool useDefaultWebBrowser = config_file.readBoolEntry("Chat", "UseDefaultWebBrowser", true);
  48. if (!useDefaultWebBrowser)
  49. browser = config_file.readEntry("Chat", "WebBrowser");
  50. if (!openUrl(url, url, browser))
  51. MessageDialog::show(KaduIcon("dialog-error"), qApp->translate("@default", QT_TR_NOOP("Kadu")),
  52. qApp->translate("@default", QT_TR_NOOP("Could not spawn Web browser process. Check if the Web browser is functional")));
  53. }
  54. void UrlOpener::openEmail(const QByteArray &email)
  55. {
  56. QString client;
  57. bool useDefaultEMailClient = config_file.readBoolEntry("Chat", "UseDefaultEMailClient", true);
  58. if (useDefaultEMailClient)
  59. client = config_file.readEntry("Chat", "MailClient");
  60. QByteArray urlForDesktopServices;
  61. QByteArray urlForApplication;
  62. if (email.startsWith("mailto:"))
  63. {
  64. urlForDesktopServices = email;
  65. urlForApplication = email;
  66. urlForApplication.remove(0, 7);
  67. }
  68. else
  69. {
  70. urlForDesktopServices = "mailto:" + email;
  71. urlForApplication = email;
  72. }
  73. if (!openUrl(urlForDesktopServices, urlForApplication, client))
  74. MessageDialog::show(KaduIcon("dialog-error"), qApp->translate("@default", QT_TR_NOOP("Kadu")),
  75. qApp->translate("@default", QT_TR_NOOP("Could not spawn Mail client process. Check if the Mail client is functional")));
  76. }