summaryrefslogtreecommitdiffstats
path: root/kdeui/kstringvalidator.cpp
blob: f783a4b9f39ecadfc5fba0fb1f877d101f0661d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
    kstringvalidator.cpp

    Copyright (c) 2001 Marc Mutz <mutz@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; version 2.0
    of the License.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301 USA
*/

#include "kstringvalidator.h"
#include "kdebug.h"

//
// KStringListValidator
//

TQValidator::State KStringListValidator::validate( TQString & input, int& ) const {
  if ( input.isEmpty() ) return Intermediate;

  if ( isRejecting() ) // anything not in mStringList is acceptable:
    if ( mStringList.find( input ) == mStringList.end() )
      return Acceptable;
    else
      return Intermediate;
  else // only what is in mStringList is acceptable:
    if ( mStringList.find( input ) != mStringList.end() )
      return Acceptable;
    else
      for ( TQStringList::ConstIterator it = mStringList.begin() ;
	    it != mStringList.end() ; ++it )
	if ( (*it).startsWith( input ) || input.startsWith( *it ) )
	  return Intermediate;

  return Invalid;
}

void KStringListValidator::fixup( TQString & /* input */ ) const {
  if ( !isFixupEnabled() ) return;
  // warn (but only once!) about non-implemented fixup():
  static bool warn = true;
  if ( warn ) {
    kdDebug() << "KStringListValidator::fixup() isn't yet implemented!"
	      << endl;
    warn = false;
  }
}

//
// KMimeTypeValidator
//

#define ALLOWED_CHARS "!#-'*+.0-9^-~+-"

TQValidator::State KMimeTypeValidator::validate( TQString & input, int& ) const
{
  if ( input.isEmpty() )
    return Intermediate;

  TQRegExp acceptable( "[" ALLOWED_CHARS "]+/[" ALLOWED_CHARS "]+",
		      false /*case-insens.*/);
  if ( acceptable.exactMatch( input ) )
    return Acceptable;

  TQRegExp intermediate( "[" ALLOWED_CHARS "]*/?[" ALLOWED_CHARS "]*",
			false /*case-insensitive*/);
  if ( intermediate.exactMatch( input ) )
    return Intermediate;

  return Invalid;
}

void KMimeTypeValidator::fixup( TQString & input ) const
{
  TQRegExp invalidChars("[^/" ALLOWED_CHARS "]+");
  input.replace( invalidChars, TQString::null);
}

#include "kstringvalidator.moc"