Skip to content

Commit

Permalink
Add url field to download favicon
Browse files Browse the repository at this point in the history
  • Loading branch information
peshevv authored and droidmonkey committed Dec 12, 2021
1 parent 12d16f6 commit 4567265
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
16 changes: 12 additions & 4 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3239,10 +3239,6 @@ Supported extensions are: %1.</source>
</context>
<context>
<name>EditWidgetIcons</name>
<message>
<source>Add custom icon</source>
<translation>Add custom icon</translation>
</message>
<message>
<source>Download favicon</source>
<translation>Download favicon</translation>
Expand Down Expand Up @@ -3332,6 +3328,18 @@ Supported extensions are: %1.</source>
<source>Apply icon to…</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Choose icon…</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Set the URL to use to search for a favicon</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Favicon URL</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>EditWidgetProperties</name>
Expand Down
30 changes: 24 additions & 6 deletions src/gui/EditWidgetIcons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "gui/IconDownloader.h"
#endif

#include <QKeyEvent>

IconStruct::IconStruct()
: uuid(QUuid())
, number(0)
Expand Down Expand Up @@ -78,8 +80,10 @@ EditWidgetIcons::EditWidgetIcons(QWidget* parent)
#endif
// clang-format on

#ifndef WITH_XC_NETWORKING
m_ui->faviconButton->setVisible(false);
m_ui->addButton->setEnabled(true);
m_ui->faviconURL->setVisible(false);
#endif
}

EditWidgetIcons::~EditWidgetIcons()
Expand Down Expand Up @@ -175,22 +179,36 @@ QMenu* EditWidgetIcons::createApplyIconToMenu()
return applyIconToMenu;
}

void EditWidgetIcons::keyPressEvent(QKeyEvent* event)
{
if (m_ui->faviconURL->hasFocus() && (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)) {
m_ui->faviconButton->animateClick();
} else {
QWidget::keyPressEvent(event);
}
}

void EditWidgetIcons::setUrl(const QString& url)
{
#ifdef WITH_XC_NETWORKING
m_url = url;
m_ui->faviconButton->setVisible(!url.isEmpty());
QUrl urlCheck(url);
if (urlCheck.scheme().startsWith("http")) {
m_ui->faviconURL->setText(urlCheck.url(QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment));
m_ui->faviconURL->setCursorPosition(0);
} else {
m_ui->faviconURL->setText("");
}
#else
Q_UNUSED(url);
m_ui->faviconButton->setVisible(false);
#endif
}

void EditWidgetIcons::downloadFavicon()
{
#ifdef WITH_XC_NETWORKING
if (!m_url.isEmpty()) {
m_downloader->setUrl(m_url);
auto url = m_ui->faviconURL->text();
if (!url.isEmpty()) {
m_downloader->setUrl(url);
m_downloader->download();
}
#endif
Expand Down
6 changes: 4 additions & 2 deletions src/gui/EditWidgetIcons.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class EditWidgetIcons : public QWidget
const QString& url = "");
void setShowApplyIconToButton(bool state);

protected:
void keyPressEvent(QKeyEvent* event) override;

public slots:
void setUrl(const QString& url);
void abortRequests();
Expand Down Expand Up @@ -102,8 +105,7 @@ private slots:
DefaultIconModel* const m_defaultIconModel;
CustomIconModel* const m_customIconModel;
#ifdef WITH_XC_NETWORKING
QScopedPointer<IconDownloader> m_downloader;
QString m_url;
QSharedPointer<IconDownloader> m_downloader;
#endif

Q_DISABLE_COPY(EditWidgetIcons)
Expand Down
34 changes: 32 additions & 2 deletions src/gui/EditWidgetIcons.ui
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,40 @@
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="customIconButtonsHorizontalLayout">
<layout class="QHBoxLayout" name="customIconButtonsHorizontalLayout" stretch="0,0,0,0">
<item>
<widget class="QPushButton" name="addButton">
<property name="text">
<string>Add custom icon</string>
<string>Choose icon…</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLineEdit" name="faviconURL">
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
<property name="toolTip">
<string>Set the URL to use to search for a favicon</string>
</property>
<property name="placeholderText">
<string>Favicon URL</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -174,6 +203,7 @@
<tabstop>customIconsRadio</tabstop>
<tabstop>customIconsView</tabstop>
<tabstop>addButton</tabstop>
<tabstop>faviconURL</tabstop>
<tabstop>faviconButton</tabstop>
<tabstop>applyIconToPushButton</tabstop>
</tabstops>
Expand Down

0 comments on commit 4567265

Please sign in to comment.