Move the code to open database files from DatabaseTabWidget to DatabaseOpenDialog.

This commit is contained in:
Felix Geyer 2012-04-05 19:03:55 +02:00
parent 9d1838a0fe
commit 8fc68045f6
4 changed files with 37 additions and 24 deletions

View File

@ -22,12 +22,15 @@
#include "core/Config.h"
#include "gui/FileDialog.h"
#include "format/KeePass2Reader.h"
#include "keys/FileKey.h"
#include "keys/PasswordKey.h"
DatabaseOpenDialog::DatabaseOpenDialog(const QString& filename, QWidget* parent)
DatabaseOpenDialog::DatabaseOpenDialog(QFile* file, QString filename, QWidget* parent)
: QDialog(parent)
, m_ui(new Ui::DatabaseOpenDialog())
, m_db(0)
, m_file(file)
, m_filename(filename)
{
m_ui->setupUi(this);
@ -44,7 +47,7 @@ DatabaseOpenDialog::DatabaseOpenDialog(const QString& filename, QWidget* parent)
connect(m_ui->checkKeyFile, SIGNAL(toggled(bool)), SLOT(setOkButtonEnabled()));
connect(m_ui->comboKeyFile, SIGNAL(editTextChanged(QString)), SLOT(setOkButtonEnabled()));
connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(createKey()));
connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(openDatabase()));
connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(reject()));
QHash<QString,QVariant> lastKeyFiles = config()->get("LastKeyFiles").toHash();
@ -58,15 +61,18 @@ DatabaseOpenDialog::~DatabaseOpenDialog()
{
}
CompositeKey DatabaseOpenDialog::key()
Database* DatabaseOpenDialog::database()
{
return m_key;
return m_db;
}
void DatabaseOpenDialog::createKey()
void DatabaseOpenDialog::openDatabase()
{
KeePass2Reader reader;
CompositeKey masterKey;
if (m_ui->checkPassword->isChecked()) {
m_key.addKey(PasswordKey(m_ui->editPassword->text()));
masterKey.addKey(PasswordKey(m_ui->editPassword->text()));
}
QHash<QString,QVariant> lastKeyFiles = config()->get("LastKeyFiles").toHash();
@ -79,7 +85,7 @@ void DatabaseOpenDialog::createKey()
QMessageBox::warning(this, tr("Error"), tr("Can't open key file:\n%1").arg(errorMsg));
return;
}
m_key.addKey(key);
masterKey.addKey(key);
lastKeyFiles[m_filename] = keyFilename;
}
else {
@ -88,8 +94,17 @@ void DatabaseOpenDialog::createKey()
config()->set("LastKeyFiles", lastKeyFiles);
m_file->reset();
m_db = reader.readDatabase(m_file, masterKey);
if (m_db) {
accept();
}
else {
QMessageBox::warning(this, tr("Error"), tr("Unable to open the database.\n%1").arg(reader.errorString()));
m_ui->editPassword->clear();
}
}
void DatabaseOpenDialog::togglePassword(bool checked)
{

View File

@ -23,6 +23,9 @@
#include "keys/CompositeKey.h"
class Database;
class QFile;
namespace Ui {
class DatabaseOpenDialog;
}
@ -32,12 +35,12 @@ class DatabaseOpenDialog : public QDialog
Q_OBJECT
public:
explicit DatabaseOpenDialog(const QString& filename, QWidget* parent = 0);
explicit DatabaseOpenDialog(QFile* file, QString filename, QWidget* parent = 0);
~DatabaseOpenDialog();
CompositeKey key();
Database* database();
private Q_SLOTS:
void createKey();
void openDatabase();
void togglePassword(bool checked);
void activatePassword();
void activateKeyFile();
@ -46,7 +49,8 @@ private Q_SLOTS:
private:
QScopedPointer<Ui::DatabaseOpenDialog> m_ui;
CompositeKey m_key;
Database* m_db;
QFile* m_file;
QString m_filename;
Q_DISABLE_COPY(DatabaseOpenDialog)

View File

@ -86,7 +86,7 @@ void DatabaseTabWidget::openDatabase(const QString& fileName)
void DatabaseTabWidget::openDatabaseDialog()
{
m_curKeyDialog = new DatabaseOpenDialog(m_curDbStruct.fileName, m_window);
m_curKeyDialog = new DatabaseOpenDialog(m_curDbStruct.file, m_curDbStruct.fileName, m_window);
connect(m_curKeyDialog, SIGNAL(accepted()), SLOT(openDatabaseRead()));
connect(m_curKeyDialog, SIGNAL(rejected()), SLOT(openDatabaseCleanup()));
m_curKeyDialog->setModal(true);
@ -95,20 +95,15 @@ void DatabaseTabWidget::openDatabaseDialog()
void DatabaseTabWidget::openDatabaseRead()
{
m_curDbStruct.file->reset();
Database* db = m_reader.readDatabase(m_curDbStruct.file, m_curKeyDialog->key());
Database* db = m_curKeyDialog->database();
delete m_curKeyDialog;
m_curKeyDialog = 0;
if (!db) {
openDatabaseDialog();
}
else {
m_curDbStruct.dbWidget = new DatabaseWidget(db, this);
insertDatabase(db, m_curDbStruct);
m_curDbStruct = DatabaseManagerStruct();
}
}
void DatabaseTabWidget::openDatabaseCleanup()
{

View File

@ -81,7 +81,6 @@ private:
void insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct);
QWidget* m_window;
KeePass2Reader m_reader;
KeePass2Writer m_writer;
QHash<Database*, DatabaseManagerStruct> m_dbList;
DatabaseManagerStruct m_curDbStruct;