Merge pull request #644 from louib/adding_save_database_file

Adding saveToFile function to Database.
This commit is contained in:
Janek Bevendorff 2017-06-15 23:11:24 +02:00 committed by GitHub
commit f817eaa5c8
7 changed files with 53 additions and 63 deletions

View File

@ -22,12 +22,10 @@
#include <QApplication> #include <QApplication>
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QCoreApplication> #include <QCoreApplication>
#include <QSaveFile>
#include <QStringList> #include <QStringList>
#include <QTextStream> #include <QTextStream>
#include "core/Database.h" #include "core/Database.h"
#include "format/KeePass2Writer.h"
#include "gui/UnlockDatabaseDialog.h" #include "gui/UnlockDatabaseDialog.h"
int Merge::execute(int argc, char** argv) int Merge::execute(int argc, char** argv)
@ -95,26 +93,13 @@ int Merge::execute(int argc, char** argv)
} }
db1->merge(db2); db1->merge(db2);
QString errorMessage = db1->saveToFile(args.at(0));
QSaveFile saveFile(args.at(0)); if (!errorMessage.isEmpty()) {
if (!saveFile.open(QIODevice::WriteOnly)) { qCritical("Unable to save database to file : %s", qPrintable(errorMessage));
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()));
return EXIT_FAILURE; return EXIT_FAILURE;
} }
out << "Successfully merged the database files.\n"; out << "Successfully merged the database files.\n";
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -19,6 +19,7 @@
#include "Database.h" #include "Database.h"
#include <QFile> #include <QFile>
#include <QSaveFile>
#include <QTextStream> #include <QTextStream>
#include <QTimer> #include <QTimer>
#include <QXmlStreamReader> #include <QXmlStreamReader>
@ -28,6 +29,7 @@
#include "crypto/Random.h" #include "crypto/Random.h"
#include "format/KeePass2.h" #include "format/KeePass2.h"
#include "format/KeePass2Reader.h" #include "format/KeePass2Reader.h"
#include "format/KeePass2Writer.h"
QHash<Uuid, Database*> Database::m_uuidMap; QHash<Uuid, Database*> Database::m_uuidMap;
@ -222,14 +224,12 @@ bool Database::setTransformRounds(quint64 rounds)
return true; return true;
} }
bool Database::setKey(const CompositeKey& key, const QByteArray& transformSeed, bool Database::setKey(const CompositeKey& key, const QByteArray& transformSeed, bool updateChangedTime)
bool updateChangedTime)
{ {
bool ok; bool ok;
QString errorString; QString errorString;
QByteArray transformedMasterKey = QByteArray transformedMasterKey = key.transform(transformSeed, transformRounds(), &ok, &errorString);
key.transform(transformSeed, transformRounds(), &ok, &errorString);
if (!ok) { if (!ok) {
return false; return false;
} }
@ -291,23 +291,21 @@ void Database::recycleEntry(Entry* entry)
createRecycleBin(); createRecycleBin();
} }
entry->setGroup(metadata()->recycleBin()); entry->setGroup(metadata()->recycleBin());
} } else {
else {
delete entry; delete entry;
} }
} }
void Database::recycleGroup(Group* group) void Database::recycleGroup(Group* group)
{ {
if (m_metadata->recycleBinEnabled()) { if (m_metadata->recycleBinEnabled()) {
if (!m_metadata->recycleBin()) { if (!m_metadata->recycleBin()) {
createRecycleBin(); createRecycleBin();
} }
group->setParent(metadata()->recycleBin()); group->setParent(metadata()->recycleBin());
} } else {
else {
delete group; delete group;
} }
} }
void Database::emptyRecycleBin() void Database::emptyRecycleBin()
@ -396,7 +394,6 @@ Database* Database::openDatabaseFile(QString fileName, CompositeKey key)
} }
return db; return db;
} }
Database* Database::unlockFromStdin(QString databaseFilename) Database* Database::unlockFromStdin(QString databaseFilename)
@ -410,5 +407,28 @@ Database* Database::unlockFromStdin(QString databaseFilename)
QString line = inputTextStream.readLine(); QString line = inputTextStream.readLine();
CompositeKey key = CompositeKey::readFromLine(line); CompositeKey key = CompositeKey::readFromLine(line);
return Database::openDatabaseFile(databaseFilename, key); 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();
}
} }

View File

