From 95a44ded444de25c4a1f916e8ae781691366b588 Mon Sep 17 00:00:00 2001 From: nowrep Date: Wed, 15 May 2013 21:30:20 +0200 Subject: [PATCH] [PasswordManager] Added KWallet backend plugin. When building with KDE integration (KDE build flag), this plugin will now be built. Closes #592 --- BUILDING | 6 +- CHANGELOG | 1 + src/lib/autofill/passwordmanager.cpp | 4 + .../KWalletPasswords/KWalletPasswords.pro | 17 ++ src/plugins/KWalletPasswords/data/icon.png | Bin 0 -> 1826 bytes .../kwalletpasswordbackend.cpp | 183 ++++++++++++++++++ .../KWalletPasswords/kwalletpasswordbackend.h | 50 +++++ .../KWalletPasswords/kwalletpasswords.qrc | 5 + .../KWalletPasswords/kwalletplugin.cpp | 66 +++++++ src/plugins/KWalletPasswords/kwalletplugin.h | 47 +++++ 10 files changed, 376 insertions(+), 3 deletions(-) create mode 100644 src/plugins/KWalletPasswords/KWalletPasswords.pro create mode 100644 src/plugins/KWalletPasswords/data/icon.png create mode 100644 src/plugins/KWalletPasswords/kwalletpasswordbackend.cpp create mode 100644 src/plugins/KWalletPasswords/kwalletpasswordbackend.h create mode 100644 src/plugins/KWalletPasswords/kwalletpasswords.qrc create mode 100644 src/plugins/KWalletPasswords/kwalletplugin.cpp create mode 100644 src/plugins/KWalletPasswords/kwalletplugin.h diff --git a/BUILDING b/BUILDING index 2bc8f29c9..7c6440285 100644 --- a/BUILDING +++ b/BUILDING @@ -133,9 +133,9 @@ Available Defines example: $ export NO_X11="true" - KDE For now, it just use better oxygen icons in Preferences. - Nepomuk integration is planned, and it will be enabled with - this define also. + KDE Enable KDE integration. + Currently it enables building of KWallet Password plugin, + which provides support for storing passwords in KWallet. example: $ export KDE="true" diff --git a/CHANGELOG b/CHANGELOG index 4f3a5567d..d6e0e74ef 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -6,6 +6,7 @@ Version 1.5.0 * added delete action in edit context menu on page * added possibility to remove EasyList from AdBlock * added inline domain completion to urlbar + * added KWallet password backend plugin * proxy exceptions now supports wildcards (*, ?) * cancel upload when trying to upload non-readable files * GreaseMonkey: added support for GM_Settings diff --git a/src/lib/autofill/passwordmanager.cpp b/src/lib/autofill/passwordmanager.cpp index b3a320a4d..3f8d6fbd1 100644 --- a/src/lib/autofill/passwordmanager.cpp +++ b/src/lib/autofill/passwordmanager.cpp @@ -130,6 +130,10 @@ void PasswordManager::unregisterBackend(PasswordBackend* backend) { const QString &key = m_backends.key(backend); m_backends.remove(key); + + if (m_backend == backend) { + m_backend = m_databaseBackend; + } } QString PasswordManager::createHost(const QUrl &url) diff --git a/src/plugins/KWalletPasswords/KWalletPasswords.pro b/src/plugins/KWalletPasswords/KWalletPasswords.pro new file mode 100644 index 000000000..ecbb75ac9 --- /dev/null +++ b/src/plugins/KWalletPasswords/KWalletPasswords.pro @@ -0,0 +1,17 @@ +include(../../defines.pri) + +contains(DEFINES, "KDE") { + TARGET = $$qtLibraryTarget(KWalletPasswords) + + SOURCES += kwalletplugin.cpp \ + kwalletpasswordbackend.cpp + + HEADERS += kwalletplugin.h \ + kwalletpasswordbackend.h + + RESOURCES += kwalletpasswords.qrc + + LIBS += -lkdeui +} + +include(../../plugins.pri) diff --git a/src/plugins/KWalletPasswords/data/icon.png b/src/plugins/KWalletPasswords/data/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..4029d6df61964ebfd6a5c3586cdc64357972e3d7 GIT binary patch literal 1826 zcmV+-2i^FIP)BE+S;XGQci4PU1X>6Fi>SKlj^)=z$xXftlS{W|eeXm$v|tfd$?reR<;ED;?^ zF)$7brjK^%P+}DFwBqLCElzA~aQ1UgBVo>13R~-e5T4-b`|oo7?RQzb?c_}H z`-@}d6mueu;}}9E&;>=zDQb+x*_h8cb?#Aq{^vJ&?edk|Ha-8q7GFAP3_BlE6-K=~ zfN%&v930f0C`Q0gbH=Q6!ALhKm8dS8=HLV+uvd5 z+O<|vhf`6g|z=*Rb`cxSroc1K!}(mW~n2k#GiiuJ65ZKo4dO_ z_~4nt{f6ZN+%Y>&32@vDH>aqRP{a*)qgbF%kOHW}RACmtJX34M%vtX3@ta@$5gFXp@qYBwz7MfN#!gBEu&pu}Cob1SI z9E_ESQOtPwk;l1w>0)c|djT+qfOF7_tEm#KAR%A|tD?+;lY*csj9KZE^Wav;$@Rcu zIWY@L3X$tWWoIdEQxS|~=s*BM5JaF3SDg3aA3^VbYVZBTwxYzidhOnR{H3K&_t_P zBng-s=Ixoa?igJFVAksAS}CP4PRcw9od~rmQ*pXr!~h{gMir79eTo=?RdxCZB2Ba& zzwjg>1h6|te;WWb2XEuo-O1HSqTq$<%2<>rb)N&ya;nV42;!)Nk?12JO|FYMDJFuz zFy(s#2m(Q*9bSx1oGyT>GmMo!K^KHtjY(mx3v{7nZIzv91yGR0sBN~q^UhnS8ZNVY z0}#O6Db-1#sbW=Q%*H%H5^1_%&<6vns0rBvvuMNw?tx`C0&S+O#u0~>$$J4XcTAlq zO#!PJt4W!q=7tWybSOa_Cs172meRE4W zD`RzL$uU!k2}5o&J50);!cd@EGppBkJ|qRF3wP(htyv(=52WBsrno`zz&y2^Qx&p0 z^FDw%t70ssimNhaW5~v;K(W@AIGniuex~IvDI7zJ%^?b?2+$&nmdPem6IPRvk}yop zl#OL>_WZ!2jdh>M%~%mcIDh^;moMF<+A&BNLtwVHi8)M`ICcI48)q(XeJ^uuQ8;n- zQMS%L!6HYtKg!JSJHuxleTv!UX@=}bHLw$SK-L3nTP~n9XJJS06owhS?S1%`{g@W2l)1( z(?q4M3>07lZUQ^N4R^1%0r*&rj@Iep6uvv9KY4weeGa%EIrI|%{@2%k0Pz7=+j& +* +* 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 3 of the License, or +* (at your option) any later version. +* +* 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 "kwalletpasswordbackend.h" + +#include +#include + +static PasswordEntry decodeEntry(const QByteArray &data) +{ + QDataStream stream(data); + PasswordEntry entry; + stream >> entry; + return entry; +} + +static QByteArray encodeEntry(const PasswordEntry &entry) +{ + QByteArray data; + QDataStream stream(&data, QIODevice::WriteOnly); + stream << entry; + return data; +} + +KWalletPasswordBackend::KWalletPasswordBackend() + : PasswordBackend() + , m_wallet(0) +{ +} + +static bool compareEntries(const PasswordEntry &e1, const PasswordEntry &e2) +{ + return e1.id.toString() > e2.id.toString(); +} + +QVector KWalletPasswordBackend::getEntries(const QUrl &url) +{ + initialize(); + + const QString &host = PasswordManager::createHost(url); + + QVector list; + + foreach (const PasswordEntry &entry, m_allEntries) { + if (entry.host == host) { + list.append(entry); + } + } + + // Sort to prefer last updated entries + qSort(list.begin(), list.end(), compareEntries); + + return list; +} + +QVector KWalletPasswordBackend::getAllEntries() +{ + initialize(); + + return m_allEntries; +} + +void KWalletPasswordBackend::addEntry(const PasswordEntry &entry) +{ + initialize(); + + QString id = QString("%1/%2").arg(entry.host, QString::number(QDateTime::currentDateTime().toTime_t())); + + PasswordEntry stored = entry; + stored.id = id; + + m_wallet->writeEntry(stored.id.toString(), encodeEntry(stored)); + m_allEntries.append(stored); +} + +void KWalletPasswordBackend::updateEntry(const PasswordEntry &entry) +{ + initialize(); + + m_wallet->removeEntry(entry.id.toString()); + m_wallet->writeEntry(entry.id.toString(), encodeEntry(entry)); + + int index = m_allEntries.indexOf(entry); + + if (index > -1) { + m_allEntries[index] = entry; + } +} + +void KWalletPasswordBackend::updateLastUsed(const PasswordEntry &entry) +{ + initialize(); + + QString id = QString("%1/%2").arg(entry.host, QString::number(QDateTime::currentDateTime().toTime_t())); + + PasswordEntry stored = entry; + stored.id = id; + + m_wallet->removeEntry(entry.id.toString()); + m_wallet->writeEntry(stored.id.toString(), encodeEntry(stored)); + + int index = m_allEntries.indexOf(entry); + + if (index > -1) { + m_allEntries[index] = stored; + } +} + +void KWalletPasswordBackend::removeEntry(const PasswordEntry &entry) +{ + initialize(); + + m_wallet->removeEntry(entry.id.toString()); + + int index = m_allEntries.indexOf(entry); + + if (index > -1) { + m_allEntries.remove(index); + } +} + +void KWalletPasswordBackend::removeAll() +{ + initialize(); + + m_wallet->removeFolder("QupZilla"); + m_wallet->createFolder("QupZilla"); +} + +void KWalletPasswordBackend::initialize() +{ + if (m_wallet) { + return; + } + + m_wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), 0); + + if (!m_wallet) { + qWarning() << "KWalletPasswordBackend::initialize Cannot open wallet!"; + return; + } + + if (!m_wallet->hasFolder("QupZilla") && !m_wallet->createFolder("QupZilla")) { + qWarning() << "KWalletPasswordBackend::initialize Cannot create folder \"QupZilla\"!"; + return; + } + + if (!m_wallet->setFolder("QupZilla")) { + qWarning() << "KWalletPasswordBackend::initialize Cannot set folder \"QupZilla\"!"; + return; + } + + QMap entries; + if (m_wallet->readEntryList("*", entries) != 0) { + qWarning() << "KWalletPasswordBackend::initialize Cannot read entries!"; + return; + } + + QMap::const_iterator i = entries.constBegin(); + while (i != entries.constEnd()) { + m_allEntries.append(decodeEntry(i.value())); + ++i; + } +} + +KWalletPasswordBackend::~KWalletPasswordBackend() +{ + delete m_wallet; +} diff --git a/src/plugins/KWalletPasswords/kwalletpasswordbackend.h b/src/plugins/KWalletPasswords/kwalletpasswordbackend.h new file mode 100644 index 000000000..e6290664d --- /dev/null +++ b/src/plugins/KWalletPasswords/kwalletpasswordbackend.h @@ -0,0 +1,50 @@ +/* ============================================================ +* KWalletPasswords - KWallet support plugin for QupZilla +* Copyright (C) 2013 David Rosca +* +* 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 3 of the License, or +* (at your option) any later version. +* +* 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 KWALLETPASSWORDBACKEND_H +#define KWALLETPASSWORDBACKEND_H + +#include +#include + +#include "passwordbackends/passwordbackend.h" +#include "passwordmanager.h" + +class KWalletPasswordBackend : public PasswordBackend +{ +public: + explicit KWalletPasswordBackend(); + ~KWalletPasswordBackend(); + + QVector getEntries(const QUrl &url); + QVector getAllEntries(); + + void addEntry(const PasswordEntry &entry); + void updateEntry(const PasswordEntry &entry); + void updateLastUsed(const PasswordEntry &entry); + + void removeEntry(const PasswordEntry &entry); + void removeAll(); + +private: + void initialize(); + + KWallet::Wallet* m_wallet; + QVector m_allEntries; +}; + +#endif // KWALLETPASSWORDBACKEND_H diff --git a/src/plugins/KWalletPasswords/kwalletpasswords.qrc b/src/plugins/KWalletPasswords/kwalletpasswords.qrc new file mode 100644 index 000000000..d67aca1ff --- /dev/null +++ b/src/plugins/KWalletPasswords/kwalletpasswords.qrc @@ -0,0 +1,5 @@ + + + data/icon.png + + diff --git a/src/plugins/KWalletPasswords/kwalletplugin.cpp b/src/plugins/KWalletPasswords/kwalletplugin.cpp new file mode 100644 index 000000000..87d7fecff --- /dev/null +++ b/src/plugins/KWalletPasswords/kwalletplugin.cpp @@ -0,0 +1,66 @@ +/* ============================================================ +* KWalletPasswords - KWallet support plugin for QupZilla +* Copyright (C) 2013 David Rosca +* +* 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 3 of the License, or +* (at your option) any later version. +* +* 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 "kwalletplugin.h" +#include "kwalletpasswordbackend.h" +#include "pluginproxy.h" +#include "qupzilla.h" + +#include + +KWalletPlugin::KWalletPlugin() + : QObject() + , m_backend(0) +{ +} + +PluginSpec KWalletPlugin::pluginSpec() +{ + PluginSpec spec; + spec.name = "KWallet Passwords"; + spec.info = "KWallet password backend"; + spec.description = "Provides support for storing passwords in KWallet"; + spec.version = "0.1.0"; + spec.author = "David Rosca "; + spec.icon = QPixmap(":kwp/data/icon.png"); + spec.hasSettings = false; + + return spec; +} + +void KWalletPlugin::init(const QString &sPath) +{ + Q_UNUSED(sPath); + + m_backend = new KWalletPasswordBackend; + QZ_REGISTER_PASSWORD_BACKEND("KWallet", m_backend); +} + +void KWalletPlugin::unload() +{ + QZ_UNREGISTER_PASSWORD_BACKEND(m_backend); + delete m_backend; +} + +bool KWalletPlugin::testPlugin() +{ + return (QupZilla::VERSION == QLatin1String("1.5.0")); +} + +#if QT_VERSION < 0x050000 +Q_EXPORT_PLUGIN2(KWalletPasswords, KWalletPlugin) +#endif diff --git a/src/plugins/KWalletPasswords/kwalletplugin.h b/src/plugins/KWalletPasswords/kwalletplugin.h new file mode 100644 index 000000000..7aa88fdb8 --- /dev/null +++ b/src/plugins/KWalletPasswords/kwalletplugin.h @@ -0,0 +1,47 @@ +/* ============================================================ +* KWalletPasswords - KWallet support plugin for QupZilla +* Copyright (C) 2013 David Rosca +* +* 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 3 of the License, or +* (at your option) any later version. +* +* 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 KWALLETPLUGIN_H +#define KWALLETPLUGIN_H + +#include "plugininterface.h" + +class KWalletPasswordBackend; + +class KWalletPlugin : public QObject, public PluginInterface +{ + Q_OBJECT + Q_INTERFACES(PluginInterface) + +#if QT_VERSION >= 0x050000 + Q_PLUGIN_METADATA(IID "QupZilla.Browser.plugin.KWalletPasswords") +#endif + +public: + explicit KWalletPlugin(); + PluginSpec pluginSpec(); + + void init(const QString &sPath); + void unload(); + bool testPlugin(); + +private: + KWalletPasswordBackend* m_backend; + +}; + +#endif // KWALLETPLUGIN_H