* @brief Worker handed back the global project JSON -- finish the restore on the main thread. */
| 1081 | * @brief Worker handed back the global project JSON -- finish the restore on the main thread. |
| 1082 | */ |
| 1083 | void Sessions::DatabaseManager::runRestoreProjectFromJson(const QString& json) |
| 1084 | { |
| 1085 | if (json.isEmpty()) { |
| 1086 | Misc::Utilities::showMessageBox(tr("No project data"), |
| 1087 | tr("This session file does not contain an embedded project."), |
| 1088 | QMessageBox::Warning); |
| 1089 | return; |
| 1090 | } |
| 1091 | |
| 1092 | QJsonParseError parseError{}; |
| 1093 | const auto doc = QJsonDocument::fromJson(json.toUtf8(), &parseError); |
| 1094 | if (parseError.error != QJsonParseError::NoError || doc.isEmpty()) { |
| 1095 | Misc::Utilities::showMessageBox( |
| 1096 | tr("Invalid project data"), |
| 1097 | tr("The embedded project JSON is malformed and cannot be restored."), |
| 1098 | QMessageBox::Critical); |
| 1099 | return; |
| 1100 | } |
| 1101 | |
| 1102 | const auto title = doc.object().value("title").toString("Restored Project"); |
| 1103 | const auto projectsDir = DataModel::ProjectModel::instance().jsonProjectsPath(); |
| 1104 | const auto suggested = QStringLiteral("%1/%2.ssproj").arg(projectsDir, title); |
| 1105 | |
| 1106 | const auto path = QFileDialog::getSaveFileName( |
| 1107 | nullptr, tr("Restore Project"), suggested, tr("Serial Studio projects (*.ssproj *.json)")); |
| 1108 | |
| 1109 | if (path.isEmpty()) |
| 1110 | return; |
| 1111 | |
| 1112 | QFile file(path); |
| 1113 | if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { |
| 1114 | Misc::Utilities::showMessageBox( |
| 1115 | tr("Cannot write file"), tr("Check file permissions and try again."), QMessageBox::Critical); |
| 1116 | return; |
| 1117 | } |
| 1118 | |
| 1119 | file.write(doc.toJson(QJsonDocument::Indented)); |
| 1120 | file.close(); |
| 1121 | |
| 1122 | AppState::instance().setOperationMode(SerialStudio::ProjectFile); |
| 1123 | DataModel::ProjectModel::instance().openJsonFile(path); |
| 1124 | |
| 1125 | Q_EMIT projectMetadataRestored(); |
| 1126 | } |
| 1127 | |
| 1128 | /** |
| 1129 | * @brief Reopens the database that was open during the last application run. |
nothing calls this directly
no test coverage detected