1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

SessionManager: Make it possible to restore backups again

This commit is contained in:
David Rosca 2017-06-08 14:56:52 +02:00
parent 93d4d66054
commit 26eb3753c0
3 changed files with 45 additions and 20 deletions

View File

@ -72,7 +72,7 @@ void SessionManager::sessionsDirectoryChanged()
m_sessionsMetaDataList.clear(); m_sessionsMetaDataList.clear();
} }
void SessionManager::openSession(QString sessionFilePath, bool switchSession) void SessionManager::openSession(QString sessionFilePath, SessionFlags flags)
{ {
if (sessionFilePath.isEmpty()) { if (sessionFilePath.isEmpty()) {
QAction* action = qobject_cast<QAction*>(sender()); QAction* action = qobject_cast<QAction*>(sender());
@ -93,7 +93,7 @@ void SessionManager::openSession(QString sessionFilePath, bool switchSession)
return; return;
BrowserWindow* window = mApp->getWindow(); BrowserWindow* window = mApp->getWindow();
if (switchSession) { if (flags.testFlag(SwitchSession)) {
writeCurrentSession(m_lastActiveSessionPath); writeCurrentSession(m_lastActiveSessionPath);
window = mApp->createWindow(Qz::BW_OtherRestoredWindow); window = mApp->createWindow(Qz::BW_OtherRestoredWindow);
@ -102,14 +102,16 @@ void SessionManager::openSession(QString sessionFilePath, bool switchSession)
win->close(); win->close();
} }
if (!flags.testFlag(ReplaceSession)) {
m_lastActiveSessionPath = QFileInfo(sessionFilePath).canonicalFilePath(); m_lastActiveSessionPath = QFileInfo(sessionFilePath).canonicalFilePath();
m_sessionsMetaDataList.clear(); m_sessionsMetaDataList.clear();
} }
}
mApp->openSession(window, sessionData); mApp->openSession(window, sessionData);
} }
void SessionManager::renameSession(QString sessionFilePath, bool clone) void SessionManager::renameSession(QString sessionFilePath, SessionFlags flags)
{ {
if (sessionFilePath.isEmpty()) { if (sessionFilePath.isEmpty()) {
QAction* action = qobject_cast<QAction*>(sender()); QAction* action = qobject_cast<QAction*>(sender());
@ -120,8 +122,8 @@ void SessionManager::renameSession(QString sessionFilePath, bool clone)
} }
bool ok; bool ok;
const QString suggestedName = QFileInfo(sessionFilePath).baseName() + (clone ? tr("_cloned") : tr("_renamed")); const QString suggestedName = QFileInfo(sessionFilePath).baseName() + (flags.testFlag(CloneSession) ? tr("_cloned") : tr("_renamed"));
QString newName = QInputDialog::getText(mApp->activeWindow(), (clone ? tr("Clone Session") : tr("Rename Session")), QString newName = QInputDialog::getText(mApp->activeWindow(), (flags.testFlag(CloneSession) ? tr("Clone Session") : tr("Rename Session")),
tr("Please enter a new name:"), QLineEdit::Normal, tr("Please enter a new name:"), QLineEdit::Normal,
suggestedName, &ok); 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); const QString newSessionPath = QString("%1/%2.dat").arg(DataPaths::path(DataPaths::Sessions)).arg(newName);
if (QFile::exists(newSessionPath)) { if (QFile::exists(newSessionPath)) {
QMessageBox::information(mApp->activeWindow(), tr("Error!"), tr("The session file \"%1\" exists. Please enter another name.").arg(newName)); 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; return;
} }
if (clone) { if (flags.testFlag(CloneSession)) {
if (!QFile::copy(sessionFilePath, newSessionPath)) { if (!QFile::copy(sessionFilePath, newSessionPath)) {
QMessageBox::information(mApp->activeWindow(), tr("Error!"), tr("An error occurred when cloning session file.")); QMessageBox::information(mApp->activeWindow(), tr("Error!"), tr("An error occurred when cloning session file."));
return; return;
@ -172,14 +174,23 @@ void SessionManager::saveSession()
writeCurrentSession(filePath); 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) void SessionManager::switchToSession(const QString &filePath)
{ {
openSession(filePath, /*switchSession*/ true); openSession(filePath, SwitchSession);
} }
void SessionManager::cloneSession(const QString &filePath) void SessionManager::cloneSession(const QString &filePath)
{ {
renameSession(filePath, /*clone*/ true); renameSession(filePath, CloneSession);
} }
void SessionManager::deleteSession(const QString &filePath) void SessionManager::deleteSession(const QString &filePath)
@ -228,14 +239,14 @@ QList<SessionManager::SessionMetaData> SessionManager::sessionMetaData(bool with
if (withBackups && QFile::exists(m_firstBackupSession)) { if (withBackups && QFile::exists(m_firstBackupSession)) {
SessionMetaData data; SessionMetaData data;
data.name = tr("First Backup"); data.name = tr("Backup 1");
data.filePath = m_firstBackupSession; data.filePath = m_firstBackupSession;
data.isBackup = true; data.isBackup = true;
out.append(data); out.append(data);
} }
if (withBackups && QFile::exists(m_secondBackupSession)) { if (withBackups && QFile::exists(m_secondBackupSession)) {
SessionMetaData data; SessionMetaData data;
data.name = tr("Second Backup"); data.name = tr("Backup 2");
data.filePath = m_secondBackupSession; data.filePath = m_secondBackupSession;
data.isBackup = true; data.isBackup = true;
out.append(data); out.append(data);

View File

@ -29,8 +29,6 @@ class QUPZILLA_EXPORT SessionManager : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit SessionManager(QObject* parent = 0);
struct SessionMetaData { struct SessionMetaData {
QString name; QString name;
QString filePath; QString filePath;
@ -39,6 +37,15 @@ public:
bool isBackup = false; 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 loadSettings();
void saveSettings(); void saveSettings();
@ -59,10 +66,11 @@ public slots:
private slots: private slots:
void aboutToShowSessionsMenu(); void aboutToShowSessionsMenu();
void sessionsDirectoryChanged(); void sessionsDirectoryChanged();
void openSession(QString sessionFilePath = QString(), bool switchSession = false); void openSession(QString sessionFilePath = QString(), SessionFlags flags = nullptr);
void renameSession(QString sessionFilePath = QString(), bool clone = false); void renameSession(QString sessionFilePath = QString(), SessionFlags flags = nullptr);
void saveSession(); void saveSession();
void replaceSession(const QString &filePath);
void switchToSession(const QString &filePath); void switchToSession(const QString &filePath);
void cloneSession(const QString &filePath); void cloneSession(const QString &filePath);
void deleteSession(const QString &filePath); void deleteSession(const QString &filePath);
@ -84,4 +92,6 @@ private:
friend class SessionManagerDialog; friend class SessionManagerDialog;
}; };
Q_DECLARE_OPERATORS_FOR_FLAGS(SessionManager::SessionFlags)
#endif // SESSIONMANAGER_H #endif // SESSIONMANAGER_H

View File

@ -98,10 +98,13 @@ void SessionManagerDialog::switchToSession()
} }
const QString filePath = item->data(0, SessionFileRole).toString(); const QString filePath = item->data(0, SessionFileRole).toString();
if (!filePath.isEmpty()) { if (!filePath.isEmpty()) {
close(); if (item->data(0, IsBackupSessionRole).toBool()) {
mApp->sessionManager()->replaceSession(filePath);
} else {
mApp->sessionManager()->switchToSession(filePath); mApp->sessionManager()->switchToSession(filePath);
} }
} }
}
void SessionManagerDialog::refresh() void SessionManagerDialog::refresh()
{ {
@ -133,7 +136,8 @@ void SessionManagerDialog::updateButtons()
ui->renameButton->setEnabled(item && !isDefault && !isBackup); ui->renameButton->setEnabled(item && !isDefault && !isBackup);
ui->cloneButton->setEnabled(item && !isBackup); ui->cloneButton->setEnabled(item && !isBackup);
ui->deleteButton->setEnabled(item && !isBackup && !isDefault && !isActive); 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) void SessionManagerDialog::updateItem(QTreeWidgetItem *item)