certmanager/lib

keyapprovaldialog.cpp
1 /*
2  keyapprovaldialog.h
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  Copyright (c) 2004 Klarälvdalens Datakonsult AB
6 
7  Based on kpgpui.h
8  Copyright (C) 2001,2002 the KPGP authors
9  See file libtdenetwork/AUTHORS.kpgp for details
10 
11  Libkleopatra is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15 
16  Libkleopatra is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 
25  In addition, as a special exception, the copyright holders give
26  permission to link the code of this program with any edition of
27  the TQt library by Trolltech AS, Norway (or with modified versions
28  of TQt that use the same license as TQt), and distribute linked
29  combinations including the two. You must obey the GNU General
30  Public License in all respects for all of the code used other than
31  TQt. If you modify this file, you may extend this exception to
32  your version of the file, but you are not obligated to do so. If
33  you do not wish to do so, delete this exception statement from
34  your version.
35 */
36 
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40 
41 #include "keyapprovaldialog.h"
42 
43 #include "keyrequester.h"
44 
45 #include <cryptplugfactory.h>
46 #include <kleo/cryptobackend.h>
47 
48 #include <tdelocale.h>
49 #include <tdeglobalsettings.h>
50 #include <kseparator.h>
51 
52 #include <tqstringlist.h>
53 #include <tqframe.h>
54 #include <tqlayout.h>
55 #include <tqlabel.h>
56 #include <tqcombobox.h>
57 #include <tqscrollview.h>
58 #include <tqpushbutton.h>
59 
60 #include <gpgmepp/key.h>
61 
62 #include <assert.h>
63 
64 static Kleo::EncryptionPreference cb2pref( int i ) {
65  switch ( i ) {
66  default:
67  case 0: return Kleo::UnknownPreference;
68  case 1: return Kleo::NeverEncrypt;
69  case 2: return Kleo::AlwaysEncrypt;
70  case 3: return Kleo::AlwaysEncryptIfPossible;
71  case 4: return Kleo::AlwaysAskForEncryption;
72  case 5: return Kleo::AskWheneverPossible;
73  }
74 }
75 
76 static int pref2cb( Kleo::EncryptionPreference p ) {
77  switch ( p ) {
78  default: return 0;
79  case Kleo::NeverEncrypt: return 1;
80  case Kleo::AlwaysEncrypt: return 2;
81  case Kleo::AlwaysEncryptIfPossible: return 3;
82  case Kleo::AlwaysAskForEncryption: return 4;
83  case Kleo::AskWheneverPossible: return 5;
84  }
85 }
86 
87 static TQStringList preferencesStrings() {
88  return TQStringList() << i18n("<none>")
89  << i18n("Never Encrypt with This Key")
90  << i18n("Always Encrypt with This Key")
91  << i18n("Encrypt Whenever Encryption is Possible")
92  << i18n("Always Ask")
93  << i18n("Ask Whenever Encryption is Possible");
94 }
95 
96 
97 struct Kleo::KeyApprovalDialog::Private {
98  Private() : selfRequester( 0 ), prefsChanged( false ) {}
99 
100  Kleo::KeyRequester * selfRequester;
101  TQStringList addresses;
102  std::vector<Kleo::KeyRequester*> requesters;
103  std::vector<TQComboBox*> preferences;
104  bool prefsChanged;
105 };
106 
107 Kleo::KeyApprovalDialog::KeyApprovalDialog( const std::vector<Item> & recipients,
108  const std::vector<GpgME::Key> & sender,
109  TQWidget * parent, const char * name,
110  bool modal )
111  : KDialogBase( parent, name, modal, i18n("Encryption Key Approval"), Ok|Cancel, Ok ),
112  d( 0 )
113 {
114  assert( !recipients.empty() );
115 
116  d = new Private();
117 
118  TQFrame *page = makeMainWidget();
119  TQVBoxLayout * vlay = new TQVBoxLayout( page, 0, spacingHint() );
120 
121  vlay->addWidget( new TQLabel( i18n("The following keys will be used for encryption:"), page ) );
122 
123  TQScrollView * sv = new TQScrollView( page );
124  sv->setResizePolicy( TQScrollView::AutoOneFit );
125  vlay->addWidget( sv );
126 
127  TQWidget * view = new TQWidget( sv->viewport() );
128 
129  TQGridLayout * glay = new TQGridLayout( view, 3, 2, marginHint(), spacingHint() );
130  glay->setColStretch( 1, 1 );
131  sv->addChild( view );
132 
133  int row = -1;
134 
135  if ( !sender.empty() ) {
136  ++row;
137  glay->addWidget( new TQLabel( i18n("Your keys:"), view ), row, 0 );
138  d->selfRequester = new EncryptionKeyRequester( true, EncryptionKeyRequester::AllProtocols, view );
139  d->selfRequester->setKeys( sender );
140  glay->addWidget( d->selfRequester, row, 1 );
141  ++row;
142  glay->addMultiCellWidget( new KSeparator( TQt::Horizontal, view ), row, row, 0, 1 );
143  }
144 
145  const TQStringList prefs = preferencesStrings();
146 
147  for ( std::vector<Item>::const_iterator it = recipients.begin() ; it != recipients.end() ; ++it ) {
148  ++row;
149  glay->addWidget( new TQLabel( i18n("Recipient:"), view ), row, 0 );
150  glay->addWidget( new TQLabel( it->address, view ), row, 1 );
151  d->addresses.push_back( it->address );
152 
153  ++row;
154  glay->addWidget( new TQLabel( i18n("Encryption keys:"), view ), row, 0 );
155  KeyRequester * req = new EncryptionKeyRequester( true, EncryptionKeyRequester::AllProtocols, view );
156  req->setKeys( it->keys );
157  glay->addWidget( req, row, 1 );
158  d->requesters.push_back( req );
159 
160  ++row;
161  glay->addWidget( new TQLabel( i18n("Encryption preference:"), view ), row, 0 );
162  TQComboBox * cb = new TQComboBox( false, view );
163  cb->insertStringList( prefs );
164  glay->addWidget( cb, row, 1 );
165  cb->setCurrentItem( pref2cb( it->pref ) );
166  connect( cb, TQ_SIGNAL(activated(int)), TQ_SLOT(slotPrefsChanged()) );
167  d->preferences.push_back( cb );
168  }
169 
170  // calculate the optimal width for the dialog
171  const int dialogWidth = marginHint()
172  + sv->frameWidth()
173  + view->sizeHint().width()
174  + sv->verticalScrollBar()->sizeHint().width()
175  + sv->frameWidth()
176  + marginHint()
177  + 2;
178  // calculate the optimal height for the dialog
179  const int dialogHeight = marginHint()
180  + fontMetrics().height()
181  + spacingHint()
182  + sv->frameWidth()
183  + view->sizeHint().height()
184  + sv->horizontalScrollBar()->sizeHint().height()
185  + sv->frameWidth()
186  + spacingHint()
187  + actionButton( KDialogBase::Cancel )->sizeHint().height()
188  + marginHint()
189  + 2;
190 
191  // don't make the dialog too large
192  const TQRect desk = TDEGlobalSettings::desktopGeometry( this );
193  setInitialSize( TQSize( kMin( dialogWidth, 3 * desk.width() / 4 ),
194  kMin( dialogHeight, 7 * desk.height() / 8 ) ) );
195 }
196 
197 Kleo::KeyApprovalDialog::~KeyApprovalDialog() {
198  delete d; d = 0;
199 }
200 
201 std::vector<GpgME::Key> Kleo::KeyApprovalDialog::senderKeys() const {
202  return d->selfRequester ? d->selfRequester->keys() : std::vector<GpgME::Key>() ;
203 }
204 
205 std::vector<Kleo::KeyApprovalDialog::Item> Kleo::KeyApprovalDialog::items() const {
206  assert( d->requesters.size() == d->addresses.size() );
207  assert( d->requesters.size() == d->preferences.size() );
208 
209  std::vector<Item> result;
210  result.reserve( d->requesters.size() );
211  TQStringList::const_iterator ait = d->addresses.begin();
212  std::vector<KeyRequester*>::const_iterator rit = d->requesters.begin();
213  std::vector<TQComboBox*>::const_iterator cit = d->preferences.begin();
214  while ( ait != d->addresses.end() )
215  result.push_back( Item( *ait++, (*rit++)->keys(), cb2pref( (*cit++)->currentItem() ) ) );
216  return result;
217 }
218 
219 bool Kleo::KeyApprovalDialog::preferencesChanged() const {
220  return d->prefsChanged;
221 }
222 
223 void Kleo::KeyApprovalDialog::slotPrefsChanged() {
224  d->prefsChanged = true;
225 }
226 
227 #include "keyapprovaldialog.moc"
Base class for SigningKeyRequester and EncryptionKeyRequester.
Definition: keyrequester.h:76