KDEUI
kstringvalidator.cpp
Go to the documentation of this file.00001 /* 00002 Copyright (c) 2001 Marc Mutz <mutz@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; version 2.0 00007 of the License. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301 USA 00018 */ 00019 00020 #include "kstringvalidator.h" 00021 00022 #include <kdebug.h> 00023 00024 class KStringListValidator::Private 00025 { 00026 public: 00027 QStringList mStringList; 00028 bool mRejecting : 1; 00029 bool mFixupEnabled : 1; 00030 }; 00031 00032 // 00033 // KStringListValidator 00034 // 00035 KStringListValidator::KStringListValidator( const QStringList &list, bool rejecting, 00036 bool fixupEnabled, QObject *parent ) 00037 : QValidator( parent ), 00038 d( new Private ) 00039 { 00040 d->mStringList = list; 00041 d->mRejecting = rejecting; 00042 d->mFixupEnabled = fixupEnabled; 00043 } 00044 00045 KStringListValidator::~KStringListValidator() 00046 { 00047 delete d; 00048 } 00049 00050 QValidator::State KStringListValidator::validate( QString & input, int& ) const 00051 { 00052 if ( input.isEmpty() ) 00053 return Intermediate; 00054 00055 if ( isRejecting() ) // anything not in mStringList is acceptable: 00056 if ( !d->mStringList.contains( input ) ) 00057 return Acceptable; 00058 else 00059 return Intermediate; 00060 else // only what is in mStringList is acceptable: 00061 if ( d->mStringList.contains( input ) ) 00062 return Acceptable; 00063 else 00064 for ( QStringList::ConstIterator it = d->mStringList.constBegin(); it != d->mStringList.constEnd() ; ++it ) 00065 if ( (*it).startsWith( input ) || input.startsWith( *it ) ) 00066 return Intermediate; 00067 00068 return Invalid; 00069 } 00070 00071 void KStringListValidator::fixup( QString& ) const 00072 { 00073 if ( !isFixupEnabled() ) 00074 return; 00075 00076 // warn (but only once!) about non-implemented fixup(): 00077 static bool warn = true; 00078 if ( warn ) { 00079 kDebug() << "KStringListValidator::fixup() isn't yet implemented!"; 00080 warn = false; 00081 } 00082 } 00083 00084 void KStringListValidator::setRejecting( bool rejecting ) 00085 { 00086 d->mRejecting = rejecting; 00087 } 00088 00089 bool KStringListValidator::isRejecting() const 00090 { 00091 return d->mRejecting; 00092 } 00093 00094 void KStringListValidator::setFixupEnabled( bool fixupEnabled ) 00095 { 00096 d->mFixupEnabled = fixupEnabled; 00097 } 00098 00099 bool KStringListValidator::isFixupEnabled() const 00100 { 00101 return d->mFixupEnabled; 00102 } 00103 00104 void KStringListValidator::setStringList( const QStringList &list ) 00105 { 00106 d->mStringList = list; 00107 } 00108 00109 QStringList KStringListValidator::stringList() const 00110 { 00111 return d->mStringList; 00112 } 00113 00114 // 00115 // KMimeTypeValidator 00116 // 00117 00118 #define ALLOWED_CHARS "!#-'*+.0-9^-~+-" 00119 00120 class KMimeTypeValidator::Private 00121 { 00122 }; 00123 00124 KMimeTypeValidator::KMimeTypeValidator( QObject* parent ) 00125 : QValidator( parent ), 00126 d( 0 ) 00127 { 00128 } 00129 00130 KMimeTypeValidator::~KMimeTypeValidator() 00131 { 00132 delete d; 00133 } 00134 00135 QValidator::State KMimeTypeValidator::validate( QString &input, int& ) const 00136 { 00137 if ( input.isEmpty() ) 00138 return Intermediate; 00139 00140 QRegExp acceptable( "[" ALLOWED_CHARS "]+/[" ALLOWED_CHARS "]+", Qt::CaseInsensitive ); 00141 if ( acceptable.exactMatch( input ) ) 00142 return Acceptable; 00143 00144 QRegExp intermediate( "[" ALLOWED_CHARS "]*/?[" ALLOWED_CHARS "]*", Qt::CaseInsensitive ); 00145 if ( intermediate.exactMatch( input ) ) 00146 return Intermediate; 00147 00148 return Invalid; 00149 } 00150 00151 void KMimeTypeValidator::fixup( QString &input ) const 00152 { 00153 QRegExp invalidChars("[^/" ALLOWED_CHARS "]+"); 00154 input.remove( invalidChars ); 00155 } 00156 00157 #include "kstringvalidator.moc"