@ -113,6 +113,7 @@ public:
void setEmitModified(bool value); void setEmitModified(bool value);
void copyAttributesFrom(const Database* other); void copyAttributesFrom(const Database* other);
void merge(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. * Returns a unique id that is only valid as long as the Database exists.

View File

@ -20,7 +20,6 @@
#include <QFileInfo> #include <QFileInfo>
#include <QLockFile> #include <QLockFile>
#include <QSaveFile>
#include <QTabWidget> #include <QTabWidget>
#include <QPushButton> #include <QPushButton>
@ -351,36 +350,24 @@ bool DatabaseTabWidget::saveDatabase(Database* db)
DatabaseManagerStruct& dbStruct = m_dbList[db]; DatabaseManagerStruct& dbStruct = m_dbList[db];
if (dbStruct.saveToFilename) { 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()) { dbStruct.dbWidget->blockAutoReload(true);
emit messageTab(tr("Writing the database failed.").append("\n") QString errorMessage = db->saveToFile(dbStruct.canonicalFilePath);
.append(m_writer.errorString()), MessageWidget::Error); dbStruct.dbWidget->blockAutoReload(false);
return false;
}
if (saveFile.commit()) { if (errorMessage.isEmpty()) {
// successfully saved database file // successfully saved database file
dbStruct.modified = false; dbStruct.modified = false;
dbStruct.dbWidget->databaseSaved(); dbStruct.dbWidget->databaseSaved();
updateTabName(db); updateTabName(db);
emit messageDismissTab(); emit messageDismissTab();
return true; return true;
} else {
emit messageTab(tr("Writing the database failed.").append("\n")
.append(saveFile.errorString()), MessageWidget::Error);
return false;
}
} else { } else {
emit messageTab(tr("Writing the database failed.").append("\n") emit messageTab(tr("Writing the database failed.").append("\n").append(errorMessage),
.append(saveFile.errorString()), MessageWidget::Error); MessageWidget::Error);
return false; return false;
} }
} else { } else {
return saveDatabaseAs(db); return saveDatabaseAs(db);
} }

View File

@ -22,7 +22,6 @@
#include <QHash> #include <QHash>
#include <QTabWidget> #include <QTabWidget>
#include "format/KeePass2Writer.h"
#include "gui/DatabaseWidget.h" #include "gui/DatabaseWidget.h"
#include "gui/MessageWidget.h" #include "gui/MessageWidget.h"
@ -118,7 +117,6 @@ private:
void updateLastDatabases(const QString& filename); void updateLastDatabases(const QString& filename);
void connectDatabase(Database* newDb, Database* oldDb = nullptr); void connectDatabase(Database* newDb, Database* oldDb = nullptr);
KeePass2Writer m_writer;
QHash<Database*, DatabaseManagerStruct> m_dbList; QHash<Database*, DatabaseManagerStruct> m_dbList;
DatabaseWidgetStateSync* m_dbWidgetStateSync; DatabaseWidgetStateSync* m_dbWidgetStateSync;
}; };

View File

@ -23,6 +23,7 @@
#include <QFileInfo> #include <QFileInfo>
#include <QSpacerItem> #include <QSpacerItem>
#include "format/KeePass2Writer.h"
#include "gui/MessageBox.h" #include "gui/MessageBox.h"
#include "gui/MessageWidget.h" #include "gui/MessageWidget.h"

View File

@ -28,7 +28,6 @@
#include <QStackedWidget> #include <QStackedWidget>
#include "core/Metadata.h" #include "core/Metadata.h"
#include "format/KeePass2Writer.h"
#include "gui/csvImport/CsvParserModel.h" #include "gui/csvImport/CsvParserModel.h"
#include "keys/PasswordKey.h" #include "keys/PasswordKey.h"
@ -42,7 +41,7 @@ class CsvImportWidget : public QWidget
Q_OBJECT Q_OBJECT
public: public:
explicit CsvImportWidget(QWidget *parent = nullptr); explicit CsvImportWidget(QWidget* parent = nullptr);
~CsvImportWidget(); ~CsvImportWidget();
void load(const QString& filename, Database* const db); void load(const QString& filename, Database* const db);
@ -65,9 +64,8 @@ private:
QStringListModel* const m_comboModel; QStringListModel* const m_comboModel;
QSignalMapper* m_comboMapper; QSignalMapper* m_comboMapper;
QList<QComboBox*> m_combos; QList<QComboBox*> m_combos;
Database *m_db; Database* m_db;
KeePass2Writer m_writer;
static const QStringList m_columnHeader; static const QStringList m_columnHeader;
void configParser(); void configParser();
void updateTableview(); void updateTableview();