Download favicon in the background after credential add

This commit is contained in:
varjolintu 2021-11-25 19:47:45 +02:00 committed by Jonathan White
parent d16fc2d62a
commit 12d16f67ae
7 changed files with 53 additions and 10 deletions

View File

@ -364,11 +364,13 @@ QJsonObject BrowserAction::handleSetLogin(const QJsonObject& json, const QString
const QString uuid = decrypted.value("uuid").toString(); const QString uuid = decrypted.value("uuid").toString();
const QString group = decrypted.value("group").toString(); const QString group = decrypted.value("group").toString();
const QString groupUuid = decrypted.value("groupUuid").toString(); const QString groupUuid = decrypted.value("groupUuid").toString();
const QString downloadFavicon = decrypted.value("downloadFavicon").toString();
const QString realm; const QString realm;
bool result = true; bool result = true;
if (uuid.isEmpty()) { if (uuid.isEmpty()) {
browserService()->addEntry(id, login, password, url, submitUrl, realm, group, groupUuid); auto dlFavicon = !downloadFavicon.isEmpty() && downloadFavicon.compare(TRUE_STR) == 0;
browserService()->addEntry(id, login, password, url, submitUrl, realm, group, groupUuid, dlFavicon);
} else { } else {
if (!Tools::isValidUuid(uuid)) { if (!Tools::isValidUuid(uuid)) {
return getErrorReply(action, ERROR_KEEPASS_NO_VALID_UUID_PROVIDED); return getErrorReply(action, ERROR_KEEPASS_NO_VALID_UUID_PROVIDED);

View File

@ -494,6 +494,7 @@ void BrowserService::addEntry(const QString& dbid,
const QString& realm, const QString& realm,
const QString& group, const QString& group,
const QString& groupUuid, const QString& groupUuid,
const bool downloadFavicon,
const QSharedPointer<Database>& selectedDb) const QSharedPointer<Database>& selectedDb)
{ {
// TODO: select database based on this key id // TODO: select database based on this key id
@ -537,6 +538,10 @@ void BrowserService::addEntry(const QString& dbid,
config.setRealm(realm); config.setRealm(realm);
} }
config.save(entry); config.save(entry);
if (downloadFavicon && m_currentDatabaseWidget) {
m_currentDatabaseWidget->downloadFaviconInBackground(entry);
}
} }
bool BrowserService::updateEntry(const QString& dbid, bool BrowserService::updateEntry(const QString& dbid,

View File

@ -67,6 +67,7 @@ public:
const QString& realm, const QString& realm,
const QString& group, const QString& group,
const QString& groupUuid, const QString& groupUuid,
const bool downloadFavicon,
const QSharedPointer<Database>& selectedDb = {}); const QSharedPointer<Database>& selectedDb = {});
bool updateEntry(const QString& dbid, bool updateEntry(const QString& dbid,
const QString& uuid, const QString& uuid,

View File

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de> * Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2021 KeePassXC Team <team@keepassxc.org>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -794,15 +794,30 @@ void DatabaseWidget::downloadAllFavicons()
#endif #endif
} }
void DatabaseWidget::performIconDownloads(const QList<Entry*>& entries, bool force) void DatabaseWidget::downloadFaviconInBackground(Entry* entry)
{
#ifdef WITH_XC_NETWORKING
performIconDownloads({entry}, true, true);
#else
Q_UNUSED(entry);
#endif
}
void DatabaseWidget::performIconDownloads(const QList<Entry*>& entries, bool force, bool downloadInBackground)
{ {
#ifdef WITH_XC_NETWORKING #ifdef WITH_XC_NETWORKING
auto* iconDownloaderDialog = new IconDownloaderDialog(this); auto* iconDownloaderDialog = new IconDownloaderDialog(this);
connect(this, SIGNAL(databaseLockRequested()), iconDownloaderDialog, SLOT(close())); connect(this, SIGNAL(databaseLockRequested()), iconDownloaderDialog, SLOT(close()));
iconDownloaderDialog->downloadFavicons(m_db, entries, force);
if (downloadInBackground && entries.count() > 0) {
iconDownloaderDialog->downloadFaviconInBackground(m_db, entries.first());
} else {
iconDownloaderDialog->downloadFavicons(m_db, entries, force);
}
#else #else
Q_UNUSED(entries); Q_UNUSED(entries);
Q_UNUSED(force); Q_UNUSED(force);
Q_UNUSED(downloadInBackground);
#endif #endif
} }

View File

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de> * Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2021 KeePassXC Team <team@keepassxc.org>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -193,6 +193,7 @@ public slots:
void openUrl(); void openUrl();
void downloadSelectedFavicons(); void downloadSelectedFavicons();
void downloadAllFavicons(); void downloadAllFavicons();
void downloadFaviconInBackground(Entry* entry);
void openUrlForEntry(Entry* entry); void openUrlForEntry(Entry* entry);
void createGroup(); void createGroup();
void cloneGroup(); void cloneGroup();
@ -259,7 +260,7 @@ private:
void setClipboardTextAndMinimize(const QString& text); void setClipboardTextAndMinimize(const QString& text);
void processAutoOpen(); void processAutoOpen();
void openDatabaseFromEntry(const Entry* entry, bool inBackground = true); void openDatabaseFromEntry(const Entry* entry, bool inBackground = true);
void performIconDownloads(const QList<Entry*>& entries, bool force = false); void performIconDownloads(const QList<Entry*>& entries, bool force = false, bool downloadInBackground = false);
bool performSave(QString& errorMessage, const QString& fileName = {}); bool performSave(QString& errorMessage, const QString& fileName = {});
QSharedPointer<Database> m_db; QSharedPointer<Database> m_db;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org> * Copyright (C) 2021 KeePassXC Team <team@keepassxc.org>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -102,6 +102,23 @@ void IconDownloaderDialog::downloadFavicons(const QSharedPointer<Database>& data
} }
} }
void IconDownloaderDialog::downloadFaviconInBackground(const QSharedPointer<Database>& database, Entry* entry)
{
m_db = database;
m_urlToEntries.clear();
abortDownloads();
auto webUrl = entry->webUrl();
if (!webUrl.isEmpty()) {
m_urlToEntries.insert(webUrl, entry);
}
if (m_urlToEntries.count() > 0) {
m_activeDownloaders.append(createDownloader(webUrl));
m_activeDownloaders.first()->download();
}
}
IconDownloader* IconDownloaderDialog::createDownloader(const QString& url) IconDownloader* IconDownloaderDialog::createDownloader(const QString& url)
{ {
auto downloader = new IconDownloader(); auto downloader = new IconDownloader();
@ -131,9 +148,10 @@ void IconDownloaderDialog::downloadFinished(const QString& url, const QImage& ic
if (m_db && !icon.isNull()) { if (m_db && !icon.isNull()) {
// Don't add an icon larger than 128x128, but retain original size if smaller // Don't add an icon larger than 128x128, but retain original size if smaller
constexpr auto maxIconSize = 128;
auto scaledIcon = icon; auto scaledIcon = icon;
if (icon.width() > 128 || icon.height() > 128) { if (icon.width() > maxIconSize || icon.height() > maxIconSize) {
scaledIcon = icon.scaled(128, 128); scaledIcon = icon.scaled(maxIconSize, maxIconSize);
} }
QByteArray serializedIcon = Icons::saveToBytes(scaledIcon); QByteArray serializedIcon = Icons::saveToBytes(scaledIcon);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org> * Copyright (C) 2021 KeePassXC Team <team@keepassxc.org>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -42,6 +42,7 @@ public:
~IconDownloaderDialog() override; ~IconDownloaderDialog() override;
void downloadFavicons(const QSharedPointer<Database>& database, const QList<Entry*>& entries, bool force = false); void downloadFavicons(const QSharedPointer<Database>& database, const QList<Entry*>& entries, bool force = false);
void downloadFaviconInBackground(const QSharedPointer<Database>& database, Entry* entry);
private slots: private slots:
void downloadFinished(const QString& url, const QImage& icon); void downloadFinished(const QString& url, const QImage& icon);