1 /* 2 SPDX-FileCopyrightText: 1998-2007 Sebastian Trueg <trueg@k3b.org> 3 SPDX-License-Identifier: GPL-2.0-or-later 4 */ 5 6 #ifndef _K3B_VALIDATORS_H_ 7 #define _K3B_VALIDATORS_H_ 8 9 #include "k3b_export.h" 10 #include <QValidator> 11 12 13 namespace K3b { 14 /** 15 * Simple validator that validates a string char by char 16 */ 17 class LIBK3B_EXPORT CharValidator : public QValidator 18 { 19 public: 20 explicit CharValidator( QObject* parent = 0 ); 21 22 virtual State validateChar( const QChar& ) const = 0; 23 24 State validate( QString& s, int& pos ) const override; 25 26 /** 27 * Replaces all invalid chars with the repplace char 28 */ 29 void fixup( QString& ) const override; 30 31 /** 32 * Default to '_' 33 */ setReplaceChar(const QChar & c)34 void setReplaceChar( const QChar& c ) { m_replaceChar = c; } 35 36 private: 37 QChar m_replaceChar; 38 }; 39 40 41 class LIBK3B_EXPORT Latin1Validator : public CharValidator 42 { 43 public: 44 explicit Latin1Validator( QObject* parent = 0 ); 45 46 State validateChar( const QChar& ) const override; 47 }; 48 49 50 class LIBK3B_EXPORT AsciiValidator : public Latin1Validator 51 { 52 public: 53 explicit AsciiValidator( QObject* parent = 0 ); 54 55 State validateChar( const QChar& ) const override; 56 }; 57 58 59 /** 60 * The Validator extends QRegExpValidator with a fixup method 61 * that just replaces all characters that are not allowed with the 62 * replace character. It only makes sense for QRegExps that simply 63 * allow or forbid some characters. 64 */ 65 class LIBK3B_EXPORT Validator : public QRegExpValidator 66 { 67 public: 68 explicit Validator( QObject* parent ); 69 Validator( const QRegExp& rx, QObject* parent ); 70 setReplaceChar(const QChar & s)71 void setReplaceChar( const QChar& s ) { m_replaceChar = s; } replaceChar()72 const QChar& replaceChar() const { return m_replaceChar; } 73 74 void fixup( QString& ) const override; 75 76 private: 77 QChar m_replaceChar; 78 }; 79 80 81 namespace Validators 82 { 83 /** 84 * just replaces all characters that are not allowed with the 85 * replace character. It only makes sense for QRegExps that simply 86 * allow or forbid some characters. 87 */ 88 LIBK3B_EXPORT QString fixup( const QString&, const QRegExp&, const QChar& replaceChar = '_' ); 89 90 /** 91 * Validates an ISRC code of the form "CCOOOYYSSSSS" where: 92 * <ul> 93 * <li>C: country code (upper case letters or digits)</li> 94 * <li>O: owner code (upper case letters or digits)</li> 95 * <li>Y: year (digits)</li> 96 * <li>S: serial number (digits)</li> 97 * </ul> 98 */ 99 LIBK3B_EXPORT Validator* isrcValidator( QObject* parent = 0 ); 100 101 /** 102 * This needs to be replaced by something better in the future... 103 * Even the name sucks! 104 */ 105 LIBK3B_EXPORT Validator* iso9660Validator( bool allowEmpty = true, QObject* parent = 0 ); 106 107 /** 108 * (1) d-characters are: A-Z, 0-9, _ (see ISO-9660:1988, Annex A, Table 15) 109 * (2) a-characters are: A-Z, 0-9, _, space, !, ", %, &, ', (, ), *, +, ,, -, ., /, :, ;, <, =, >, ? 110 * (see ISO-9660:1988, Annex A, Table 14) 111 */ 112 enum Iso646Type { 113 Iso646_a, 114 Iso646_d 115 }; 116 117 LIBK3B_EXPORT Validator* iso646Validator( int type = Iso646_a, 118 bool AllowLowerCase = false, 119 QObject* parent = 0 ); 120 } 121 } 122 123 #endif 124