0
0
mirror of https://github.com/keepassxreboot/keepassxc.git synced 2024-09-20 12:22:16 +02:00

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 <QCommandLineParser>
#include <QCoreApplication>
#include <QSaveFile>
#include <QStringList>
#include <QTextStream>
#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;
}

View File

@ -19,6 +19,7 @@
#include "Database.h"
#include <QFile>
#include <QSaveFile>
#include <QTextStream>
#include <QTimer>
#include <QXmlStreamReader>
@ -28,6 +29,7 @@
#include "crypto/Random.h"
#include "format/KeePass2.h"
#include "format/KeePass2Reader.h"
#include "format/KeePass2Writer.h"
QHash<Uuid, Database*> Database::m_uuidMap;
@ -222,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;
}
@ -291,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()
@ -396,7 +394,6 @@ Database* Database::openDatabaseFile(QString fileName, CompositeKey key)
}
return db;
}
Database* Database::unlockFromStdin(QString databaseFilename)
@ -410,5 +407,28 @@ Database* Database::unlockFromStdin(QString databaseFilename)
QString line = inputTextStream.readLine();
CompositeKey key = CompositeKey::readFromLine(line);
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 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.

View File

@ -20,7 +20,6 @@
#include <QFileInfo>
#include <QLockFile>
#include <QSaveFile>
#include <QTabWidget>
#include <QPushButton>
@ -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);
}

View File

@ -22,7 +22,6 @@
#include <QHash>
#include <QTabWidget>
#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<Database*, DatabaseManagerStruct> m_dbList;
DatabaseWidgetStateSync* m_dbWidgetStateSync;
};

View File

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

View File

@ -28,7 +28,6 @@
#include <QStackedWidget>
#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<QComboBox*> m_combos;
Database *m_db;
Database* m_db;
KeePass2Writer m_writer;
static const QStringList m_columnHeader;
void configParser();
void updateTableview();