File indexing completed on 2024-04-28 04:49:53

0001 /*
0002     SPDX-FileCopyrightText: 1998-2007 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef _K3B_VALIDATORS_H_
0007 #define _K3B_VALIDATORS_H_
0008 
0009 #include "k3b_export.h"
0010 #include <QValidator>
0011 
0012 
0013 namespace K3b {
0014     /**
0015      * Simple validator that validates a string char by char
0016      */
0017     class LIBK3B_EXPORT CharValidator : public QValidator
0018     {
0019     public:
0020         explicit CharValidator( QObject* parent = 0 );
0021 
0022         virtual State validateChar( const QChar& ) const = 0;
0023 
0024         State validate( QString& s, int& pos ) const override;
0025 
0026         /**
0027          * Replaces all invalid chars with the repplace char
0028          */
0029         void fixup( QString& ) const override;
0030 
0031         /**
0032          * Default to '_'
0033          */
0034         void setReplaceChar( const QChar& c ) { m_replaceChar = c; }
0035 
0036     private:
0037         QChar m_replaceChar;
0038     };
0039 
0040 
0041     class LIBK3B_EXPORT Latin1Validator : public CharValidator
0042     {
0043     public:
0044         explicit Latin1Validator( QObject* parent = 0 );
0045 
0046         State validateChar( const QChar& ) const override;
0047     };
0048 
0049 
0050     class LIBK3B_EXPORT AsciiValidator : public Latin1Validator
0051     {
0052     public:
0053         explicit AsciiValidator( QObject* parent = 0 );
0054 
0055         State validateChar( const QChar& ) const override;
0056     };
0057 
0058 
0059     /**
0060      * The Validator extends QRegExpValidator with a fixup method
0061      * that just replaces all characters that are not allowed with the
0062      * replace character. It only makes sense for QRegExps that simply
0063      * allow or forbid some characters.
0064      */
0065     class LIBK3B_EXPORT Validator : public QRegularExpressionValidator
0066     {
0067     public:
0068         explicit Validator( QObject* parent );
0069         Validator( const QRegularExpression& rx, QObject* parent );
0070 
0071         void setReplaceChar( const QChar& s ) { m_replaceChar = s; }
0072         const QChar& replaceChar() const { return m_replaceChar; }
0073 
0074         void fixup( QString& ) const override;
0075 
0076     private:
0077         QChar m_replaceChar;
0078     };
0079 
0080 
0081     namespace Validators
0082     {
0083         /**
0084          * just replaces all characters that are not allowed with the
0085          * replace character. It only makes sense for QRegExps that simply
0086          * allow or forbid some characters.
0087          */
0088         LIBK3B_EXPORT QString fixup( const QString&, const QRegularExpression&, const QChar& replaceChar = '_' );
0089 
0090         /**
0091          * Validates an ISRC code of the form "CCOOOYYSSSSS" where:
0092          * <ul>
0093          * <li>C: country code (upper case letters or digits)</li>
0094          * <li>O: owner code (upper case letters or digits)</li>
0095          * <li>Y: year (digits)</li>
0096          * <li>S: serial number (digits)</li>
0097          * </ul>
0098          */
0099         LIBK3B_EXPORT Validator* isrcValidator( QObject* parent = 0 );
0100 
0101         /**
0102          * This needs to be replaced by something better in the future...
0103          * Even the name sucks!
0104          */
0105         LIBK3B_EXPORT Validator* iso9660Validator( bool allowEmpty = true, QObject* parent = 0 );
0106 
0107         /**
0108          * (1) d-characters are: A-Z, 0-9, _ (see ISO-9660:1988, Annex A, Table 15)
0109          * (2) a-characters are: A-Z, 0-9, _, space, !, ", %, &, ', (, ), *, +, ,, -, ., /, :, ;, <, =, >, ?
0110          * (see ISO-9660:1988, Annex A, Table 14)
0111          */
0112         enum Iso646Type {
0113             Iso646_a,
0114             Iso646_d
0115         };
0116 
0117         LIBK3B_EXPORT Validator* iso646Validator( int type = Iso646_a,
0118                                                   bool AllowLowerCase = false,
0119                                                   QObject* parent = 0 );
0120     }
0121 }
0122 
0123 #endif