From 2612fc8e44006eaf8d52ffd4716a466adff8d6c8 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Fri, 13 Jan 2012 17:52:37 +0100 Subject: [PATCH] Expand GUI to allow changing the master key. --- src/CMakeLists.txt | 3 + src/gui/ChangeMasterKeyWidget.cpp | 82 ++++++++++++++++ src/gui/ChangeMasterKeyWidget.h | 56 +++++++++++ src/gui/ChangeMasterKeyWidget.ui | 150 ++++++++++++++++++++++++++++++ src/gui/DatabaseTabWidget.cpp | 5 + src/gui/DatabaseTabWidget.h | 7 +- src/gui/DatabaseWidget.cpp | 27 ++++++ src/gui/DatabaseWidget.h | 5 + src/gui/MainWindow.cpp | 2 + src/gui/MainWindow.ui | 12 ++- 10 files changed, 345 insertions(+), 4 deletions(-) create mode 100644 src/gui/ChangeMasterKeyWidget.cpp create mode 100644 src/gui/ChangeMasterKeyWidget.h create mode 100644 src/gui/ChangeMasterKeyWidget.ui diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 79fadb40f..dc0a5c98d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -46,6 +46,7 @@ set(keepassx_SOURCES format/KeePass2Writer.cpp format/KeePass2XmlReader.cpp format/KeePass2XmlWriter.cpp + gui/ChangeMasterKeyWidget.cpp gui/DatabaseTabWidget.cpp gui/DatabaseWidget.cpp gui/EditEntryWidget.cpp @@ -72,6 +73,7 @@ set(keepassx_MOC core/Entry.h core/Group.h core/Metadata.h + gui/ChangeMasterKeyWidget.h gui/DatabaseTabWidget.h gui/DatabaseWidget.h gui/EditEntryWidget.h @@ -89,6 +91,7 @@ set(keepassx_MOC ) set(keepassx_FORMS + gui/ChangeMasterKeyWidget.ui gui/EditEntryWidget.ui gui/EditEntryWidgetMain.ui gui/EditEntryWidgetNotes.ui diff --git a/src/gui/ChangeMasterKeyWidget.cpp b/src/gui/ChangeMasterKeyWidget.cpp new file mode 100644 index 000000000..6172b31dc --- /dev/null +++ b/src/gui/ChangeMasterKeyWidget.cpp @@ -0,0 +1,82 @@ +/* + * 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 "ChangeMasterKeyWidget.h" +#include "ui_ChangeMasterKeyWidget.h" + +#include "keys/FileKey.h" +#include "keys/PasswordKey.h" + +ChangeMasterKeyWidget::ChangeMasterKeyWidget(QWidget* parent) + : QWidget(parent) + , m_ui(new Ui::ChangeMasterKeyWidget()) +{ + m_ui->setupUi(this); + + connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(generateKey())); + connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(reject())); +} + +ChangeMasterKeyWidget::~ChangeMasterKeyWidget() +{ +} + +void ChangeMasterKeyWidget::clearForms() +{ + m_key.clear(); + + m_ui->passwordGroup->setChecked(true); + m_ui->enterPasswordEdit->setText(""); + m_ui->repeatPasswordEdit->setText(""); + m_ui->keyFileGroup->setChecked(false); + // TODO clear m_ui->keyFileCombo +} + +CompositeKey ChangeMasterKeyWidget::newMasterKey() +{ + return m_key; +} + +QLabel* ChangeMasterKeyWidget::headlineLabel() +{ + return m_ui->headlineLabel; +} + +void ChangeMasterKeyWidget::generateKey() +{ + m_key.clear(); + + if (m_ui->passwordGroup->isChecked()) { + m_key.addKey(PasswordKey(m_ui->enterPasswordEdit->text())); + } + if (m_ui->keyFileGroup->isChecked()) { + FileKey fileKey; + QString errorMsg; + if (!fileKey.load(m_ui->keyFileCombo->currentText()), &errorMsg) { + // TODO error handling + } + m_key.addKey(fileKey); + } + + Q_EMIT editFinished(true); +} + + +void ChangeMasterKeyWidget::reject() +{ + Q_EMIT editFinished(false); +} diff --git a/src/gui/ChangeMasterKeyWidget.h b/src/gui/ChangeMasterKeyWidget.h new file mode 100644 index 000000000..80c3ea9a0 --- /dev/null +++ b/src/gui/ChangeMasterKeyWidget.h @@ -0,0 +1,56 @@ +/* + * 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_CHANGEMASTERKEYWIDGET_H +#define KEEPASSX_CHANGEMASTERKEYWIDGET_H + +#include +#include + +#include "keys/CompositeKey.h" + +class QLabel; +namespace Ui { + class ChangeMasterKeyWidget; +} + +class ChangeMasterKeyWidget : public QWidget +{ + Q_OBJECT + +public: + explicit ChangeMasterKeyWidget(QWidget* parent = 0); + ~ChangeMasterKeyWidget(); + void clearForms(); + CompositeKey newMasterKey(); + QLabel* headlineLabel(); + +Q_SIGNALS: + void editFinished(bool accepted); + +private Q_SLOTS: + void generateKey(); + void reject(); + +private: + QScopedPointer m_ui; + CompositeKey m_key; + + Q_DISABLE_COPY(ChangeMasterKeyWidget) +}; + +#endif // KEEPASSX_CHANGEMASTERKEYWIDGET_H diff --git a/src/gui/ChangeMasterKeyWidget.ui b/src/gui/ChangeMasterKeyWidget.ui new file mode 100644 index 000000000..6c842110b --- /dev/null +++ b/src/gui/ChangeMasterKeyWidget.ui @@ -0,0 +1,150 @@ + + + ChangeMasterKeyWidget + + + + 0 + 0 + 438 + 237 + + + + + + + + + + Qt::Vertical + + + QSizePolicy::Fixed + + + + 1 + 3 + + + + + + + + Password + + + true + + + true + + + + + + Enter password: + + + + + + + + + QLineEdit::Password + + + + + + + ... + + + + + + + + + Repeat password: + + + + + + + QLineEdit::Password + + + + + + + + + + Key file + + + true + + + + + + Browse + + + + + + + Create + + + + + + + + 0 + 0 + + + + true + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index 847bd7e86..eb41ad058 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -223,6 +223,11 @@ void DatabaseTabWidget::saveDatabaseAs(int index) saveDatabaseAs(indexDatabase(index)); } +void DatabaseTabWidget::changeMasterKey() +{ + currentDatabaseWidget()->switchToMasterKeyChange(); +} + void DatabaseTabWidget::createEntry() { currentDatabaseWidget()->createEntry(); diff --git a/src/gui/DatabaseTabWidget.h b/src/gui/DatabaseTabWidget.h index faa650b58..e1ae05b90 100644 --- a/src/gui/DatabaseTabWidget.h +++ b/src/gui/DatabaseTabWidget.h @@ -56,11 +56,15 @@ public Q_SLOTS: void saveDatabase(int index = -1); void saveDatabaseAs(int index = -1); void closeDatabase(int index = -1); + void changeMasterKey(); void createEntry(); void editEntry(); void createGroup(); void editGroup(); +Q_SIGNALS: + void entrySelectionChanged(bool singleEntrySelected); + private Q_SLOTS: void updateTabName(Database* db); void openDatabaseDialog(); @@ -68,9 +72,6 @@ private Q_SLOTS: void openDatabaseCleanup(); void emitEntrySelectionChanged(); -Q_SIGNALS: - void entrySelectionChanged(bool singleEntrySelected); - private: void saveDatabase(Database* db); void saveDatabaseAs(Database* db); diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index e81508596..19f6584fc 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -18,8 +18,10 @@ #include "DatabaseWidget.h" #include +#include #include +#include "gui/ChangeMasterKeyWidget.h" #include "gui/EditEntryWidget.h" #include "gui/EditGroupWidget.h" #include "gui/EntryView.h" @@ -27,6 +29,7 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent) : QStackedWidget(parent) + , m_db(db) , m_newGroup(0) , m_newEntry(0) , m_newParent(0) @@ -56,15 +59,23 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent) m_editEntryWidget = new EditEntryWidget(); m_editGroupWidget = new EditGroupWidget(); + m_changeMasterKeyWidget = new ChangeMasterKeyWidget(); + m_changeMasterKeyWidget->headlineLabel()->setText(tr("Change master key")); + QFont headlineLabelFont = m_changeMasterKeyWidget->headlineLabel()->font(); + headlineLabelFont.setBold(true); + headlineLabelFont.setPointSize(headlineLabelFont.pointSize() + 2); + m_changeMasterKeyWidget->headlineLabel()->setFont(headlineLabelFont); addWidget(m_mainWidget); addWidget(m_editEntryWidget); addWidget(m_editGroupWidget); + addWidget(m_changeMasterKeyWidget); connect(m_groupView, SIGNAL(groupChanged(Group*)), m_entryView, SLOT(setGroup(Group*))); connect(m_entryView, SIGNAL(entryActivated(Entry*)), SLOT(switchToEntryEdit(Entry*))); connect(m_editEntryWidget, SIGNAL(editFinished(bool)), SLOT(switchToView(bool))); connect(m_editGroupWidget, SIGNAL(editFinished(bool)), SLOT(switchToView(bool))); + connect(m_changeMasterKeyWidget, SIGNAL(editFinished(bool)), SLOT(updateMasterKey(bool))); setCurrentIndex(0); } @@ -139,6 +150,16 @@ void DatabaseWidget::switchToGroupEdit(Group* group, bool create) m_editGroupWidget->loadGroup(group, create); setCurrentIndex(2); } + +void DatabaseWidget::updateMasterKey(bool accepted) +{ + if (accepted) { + m_db->setKey(m_changeMasterKeyWidget->newMasterKey()); + } + + setCurrentIndex(0); +} + void DatabaseWidget::switchToEntryEdit() { switchToEntryEdit(m_entryView->currentEntry(), false); @@ -148,3 +169,9 @@ void DatabaseWidget::switchToGroupEdit() { switchToGroupEdit(m_groupView->currentGroup(), false); } + +void DatabaseWidget::switchToMasterKeyChange() +{ + m_changeMasterKeyWidget->clearForms(); + setCurrentIndex(3); +} diff --git a/src/gui/DatabaseWidget.h b/src/gui/DatabaseWidget.h index 1f30bf434..d5034b990 100644 --- a/src/gui/DatabaseWidget.h +++ b/src/gui/DatabaseWidget.h @@ -20,6 +20,7 @@ #include +class ChangeMasterKeyWidget; class Database; class EditEntryWidget; class EditGroupWidget; @@ -42,17 +43,21 @@ public Q_SLOTS: void createGroup(); void switchToEntryEdit(); void switchToGroupEdit(); + void switchToMasterKeyChange(); private Q_SLOTS: void switchToView(bool accepted); void switchToEntryEdit(Entry* entry); void switchToEntryEdit(Entry* entry, bool create); void switchToGroupEdit(Group* entry, bool create); + void updateMasterKey(bool accepted); private: + Database* m_db; QWidget* m_mainWidget; EditEntryWidget* m_editEntryWidget; EditGroupWidget* m_editGroupWidget; + ChangeMasterKeyWidget* m_changeMasterKeyWidget; GroupView* m_groupView; EntryView* m_entryView; Group* m_newGroup; diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 90e33c9ab..5a8ca783f 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -39,6 +39,7 @@ MainWindow::MainWindow() connect(m_ui->actionDatabaseSave, SIGNAL(triggered()), m_ui->tabWidget, SLOT(saveDatabase())); connect(m_ui->actionDatabaseSaveAs, SIGNAL(triggered()), m_ui->tabWidget, SLOT(saveDatabaseAs())); connect(m_ui->actionDatabaseClose, SIGNAL(triggered()), m_ui->tabWidget, SLOT(closeDatabase())); + connect(m_ui->actionChangeMasterKey, SIGNAL(triggered()), m_ui->tabWidget, SLOT(changeMasterKey())); 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->actionGroupNew, SIGNAL(triggered()), m_ui->tabWidget, SLOT(createGroup())); @@ -60,4 +61,5 @@ void MainWindow::currentTabChanged(int index) m_ui->actionEntryNew->setEnabled(hasTab); m_ui->actionGroupNew->setEnabled(hasTab); m_ui->actionGroupEdit->setEnabled(hasTab); + m_ui->actionChangeMasterKey->setEnabled(hasTab); } diff --git a/src/gui/MainWindow.ui b/src/gui/MainWindow.ui index d49a7757e..396c13781 100644 --- a/src/gui/MainWindow.ui +++ b/src/gui/MainWindow.ui @@ -36,7 +36,7 @@ 0 0 800 - 20 + 19 @@ -49,6 +49,8 @@ + + @@ -187,6 +189,14 @@ Save database as + + + false + + + Change master key + +