From a80d1bbd2b486297d57067d8cfddbad26e490fb4 Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Sun, 26 Nov 2017 22:30:18 -0500 Subject: [PATCH] Addressed comments; further streamlined code --- src/gui/DatabaseTabWidget.cpp | 33 ++++++++++++++++++--------------- src/gui/DatabaseWidget.cpp | 1 + 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index 8b36c0079..c1b77813f 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -166,7 +166,7 @@ void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw, updateLastDatabases(dbStruct.fileInfo.absoluteFilePath()); - if (!(pw.isNull() && keyFile.isEmpty())) { + if (!pw.isNull() || !keyFile.isEmpty()) { dbStruct.dbWidget->switchToOpenDatabase(dbStruct.fileInfo.absoluteFilePath(), pw, keyFile); } else { dbStruct.dbWidget->switchToOpenDatabase(dbStruct.fileInfo.absoluteFilePath()); @@ -308,8 +308,10 @@ bool DatabaseTabWidget::saveDatabase(Database* db, QString filePath) { DatabaseManagerStruct& dbStruct = m_dbList[db]; + // Never allow saving a locked database; it causes corruption + Q_ASSERT(dbStruct.dbWidget->currentMode() != DatabaseWidget::LockedMode); + // Release build interlock if (dbStruct.dbWidget->currentMode() == DatabaseWidget::LockedMode) { - // Never allow saving a locked database; it causes corruption // We return true since a save is not required return true; } @@ -326,6 +328,7 @@ bool DatabaseTabWidget::saveDatabase(Database* db, QString filePath) if (errorMessage.isEmpty()) { // successfully saved database file dbStruct.modified = false; + dbStruct.fileInfo = QFileInfo(filePath); dbStruct.dbWidget->databaseSaved(); updateTabName(db); emit messageDismissTab(); @@ -346,31 +349,31 @@ bool DatabaseTabWidget::saveDatabaseAs(Database* db) { while (true) { DatabaseManagerStruct& dbStruct = m_dbList[db]; - QString oldFileName; + QString oldFilePath; if (dbStruct.fileInfo.exists()) { - oldFileName = dbStruct.fileInfo.absoluteFilePath(); + oldFilePath = dbStruct.fileInfo.absoluteFilePath(); } else { - oldFileName = QDir::toNativeSeparators(QDir::homePath() + "/" + tr("Passwords").append(".kdbx")); + oldFilePath = QDir::toNativeSeparators(QDir::homePath() + "/" + tr("Passwords").append(".kdbx")); } - QString fileName = fileDialog()->getSaveFileName(this, tr("Save database as"), - oldFileName, tr("KeePass 2 Database").append(" (*.kdbx)"), + QString newFilePath = fileDialog()->getSaveFileName(this, tr("Save database as"), + oldFilePath, tr("KeePass 2 Database").append(" (*.kdbx)"), nullptr, 0, "kdbx"); - if (!fileName.isEmpty()) { - if (!saveDatabase(db, fileName)) { + if (!newFilePath.isEmpty()) { + // Ensure we don't recurse back into this function + dbStruct.readOnly = false; + + if (!saveDatabase(db, newFilePath)) { // Failed to save, try again continue; } - dbStruct.modified = false; - dbStruct.readOnly = false; - dbStruct.fileInfo = QFileInfo(fileName); dbStruct.dbWidget->updateFilename(dbStruct.fileInfo.absoluteFilePath()); - updateTabName(db); updateLastDatabases(dbStruct.fileInfo.absoluteFilePath()); return true; - } else { - return false; } + + // Canceled file selection + return false; } } diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index f17a7ef02..56400c246 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -1287,6 +1287,7 @@ void DatabaseWidget::reloadDatabaseFile() tr("Could not open the new database file while attempting to autoreload this database.") .append("\n").append(file.errorString()), MessageWidget::Error); + // HACK: Directly calling the database's signal // Mark db as modified since existing data may differ from file or file was deleted m_db->modified(); }