diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4f767a78..19b75c57 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/format/KeePass1Reader.cpp b/src/format/KeePass1Reader.cpp index aa12bec6..ea7ab5b3 100644 --- a/src/format/KeePass1Reader.cpp +++ b/src/format/KeePass1Reader.cpp @@ -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 keyFile; if (!keyfileName.isEmpty()) { keyFile.reset(new QFile(keyfileName)); @@ -253,14 +247,28 @@ Database* KeePass1Reader::readDatabase(const QString& filename, const QString& p } } - QScopedPointer db(readDatabase(&dbFile, password, keyFile.data())); + QScopedPointer 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() diff --git a/src/format/KeePass1Reader.h b/src/format/KeePass1Reader.h index e94ab3b0..3375c100 100644 --- a/src/format/KeePass1Reader.h +++ b/src/format/KeePass1Reader.h @@ -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(); diff --git a/src/gui/DatabaseOpenDialog.cpp b/src/gui/DatabaseOpenDialog.cpp index aa1a08cc..cd6950fc 100644 --- a/src/gui/DatabaseOpenDialog.cpp +++ b/src/gui/DatabaseOpenDialog.cpp @@ -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) diff --git a/src/gui/DatabaseOpenDialog.h b/src/gui/DatabaseOpenDialog.h index 7c04b668..4f9cef71 100644 --- a/src/gui/DatabaseOpenDialog.h +++ b/src/gui/DatabaseOpenDialog.h @@ -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 m_ui; Database* m_db; QFile* const m_file; const QString m_filename; +private: Q_DISABLE_COPY(DatabaseOpenDialog) }; diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index d2ce62ee..dec24eff 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -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 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(); diff --git a/src/gui/DatabaseTabWidget.h b/src/gui/DatabaseTabWidget.h index c9819d48..8ec617df 100644 --- a/src/gui/DatabaseTabWidget.h +++ b/src/gui/DatabaseTabWidget.h @@ -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); diff --git a/src/gui/KeePass1OpenDialog.cpp b/src/gui/KeePass1OpenDialog.cpp new file mode 100644 index 00000000..6c9fee18 --- /dev/null +++ b/src/gui/KeePass1OpenDialog.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2012 Felix Geyer + * + * 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 . + */ + +#include "KeePass1OpenDialog.h" + +#include +#include +#include + +#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(); + } +} diff --git a/src/gui/KeePass1OpenDialog.h b/src/gui/KeePass1OpenDialog.h new file mode 100644 index 00000000..2e2db4f0 --- /dev/null +++ b/src/gui/KeePass1OpenDialog.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2012 Felix Geyer + * + * 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 . + */ + +#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 diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 46c6a327..2606ceb5 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -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() diff --git a/src/gui/MainWindow.ui b/src/gui/MainWindow.ui index dd904ee5..b7180058 100644 --- a/src/gui/MainWindow.ui +++ b/src/gui/MainWindow.ui @@ -52,6 +52,8 @@ + + @@ -209,6 +211,11 @@ Database settings + + + Import KeePass 1 database + +