Skip to content
This repository has been archived by the owner on May 10, 2018. It is now read-only.

Commit

Permalink
[PasswordManager] Added KWallet backend plugin.
Browse files Browse the repository at this point in the history
When building with KDE integration (KDE build flag), this plugin
will now be built.

Closes #592
  • Loading branch information
nowrep committed May 15, 2013
1 parent f8ee9a4 commit 95a44de
Show file tree
Hide file tree
Showing 10 changed files with 376 additions and 3 deletions.
6 changes: 3 additions & 3 deletions BUILDING
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/lib/autofill/passwordmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions src/plugins/KWalletPasswords/KWalletPasswords.pro
Original file line number Diff line number Diff line change
@@ -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)
Binary file added src/plugins/KWalletPasswords/data/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
183 changes: 183 additions & 0 deletions src/plugins/KWalletPasswords/kwalletpasswordbackend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/* ============================================================
* KWalletPasswords - KWallet support plugin for QupZilla
* Copyright (C) 2013 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "kwalletpasswordbackend.h"

#include <QDateTime>
#include <QDebug>

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<PasswordEntry> KWalletPasswordBackend::getEntries(const QUrl &url)
{
initialize();

const QString &host = PasswordManager::createHost(url);

QVector<PasswordEntry> 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<PasswordEntry> 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<QString, QByteArray> entries;
if (m_wallet->readEntryList("*", entries) != 0) {
qWarning() << "KWalletPasswordBackend::initialize Cannot read entries!";
return;
}

QMap<QString, QByteArray>::const_iterator i = entries.constBegin();
while (i != entries.constEnd()) {
m_allEntries.append(decodeEntry(i.value()));
++i;
}
}

KWalletPasswordBackend::~KWalletPasswordBackend()
{
delete m_wallet;
}
50 changes: 50 additions & 0 deletions src/plugins/KWalletPasswords/kwalletpasswordbackend.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* ============================================================
* KWalletPasswords - KWallet support plugin for QupZilla
* Copyright (C) 2013 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#ifndef KWALLETPASSWORDBACKEND_H
#define KWALLETPASSWORDBACKEND_H

#include <QVector>
#include <KDE/KWallet/Wallet>

#include "passwordbackends/passwordbackend.h"
#include "passwordmanager.h"

class KWalletPasswordBackend : public PasswordBackend
{
public:
explicit KWalletPasswordBackend();
~KWalletPasswordBackend();

QVector<PasswordEntry> getEntries(const QUrl &url);
QVector<PasswordEntry> 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<PasswordEntry> m_allEntries;
};

#endif // KWALLETPASSWORDBACKEND_H
5 changes: 5 additions & 0 deletions src/plugins/KWalletPasswords/kwalletpasswords.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/kwp">
<file>data/icon.png</file>
</qresource>
</RCC>
66 changes: 66 additions & 0 deletions src/plugins/KWalletPasswords/kwalletplugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* ============================================================
* KWalletPasswords - KWallet support plugin for QupZilla
* Copyright (C) 2013 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "kwalletplugin.h"
#include "kwalletpasswordbackend.h"
#include "pluginproxy.h"
#include "qupzilla.h"

#include <QTranslator>

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 <nowrep@gmail.com>";
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
Loading

0 comments on commit 95a44de

Please sign in to comment.