kaddressbook Library API Documentation

ldapoptionswidget.cpp

00001 /* 00002 This file is part of KAddressBook. 00003 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 00019 As a special exception, permission is given to link this program 00020 with any edition of Qt, and distribute the resulting executable, 00021 without including the source code for Qt in the source distribution. 00022 */ 00023 00024 #include <qgroupbox.h> 00025 #include <qheader.h> 00026 #include <qlabel.h> 00027 #include <qlayout.h> 00028 #include <qpushbutton.h> 00029 #include <qtoolbutton.h> 00030 #include <qstring.h> 00031 00032 #include <kabc/addresslineedit.h> 00033 #include <kapplication.h> 00034 #include <kbuttonbox.h> 00035 #include <kconfig.h> 00036 #include <klistview.h> 00037 #include <klocale.h> 00038 00039 #include "addhostdialog.h" 00040 #include "ldapoptionswidget.h" 00041 #include <qvgroupbox.h> 00042 #include <qhbox.h> 00043 #include <qvbox.h> 00044 #include <kiconloader.h> 00045 00046 class LDAPServer 00047 { 00048 public: 00049 LDAPServer() : mPort( 389 ) {} 00050 LDAPServer( const QString &host, int port, const QString &baseDN, 00051 const QString &bindDN, const QString &pwdBindDN ) 00052 : mHost( host ), mPort( port ), mBaseDN( baseDN ), mBindDN( bindDN ), 00053 mPwdBindDN( pwdBindDN ) 00054 { } 00055 00056 QString host() const { return mHost; } 00057 int port() const { return mPort; } 00058 QString baseDN() const { return mBaseDN; } 00059 QString bindDN() const { return mBindDN; } 00060 QString pwdBindDN() const { return mPwdBindDN; } 00061 00062 void setHost( const QString &host ) { mHost = host; } 00063 void setPort( int port ) { mPort = port; } 00064 void setBaseDN( const QString &baseDN ) { mBaseDN = baseDN; } 00065 void setBindDN( const QString &bindDN ) { mBindDN = bindDN; } 00066 void setPwdBindDN( const QString &pwdBindDN ) { mPwdBindDN = pwdBindDN; } 00067 00068 private: 00069 QString mHost; 00070 int mPort; 00071 QString mBaseDN; 00072 QString mBindDN; 00073 QString mPwdBindDN; 00074 }; 00075 00076 class LDAPItem : public QCheckListItem 00077 { 00078 public: 00079 LDAPItem( QListView *parent, const LDAPServer &server, bool isActive = false ) 00080 : QCheckListItem( parent, parent->lastItem(), QString::null, QCheckListItem::CheckBox ), 00081 mIsActive( isActive ) 00082 { 00083 setServer( server ); 00084 } 00085 00086 void setServer( const LDAPServer &server ) 00087 { 00088 mServer = server; 00089 00090 setText( 0, mServer.host() ); 00091 } 00092 00093 LDAPServer server() const { return mServer; } 00094 00095 void setIsActive( bool isActive ) { mIsActive = isActive; } 00096 bool isActive() const { return mIsActive; } 00097 00098 private: 00099 LDAPServer mServer; 00100 bool mIsActive; 00101 }; 00102 00103 LDAPOptionsWidget::LDAPOptionsWidget( QWidget* parent, const char* name ) 00104 : QWidget( parent, name ) 00105 { 00106 initGUI(); 00107 00108 mHostListView->setSorting( -1 ); 00109 mHostListView->addColumn( QString::null ); 00110 mHostListView->header()->hide(); 00111 00112 connect( mHostListView, SIGNAL( selectionChanged( QListViewItem* ) ), 00113 SLOT( slotSelectionChanged( QListViewItem* ) ) ); 00114 connect( mHostListView, SIGNAL(doubleClicked( QListViewItem *, const QPoint &, int )), this, SLOT(slotEditHost())); 00115 connect( mHostListView, SIGNAL( clicked( QListViewItem* ) ), 00116 SLOT( slotItemClicked( QListViewItem* ) ) ); 00117 00118 connect( mUpButton, SIGNAL( clicked() ), this, SLOT( slotMoveUp() ) ); 00119 connect( mDownButton, SIGNAL( clicked() ), this, SLOT( slotMoveDown() ) ); 00120 } 00121 00122 LDAPOptionsWidget::~LDAPOptionsWidget() 00123 { 00124 } 00125 00126 void LDAPOptionsWidget::slotSelectionChanged( QListViewItem *item ) 00127 { 00128 bool state = ( item != 0 ); 00129 mEditButton->setEnabled( state ); 00130 mRemoveButton->setEnabled( state ); 00131 mDownButton->setEnabled( item && item->itemBelow() ); 00132 mUpButton->setEnabled( item && item->itemAbove() ); 00133 } 00134 00135 void LDAPOptionsWidget::slotItemClicked( QListViewItem *item ) 00136 { 00137 LDAPItem *ldapItem = dynamic_cast<LDAPItem*>( item ); 00138 if ( !ldapItem ) 00139 return; 00140 00141 if ( ldapItem->isOn() != ldapItem->isActive() ) { 00142 emit changed( true ); 00143 ldapItem->setIsActive( ldapItem->isOn() ); 00144 } 00145 } 00146 00147 void LDAPOptionsWidget::slotAddHost() 00148 { 00149 AddHostDialog dlg( this ); 00150 00151 if ( dlg.exec() && !dlg.host().isEmpty() ) { 00152 LDAPServer server( dlg.host(), dlg.port(), dlg.baseDN(), 00153 dlg.bindDN(), dlg.pwdBindDN() ); 00154 new LDAPItem( mHostListView, server ); 00155 00156 emit changed( true ); 00157 } 00158 } 00159 00160 void LDAPOptionsWidget::slotEditHost() 00161 { 00162 LDAPItem *item = dynamic_cast<LDAPItem*>( mHostListView->currentItem() ); 00163 if ( !item ) 00164 return; 00165 00166 AddHostDialog dlg( this ); 00167 dlg.setCaption( i18n( "Edit Host" ) ); 00168 00169 dlg.setHost( item->server().host() ); 00170 dlg.setPort( item->server().port() ); 00171 dlg.setBaseDN( item->server().baseDN() ); 00172 dlg.setBindDN( item->server().bindDN() ); 00173 dlg.setPwdBindDN( item->server().pwdBindDN() ); 00174 00175 if ( dlg.exec() && !dlg.host().isEmpty() ) { 00176 LDAPServer server( dlg.host(), dlg.port(), dlg.baseDN(), 00177 dlg.bindDN(), dlg.pwdBindDN() ); 00178 item->setServer( server ); 00179 00180 emit changed( true ); 00181 } 00182 } 00183 00184 void LDAPOptionsWidget::slotRemoveHost() 00185 { 00186 QListViewItem *item = mHostListView->currentItem(); 00187 if ( !item ) 00188 return; 00189 00190 mHostListView->takeItem( item ); 00191 delete item; 00192 00193 slotSelectionChanged( mHostListView->currentItem() ); 00194 00195 emit changed( true ); 00196 } 00197 00198 static void swapItems( LDAPItem *item, LDAPItem *other ) 00199 { 00200 LDAPServer server = item->server(); 00201 bool isActive = item->isActive(); 00202 item->setServer( other->server() ); 00203 item->setIsActive( other->isActive() ); 00204 other->setServer( server ); 00205 other->setIsActive( isActive ); 00206 } 00207 00208 void LDAPOptionsWidget::slotMoveUp() 00209 { 00210 LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItem() ); 00211 if ( !item ) return; 00212 LDAPItem *above = static_cast<LDAPItem *>( item->itemAbove() ); 00213 if ( !above ) return; 00214 swapItems( item, above ); 00215 mHostListView->setCurrentItem( above ); 00216 mHostListView->setSelected( above, true ); 00217 emit changed( true ); 00218 } 00219 00220 void LDAPOptionsWidget::slotMoveDown() 00221 { 00222 LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItem() ); 00223 if ( !item ) return; 00224 LDAPItem *below = static_cast<LDAPItem *>( item->itemBelow() ); 00225 if ( !below ) return; 00226 swapItems( item, below ); 00227 mHostListView->setCurrentItem( below ); 00228 mHostListView->setSelected( below, true ); 00229 emit changed( true ); 00230 } 00231 00232 void LDAPOptionsWidget::restoreSettings() 00233 { 00234 mHostListView->clear(); 00235 KConfig *config = KABC::AddressLineEdit::config(); 00236 KConfigGroupSaver saver( config, "LDAP" ); 00237 00238 QString host; 00239 00240 uint count = config->readUnsignedNumEntry( "NumSelectedHosts"); 00241 for ( uint i = 0; i < count; ++i ) { 00242 LDAPServer server; 00243 server.setHost( config->readEntry( QString( "SelectedHost%1").arg( i ) ) ); 00244 server.setPort( config->readUnsignedNumEntry( QString( "SelectedPort%1" ).arg( i ) ) ); 00245 server.setBaseDN( config->readEntry( QString( "SelectedBase%1" ).arg( i ) ) ); 00246 server.setBindDN( config->readEntry( QString( "SelectedBind%1" ).arg( i ) ) ); 00247 server.setPwdBindDN( config->readEntry( QString( "SelectedPwdBind%1" ).arg( i ) ) ); 00248 00249 LDAPItem *item = new LDAPItem( mHostListView, server, true ); 00250 item->setOn( true ); 00251 } 00252 00253 count = config->readUnsignedNumEntry( "NumHosts" ); 00254 for ( uint i = 0; i < count; ++i ) { 00255 LDAPServer server; 00256 server.setHost( config->readEntry( QString( "Host%1" ).arg( i ) ) ); 00257 server.setPort( config->readUnsignedNumEntry( QString( "Port%1" ).arg( i ) ) ); 00258 server.setBaseDN( config->readEntry( QString( "Base%1" ).arg( i ) ) ); 00259 server.setBindDN( config->readEntry( QString( "Bind%1" ).arg( i ) ) ); 00260 server.setPwdBindDN( config->readEntry( QString( "PwdBind%1" ).arg( i ) ) ); 00261 00262 new LDAPItem( mHostListView, server ); 00263 } 00264 00265 emit changed( false ); 00266 } 00267 00268 void LDAPOptionsWidget::saveSettings() 00269 { 00270 KConfig *config = KABC::AddressLineEdit::config(); 00271 config->deleteGroup( "LDAP" ); 00272 00273 KConfigGroupSaver saver( config, "LDAP" ); 00274 00275 uint selected = 0; uint unselected = 0; 00276 QListViewItemIterator it( mHostListView ); 00277 for ( ; it.current(); ++it ) { 00278 LDAPItem *item = dynamic_cast<LDAPItem*>( it.current() ); 00279 if ( !item ) 00280 continue; 00281 00282 LDAPServer server = item->server(); 00283 if ( item->isOn() ) { 00284 config->writeEntry( QString( "SelectedHost%1" ).arg( selected ), server.host() ); 00285 config->writeEntry( QString( "SelectedPort%1" ).arg( selected ), server.port() ); 00286 config->writeEntry( QString( "SelectedBase%1" ).arg( selected ), server.baseDN() ); 00287 config->writeEntry( QString( "SelectedBind%1" ).arg( selected ), server.bindDN() ); 00288 config->writeEntry( QString( "SelectedPwdBind%1" ).arg( selected ), server.pwdBindDN() ); 00289 selected++; 00290 } else { 00291 config->writeEntry( QString( "Host%1" ).arg( unselected ), server.host() ); 00292 config->writeEntry( QString( "Port%1" ).arg( unselected ), server.port() ); 00293 config->writeEntry( QString( "Base%1" ).arg( unselected ), server.baseDN() ); 00294 config->writeEntry( QString( "Bind%1" ).arg( unselected ), server.bindDN() ); 00295 config->writeEntry( QString( "PwdBind%1" ).arg( unselected ), server.pwdBindDN() ); 00296 unselected++; 00297 } 00298 } 00299 00300 config->writeEntry( "NumSelectedHosts", selected ); 00301 config->writeEntry( "NumHosts", unselected ); 00302 config->sync(); 00303 00304 emit changed( false ); 00305 } 00306 00307 void LDAPOptionsWidget::defaults() 00308 { 00309 // add default configuration here 00310 } 00311 00312 void LDAPOptionsWidget::initGUI() 00313 { 00314 QVBoxLayout *layout = new QVBoxLayout( this, 0, KDialog::spacingHint() ); 00315 00316 QVGroupBox *groupBox = new QVGroupBox( i18n( "LDAP Servers" ), this ); 00317 groupBox->setInsideSpacing( KDialog::spacingHint() ); 00318 groupBox->setInsideMargin( KDialog::marginHint() ); 00319 00320 // Contents of the QVGroupBox: label and hbox 00321 /*QLabel *label =*/ new QLabel( i18n( "Check all servers that should be used:" ), groupBox ); 00322 00323 QHBox* hBox = new QHBox( groupBox ); 00324 // Contents of the hbox: listview and up/down buttons on the right (vbox) 00325 mHostListView = new KListView( hBox ); 00326 00327 QVBox* upDownBox = new QVBox( hBox ); 00328 mUpButton = new QToolButton( upDownBox, "mUpButton" ); 00329 mUpButton->setPixmap( BarIcon( "up", KIcon::SizeSmall ) ); 00330 mUpButton->setEnabled( false ); // b/c no item is selected yet 00331 00332 mDownButton = new QToolButton( upDownBox, "mDownButton" ); 00333 mDownButton->setPixmap( BarIcon( "down", KIcon::SizeSmall ) ); 00334 mDownButton->setEnabled( false ); // b/c no item is selected yet 00335 00336 QWidget* spacer = new QWidget( upDownBox ); 00337 upDownBox->setStretchFactor( spacer, 100 ); 00338 00339 layout->addWidget( groupBox ); 00340 00341 KButtonBox *buttons = new KButtonBox( this ); 00342 buttons->addButton( i18n( "&Add Host..." ), this, SLOT( slotAddHost() ) ); 00343 mEditButton = buttons->addButton( i18n( "&Edit Host..." ), this, SLOT( slotEditHost() ) ); 00344 mEditButton->setEnabled( false ); 00345 mRemoveButton = buttons->addButton( i18n( "&Remove Host" ), this, SLOT( slotRemoveHost() ) ); 00346 mRemoveButton->setEnabled( false ); 00347 buttons->layout(); 00348 00349 layout->addWidget( buttons ); 00350 00351 resize( QSize( 460, 300 ).expandedTo( sizeHint() ) ); 00352 } 00353 00354 #include "ldapoptionswidget.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Oct 1 15:19:05 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003