directoryserviceswidget.cpp
00001 /* 00002 directoryserviceswidget.cpp 00003 00004 This file is part of Kleopatra, the KDE keymanager 00005 Copyright (c) 2001,2002,2004 Klarälvdalens Datakonsult AB 00006 00007 Kleopatra is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 2 of the License, or 00010 (at your option) any later version. 00011 00012 Kleopatra is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 00021 In addition, as a special exception, the copyright holders give 00022 permission to link the code of this program with any edition of 00023 the TQt library by Trolltech AS, Norway (or with modified versions 00024 of TQt that use the same license as TQt), and distribute linked 00025 combinations including the two. You must obey the GNU General 00026 Public License in all respects for all of the code used other than 00027 TQt. If you modify this file, you may extend this exception to 00028 your version of the file, but you are not obligated to do so. If 00029 you do not wish to do so, delete this exception statement from 00030 your version. 00031 */ 00032 00033 #include <config.h> 00034 00035 #include "directoryserviceswidget.h" 00036 #include "adddirectoryservicedialogimpl.h" 00037 #include "cryptplugwrapper.h" 00038 00039 #include <klineedit.h> 00040 #include <kleo/cryptoconfig.h> 00041 #include <kiconloader.h> 00042 #include <kdebug.h> 00043 00044 #include <tqbuttongroup.h> 00045 #include <tqtoolbutton.h> 00046 #include <tqlistview.h> 00047 #include <tqpushbutton.h> 00048 00049 using namespace Kleo; 00050 00051 class QX500ListViewItem : public TQListViewItem 00052 { 00053 public: 00054 QX500ListViewItem( TQListView* lv, TQListViewItem* prev, 00055 const TQString& serverName, 00056 const TQString& portNumber, 00057 const TQString& dn, 00058 const TQString& username, 00059 const TQString& password ) 00060 : TQListViewItem( lv, prev, serverName, portNumber, dn, username ) { 00061 setPassword( password ); 00062 } 00063 00064 void setPassword( const TQString& pass ) { 00065 mPassword = pass; 00066 setText( 4, pass.isEmpty() ? TQString() : TQString::fromLatin1( "******" ) ); 00067 } 00068 00069 const TQString& password() const { return mPassword; } 00070 00071 void setData( const TQString& serverName, 00072 const TQString& portNumber, 00073 const TQString& dn, 00074 const TQString& username, 00075 const TQString& password ) { 00076 setText( 0, serverName ); 00077 setText( 1, portNumber ); 00078 setText( 2, dn ); 00079 setText( 3, username ); 00080 setPassword( password ); 00081 } 00082 00083 void copyItem( QX500ListViewItem* item ) { 00084 for ( unsigned int i = 0; i < 4 ; ++i ) 00085 setText( i, item->text( i ) ); 00086 setPassword( item->password() ); 00087 } 00088 00089 private: 00090 TQString mPassword; 00091 }; 00092 00093 Kleo::DirectoryServicesWidget::DirectoryServicesWidget( 00094 Kleo::CryptoConfigEntry* configEntry, 00095 TQWidget* parent, const char* name, WFlags fl ) 00096 : DirectoryServicesWidgetBase( parent, name, fl ), 00097 mConfigEntry( configEntry ) 00098 { 00099 x500LV->setSorting( -1 ); 00100 00101 // taken from kmail's configuredialog.cpp 00102 upButton->setIconSet( BarIconSet( "go-up", TDEIcon::SizeSmall ) ); 00103 upButton->setEnabled( false ); // b/c no item is selected yet 00104 00105 downButton->setIconSet( BarIconSet( "go-down", TDEIcon::SizeSmall ) ); 00106 downButton->setEnabled( false ); // b/c no item is selected yet 00107 } 00108 00109 00110 /* 00111 * Destroys the object and frees any allocated resources 00112 */ 00113 DirectoryServicesWidget::~DirectoryServicesWidget() 00114 { 00115 // no need to delete child widgets, TQt does it all for us 00116 } 00117 00118 00123 void DirectoryServicesWidget::enableDisable( CryptPlugWrapper* cryptPlug ) // unused? 00124 { 00125 // disable the whole page if the plugin does not support the use 00126 // of directory services 00127 setEnabled( cryptPlug->hasFeature( Feature_CertificateDirectoryService ) || 00128 cryptPlug->hasFeature( Feature_CRLDirectoryService ) ); 00129 } 00130 00131 00132 /* 00133 * protected slot, connected to selectionChanged() 00134 */ 00135 void DirectoryServicesWidget::slotServiceChanged( TQListViewItem* item ) 00136 { 00137 if( item ) 00138 removeServicePB->setEnabled( true ); 00139 else 00140 removeServicePB->setEnabled( false ); 00141 downButton->setEnabled( item && item->itemBelow() ); 00142 upButton->setEnabled( item && item->itemAbove() ); 00143 } 00144 00145 00146 /* 00147 * protected slot, connected to returnPressed/doubleClicked 00148 */ 00149 void DirectoryServicesWidget::slotServiceSelected( TQListViewItem* item ) 00150 { 00151 AddDirectoryServiceDialogImpl* dlg = new AddDirectoryServiceDialogImpl( this ); 00152 dlg->serverNameED->setText( item->text( 0 ) ); 00153 dlg->portED->setText( item->text( 1 ) ); 00154 dlg->descriptionED->setText( item->text( 2 ) ); 00155 dlg->usernameED->setText( item->text( 3 ) ); 00156 TQString pass = static_cast<QX500ListViewItem *>( item )->password(); 00157 dlg->passwordED->setText( pass ); 00158 00159 if( dlg->exec() == TQDialog::Accepted ) { 00160 item->setText( 0, dlg->serverNameED->text() ); 00161 item->setText( 1, dlg->portED->text() ); 00162 item->setText( 2, dlg->descriptionED->text() ); 00163 item->setText( 3, dlg->usernameED->text() ); 00164 static_cast<QX500ListViewItem *>( item )->setPassword( dlg->passwordED->text() ); 00165 emit changed(); 00166 } 00167 delete dlg; 00168 } 00169 00170 00171 /* 00172 * protected slot 00173 */ 00174 void DirectoryServicesWidget::slotAddService() 00175 { 00176 AddDirectoryServiceDialogImpl* dlg = new AddDirectoryServiceDialogImpl( this ); 00177 if( dlg->exec() == TQDialog::Accepted ) { 00178 QX500ListViewItem *item = new QX500ListViewItem( x500LV, x500LV->lastItem(), 00179 dlg->serverNameED->text(), 00180 dlg->portED->text(), 00181 dlg->descriptionED->text(), 00182 dlg->usernameED->text(), 00183 dlg->passwordED->text() ); 00184 slotServiceChanged(item); 00185 emit changed(); 00186 } 00187 delete dlg; 00188 } 00189 00190 /* 00191 * protected slot 00192 */ 00193 void DirectoryServicesWidget::slotDeleteService() 00194 { 00195 TQListViewItem* item = x500LV->selectedItem(); 00196 Q_ASSERT( item ); 00197 if( !item ) 00198 return; 00199 else 00200 delete item; 00201 x500LV->triggerUpdate(); 00202 item = x500LV->currentItem(); 00203 x500LV->setCurrentItem( item ); // seems necessary... 00204 x500LV->setSelected( item, true ); 00205 emit changed(); 00206 } 00207 00208 00209 void DirectoryServicesWidget::setInitialServices( const KURL::List& urls ) 00210 { 00211 x500LV->clear(); 00212 for( KURL::List::const_iterator it = urls.begin(); it != urls.end(); ++it ) { 00213 TQString dn = KURL::decode_string( (*it).query().mid( 1 ) ); // decode query and skip leading '?' 00214 (void)new QX500ListViewItem( x500LV, x500LV->lastItem(), 00215 (*it).host(), 00216 TQString::number( (*it).port() ), 00217 dn, 00218 (*it).user(), 00219 (*it).pass()); 00220 } 00221 } 00222 00223 KURL::List DirectoryServicesWidget::urlList() const 00224 { 00225 KURL::List lst; 00226 TQListViewItemIterator it( x500LV ); 00227 for ( ; it.current() ; ++it ) { 00228 TQListViewItem* item = it.current(); 00229 KURL url; 00230 url.setProtocol( "ldap" ); 00231 url.setHost( item->text( 0 ) ); 00232 url.setPort( item->text( 1 ).toInt() ); 00233 url.setPath( "/" ); // workaround KURL parsing bug 00234 url.setQuery( item->text( 2 ) ); 00235 url.setUser( item->text( 3 ) ); 00236 url.setPass( static_cast<QX500ListViewItem *>( item )->password() ); 00237 kdDebug() << url << endl; 00238 lst << url; 00239 } 00240 return lst; 00241 } 00242 00243 void DirectoryServicesWidget::clear() 00244 { 00245 x500LV->clear(); 00246 emit changed(); 00247 } 00248 00249 void DirectoryServicesWidget::load() 00250 { 00251 if ( mConfigEntry ) { 00252 setInitialServices( mConfigEntry->urlValueList() ); 00253 } 00254 } 00255 00256 void DirectoryServicesWidget::save() 00257 { 00258 if ( mConfigEntry ) { 00259 mConfigEntry->setURLValueList( urlList() ); 00260 } 00261 } 00262 00263 void DirectoryServicesWidget::defaults() 00264 { 00265 if ( mConfigEntry ) { 00266 // resetToDefault doesn't work since gpgconf doesn't know any defaults for this entry. 00267 //mConfigEntry->resetToDefault(); 00268 //load(); 00269 clear(); // the default is an empty list. 00270 } 00271 } 00272 00273 static void swapItems( QX500ListViewItem *item, QX500ListViewItem *other ) 00274 { 00275 TQString serverName = item->text( 0 ); 00276 TQString portNumber = item->text( 1 ); 00277 TQString dn = item->text( 2 ); 00278 TQString username = item->text( 3 ); 00279 TQString password = item->password(); 00280 item->copyItem( other ); 00281 other->setData( serverName, portNumber, dn, username, password ); 00282 } 00283 00284 void Kleo::DirectoryServicesWidget::slotMoveUp() 00285 { 00286 QX500ListViewItem *item = static_cast<QX500ListViewItem *>( x500LV->selectedItem() ); 00287 if ( !item ) return; 00288 QX500ListViewItem *above = static_cast<QX500ListViewItem *>( item->itemAbove() ); 00289 if ( !above ) return; 00290 swapItems( item, above ); 00291 x500LV->setCurrentItem( above ); 00292 x500LV->setSelected( above, true ); 00293 emit changed(); 00294 } 00295 00296 void Kleo::DirectoryServicesWidget::slotMoveDown() 00297 { 00298 QX500ListViewItem *item = static_cast<QX500ListViewItem *>( x500LV->selectedItem() ); 00299 if ( !item ) return; 00300 QX500ListViewItem *below = static_cast<QX500ListViewItem *>( item->itemBelow() ); 00301 if ( !below ) return; 00302 swapItems( item, below ); 00303 x500LV->setCurrentItem( below ); 00304 x500LV->setSelected( below, true ); 00305 emit changed(); 00306 } 00307 00308 #include "directoryserviceswidget.moc"