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 <qstring.h>
00030 
00031 #include <kabc/addresslineedit.h>
00032 #include <kapplication.h>
00033 #include <kbuttonbox.h>
00034 #include <kconfig.h>
00035 #include <klistview.h>
00036 #include <klocale.h>
00037 
00038 #include "addhostdialog.h"
00039 #include "ldapoptionswidget.h"
00040 
00041 class LDAPServer
00042 {
00043   public:
00044     LDAPServer() : mPort( 389 ) {}
00045     LDAPServer( const QString &host, int port, const QString &baseDN,
00046                 const QString &bindDN, const QString &pwdBindDN )
00047       : mHost( host ), mPort( port ), mBaseDN( baseDN ), mBindDN( bindDN ),
00048         mPwdBindDN( pwdBindDN )
00049     { }
00050 
00051     QString host() const { return mHost; }
00052     int port() const { return mPort; }
00053     QString baseDN() const { return mBaseDN; }
00054     QString bindDN() const { return mBindDN; }
00055     QString pwdBindDN() const { return mPwdBindDN; }
00056 
00057     void setHost( const QString &host ) { mHost = host; }
00058     void setPort( int port ) { mPort = port; }
00059     void setBaseDN( const QString &baseDN ) {  mBaseDN = baseDN; }
00060     void setBindDN( const QString &bindDN ) {  mBindDN = bindDN; }
00061     void setPwdBindDN( const QString &pwdBindDN ) {  mPwdBindDN = pwdBindDN; }
00062 
00063   private:
00064     QString mHost;
00065     int mPort;
00066     QString mBaseDN;
00067     QString mBindDN;
00068     QString mPwdBindDN;
00069 };
00070 
00071 class LDAPItem : public QCheckListItem
00072 {
00073   public:
00074     LDAPItem( QListView *parent, const LDAPServer &server, bool isActive = false )
00075       : QCheckListItem( parent, QString::null, QCheckListItem::CheckBox ),
00076         mIsActive( isActive )
00077     {
00078       setServer( server );
00079     }
00080 
00081     void setServer( const LDAPServer &server )
00082     {
00083       mServer = server;
00084 
00085       setText( 0, mServer.host() );
00086     }
00087 
00088     LDAPServer server() const { return mServer; }
00089 
00090     void setIsActive( bool isActive ) { mIsActive = isActive; }
00091     bool isActive() const { return mIsActive; }
00092 
00093   private:
00094     LDAPServer mServer;
00095     bool mIsActive;
00096 };
00097 
00098 LDAPOptionsWidget::LDAPOptionsWidget( QWidget* parent,  const char* name )
00099   : QWidget( parent, name )
00100 {
00101   initGUI();
00102 
00103   mHostListView->addColumn( QString::null );
00104   mHostListView->header()->hide();
00105 
00106   connect( mHostListView, SIGNAL( selectionChanged( QListViewItem* ) ),
00107            SLOT( slotSelectionChanged( QListViewItem* ) ) );
00108   connect( mHostListView, SIGNAL(doubleClicked( QListViewItem *, const QPoint &, int )), this, SLOT(slotEditHost()));
00109   connect( mHostListView, SIGNAL( clicked( QListViewItem* ) ),
00110            SLOT( slotItemClicked( QListViewItem* ) ) );
00111 }
00112 
00113 LDAPOptionsWidget::~LDAPOptionsWidget()
00114 {
00115 }
00116 
00117 void LDAPOptionsWidget::slotSelectionChanged( QListViewItem *item )
00118 {
00119   bool state = ( item != 0 );
00120 
00121   mEditButton->setEnabled( state );
00122   mRemoveButton->setEnabled( state );
00123 }
00124 
00125 void LDAPOptionsWidget::slotItemClicked( QListViewItem *item )
00126 {
00127   LDAPItem *ldapItem = dynamic_cast<LDAPItem*>( item );
00128   if ( !ldapItem )
00129     return;
00130 
00131   if ( ldapItem->isOn() != ldapItem->isActive() ) {
00132     emit changed( true );
00133     ldapItem->setIsActive( ldapItem->isOn() );
00134   }
00135 }
00136 
00137 void LDAPOptionsWidget::slotAddHost()
00138 {
00139   AddHostDialog dlg( this );
00140 
00141   if ( dlg.exec() && !dlg.host().isEmpty() ) {
00142     LDAPServer server( dlg.host(), dlg.port(), dlg.baseDN(),
00143                        dlg.bindDN(), dlg.pwdBindDN() );
00144     new LDAPItem( mHostListView, server );
00145 
00146     emit changed( true );
00147   }
00148 }
00149 
00150 void LDAPOptionsWidget::slotEditHost()
00151 {
00152   LDAPItem *item = dynamic_cast<LDAPItem*>( mHostListView->currentItem() );
00153   if ( !item )
00154     return;
00155 
00156   AddHostDialog dlg( this );
00157   dlg.setCaption( i18n( "Edit Host" ) );
00158 
00159   dlg.setHost( item->server().host() );
00160   dlg.setPort( item->server().port() );
00161   dlg.setBaseDN( item->server().baseDN() );
00162   dlg.setBindDN( item->server().bindDN() );
00163   dlg.setPwdBindDN( item->server().pwdBindDN() );
00164 
00165   if ( dlg.exec() && !dlg.host().isEmpty() ) {
00166     LDAPServer server( dlg.host(), dlg.port(), dlg.baseDN(),
00167                        dlg.bindDN(), dlg.pwdBindDN() );
00168     item->setServer( server );
00169 
00170     emit changed( true );
00171   }
00172 }
00173 
00174 void LDAPOptionsWidget::slotRemoveHost()
00175 {
00176   QListViewItem *item = mHostListView->currentItem();
00177   if ( !item )
00178     return;
00179 
00180   mHostListView->takeItem( item );
00181   delete item;
00182 
00183   slotSelectionChanged( mHostListView->currentItem() );
00184 
00185   emit changed( true );
00186 }
00187 
00188 void LDAPOptionsWidget::restoreSettings()
00189 {
00190     mHostListView->clear();
00191   KConfig *config = KABC::AddressLineEdit::config();
00192   KConfigGroupSaver saver( config, "LDAP" );
00193 
00194   QString host;
00195 
00196   uint count = config->readUnsignedNumEntry( "NumSelectedHosts");
00197   for ( uint i = 0; i < count; ++i ) {
00198     LDAPServer server;
00199     server.setHost( config->readEntry( QString( "SelectedHost%1").arg( i ) ) );
00200     server.setPort( config->readUnsignedNumEntry( QString( "SelectedPort%1" ).arg( i ) ) );
00201     server.setBaseDN( config->readEntry( QString( "SelectedBase%1" ).arg( i ) ) );
00202     server.setBindDN( config->readEntry( QString( "SelectedBind%1" ).arg( i ) ) );
00203     server.setPwdBindDN( config->readEntry( QString( "SelectedPwdBind%1" ).arg( i ) ) );
00204 
00205     LDAPItem *item = new LDAPItem( mHostListView, server, true );
00206     item->setOn( true );
00207   }
00208 
00209   count = config->readUnsignedNumEntry( "NumHosts" );
00210   for ( uint i = 0; i < count; ++i ) {
00211     LDAPServer server;
00212     server.setHost( config->readEntry( QString( "Host%1" ).arg( i ) ) );
00213     server.setPort( config->readUnsignedNumEntry( QString( "Port%1" ).arg( i ) ) );
00214     server.setBaseDN( config->readEntry( QString( "Base%1" ).arg( i ) ) );
00215     server.setBindDN( config->readEntry( QString( "Bind%1" ).arg( i ) ) );
00216     server.setPwdBindDN( config->readEntry( QString( "PwdBind%1" ).arg( i ) ) );
00217 
00218     new LDAPItem( mHostListView, server );
00219   }
00220 
00221   emit changed( false );
00222 }
00223 
00224 void LDAPOptionsWidget::saveSettings()
00225 {
00226   KConfig *config = KABC::AddressLineEdit::config();
00227   config->deleteGroup( "LDAP" );
00228 
00229   KConfigGroupSaver saver( config, "LDAP" );
00230 
00231   uint selected = 0; uint unselected = 0;
00232   QListViewItemIterator it( mHostListView );
00233   for ( ; it.current(); ++it ) {
00234     LDAPItem *item = dynamic_cast<LDAPItem*>( it.current() );
00235     if ( !item )
00236       continue;
00237 
00238     LDAPServer server = item->server();
00239     if ( item->isOn() ) {
00240       config->writeEntry( QString( "SelectedHost%1" ).arg( selected ), server.host() );
00241       config->writeEntry( QString( "SelectedPort%1" ).arg( selected ), server.port() );
00242       config->writeEntry( QString( "SelectedBase%1" ).arg( selected ), server.baseDN() );
00243       config->writeEntry( QString( "SelectedBind%1" ).arg( selected ), server.bindDN() );
00244       config->writeEntry( QString( "SelectedPwdBind%1" ).arg( selected ), server.pwdBindDN() );
00245       selected++;
00246     } else {
00247       config->writeEntry( QString( "Host%1" ).arg( unselected ), server.host() );
00248       config->writeEntry( QString( "Port%1" ).arg( unselected ), server.port() );
00249       config->writeEntry( QString( "Base%1" ).arg( unselected ), server.baseDN() );
00250       config->writeEntry( QString( "Bind%1" ).arg( unselected ), server.bindDN() );
00251       config->writeEntry( QString( "PwdBind%1" ).arg( unselected ), server.pwdBindDN() );
00252       unselected++;
00253     }
00254   }
00255 
00256   config->writeEntry( "NumSelectedHosts", selected );
00257   config->writeEntry( "NumHosts", unselected );
00258   config->sync();
00259 
00260   emit changed( false );
00261 }
00262 
00263 void LDAPOptionsWidget::defaults()
00264 {
00265   // add default configuration here
00266 }
00267 
00268 void LDAPOptionsWidget::initGUI()
00269 {
00270   QVBoxLayout *layout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
00271 
00272   QGroupBox *groupBox = new QGroupBox( i18n( "LDAP Servers" ), this );
00273   groupBox->setColumnLayout( 0, Qt::Vertical );
00274   groupBox->layout()->setSpacing( KDialog::spacingHint() );
00275   groupBox->layout()->setMargin( KDialog::marginHint() );
00276 
00277   QVBoxLayout *groupBoxLayout = new QVBoxLayout( groupBox->layout() );
00278   groupBoxLayout->setAlignment( Qt::AlignTop );
00279 
00280   QLabel *label = new QLabel( i18n( "Check all servers that should be used:" ), groupBox );
00281   groupBoxLayout->addWidget( label );
00282 
00283   mHostListView = new KListView( groupBox );
00284   groupBoxLayout->addWidget( mHostListView );
00285 
00286   layout->addWidget( groupBox );
00287 
00288   KButtonBox *buttons = new KButtonBox( this );
00289   buttons->addButton( i18n( "&Add Host..." ), this, SLOT( slotAddHost() ) );
00290   mEditButton = buttons->addButton( i18n( "&Edit Host..." ), this, SLOT( slotEditHost() ) );
00291   mEditButton->setEnabled( false );
00292   mRemoveButton = buttons->addButton( i18n( "&Remove Host" ), this, SLOT( slotRemoveHost() ) );
00293   mRemoveButton->setEnabled( false );
00294   buttons->layout();
00295 
00296   layout->addWidget( buttons );
00297 
00298   resize( QSize( 460, 300 ).expandedTo( sizeHint() ) );
00299 }
00300 
00301 #include "ldapoptionswidget.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:52 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003