mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-02-04 17:05:23 -05:00
Fetch favicon from the root of the website and use Google as a fallback (#36)
* Replace favicon fetching using Google with fetching from the root of the website * Follow up to 3 http redirects for the favicon * Add download favicon from Google as fallback * Move code responsible for fetching the favicon from Google on its own function to reduce repetitiveness.
This commit is contained in:
parent
5bb27b2989
commit
119af3d760
@ -143,28 +143,54 @@ void EditWidgetIcons::setUrl(const QString &url)
|
|||||||
|
|
||||||
void EditWidgetIcons::downloadFavicon()
|
void EditWidgetIcons::downloadFavicon()
|
||||||
{
|
{
|
||||||
if (m_networkOperation == nullptr) {
|
QUrl url = QUrl(m_url);
|
||||||
QUrl url(m_url);
|
url.setPath("/favicon.ico");
|
||||||
QString path = "http://www.google.com/s2/favicons?domain=" + url.host();
|
fetchFavicon(url);
|
||||||
|
}
|
||||||
|
|
||||||
m_networkOperation = m_networkAccessMngr->get(QNetworkRequest(path));
|
void EditWidgetIcons::fetchFavicon(QUrl url)
|
||||||
|
{
|
||||||
|
if (m_networkOperation == nullptr) {
|
||||||
|
m_networkOperation = m_networkAccessMngr->get(QNetworkRequest(url));
|
||||||
m_ui->faviconButton->setDisabled(true);
|
m_ui->faviconButton->setDisabled(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditWidgetIcons::abortFaviconDownload()
|
void EditWidgetIcons::fetchFaviconFromGoogle(QString domain)
|
||||||
|
{
|
||||||
|
if (m_fallbackToGoogle) {
|
||||||
|
abortFaviconDownload();
|
||||||
|
m_fallbackToGoogle = false;
|
||||||
|
fetchFavicon(QUrl("http://www.google.com/s2/favicons?domain=" + domain));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
abortFaviconDownload();
|
||||||
|
MessageBox::warning(this, tr("Error"), tr("Unable to fetch favicon."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void EditWidgetIcons::abortFaviconDownload(bool clearRedirect)
|
||||||
{
|
{
|
||||||
if (m_networkOperation != nullptr) {
|
if (m_networkOperation != nullptr) {
|
||||||
m_networkOperation->abort();
|
m_networkOperation->abort();
|
||||||
m_networkOperation->deleteLater();
|
m_networkOperation->deleteLater();
|
||||||
m_networkOperation = nullptr;
|
m_networkOperation = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (clearRedirect) {
|
||||||
|
if (!m_redirectUrl.isEmpty()) {
|
||||||
|
m_redirectUrl.clear();
|
||||||
|
}
|
||||||
|
m_redirectCount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_fallbackToGoogle = true;
|
||||||
m_ui->faviconButton->setDisabled(false);
|
m_ui->faviconButton->setDisabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditWidgetIcons::onRequestFinished(QNetworkReply *reply)
|
void EditWidgetIcons::onRequestFinished(QNetworkReply *reply)
|
||||||
{
|
{
|
||||||
if (!reply->error()) {
|
if (!reply->error()) {
|
||||||
QImage image;
|
QImage image;
|
||||||
image.loadFromData(reply->readAll());
|
image.loadFromData(reply->readAll());
|
||||||
|
|
||||||
@ -177,10 +203,26 @@ void EditWidgetIcons::onRequestFinished(QNetworkReply *reply)
|
|||||||
QModelIndex index = m_customIconModel->indexFromUuid(uuid);
|
QModelIndex index = m_customIconModel->indexFromUuid(uuid);
|
||||||
m_ui->customIconsView->setCurrentIndex(index);
|
m_ui->customIconsView->setCurrentIndex(index);
|
||||||
m_ui->customIconsRadio->setChecked(true);
|
m_ui->customIconsRadio->setChecked(true);
|
||||||
|
|
||||||
|
abortFaviconDownload();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Check if server has sent a redirect
|
||||||
|
QUrl possibleRedirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
|
||||||
|
if (!possibleRedirectUrl.isEmpty() && possibleRedirectUrl != m_redirectUrl && m_redirectCount < 3) {
|
||||||
|
abortFaviconDownload(false);
|
||||||
|
m_redirectUrl = possibleRedirectUrl;
|
||||||
|
++m_redirectCount;
|
||||||
|
fetchFavicon(m_redirectUrl);
|
||||||
|
}
|
||||||
|
else { // Webpage is trying to redirect back to itself or the maximum number of redirects has been reached, fallback to Google
|
||||||
|
fetchFaviconFromGoogle(reply->url().host());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else { // Request Error e.g. 404, fallback to Google
|
||||||
abortFaviconDownload();
|
fetchFaviconFromGoogle(reply->url().host());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditWidgetIcons::addCustomIcon()
|
void EditWidgetIcons::addCustomIcon()
|
||||||
|
@ -60,7 +60,9 @@ public Q_SLOTS:
|
|||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void downloadFavicon();
|
void downloadFavicon();
|
||||||
void abortFaviconDownload();
|
void fetchFavicon(QUrl url);
|
||||||
|
void fetchFaviconFromGoogle(QString domain);
|
||||||
|
void abortFaviconDownload(bool clearRedirect = true);
|
||||||
void onRequestFinished(QNetworkReply *reply);
|
void onRequestFinished(QNetworkReply *reply);
|
||||||
void addCustomIcon();
|
void addCustomIcon();
|
||||||
void removeCustomIcon();
|
void removeCustomIcon();
|
||||||
@ -74,6 +76,9 @@ private:
|
|||||||
Database* m_database;
|
Database* m_database;
|
||||||
Uuid m_currentUuid;
|
Uuid m_currentUuid;
|
||||||
QString m_url;
|
QString m_url;
|
||||||
|
QUrl m_redirectUrl;
|
||||||
|
bool m_fallbackToGoogle = true;
|
||||||
|
unsigned short m_redirectCount = 0;
|
||||||
DefaultIconModel* const m_defaultIconModel;
|
DefaultIconModel* const m_defaultIconModel;
|
||||||
CustomIconModel* const m_customIconModel;
|
CustomIconModel* const m_customIconModel;
|
||||||
QNetworkAccessManager* const m_networkAccessMngr;
|
QNetworkAccessManager* const m_networkAccessMngr;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user