From 26eb3753c0ba79bb9d3c3e9a9087bd3ced6fa7d6 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Thu, 8 Jun 2017 14:56:52 +0200 Subject: [PATCH] SessionManager: Make it possible to restore backups again --- src/lib/session/sessionmanager.cpp | 37 +++++++++++++++--------- src/lib/session/sessionmanager.h | 18 +++++++++--- src/lib/session/sessionmanagerdialog.cpp | 10 +++++-- 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/lib/session/sessionmanager.cpp b/src/lib/session/sessionmanager.cpp index 00858853b..9890c546e 100644 --- a/src/lib/session/sessionmanager.cpp +++ b/src/lib/session/sessionmanager.cpp @@ -72,7 +72,7 @@ void SessionManager::sessionsDirectoryChanged() m_sessionsMetaDataList.clear(); } -void SessionManager::openSession(QString sessionFilePath, bool switchSession) +void SessionManager::openSession(QString sessionFilePath, SessionFlags flags) { if (sessionFilePath.isEmpty()) { QAction* action = qobject_cast(sender()); @@ -93,7 +93,7 @@ void SessionManager::openSession(QString sessionFilePath, bool switchSession) return; BrowserWindow* window = mApp->getWindow(); - if (switchSession) { + if (flags.testFlag(SwitchSession)) { writeCurrentSession(m_lastActiveSessionPath); window = mApp->createWindow(Qz::BW_OtherRestoredWindow); @@ -102,14 +102,16 @@ void SessionManager::openSession(QString sessionFilePath, bool switchSession) win->close(); } - m_lastActiveSessionPath = QFileInfo(sessionFilePath).canonicalFilePath(); - m_sessionsMetaDataList.clear(); + if (!flags.testFlag(ReplaceSession)) { + m_lastActiveSessionPath = QFileInfo(sessionFilePath).canonicalFilePath(); + m_sessionsMetaDataList.clear(); + } } mApp->openSession(window, sessionData); } -void SessionManager::renameSession(QString sessionFilePath, bool clone) +void SessionManager::renameSession(QString sessionFilePath, SessionFlags flags) { if (sessionFilePath.isEmpty()) { QAction* action = qobject_cast(sender()); @@ -120,8 +122,8 @@ void SessionManager::renameSession(QString sessionFilePath, bool clone) } bool ok; - const QString suggestedName = QFileInfo(sessionFilePath).baseName() + (clone ? tr("_cloned") : tr("_renamed")); - QString newName = QInputDialog::getText(mApp->activeWindow(), (clone ? tr("Clone Session") : tr("Rename Session")), + const QString suggestedName = QFileInfo(sessionFilePath).baseName() + (flags.testFlag(CloneSession) ? tr("_cloned") : tr("_renamed")); + QString newName = QInputDialog::getText(mApp->activeWindow(), (flags.testFlag(CloneSession) ? tr("Clone Session") : tr("Rename Session")), tr("Please enter a new name:"), QLineEdit::Normal, suggestedName, &ok); @@ -131,11 +133,11 @@ void SessionManager::renameSession(QString sessionFilePath, bool clone) const QString newSessionPath = QString("%1/%2.dat").arg(DataPaths::path(DataPaths::Sessions)).arg(newName); if (QFile::exists(newSessionPath)) { QMessageBox::information(mApp->activeWindow(), tr("Error!"), tr("The session file \"%1\" exists. Please enter another name.").arg(newName)); - renameSession(sessionFilePath, clone); + renameSession(sessionFilePath, flags); return; } - if (clone) { + if (flags.testFlag(CloneSession)) { if (!QFile::copy(sessionFilePath, newSessionPath)) { QMessageBox::information(mApp->activeWindow(), tr("Error!"), tr("An error occurred when cloning session file.")); return; @@ -172,14 +174,23 @@ void SessionManager::saveSession() writeCurrentSession(filePath); } +void SessionManager::replaceSession(const QString &filePath) +{ + QMessageBox::StandardButton result = QMessageBox::information(mApp->activeWindow(), tr("Restore Backup"), tr("Are you sure you want to replace current session?"), + QMessageBox::Yes | QMessageBox::No); + if (result == QMessageBox::Yes) { + openSession(filePath, ReplaceSession); + } +} + void SessionManager::switchToSession(const QString &filePath) { - openSession(filePath, /*switchSession*/ true); + openSession(filePath, SwitchSession); } void SessionManager::cloneSession(const QString &filePath) { - renameSession(filePath, /*clone*/ true); + renameSession(filePath, CloneSession); } void SessionManager::deleteSession(const QString &filePath) @@ -228,14 +239,14 @@ QList SessionManager::sessionMetaData(bool with if (withBackups && QFile::exists(m_firstBackupSession)) { SessionMetaData data; - data.name = tr("First Backup"); + data.name = tr("Backup 1"); data.filePath = m_firstBackupSession; data.isBackup = true; out.append(data); } if (withBackups && QFile::exists(m_secondBackupSession)) { SessionMetaData data; - data.name = tr("Second Backup"); + data.name = tr("Backup 2"); data.filePath = m_secondBackupSession; data.isBackup = true; out.append(data); diff --git a/src/lib/session/sessionmanager.h b/src/lib/session/sessionmanager.h index 83cbd918d..0223c14f2 100644 --- a/src/lib/session/sessionmanager.h +++ b/src/lib/session/sessionmanager.h @@ -29,8 +29,6 @@ class QUPZILLA_EXPORT SessionManager : public QObject Q_OBJECT public: - explicit SessionManager(QObject* parent = 0); - struct SessionMetaData { QString name; QString filePath; @@ -39,6 +37,15 @@ public: bool isBackup = false; }; + enum SessionFlag { + SwitchSession = 1, + CloneSession = 2, + ReplaceSession = SwitchSession | 4 + }; + Q_DECLARE_FLAGS(SessionFlags, SessionFlag) + + explicit SessionManager(QObject* parent = 0); + void loadSettings(); void saveSettings(); @@ -59,10 +66,11 @@ public slots: private slots: void aboutToShowSessionsMenu(); void sessionsDirectoryChanged(); - void openSession(QString sessionFilePath = QString(), bool switchSession = false); - void renameSession(QString sessionFilePath = QString(), bool clone = false); + void openSession(QString sessionFilePath = QString(), SessionFlags flags = nullptr); + void renameSession(QString sessionFilePath = QString(), SessionFlags flags = nullptr); void saveSession(); + void replaceSession(const QString &filePath); void switchToSession(const QString &filePath); void cloneSession(const QString &filePath); void deleteSession(const QString &filePath); @@ -84,4 +92,6 @@ private: friend class SessionManagerDialog; }; +Q_DECLARE_OPERATORS_FOR_FLAGS(SessionManager::SessionFlags) + #endif // SESSIONMANAGER_H diff --git a/src/lib/session/sessionmanagerdialog.cpp b/src/lib/session/sessionmanagerdialog.cpp index fc50521c8..c756eb38e 100644 --- a/src/lib/session/sessionmanagerdialog.cpp +++ b/src/lib/session/sessionmanagerdialog.cpp @@ -98,8 +98,11 @@ void SessionManagerDialog::switchToSession() } const QString filePath = item->data(0, SessionFileRole).toString(); if (!filePath.isEmpty()) { - close(); - mApp->sessionManager()->switchToSession(filePath); + if (item->data(0, IsBackupSessionRole).toBool()) { + mApp->sessionManager()->replaceSession(filePath); + } else { + mApp->sessionManager()->switchToSession(filePath); + } } } @@ -133,7 +136,8 @@ void SessionManagerDialog::updateButtons() ui->renameButton->setEnabled(item && !isDefault && !isBackup); ui->cloneButton->setEnabled(item && !isBackup); ui->deleteButton->setEnabled(item && !isBackup && !isDefault && !isActive); - ui->switchToButton->setEnabled(item && !isActive && !isBackup); + ui->switchToButton->setEnabled(item && !isActive); + ui->switchToButton->setText(isBackup ? tr("Restore") : tr("Switch To")); } void SessionManagerDialog::updateItem(QTreeWidgetItem *item)