33 #include <config-kleopatra.h>
37 #include "ui_adduseriddialog.h"
41 #include <kleo/stl_util.h>
44 #include <QStringList>
45 #include <QPushButton>
48 #include <KConfigGroup>
50 #include <KLocalizedString>
56 using namespace Kleo::Dialogs;
67 static QString
pgpLabel(
const QString & attr ) {
68 if ( attr == QLatin1String(
"NAME") )
70 if ( attr == QLatin1String(
"COMMENT") )
71 return i18n(
"Comment");
72 if ( attr == QLatin1String(
"EMAIL") )
80 const QString label =
pgpLabel( attr ) ;
81 if ( !label.isEmpty() )
85 return i18nc(
"Format string for the labels in the \"Your Personal Data\" page",
86 "%1 (%2)", label, attr );
92 return key.remove( QLatin1Char(
'!') );
96 const int idx = l->indexOf( w );
98 l->getItemPosition( idx, &r, &c, &rs, &cs );
102 static QLineEdit *
adjust_row( QGridLayout * l,
int row,
const QString & label,
const QString & preset,
QValidator * validator,
bool readonly,
bool required ) {
105 assert( row < l->rowCount() );
107 QLabel * lb = qobject_cast<QLabel*>( l->itemAtPosition( row, 0 )->widget() );
109 QLineEdit * le = qobject_cast<QLineEdit*>( l->itemAtPosition( row, 1 )->widget() );
111 QLabel * reqLB = qobject_cast<QLabel*>( l->itemAtPosition( row, 2 )->widget() );
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();
119 if ( !validator->parent() )
120 validator->setParent( le );
121 le->setValidator( validator );
124 le->setReadOnly( readonly && le->hasAcceptableInput() );
133 class AddUserIDDialog::Private {
134 friend class ::Kleo::Dialogs::AddUserIDDialog;
145 void slotUserIDChanged();
148 bool isComplete()
const;
151 struct UI :
public Ui_AddUserIDDialog {
153 QVector<Line> lineList;
156 : Ui_AddUserIDDialog()
167 nameRequiredLB->hide();
171 emailRequiredLB->hide();
175 commentRequiredLB->hide();
178 errorLB->setText( QLatin1String(
"2<br>1") );
179 errorLB->setFixedHeight( errorLB->minimumSizeHint().height() );
182 const KConfigGroup config( KGlobal::config(),
"CertificateCreationWizard" );
183 const QStringList attrOrder = config.readEntry(
"OpenPGPAttributeOrder",
184 QStringList() << QLatin1String(
"NAME!") << QLatin1String(
"EMAIL!") << QLatin1String(
"COMMENT") );
188 Q_FOREACH(
const QString & rawKey, attrOrder ) {
189 const QString key = rawKey.trimmed().toUpper();
191 if ( attr.isEmpty() )
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"),
198 const QString regex = config.readEntry( attr + QLatin1String(
"_regex") );
202 if ( attr == QLatin1String(
"EMAIL") ) {
205 }
else if ( attr == QLatin1String(
"NAME") ) {
208 }
else if ( attr == QLatin1String(
"COMMENT") ) {
215 QLineEdit * le =
adjust_row( gridLayout, row, label, preset, validator, readonly, required );
217 const Line line = { key, label, regex, le };
221 lineList = kdtools::copy< QVector<Line> >( lines );
224 QPushButton * okPB()
const {
231 :
QDialog( p, f ),
d( new Private( this ) )
240 d->ui.nameLE->setText( name );
244 return d->ui.nameLE->text().trimmed();
248 d->ui.emailLE->setText( email );
252 return d->ui.emailLE->text().trimmed();
256 d->ui.commentLE->setText( comment );
260 return d->ui.commentLE->text().trimmed();
264 QString text = le->text();
265 int pos = le->cursorPosition();
267 return !v || v->validate( text, pos ) == QValidator::Intermediate ;
271 Q_FOREACH(
const Line & line, list ) {
272 const QLineEdit * le = line.edit;
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 );
282 error = i18nc(
"@info",
"<interface>%1</interface> is required, but empty.<nl/>"
283 "Local Admin rule: <icode>%2</icode>", line.label, line.regex );
287 if ( line.regex.isEmpty() )
288 error = i18nc(
"@info",
"<interface>%1</interface> is incomplete.", line.label );
290 error = i18nc(
"@info",
"<interface>%1</interface> is incomplete.<nl/>"
291 "Local Admin rule: <icode>%2</icode>", line.label, line.regex );
293 }
else if ( !le->hasAcceptableInput() ) {
294 if ( line.regex.isEmpty() )
295 error = i18nc(
"@info",
"<interface>%1</interface> is invalid.", line.label );
297 error = i18nc(
"@info",
"<interface>%1</interface> is invalid.<nl/>"
298 "Local Admin rule: <icode>%2</icode>", line.label, line.regex );
301 kDebug() <<
"ok" << endl;
306 bool AddUserIDDialog::Private::isComplete()
const {
309 ui.errorLB->setText( error );
313 void AddUserIDDialog::Private::slotUserIDChanged() {
315 ui.okPB()->setEnabled( isComplete() );
317 const QString
name =
q->name();
318 const QString
email =
q->email();
319 const QString comment =
q->comment();
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(
'>' ) );
329 ui.resultLB->setText( parts.join( QLatin1String(
" " ) ) );
332 #include "moc_adduseriddialog.cpp"
static QLineEdit * adjust_row(QGridLayout *l, int row, const QString &label, const QString &preset, QValidator *validator, bool readonly, bool required)
QValidator * email(QObject *parent=0)
static bool has_intermediate_input(const QLineEdit *le)
static int row_index_of(QWidget *w, QGridLayout *l)
static QString pgpLabel(const QString &attr)
static std::string email(const UserID &uid)
static QString attributeLabel(const QString &attr, bool pgp)
QValidator * pgpComment(QObject *parent=0)
void setName(const QString &name)
AddUserIDDialog(QWidget *parent=0, Qt::WindowFlags f=0)
void setEmail(const QString &email)
void setComment(const QString &comment)
static bool requirementsAreMet(const QVector< Line > &list, QString &error)
static QString attributeFromKey(QString key)
QValidator * pgpName(QObject *parent=0)