1 /*
2 * Copyright 2013-2015 Christian Dávid <christian-david@web.de>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "ibanvalidator.h"
19
20 #include "payeeidentifier/ibanbic/ibanbic.h"
21 #include <KLocalizedString>
22
23 #include "widgetenums.h"
24
ibanValidator(QObject * parent)25 ibanValidator::ibanValidator(QObject* parent)
26 : QValidator(parent)
27 {
28
29 }
30
validate(QString & string,int &) const31 QValidator::State ibanValidator::validate(QString& string, int&) const
32 {
33 // Check country code and set it uppercase
34 if (string.length() >= 1) {
35 if (!string.at(0).isLetter())
36 return Invalid;
37 if (string.at(0).isLower())
38 string[0] = string.at(0).toUpper();
39 }
40
41 if (string.length() >= 2) {
42 if (!string.at(1).isLetterOrNumber())
43 return Invalid;
44 if (string.at(1).isLower())
45 string[1] = string.at(1).toUpper();
46 }
47
48 // Check rest of the iban
49 int characterCount = qMin(string.length(), 2);
50 for (int i = 2; i < string.length(); ++i) {
51 if (string.at(i).isLetterOrNumber()) {
52 ++characterCount;
53 } else if (!string.at(i).isSpace()) {
54 return Invalid;
55 }
56 }
57
58 if (characterCount > 32)
59 return Invalid;
60
61 if (characterCount > 5) {
62 return Acceptable;
63 }
64
65 return Intermediate;
66 }
67
validateWithMessage(const QString & string)68 QPair< eWidgets::ValidationFeedback::MessageType, QString > ibanValidator::validateWithMessage(const QString& string)
69 {
70 // string.length() > 32 should not happen because all line edits should have this validator installed
71 if (string.length() < 5)
72 return QPair< eWidgets::ValidationFeedback::MessageType, QString >(eWidgets::ValidationFeedback::MessageType::Error, i18n("This IBAN is too short."));
73
74 if (!payeeIdentifiers::ibanBic::validateIbanChecksum(payeeIdentifiers::ibanBic::ibanToElectronic(string)))
75 return QPair< eWidgets::ValidationFeedback::MessageType, QString >(eWidgets::ValidationFeedback::MessageType::Warning, i18n("This IBAN is invalid."));
76
77 return QPair< eWidgets::ValidationFeedback::MessageType, QString >(eWidgets::ValidationFeedback::MessageType::None, QString());
78 }
79
fixup(QString & string) const80 void ibanValidator::fixup(QString& string) const
81 {
82 string = payeeIdentifiers::ibanBic::ibanToPaperformat(string);
83 }
84