• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kleopatra

  • sources
  • kde-4.12
  • kdepim
  • kleopatra
  • utils
validation.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  utils/validation.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2008 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "validation.h"
36 
37 #include <utils/multivalidator.h>
38 
39 #include <KDebug>
40 
41 #include <QRegExp>
42 #include <QUrl>
43 
44 #include <cassert>
45 
46 using namespace Kleo;
47 
48 static const QString email_rx = QLatin1String("[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?");
49 // these are modeled after gnupg/g10/keygen.c:ask_user_id:
50 static const QString name_rx = QLatin1String("[^0-9<>][^<>@]{4,}");
51 static const QString comment_rx = QLatin1String("[^()]*");
52 
53 namespace {
54 
55  class EMailValidator : public QValidator {
56  QRegExp rx;
57  public:
58  explicit EMailValidator( QObject * parent=0 ) : QValidator( parent ), rx( QRegExp( email_rx ) ) {}
59 
60  /* reimp */ void fixup( QString & ) const {}
61 
62  /* reimp */ State validate( QString & str, int & pos ) const {
63  const int atIdx = str.lastIndexOf( QLatin1Char('@') );
64  if ( atIdx < 0 || str.endsWith( QLatin1Char('@') ) )
65  return regexValidate( str, pos );
66 
67  // toAce/fromAce doesn't like intermediate domain names,
68  // so we fix them up with something innocuous to help it
69  // along, and which we strip again afterwards
70 
71  QString domain = str.mid( atIdx + 1 ).toLower();
72  const int dotIndex = domain.lastIndexOf( QLatin1Char('.') );
73  const bool needsOrgAdded = domain.endsWith( QLatin1Char('.') );
74  // during typing, the domain might end with '-', which is okay
75  // yeah, foo.s also disrupts fromAce, during typing this is okay
76  const bool needsDotOrgAdded = !needsOrgAdded && ( dotIndex < 0 || dotIndex == domain.size() - 2 || domain.endsWith( QLatin1Char('-') ) );
77  if ( needsOrgAdded )
78  domain += QLatin1String("org");
79  if ( needsDotOrgAdded )
80  domain += QLatin1String("tmp.org");
81  const QByteArray domainEncoded = QUrl::toAce( domain );
82  const QString domainRestored = QUrl::fromAce( domainEncoded );
83  QString encoded = str.left( atIdx ) + QLatin1Char('@') + QString::fromLatin1( domainEncoded );
84  if ( needsDotOrgAdded ) {
85  assert( encoded.endsWith( QLatin1String( "tmp.org" ) ) );
86  encoded.chop( 7 );
87  }
88  if ( needsOrgAdded ) {
89  assert( encoded.endsWith( QLatin1String( ".org" ) ) );
90  encoded.chop( 3 ); // '.' was part of domain before
91  }
92  kDebug() << "\n str :" << str
93  << "\n domain :" << domain
94  << "\n domainEncoded :" << domainEncoded
95  << "\n domainRestored:" << domainRestored
96  << "\n encoded :" << encoded ;
97  if ( domain != domainRestored )
98  return Invalid;
99 
100  // there's no difference between 'encoded' and 'str' at
101  // least up to and including 'atIdx', and we need the
102  // position for the fixed Intermediate state in
103  // regexValidate (e.g. adding a . after marc in
104  // marc@kdab.com, intending to eventually arrive at
105  // marc.mutz@kdab.com)
106  int adjustedPos = pos <= atIdx ? pos : encoded.size() ;
107  return regexValidate( encoded, adjustedPos );
108  }
109 
110  private:
111  State regexValidate( QString & input, int & pos ) const {
112  // fixed version of QRegExpValidator::validate():
113  if (rx.exactMatch(input)) {
114  return Acceptable;
115  } else {
116  if (const_cast<QRegExp &>(rx).matchedLength() >= /*input.size()*/pos) {
117  return Intermediate;
118  } else {
119  pos = input.size();
120  return Invalid;
121  }
122  }
123  }
124 
125  };
126 
127 }
128 
129 QValidator * Validation::email( QObject * parent ) {
130  return new EMailValidator( parent );
131 }
132 
133 QValidator * Validation::email( const QRegExp & addRX, QObject * parent ) {
134  return new MultiValidator( email(), new QRegExpValidator( addRX, 0 ), parent );
135 }
136 
137 QValidator * Validation::pgpName( QObject * parent ) {
138  return new QRegExpValidator( QRegExp( name_rx ), parent );
139 }
140 
141 QValidator * Validation::pgpName( const QRegExp & addRX, QObject * parent ) {
142  return new MultiValidator( pgpName(), new QRegExpValidator( addRX, 0 ), parent );
143 }
144 
145 QValidator * Validation::pgpComment( QObject * parent ) {
146  return new QRegExpValidator( QRegExp( comment_rx ), parent );
147 }
148 
149 QValidator * Validation::pgpComment( const QRegExp & addRX, QObject * parent ) {
150  return new MultiValidator( pgpComment(), new QRegExpValidator( addRX, 0 ), parent );
151 }
152 
name_rx
static const QString name_rx
Definition: validation.cpp:50
Kleo::Validation::email
QValidator * email(QObject *parent=0)
Definition: validation.cpp:129
multivalidator.h
email
static std::string email(const UserID &uid)
Definition: keycache.cpp:593
validation.h
email_rx
static const QString email_rx
Definition: validation.cpp:48
comment_rx
static const QString comment_rx
Definition: validation.cpp:51
State
State
Definition: setinitialpindialog.cpp:54
QValidator
Kleo::Validation::pgpComment
QValidator * pgpComment(QObject *parent=0)
Definition: validation.cpp:145
Kleo::MultiValidator
Definition: multivalidator.h:41
Kleo::Validation::pgpName
QValidator * pgpName(QObject *parent=0)
Definition: validation.cpp:137
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:42 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal