From 850c7c7ecf14b36cd1f13726bcef139877e880d5 Mon Sep 17 00:00:00 2001 From: Francois Ferrand Date: Mon, 22 Apr 2013 21:33:42 +0200 Subject: [PATCH] Option to automatically reopen databases which were last opened. --- src/core/Config.cpp | 1 + src/gui/DatabaseTabWidget.cpp | 34 ++++++++++++++++++++------------ src/gui/DatabaseTabWidget.h | 3 ++- src/gui/MainWindow.cpp | 2 ++ src/gui/SettingsWidget.cpp | 7 +++++++ src/gui/SettingsWidgetGeneral.ui | 21 +++++++++++++------- 6 files changed, 47 insertions(+), 21 deletions(-) diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 047f4d670..08fbfdfb2 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -88,6 +88,7 @@ void Config::init(const QString& fileName) m_settings.reset(new QSettings(fileName, QSettings::IniFormat)); m_defaults.insert("RememberLastDatabases", true); + m_defaults.insert("AutoReopenLastDatabases", true); m_defaults.insert("ModifiedOnExpandedStateChanges", true); m_defaults.insert("AutoSaveAfterEveryChange", false); m_defaults.insert("AutoSaveOnExit", false); diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index cc87a5745..e81db035c 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -66,16 +66,7 @@ DatabaseTabWidget::~DatabaseTabWidget() void DatabaseTabWidget::toggleTabbar() { - if (count() > 1) { - if (!tabBar()->isVisible()) { - tabBar()->show(); - } - } - else { - if (tabBar()->isVisible()) { - tabBar()->hide(); - } - } + tabBar()->setVisible(count() > 1); } void DatabaseTabWidget::newDatabase() @@ -148,7 +139,7 @@ void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw, insertDatabase(db, dbStruct); - updateLastDatabases(dbStruct.filePath); + updateRecentDatabases(dbStruct.filePath); if (!pw.isNull() || !keyFile.isEmpty()) { dbStruct.dbWidget->switchToOpenDatabase(dbStruct.filePath, pw, keyFile); @@ -235,8 +226,25 @@ void DatabaseTabWidget::deleteDatabase(Database* db) delete db; } +void DatabaseTabWidget::reopenLastDatabases() +{ + if (config()->get("AutoReopenLastDatabases", false).toBool()) { + int index = count(); + Q_FOREACH (const QString & database, config()->get("LastOpenDatabases", QVariant()).toStringList()) + openDatabase(database); + setCurrentIndex(index); + } +} + bool DatabaseTabWidget::closeAllDatabases() { + QStringList reloadDatabases; + if (config()->get("AutoReopenLastDatabases", false).toBool()) { + for (int i = 0; i < count(); i ++) + reloadDatabases << indexDatabaseManagerStruct(i).filePath; + } + config()->set("LastOpenDatabases", reloadDatabases); + while (!m_dbList.isEmpty()) { if (!closeDatabase()) { return false; @@ -299,7 +307,7 @@ void DatabaseTabWidget::saveDatabaseAs(Database* db) dbStruct.fileName = fileInfo.fileName(); dbStruct.dbWidget->updateFilename(dbStruct.filePath); updateTabName(db); - updateLastDatabases(dbStruct.filePath); + updateRecentDatabases(dbStruct.filePath); } else { QMessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n" @@ -545,7 +553,7 @@ void DatabaseTabWidget::modified() } } -void DatabaseTabWidget::updateLastDatabases(const QString& filename) +void DatabaseTabWidget::updateRecentDatabases(const QString& filename) { if (!config()->get("RememberLastDatabases").toBool()) { config()->set("LastDatabases", QVariant()); diff --git a/src/gui/DatabaseTabWidget.h b/src/gui/DatabaseTabWidget.h index 51dcc76f9..bdbe8034c 100644 --- a/src/gui/DatabaseTabWidget.h +++ b/src/gui/DatabaseTabWidget.h @@ -65,6 +65,7 @@ public Q_SLOTS: void saveDatabaseAs(int index = -1); bool closeDatabase(int index = -1); void closeDatabaseFromSender(); + void reopenLastDatabases(); bool closeAllDatabases(); void changeMasterKey(); void changeDatabaseSettings(); @@ -93,7 +94,7 @@ private: DatabaseManagerStruct indexDatabaseManagerStruct(int index); Database* databaseFromDatabaseWidget(DatabaseWidget* dbWidget); void insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct); - void updateLastDatabases(const QString& filename); + void updateRecentDatabases(const QString& filename); void connectDatabase(Database* newDb, Database* oldDb = Q_NULLPTR); KeePass2Writer m_writer; diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 2ecad9b92..97a098071 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -227,6 +227,8 @@ MainWindow::MainWindow() m_actionMultiplexer.connect(m_ui->actionSearch, SIGNAL(triggered()), SLOT(toggleSearch())); + + m_ui->tabWidget->reopenLastDatabases(); } MainWindow::~MainWindow() diff --git a/src/gui/SettingsWidget.cpp b/src/gui/SettingsWidget.cpp index 9a86eac99..17ab6082f 100644 --- a/src/gui/SettingsWidget.cpp +++ b/src/gui/SettingsWidget.cpp @@ -63,8 +63,12 @@ SettingsWidget::SettingsWidget(QWidget* parent) connect(m_generalUi->autoSaveAfterEveryChangeCheckBox, SIGNAL(toggled(bool)), this, SLOT(enableAutoSaveOnExit(bool))); + connect(m_generalUi->rememberLastDatabasesCheckBox, SIGNAL(toggled(bool)), + m_generalUi->autoReopenLastDatabases, SLOT(setEnabled(bool))); + connect(m_secUi->clearClipboardCheckBox, SIGNAL(toggled(bool)), m_secUi->clearClipboardSpinBox, SLOT(setEnabled(bool))); + } SettingsWidget::~SettingsWidget() @@ -82,6 +86,7 @@ void SettingsWidget::addSettingsPage(ISettingsPage *page) void SettingsWidget::loadSettings() { m_generalUi->rememberLastDatabasesCheckBox->setChecked(config()->get("RememberLastDatabases").toBool()); + m_generalUi->autoReopenLastDatabases->setChecked(config()->get("AutoReopenLastDatabases").toBool()); m_generalUi->modifiedExpandedChangedCheckBox->setChecked(config()->get("ModifiedOnExpandedStateChanges").toBool()); m_generalUi->autoSaveAfterEveryChangeCheckBox->setChecked(config()->get("AutoSaveAfterEveryChange").toBool()); m_generalUi->autoSaveOnExitCheckBox->setChecked(config()->get("AutoSaveOnExit").toBool()); @@ -104,6 +109,8 @@ void SettingsWidget::loadSettings() void SettingsWidget::saveSettings() { config()->set("RememberLastDatabases", m_generalUi->rememberLastDatabasesCheckBox->isChecked()); + config()->set("AutoReopenLastDatabases", m_generalUi->rememberLastDatabasesCheckBox->isChecked() + && m_generalUi->autoReopenLastDatabases->isChecked()); config()->set("ModifiedOnExpandedStateChanges", m_generalUi->modifiedExpandedChangedCheckBox->isChecked()); config()->set("AutoSaveAfterEveryChange", m_generalUi->autoSaveAfterEveryChangeCheckBox->isChecked()); config()->set("AutoSaveOnExit", m_generalUi->autoSaveOnExitCheckBox->isChecked()); diff --git a/src/gui/SettingsWidgetGeneral.ui b/src/gui/SettingsWidgetGeneral.ui index 78e9dadba..a369cf1c2 100644 --- a/src/gui/SettingsWidgetGeneral.ui +++ b/src/gui/SettingsWidgetGeneral.ui @@ -7,21 +7,21 @@ 0 0 456 - 146 + 172 - Remember last databases + Remember recent databases true - + Mark as modified on expanded state changes @@ -31,30 +31,37 @@ - + Automatically save after every change - + Automatically save on exit - + Global Auto-Type shortcut - + + + + + Automatically reopen last databases + + +