From 6f48983efe0137333f1e27389685588e9b3eb7d9 Mon Sep 17 00:00:00 2001 From: Florian Geyer Date: Sun, 27 May 2012 23:01:14 +0200 Subject: [PATCH] Remember last opened files. --- src/core/Config.cpp | 1 + src/gui/DatabaseTabWidget.cpp | 25 +++++++++++++++++++++++++ src/gui/DatabaseTabWidget.h | 5 +++++ 3 files changed, 31 insertions(+) diff --git a/src/core/Config.cpp b/src/core/Config.cpp index c0d330329..74c50e003 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -67,6 +67,7 @@ Config::Config() m_settings.reset(new QSettings(userPath, QSettings::IniFormat)); + m_defaults.insert("RememberLastDatabases", true); m_defaults.insert("security/clearclipboard", true); m_defaults.insert("security/clearclipboardtimeout", 10); } diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index 50c32fdc7..55f61535d 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -21,6 +21,7 @@ #include #include +#include "core/Config.h" #include "core/Database.h" #include "core/Group.h" #include "core/Metadata.h" @@ -40,6 +41,8 @@ DatabaseManagerStruct::DatabaseManagerStruct() { } +const int DatabaseTabWidget::LastDatabasesCount = 5; + DatabaseTabWidget::DatabaseTabWidget(QWidget* parent) : QTabWidget(parent) , m_window(parent->window()) @@ -153,8 +156,11 @@ void DatabaseTabWidget::openDatabaseRead() m_curKeyDialog = 0; m_curDbStruct.dbWidget = new DatabaseWidget(db, this); + QString filename = m_curDbStruct.fileName; insertDatabase(db, m_curDbStruct); m_curDbStruct = DatabaseManagerStruct(); + + updateLastDatabases(filename); } void DatabaseTabWidget::openDatabaseCleanup() @@ -247,6 +253,7 @@ void DatabaseTabWidget::saveDatabase(Database* db) dbStruct.modified = false; updateTabName(db); + updateLastDatabases(dbStruct.fileName); } else { saveDatabaseAs(db); @@ -278,6 +285,7 @@ void DatabaseTabWidget::saveDatabaseAs(Database* db) dbStruct.modified = false; dbStruct.fileName = QFileInfo(fileName).absoluteFilePath(); updateTabName(db); + updateLastDatabases(dbStruct.fileName); } } @@ -497,3 +505,20 @@ void DatabaseTabWidget::modified() updateTabName(db); } } + +void DatabaseTabWidget::updateLastDatabases(const QString& filename) +{ + if (!config()->get("RememberLastDatabases").toBool()) { + config()->set("LastDatabases", QVariant()); + } + else { + QStringList lastDatabases = config()->get("LastDatabases", QVariant()).toStringList(); + lastDatabases.prepend(filename); + lastDatabases.removeDuplicates(); + + while (lastDatabases.count() > LastDatabasesCount) { + lastDatabases.removeLast(); + } + config()->set("LastDatabases", lastDatabases); + } +} diff --git a/src/gui/DatabaseTabWidget.h b/src/gui/DatabaseTabWidget.h index 740fa38ad..4b43373c3 100644 --- a/src/gui/DatabaseTabWidget.h +++ b/src/gui/DatabaseTabWidget.h @@ -51,6 +51,8 @@ public: const QString& keyFile = QString()); DatabaseWidget* currentDatabaseWidget(); + static const int LastDatabasesCount; + public Q_SLOTS: void newDatabase(); void openDatabase(); @@ -96,12 +98,15 @@ private: Database* indexDatabase(int index); Database* databaseFromDatabaseWidget(DatabaseWidget* dbWidget); void insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct); + void updateLastDatabases(const QString& filename); QWidget* const m_window; KeePass2Writer m_writer; QHash m_dbList; DatabaseManagerStruct m_curDbStruct; DatabaseOpenDialog* m_curKeyDialog; + + }; #endif // KEEPASSX_DATABASETABWIDGET_H