summaryrefslogtreecommitdiffstats
path: root/certmanager/lib/ui/keyapprovaldialog.cpp
blob: 6b873c0ba113d54a4c3fe4b6fae9c041a0c234cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
    keyapprovaldialog.h

    This file is part of libkleopatra, the KDE keymanagement library
    Copyright (c) 2004 Klarälvdalens Datakonsult AB

    Based on kpgpui.h
    Copyright (C) 2001,2002 the KPGP authors
    See file libtdenetwork/AUTHORS.kpgp for details

    Libkleopatra is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.

    Libkleopatra is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the TQt library by Trolltech AS, Norway (or with modified versions
    of TQt that use the same license as TQt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    TQt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "keyapprovaldialog.h"

#include "keyrequester.h"

#include <cryptplugfactory.h>
#include <kleo/cryptobackend.h>

#include <tdelocale.h>
#include <tdeglobalsettings.h>
#include <kseparator.h>

#include <tqstringlist.h>
#include <tqframe.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqcombobox.h>
#include <tqscrollview.h>
#include <tqpushbutton.h>

#include <gpgmepp/key.h>

#include <assert.h>

static Kleo::EncryptionPreference cb2pref( int i ) {
  switch ( i ) {
  default:
  case 0: return Kleo::UnknownPreference;
  case 1: return Kleo::NeverEncrypt;
  case 2: return Kleo::AlwaysEncrypt;
  case 3: return Kleo::AlwaysEncryptIfPossible;
  case 4: return Kleo::AlwaysAskForEncryption;
  case 5: return Kleo::AskWheneverPossible;
  }
}

static int pref2cb( Kleo::EncryptionPreference p ) {
  switch ( p ) {
  default:                            return 0;
  case Kleo::NeverEncrypt:            return 1;
  case Kleo::AlwaysEncrypt:           return 2;
  case Kleo::AlwaysEncryptIfPossible: return 3;
  case Kleo::AlwaysAskForEncryption:  return 4;
  case Kleo::AskWheneverPossible:     return 5;
  }
}

static TQStringList preferencesStrings() {
  return TQStringList() << i18n("<none>")
		       << i18n("Never Encrypt with This Key")
		       << i18n("Always Encrypt with This Key")
		       << i18n("Encrypt Whenever Encryption is Possible")
		       << i18n("Always Ask")
		       << i18n("Ask Whenever Encryption is Possible");
}


struct Kleo::KeyApprovalDialog::Private {
  Private() : selfRequester( 0 ), prefsChanged( false ) {}

  Kleo::KeyRequester * selfRequester;
  TQStringList addresses;
  std::vector<Kleo::KeyRequester*> requesters;
  std::vector<TQComboBox*> preferences;
  bool prefsChanged;
};

Kleo::KeyApprovalDialog::KeyApprovalDialog( const std::vector<Item> & recipients,
                                      const std::vector<GpgME::Key> & sender,
                                      TQWidget * parent, const char * name,
                                      bool modal )
  : KDialogBase( parent, name, modal, i18n("Encryption Key Approval"), Ok|Cancel, Ok ),
    d( 0 )
{
  assert( !recipients.empty() );

  d = new Private();

  TQFrame *page = makeMainWidget();
  TQVBoxLayout * vlay = new TQVBoxLayout( page, 0, spacingHint() );

  vlay->addWidget( new TQLabel( i18n("The following keys will be used for encryption:"), page ) );

  TQScrollView * sv = new TQScrollView( page );
  sv->setResizePolicy( TQScrollView::AutoOneFit );
  vlay->addWidget( sv );

  TQWidget * view = new TQWidget( sv->viewport() );

  TQGridLayout * glay = new TQGridLayout( view, 3, 2, marginHint(), spacingHint() );
  glay->setColStretch( 1, 1 );
  sv->addChild( view );

  int row = -1;

  if ( !sender.empty() ) {
    ++row;
    glay->addWidget( new TQLabel( i18n("Your keys:"), view ), row, 0 );
    d->selfRequester = new EncryptionKeyRequester( true, EncryptionKeyRequester::AllProtocols, view );
    d->selfRequester->setKeys( sender );
    glay->addWidget( d->selfRequester, row, 1 );
    ++row;
    glay->addMultiCellWidget( new KSeparator( TQt::Horizontal, view ), row, row, 0, 1 );
  }

  const TQStringList prefs = preferencesStrings();

  for ( std::vector<Item>::const_iterator it = recipients.begin() ; it != recipients.end() ; ++it ) {
    ++row;
    glay->addWidget( new TQLabel( i18n("Recipient:"), view ), row, 0 );
    glay->addWidget( new TQLabel( it->address, view ), row, 1 );
    d->addresses.push_back( it->address );

    ++row;
    glay->addWidget( new TQLabel( i18n("Encryption keys:"), view ), row, 0 );
    KeyRequester * req = new EncryptionKeyRequester( true, EncryptionKeyRequester::AllProtocols, view );
    req->setKeys( it->keys );
    glay->addWidget( req, row, 1 );
    d->requesters.push_back( req );

    ++row;
    glay->addWidget( new TQLabel( i18n("Encryption preference:"), view ), row, 0 );
    TQComboBox * cb = new TQComboBox( false, view );
    cb->insertStringList( prefs );
    glay->addWidget( cb, row, 1 );
    cb->setCurrentItem( pref2cb( it->pref ) );
    connect( cb, TQT_SIGNAL(activated(int)), TQT_SLOT(slotPrefsChanged()) );
    d->preferences.push_back( cb );
  }

  // calculate the optimal width for the dialog
  const int dialogWidth = marginHint()
                  + sv->frameWidth()
                  + view->sizeHint().width()
                  + sv->verticalScrollBar()->sizeHint().width()
                  + sv->frameWidth()
                  + marginHint()
                  + 2;
  // calculate the optimal height for the dialog
  const int dialogHeight = marginHint()
                   + fontMetrics().height()
                   + spacingHint()
                   + sv->frameWidth()
                   + view->sizeHint().height()
                   + sv->horizontalScrollBar()->sizeHint().height()
                   + sv->frameWidth()
                   + spacingHint()
                   + actionButton( KDialogBase::Cancel )->sizeHint().height()
                   + marginHint()
                   + 2;

  // don't make the dialog too large
  const TQRect desk = TDEGlobalSettings::desktopGeometry( this );
  setInitialSize( TQSize( kMin( dialogWidth, 3 * desk.width() / 4 ),
			 kMin( dialogHeight, 7 * desk.height() / 8 ) ) );
}

Kleo::KeyApprovalDialog::~KeyApprovalDialog() {
  delete d; d = 0;
}

std::vector<GpgME::Key> Kleo::KeyApprovalDialog::senderKeys() const {
  return d->selfRequester ? d->selfRequester->keys() : std::vector<GpgME::Key>() ;
}

std::vector<Kleo::KeyApprovalDialog::Item> Kleo::KeyApprovalDialog::items() const {
  assert( d->requesters.size() == d->addresses.size() );
  assert( d->requesters.size() == d->preferences.size() );

  std::vector<Item> result;
  result.reserve( d->requesters.size() );
  TQStringList::const_iterator ait = d->addresses.begin();
  std::vector<KeyRequester*>::const_iterator rit = d->requesters.begin();
  std::vector<TQComboBox*>::const_iterator cit = d->preferences.begin();
  while ( ait != d->addresses.end() )
    result.push_back( Item( *ait++, (*rit++)->keys(), cb2pref( (*cit++)->currentItem() ) ) );
  return result;
}

bool Kleo::KeyApprovalDialog::preferencesChanged() const {
  return d->prefsChanged;
}

void Kleo::KeyApprovalDialog::slotPrefsChanged() {
  d->prefsChanged = true;
}

#include "keyapprovaldialog.moc"