Skip to content

Commit

Permalink
Add UI warning for invalid URL
Browse files Browse the repository at this point in the history
  • Loading branch information
varjolintu committed Nov 7, 2019
1 parent d4cb319 commit 3cdc3f7
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ set(keepassx_SOURCES
gui/TotpDialog.cpp
gui/TotpExportSettingsDialog.cpp
gui/DatabaseOpenDialog.cpp
gui/URLEdit.cpp
gui/WelcomeWidget.cpp
gui/csvImport/CsvImportWidget.cpp
gui/csvImport/CsvImportWizard.cpp
Expand Down
89 changes: 89 additions & 0 deletions src/gui/URLEdit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (C) 2014 Felix Geyer <[email protected]>
* Copyright (C) 2019 KeePassXC Team <[email protected]>
*
* 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
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "URLEdit.h"

#include <QRegularExpression>
#include <QUrl>

#include "core/Config.h"
#include "core/FilePath.h"
#include "gui/Font.h"

const QColor URLEdit::CorrectSoFarColor = QColor(255, 205, 15);
const QColor URLEdit::ErrorColor = QColor(255, 125, 125);

URLEdit::URLEdit(QWidget* parent)
: QLineEdit(parent)
{
const QIcon errorIcon = filePath()->icon("status", "dialog-error");
m_errorAction = addAction(errorIcon, QLineEdit::TrailingPosition);
m_errorAction->setVisible(false);
m_errorAction->setToolTip(tr("Invalid URL"));

const QIcon correctIcon = filePath()->icon("actions", "dialog-ok");
m_correctAction = addAction(correctIcon, QLineEdit::TrailingPosition);
m_correctAction->setVisible(false);
m_correctAction->setToolTip(tr("URL is valid"));

updateStylesheet();
}

void URLEdit::enableVerifyMode()
{
updateStylesheet();

connect(this, SIGNAL(textChanged(QString)), SLOT(updateStylesheet()));
}

bool URLEdit::validURL() const
{
if (text().isEmpty()) {
return true;
}

QUrl url(text());

if (url.host().isEmpty() || url.scheme().isEmpty() || !url.isValid()) {
return false;
}

// Check for illegal characters
QRegularExpression re("[\"<>\\^`{|}]");
auto match = re.match(text());
if (match.hasMatch()) {
return false;
}

return true;
}

void URLEdit::updateStylesheet()
{
const QString stylesheetTemplate("QLineEdit { background: %1; }");

if (!validURL()) {
setStyleSheet(stylesheetTemplate.arg(ErrorColor.name()));
m_correctAction->setVisible(false);
m_errorAction->setVisible(true);
} else {
m_correctAction->setVisible(false);
m_errorAction->setVisible(false);
setStyleSheet("");
}
}
47 changes: 47 additions & 0 deletions src/gui/URLEdit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2014 Felix Geyer <[email protected]>
* Copyright (C) 2019 KeePassXC Team <[email protected]>
*
* 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
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef KEEPASSX_URLEDIT_H
#define KEEPASSX_URLEDIT_H

#include <QAction>
#include <QLineEdit>
#include <QPointer>

class URLEdit : public QLineEdit
{
Q_OBJECT

public:
static const QColor CorrectSoFarColor;
static const QColor ErrorColor;

explicit URLEdit(QWidget* parent = nullptr);
void enableVerifyMode();

private slots:
void updateStylesheet();

private:
bool validURL() const;

QPointer<QAction> m_errorAction;
QPointer<QAction> m_correctAction;
};

#endif // KEEPASSX_URLEDIT_H
1 change: 1 addition & 0 deletions src/gui/entry/EditEntryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ void EditEntryWidget::setupMain()
#ifdef WITH_XC_NETWORKING
connect(m_mainUi->fetchFaviconButton, SIGNAL(clicked()), m_iconsWidget, SLOT(downloadFavicon()));
connect(m_mainUi->urlEdit, SIGNAL(textChanged(QString)), m_iconsWidget, SLOT(setUrl(QString)));
m_mainUi->urlEdit->enableVerifyMode();
#endif
connect(m_mainUi->expireCheck, SIGNAL(toggled(bool)), m_mainUi->expireDatePicker, SLOT(setEnabled(bool)));
connect(m_mainUi->notesEnabled, SIGNAL(toggled(bool)), this, SLOT(toggleHideNotes(bool)));
Expand Down
8 changes: 7 additions & 1 deletion src/gui/entry/EditEntryWidgetMain.ui
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<item row="5" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QLineEdit" name="urlEdit">
<widget class="URLEdit" name="urlEdit">
<property name="accessibleName">
<string>Url field</string>
</property>
Expand Down Expand Up @@ -256,6 +256,12 @@
<header>gui/PasswordEdit.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>URLEdit</class>
<extends>QLineEdit</extends>
<header>gui/URLEdit.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>titleEdit</tabstop>
Expand Down

0 comments on commit 3cdc3f7

Please sign in to comment.