Auto-reload settings.

This commit is contained in:
Francois Ferrand 2013-04-30 17:31:08 +02:00
parent f4ff8b17f7
commit d2ab008aa0
5 changed files with 52 additions and 26 deletions

View File

@ -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);
}

View File

@ -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()));

View File

@ -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<QString> m_changedFiles;
QSet<QString> m_expectedFileChanges;
QFileSystemWatcher* m_fileWatcher;
ReloadBehavior m_reloadBehavior;
};
#endif // KEEPASSX_DATABASETABWIDGET_H

View File

@ -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<Qt::Key>(config()->get("GlobalAutoTypeKey").toInt());
m_globalAutoTypeModifiers = static_cast<Qt::KeyboardModifiers>(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<int>(m_generalUi->autoTypeShortcutWidget->modifiers()));
config()->set("security/clearclipboard", m_secUi->clearClipboardCheckBox->isChecked());

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>456</width>
<height>172</height>
<width>481</width>
<height>202</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
@ -45,14 +45,14 @@
</property>
</widget>
</item>
<item row="5" column="0">
<item row="6" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Global Auto-Type shortcut</string>
</property>
</widget>
</item>
<item row="5" column="1">
<item row="6" column="1">
<widget class="ShortcutWidget" name="autoTypeShortcutWidget"/>
</item>
<item row="1" column="0">
@ -62,6 +62,32 @@
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>When database files are externally modified</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QComboBox" name="reloadBehavior">
<item>
<property name="text">
<string>Always ask</string>
</property>
</item>
<item>
<property name="text">
<string>Reload unmodified databases</string>
</property>
</item>
<item>
<property name="text">
<string>Ignore modifications</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
<customwidgets>