26#include <kwallet_version.h>
31 QDataStream stream(data);
37static QMap<QString, QString> encodeEntry(
const PasswordEntry &entry)
39 QMap<QString, QString> data = {
43 {
QSL(
"updated"), QString::number(entry.
updated)},
44 {
QSL(
"data"), QString::fromUtf8(entry.
data)}
57 return KDEFrameworksIntegrationPlugin::tr(
"KWallet");
66 QVector<PasswordEntry> list;
68 for (
const PasswordEntry &entry : std::as_const(m_allEntries)) {
69 if (entry.host == host) {
75 std::sort(list.begin(), list.end());
88static uint Q_DATETIME_TOTIME_T(
const QDateTime &dateTime)
90 if (!dateTime.isValid())
92 qint64 retval = dateTime.toMSecsSinceEpoch() / 1000;
93 if (quint64(retval) >= Q_UINT64_C(0xFFFFFFFF))
103 showErrorNotification();
109 stored.
updated = Q_DATETIME_TOTIME_T(QDateTime::currentDateTime());
111 m_wallet->writeMap(stored.
id.toString(), encodeEntry(stored));
112 m_allEntries.append(stored);
120 showErrorNotification();
124 m_wallet->removeEntry(entry.
id.toString());
125 m_wallet->writeMap(entry.
id.toString(), encodeEntry(entry));
127 int index = m_allEntries.indexOf(entry);
130 m_allEntries[index] = entry;
141 showErrorNotification();
145 m_wallet->removeEntry(entry.
id.toString());
147 entry.
updated = Q_DATETIME_TOTIME_T(QDateTime::currentDateTime());
149 m_wallet->writeMap(entry.
id.toString(), encodeEntry(entry));
151 int index = m_allEntries.indexOf(entry);
154 m_allEntries[index] = entry;
163 showErrorNotification();
167 m_wallet->removeEntry(entry.
id.toString());
169 int index = m_allEntries.indexOf(entry);
172 m_allEntries.remove(index);
181 showErrorNotification();
185 m_allEntries.clear();
187 m_wallet->removeFolder(
QSL(
"FalkonPasswords"));
188 m_wallet->createFolder(
QSL(
"FalkonPasswords"));
191void KWalletPasswordBackend::showErrorNotification()
193 static bool initialized;
197 mApp->desktopNotifications()->showNotification(KDEFrameworksIntegrationPlugin::tr(
"KWallet disabled"), KDEFrameworksIntegrationPlugin::tr(
"Please enable KWallet to save password."));
201void KWalletPasswordBackend::initialize()
209 if (w && w->window()) {
210 wid = w->window()->winId();
212 m_wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), wid);
215 qWarning() <<
"KWalletPasswordBackend::initialize Cannot open wallet!";
219 bool migrationFalkon = !m_wallet->hasFolder(
QSL(
"FalkonPasswords")) && m_wallet->hasFolder(
QSL(
"Falkon"));
220 bool migrateQupzilla = !m_wallet->hasFolder(
QSL(
"FalkonPasswords")) && !m_wallet->hasFolder(
QSL(
"Falkon")) && m_wallet->hasFolder(
QSL(
"QupZilla"));
221 bool migration =
false;
223 if (!m_wallet->hasFolder(
QSL(
"FalkonPasswords")) && !m_wallet->createFolder(
QSL(
"FalkonPasswords"))) {
224 qWarning() <<
"KWalletPasswordBackend::initialize Cannot create folder \"FalkonPasswords\"!";
228 if (migrationFalkon) {
229 if (!m_wallet->setFolder(
QSL(
"Falkon"))) {
230 qWarning() <<
"KWalletPasswordBackend::initialize Cannot set folder \"Falkon\"!";
235 else if (migrateQupzilla) {
236 if (!m_wallet->setFolder(
QSL(
"QupZilla"))) {
237 qWarning() <<
"KWalletPasswordBackend::initialize Cannot set folder \"QupZilla\"!";
243 if (!m_wallet->setFolder(
QSL(
"FalkonPasswords"))) {
244 qWarning() <<
"KWalletPasswordBackend::initialize Cannot set folder \"FalkonPasswords\"!";
250 QMap<QString, QByteArray> entries;
252 entries = m_wallet->entriesList(&ok);
254 qWarning() <<
"KWalletPasswordBackend::initialize Cannot read entries!";
258 QMap<QString, QByteArray>::const_iterator
i = entries.constBegin();
259 while (
i != entries.constEnd()) {
262 m_allEntries.append(entry);
267 if (!m_wallet->setFolder(
QSL(
"FalkonPasswords"))) {
268 qWarning() <<
"KWalletPasswordBackend::initialize Cannot set folder \"FalkonPasswords\"!";
272 for (
const PasswordEntry &entry : std::as_const(m_allEntries)) {
273 m_wallet->writeMap(entry.
id.toString(), encodeEntry(entry));
277 QMap<QString, QMap<QString, QString>> entriesMap;
279 entriesMap = m_wallet->mapList(&ok);
280 QMap<QString, QMap<QString, QString>>::const_iterator j = entriesMap.constBegin();
281 while (j != entriesMap.constEnd()) {
284 entry.
host = j.value()[
QSL(
"host")];
287 entry.
updated = j.value()[
QSL(
"updated")].toInt();
288 entry.
data = j.value()[
QSL(
"data")].toUtf8();
290 m_allEntries.append(entry);
void removeEntry(const PasswordEntry &entry) override
void updateLastUsed(PasswordEntry &entry) override
void removeAll() override
bool updateEntry(const PasswordEntry &entry) override
~KWalletPasswordBackend() override
void addEntry(const PasswordEntry &entry) override
QVector< PasswordEntry > getEntries(const QUrl &url) override
QString name() const override
QVector< PasswordEntry > getAllEntries() override
static QString createHost(const QUrl &url)