certmanager/lib

directoryserviceswidget.cpp
1 /*
2  directoryserviceswidget.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2001,2002,2004 Klarälvdalens Datakonsult AB
6 
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.
11 
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.
16 
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
20 
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 TQt library by Trolltech AS, Norway (or with modified versions
24  of TQt that use the same license as TQt), 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  TQt. 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.
31 */
32 
33 #include <config.h>
34 
35 #include "directoryserviceswidget.h"
36 #include "adddirectoryservicedialogimpl.h"
37 #include "cryptplugwrapper.h"
38 
39 #include <klineedit.h>
40 #include <kleo/cryptoconfig.h>
41 #include <kiconloader.h>
42 #include <kdebug.h>
43 
44 #include <tqbuttongroup.h>
45 #include <tqtoolbutton.h>
46 #include <tqlistview.h>
47 #include <tqpushbutton.h>
48 
49 using namespace Kleo;
50 
51 class QX500ListViewItem : public TQListViewItem
52 {
53 public:
54  QX500ListViewItem( TQListView* lv, TQListViewItem* prev,
55  const TQString& serverName,
56  const TQString& portNumber,
57  const TQString& dn,
58  const TQString& username,
59  const TQString& password )
60  : TQListViewItem( lv, prev, serverName, portNumber, dn, username ) {
61  setPassword( password );
62  }
63 
64  void setPassword( const TQString& pass ) {
65  mPassword = pass;
66  setText( 4, pass.isEmpty() ? TQString() : TQString::fromLatin1( "******" ) );
67  }
68 
69  const TQString& password() const { return mPassword; }
70 
71  void setData( const TQString& serverName,
72  const TQString& portNumber,
73  const TQString& dn,
74  const TQString& username,
75  const TQString& password ) {
76  setText( 0, serverName );
77  setText( 1, portNumber );
78  setText( 2, dn );
79  setText( 3, username );
80  setPassword( password );
81  }
82 
83  void copyItem( QX500ListViewItem* item ) {
84  for ( unsigned int i = 0; i < 4 ; ++i )
85  setText( i, item->text( i ) );
86  setPassword( item->password() );
87  }
88 
89 private:
90  TQString mPassword;
91 };
92 
93 Kleo::DirectoryServicesWidget::DirectoryServicesWidget(
94  Kleo::CryptoConfigEntry* configEntry,
95  TQWidget* parent, const char* name, WFlags fl )
96  : DirectoryServicesWidgetBase( parent, name, fl ),
97  mConfigEntry( configEntry )
98 {
99  x500LV->setSorting( -1 );
100 
101  // taken from kmail's configuredialog.cpp
102  upButton->setIconSet( BarIconSet( "go-up", TDEIcon::SizeSmall ) );
103  upButton->setEnabled( false ); // b/c no item is selected yet
104 
105  downButton->setIconSet( BarIconSet( "go-down", TDEIcon::SizeSmall ) );
106  downButton->setEnabled( false ); // b/c no item is selected yet
107 }
108 
109 
110 /*
111  * Destroys the object and frees any allocated resources
112  */
113 DirectoryServicesWidget::~DirectoryServicesWidget()
114 {
115  // no need to delete child widgets, TQt does it all for us
116 }
117 
118 
123 void DirectoryServicesWidget::enableDisable( CryptPlugWrapper* cryptPlug ) // unused?
124 {
125  // disable the whole page if the plugin does not support the use
126  // of directory services
127  setEnabled( cryptPlug->hasFeature( Feature_CertificateDirectoryService ) ||
128  cryptPlug->hasFeature( Feature_CRLDirectoryService ) );
129 }
130 
131 
132 /*
133  * protected slot, connected to selectionChanged()
134  */
135 void DirectoryServicesWidget::slotServiceChanged( TQListViewItem* item )
136 {
137  if( item )
138  removeServicePB->setEnabled( true );
139  else
140  removeServicePB->setEnabled( false );
141  downButton->setEnabled( item && item->itemBelow() );
142  upButton->setEnabled( item && item->itemAbove() );
143 }
144 
145 
146 /*
147  * protected slot, connected to returnPressed/doubleClicked
148  */
149 void DirectoryServicesWidget::slotServiceSelected( TQListViewItem* item )
150 {
151  AddDirectoryServiceDialogImpl* dlg = new AddDirectoryServiceDialogImpl( this );
152  dlg->serverNameED->setText( item->text( 0 ) );
153  dlg->portED->setText( item->text( 1 ) );
154  dlg->descriptionED->setText( item->text( 2 ) );
155  dlg->usernameED->setText( item->text( 3 ) );
156  TQString pass = static_cast<QX500ListViewItem *>( item )->password();
157  dlg->passwordED->setText( pass );
158 
159  if( dlg->exec() == TQDialog::Accepted ) {
160  item->setText( 0, dlg->serverNameED->text() );
161  item->setText( 1, dlg->portED->text() );
162  item->setText( 2, dlg->descriptionED->text() );
163  item->setText( 3, dlg->usernameED->text() );
164  static_cast<QX500ListViewItem *>( item )->setPassword( dlg->passwordED->text() );
165  emit changed();
166  }
167  delete dlg;
168 }
169 
170 
171 /*
172  * protected slot
173  */
174 void DirectoryServicesWidget::slotAddService()
175 {
176  AddDirectoryServiceDialogImpl* dlg = new AddDirectoryServiceDialogImpl( this );
177  if( dlg->exec() == TQDialog::Accepted ) {
178  QX500ListViewItem *item = new QX500ListViewItem( x500LV, x500LV->lastItem(),
179  dlg->serverNameED->text(),
180  dlg->portED->text(),
181  dlg->descriptionED->text(),
182  dlg->usernameED->text(),
183  dlg->passwordED->text() );
184  slotServiceChanged(item);
185  emit changed();
186  }
187  delete dlg;
188 }
189 
190 /*
191  * protected slot
192  */
193 void DirectoryServicesWidget::slotDeleteService()
194 {
195  TQListViewItem* item = x500LV->selectedItem();
196  Q_ASSERT( item );
197  if( !item )
198  return;
199  else
200  delete item;
201  x500LV->triggerUpdate();
202  item = x500LV->currentItem();
203  x500LV->setCurrentItem( item ); // seems necessary...
204  x500LV->setSelected( item, true );
205  emit changed();
206 }
207 
208 
209 void DirectoryServicesWidget::setInitialServices( const KURL::List& urls )
210 {
211  x500LV->clear();
212  for( KURL::List::const_iterator it = urls.begin(); it != urls.end(); ++it ) {
213  TQString dn = KURL::decode_string( (*it).query().mid( 1 ) ); // decode query and skip leading '?'
214  (void)new QX500ListViewItem( x500LV, x500LV->lastItem(),
215  (*it).host(),
216  TQString::number( (*it).port() ),
217  dn,
218  (*it).user(),
219  (*it).pass());
220  }
221 }
222 
223 KURL::List DirectoryServicesWidget::urlList() const
224 {
225  KURL::List lst;
226  TQListViewItemIterator it( x500LV );
227  for ( ; it.current() ; ++it ) {
228  TQListViewItem* item = it.current();
229  KURL url;
230  url.setProtocol( "ldap" );
231  url.setHost( item->text( 0 ) );
232  url.setPort( item->text( 1 ).toInt() );
233  url.setPath( "/" ); // workaround KURL parsing bug
234  url.setQuery( item->text( 2 ) );
235  url.setUser( item->text( 3 ) );
236  url.setPass( static_cast<QX500ListViewItem *>( item )->password() );
237  kdDebug() << url << endl;
238  lst << url;
239  }
240  return lst;
241 }
242 
243 void DirectoryServicesWidget::clear()
244 {
245  x500LV->clear();
246  emit changed();
247 }
248 
249 void DirectoryServicesWidget::load()
250 {
251  if ( mConfigEntry ) {
252  setInitialServices( mConfigEntry->urlValueList() );
253  }
254 }
255 
256 void DirectoryServicesWidget::save()
257 {
258  if ( mConfigEntry ) {
259  mConfigEntry->setURLValueList( urlList() );
260  }
261 }
262 
263 void DirectoryServicesWidget::defaults()
264 {
265  if ( mConfigEntry ) {
266  // resetToDefault doesn't work since gpgconf doesn't know any defaults for this entry.
267  //mConfigEntry->resetToDefault();
268  //load();
269  clear(); // the default is an empty list.
270  }
271 }
272 
273 static void swapItems( QX500ListViewItem *item, QX500ListViewItem *other )
274 {
275  TQString serverName = item->text( 0 );
276  TQString portNumber = item->text( 1 );
277  TQString dn = item->text( 2 );
278  TQString username = item->text( 3 );
279  TQString password = item->password();
280  item->copyItem( other );
281  other->setData( serverName, portNumber, dn, username, password );
282 }
283 
284 void Kleo::DirectoryServicesWidget::slotMoveUp()
285 {
286  QX500ListViewItem *item = static_cast<QX500ListViewItem *>( x500LV->selectedItem() );
287  if ( !item ) return;
288  QX500ListViewItem *above = static_cast<QX500ListViewItem *>( item->itemAbove() );
289  if ( !above ) return;
290  swapItems( item, above );
291  x500LV->setCurrentItem( above );
292  x500LV->setSelected( above, true );
293  emit changed();
294 }
295 
296 void Kleo::DirectoryServicesWidget::slotMoveDown()
297 {
298  QX500ListViewItem *item = static_cast<QX500ListViewItem *>( x500LV->selectedItem() );
299  if ( !item ) return;
300  QX500ListViewItem *below = static_cast<QX500ListViewItem *>( item->itemBelow() );
301  if ( !below ) return;
302  swapItems( item, below );
303  x500LV->setCurrentItem( below );
304  x500LV->setSelected( below, true );
305  emit changed();
306 }
307 
308 #include "directoryserviceswidget.moc"
This class provides C++ access to the CRYPTPLUG API.
Description of a single option.
Definition: cryptoconfig.h:49
C++ wrapper for the CRYPTPLUG library API.
bool hasFeature(::Feature)
This function returns true if the specified feature is available in the plugin, and false otherwise.