diff --git a/src/gui/EditWidgetIcons.cpp b/src/gui/EditWidgetIcons.cpp index ba1d9b231..58117679e 100644 --- a/src/gui/EditWidgetIcons.cpp +++ b/src/gui/EditWidgetIcons.cpp @@ -19,7 +19,8 @@ #include "ui_EditWidgetIcons.h" #include -#include +#include +#include #include "core/Group.h" #include "core/Metadata.h" @@ -39,6 +40,8 @@ EditWidgetIcons::EditWidgetIcons(QWidget* parent) , m_database(nullptr) , m_defaultIconModel(new DefaultIconModel(this)) , m_customIconModel(new CustomIconModel(this)) + , m_networkAccessMngr(new QNetworkAccessManager(this)) + , m_networkOperation(nullptr) { m_ui->setupUi(this); @@ -55,14 +58,22 @@ EditWidgetIcons::EditWidgetIcons(QWidget* parent) this, SLOT(updateWidgetsCustomIcons(bool))); connect(m_ui->addButton, SIGNAL(clicked()), SLOT(addCustomIcon())); connect(m_ui->deleteButton, SIGNAL(clicked()), SLOT(removeCustomIcon())); + connect(m_ui->faviconButton, SIGNAL(clicked()), SLOT(downloadFavicon())); + connect(m_networkAccessMngr, SIGNAL(finished(QNetworkReply*)), + this, SLOT(onRequestFinished(QNetworkReply*)) ); + + m_ui->faviconButton->setVisible(false); } EditWidgetIcons::~EditWidgetIcons() { } -IconStruct EditWidgetIcons::state() const +IconStruct EditWidgetIcons::state() { + Q_ASSERT(m_database); + Q_ASSERT(!m_currentUuid.isNull()); + IconStruct iconStruct; if (m_ui->defaultIconsRadio->isChecked()) { QModelIndex index = m_ui->defaultIconsView->currentIndex(); @@ -82,7 +93,7 @@ IconStruct EditWidgetIcons::state() const iconStruct.number = -1; } } - + return iconStruct; } @@ -92,13 +103,14 @@ void EditWidgetIcons::reset() m_currentUuid = Uuid(); } -void EditWidgetIcons::load(Uuid currentUuid, Database* database, IconStruct iconStruct) +void EditWidgetIcons::load(Uuid currentUuid, Database* database, IconStruct iconStruct, const QString &url) { Q_ASSERT(database); Q_ASSERT(!currentUuid.isNull()); m_database = database; m_currentUuid = currentUuid; + setUrl(url); m_customIconModel->setIcons(database->metadata()->customIconsScaledPixmaps(), database->metadata()->customIconsOrder()); @@ -122,6 +134,55 @@ void EditWidgetIcons::load(Uuid currentUuid, Database* database, IconStruct icon } } +void EditWidgetIcons::setUrl(const QString &url) +{ + m_url = url; + m_ui->faviconButton->setVisible(!url.isEmpty()); + abortFaviconDownload(); +} + +void EditWidgetIcons::downloadFavicon() +{ + if (m_networkOperation == nullptr) { + QUrl url(m_url); + QString path = "http://www.google.com/s2/favicons?domain=" + url.host(); + + m_networkOperation = m_networkAccessMngr->get(QNetworkRequest(path)); + m_ui->faviconButton->setDisabled(true); + } +} + +void EditWidgetIcons::abortFaviconDownload() +{ + if (m_networkOperation != nullptr) { + m_networkOperation->abort(); + m_networkOperation->deleteLater(); + m_networkOperation = nullptr; + } + m_ui->faviconButton->setDisabled(false); +} + +void EditWidgetIcons::onRequestFinished(QNetworkReply *reply) +{ + if (!reply->error()) { + QImage image; + image.loadFromData(reply->readAll()); + + if (!image.isNull()) { + //Set the image + Uuid uuid = Uuid::random(); + m_database->metadata()->addCustomIcon(uuid, image.scaled(16, 16)); + m_customIconModel->setIcons(m_database->metadata()->customIconsScaledPixmaps(), + m_database->metadata()->customIconsOrder()); + QModelIndex index = m_customIconModel->indexFromUuid(uuid); + m_ui->customIconsView->setCurrentIndex(index); + m_ui->customIconsRadio->setChecked(true); + } + } + + abortFaviconDownload(); +} + void EditWidgetIcons::addCustomIcon() { if (m_database) { @@ -131,21 +192,17 @@ void EditWidgetIcons::addCustomIcon() QString filename = QFileDialog::getOpenFileName( this, tr("Select Image"), "", filter); if (!filename.isEmpty()) { - QImageReader imageReader(filename); - // detect from content, otherwise reading fails if file extension is wrong - imageReader.setDecideFormatFromContent(true); - QImage image = imageReader.read(); + QImage image(filename); if (!image.isNull()) { Uuid uuid = Uuid::random(); - m_database->metadata()->addCustomIconScaled(uuid, image); + m_database->metadata()->addCustomIcon(uuid, image.scaled(16, 16)); m_customIconModel->setIcons(m_database->metadata()->customIconsScaledPixmaps(), m_database->metadata()->customIconsOrder()); QModelIndex index = m_customIconModel->indexFromUuid(uuid); m_ui->customIconsView->setCurrentIndex(index); } else { - MessageBox::critical(this, tr("Error"), - tr("Can't read icon:").append("\n").append(imageReader.errorString())); + MessageBox::critical(this, tr("Error"), tr("Can't read icon")); } } } @@ -200,7 +257,8 @@ void EditWidgetIcons::removeCustomIcon() } else { MessageBox::information(this, tr("Can't delete icon!"), - tr("Can't delete icon. Still used by %n item(s).", 0, iconUsedCount)); + tr("Can't delete icon. Still used by %1 items.") + .arg(iconUsedCount)); } } } diff --git a/src/gui/EditWidgetIcons.h b/src/gui/EditWidgetIcons.h index db58801fa..9330a4b85 100644 --- a/src/gui/EditWidgetIcons.h +++ b/src/gui/EditWidgetIcons.h @@ -19,7 +19,12 @@ #define KEEPASSX_EDITWIDGETICONS_H #include +#include +#include +#include + +#include "core/Global.h" #include "core/Uuid.h" class Database; @@ -46,11 +51,17 @@ public: explicit EditWidgetIcons(QWidget* parent = nullptr); ~EditWidgetIcons(); - IconStruct state() const; + IconStruct state(); void reset(); - void load(Uuid currentUuid, Database* database, IconStruct iconStruct); + void load(Uuid currentUuid, Database* database, IconStruct iconStruct, const QString &url = QString()); + +public Q_SLOTS: + void setUrl(const QString &url); private Q_SLOTS: + void downloadFavicon(); + void abortFaviconDownload(); + void onRequestFinished(QNetworkReply *reply); void addCustomIcon(); void removeCustomIcon(); void updateWidgetsDefaultIcons(bool checked); @@ -62,8 +73,11 @@ private: const QScopedPointer m_ui; Database* m_database; Uuid m_currentUuid; + QString m_url; DefaultIconModel* const m_defaultIconModel; CustomIconModel* const m_customIconModel; + QNetworkAccessManager* const m_networkAccessMngr; + QNetworkReply* m_networkOperation; Q_DISABLE_COPY(EditWidgetIcons) }; diff --git a/src/gui/EditWidgetIcons.ui b/src/gui/EditWidgetIcons.ui index bbdcebca5..047e106b8 100644 --- a/src/gui/EditWidgetIcons.ui +++ b/src/gui/EditWidgetIcons.ui @@ -91,6 +91,13 @@ + + + + Download favicon + + + diff --git a/src/gui/entry/EditEntryWidget.cpp b/src/gui/entry/EditEntryWidget.cpp index 77f68c927..eb5ebd476 100644 --- a/src/gui/entry/EditEntryWidget.cpp +++ b/src/gui/entry/EditEntryWidget.cpp @@ -348,7 +348,8 @@ void EditEntryWidget::setForms(const Entry* entry, bool restore) IconStruct iconStruct; iconStruct.uuid = entry->iconUuid(); iconStruct.number = entry->iconNumber(); - m_iconsWidget->load(entry->uuid(), m_database, iconStruct); + m_iconsWidget->load(entry->uuid(), m_database, iconStruct, entry->url()); + connect(m_mainUi->urlEdit, SIGNAL(textChanged(QString)), m_iconsWidget, SLOT(setUrl(QString))); m_autoTypeUi->enableButton->setChecked(entry->autoTypeEnabled()); if (entry->defaultAutoTypeSequence().isEmpty()) {