From 12d16f67ae433c066d29541c9576633ab222cd6d Mon Sep 17 00:00:00 2001 From: varjolintu Date: Thu, 25 Nov 2021 19:47:45 +0200 Subject: [PATCH] Download favicon in the background after credential add --- src/browser/BrowserAction.cpp | 4 +++- src/browser/BrowserService.cpp | 5 +++++ src/browser/BrowserService.h | 1 + src/gui/DatabaseWidget.cpp | 21 ++++++++++++++++++--- src/gui/DatabaseWidget.h | 5 +++-- src/gui/IconDownloaderDialog.cpp | 24 +++++++++++++++++++++--- src/gui/IconDownloaderDialog.h | 3 ++- 7 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/browser/BrowserAction.cpp b/src/browser/BrowserAction.cpp index c84248469..3f7665797 100644 --- a/src/browser/BrowserAction.cpp +++ b/src/browser/BrowserAction.cpp @@ -364,11 +364,13 @@ QJsonObject BrowserAction::handleSetLogin(const QJsonObject& json, const QString const QString uuid = decrypted.value("uuid").toString(); const QString group = decrypted.value("group").toString(); const QString groupUuid = decrypted.value("groupUuid").toString(); + const QString downloadFavicon = decrypted.value("downloadFavicon").toString(); const QString realm; bool result = true; 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 { if (!Tools::isValidUuid(uuid)) { return getErrorReply(action, ERROR_KEEPASS_NO_VALID_UUID_PROVIDED); diff --git a/src/browser/BrowserService.cpp b/src/browser/BrowserService.cpp index 46de0a9d3..b4b2f98f3 100644 --- a/src/browser/BrowserService.cpp +++ b/src/browser/BrowserService.cpp @@ -494,6 +494,7 @@ void BrowserService::addEntry(const QString& dbid, const QString& realm, const QString& group, const QString& groupUuid, + const bool downloadFavicon, const QSharedPointer& selectedDb) { // TODO: select database based on this key id @@ -537,6 +538,10 @@ void BrowserService::addEntry(const QString& dbid, config.setRealm(realm); } config.save(entry); + + if (downloadFavicon && m_currentDatabaseWidget) { + m_currentDatabaseWidget->downloadFaviconInBackground(entry); + } } bool BrowserService::updateEntry(const QString& dbid, diff --git a/src/browser/BrowserService.h b/src/browser/BrowserService.h index 829f3b108..111fe9d78 100644 --- a/src/browser/BrowserService.h +++ b/src/browser/BrowserService.h @@ -67,6 +67,7 @@ public: const QString& realm, const QString& group, const QString& groupUuid, + const bool downloadFavicon, const QSharedPointer& selectedDb = {}); bool updateEntry(const QString& dbid, const QString& uuid, diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 4e8011a92..2caacb7a0 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -1,6 +1,6 @@ /* - * Copyright (C) 2018 KeePassXC Team * Copyright (C) 2010 Felix Geyer + * Copyright (C) 2021 KeePassXC Team * * 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 @@ -794,15 +794,30 @@ void DatabaseWidget::downloadAllFavicons() #endif } -void DatabaseWidget::performIconDownloads(const QList& 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& entries, bool force, bool downloadInBackground) { #ifdef WITH_XC_NETWORKING auto* iconDownloaderDialog = new IconDownloaderDialog(this); 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 Q_UNUSED(entries); Q_UNUSED(force); + Q_UNUSED(downloadInBackground); #endif } diff --git a/src/gui/DatabaseWidget.h b/src/gui/DatabaseWidget.h index c329b98fa..f804d0518 100644 --- a/src/gui/DatabaseWidget.h +++ b/src/gui/DatabaseWidget.h @@ -1,6 +1,6 @@ /* - * Copyright (C) 2018 KeePassXC Team * Copyright (C) 2010 Felix Geyer + * Copyright (C) 2021 KeePassXC Team * * 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 @@ -193,6 +193,7 @@ public slots: void openUrl(); void downloadSelectedFavicons(); void downloadAllFavicons(); + void downloadFaviconInBackground(Entry* entry); void openUrlForEntry(Entry* entry); void createGroup(); void cloneGroup(); @@ -259,7 +260,7 @@ private: void setClipboardTextAndMinimize(const QString& text); void processAutoOpen(); void openDatabaseFromEntry(const Entry* entry, bool inBackground = true); - void performIconDownloads(const QList& entries, bool force = false); + void performIconDownloads(const QList& entries, bool force = false, bool downloadInBackground = false); bool performSave(QString& errorMessage, const QString& fileName = {}); QSharedPointer m_db; diff --git a/src/gui/IconDownloaderDialog.cpp b/src/gui/IconDownloaderDialog.cpp index ffb849bb8..929146bd3 100644 --- a/src/gui/IconDownloaderDialog.cpp +++ b/src/gui/IconDownloaderDialog.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 KeePassXC Team + * Copyright (C) 2021 KeePassXC Team * * 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 @@ -102,6 +102,23 @@ void IconDownloaderDialog::downloadFavicons(const QSharedPointer& data } } +void IconDownloaderDialog::downloadFaviconInBackground(const QSharedPointer& 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) { auto downloader = new IconDownloader(); @@ -131,9 +148,10 @@ void IconDownloaderDialog::downloadFinished(const QString& url, const QImage& ic if (m_db && !icon.isNull()) { // Don't add an icon larger than 128x128, but retain original size if smaller + constexpr auto maxIconSize = 128; auto scaledIcon = icon; - if (icon.width() > 128 || icon.height() > 128) { - scaledIcon = icon.scaled(128, 128); + if (icon.width() > maxIconSize || icon.height() > maxIconSize) { + scaledIcon = icon.scaled(maxIconSize, maxIconSize); } QByteArray serializedIcon = Icons::saveToBytes(scaledIcon); diff --git a/src/gui/IconDownloaderDialog.h b/src/gui/IconDownloaderDialog.h index e0a77070f..32f18bf23 100644 --- a/src/gui/IconDownloaderDialog.h +++ b/src/gui/IconDownloaderDialog.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 KeePassXC Team + * Copyright (C) 2021 KeePassXC Team * * 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 @@ -42,6 +42,7 @@ public: ~IconDownloaderDialog() override; void downloadFavicons(const QSharedPointer& database, const QList& entries, bool force = false); + void downloadFaviconInBackground(const QSharedPointer& database, Entry* entry); private slots: void downloadFinished(const QString& url, const QImage& icon);