mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-06-22 05:34:40 -04:00
Download favorite icon of entry url and add to the custom icon registry (#30)
* Favicon download button. * Simplified icon grabbing code
This commit is contained in:
parent
6ac773d5ae
commit
c4b3f08618
4 changed files with 95 additions and 15 deletions
|
@ -19,7 +19,8 @@
|
||||||
#include "ui_EditWidgetIcons.h"
|
#include "ui_EditWidgetIcons.h"
|
||||||
|
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
#include <QImageReader>
|
#include <QMessageBox>
|
||||||
|
#include <QFileDialog>
|
||||||
|
|
||||||
#include "core/Group.h"
|
#include "core/Group.h"
|
||||||
#include "core/Metadata.h"
|
#include "core/Metadata.h"
|
||||||
|
@ -39,6 +40,8 @@ EditWidgetIcons::EditWidgetIcons(QWidget* parent)
|
||||||
, m_database(nullptr)
|
, m_database(nullptr)
|
||||||
, m_defaultIconModel(new DefaultIconModel(this))
|
, m_defaultIconModel(new DefaultIconModel(this))
|
||||||
, m_customIconModel(new CustomIconModel(this))
|
, m_customIconModel(new CustomIconModel(this))
|
||||||
|
, m_networkAccessMngr(new QNetworkAccessManager(this))
|
||||||
|
, m_networkOperation(nullptr)
|
||||||
{
|
{
|
||||||
m_ui->setupUi(this);
|
m_ui->setupUi(this);
|
||||||
|
|
||||||
|
@ -55,14 +58,22 @@ EditWidgetIcons::EditWidgetIcons(QWidget* parent)
|
||||||
this, SLOT(updateWidgetsCustomIcons(bool)));
|
this, SLOT(updateWidgetsCustomIcons(bool)));
|
||||||
connect(m_ui->addButton, SIGNAL(clicked()), SLOT(addCustomIcon()));
|
connect(m_ui->addButton, SIGNAL(clicked()), SLOT(addCustomIcon()));
|
||||||
connect(m_ui->deleteButton, SIGNAL(clicked()), SLOT(removeCustomIcon()));
|
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()
|
EditWidgetIcons::~EditWidgetIcons()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
IconStruct EditWidgetIcons::state() const
|
IconStruct EditWidgetIcons::state()
|
||||||
{
|
{
|
||||||
|
Q_ASSERT(m_database);
|
||||||
|
Q_ASSERT(!m_currentUuid.isNull());
|
||||||
|
|
||||||
IconStruct iconStruct;
|
IconStruct iconStruct;
|
||||||
if (m_ui->defaultIconsRadio->isChecked()) {
|
if (m_ui->defaultIconsRadio->isChecked()) {
|
||||||
QModelIndex index = m_ui->defaultIconsView->currentIndex();
|
QModelIndex index = m_ui->defaultIconsView->currentIndex();
|
||||||
|
@ -82,7 +93,7 @@ IconStruct EditWidgetIcons::state() const
|
||||||
iconStruct.number = -1;
|
iconStruct.number = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return iconStruct;
|
return iconStruct;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,13 +103,14 @@ void EditWidgetIcons::reset()
|
||||||
m_currentUuid = Uuid();
|
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(database);
|
||||||
Q_ASSERT(!currentUuid.isNull());
|
Q_ASSERT(!currentUuid.isNull());
|
||||||
|
|
||||||
m_database = database;
|
m_database = database;
|
||||||
m_currentUuid = currentUuid;
|
m_currentUuid = currentUuid;
|
||||||
|
setUrl(url);
|
||||||
|
|
||||||
m_customIconModel->setIcons(database->metadata()->customIconsScaledPixmaps(),
|
m_customIconModel->setIcons(database->metadata()->customIconsScaledPixmaps(),
|
||||||
database->metadata()->customIconsOrder());
|
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()
|
void EditWidgetIcons::addCustomIcon()
|
||||||
{
|
{
|
||||||
if (m_database) {
|
if (m_database) {
|
||||||
|
@ -131,21 +192,17 @@ void EditWidgetIcons::addCustomIcon()
|
||||||
QString filename = QFileDialog::getOpenFileName(
|
QString filename = QFileDialog::getOpenFileName(
|
||||||
this, tr("Select Image"), "", filter);
|
this, tr("Select Image"), "", filter);
|
||||||
if (!filename.isEmpty()) {
|
if (!filename.isEmpty()) {
|
||||||
QImageReader imageReader(filename);
|
QImage image(filename);
|
||||||
// detect from content, otherwise reading fails if file extension is wrong
|
|
||||||
imageReader.setDecideFormatFromContent(true);
|
|
||||||
QImage image = imageReader.read();
|
|
||||||
if (!image.isNull()) {
|
if (!image.isNull()) {
|
||||||
Uuid uuid = Uuid::random();
|
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_customIconModel->setIcons(m_database->metadata()->customIconsScaledPixmaps(),
|
||||||
m_database->metadata()->customIconsOrder());
|
m_database->metadata()->customIconsOrder());
|
||||||
QModelIndex index = m_customIconModel->indexFromUuid(uuid);
|
QModelIndex index = m_customIconModel->indexFromUuid(uuid);
|
||||||
m_ui->customIconsView->setCurrentIndex(index);
|
m_ui->customIconsView->setCurrentIndex(index);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
MessageBox::critical(this, tr("Error"),
|
MessageBox::critical(this, tr("Error"), tr("Can't read icon"));
|
||||||
tr("Can't read icon:").append("\n").append(imageReader.errorString()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -200,7 +257,8 @@ void EditWidgetIcons::removeCustomIcon()
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
MessageBox::information(this, tr("Can't delete icon!"),
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,12 @@
|
||||||
#define KEEPASSX_EDITWIDGETICONS_H
|
#define KEEPASSX_EDITWIDGETICONS_H
|
||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
#include <QSet>
|
||||||
|
|
||||||
|
#include <QtNetwork/QNetworkAccessManager>
|
||||||
|
#include <QtNetwork/QNetworkReply>
|
||||||
|
|
||||||
|
#include "core/Global.h"
|
||||||
#include "core/Uuid.h"
|
#include "core/Uuid.h"
|
||||||
|
|
||||||
class Database;
|
class Database;
|
||||||
|
@ -46,11 +51,17 @@ public:
|
||||||
explicit EditWidgetIcons(QWidget* parent = nullptr);
|
explicit EditWidgetIcons(QWidget* parent = nullptr);
|
||||||
~EditWidgetIcons();
|
~EditWidgetIcons();
|
||||||
|
|
||||||
IconStruct state() const;
|
IconStruct state();
|
||||||
void reset();
|
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:
|
private Q_SLOTS:
|
||||||
|
void downloadFavicon();
|
||||||
|
void abortFaviconDownload();
|
||||||
|
void onRequestFinished(QNetworkReply *reply);
|
||||||
void addCustomIcon();
|
void addCustomIcon();
|
||||||
void removeCustomIcon();
|
void removeCustomIcon();
|
||||||
void updateWidgetsDefaultIcons(bool checked);
|
void updateWidgetsDefaultIcons(bool checked);
|
||||||
|
@ -62,8 +73,11 @@ private:
|
||||||
const QScopedPointer<Ui::EditWidgetIcons> m_ui;
|
const QScopedPointer<Ui::EditWidgetIcons> m_ui;
|
||||||
Database* m_database;
|
Database* m_database;
|
||||||
Uuid m_currentUuid;
|
Uuid m_currentUuid;
|
||||||
|
QString m_url;
|
||||||
DefaultIconModel* const m_defaultIconModel;
|
DefaultIconModel* const m_defaultIconModel;
|
||||||
CustomIconModel* const m_customIconModel;
|
CustomIconModel* const m_customIconModel;
|
||||||
|
QNetworkAccessManager* const m_networkAccessMngr;
|
||||||
|
QNetworkReply* m_networkOperation;
|
||||||
|
|
||||||
Q_DISABLE_COPY(EditWidgetIcons)
|
Q_DISABLE_COPY(EditWidgetIcons)
|
||||||
};
|
};
|
||||||
|
|
|
@ -91,6 +91,13 @@
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="faviconButton">
|
||||||
|
<property name="text">
|
||||||
|
<string>Download favicon</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
|
|
|
@ -348,7 +348,8 @@ void EditEntryWidget::setForms(const Entry* entry, bool restore)
|
||||||
IconStruct iconStruct;
|
IconStruct iconStruct;
|
||||||
iconStruct.uuid = entry->iconUuid();
|
iconStruct.uuid = entry->iconUuid();
|
||||||
iconStruct.number = entry->iconNumber();
|
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());
|
m_autoTypeUi->enableButton->setChecked(entry->autoTypeEnabled());
|
||||||
if (entry->defaultAutoTypeSequence().isEmpty()) {
|
if (entry->defaultAutoTypeSequence().isEmpty()) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue