/src/gui/operations/HandleUploadResultOperation.cpp

https://github.com/ksnip/ksnip · C++ · 139 lines · 102 code · 18 blank · 19 comment · 12 complexity · 43d338e2b883b50177d959dc8d907643 MD5 · raw file

  1. /*
  2. * Copyright (C) 2019 Damir Porobic <damir.porobic@gmx.com>
  3. *
  4. * This program 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 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. */
  19. #include "HandleUploadResultOperation.h"
  20. HandleUploadResultOperation::HandleUploadResultOperation(const UploadResult &result, TrayIcon *trayIcon) :
  21. mUploadResult(result),
  22. mTrayIcon(trayIcon),
  23. mConfig(KsnipConfigProvider::instance()),
  24. mClipboard(QApplication::clipboard())
  25. {
  26. }
  27. bool HandleUploadResultOperation::execute()
  28. {
  29. switch (mUploadResult.type) {
  30. case UploaderType::Imgur:
  31. handleImgurResult();
  32. break;
  33. case UploaderType::Script:
  34. handleScriptResult();
  35. break;
  36. }
  37. return mUploadResult.status == UploadStatus::NoError;
  38. }
  39. void HandleUploadResultOperation::handleImgurResult()
  40. {
  41. if(mUploadResult.status == UploadStatus::NoError) {
  42. if (mConfig->imgurOpenLinkInBrowser()) {
  43. OpenUrl(mUploadResult.content);
  44. }
  45. if (mConfig->imgurAlwaysCopyToClipboard()) {
  46. copyToClipboard(mUploadResult.content);
  47. }
  48. notifyImgurSuccessfulUpload(mUploadResult.content);
  49. } else {
  50. handleUploadError();
  51. }
  52. }
  53. void HandleUploadResultOperation::handleScriptResult()
  54. {
  55. if(mUploadResult.status == UploadStatus::NoError) {
  56. if (mConfig->uploadScriptCopyOutputToClipboard()) {
  57. copyToClipboard(mUploadResult.content);
  58. }
  59. notifyScriptSuccessfulUpload();
  60. } else {
  61. handleUploadError();
  62. }
  63. }
  64. void HandleUploadResultOperation::notifyScriptSuccessfulUpload() const
  65. {
  66. NotifyOperation operation(mTrayIcon, tr("Upload Successful"), tr("Upload script ") + mConfig->uploadScriptPath() + tr(" finished successfully."), NotificationTypes::Information);
  67. operation.execute();
  68. }
  69. void HandleUploadResultOperation::notifyImgurSuccessfulUpload(const QString &url) const
  70. {
  71. NotifyOperation operation(mTrayIcon, tr("Upload Successful"), tr("Uploaded to") + QLatin1Literal(" ") + url, url, NotificationTypes::Information);
  72. operation.execute();
  73. }
  74. void HandleUploadResultOperation::copyToClipboard(const QString &url) const
  75. {
  76. mClipboard->setText(url);
  77. }
  78. void HandleUploadResultOperation::OpenUrl(const QString &url) const
  79. {
  80. QDesktopServices::openUrl(url);
  81. }
  82. void HandleUploadResultOperation::handleUploadError()
  83. {
  84. switch (mUploadResult.status) {
  85. case UploadStatus::NoError:
  86. // Nothing to report all good
  87. break;
  88. case UploadStatus::UnableToSaveTemporaryImage:
  89. notifyFailedUpload(tr("Unable to save temporary image for upload."));
  90. break;
  91. case UploadStatus::FailedToStart:
  92. notifyFailedUpload(tr("Unable to start process, check path and permissions."));
  93. break;
  94. case UploadStatus::Crashed:
  95. notifyFailedUpload(tr("Process crashed"));
  96. break;
  97. case UploadStatus::Timedout:
  98. notifyFailedUpload(tr("Process timed out."));
  99. break;
  100. case UploadStatus::ReadError:
  101. notifyFailedUpload(tr("Process read error."));
  102. break;
  103. case UploadStatus::WriteError:
  104. notifyFailedUpload(tr("Process write error."));
  105. break;
  106. case UploadStatus::WebError:
  107. notifyFailedUpload(tr("Web error, check console output."));
  108. break;
  109. case UploadStatus::UnknownError:
  110. notifyFailedUpload(tr("Unknown process error."));
  111. break;
  112. case UploadStatus::ScriptWroteToStdErr:
  113. notifyFailedUpload(tr("Script wrote to StdErr."));
  114. break;
  115. }
  116. }
  117. void HandleUploadResultOperation::notifyFailedUpload(const QString &message) const
  118. {
  119. NotifyOperation operation(mTrayIcon, tr("Upload Failed"), message, NotificationTypes::Warning);
  120. operation.execute();
  121. }