Commit 6489ce27 authored by Laurent Montel's avatar Laurent Montel 😁

Move emailvalidator here

parent e792563e
Pipeline #23824 passed with stage
in 10 minutes and 4 seconds
cmake_minimum_required(VERSION 3.5)
set(PIM_VERSION "5.14.46")
set(PIM_VERSION "5.14.47")
project(pimcommon VERSION ${PIM_VERSION})
......
......@@ -115,8 +115,14 @@ set(libpimcommon_networkmanager_SRCS
network/networkmanager.cpp
)
set(libpimcommon_misc_SRCS
misc/emailvalidator.cpp
)
set(libpimcommon_SRCS
settings/pimcommonsettings.cpp
${libpimcommon_misc_SRCS}
${libpimcommon_configure_plugins_SRCS}
${libpimcommon_networkmanager_SRCS}
${libpimcommon_configure_plugins_SRCS}
......@@ -213,6 +219,7 @@ if (BUILD_TESTING)
add_subdirectory(logactivities/tests)
add_subdirectory(configureplugins/autotests)
add_subdirectory(genericplugins/autotests)
add_subdirectory(misc/autotests)
endif()
ecm_generate_headers(PimCommon_CamelCasegenericplugins_HEADERS
......@@ -235,6 +242,14 @@ ecm_generate_headers(PimCommon_CamelCasenetworkmanager_HEADERS
RELATIVE network
)
ecm_generate_headers(PimCommon_CamelCasemisc_HEADERS
HEADER_NAMES
EmailValidator
REQUIRED_HEADERS PimCommon_misc_HEADERS
PREFIX PimCommon
RELATIVE misc
)
ecm_generate_headers(PimCommon_CamelCase_HEADERS
HEADER_NAMES
PimUtil
......@@ -383,6 +398,7 @@ install(FILES
${PimCommon_Camelcasetexteditor_commonwidget_HEADERS}
${PimCommon_Camelcasetranslator_HEADERS}
${PimCommon_Camelcasewidgets_HEADERS}
${PimCommon_CamelCasemisc_HEADERS}
DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/PimCommon
COMPONENT Devel
)
......@@ -415,6 +431,7 @@ install(FILES
${PimCommon_translator_HEADERS}
${PimCommon_widgets_HEADERS}
${PimCommon_logactivities_HEADERS}
${PimCommon_misc_HEADERS}
DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/pimcommon
COMPONENT Devel
......
macro(add_pimcommon_misc_unittest _name)
ecm_add_test(${ARGN}
TEST_NAME ${_name}
NAME_PREFIX "pimcommon-misc-"
LINK_LIBRARIES Qt5::Test
Qt5::Widgets
KF5::PimCommon
)
endmacro()
add_pimcommon_misc_unittest(emailvalidatortest emailvalidatortest.cpp)
/*
Copyright (C) 2017-2020 Laurent Montel <montel@kde.org>
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 of the License, or (at your option) any later version.
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; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "emailvalidatortest.h"
#include <PimCommon/EmailValidator>
#include <QTest>
QTEST_MAIN(EmailValidatorTest)
Q_DECLARE_METATYPE(QValidator::State)
EmailValidatorTest::EmailValidatorTest(QObject *parent)
: QObject(parent)
{
}
void EmailValidatorTest::shouldValidateEmail_data()
{
QTest::addColumn<QString>("email");
QTest::addColumn<QValidator::State>("state");
QTest::newRow("empty") << QString() << QValidator::Intermediate;
QTest::newRow("email") << QStringLiteral("foo@kde.org") << QValidator::Acceptable;
QTest::newRow("notanemail") << QStringLiteral("foo") << QValidator::Intermediate;
QTest::newRow("space") << QStringLiteral("foo ") << QValidator::Invalid;
QTest::newRow("space1") << QStringLiteral(" foo") << QValidator::Invalid;
QTest::newRow("email2") << QStringLiteral("<foo@kde.org>") << QValidator::Intermediate;
QTest::newRow("email3") << QStringLiteral("\"bla\" <foo@kde.org>") << QValidator::Invalid;
}
void EmailValidatorTest::shouldValidateEmail()
{
QFETCH(QString, email);
QFETCH(QValidator::State, state);
PimCommon::EmailValidator val(nullptr);
int pos;
QCOMPARE(val.validate(email, pos), state);
}
/*
Copyright (C) 2017-2020 Laurent Montel <montel@kde.org>
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 of the License, or (at your option) any later version.
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; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef EMAILVALIDATORTEST_H
#define EMAILVALIDATORTEST_H
#include <QObject>
class EmailValidatorTest : public QObject
{
Q_OBJECT
public:
explicit EmailValidatorTest(QObject *parent = nullptr);
~EmailValidatorTest() = default;
private Q_SLOTS:
void shouldValidateEmail_data();
void shouldValidateEmail();
};
#endif // EMAILVALIDATORTEST_H
/*
Copyright (C) 2010 Casey Link <unnamedrambler@gmail.com>
Copyright (C) 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "emailvalidator.h"
#include <KEmailAddress>
#include <QRegularExpression>
using namespace PimCommon;
EmailValidator::EmailValidator(QObject *parent) : QValidator(parent)
{
}
QValidator::State EmailValidator::validate(QString &str, int &pos) const
{
Q_UNUSED(pos);
if (KEmailAddress::isValidSimpleAddress(str)) {
return QValidator::Acceptable;
}
QRegularExpression re(QStringLiteral("\\s"));
QRegularExpressionMatch match = re.match(str);
if (match.hasMatch()) {
return QValidator::Invalid;
}
return QValidator::Intermediate;
}
void EmailValidator::fixup(QString &str) const
{
str = str.trimmed();
}
/*
Copyright (C) 2010 Casey Link <unnamedrambler@gmail.com>
Copyright (C) 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef EMAILVALIDATOR_H
#define EMAILVALIDATOR_H
#include "pimcommon_export.h"
#include <QValidator>
namespace PimCommon {
/**
A validator that enforces correct email addresses.
@see KEmailAddress::isValidSimpleAddress
*/
class PIMCOMMON_EXPORT EmailValidator : public QValidator //krazy:exclude=dpointer
{
Q_OBJECT
public:
explicit EmailValidator(QObject *parent);
State validate(QString &str, int &pos) const override;
void fixup(QString &str) const override;
};
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment