File indexing completed on 2024-04-21 15:30:05

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003-2005 Jarosław Staniek <staniek@kde.org>
0003    Copyright (C) 2005 Martin Ellis <martin.ellis@kdemail.net>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This program is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this program; see the file COPYING.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "KDbIdentifierValidator.h"
0022 #include "KDb.h"
0023 
0024 class Q_DECL_HIDDEN KDbIdentifierValidator::Private
0025 {
0026 public:
0027     Private() : isLowerCaseForced(false) {}
0028     bool isLowerCaseForced;
0029 private:
0030     Q_DISABLE_COPY(Private)
0031 };
0032 
0033 KDbIdentifierValidator::KDbIdentifierValidator(QObject * parent)
0034         : KDbValidator(parent), d(new Private)
0035 {
0036 }
0037 
0038 KDbIdentifierValidator::~KDbIdentifierValidator()
0039 {
0040     delete d;
0041 }
0042 
0043 QValidator::State KDbIdentifierValidator::validate(QString& input, int& pos) const
0044 {
0045     int i;
0046     for (i = 0; i < input.length() && input.at(i) == QLatin1Char(' '); i++)
0047         ;
0048     pos -= i; //i chars will be removed from beginning
0049     if (i < input.length() && input.at(i) >= QLatin1Char('0') && input.at(i) <= QLatin1Char('9'))
0050         pos++; //_ will be added at the beginning
0051     bool addspace = (input.right(1) == QLatin1String(" "));
0052     input = d->isLowerCaseForced ? KDb::stringToIdentifier(input).toLower() : KDb::stringToIdentifier(input);
0053     if (addspace)
0054         input += QLatin1Char('_');
0055     if (pos > input.length())
0056         pos = input.length();
0057     return (input.isEmpty() && !acceptsEmptyValue())
0058             ? Intermediate
0059             : Acceptable;
0060 }
0061 
0062 KDbValidator::Result KDbIdentifierValidator::internalCheck(
0063     const QString &valueName, const QVariant& value,
0064     QString *message, QString *details)
0065 {
0066     Q_UNUSED(details);
0067     if (KDb::isIdentifier(value.toString()))
0068         return KDbValidator::Ok;
0069     if (message) {
0070         *message = KDb::identifierExpectedMessage(valueName, value);
0071     }
0072     return KDbValidator::Error;
0073 }
0074 
0075 bool KDbIdentifierValidator::isLowerCaseForced() const
0076 {
0077     return d->isLowerCaseForced;
0078 }
0079 
0080 void KDbIdentifierValidator::setLowerCaseForced(bool set)
0081 {
0082     d->isLowerCaseForced = set;
0083 }