SVN_SILENT made messages (.desktop file) - always resolve ours
[kdepim.git] / kleopatra / dialogs / lookupcertificatesdialog.cpp
blob2ce338749dc43404aa732d4c141b2f7d36da3c3e
1 /* -*- mode: c++; c-basic-offset:4 -*-
2 dialogs/lookupcertificatesdialog.cpp
4 This file is part of Kleopatra, the KDE keymanager
5 Copyright (c) 2008 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 "lookupcertificatesdialog.h"
37 #include "ui_lookupcertificatesdialog.h"
39 #include <models/keylistmodel.h>
40 #include <models/keylistsortfilterproxymodel.h>
42 #include <utils/headerview.h>
44 #include <Libkleo/Stl_Util>
45 #include <KConfigGroup>
46 #include <gpgme++/key.h>
48 #include <KLocalizedString>
50 #include <QPushButton>
51 #include <QHeaderView>
53 #include <boost/bind.hpp>
55 #include <cassert>
56 #include <KSharedConfig>
58 using namespace Kleo;
59 using namespace Kleo::Dialogs;
60 using namespace GpgME;
61 using namespace boost;
63 static const int minimalSearchTextLength = 2; // ### TODO: make that KIOSK-able
65 class LookupCertificatesDialog::Private
67 friend class ::Kleo::Dialogs::LookupCertificatesDialog;
68 LookupCertificatesDialog *const q;
69 public:
70 explicit Private(LookupCertificatesDialog *qq);
71 ~Private();
73 private:
74 void slotSelectionChanged()
76 enableDisableWidgets();
78 void slotSearchTextChanged()
80 enableDisableWidgets();
82 void slotSearchClicked()
84 Q_EMIT q->searchTextChanged(ui.findED->text());
86 void slotDetailsClicked()
88 assert(q->selectedCertificates().size() == 1);
89 Q_EMIT q->detailsRequested(q->selectedCertificates().front());
91 void slotSaveAsClicked()
93 Q_EMIT q->saveAsRequested(q->selectedCertificates());
96 void readConfig();
97 void writeConfig();
98 void enableDisableWidgets();
100 QString searchText() const
102 return ui.findED->text().trimmed();
104 QModelIndexList selectedIndexes() const
106 if (const QItemSelectionModel *const sm = ui.resultTV->selectionModel()) {
107 return sm->selectedRows();
108 } else {
109 return QModelIndexList();
112 unsigned int numSelectedCertificates() const
114 return selectedIndexes().size();
116 private:
117 AbstractKeyListModel *model;
118 KeyListSortFilterProxyModel proxy;
119 bool passive;
121 struct Ui : Ui_LookupCertificatesDialog {
123 explicit Ui(LookupCertificatesDialog *q)
124 : Ui_LookupCertificatesDialog()
126 setupUi(q);
128 saveAsPB->hide(); // ### not yet implemented in LookupCertificatesCommand
130 findED->setClearButtonEnabled(true);
132 importPB()->setText(i18n("Import"));
133 importPB()->setEnabled(false);
135 HeaderView *hv = new HeaderView(Qt::Horizontal);
136 KDAB_SET_OBJECT_NAME(hv);
137 resultTV->setHeader(hv);
139 connect(resultTV, SIGNAL(doubleClicked(QModelIndex)),
140 importPB(), SLOT(animateClick()));
142 findED->setFocus();
145 QPushButton *importPB() const
147 return buttonBox->button(QDialogButtonBox::Save);
149 QPushButton *closePB() const
151 return buttonBox->button(QDialogButtonBox::Close);
153 } ui;
156 LookupCertificatesDialog::Private::Private(LookupCertificatesDialog *qq)
157 : q(qq),
158 model(AbstractKeyListModel::createFlatKeyListModel()),
159 proxy(),
160 passive(false),
161 ui(q)
163 KDAB_SET_OBJECT_NAME(model);
164 KDAB_SET_OBJECT_NAME(proxy);
166 proxy.setSourceModel(model);
167 ui.resultTV->setModel(&proxy);
169 connect(ui.resultTV->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
170 q, SLOT(slotSelectionChanged()));
173 LookupCertificatesDialog::Private::~Private() {}
175 void LookupCertificatesDialog::Private::readConfig()
177 KConfigGroup dialog(KSharedConfig::openConfig(), "LookupCertificatesDialog");
178 const QSize size = dialog.readEntry("Size", QSize(600, 400));
179 if (size.isValid()) {
180 q->resize(size);
182 const QByteArray headerState = dialog.readEntry("header", QByteArray());
183 if (!headerState.isEmpty()) {
184 ui.resultTV->header()->restoreState(headerState);
188 void LookupCertificatesDialog::Private::writeConfig()
190 KConfigGroup dialog(KSharedConfig::openConfig(), "LookupCertificatesDialog");
191 dialog.writeEntry("header", ui.resultTV->header()->saveState());
192 dialog.writeEntry("Size", q->size());
193 dialog.sync();
196 LookupCertificatesDialog::LookupCertificatesDialog(QWidget *p, Qt::WindowFlags f)
197 : QDialog(p, f), d(new Private(this))
199 d->ui.findPB->setEnabled(false);
200 d->readConfig();
203 LookupCertificatesDialog::~LookupCertificatesDialog()
205 d->writeConfig();
208 void LookupCertificatesDialog::setCertificates(const std::vector<Key> &certs)
210 d->model->setKeys(certs);
211 d->ui.resultTV->header()->resizeSections(QHeaderView::ResizeToContents);
212 d->ui.resultTV->setFocus();
215 std::vector<Key> LookupCertificatesDialog::selectedCertificates() const
217 return d->proxy.keys(d->selectedIndexes());
220 void LookupCertificatesDialog::setPassive(bool on)
222 if (d->passive == on) {
223 return;
225 d->passive = on;
226 d->enableDisableWidgets();
229 bool LookupCertificatesDialog::isPassive() const
231 return d->passive;
234 void LookupCertificatesDialog::setSearchText(const QString &text)
236 d->ui.findED->setText(text);
239 QString LookupCertificatesDialog::searchText() const
241 return d->ui.findED->text();
244 void LookupCertificatesDialog::accept()
246 assert(!d->selectedIndexes().empty());
247 Q_EMIT importRequested(selectedCertificates());
248 QDialog::accept();
251 void LookupCertificatesDialog::Private::enableDisableWidgets()
253 // enable/disable everything except 'close', based on passive:
254 Q_FOREACH (QObject *const o, q->children())
255 if (QWidget *const w = qobject_cast<QWidget *>(o)) {
256 w->setDisabled(passive && w != ui.closePB() && w != ui.buttonBox);
259 if (passive) {
260 return;
263 ui.findPB->setEnabled(searchText().length() > minimalSearchTextLength);
265 const unsigned int n = numSelectedCertificates();
267 ui.detailsPB->setEnabled(n == 1);
268 ui.saveAsPB->setEnabled(n == 1);
269 ui.importPB()->setEnabled(n != 0);
270 ui.importPB()->setDefault(false); // otherwise Import becomes default button if enabled and return triggers both a search and accept()
273 #include "moc_lookupcertificatesdialog.cpp"