2013-05-15 21:30:20 +02:00
|
|
|
/* ============================================================
|
|
|
|
* 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"
|
2013-05-17 15:42:06 +02:00
|
|
|
#include "kwalletplugin.h"
|
2013-05-15 21:30:20 +02:00
|
|
|
|
|
|
|
#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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:42:06 +02:00
|
|
|
QString KWalletPasswordBackend::name() const
|
|
|
|
{
|
|
|
|
return KWalletPlugin::tr("KWallet");
|
|
|
|
}
|
|
|
|
|
2013-05-15 21:30:20 +02:00
|
|
|
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
|
2013-05-21 15:35:11 +02:00
|
|
|
qSort(list.begin(), list.end());
|
2013-05-15 21:30:20 +02:00
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector<PasswordEntry> KWalletPasswordBackend::getAllEntries()
|
|
|
|
{
|
|
|
|
initialize();
|
|
|
|
|
|
|
|
return m_allEntries;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWalletPasswordBackend::addEntry(const PasswordEntry &entry)
|
|
|
|
{
|
|
|
|
initialize();
|
|
|
|
|
|
|
|
PasswordEntry stored = entry;
|
2013-05-21 15:35:11 +02:00
|
|
|
stored.id = QString("%1/%2").arg(entry.host, entry.username);
|
|
|
|
stored.updated = QDateTime::currentDateTime().toTime_t();
|
2013-05-15 21:30:20 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-20 11:05:54 +02:00
|
|
|
void KWalletPasswordBackend::updateLastUsed(PasswordEntry &entry)
|
2013-05-15 21:30:20 +02:00
|
|
|
{
|
|
|
|
initialize();
|
|
|
|
|
|
|
|
m_wallet->removeEntry(entry.id.toString());
|
|
|
|
|
2013-05-21 15:35:11 +02:00
|
|
|
entry.updated = QDateTime::currentDateTime().toTime_t();
|
2013-05-20 11:05:54 +02:00
|
|
|
|
|
|
|
m_wallet->writeEntry(entry.id.toString(), encodeEntry(entry));
|
|
|
|
|
2013-05-21 15:35:11 +02:00
|
|
|
int index = m_allEntries.indexOf(entry);
|
2013-05-15 21:30:20 +02:00
|
|
|
|
|
|
|
if (index > -1) {
|
2013-05-20 11:05:54 +02:00
|
|
|
m_allEntries[index] = entry;
|
2013-05-15 21:30:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2013-05-21 15:35:11 +02:00
|
|
|
m_allEntries.clear();
|
|
|
|
|
2013-05-15 21:30:20 +02:00
|
|
|
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()) {
|
2013-05-20 11:14:00 +02:00
|
|
|
PasswordEntry entry = decodeEntry(i.value());
|
|
|
|
if (entry.isValid()) {
|
|
|
|
m_allEntries.append(entry);
|
|
|
|
}
|
2013-05-15 21:30:20 +02:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KWalletPasswordBackend::~KWalletPasswordBackend()
|
|
|
|
{
|
|
|
|
delete m_wallet;
|
|
|
|
}
|