From 34f8162615b7cbbce17a4e12a8c4c67db6a0d1fc Mon Sep 17 00:00:00 2001 From: nowrep Date: Fri, 17 May 2013 15:42:06 +0200 Subject: [PATCH] [PasswordBackends] Added possibility to change backends in preferences. --- .../databasepasswordbackend.cpp | 6 +++ .../databasepasswordbackend.h | 2 + .../passwordbackends/passwordbackend.h | 2 + src/lib/autofill/passwordmanager.cpp | 10 ++++ src/lib/autofill/passwordmanager.h | 3 ++ src/lib/preferences/autofillmanager.cpp | 46 ++++++++++++++++++- src/lib/preferences/autofillmanager.h | 1 + src/lib/preferences/autofillmanager.ui | 42 ++++++++++++++++- .../kwalletpasswordbackend.cpp | 6 +++ .../KWalletPasswords/kwalletpasswordbackend.h | 2 + 10 files changed, 117 insertions(+), 3 deletions(-) diff --git a/src/lib/autofill/passwordbackends/databasepasswordbackend.cpp b/src/lib/autofill/passwordbackends/databasepasswordbackend.cpp index 94eb0706c..758917712 100644 --- a/src/lib/autofill/passwordbackends/databasepasswordbackend.cpp +++ b/src/lib/autofill/passwordbackends/databasepasswordbackend.cpp @@ -18,6 +18,7 @@ #include "databasepasswordbackend.h" #include "mainapplication.h" #include "databasewriter.h" +#include "autofill.h" #include #include @@ -27,6 +28,11 @@ DatabasePasswordBackend::DatabasePasswordBackend() { } +QString DatabasePasswordBackend::name() const +{ + return AutoFill::tr("Database (plaintext)"); +} + QVector DatabasePasswordBackend::getEntries(const QUrl &url) { const QString &host = PasswordManager::createHost(url); diff --git a/src/lib/autofill/passwordbackends/databasepasswordbackend.h b/src/lib/autofill/passwordbackends/databasepasswordbackend.h index a6994cd7c..e44a85a37 100644 --- a/src/lib/autofill/passwordbackends/databasepasswordbackend.h +++ b/src/lib/autofill/passwordbackends/databasepasswordbackend.h @@ -25,6 +25,8 @@ class DatabasePasswordBackend : public PasswordBackend public: explicit DatabasePasswordBackend(); + QString name() const; + QVector getEntries(const QUrl &url); QVector getAllEntries(); diff --git a/src/lib/autofill/passwordbackends/passwordbackend.h b/src/lib/autofill/passwordbackends/passwordbackend.h index 1f1de0d84..fb8f8523a 100644 --- a/src/lib/autofill/passwordbackends/passwordbackend.h +++ b/src/lib/autofill/passwordbackends/passwordbackend.h @@ -26,6 +26,8 @@ public: explicit PasswordBackend(); virtual ~PasswordBackend() { } + virtual QString name() const = 0; + virtual QVector getEntries(const QUrl &url) = 0; virtual QVector getAllEntries() = 0; diff --git a/src/lib/autofill/passwordmanager.cpp b/src/lib/autofill/passwordmanager.cpp index 3f8d6fbd1..be548ecee 100644 --- a/src/lib/autofill/passwordmanager.cpp +++ b/src/lib/autofill/passwordmanager.cpp @@ -116,6 +116,16 @@ void PasswordManager::removeAllEntries() m_backend->removeAll(); } +QHash PasswordManager::availableBackends() const +{ + return m_backends; +} + +PasswordBackend* PasswordManager::activeBackend() const +{ + return m_backend; +} + bool PasswordManager::registerBackend(const QString &id, PasswordBackend* backend) { if (m_backends.contains(id)) { diff --git a/src/lib/autofill/passwordmanager.h b/src/lib/autofill/passwordmanager.h index cdb8e265d..e8deab72d 100644 --- a/src/lib/autofill/passwordmanager.h +++ b/src/lib/autofill/passwordmanager.h @@ -65,6 +65,9 @@ public: void removeEntry(const PasswordEntry &entry); void removeAllEntries(); + QHash availableBackends() const; + PasswordBackend* activeBackend() const; + bool registerBackend(const QString &id, PasswordBackend* backend); void unregisterBackend(PasswordBackend* backend); diff --git a/src/lib/preferences/autofillmanager.cpp b/src/lib/preferences/autofillmanager.cpp index f40d37789..ef285b213 100644 --- a/src/lib/preferences/autofillmanager.cpp +++ b/src/lib/preferences/autofillmanager.cpp @@ -16,17 +16,19 @@ * along with this program. If not, see . * ============================================================ */ #include "autofillmanager.h" +#include "ui_autofillmanager.h" #include "autofill.h" #include "passwordmanager.h" +#include "passwordbackends/passwordbackend.h" #include "mainapplication.h" -#include "ui_autofillmanager.h" +#include "settings.h" +#include #include #include #include #include #include -#include #include AutoFillManager::AutoFillManager(QWidget* parent) @@ -41,6 +43,7 @@ AutoFillManager::AutoFillManager(QWidget* parent) connect(ui->editPass, SIGNAL(clicked()), this, SLOT(editPass())); connect(ui->showPasswords, SIGNAL(clicked()), this, SLOT(showPasswords())); connect(ui->search, SIGNAL(textChanged(QString)), ui->treePass, SLOT(filterString(QString))); + connect(ui->changeBackend, SIGNAL(clicked()), this, SLOT(changePasswordBackend())); connect(ui->removeExcept, SIGNAL(clicked()), this, SLOT(removeExcept())); connect(ui->removeAllExcept, SIGNAL(clicked()), this, SLOT(removeAllExcept())); @@ -52,6 +55,10 @@ AutoFillManager::AutoFillManager(QWidget* parent) ui->importExport->setPopupMode(QToolButton::InstantPopup); ui->search->setPlaceholderText(tr("Search")); + // Password backends + ui->currentBackend->setText(QString("%1").arg(mApp->autoFill()->passwordManager()->activeBackend()->name())); + + // Load passwords QTimer::singleShot(0, this, SLOT(loadPasswords())); } @@ -89,6 +96,41 @@ void AutoFillManager::loadPasswords() ui->treeExcept->sortByColumn(-1); } +void AutoFillManager::changePasswordBackend() +{ + QHash backends = mApp->autoFill()->passwordManager()->availableBackends(); + QStringList items; + + int current = 0; + + foreach (const QString &key, backends.keys()) { + if (backends[key] == mApp->autoFill()->passwordManager()->activeBackend()) { + current = items.size(); + } + + items << backends[key]->name(); + } + + QString item = QInputDialog::getItem(this, tr("Change backend..."), tr("Change backend:"), items, current, false); + + if (!item.isEmpty()) { + Settings settings; + settings.beginGroup("PasswordManager"); + + foreach (const QString &key, backends.keys()) { + if (backends[key]->name() == item) { + settings.setValue("Backend", key); + break; + } + } + + settings.endGroup(); + } + + mApp->autoFill()->passwordManager()->loadSettings(); + QTimer::singleShot(0, this, SLOT(loadPasswords())); +} + void AutoFillManager::showPasswords() { if (m_passwordsShown) { diff --git a/src/lib/preferences/autofillmanager.h b/src/lib/preferences/autofillmanager.h index ee2842af9..b626a6af9 100644 --- a/src/lib/preferences/autofillmanager.h +++ b/src/lib/preferences/autofillmanager.h @@ -39,6 +39,7 @@ public: private slots: void loadPasswords(); + void changePasswordBackend(); void removePass(); void removeAllPass(); diff --git a/src/lib/preferences/autofillmanager.ui b/src/lib/preferences/autofillmanager.ui index 9c0bf88b3..46471fd78 100644 --- a/src/lib/preferences/autofillmanager.ui +++ b/src/lib/preferences/autofillmanager.ui @@ -7,10 +7,50 @@ 0 0 537 - 348 + 372 + + + + + + + 0 + 0 + + + + Passwords are stored in: + + + + + + + + + + Change backend + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + diff --git a/src/plugins/KWalletPasswords/kwalletpasswordbackend.cpp b/src/plugins/KWalletPasswords/kwalletpasswordbackend.cpp index 2d1d55893..b3386e6b7 100644 --- a/src/plugins/KWalletPasswords/kwalletpasswordbackend.cpp +++ b/src/plugins/KWalletPasswords/kwalletpasswordbackend.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . * ============================================================ */ #include "kwalletpasswordbackend.h" +#include "kwalletplugin.h" #include #include @@ -42,6 +43,11 @@ KWalletPasswordBackend::KWalletPasswordBackend() { } +QString KWalletPasswordBackend::name() const +{ + return KWalletPlugin::tr("KWallet"); +} + static bool compareEntries(const PasswordEntry &e1, const PasswordEntry &e2) { return e1.id.toString() > e2.id.toString(); diff --git a/src/plugins/KWalletPasswords/kwalletpasswordbackend.h b/src/plugins/KWalletPasswords/kwalletpasswordbackend.h index e6290664d..096e3e725 100644 --- a/src/plugins/KWalletPasswords/kwalletpasswordbackend.h +++ b/src/plugins/KWalletPasswords/kwalletpasswordbackend.h @@ -30,6 +30,8 @@ public: explicit KWalletPasswordBackend(); ~KWalletPasswordBackend(); + QString name() const; + QVector getEntries(const QUrl &url); QVector getAllEntries();