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 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);

View File

@ -494,6 +494,7 @@ void BrowserService::addEntry(const QString& dbid,
const QString& realm,
const QString& group,
const QString& groupUuid,
const bool downloadFavicon,
const QSharedPointer<Database>& 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,

View File

@ -67,6 +67,7 @@ public:
const QString& realm,
const QString& group,
const QString& groupUuid,
const bool downloadFavicon,
const QSharedPointer<Database>& selectedDb = {});
bool updateEntry(const QString& dbid,
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) 2021 KeePassXC Team <team@keepassxc.org>
*
* 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<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
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
}

View File

@ -1,6 +1,6 @@
/*
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
* 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
* 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<Entry*>& entries, bool force = false);
void performIconDownloads(const QList<Entry*>& entries, bool force = false, bool downloadInBackground = false);
bool performSave(QString& errorMessage, const QString& fileName = {});
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
* 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)
{
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);

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
* 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>& database, const QList<Entry*>& entries, bool force = false);
void downloadFaviconInBackground(const QSharedPointer<Database>& database, Entry* entry);
private slots:
void downloadFinished(const QString& url, const QImage& icon);