Use QSaveFile to save databases.

This commit is contained in:
Felix Geyer 2012-07-06 18:50:52 +02:00
parent 78849c43f1
commit b579eb954e
8 changed files with 76 additions and 69 deletions

View File

@ -31,7 +31,6 @@ DatabaseOpenWidget::DatabaseOpenWidget(QWidget* parent)
: DialogyWidget(parent)
, m_ui(new Ui::DatabaseOpenWidget())
, m_db(Q_NULLPTR)
, m_file(Q_NULLPTR)
{
m_ui->setupUi(this);
@ -60,9 +59,8 @@ DatabaseOpenWidget::~DatabaseOpenWidget()
{
}
void DatabaseOpenWidget::load(QFile* file, const QString& filename)
void DatabaseOpenWidget::load(const QString& filename)
{
m_file = file;
m_filename = filename;
m_ui->labelFilename->setText(filename);
@ -121,11 +119,15 @@ void DatabaseOpenWidget::openDatabase()
config()->set("LastKeyFiles", lastKeyFiles);
m_file->reset();
QFile file(m_filename);
if (!file.open(QIODevice::ReadOnly)) {
// TODO: error message
return;
}
if (m_db) {
delete m_db;
}
m_db = reader.readDatabase(m_file, masterKey);
m_db = reader.readDatabase(&file, masterKey);
if (m_db) {
Q_EMIT editFinished(true);

View File

@ -36,7 +36,7 @@ class DatabaseOpenWidget : public DialogyWidget
public:
explicit DatabaseOpenWidget(QWidget* parent = Q_NULLPTR);
~DatabaseOpenWidget();
void load(QFile* file, const QString& filename);
void load(const QString& filename);
void enterKey(const QString& pw, const QString& keyFile);
Database* database();
@ -57,7 +57,6 @@ private Q_SLOTS:
protected:
const QScopedPointer<Ui::DatabaseOpenWidget> m_ui;
Database* m_db;
QFile* m_file;
QString m_filename;
private:

View File

@ -25,6 +25,7 @@
#include "core/Database.h"
#include "core/Group.h"
#include "core/Metadata.h"
#include "core/qsavefile.h"
#include "gui/DatabaseWidget.h"
#include "gui/DragTabBar.h"
#include "gui/FileDialog.h"
@ -32,8 +33,8 @@
#include "gui/group/GroupView.h"
DatabaseManagerStruct::DatabaseManagerStruct()
: file(Q_NULLPTR)
, dbWidget(Q_NULLPTR)
: dbWidget(Q_NULLPTR)
, saveToFilename(false)
, modified(false)
, readOnly(false)
{
@ -103,11 +104,13 @@ void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw,
{
DatabaseManagerStruct dbStruct;
QScopedPointer<QFile> file(new QFile(fileName));
// test if we can read/write or read the file
QFile file(fileName);
// TODO: error handling
if (!file->open(QIODevice::ReadWrite)) {
if (!file->open(QIODevice::ReadOnly)) {
if (!file.open(QIODevice::ReadWrite)) {
if (!file.open(QIODevice::ReadOnly)) {
// can't open
// TODO: error message
return;
}
else {
@ -115,21 +118,22 @@ void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw,
dbStruct.readOnly = true;
}
}
file.close();
Database* db = new Database();
dbStruct.dbWidget = new DatabaseWidget(db, this);
dbStruct.file = file.take();
dbStruct.fileName = QFileInfo(fileName).absoluteFilePath();
dbStruct.saveToFilename = !dbStruct.readOnly;
insertDatabase(db, dbStruct);
updateLastDatabases(dbStruct.fileName);
if (!pw.isNull() || !keyFile.isEmpty()) {
dbStruct.dbWidget->switchToOpenDatabase(dbStruct.file, dbStruct.fileName, pw, keyFile);
dbStruct.dbWidget->switchToOpenDatabase(dbStruct.fileName, pw, keyFile);
}
else {
dbStruct.dbWidget->switchToOpenDatabase(dbStruct.file, dbStruct.fileName);
dbStruct.dbWidget->switchToOpenDatabase(dbStruct.fileName);
}
}
@ -142,12 +146,6 @@ void DatabaseTabWidget::importKeePass1Database()
return;
}
QScopedPointer<QFile> file(new QFile(fileName));
// TODO: error handling
if (!file->open(QIODevice::ReadOnly)) {
return;
}
Database* db = new Database();
DatabaseManagerStruct dbStruct;
dbStruct.dbWidget = new DatabaseWidget(db, this);
@ -155,7 +153,7 @@ void DatabaseTabWidget::importKeePass1Database()
insertDatabase(db, dbStruct);
dbStruct.dbWidget->switchToImportKeepass1(file.take(), fileName);
dbStruct.dbWidget->switchToImportKeepass1(fileName);
}
void DatabaseTabWidget::emitEntrySelectionChanged()
@ -224,7 +222,6 @@ void DatabaseTabWidget::deleteDatabase(Database* db)
removeTab(index);
toggleTabbar();
m_dbList.remove(db);
delete dbStruct.file;
delete dbStruct.dbWidget;
delete db;
}
@ -243,15 +240,23 @@ void DatabaseTabWidget::saveDatabase(Database* db)
{
DatabaseManagerStruct& dbStruct = m_dbList[db];
// TODO: ensure that the data is actually written to disk
if (dbStruct.file) {
dbStruct.file->reset();
m_writer.writeDatabase(dbStruct.file, db);
dbStruct.file->resize(dbStruct.file->pos());
dbStruct.file->flush();
if (dbStruct.saveToFilename) {
bool result = false;
dbStruct.modified = false;
updateTabName(db);
QSaveFile saveFile(dbStruct.fileName);
if (saveFile.open(QIODevice::WriteOnly)) {
m_writer.writeDatabase(&saveFile, db);
result = saveFile.commit();
}
if (result) {
dbStruct.modified = false;
updateTabName(db);
}
else {
QMessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n"
+ saveFile.errorString());
}
}
else {
saveDatabaseAs(db);
@ -262,28 +267,31 @@ void DatabaseTabWidget::saveDatabaseAs(Database* db)
{
DatabaseManagerStruct& dbStruct = m_dbList[db];
QString oldFileName;
if (dbStruct.file) {
if (dbStruct.saveToFilename) {
oldFileName = dbStruct.fileName;
}
QString fileName = fileDialog()->getSaveFileName(m_window, tr("Save database as"),
oldFileName, tr("KeePass 2 Database").append(" (*.kdbx)"));
if (!fileName.isEmpty()) {
QFile* oldFile = dbStruct.file;
QScopedPointer<QFile> file(new QFile(fileName));
// TODO: error handling
if (!file->open(QIODevice::ReadWrite)) {
return;
}
dbStruct.file = file.take();
// TODO: ensure that the data is actually written to disk
m_writer.writeDatabase(dbStruct.file, db);
dbStruct.file->flush();
delete oldFile;
bool result = false;
dbStruct.modified = false;
dbStruct.fileName = QFileInfo(fileName).absoluteFilePath();
updateTabName(db);
updateLastDatabases(dbStruct.fileName);
QSaveFile saveFile(fileName);
if (saveFile.open(QIODevice::WriteOnly)) {
m_writer.writeDatabase(&saveFile, db);
result = saveFile.commit();
}
if (result) {
dbStruct.modified = false;
dbStruct.fileName = QFileInfo(fileName).absoluteFilePath();
dbStruct.saveToFilename = true;
updateTabName(db);
updateLastDatabases(dbStruct.fileName);
}
else {
QMessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n"
+ saveFile.errorString());
}
}
}
@ -403,8 +411,8 @@ void DatabaseTabWidget::updateTabName(Database* db)
QString tabName;
if (dbStruct.file) {
QFileInfo fileInfo(*dbStruct.file);
if (dbStruct.saveToFilename) {
QFileInfo fileInfo(dbStruct.fileName);
if (db->metadata()->name().isEmpty()) {
tabName = fileInfo.fileName();
@ -522,7 +530,7 @@ void DatabaseTabWidget::modified()
Database* db = static_cast<Database*>(sender());
DatabaseManagerStruct& dbStruct = m_dbList[db];
if (config()->get("AutoSaveAfterEveryChange").toBool() && dbStruct.file) {
if (config()->get("AutoSaveAfterEveryChange").toBool() && dbStruct.saveToFilename) {
saveDatabase(db);
return;
}

View File

@ -32,9 +32,9 @@ struct DatabaseManagerStruct
{
DatabaseManagerStruct();
QFile* file;
DatabaseWidget* dbWidget;
QString fileName;
bool saveToFilename;
bool modified;
bool readOnly;
};

View File

@ -509,22 +509,22 @@ void DatabaseWidget::switchToDatabaseSettings()
setCurrentIndex(4);
}
void DatabaseWidget::switchToOpenDatabase(QFile* file, const QString& fileName)
void DatabaseWidget::switchToOpenDatabase(const QString& fileName)
{
m_databaseOpenWidget->load(file, fileName);
m_databaseOpenWidget->load(fileName);
setCurrentIndex(6);
}
void DatabaseWidget::switchToOpenDatabase(QFile* file, const QString& fileName,
const QString& password, const QString& keyFile)
void DatabaseWidget::switchToOpenDatabase(const QString& fileName, const QString& password,
const QString& keyFile)
{
switchToOpenDatabase(file, fileName);
switchToOpenDatabase(fileName);
m_databaseOpenWidget->enterKey(password, keyFile);
}
void DatabaseWidget::switchToImportKeepass1(QFile* file, const QString& fileName)
void DatabaseWidget::switchToImportKeepass1(const QString& fileName)
{
m_keepass1OpenWidget->load(file, fileName);
m_keepass1OpenWidget->load(fileName);
setCurrentIndex(7);
}

View File

@ -93,9 +93,9 @@ public Q_SLOTS:
void switchToGroupEdit();
void switchToMasterKeyChange();
void switchToDatabaseSettings();
void switchToOpenDatabase(QFile* file, const QString& fileName);
void switchToOpenDatabase(QFile* file, const QString& fileName, const QString& password, const QString& keyFile);
void switchToImportKeepass1(QFile* file, const QString& fileName);
void switchToOpenDatabase(const QString& fileName);
void switchToOpenDatabase(const QString& fileName, const QString& password, const QString& keyFile);
void switchToImportKeepass1(const QString& fileName);
void toggleSearch();
private Q_SLOTS:

View File

@ -32,11 +32,6 @@ KeePass1OpenWidget::KeePass1OpenWidget(QWidget* parent)
setWindowTitle(tr("Import KeePass1 database"));
}
KeePass1OpenWidget::~KeePass1OpenWidget()
{
delete m_file;
}
void KeePass1OpenWidget::openDatabase()
{
KeePass1Reader reader;
@ -52,11 +47,15 @@ void KeePass1OpenWidget::openDatabase()
keyFileName = m_ui->comboKeyFile->currentText();
}
m_file->reset();
QFile file(m_filename);
if (!file.open(QIODevice::ReadOnly)) {
// TODO: error message
return;
}
if (m_db) {
delete m_db;
}
m_db = reader.readDatabase(m_file, password, keyFileName);
m_db = reader.readDatabase(&file, password, keyFileName);
if (m_db) {
m_db->metadata()->setName(QFileInfo(m_filename).completeBaseName());

View File

@ -26,7 +26,6 @@ class KeePass1OpenWidget : public DatabaseOpenWidget
public:
explicit KeePass1OpenWidget(QWidget* parent = Q_NULLPTR);
~KeePass1OpenWidget();
protected:
void openDatabase() Q_DECL_OVERRIDE;