From 0dbff2ece5539a2272480e6c13aa678c194ec612 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 22 May 2023 12:07:18 +0200 Subject: [PATCH] Separate session saving from project manager Move the generic parts to session manager and let the project manager save its parts separately via SessionManager::setSessionValue. Change-Id: Iec2e81ea2488a15efc7333adb2b327761afdf274 Reviewed-by: hjk Reviewed-by: Christian Kandeler Reviewed-by: Qt CI Bot --- .../projectexplorer/projectexplorer.cpp | 6 +- .../projectexplorer/projectmanager.cpp | 105 +++++------------- src/plugins/projectexplorer/projectmanager.h | 1 - src/plugins/projectexplorer/session.cpp | 66 ++++++++++- src/plugins/projectexplorer/session.h | 1 + 5 files changed, 98 insertions(+), 81 deletions(-) diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index 1244dbd1ba2..e75e23d18f9 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -1633,7 +1633,7 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er dd, &ProjectExplorerPluginPrivate::savePersistentSettings); connect(EditorManager::instance(), &EditorManager::autoSaved, this, [] { if (!dd->m_shuttingDown && !SessionManager::loadingSession()) - ProjectManager::save(); + SessionManager::saveSession(); }); connect(qApp, &QApplication::applicationStateChanged, this, [](Qt::ApplicationState state) { if (!dd->m_shuttingDown && state == Qt::ApplicationActive) @@ -2212,7 +2212,7 @@ void ProjectExplorerPlugin::openNewProjectDialog() void ProjectExplorerPluginPrivate::showSessionManager() { - ProjectManager::save(); + SessionManager::saveSession(); SessionDialog sessionDialog(ICore::dialogParent()); sessionDialog.setAutoLoadSession(sb_d->isAutoRestoreLastSession()); sessionDialog.exec(); @@ -2258,7 +2258,7 @@ void ProjectExplorerPluginPrivate::savePersistentSettings() for (Project *pro : ProjectManager::projects()) pro->saveSettings(); - ProjectManager::save(); + SessionManager::saveSession(); } QtcSettings *s = ICore::settings(); diff --git a/src/plugins/projectexplorer/projectmanager.cpp b/src/plugins/projectexplorer/projectmanager.cpp index ccfa95a289a..ff5b214b10a 100644 --- a/src/plugins/projectexplorer/projectmanager.cpp +++ b/src/plugins/projectexplorer/projectmanager.cpp @@ -53,6 +53,7 @@ class ProjectManagerPrivate { public: void loadSession(); + void saveSession(); void restoreDependencies(); void restoreStartupProject(); void restoreProjects(const FilePaths &fileList); @@ -109,6 +110,9 @@ ProjectManager::ProjectManager() connect(SessionManager::instance(), &SessionManager::aboutToLoadSession, this, [] { d->loadSession(); }); + connect(SessionManager::instance(), &SessionManager::aboutToSaveSession, this, [] { + d->saveSession(); + }); } ProjectManager::~ProjectManager() @@ -315,87 +319,38 @@ void ProjectManager::removeProject(Project *project) removeProjects({project}); } -bool ProjectManager::save() +void ProjectManagerPrivate::saveSession() { - emit SessionManager::instance()->aboutToSaveSession(); + // save the startup project + if (d->m_startupProject) + SessionManager::setSessionValue("StartupProject", + m_startupProject->projectFilePath().toSettings()); - const FilePath filePath = SessionManager::sessionNameToFileName(sb_d->m_sessionName); - QVariantMap data; - - // See the explanation at loadSession() for how we handle the implicit default session. - if (SessionManager::isDefaultVirgin()) { - if (filePath.exists()) { - PersistentSettingsReader reader; - if (!reader.load(filePath)) { - QMessageBox::warning(ICore::dialogParent(), Tr::tr("Error while saving session"), - Tr::tr("Could not save session %1").arg(filePath.toUserOutput())); - return false; - } - data = reader.restoreValues(); - } - } else { - // save the startup project - if (d->m_startupProject) - data.insert("StartupProject", d->m_startupProject->projectFilePath().toSettings()); - - const QColor c = StyleHelper::requestedBaseColor(); - if (c.isValid()) { - QString tmp = QString::fromLatin1("#%1%2%3") - .arg(c.red(), 2, 16, QLatin1Char('0')) - .arg(c.green(), 2, 16, QLatin1Char('0')) - .arg(c.blue(), 2, 16, QLatin1Char('0')); - data.insert(QLatin1String("Color"), tmp); - } - - FilePaths projectFiles = Utils::transform(projects(), &Project::projectFilePath); - // Restore information on projects that failed to load: - // don't read projects to the list, which the user loaded - for (const FilePath &failed : std::as_const(d->m_failedProjects)) { - if (!projectFiles.contains(failed)) - projectFiles << failed; - } - - data.insert("ProjectList", Utils::transform(projectFiles, - &FilePath::toString)); - data.insert("CascadeSetActive", d->m_casadeSetActive); - - QVariantMap depMap; - auto i = d->m_depMap.constBegin(); - while (i != d->m_depMap.constEnd()) { - QString key = i.key().toString(); - QStringList values; - const FilePaths valueList = i.value(); - for (const FilePath &value : valueList) - values << value.toString(); - depMap.insert(key, values); - ++i; - } - data.insert(QLatin1String("ProjectDependencies"), QVariant(depMap)); - data.insert(QLatin1String("EditorSettings"), EditorManager::saveState().toBase64()); + FilePaths projectFiles = Utils::transform(m_projects, &Project::projectFilePath); + // Restore information on projects that failed to load: + // don't read projects to the list, which the user loaded + for (const FilePath &failed : std::as_const(m_failedProjects)) { + if (!projectFiles.contains(failed)) + projectFiles << failed; } - const auto end = sb_d->m_values.constEnd(); - QStringList keys; - for (auto it = sb_d->m_values.constBegin(); it != end; ++it) { - data.insert(QLatin1String("value-") + it.key(), it.value()); - keys << it.key(); - } - data.insert(QLatin1String("valueKeys"), keys); + SessionManager::setSessionValue("ProjectList", + Utils::transform(projectFiles, + &FilePath::toString)); + SessionManager::setSessionValue("CascadeSetActive", m_casadeSetActive); - if (!sb_d->m_writer || sb_d->m_writer->fileName() != filePath) { - delete sb_d->m_writer; - sb_d->m_writer = new PersistentSettingsWriter(filePath, "QtCreatorSession"); + QVariantMap depMap; + auto i = m_depMap.constBegin(); + while (i != m_depMap.constEnd()) { + QString key = i.key().toString(); + QStringList values; + const FilePaths valueList = i.value(); + for (const FilePath &value : valueList) + values << value.toString(); + depMap.insert(key, values); + ++i; } - const bool result = sb_d->m_writer->save(data, ICore::dialogParent()); - if (result) { - if (!SessionManager::isDefaultVirgin()) - sb_d->m_sessionDateTimes.insert(SessionManager::activeSession(), QDateTime::currentDateTime()); - } else { - QMessageBox::warning(ICore::dialogParent(), Tr::tr("Error while saving session"), - Tr::tr("Could not save session to file %1").arg(sb_d->m_writer->fileName().toUserOutput())); - } - - return result; + SessionManager::setSessionValue(QLatin1String("ProjectDependencies"), QVariant(depMap)); } /*! diff --git a/src/plugins/projectexplorer/projectmanager.h b/src/plugins/projectexplorer/projectmanager.h index 7f515869013..f49cc96e54c 100644 --- a/src/plugins/projectexplorer/projectmanager.h +++ b/src/plugins/projectexplorer/projectmanager.h @@ -47,7 +47,6 @@ public: }); } - static bool save(); static void closeAllProjects(); static void addProject(Project *project); diff --git a/src/plugins/projectexplorer/session.cpp b/src/plugins/projectexplorer/session.cpp index 5edd7b4f9d6..ccedbb885fe 100644 --- a/src/plugins/projectexplorer/session.cpp +++ b/src/plugins/projectexplorer/session.cpp @@ -7,7 +7,6 @@ #include "projectexplorer.h" #include "projectexplorertr.h" -#include "projectmanager.h" #include #include @@ -514,7 +513,7 @@ bool SessionManager::loadSession(const QString &session, bool initial) // Allow everyone to set something in the session and before saving emit SessionManager::instance()->aboutToUnloadSession(sb_d->m_sessionName); - if (!ProjectManager::save()) { + if (!saveSession()) { sb_d->m_loadingSession = false; return false; } @@ -571,4 +570,67 @@ bool SessionManager::loadSession(const QString &session, bool initial) return true; } +bool SessionManager::saveSession() +{ + emit SessionManager::instance()->aboutToSaveSession(); + + const FilePath filePath = SessionManager::sessionNameToFileName(sb_d->m_sessionName); + QVariantMap data; + + // See the explanation at loadSession() for how we handle the implicit default session. + if (SessionManager::isDefaultVirgin()) { + if (filePath.exists()) { + PersistentSettingsReader reader; + if (!reader.load(filePath)) { + QMessageBox::warning(ICore::dialogParent(), + Tr::tr("Error while saving session"), + Tr::tr("Could not save session %1") + .arg(filePath.toUserOutput())); + return false; + } + data = reader.restoreValues(); + } + } else { + const QColor c = StyleHelper::requestedBaseColor(); + if (c.isValid()) { + QString tmp = QString::fromLatin1("#%1%2%3") + .arg(c.red(), 2, 16, QLatin1Char('0')) + .arg(c.green(), 2, 16, QLatin1Char('0')) + .arg(c.blue(), 2, 16, QLatin1Char('0')); + setSessionValue("Color", tmp); + } + setSessionValue("EditorSettings", EditorManager::saveState().toBase64()); + + const auto end = sb_d->m_sessionValues.constEnd(); + for (auto it = sb_d->m_sessionValues.constBegin(); it != end; ++it) + data.insert(it.key(), it.value()); + } + + const auto end = sb_d->m_values.constEnd(); + QStringList keys; + for (auto it = sb_d->m_values.constBegin(); it != end; ++it) { + data.insert("value-" + it.key(), it.value()); + keys << it.key(); + } + data.insert("valueKeys", keys); + + if (!sb_d->m_writer || sb_d->m_writer->fileName() != filePath) { + delete sb_d->m_writer; + sb_d->m_writer = new PersistentSettingsWriter(filePath, "QtCreatorSession"); + } + const bool result = sb_d->m_writer->save(data, ICore::dialogParent()); + if (result) { + if (!SessionManager::isDefaultVirgin()) + sb_d->m_sessionDateTimes.insert(SessionManager::activeSession(), + QDateTime::currentDateTime()); + } else { + QMessageBox::warning(ICore::dialogParent(), + Tr::tr("Error while saving session"), + Tr::tr("Could not save session to file %1") + .arg(sb_d->m_writer->fileName().toUserOutput())); + } + + return result; +} + } // namespace ProjectExplorer diff --git a/src/plugins/projectexplorer/session.h b/src/plugins/projectexplorer/session.h index b40e687d648..8f2da70bd24 100644 --- a/src/plugins/projectexplorer/session.h +++ b/src/plugins/projectexplorer/session.h @@ -64,6 +64,7 @@ public: static void addSessionLoadingSteps(int steps); static bool loadSession(const QString &session, bool initial = false); + static bool saveSession(); signals: void startupSessionRestored();