From d2ab008aa00cd419646d8e89c777a0551ee5b8d7 Mon Sep 17 00:00:00 2001 From: Francois Ferrand Date: Tue, 30 Apr 2013 17:31:08 +0200 Subject: [PATCH] Auto-reload settings. --- src/core/Config.cpp | 1 + src/gui/DatabaseTabWidget.cpp | 35 +++++++++++++++----------------- src/gui/DatabaseTabWidget.h | 6 +++--- src/gui/SettingsWidget.cpp | 2 ++ src/gui/SettingsWidgetGeneral.ui | 34 +++++++++++++++++++++++++++---- 5 files changed, 52 insertions(+), 26 deletions(-) diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 08fbfdfb2..188758866 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -93,6 +93,7 @@ void Config::init(const QString& fileName) m_defaults.insert("AutoSaveAfterEveryChange", false); m_defaults.insert("AutoSaveOnExit", false); m_defaults.insert("ShowToolbar", true); + m_defaults.insert("ReloadBehavior", 0 /*always ask*/); 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 a520fd46d..6af2fe545 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -49,8 +49,7 @@ const int DatabaseTabWidget::LastDatabasesCount = 5; DatabaseTabWidget::DatabaseTabWidget(QWidget* parent) : QTabWidget(parent), - m_fileWatcher(new QFileSystemWatcher(this)), - m_reloadBehavior(ReloadUnmodified) //TODO: setting + m_fileWatcher(new QFileSystemWatcher(this)) { DragTabBar* tabBar = new DragTabBar(this); tabBar->setDrawBase(false); @@ -98,7 +97,7 @@ void DatabaseTabWidget::openDatabase() } void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw, - const QString& keyFile, const CompositeKey& key) + const QString& keyFile, const CompositeKey& key, int index) { QFileInfo fileInfo(fileName); QString canonicalFilePath = fileInfo.canonicalFilePath(); @@ -144,7 +143,7 @@ void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw, dbStruct.fileName = fileInfo.fileName(); dbStruct.lastModified = fileInfo.lastModified(); - insertDatabase(db, dbStruct); + insertDatabase(db, dbStruct, index); m_fileWatcher->addPath(dbStruct.filePath); updateRecentDatabases(dbStruct.filePath); @@ -228,14 +227,14 @@ void DatabaseTabWidget::checkReloadDatabases() if (mode == DatabaseWidget::None || mode == DatabaseWidget::LockedMode || !db->hasKey()) continue; - if ( (m_reloadBehavior == AlwaysAsk) - || (m_reloadBehavior == ReloadUnmodified && mode == DatabaseWidget::EditMode) - || (m_reloadBehavior == ReloadUnmodified && dbStruct.modified)) { - //TODO: display banner instead, to let user now file has changed and choose to Reload, Overwrite, and SaveAs - // --> less obstrubsive (esp. if multiple DB are open), cleaner UI - if (QMessageBox::warning(this, fi.exists() ? tr("Database file changed") : tr("Database file removed"), - tr("Do you want to discard your changes and reload?"), - QMessageBox::Yes|QMessageBox::No) == QMessageBox::No) + ReloadBehavior reloadBehavior = ReloadBehavior(config()->get("ReloadBehavior").toInt()); + if ( (reloadBehavior == AlwaysAsk) + || (reloadBehavior == ReloadUnmodified && mode == DatabaseWidget::EditMode) + || (reloadBehavior == ReloadUnmodified && dbStruct.modified)) { + int res = QMessageBox::warning(this, fi.exists() ? tr("Database file changed") : tr("Database file removed"), + tr("Do you want to discard your changes and reload?"), + QMessageBox::Yes|QMessageBox::No); + if (res == QMessageBox::No) continue; } @@ -257,12 +256,13 @@ void DatabaseTabWidget::checkReloadDatabases() //Reload updated db CompositeKey key = db->key(); + int tabPos = databaseIndex(db); closeDatabase(db); - openDatabase(filePath, QString(), QString(), key); + openDatabase(filePath, QString(), QString(), key, tabPos); //Restore current group/entry dbStruct = indexDatabaseManagerStruct(count() - 1); - if (dbStruct.dbWidget) { + if (dbStruct.dbWidget && dbStruct.dbWidget->currentMode() == DatabaseWidget::ViewMode) { Database * db = dbStruct.dbWidget->database(); if (!currentGroup.isNull()) if (Group* group = db->resolveGroup(currentGroup)) @@ -273,8 +273,6 @@ void DatabaseTabWidget::checkReloadDatabases() if (Entry* entry = db->resolveEntry(currentEntry)) dbStruct.dbWidget->entryView()->setCurrentEntry(entry); } - - //TODO: keep tab order... } else { //Ignore/cancel all edits dbStruct.dbWidget->switchToView(false); @@ -604,14 +602,13 @@ Database* DatabaseTabWidget::databaseFromDatabaseWidget(DatabaseWidget* dbWidget return Q_NULLPTR; } -void DatabaseTabWidget::insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct) +void DatabaseTabWidget::insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct, int index) { m_dbList.insert(db, dbStruct); - addTab(dbStruct.dbWidget, ""); + index = insertTab(index, dbStruct.dbWidget, ""); toggleTabbar(); updateTabName(db); - int index = databaseIndex(db); setCurrentIndex(index); connectDatabase(db); connect(dbStruct.dbWidget, SIGNAL(closeRequest()), SLOT(closeDatabaseFromSender())); diff --git a/src/gui/DatabaseTabWidget.h b/src/gui/DatabaseTabWidget.h index 6b473eb7f..c9ca003be 100644 --- a/src/gui/DatabaseTabWidget.h +++ b/src/gui/DatabaseTabWidget.h @@ -55,7 +55,8 @@ public: explicit DatabaseTabWidget(QWidget* parent = Q_NULLPTR); ~DatabaseTabWidget(); void openDatabase(const QString& fileName, const QString& pw = QString(), - const QString& keyFile = QString(), const CompositeKey& key = CompositeKey()); + const QString& keyFile = QString(), const CompositeKey& key = CompositeKey(), + int index = -1); DatabaseWidget* currentDatabaseWidget(); bool hasLockableDatabases(); @@ -105,7 +106,7 @@ private: Database* indexDatabase(int index); DatabaseManagerStruct indexDatabaseManagerStruct(int index); Database* databaseFromDatabaseWidget(DatabaseWidget* dbWidget); - void insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct); + void insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct, int index = -1); void updateRecentDatabases(const QString& filename); void connectDatabase(Database* newDb, Database* oldDb = Q_NULLPTR); void expectFileChange(const DatabaseManagerStruct& dbStruct); @@ -116,7 +117,6 @@ private: QSet m_changedFiles; QSet m_expectedFileChanges; QFileSystemWatcher* m_fileWatcher; - ReloadBehavior m_reloadBehavior; }; #endif // KEEPASSX_DATABASETABWIDGET_H diff --git a/src/gui/SettingsWidget.cpp b/src/gui/SettingsWidget.cpp index 17ab6082f..3afb5af19 100644 --- a/src/gui/SettingsWidget.cpp +++ b/src/gui/SettingsWidget.cpp @@ -90,6 +90,7 @@ void SettingsWidget::loadSettings() m_generalUi->modifiedExpandedChangedCheckBox->setChecked(config()->get("ModifiedOnExpandedStateChanges").toBool()); m_generalUi->autoSaveAfterEveryChangeCheckBox->setChecked(config()->get("AutoSaveAfterEveryChange").toBool()); m_generalUi->autoSaveOnExitCheckBox->setChecked(config()->get("AutoSaveOnExit").toBool()); + m_generalUi->reloadBehavior->setCurrentIndex(config()->get("ReloadBehavior").toInt()); m_globalAutoTypeKey = static_cast(config()->get("GlobalAutoTypeKey").toInt()); m_globalAutoTypeModifiers = static_cast(config()->get("GlobalAutoTypeModifiers").toInt()); @@ -114,6 +115,7 @@ void SettingsWidget::saveSettings() config()->set("ModifiedOnExpandedStateChanges", m_generalUi->modifiedExpandedChangedCheckBox->isChecked()); config()->set("AutoSaveAfterEveryChange", m_generalUi->autoSaveAfterEveryChangeCheckBox->isChecked()); config()->set("AutoSaveOnExit", m_generalUi->autoSaveOnExitCheckBox->isChecked()); + config()->set("ReloadBehavior", m_generalUi->reloadBehavior->currentIndex()); config()->set("GlobalAutoTypeKey", m_generalUi->autoTypeShortcutWidget->key()); config()->set("GlobalAutoTypeModifiers", static_cast(m_generalUi->autoTypeShortcutWidget->modifiers())); config()->set("security/clearclipboard", m_secUi->clearClipboardCheckBox->isChecked()); diff --git a/src/gui/SettingsWidgetGeneral.ui b/src/gui/SettingsWidgetGeneral.ui index a369cf1c2..c7b08bd35 100644 --- a/src/gui/SettingsWidgetGeneral.ui +++ b/src/gui/SettingsWidgetGeneral.ui @@ -6,8 +6,8 @@ 0 0 - 456 - 172 + 481 + 202 @@ -45,14 +45,14 @@ - + Global Auto-Type shortcut - + @@ -62,6 +62,32 @@ + + + + When database files are externally modified + + + + + + + + Always ask + + + + + Reload unmodified databases + + + + + Ignore modifications + + + +