File indexing completed on 2024-06-16 04:46:58

0001 /*
0002     SPDX-FileCopyrightText: 2013-2015 Christian Dávid <christian-david@web.de>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "bicvalidator.h"
0007 
0008 #include <KLocalizedString>
0009 
0010 #include "payeeidentifier/ibanbic/ibanbic.h"
0011 #include "widgetenums.h"
0012 
0013 bicValidator::bicValidator(QObject* parent)
0014     : QValidator(parent)
0015 {
0016 }
0017 
0018 QValidator::State bicValidator::validate(QString &string, int&) const
0019 {
0020     for (int i = 0; i < qMin(string.length(), 6); ++i) {
0021         if (!string.at(i).isLetter())
0022             return Invalid;
0023         if (string.at(i).isLower())
0024             string[i] = string.at(i).toUpper();
0025     }
0026 
0027     for (int i = 6; i < string.length(); ++i) {
0028         if (!string.at(i).isLetterOrNumber())
0029             return Invalid;
0030         if (string.at(i).isLower())
0031             string[i] = string.at(i).toUpper();
0032     }
0033 
0034     if (string.length() > 11)
0035         return Invalid;
0036     else if (string.length() == 8 || string.length() == 11) {
0037         return Acceptable;
0038     }
0039     return Intermediate;
0040 }
0041 
0042 QPair< eWidgets::ValidationFeedback::MessageType, QString > bicValidator::validateWithMessage(const QString& string)
0043 {
0044     // Do not show an error message if no BIC is given.
0045     if (string.length() != 8 && string.length() != 11)
0046         return QPair< eWidgets::ValidationFeedback::MessageType, QString >(eWidgets::ValidationFeedback::MessageType::Error, i18n("A valid BIC is 8 or 11 characters long."));
0047 
0048     if (payeeIdentifiers::ibanBic::isBicAllocated(string) == payeeIdentifiers::ibanBic::bicNotAllocated)
0049         return QPair< eWidgets::ValidationFeedback::MessageType, QString >(eWidgets::ValidationFeedback::MessageType::Error, i18n("The given BIC is not assigned to any credit institute."));
0050 
0051     return QPair< eWidgets::ValidationFeedback::MessageType, QString >(eWidgets::ValidationFeedback::MessageType::None, QString());
0052 
0053 }