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

Adding saveToFile function.

This commit is contained in:
Louis-Bertrand Varin 2017-06-14 19:50:19 -04:00
parent 139658b5c3
commit f3f6f6a493
4 changed files with 41 additions and 28 deletions

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;
@ -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();
}
}

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;
};