Better wording
[kdepim.git] / kleopatra / dialogs / lookupcertificatesdialog.cpp
blobb50bcd4ab99491f9d526a6b4349c1a5f1aae698f
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 <kleo/stl_util.h>
46 #include <gpgme++/key.h>
48 #include <KLineEdit>
49 #include <KLocale>
51 #include <QTreeView>
52 #include <QLayout>
53 #include <QPushButton>
54 #include <QHeaderView>
55 #include <QLabel>
57 #include <boost/bind.hpp>
59 #include <cassert>
61 using namespace Kleo;
62 using namespace Kleo::Dialogs;
63 using namespace GpgME;
64 using namespace boost;
66 static const int minimalSearchTextLength = 2; // ### TODO: make that KIOSK-able
68 class LookupCertificatesDialog::Private {
69 friend class ::Kleo::Dialogs::LookupCertificatesDialog;
70 LookupCertificatesDialog * const q;
71 public:
72 explicit Private( LookupCertificatesDialog * qq );
73 ~Private();
75 private:
76 void slotSelectionChanged() {
77 enableDisableWidgets();
79 void slotSearchTextChanged() {
80 enableDisableWidgets();
82 void slotSearchClicked() {
83 emit q->searchTextChanged( ui.findED->text() );
85 void slotDetailsClicked() {
86 assert( q->selectedCertificates().size() == 1 );
87 emit q->detailsRequested( q->selectedCertificates().front() );
89 void slotSaveAsClicked() {
90 emit q->saveAsRequested( q->selectedCertificates() );
93 void enableDisableWidgets();
95 QString searchText() const { return ui.findED->text().trimmed(); }
96 QModelIndexList selectedIndexes() const {
97 if ( const QItemSelectionModel * const sm = ui.resultTV->selectionModel() )
98 return sm->selectedRows();
99 else
100 return QModelIndexList();
102 unsigned int numSelectedCertificates() const {
103 return selectedIndexes().size();
105 private:
106 AbstractKeyListModel * model;
107 KeyListSortFilterProxyModel proxy;
108 bool passive;
110 struct Ui : Ui_LookupCertificatesDialog {
112 explicit Ui( LookupCertificatesDialog * q )
113 : Ui_LookupCertificatesDialog()
115 setupUi( q );
117 saveAsPB->hide(); // ### not yet implemented in LookupCertificatesCommand
119 findED->setClearButtonShown( true );
121 importPB()->setText( i18n("Import") );
122 importPB()->setEnabled( false );
124 HeaderView * hv = new HeaderView( Qt::Horizontal );
125 KDAB_SET_OBJECT_NAME( hv );
126 resultTV->setHeader( hv );
128 connect( resultTV, SIGNAL(doubleClicked(QModelIndex)),
129 importPB(), SLOT(animateClick()) );
131 findED->setFocus();
134 QPushButton * importPB() const { return buttonBox->button( QDialogButtonBox::Save ); }
135 QPushButton * closePB() const { return buttonBox->button( QDialogButtonBox::Close ); }
136 } ui;
139 LookupCertificatesDialog::Private::Private( LookupCertificatesDialog * qq )
140 : q( qq ),
141 model( AbstractKeyListModel::createFlatKeyListModel() ),
142 proxy(),
143 passive( false ),
144 ui( q )
146 KDAB_SET_OBJECT_NAME( model );
147 KDAB_SET_OBJECT_NAME( proxy );
149 proxy.setSourceModel( model );
150 ui.resultTV->setModel( &proxy );
152 connect( ui.resultTV->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
153 q, SLOT(slotSelectionChanged()) );
156 LookupCertificatesDialog::Private::~Private() {}
158 LookupCertificatesDialog::LookupCertificatesDialog( QWidget * p, Qt::WindowFlags f )
159 : QDialog( p, f ), d( new Private( this ) )
164 LookupCertificatesDialog::~LookupCertificatesDialog() {}
166 void LookupCertificatesDialog::setCertificates( const std::vector<Key> & certs ) {
167 d->model->setKeys( certs );
168 d->ui.resultTV->header()->resizeSections( QHeaderView::ResizeToContents );
169 d->ui.resultTV->setFocus();
172 std::vector<Key> LookupCertificatesDialog::selectedCertificates() const {
173 return d->proxy.keys( d->selectedIndexes() );
176 void LookupCertificatesDialog::setPassive( bool on ) {
177 if ( d->passive == on )
178 return;
179 d->passive = on;
180 d->enableDisableWidgets();
183 bool LookupCertificatesDialog::isPassive() const {
184 return d->passive;
187 void LookupCertificatesDialog::accept() {
188 assert( !d->selectedIndexes().empty() );
189 emit importRequested( selectedCertificates() );
190 QDialog::accept();
193 void LookupCertificatesDialog::Private::enableDisableWidgets() {
194 // enable/disable everything except 'close', based on passive:
195 Q_FOREACH( QObject * const o, q->children() )
196 if ( QWidget * const w = qobject_cast<QWidget*>( o ) )
197 w->setDisabled( passive && w != ui.closePB() && w != ui.buttonBox );
199 if ( passive )
200 return;
202 ui.findPB->setEnabled( searchText().length() > minimalSearchTextLength );
204 const unsigned int n = numSelectedCertificates();
206 ui.detailsPB->setEnabled( n == 1 );
207 ui.saveAsPB->setEnabled( n == 1 );
208 ui.importPB()->setEnabled( n != 0 );
209 ui.importPB()->setDefault( false ); // otherwise Import becomes default button if enabled and return triggers both a search and accept()
212 #include "moc_lookupcertificatesdialog.cpp"