Implement GUI to import KeePass 1 databases.

Closes #2
This commit is contained in:
Felix Geyer 2012-05-12 10:08:41 +02:00
parent 4807ec3368
commit 05c41c083e
11 changed files with 170 additions and 19 deletions

View File

@ -68,6 +68,7 @@ set(keepassx_SOURCES
gui/GroupModel.cpp
gui/GroupView.cpp
gui/IconModels.cpp
gui/KeePass1OpenDialog.cpp
gui/MainWindow.cpp
keys/CompositeKey.cpp
keys/FileKey.cpp

View File

@ -235,15 +235,9 @@ Database* KeePass1Reader::readDatabase(QIODevice* device, const QString& passwor
return db.take();
}
Database* KeePass1Reader::readDatabase(const QString& filename, const QString& password,
Database* KeePass1Reader::readDatabase(QIODevice* device, const QString& password,
const QString& keyfileName)
{
QFile dbFile(filename);
if (!dbFile.open(QFile::ReadOnly)) {
raiseError(dbFile.errorString());
return 0;
}
QScopedPointer<QFile> keyFile;
if (!keyfileName.isEmpty()) {
keyFile.reset(new QFile(keyfileName));
@ -253,14 +247,28 @@ Database* KeePass1Reader::readDatabase(const QString& filename, const QString& p
}
}
QScopedPointer<Database> db(readDatabase(&dbFile, password, keyFile.data()));
QScopedPointer<Database> db(readDatabase(device, password, keyFile.data()));
return db.take();
}
Database* KeePass1Reader::readDatabase(const QString& filename, const QString& password,
const QString& keyfileName)
{
QFile dbFile(filename);
if (!dbFile.open(QFile::ReadOnly)) {
raiseError(dbFile.errorString());
return 0;
}
Database* db = readDatabase(&dbFile, password, keyfileName);
if (dbFile.error() != QFile::NoError) {
raiseError(dbFile.errorString());
return 0;
}
return db.take();
return db;
}
bool KeePass1Reader::hasError()

View File

@ -36,6 +36,8 @@ public:
KeePass1Reader();
Database* readDatabase(QIODevice* device, const QString& password,
QIODevice* keyfileDevice);
Database* readDatabase(QIODevice* device, const QString& password,
const QString& keyfileName);
Database* readDatabase(const QString& filename, const QString& password,
const QString& keyfileName);
bool hasError();

View File

@ -26,7 +26,7 @@
#include "keys/FileKey.h"
#include "keys/PasswordKey.h"
DatabaseOpenDialog::DatabaseOpenDialog(QFile* file, QString filename, QWidget* parent)
DatabaseOpenDialog::DatabaseOpenDialog(QFile* file, const QString& filename, QWidget* parent)
: QDialog(parent)
, m_ui(new Ui::DatabaseOpenDialog())
, m_db(0)

View File

@ -35,25 +35,28 @@ class DatabaseOpenDialog : public QDialog
Q_OBJECT
public:
explicit DatabaseOpenDialog(QFile* file, QString filename, QWidget* parent = 0);
DatabaseOpenDialog(QFile* file, const QString& filename, QWidget* parent = 0);
~DatabaseOpenDialog();
Database* database();
void enterKey(const QString& pw, const QString& keyFile);
protected Q_SLOTS:
virtual void openDatabase();
private Q_SLOTS:
void openDatabase();
void togglePassword(bool checked);
void activatePassword();
void activateKeyFile();
void setOkButtonEnabled();
void browseKeyFile();
private:
protected:
const QScopedPointer<Ui::DatabaseOpenDialog> m_ui;
Database* m_db;
QFile* const m_file;
const QString m_filename;
private:
Q_DISABLE_COPY(DatabaseOpenDialog)
};

View File

@ -24,11 +24,12 @@
#include "core/Database.h"
#include "core/Group.h"
#include "core/Metadata.h"
#include "gui/DatabaseWidget.h"
#include "gui/FileDialog.h"
#include "gui/EntryView.h"
#include "gui/GroupView.h"
#include "gui/DatabaseOpenDialog.h"
#include "gui/DatabaseWidget.h"
#include "gui/EntryView.h"
#include "gui/FileDialog.h"
#include "gui/GroupView.h"
#include "gui/KeePass1OpenDialog.h"
DatabaseManagerStruct::DatabaseManagerStruct()
: file(0)
@ -115,6 +116,30 @@ void DatabaseTabWidget::openDatabaseDialog(const QString& pw, const QString& key
}
}
void DatabaseTabWidget::importKeePass1Database()
{
QString fileName = fileDialog()->getOpenFileName(this, tr("Open KeePass 1 database"), QString(),
tr("KeePass 1 database") + " (*.kdb);;" + tr("All files (*)"));
if (fileName.isEmpty()) {
return;
}
QScopedPointer<QFile> file(new QFile(fileName));
// TODO: error handling
if (!file->open(QIODevice::ReadOnly)) {
return;
}
m_curDbStruct.modified = true;
m_curKeyDialog = new KeePass1OpenDialog(file.take(), fileName, m_window);
connect(m_curKeyDialog, SIGNAL(accepted()), SLOT(openDatabaseRead()));
connect(m_curKeyDialog, SIGNAL(rejected()), SLOT(openDatabaseCleanup()));
m_curKeyDialog->setModal(true);
m_curKeyDialog->show();
}
void DatabaseTabWidget::openDatabaseRead()
{
Database* db = m_curKeyDialog->database();

View File

@ -56,6 +56,7 @@ public:
public Q_SLOTS:
void newDatabase();
void openDatabase();
void importKeePass1Database();
void saveDatabase(int index = -1);
void saveDatabaseAs(int index = -1);
bool closeDatabase(int index = -1);

View File

@ -0,0 +1,67 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "KeePass1OpenDialog.h"
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtGui/QMessageBox>
#include "ui_DatabaseOpenDialog.h"
#include "core/Database.h"
#include "core/Metadata.h"
#include "format/KeePass1Reader.h"
KeePass1OpenDialog::KeePass1OpenDialog(QFile* file, const QString& filename, QWidget* parent)
: DatabaseOpenDialog(file, filename, parent)
{
setWindowTitle(tr("Import KeePass1 database"));
}
KeePass1OpenDialog::~KeePass1OpenDialog()
{
delete m_file;
}
void KeePass1OpenDialog::openDatabase()
{
KeePass1Reader reader;
QString password;
QString keyFileName;
if (m_ui->checkPassword->isChecked()) {
password = m_ui->editPassword->text();
}
if (m_ui->checkKeyFile->isChecked()) {
keyFileName = m_ui->comboKeyFile->currentText();
}
m_file->reset();
m_db = reader.readDatabase(m_file, password, keyFileName);
if (m_db) {
m_db->metadata()->setName(QFileInfo(m_filename).completeBaseName());
accept();
}
else {
QMessageBox::warning(this, tr("Error"), tr("Unable to open the database.\n%1")
.arg(reader.errorString()));
m_ui->editPassword->clear();
}
}

View File

@ -0,0 +1,33 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSX_KEEPASS1OPENDIALOG_H
#define KEEPASSX_KEEPASS1OPENDIALOG_H
#include "gui/DatabaseOpenDialog.h"
class KeePass1OpenDialog : public DatabaseOpenDialog
{
public:
explicit KeePass1OpenDialog(QFile* file, const QString& filename, QWidget* parent = 0);
~KeePass1OpenDialog();
protected:
void openDatabase();
};
#endif // KEEPASSX_KEEPASS1OPENDIALOG_H

View File

@ -58,20 +58,24 @@ MainWindow::MainWindow()
SLOT(changeMasterKey()));
connect(m_ui->actionChangeDatabaseSettings, SIGNAL(triggered()), m_ui->tabWidget,
SLOT(changeDatabaseSettings()));
connect(m_ui->actionImportKeePass1, SIGNAL(triggered()), m_ui->tabWidget,
SLOT(importKeePass1Database()));
connect(m_ui->actionQuit, SIGNAL(triggered()), SLOT(close()));
connect(m_ui->actionAbout, SIGNAL(triggered()), SLOT(showAboutDialog()));
connect(m_ui->actionEntryNew, SIGNAL(triggered()), m_ui->tabWidget,
SLOT(createEntry()));
connect(m_ui->actionEntryEdit, SIGNAL(triggered()), m_ui->tabWidget,
SLOT(editEntry()));
connect(m_ui->actionEntryDelete, SIGNAL(triggered()), m_ui->tabWidget,
SLOT(deleteEntry()));
connect(m_ui->actionGroupNew, SIGNAL(triggered()), m_ui->tabWidget,
SLOT(createGroup()));
connect(m_ui->actionGroupEdit, SIGNAL(triggered()), m_ui->tabWidget,
SLOT(editGroup()));
connect(m_ui->actionGroupDelete, SIGNAL(triggered()), m_ui->tabWidget,
SLOT(deleteGroup()));
connect(m_ui->actionQuit, SIGNAL(triggered()), SLOT(close()));
connect(m_ui->actionAbout, SIGNAL(triggered()), SLOT(showAboutDialog()));
}
MainWindow::~MainWindow()

View File

@ -52,6 +52,8 @@
<addaction name="actionChangeMasterKey"/>
<addaction name="actionChangeDatabaseSettings"/>
<addaction name="separator"/>
<addaction name="actionImportKeePass1"/>
<addaction name="separator"/>
<addaction name="actionQuit"/>
</widget>
<widget class="QMenu" name="menuHelp">
@ -209,6 +211,11 @@
<string>Database settings</string>
</property>
</action>
<action name="actionImportKeePass1">
<property name="text">
<string>Import KeePass 1 database</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>