• 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
  • dialogs
adduseriddialog.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  dialogs/adduseriddialog.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 "adduseriddialog.h"
36 
37 #include "ui_adduseriddialog.h"
38 
39 #include <utils/validation.h>
40 
41 #include <kleo/stl_util.h>
42 
43 #include <QString>
44 #include <QStringList>
45 #include <QPushButton>
46 #include <QValidator>
47 
48 #include <KConfigGroup>
49 #include <KGlobal>
50 #include <KLocalizedString>
51 #include <KDebug>
52 
53 #include <cassert>
54 
55 using namespace Kleo;
56 using namespace Kleo::Dialogs;
57 
58 namespace {
59  struct Line {
60  QString attr;
61  QString label;
62  QString regex;
63  QLineEdit * edit;
64  };
65 }
66 
67 static QString pgpLabel( const QString & attr ) {
68  if ( attr == QLatin1String("NAME") )
69  return i18n("Name");
70  if ( attr == QLatin1String("COMMENT") )
71  return i18n("Comment");
72  if ( attr == QLatin1String("EMAIL") )
73  return i18n("EMail");
74  return QString();
75 }
76 
77 static QString attributeLabel( const QString & attr, bool pgp ) {
78  if ( attr.isEmpty() )
79  return QString();
80  const QString label = /*pgp ?*/ pgpLabel( attr ) /*: Kleo::DNAttributeMapper::instance()->name2label( attr )*/ ;
81  if ( !label.isEmpty() )
82  if ( pgp )
83  return label;
84  else
85  return i18nc("Format string for the labels in the \"Your Personal Data\" page",
86  "%1 (%2)", label, attr );
87  else
88  return attr;
89 }
90 
91 static QString attributeFromKey( QString key ) {
92  return key.remove( QLatin1Char('!') );
93 }
94 
95 static int row_index_of( QWidget * w, QGridLayout * l ) {
96  const int idx = l->indexOf( w );
97  int r, c, rs, cs;
98  l->getItemPosition( idx, &r, &c, &rs, &cs );
99  return r;
100 }
101 
102 static QLineEdit * adjust_row( QGridLayout * l, int row, const QString & label, const QString & preset, QValidator * validator, bool readonly, bool required ) {
103  assert( l );
104  assert( row >= 0 );
105  assert( row < l->rowCount() );
106 
107  QLabel * lb = qobject_cast<QLabel*>( l->itemAtPosition( row, 0 )->widget() );
108  assert( lb );
109  QLineEdit * le = qobject_cast<QLineEdit*>( l->itemAtPosition( row, 1 )->widget() );
110  assert( le );
111  QLabel * reqLB = qobject_cast<QLabel*>( l->itemAtPosition( row, 2 )->widget() );
112  assert( reqLB );
113 
114  lb->setText( i18nc("interpunctation for labels", "%1:", label ) );
115  le->setText( preset );
116  reqLB->setText( required ? i18n("(required)") : i18n("(optional)") );
117  delete le->validator();
118  if ( validator ) {
119  if ( !validator->parent() )
120  validator->setParent( le );
121  le->setValidator( validator );
122  }
123 
124  le->setReadOnly( readonly && le->hasAcceptableInput() );
125 
126  lb->show();
127  le->show();
128  reqLB->show();
129 
130  return le;
131 }
132 
133 class AddUserIDDialog::Private {
134  friend class ::Kleo::Dialogs::AddUserIDDialog;
135  AddUserIDDialog * const q;
136 public:
137  explicit Private( AddUserIDDialog * qq )
138  : q( qq ),
139  ui( q )
140  {
141 
142  }
143 
144 private:
145  void slotUserIDChanged();
146 
147 private:
148  bool isComplete() const;
149 
150 private:
151  struct UI : public Ui_AddUserIDDialog {
152 
153  QVector<Line> lineList;
154 
155  explicit UI( AddUserIDDialog * qq )
156  : Ui_AddUserIDDialog()
157  {
158  setupUi( qq );
159 
160  // ### this code is mostly the same as the one in
161  // ### newcertificatewizard. Find some time to factor them
162  // ### into a single copy.
163 
164  // hide the stuff
165  nameLB->hide();
166  nameLE->hide();
167  nameRequiredLB->hide();
168 
169  emailLB->hide();
170  emailLE->hide();
171  emailRequiredLB->hide();
172 
173  commentLB->hide();
174  commentLE->hide();
175  commentRequiredLB->hide();
176 
177  // set errorLB to have a fixed height of two lines:
178  errorLB->setText( QLatin1String("2<br>1") );
179  errorLB->setFixedHeight( errorLB->minimumSizeHint().height() );
180  errorLB->clear();
181 
182  const KConfigGroup config( KGlobal::config(), "CertificateCreationWizard" );
183  const QStringList attrOrder = config.readEntry( "OpenPGPAttributeOrder",
184  QStringList() << QLatin1String("NAME!") << QLatin1String("EMAIL!") << QLatin1String("COMMENT") );
185 
186  QMap<int,Line> lines;
187 
188  Q_FOREACH( const QString & rawKey, attrOrder ) {
189  const QString key = rawKey.trimmed().toUpper();
190  const QString attr = attributeFromKey( key );
191  if ( attr.isEmpty() )
192  continue;
193  const QString preset = config.readEntry( attr );
194  const bool required = key.endsWith( QLatin1Char('!') );
195  const bool readonly = config.isEntryImmutable( attr );
196  const QString label = config.readEntry( attr + QLatin1String("_label"),
197  attributeLabel( attr, true ) );
198  const QString regex = config.readEntry( attr + QLatin1String("_regex") );
199 
200  int row;
201  QValidator * validator = 0;
202  if ( attr == QLatin1String("EMAIL") ) {
203  validator = regex.isEmpty() ? Validation::email() : Validation::email( QRegExp( regex ) ) ;
204  row = row_index_of( emailLE, gridLayout );
205  } else if ( attr == QLatin1String("NAME") ) {
206  validator = regex.isEmpty() ? Validation::pgpName() : Validation::pgpName( QRegExp( regex ) ) ;
207  row = row_index_of( nameLE, gridLayout );
208  } else if ( attr == QLatin1String("COMMENT") ) {
209  validator = regex.isEmpty() ? Validation::pgpComment() : Validation::pgpComment( QRegExp( regex ) ) ;
210  row = row_index_of( commentLE, gridLayout );
211  } else {
212  continue;
213  }
214 
215  QLineEdit * le = adjust_row( gridLayout, row, label, preset, validator, readonly, required );
216 
217  const Line line = { key, label, regex, le };
218  lines[row] = line;
219  }
220 
221  lineList = kdtools::copy< QVector<Line> >( lines );
222  }
223 
224  QPushButton * okPB() const {
225  return buttonBox->button( QDialogButtonBox::Ok );
226  }
227  } ui;
228 };
229 
230 AddUserIDDialog::AddUserIDDialog( QWidget * p, Qt::WindowFlags f )
231  : QDialog( p, f ), d( new Private( this ) )
232 {
233 
234 }
235 
236 AddUserIDDialog::~AddUserIDDialog() {}
237 
238 
239 void AddUserIDDialog::setName( const QString & name ) {
240  d->ui.nameLE->setText( name );
241 }
242 
243 QString AddUserIDDialog::name() const {
244  return d->ui.nameLE->text().trimmed();
245 }
246 
247 void AddUserIDDialog::setEmail( const QString & email ) {
248  d->ui.emailLE->setText( email );
249 }
250 
251 QString AddUserIDDialog::email() const {
252  return d->ui.emailLE->text().trimmed();
253 }
254 
255 void AddUserIDDialog::setComment( const QString & comment ) {
256  d->ui.commentLE->setText( comment );
257 }
258 
259 QString AddUserIDDialog::comment() const {
260  return d->ui.commentLE->text().trimmed();
261 }
262 
263 static bool has_intermediate_input( const QLineEdit * le ) {
264  QString text = le->text();
265  int pos = le->cursorPosition();
266  const QValidator * const v = le->validator();
267  return !v || v->validate( text, pos ) == QValidator::Intermediate ;
268 }
269 
270 static bool requirementsAreMet( const QVector<Line> & list, QString & error ) {
271  Q_FOREACH( const Line & line, list ) {
272  const QLineEdit * le = line.edit;
273  if ( !le )
274  continue;
275  const QString key = line.attr;
276  kDebug() << "requirementsAreMet(): checking \"" << key << "\" against \"" << le->text() << "\":";
277  if ( le->text().trimmed().isEmpty() ) {
278  if ( key.endsWith(QLatin1Char('!')) ) {
279  if ( line.regex.isEmpty() )
280  error = i18nc("@info","<interface>%1</interface> is required, but empty.", line.label );
281  else
282  error = i18nc("@info","<interface>%1</interface> is required, but empty.<nl/>"
283  "Local Admin rule: <icode>%2</icode>", line.label, line.regex );
284  return false;
285  }
286  } else if ( has_intermediate_input( le ) ) {
287  if ( line.regex.isEmpty() )
288  error = i18nc("@info","<interface>%1</interface> is incomplete.", line.label );
289  else
290  error = i18nc("@info","<interface>%1</interface> is incomplete.<nl/>"
291  "Local Admin rule: <icode>%2</icode>", line.label, line.regex );
292  return false;
293  } else if ( !le->hasAcceptableInput() ) {
294  if ( line.regex.isEmpty() )
295  error = i18nc("@info","<interface>%1</interface> is invalid.", line.label );
296  else
297  error = i18nc("@info","<interface>%1</interface> is invalid.<nl/>"
298  "Local Admin rule: <icode>%2</icode>", line.label, line.regex );
299  return false;
300  }
301  kDebug() << "ok" << endl;
302  }
303  return true;
304 }
305 
306 bool AddUserIDDialog::Private::isComplete() const {
307  QString error;
308  const bool ok = requirementsAreMet( ui.lineList, error );
309  ui.errorLB->setText( error );
310  return ok;
311 }
312 
313 void AddUserIDDialog::Private::slotUserIDChanged() {
314 
315  ui.okPB()->setEnabled( isComplete() );
316 
317  const QString name = q->name();
318  const QString email = q->email();
319  const QString comment = q->comment();
320 
321  QStringList parts;
322  if ( !name.isEmpty() )
323  parts.push_back( name );
324  if ( !comment.isEmpty() )
325  parts.push_back( QLatin1Char( '(' ) + comment + QLatin1Char( ')' ) );
326  if ( !email.isEmpty() )
327  parts.push_back( QLatin1Char( '<' ) + email + QLatin1Char( '>' ) );
328 
329  ui.resultLB->setText( parts.join( QLatin1String( " " ) ) );
330 }
331 
332 #include "moc_adduseriddialog.cpp"
adjust_row
static QLineEdit * adjust_row(QGridLayout *l, int row, const QString &label, const QString &preset, QValidator *validator, bool readonly, bool required)
Definition: adduseriddialog.cpp:102
Kleo::Validation::email
QValidator * email(QObject *parent=0)
Definition: validation.cpp:129
has_intermediate_input
static bool has_intermediate_input(const QLineEdit *le)
Definition: adduseriddialog.cpp:263
row_index_of
static int row_index_of(QWidget *w, QGridLayout *l)
Definition: adduseriddialog.cpp:95
QDialog
pgpLabel
static QString pgpLabel(const QString &attr)
Definition: adduseriddialog.cpp:67
email
static std::string email(const UserID &uid)
Definition: keycache.cpp:593
validation.h
QWidget
d
#define d
Definition: adduseridcommand.cpp:90
QValidator
attributeLabel
static QString attributeLabel(const QString &attr, bool pgp)
Definition: adduseriddialog.cpp:77
Kleo::Validation::pgpComment
QValidator * pgpComment(QObject *parent=0)
Definition: validation.cpp:145
Kleo::Dialogs::AddUserIDDialog::setName
void setName(const QString &name)
Definition: adduseriddialog.cpp:239
Kleo::Dialogs::AddUserIDDialog::AddUserIDDialog
AddUserIDDialog(QWidget *parent=0, Qt::WindowFlags f=0)
Definition: adduseriddialog.cpp:230
Kleo::Dialogs::AddUserIDDialog::setEmail
void setEmail(const QString &email)
Definition: adduseriddialog.cpp:247
Kleo::Dialogs::AddUserIDDialog::name
QString name() const
Kleo::Dialogs::AddUserIDDialog
Definition: adduseriddialog.h:45
Kleo::Dialogs::AddUserIDDialog::~AddUserIDDialog
~AddUserIDDialog()
Definition: adduseriddialog.cpp:236
Kleo::Dialogs::AddUserIDDialog::setComment
void setComment(const QString &comment)
Definition: adduseriddialog.cpp:255
requirementsAreMet
static bool requirementsAreMet(const QVector< Line > &list, QString &error)
Definition: adduseriddialog.cpp:270
attributeFromKey
static QString attributeFromKey(QString key)
Definition: adduseriddialog.cpp:91
Ok
Definition: setinitialpindialog.cpp:59
q
#define q
Definition: adduseridcommand.cpp:91
Kleo::Dialogs::AddUserIDDialog::email
QString email() const
Kleo::Validation::pgpName
QValidator * pgpName(QObject *parent=0)
Definition: validation.cpp:137
name
const char * name
Definition: uiserver/selectcertificatecommand.cpp:114
adduseriddialog.h
Kleo::Dialogs::AddUserIDDialog::comment
QString comment() const
QMap
Definition: signingcertificateselectiondialog.h:41
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:40 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