SVN_SILENT made messages (.desktop file) - always resolve ours
[kdepim.git] / kleopatra / dialogs / setinitialpindialog.cpp
blob908ec4c969ed99baaedd5ebdc454c92a2950c867
1 /* -*- mode: c++; c-basic-offset:4 -*-
2 dialogs/setinitialpindialog.cpp
4 This file is part of Kleopatra, the KDE keymanager
5 Copyright (c) 2009 Klarälvdalens Datakonsult AB
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.
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.
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
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.
33 #include <config-kleopatra.h>
35 #include "setinitialpindialog.h"
37 #include "ui_setinitialpindialog.h"
39 #include <KIconLoader>
40 #include <KLocalizedString>
41 #include <QIcon>
43 #include <QTextDocument> // for Qt::escape
45 #include <gpgme++/error.h>
47 #include <boost/static_assert.hpp>
49 #include <cassert>
51 using namespace Kleo;
52 using namespace Kleo::Dialogs;
53 using namespace GpgME;
55 enum State {
56 Unknown = 0,
57 NotSet,
58 AlreadySet,
59 Ongoing,
60 Ok,
61 Failed,
62 NumStates
65 const char *icons[] = {
66 // PENDING(marc) use better icons, once available
67 "", // Unknown
68 "", // NotSet
69 "security-medium", // AlreadySet
70 "movie-process-working-kde", // Ongoing
71 "security-high", // Ok
72 "security-low", // Failed
75 BOOST_STATIC_ASSERT((sizeof icons / sizeof(*icons) == NumStates));
76 BOOST_STATIC_ASSERT((sizeof("movie-") == 7));
78 static void update_widget(State state, bool delay, QLabel *resultLB, QLabel *lb, QPushButton *pb, QLabel *statusLB)
80 assert(state >= 0); assert(state < NumStates);
81 const char *icon = icons[state];
82 if (qstrncmp(icon, "movie-", sizeof("movie-") - 1) == 0) {
83 resultLB->setMovie(KIconLoader::global()->loadMovie(QLatin1String(icon + sizeof("movie-")), KIconLoader::NoGroup));
84 } else if (icon && *icon) {
85 resultLB->setPixmap(QIcon::fromTheme(QLatin1String(icon)).pixmap(32));
86 } else {
87 resultLB->setPixmap(QPixmap());
89 lb->setEnabled((state == NotSet || state == Failed) && !delay);
90 pb->setEnabled((state == NotSet || state == Failed) && !delay);
91 if (state == AlreadySet) {
92 statusLB->setText(xi18nc("@info", "No NullPin found. <warning>If this PIN was not set by you personally, the card might have been tampered with.</warning>"));
96 static QString format_error(const Error &err)
98 if (err.isCanceled()) {
99 return i18nc("@info", "Canceled setting PIN.");
101 if (err)
102 return xi18nc("@info",
103 "There was an error setting the PIN: <message>%1</message>.",
104 QString::fromLocal8Bit(err.asString()).toHtmlEscaped());
105 else {
106 return i18nc("@info", "PIN set successfully.");
110 class SetInitialPinDialog::Private
112 friend class ::Kleo::Dialogs::SetInitialPinDialog;
113 SetInitialPinDialog *const q;
114 public:
115 explicit Private(SetInitialPinDialog *qq)
116 : q(qq),
117 nksState(Unknown),
118 sigGState(Unknown),
119 ui(q)
124 private:
125 void slotNksButtonClicked()
127 nksState = Ongoing;
128 ui.nksStatusLB->clear();
129 updateWidgets();
130 Q_EMIT q->nksPinRequested();
133 void slotSigGButtonClicked()
135 sigGState = Ongoing;
136 ui.sigGStatusLB->clear();
137 updateWidgets();
138 Q_EMIT q->sigGPinRequested();
141 private:
142 void updateWidgets()
144 update_widget(nksState, false,
145 ui.nksResultIcon, ui.nksLB, ui.nksPB, ui.nksStatusLB);
146 update_widget(sigGState, nksState == NotSet || nksState == Failed || nksState == Ongoing,
147 ui.sigGResultIcon, ui.sigGLB, ui.sigGPB, ui.sigGStatusLB);
148 ui.closePB()->setEnabled(q->isComplete());
149 ui.cancelPB()->setEnabled(!q->isComplete());
152 private:
153 State nksState, sigGState;
155 struct UI : public Ui::SetInitialPinDialog {
156 explicit UI(Dialogs::SetInitialPinDialog *qq)
157 : Ui::SetInitialPinDialog()
159 setupUi(qq);
161 closePB()->setEnabled(false);
163 connect(closePB(), &QAbstractButton::clicked, qq, &QDialog::accept);
166 QAbstractButton *closePB() const
168 assert(dialogButtonBox);
169 return dialogButtonBox->button(QDialogButtonBox::Close);
172 QAbstractButton *cancelPB() const
174 assert(dialogButtonBox);
175 return dialogButtonBox->button(QDialogButtonBox::Cancel);
178 } ui;
181 SetInitialPinDialog::SetInitialPinDialog(QWidget *p)
182 : QDialog(p), d(new Private(this))
187 SetInitialPinDialog::~SetInitialPinDialog() {}
189 void SetInitialPinDialog::setNksPinPresent(bool on)
191 d->nksState = on ? AlreadySet : NotSet;
192 d->updateWidgets();
195 void SetInitialPinDialog::setSigGPinPresent(bool on)
197 d->sigGState = on ? AlreadySet : NotSet;
198 d->updateWidgets();
201 void SetInitialPinDialog::setNksPinSettingResult(const Error &err)
203 d->ui.nksStatusLB->setText(format_error(err));
204 d->nksState =
205 err.isCanceled() ? NotSet :
206 err ? Failed :
208 d->updateWidgets();
211 void SetInitialPinDialog::setSigGPinSettingResult(const Error &err)
213 d->ui.sigGStatusLB->setText(format_error(err));
214 d->sigGState =
215 err.isCanceled() ? NotSet :
216 err ? Failed :
218 d->updateWidgets();
221 bool SetInitialPinDialog::isComplete() const
223 return (d->nksState == Ok || d->nksState == AlreadySet)
224 && (d->sigGState == Ok || d->sigGState == AlreadySet);
227 #include "moc_setinitialpindialog.cpp"