File indexing completed on 2025-02-23 04:34:25

0001 /**
0002  * \file tracknumbervalidator.cpp
0003  * Validator for track and disc numbers with optional total.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 24 May 2014
0008  *
0009  * Copyright (C) 2014-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "tracknumbervalidator.h"
0028 
0029 /**
0030  * Constructor.
0031  * @param parent parent object
0032  */
0033 TrackNumberValidator::TrackNumberValidator(QObject *parent) : QValidator(parent)
0034 {
0035 }
0036 
0037 /**
0038  * Validate input string.
0039  * @param input input string
0040  * @return current state of input (Invalid, Intermediate or Acceptable).
0041  */
0042 QValidator::State TrackNumberValidator::validate(QString& input, int&) const
0043 {
0044   for (auto it = input.constBegin(); it != input.constEnd(); ++it) {
0045     if (!it->isDigit() && *it != QLatin1Char('/')) {
0046       return Invalid;
0047     }
0048   }
0049 
0050   int len = input.length();
0051   if (len == 0)
0052     return Acceptable;
0053 
0054   bool ok;
0055   if (int slashPos = input.indexOf(QLatin1Char('/')); slashPos == -1) {
0056     input.toULongLong(&ok);
0057     return ok ? Acceptable : Invalid;
0058   } else {
0059     if (slashPos == len - 1) {
0060       return Intermediate;
0061     }
0062     if (input.indexOf(QLatin1Char('/'), slashPos + 1) != -1) {
0063       return Invalid;
0064     }
0065     if (slashPos == 0) {
0066       return Intermediate;
0067     }
0068 
0069 #if QT_VERSION >= 0x060000
0070     input.left(slashPos).toULongLong(&ok);
0071     if (ok) {
0072       input.mid(slashPos + 1).toULongLong(&ok);
0073     }
0074 #else
0075     input.leftRef(slashPos).toULongLong(&ok);
0076     if (ok) {
0077       input.midRef(slashPos + 1).toULongLong(&ok);
0078     }
0079 #endif
0080   }
0081   return ok ? Acceptable : Invalid;
0082 }
0083 
0084 /**
0085  * Attempt to change @a input to be valid.
0086  * @param input input string
0087  */
0088 void TrackNumberValidator::fixup(QString& input) const
0089 {
0090   if (int len = input.length(); len > 0) {
0091     if (input.at(0) == QLatin1Char('/')) {
0092       input = input.mid(1);
0093     } else if (input.at(len - 1) == QLatin1Char('/')) {
0094       input.truncate(len - 1);
0095     }
0096   }
0097 }