Addressed comments; further streamlined code

This commit is contained in:
Jonathan White 2017-11-26 22:30:18 -05:00
parent c2d04499ab
commit a80d1bbd2b
2 changed files with 19 additions and 15 deletions

View File

@ -166,7 +166,7 @@ void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw,
updateLastDatabases(dbStruct.fileInfo.absoluteFilePath()); updateLastDatabases(dbStruct.fileInfo.absoluteFilePath());
if (!(pw.isNull() && keyFile.isEmpty())) { if (!pw.isNull() || !keyFile.isEmpty()) {
dbStruct.dbWidget->switchToOpenDatabase(dbStruct.fileInfo.absoluteFilePath(), pw, keyFile); dbStruct.dbWidget->switchToOpenDatabase(dbStruct.fileInfo.absoluteFilePath(), pw, keyFile);
} else { } else {
dbStruct.dbWidget->switchToOpenDatabase(dbStruct.fileInfo.absoluteFilePath()); dbStruct.dbWidget->switchToOpenDatabase(dbStruct.fileInfo.absoluteFilePath());
@ -308,8 +308,10 @@ bool DatabaseTabWidget::saveDatabase(Database* db, QString filePath)
{ {
DatabaseManagerStruct& dbStruct = m_dbList[db]; DatabaseManagerStruct& dbStruct = m_dbList[db];
if (dbStruct.dbWidget->currentMode() == DatabaseWidget::LockedMode) {
// Never allow saving a locked database; it causes corruption // 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) {
// We return true since a save is not required // We return true since a save is not required
return true; return true;
} }
@ -326,6 +328,7 @@ bool DatabaseTabWidget::saveDatabase(Database* db, QString filePath)
if (errorMessage.isEmpty()) { if (errorMessage.isEmpty()) {
// successfully saved database file // successfully saved database file
dbStruct.modified = false; dbStruct.modified = false;
dbStruct.fileInfo = QFileInfo(filePath);
dbStruct.dbWidget->databaseSaved(); dbStruct.dbWidget->databaseSaved();
updateTabName(db); updateTabName(db);
emit messageDismissTab(); emit messageDismissTab();
@ -346,31 +349,31 @@ bool DatabaseTabWidget::saveDatabaseAs(Database* db)
{ {
while (true) { while (true) {
DatabaseManagerStruct& dbStruct = m_dbList[db]; DatabaseManagerStruct& dbStruct = m_dbList[db];
QString oldFileName; QString oldFilePath;
if (dbStruct.fileInfo.exists()) { if (dbStruct.fileInfo.exists()) {
oldFileName = dbStruct.fileInfo.absoluteFilePath(); oldFilePath = dbStruct.fileInfo.absoluteFilePath();
} else { } 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"), QString newFilePath = fileDialog()->getSaveFileName(this, tr("Save database as"),
oldFileName, tr("KeePass 2 Database").append(" (*.kdbx)"), oldFilePath, tr("KeePass 2 Database").append(" (*.kdbx)"),
nullptr, 0, "kdbx"); nullptr, 0, "kdbx");
if (!fileName.isEmpty()) { if (!newFilePath.isEmpty()) {
if (!saveDatabase(db, fileName)) { // Ensure we don't recurse back into this function
dbStruct.readOnly = false;
if (!saveDatabase(db, newFilePath)) {
// Failed to save, try again // Failed to save, try again
continue; continue;
} }
dbStruct.modified = false;
dbStruct.readOnly = false;
dbStruct.fileInfo = QFileInfo(fileName);
dbStruct.dbWidget->updateFilename(dbStruct.fileInfo.absoluteFilePath()); dbStruct.dbWidget->updateFilename(dbStruct.fileInfo.absoluteFilePath());
updateTabName(db);
updateLastDatabases(dbStruct.fileInfo.absoluteFilePath()); updateLastDatabases(dbStruct.fileInfo.absoluteFilePath());
return true; return true;
} else {
return false;
} }
// Canceled file selection
return false;
} }
} }

View File

@ -1287,6 +1287,7 @@ void DatabaseWidget::reloadDatabaseFile()
tr("Could not open the new database file while attempting to autoreload this database.") tr("Could not open the new database file while attempting to autoreload this database.")
.append("\n").append(file.errorString()), .append("\n").append(file.errorString()),
MessageWidget::Error); MessageWidget::Error);
// HACK: Directly calling the database's signal
// Mark db as modified since existing data may differ from file or file was deleted // Mark db as modified since existing data may differ from file or file was deleted
m_db->modified(); m_db->modified();
} }