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 "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;
@ -412,3 +414,28 @@ Database* Database::unlockFromStdin(QString databaseFilename)
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;
}; };