| 1 | /* Copyright (C) 2015 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or |
| 4 | modify it under the terms of the GNU General Public License as |
| 5 | published by the Free Software Foundation; either version 2 of |
| 6 | the License or (at your option) version 3 |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | #include "emailvalidator.h" |
| 17 | #include <QDebug> |
| 18 | #include <QRegularExpression> |
| 19 | |
| 20 | EmailValidator::EmailValidator(QObject *parent) : |
| 21 | QObject(parent) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | bool EmailValidator::validate(const QString &emailAddress) |
| 26 | { |
| 27 | QString rfc5322NoWsFoldMailAddressPattern("([!#-'*+/-9=?A-Z^-~-]+(\\.[!#-'*+/-9=?A-Z^-~-]+)*|\"([]!#-[^-~ \t]|(\\[\t -~]))+\")@([!#-'*+/-9=?A-Z^-~-]+(\\.[!#-'*+/-9=?A-Z^-~-]+)*|\\[[\t -Z^-~]*])"); |
| 28 | |
| 29 | QRegularExpression regExpSingleMailAddress("^"+rfc5322NoWsFoldMailAddressPattern+"$"); |
| 30 | QRegularExpressionMatch match = regExpSingleMailAddress.match(emailAddress); |
| 31 | if (match.hasMatch()) |
| 32 | return true; |
| 33 | |
| 34 | QRegularExpression regexpMailAddressWithDisplayName("<"+rfc5322NoWsFoldMailAddressPattern+">$"); |
| 35 | match = regexpMailAddressWithDisplayName.match(emailAddress); |
| 36 | return match.hasMatch(); |
| 37 | } |
| 38 | |
| 39 | QObject *EmailValidator::factory(QQmlEngine *engine, QJSEngine *scriptEngine) |
| 40 | { |
| 41 | Q_UNUSED(scriptEngine); |
| 42 | EmailValidator *validator = new EmailValidator(reinterpret_cast<QObject*>(engine)); |
| 43 | return validator; |
| 44 | } |
| 45 | |
| 46 | static QWeakPointer<EmailValidator> validator; |
| 47 | QSharedPointer<EmailValidator> EmailValidator::instance() |
| 48 | { |
| 49 | QSharedPointer<EmailValidator> val = validator.toStrongRef(); |
| 50 | if (val.isNull()) { |
| 51 | val = QSharedPointer<EmailValidator>(new EmailValidator); |
| 52 | validator = val; |
| 53 | } |
| 54 | return validator; |
| 55 | } |