From f3f6f6a493cf4a46ab77c9eb2c534a8bbb69934f Mon Sep 17 00:00:00 2001 From: Louis-Bertrand Varin Date: Wed, 14 Jun 2017 19:50:19 -0400 Subject: [PATCH 1/4] Adding saveToFile function. --- src/core/Database.cpp | 27 ++++++++++++++++++++++++ src/core/Database.h | 1 + src/gui/DatabaseTabWidget.cpp | 39 ++++++++++++----------------------- src/gui/DatabaseTabWidget.h | 2 -- 4 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/core/Database.cpp b/src/core/Database.cpp index 0572f5d72..aab9b5571 100644 --- a/src/core/Database.cpp +++ b/src/core/Database.cpp @@ -19,6 +19,7 @@ #include "Database.h" #include +#include #include #include #include @@ -28,6 +29,7 @@ #include "crypto/Random.h" #include "format/KeePass2.h" #include "format/KeePass2Reader.h" +#include "format/KeePass2Writer.h" QHash Database::m_uuidMap; @@ -412,3 +414,28 @@ Database* Database::unlockFromStdin(QString databaseFilename) return Database::openDatabaseFile(databaseFilename, key); } + +QString Database::saveToFile(QString filePath) +{ + KeePass2Writer writer; + QSaveFile saveFile(filePath); + if (saveFile.open(QIODevice::WriteOnly)) { + + // write the database to the file + writer.writeDatabase(&saveFile, this); + + if (writer.hasError()) { + return writer.errorString(); + } + + if (saveFile.commit()) { + // successfully saved database file + return QString(); + } else { + return saveFile.errorString(); + } + } else { + return saveFile.errorString(); + } + +} diff --git a/src/core/Database.h b/src/core/Database.h index 8e62e5357..a799e0b3b 100644 --- a/src/core/Database.h +++ b/src/core/Database.h @@ -113,6 +113,7 @@ public: void setEmitModified(bool value); void copyAttributesFrom(const Database* other); void merge(const Database* other); + QString saveToFile(QString filePath); /** * Returns a unique id that is only valid as long as the Database exists. diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index 038174617..e13158eac 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -20,7 +20,6 @@ #include #include -#include #include #include @@ -351,36 +350,24 @@ bool DatabaseTabWidget::saveDatabase(Database* db) DatabaseManagerStruct& dbStruct = m_dbList[db]; if (dbStruct.saveToFilename) { - QSaveFile saveFile(dbStruct.canonicalFilePath); - if (saveFile.open(QIODevice::WriteOnly)) { - // write the database to the file - dbStruct.dbWidget->blockAutoReload(true); - m_writer.writeDatabase(&saveFile, db); - dbStruct.dbWidget->blockAutoReload(false); - if (m_writer.hasError()) { - emit messageTab(tr("Writing the database failed.").append("\n") - .append(m_writer.errorString()), MessageWidget::Error); - return false; - } + dbStruct.dbWidget->blockAutoReload(true); + QString errorMessage = db->saveToFile(dbStruct.canonicalFilePath); + dbStruct.dbWidget->blockAutoReload(false); - if (saveFile.commit()) { - // successfully saved database file - dbStruct.modified = false; - dbStruct.dbWidget->databaseSaved(); - updateTabName(db); - emit messageDismissTab(); - return true; - } else { - emit messageTab(tr("Writing the database failed.").append("\n") - .append(saveFile.errorString()), MessageWidget::Error); - return false; - } + if (errorMessage.isEmpty()) { + // successfully saved database file + dbStruct.modified = false; + dbStruct.dbWidget->databaseSaved(); + updateTabName(db); + emit messageDismissTab(); + return true; } else { - emit messageTab(tr("Writing the database failed.").append("\n") - .append(saveFile.errorString()), MessageWidget::Error); + emit messageTab(tr("Writing the database failed.").append("\n").append(errorMessage), + MessageWidget::Error); return false; } + } else { return saveDatabaseAs(db); } diff --git a/src/gui/DatabaseTabWidget.h b/src/gui/DatabaseTabWidget.h index 85b3f3af6..847eaef05 100644 --- a/src/gui/DatabaseTabWidget.h +++ b/src/gui/DatabaseTabWidget.h @@ -22,7 +22,6 @@ #include #include -#include "format/KeePass2Writer.h" #include "gui/DatabaseWidget.h" #include "gui/MessageWidget.h" @@ -118,7 +117,6 @@ private: void updateLastDatabases(const QString& filename); void connectDatabase(Database* newDb, Database* oldDb = nullptr); - KeePass2Writer m_writer; QHash m_dbList; DatabaseWidgetStateSync* m_dbWidgetStateSync; }; From 46f7d971766d404c7f666d340d5bbfb08b759407 Mon Sep 17 00:00:00 2001 From: Louis-Bertrand Varin Date: Wed, 14 Jun 2017 20:00:09 -0400 Subject: [PATCH 2/4] Using saveToFile in Merge command. --- src/cli/Merge.cpp | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/src/cli/Merge.cpp b/src/cli/Merge.cpp index 738f0ee43..c69dcd604 100644 --- a/src/cli/Merge.cpp +++ b/src/cli/Merge.cpp @@ -22,12 +22,10 @@ #include #include #include -#include #include #include #include "core/Database.h" -#include "format/KeePass2Writer.h" #include "gui/UnlockDatabaseDialog.h" int Merge::execute(int argc, char** argv) @@ -95,26 +93,13 @@ int Merge::execute(int argc, char** argv) } db1->merge(db2); - - QSaveFile saveFile(args.at(0)); - if (!saveFile.open(QIODevice::WriteOnly)) { - qCritical("Unable to open file %s for writing.", qPrintable(args.at(0))); - return EXIT_FAILURE; - } - - KeePass2Writer writer; - writer.writeDatabase(&saveFile, db1); - - if (writer.hasError()) { - qCritical("Error while updating the database:\n%s\n", qPrintable(writer.errorString())); - return EXIT_FAILURE; - } - - if (!saveFile.commit()) { - qCritical("Error while updating the database:\n%s\n", qPrintable(writer.errorString())); + QString errorMessage = db1->saveToFile(args.at(0)); + if (!errorMessage.isEmpty()) { + qCritical("Unable to save database to file : %s", qPrintable(errorMessage)); return EXIT_FAILURE; } out << "Successfully merged the database files.\n"; return EXIT_SUCCESS; + } From e0e8521eb9c88cfa355306eee70bdc461505bec8 Mon Sep 17 00:00:00 2001 From: Louis-Bertrand Varin Date: Thu, 15 Jun 2017 10:26:37 -0400 Subject: [PATCH 3/4] Removing m_writer from CsvImportWidget.h --- src/gui/csvImport/CsvImportWidget.cpp | 1 + src/gui/csvImport/CsvImportWidget.h | 6 ++---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/gui/csvImport/CsvImportWidget.cpp b/src/gui/csvImport/CsvImportWidget.cpp index 1e49d283e..23db871f3 100644 --- a/src/gui/csvImport/CsvImportWidget.cpp +++ b/src/gui/csvImport/CsvImportWidget.cpp @@ -23,6 +23,7 @@ #include #include +#include "format/KeePass2Writer.h" #include "gui/MessageBox.h" #include "gui/MessageWidget.h" diff --git a/src/gui/csvImport/CsvImportWidget.h b/src/gui/csvImport/CsvImportWidget.h index d006b44e2..463a92c5c 100644 --- a/src/gui/csvImport/CsvImportWidget.h +++ b/src/gui/csvImport/CsvImportWidget.h @@ -28,7 +28,6 @@ #include #include "core/Metadata.h" -#include "format/KeePass2Writer.h" #include "gui/csvImport/CsvParserModel.h" #include "keys/PasswordKey.h" @@ -42,7 +41,7 @@ class CsvImportWidget : public QWidget Q_OBJECT public: - explicit CsvImportWidget(QWidget *parent = nullptr); + explicit CsvImportWidget(QWidget* parent = nullptr); ~CsvImportWidget(); void load(const QString& filename, Database* const db); @@ -65,9 +64,8 @@ private: QStringListModel* const m_comboModel; QSignalMapper* m_comboMapper; QList m_combos; - Database *m_db; + Database* m_db; - KeePass2Writer m_writer; static const QStringList m_columnHeader; void configParser(); void updateTableview(); From 574c5cf1b26acda94cb615ff7252fa99033c5134 Mon Sep 17 00:00:00 2001 From: Louis-Bertrand Varin Date: Thu, 15 Jun 2017 10:31:14 -0400 Subject: [PATCH 4/4] clang-format Database.cpp --- src/core/Database.cpp | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/core/Database.cpp b/src/core/Database.cpp index aab9b5571..e1a8610a2 100644 --- a/src/core/Database.cpp +++ b/src/core/Database.cpp @@ -224,14 +224,12 @@ bool Database::setTransformRounds(quint64 rounds) return true; } -bool Database::setKey(const CompositeKey& key, const QByteArray& transformSeed, - bool updateChangedTime) +bool Database::setKey(const CompositeKey& key, const QByteArray& transformSeed, bool updateChangedTime) { bool ok; QString errorString; - QByteArray transformedMasterKey = - key.transform(transformSeed, transformRounds(), &ok, &errorString); + QByteArray transformedMasterKey = key.transform(transformSeed, transformRounds(), &ok, &errorString); if (!ok) { return false; } @@ -293,23 +291,21 @@ void Database::recycleEntry(Entry* entry) createRecycleBin(); } entry->setGroup(metadata()->recycleBin()); - } - else { + } else { delete entry; } } void Database::recycleGroup(Group* group) { - if (m_metadata->recycleBinEnabled()) { + if (m_metadata->recycleBinEnabled()) { if (!m_metadata->recycleBin()) { createRecycleBin(); } group->setParent(metadata()->recycleBin()); - } - else { + } else { delete group; - } + } } void Database::emptyRecycleBin() @@ -398,7 +394,6 @@ Database* Database::openDatabaseFile(QString fileName, CompositeKey key) } return db; - } Database* Database::unlockFromStdin(QString databaseFilename) @@ -412,7 +407,6 @@ Database* Database::unlockFromStdin(QString databaseFilename) QString line = inputTextStream.readLine(); CompositeKey key = CompositeKey::readFromLine(line); return Database::openDatabaseFile(databaseFilename, key); - } QString Database::saveToFile(QString filePath) @@ -437,5 +431,4 @@ QString Database::saveToFile(QString filePath) } else { return saveFile.errorString(); } - }