PageRenderTime 71ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/actions/system/src/actions/openurlinstance.hpp

https://github.com/Jmgr/actionaz
C++ Header | 78 lines | 44 code | 15 blank | 19 comment | 5 complexity | ef59a00bad280b139839e29f1dda8f36 MD5 | raw file
  1. /*
  2. Actiona
  3. Copyright (C) 2005 Jonathan Mercier-Ganady
  4. Actiona is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Actiona is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. Contact: jmgr@jmgr.info
  15. */
  16. #pragma once
  17. #include "actiontools/actioninstance.hpp"
  18. #include "actiontools/script.hpp"
  19. #include <QUrl>
  20. #include <QDesktopServices>
  21. namespace Actions
  22. {
  23. class OpenURLInstance : public ActionTools::ActionInstance
  24. {
  25. Q_OBJECT
  26. public:
  27. enum Exceptions
  28. {
  29. FailedToOpenURL = ActionTools::ActionException::UserException
  30. };
  31. OpenURLInstance(const ActionTools::ActionDefinition *definition, QObject *parent = nullptr)
  32. : ActionTools::ActionInstance(definition, parent)
  33. {
  34. }
  35. void startExecution() override
  36. {
  37. bool ok = true;
  38. QString urlString = evaluateString(ok, QStringLiteral("url"));
  39. if(!ok)
  40. return;
  41. QUrl url(urlString, QUrl::TolerantMode);
  42. if(!url.isValid())
  43. {
  44. emit executionException(FailedToOpenURL, tr("Failed to open URL"));
  45. return;
  46. }
  47. if(url.scheme() == QString())
  48. url = QUrl(QStringLiteral("http://") + urlString, QUrl::TolerantMode);
  49. if(!QDesktopServices::openUrl(url))
  50. {
  51. emit executionException(FailedToOpenURL, tr("Failed to open URL"));
  52. return;
  53. }
  54. executionEnded();
  55. }
  56. private:
  57. Q_DISABLE_COPY(OpenURLInstance)
  58. };
  59. }