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   item->setOn( other->isActive() );
00205   other->setServer( server );
00206   other->setIsActive( isActive );
00207   other->setOn( isActive );
00208 }
00209 
00210 void LDAPOptionsWidget::slotMoveUp()
00211 {
00212   LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItem() );
00213   if ( !item ) return;
00214   LDAPItem *above = static_cast<LDAPItem *>( item->itemAbove() );
00215   if ( !above ) return;
00216   swapItems( item, above );
00217   mHostListView->setCurrentItem( above );
00218   mHostListView->setSelected( above, true );
00219   emit changed( true );
00220 }
00221 
00222 void LDAPOptionsWidget::slotMoveDown()
00223 {
00224   LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItem() );
00225   if ( !item ) return;
00226   LDAPItem *below = static_cast<LDAPItem *>( item->itemBelow() );
00227   if ( !below ) return;
00228   swapItems( item, below );
00229   mHostListView->setCurrentItem( below );
00230   mHostListView->setSelected( below, true );
00231   emit changed( true );
00232 }
00233 
00234 void LDAPOptionsWidget::restoreSettings()
00235 {
00236   mHostListView->clear();
00237   KConfig *config = KABC::AddressLineEdit::config();
00238   KConfigGroupSaver saver( config, "LDAP" );
00239 
00240   QString host;
00241 
00242   uint count = config->readUnsignedNumEntry( "NumSelectedHosts");
00243   for ( uint i = 0; i < count; ++i ) {
00244     LDAPServer server;
00245     server.setHost( config->readEntry( QString( "SelectedHost%1").arg( i ) ) );
00246     server.setPort( config->readUnsignedNumEntry( QString( "SelectedPort%1" ).arg( i ) ) );
00247     server.setBaseDN( config->readEntry( QString( "SelectedBase%1" ).arg( i ) ) );
00248     server.setBindDN( config->readEntry( QString( "SelectedBind%1" ).arg( i ) ) );
00249     server.setPwdBindDN( config->readEntry( QString( "SelectedPwdBind%1" ).arg( i ) ) );
00250 
00251     LDAPItem *item = new LDAPItem( mHostListView, server, true );
00252     item->setOn( true );
00253   }
00254 
00255   count = config->readUnsignedNumEntry( "NumHosts" );
00256   for ( uint i = 0; i < count; ++i ) {
00257     LDAPServer server;
00258     server.setHost( config->readEntry( QString( "Host%1" ).arg( i ) ) );
00259     server.setPort( config->readUnsignedNumEntry( QString( "Port%1" ).arg( i ) ) );
00260     server.setBaseDN( config->readEntry( QString( "Base%1" ).arg( i ) ) );
00261     server.setBindDN( config->readEntry( QString( "Bind%1" ).arg( i ) ) );
00262     server.setPwdBindDN( config->readEntry( QString( "PwdBind%1" ).arg( i ) ) );
00263 
00264     new LDAPItem( mHostListView, server );
00265   }
00266 
00267   emit changed( false );
00268 }
00269 
00270 void LDAPOptionsWidget::saveSettings()
00271 {
00272   KConfig *config = KABC::AddressLineEdit::config();
00273   config->deleteGroup( "LDAP" );
00274 
00275   KConfigGroupSaver saver( config, "LDAP" );
00276 
00277   uint selected = 0; uint unselected = 0;
00278   QListViewItemIterator it( mHostListView );
00279   for ( ; it.current(); ++it ) {
00280     LDAPItem *item = dynamic_cast<LDAPItem*>( it.current() );
00281     if ( !item )
00282       continue;
00283 
00284     LDAPServer server = item->server();
00285     if ( item->isOn() ) {
00286       config->writeEntry( QString( "SelectedHost%1" ).arg( selected ), server.host() );
00287       config->writeEntry( QString( "SelectedPort%1" ).arg( selected ), server.port() );
00288       config->writeEntry( QString( "SelectedBase%1" ).arg( selected ), server.baseDN() );
00289       config->writeEntry( QString( "SelectedBind%1" ).arg( selected ), server.bindDN() );
00290       config->writeEntry( QString( "SelectedPwdBind%1" ).arg( selected ), server.pwdBindDN() );
00291       selected++;
00292     } else {
00293       config->writeEntry( QString( "Host%1" ).arg( unselected ), server.host() );
00294       config->writeEntry( QString( "Port%1" ).arg( unselected ), server.port() );
00295       config->writeEntry( QString( "Base%1" ).arg( unselected ), server.baseDN() );
00296       config->writeEntry( QString( "Bind%1" ).arg( unselected ), server.bindDN() );
00297       config->writeEntry( QString( "PwdBind%1" ).arg( unselected ), server.pwdBindDN() );
00298       unselected++;
00299     }
00300   }
00301 
00302   config->writeEntry( "NumSelectedHosts", selected );
00303   config->writeEntry( "NumHosts", unselected );
00304   config->sync();
00305 
00306   emit changed( false );
00307 }
00308 
00309 void LDAPOptionsWidget::defaults()
00310 {
00311   // add default configuration here
00312 }
00313 
00314 void LDAPOptionsWidget::initGUI()
00315 {
00316   QVBoxLayout *layout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
00317 
00318   QVGroupBox *groupBox = new QVGroupBox( i18n( "LDAP Servers" ), this );
00319   groupBox->setInsideSpacing( KDialog::spacingHint() );
00320   groupBox->setInsideMargin( KDialog::marginHint() );
00321 
00322   // Contents of the QVGroupBox: label and hbox
00323   /*QLabel *label =*/ new QLabel( i18n( "Check all servers that should be used:" ), groupBox );
00324 
00325   QHBox* hBox = new QHBox( groupBox );
00326   // Contents of the hbox: listview and up/down buttons on the right (vbox)
00327   mHostListView = new KListView( hBox );
00328 
00329   QVBox* upDownBox = new QVBox( hBox );
00330   mUpButton = new QToolButton( upDownBox, "mUpButton" );
00331   mUpButton->setPixmap( BarIcon( "up", KIcon::SizeSmall ) );
00332   mUpButton->setEnabled( false ); // b/c no item is selected yet
00333 
00334   mDownButton = new QToolButton( upDownBox, "mDownButton" );
00335   mDownButton->setPixmap( BarIcon( "down", KIcon::SizeSmall ) );
00336   mDownButton->setEnabled( false ); // b/c no item is selected yet
00337 
00338   QWidget* spacer = new QWidget( upDownBox );
00339   upDownBox->setStretchFactor( spacer, 100 );
00340 
00341   layout->addWidget( groupBox );
00342 
00343   KButtonBox *buttons = new KButtonBox( this );
00344   buttons->addButton( i18n( "&Add Host..." ), this, SLOT( slotAddHost() ) );
00345   mEditButton = buttons->addButton( i18n( "&Edit Host..." ), this, SLOT( slotEditHost() ) );
00346   mEditButton->setEnabled( false );
00347   mRemoveButton = buttons->addButton( i18n( "&Remove Host" ), this, SLOT( slotRemoveHost() ) );
00348   mRemoveButton->setEnabled( false );
00349   buttons->layout();
00350 
00351   layout->addWidget( buttons );
00352 
00353   resize( QSize( 460, 300 ).expandedTo( sizeHint() ) );
00354 }
00355 
00356 #include "ldapoptionswidget.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 22:42:47 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003