ProjectExportController.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <ProjectExportController.h>
  9. #include <ProjectExportWorker.h>
  10. #include <ProjectButtonWidget.h>
  11. #include <SettingsInterface.h>
  12. #include <QMessageBox>
  13. #include <QDebug>
  14. #include <QDesktopServices>
  15. #include <QUrl>
  16. namespace O3DE::ProjectManager
  17. {
  18. ProjectExportController::ProjectExportController(const ProjectInfo& projectInfo, ProjectButton* projectButton, QWidget* parent)
  19. : QObject()
  20. , m_projectInfo(projectInfo)
  21. , m_projectButton(projectButton)
  22. , m_parent(parent)
  23. {
  24. m_worker = new ProjectExportWorker(m_projectInfo);
  25. m_worker->moveToThread(&m_workerThread);
  26. connect(&m_workerThread, &QThread::finished, m_worker, &ProjectExportWorker::deleteLater);
  27. connect(&m_workerThread, &QThread::started, m_worker, &ProjectExportWorker::ExportProject);
  28. connect(m_worker, &ProjectExportWorker::Done, this, &ProjectExportController::HandleResults);
  29. connect(m_worker, &ProjectExportWorker::UpdateProgress, this, &ProjectExportController::UpdateUIProgress);
  30. }
  31. ProjectExportController::~ProjectExportController()
  32. {
  33. m_workerThread.requestInterruption();
  34. m_workerThread.quit();
  35. m_workerThread.wait();
  36. }
  37. void ProjectExportController::Start()
  38. {
  39. m_workerThread.start();
  40. UpdateUIProgress("");
  41. }
  42. void ProjectExportController::SetProjectButton(ProjectButton* projectButton)
  43. {
  44. m_projectButton = projectButton;
  45. if (projectButton)
  46. {
  47. projectButton->SetProjectButtonAction(tr("Cancel"), [this] { HandleCancel(); });
  48. AZ::Outcome<QString, QString> logFilePathQuery = m_worker->GetLogFilePath();
  49. if (logFilePathQuery.IsSuccess())
  50. {
  51. projectButton->SetBuildLogsLink(QUrl::fromLocalFile(logFilePathQuery.GetValue()));
  52. }
  53. projectButton->SetState(ProjectButtonState::Exporting);
  54. if (!m_lastLine.isEmpty())
  55. {
  56. UpdateUIProgress(m_lastLine);
  57. }
  58. }
  59. }
  60. const ProjectInfo& ProjectExportController::GetProjectInfo() const
  61. {
  62. return m_projectInfo;
  63. }
  64. void ProjectExportController::UpdateUIProgress(const QString& lastLine)
  65. {
  66. m_lastLine = lastLine.left(s_maxDisplayedBuiltOutputChars);
  67. if (m_projectButton)
  68. {
  69. m_projectButton->SetContextualText(m_lastLine);
  70. }
  71. }
  72. void ProjectExportController::HandleResults(const QString& result)
  73. {
  74. bool success = true;
  75. if (!result.isEmpty())
  76. {
  77. AZ::Outcome<QString, QString> logFilePathQuery = m_worker->GetLogFilePath();
  78. if (result.contains(tr("log")))
  79. {
  80. QMessageBox::StandardButton openLog = QMessageBox::critical(
  81. m_parent,
  82. tr(LauncherExportFailedMessage),
  83. result + tr("\n\nWould you like to view log?"),
  84. QMessageBox::No | QMessageBox::Yes);
  85. if (openLog == QMessageBox::Yes)
  86. {
  87. // Open application assigned to this file type
  88. if (!logFilePathQuery.IsSuccess())
  89. {
  90. qDebug() << "Failed to retrieve desired log file path" << "\n";
  91. }
  92. else if (!QDesktopServices::openUrl(QUrl::fromLocalFile(logFilePathQuery.GetValue())))
  93. {
  94. qDebug() << "QDesktopServices::openUrl failed to open " << m_projectInfo.m_logUrl.toString() << "\n";
  95. }
  96. }
  97. if (logFilePathQuery.IsSuccess())
  98. {
  99. m_projectInfo.m_logUrl = QUrl::fromLocalFile(logFilePathQuery.GetValue());
  100. }
  101. }
  102. else
  103. {
  104. if (logFilePathQuery.IsSuccess())
  105. {
  106. QMessageBox::critical(m_parent,
  107. QString("%1\nYou can check the logs in the following directory:\n%2")
  108. .arg(LauncherExportFailedMessage)
  109. .arg(logFilePathQuery.GetValue()),
  110. result);
  111. }
  112. else
  113. {
  114. QMessageBox::critical(m_parent,
  115. QString("%1\nNo logs are available at this time. Unable to create the folders to hold the logs.\n%2")
  116. .arg(LauncherExportFailedMessage)
  117. .arg(logFilePathQuery.GetError()),
  118. result);
  119. }
  120. }
  121. success = false;
  122. }
  123. else
  124. {
  125. AZ::Outcome<QString, QString> expectedOutputPathQuery = m_worker->GetExpectedOutputPath();
  126. if (expectedOutputPathQuery.IsSuccess())
  127. {
  128. //Export was successful, show the expected output path
  129. QMessageBox::StandardButton openOutputDir = QMessageBox::information(
  130. m_parent,
  131. tr("Project exported successfully!"),
  132. tr("Would you like to view the exported files?"),
  133. QMessageBox::No | QMessageBox::Yes);
  134. if (openOutputDir == QMessageBox::Yes)
  135. {
  136. QString expectedOutputPath = expectedOutputPathQuery.GetValue();
  137. if (!QDesktopServices::openUrl(QUrl::fromLocalFile(expectedOutputPath)))
  138. {
  139. qDebug() << "QDesktopServices::openUrl failed to open " << expectedOutputPath << "\n";
  140. }
  141. }
  142. }
  143. else
  144. {
  145. qDebug() << "Failed to retrieve output path from recent export task:\n" << expectedOutputPathQuery.GetError() << "\n";
  146. }
  147. }
  148. emit Done(success);
  149. }
  150. void ProjectExportController::HandleCancel()
  151. {
  152. m_workerThread.quit();
  153. emit Done(false);
  154. }
  155. } // namespace O3DE::ProjectManager