LogUploadDialog.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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(QTStr("LogUploadDialog.Labels.PrivacyNotice"));
  37. if (uploadType_ == LogFileType::CrashLog) {
  38. ui->analyzeURL->hide();
  39. ui->description->setText(Str("LogUploadDialog.Labels.Description.CrashLog"));
  40. }
  41. connect(ui->confirmUploadButton, &QPushButton::clicked, this, &LogUploadDialog::startLogUpload);
  42. connect(ui->retryButton, &QPushButton::clicked, this, &LogUploadDialog::startLogUpload);
  43. connect(ui->copyURL, &QPushButton::clicked, this, &LogUploadDialog::copyToClipBoard);
  44. connect(ui->analyzeURL, &QPushButton::clicked, this, &LogUploadDialog::openAnalyzeURL);
  45. connect(ui->closeButton, &QPushButton::clicked, this, &QDialog::reject);
  46. connect(ui->retryCloseButton, &QPushButton::clicked, this, &QDialog::reject);
  47. OBSApp *app = App();
  48. connect(app, &OBSApp::logUploadFinished, this, &LogUploadDialog::handleUploadSuccess);
  49. connect(app, &OBSApp::logUploadFailed, this, &LogUploadDialog::handleUploadFailure);
  50. installEventFilter(CreateShortcutFilter());
  51. LogFileState uploadState = app->getLogFileState(uploadType);
  52. switch (uploadState) {
  53. case LogFileState::Uploaded:
  54. startLogUpload();
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. void LogUploadDialog::startLogUpload()
  61. {
  62. if (uploadType_ == LogFileType::NoType) {
  63. return;
  64. }
  65. ui->confirmUploadButton->setEnabled(false);
  66. connect(uploadStatusTimer_.get(), &QTimer::timeout, this, [this]() {
  67. ui->uploadProgress->setText(Str("LogUploadDialog.Labels.Progress"));
  68. setCursor(Qt::WaitCursor);
  69. });
  70. uploadStatusTimer_->setSingleShot(true);
  71. uploadStatusTimer_->start(1000);
  72. OBSApp *app = App();
  73. switch (uploadType_) {
  74. case LogFileType::CrashLog:
  75. app->uploadLastCrashLog();
  76. break;
  77. case LogFileType::CurrentAppLog:
  78. app->uploadCurrentAppLog();
  79. break;
  80. case LogFileType::LastAppLog:
  81. app->uploadLastAppLog();
  82. break;
  83. default:
  84. break;
  85. }
  86. }
  87. void LogUploadDialog::handleUploadSuccess(LogFileType, const QString &fileURL)
  88. {
  89. uploadStatusTimer_->stop();
  90. unsetCursor();
  91. ui->confirmUploadButton->setEnabled(true);
  92. ui->uploadProgress->setText("");
  93. ui->urlEdit->setText(fileURL);
  94. ui->stackedWidget->setCurrentIndex(DialogPage::Success);
  95. }
  96. void LogUploadDialog::handleUploadFailure(LogFileType, const QString &errorMessage)
  97. {
  98. uploadStatusTimer_->stop();
  99. unsetCursor();
  100. ui->confirmUploadButton->setEnabled(true);
  101. ui->uploadProgress->setText("");
  102. QString errorDescription = QTStr("LogUploadDialog.Errors.Template").arg(errorMessage);
  103. ui->uploadErrorMessage->setText(errorDescription);
  104. ui->stackedWidget->setCurrentIndex(DialogPage::Error);
  105. }
  106. void LogUploadDialog::copyToClipBoard() const
  107. {
  108. QClipboard *clipboard = QApplication::clipboard();
  109. clipboard->setText(ui->urlEdit->text());
  110. }
  111. void LogUploadDialog::openAnalyzeURL() const
  112. {
  113. QUrlQuery queryParameters;
  114. queryParameters.addQueryItem("log_url", QUrl::toPercentEncoding(ui->urlEdit->text()));
  115. QUrl analyzerUrl = QUrl("https://obsproject.com/tools/analyzer", QUrl::TolerantMode);
  116. analyzerUrl.setQuery(queryParameters);
  117. QDesktopServices::openUrl(analyzerUrl);
  118. }
  119. } // namespace OBS