LogUploadDialog.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "LogUploadDialog.hpp"
  15. #include <OBSApp.hpp>
  16. #include <QClipboard>
  17. #include <QDesktopServices>
  18. #include <QTimer>
  19. #include <QUrlQuery>
  20. #include "moc_LogUploadDialog.cpp"
  21. struct DialogPage {
  22. static constexpr int Start = 0;
  23. static constexpr int Success = 1;
  24. static constexpr int Error = 2;
  25. };
  26. namespace OBS {
  27. LogUploadDialog::LogUploadDialog(QWidget *parent, LogFileType uploadType)
  28. : QDialog(parent),
  29. ui(new Ui::LogUploadDialog),
  30. uploadType_(uploadType)
  31. {
  32. uploadStatusTimer_ = std::make_unique<QTimer>(this);
  33. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  34. ui->setupUi(this);
  35. ui->stackedWidget->setCurrentIndex(DialogPage::Start);
  36. ui->privacyNotice->setText(
  37. QTStr("LogUploadDialog.Labels.PrivacyNotice").arg(QTStr("LogUploadDialog.Buttons.ConfirmUpload")));
  38. if (uploadType_ == LogFileType::CrashLog) {
  39. ui->analyzeURL->hide();
  40. ui->description->setText(Str("LogUploadDialog.Labels.Description.CrashLog"));
  41. }
  42. connect(ui->confirmUploadButton, &QPushButton::clicked, this, &LogUploadDialog::startLogUpload);
  43. connect(ui->retryButton, &QPushButton::clicked, this, &LogUploadDialog::startLogUpload);
  44. connect(ui->copyURL, &QPushButton::clicked, this, &LogUploadDialog::copyToClipBoard);
  45. connect(ui->analyzeURL, &QPushButton::clicked, this, &LogUploadDialog::openAnalyzeURL);
  46. connect(ui->closeButton, &QPushButton::clicked, this, &QDialog::reject);
  47. connect(ui->retryCloseButton, &QPushButton::clicked, this, &QDialog::reject);
  48. OBSApp *app = App();
  49. connect(app, &OBSApp::logUploadFinished, this, &LogUploadDialog::handleUploadSuccess);
  50. connect(app, &OBSApp::logUploadFailed, this, &LogUploadDialog::handleUploadFailure);
  51. installEventFilter(CreateShortcutFilter());
  52. LogFileState uploadState = app->getLogFileState(uploadType);
  53. switch (uploadState) {
  54. case LogFileState::Uploaded:
  55. startLogUpload();
  56. break;
  57. default:
  58. break;
  59. }
  60. }
  61. void LogUploadDialog::startLogUpload()
  62. {
  63. if (uploadType_ == LogFileType::NoType) {
  64. return;
  65. }
  66. ui->confirmUploadButton->setEnabled(false);
  67. connect(uploadStatusTimer_.get(), &QTimer::timeout, this, [this]() {
  68. ui->uploadProgress->setText(Str("LogUploadDialog.Labels.Progress"));
  69. setCursor(Qt::WaitCursor);
  70. });
  71. uploadStatusTimer_->setSingleShot(true);
  72. uploadStatusTimer_->start(1000);
  73. OBSApp *app = App();
  74. switch (uploadType_) {
  75. case LogFileType::CrashLog:
  76. app->uploadLastCrashLog();
  77. break;
  78. case LogFileType::CurrentAppLog:
  79. app->uploadCurrentAppLog();
  80. break;
  81. case LogFileType::LastAppLog:
  82. app->uploadLastAppLog();
  83. break;
  84. default:
  85. break;
  86. }
  87. }
  88. void LogUploadDialog::handleUploadSuccess(LogFileType, const QString &fileURL)
  89. {
  90. uploadStatusTimer_->stop();
  91. unsetCursor();
  92. ui->confirmUploadButton->setEnabled(true);
  93. ui->uploadProgress->setText("");
  94. ui->urlEdit->setText(fileURL);
  95. ui->stackedWidget->setCurrentIndex(DialogPage::Success);
  96. }
  97. void LogUploadDialog::handleUploadFailure(LogFileType, const QString &errorMessage)
  98. {
  99. uploadStatusTimer_->stop();
  100. unsetCursor();
  101. ui->confirmUploadButton->setEnabled(true);
  102. ui->uploadProgress->setText("");
  103. QString errorDescription = QTStr("LogUploadDialog.Errors.Template").arg(errorMessage);
  104. ui->uploadErrorMessage->setText(errorDescription);
  105. ui->stackedWidget->setCurrentIndex(DialogPage::Error);
  106. }
  107. void LogUploadDialog::copyToClipBoard() const
  108. {
  109. QClipboard *clipboard = QApplication::clipboard();
  110. clipboard->setText(ui->urlEdit->text());
  111. }
  112. void LogUploadDialog::openAnalyzeURL() const
  113. {
  114. QUrlQuery queryParameters;
  115. queryParameters.addQueryItem("log_url", QUrl::toPercentEncoding(ui->urlEdit->text()));
  116. QUrl analyzerUrl = QUrl("https://obsproject.com/tools/analyzer", QUrl::TolerantMode);
  117. analyzerUrl.setQuery(queryParameters);
  118. QDesktopServices::openUrl(analyzerUrl);
  119. }
  120. } // namespace OBS