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., 675 Mass Ave, Cambridge, MA 02139, 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 <kapplication.h>
00032 #include <kbuttonbox.h>
00033 #include <kconfig.h>
00034 #include <klistview.h>
00035 #include <klocale.h>
00036 
00037 #include "addhostdialog.h"
00038 #include "ldapoptionswidget.h"
00039 
00040 class LDAPServer
00041 {
00042   public:
00043     LDAPServer() : mPort( 389 ) {}
00044     LDAPServer( const QString &host, int port, const QString &baseDN )
00045       : mHost( host ), mPort( port ), mBaseDN( baseDN )
00046     { }
00047 
00048     QString host() const { return mHost; }
00049     int port() const { return mPort; }
00050     QString baseDN() const { return mBaseDN; }
00051 
00052     void setHost( const QString &host ) { mHost = host; }
00053     void setPort( int port ) { mPort = port; }
00054     void setBaseDN( const QString &baseDN ) {  mBaseDN = baseDN; }
00055 
00056   private:
00057     QString mHost;
00058     int mPort;
00059     QString mBaseDN;
00060 };
00061 
00062 class LDAPItem : public QCheckListItem
00063 {
00064   public:
00065     LDAPItem( QListView *parent, const LDAPServer &server )
00066       : QCheckListItem( parent, QString::null, QCheckListItem::CheckBox )
00067     {
00068       setServer( server );
00069     }
00070 
00071     void setServer( const LDAPServer &server )
00072     {
00073       mServer = server;
00074 
00075       setText( 0, mServer.host() );
00076     }
00077 
00078     LDAPServer server() const { return mServer; }
00079 
00080   private:
00081     LDAPServer mServer;
00082 };
00083 
00084 LDAPOptionsWidget::LDAPOptionsWidget( QWidget* parent,  const char* name )
00085   : QWidget( parent, name )
00086 {
00087   initGUI();
00088 
00089   mHostListView->addColumn( QString::null );
00090   mHostListView->header()->hide();
00091 
00092   connect( mHostListView, SIGNAL( selectionChanged( QListViewItem* ) ),
00093            SLOT( slotSelectionChanged( QListViewItem* ) ) );
00094   connect( mHostListView, SIGNAL(doubleClicked( QListViewItem *, const QPoint &, int )), this, SLOT(slotEditHost()));
00095 }
00096 
00097 LDAPOptionsWidget::~LDAPOptionsWidget()
00098 {
00099 }
00100 
00101 void LDAPOptionsWidget::slotSelectionChanged( QListViewItem *item )
00102 {
00103   bool state = ( item != 0 );
00104 
00105   mEditButton->setEnabled( state );
00106   mRemoveButton->setEnabled( state );
00107 }
00108 
00109 void LDAPOptionsWidget::slotAddHost()
00110 {
00111   AddHostDialog dlg( this );
00112 
00113   if ( dlg.exec() && !dlg.host().isEmpty() ) {
00114     LDAPServer server( dlg.host(), dlg.port(), dlg.baseDN() );
00115     new LDAPItem( mHostListView, server );
00116   }
00117 }
00118 
00119 void LDAPOptionsWidget::slotEditHost()
00120 {
00121   LDAPItem *item = dynamic_cast<LDAPItem*>( mHostListView->currentItem() );
00122   if ( !item )
00123     return;
00124 
00125   AddHostDialog dlg( this );
00126   dlg.setCaption( i18n("Edit Host") );
00127 
00128   dlg.setHost( item->server().host() );
00129   dlg.setPort( item->server().port() );
00130   dlg.setBaseDN( item->server().baseDN() );
00131 
00132   if ( dlg.exec() && !dlg.host().isEmpty() ) {
00133     LDAPServer server( dlg.host(), dlg.port(), dlg.baseDN() );
00134     item->setServer( server );
00135   }
00136 }
00137 
00138 void LDAPOptionsWidget::slotRemoveHost()
00139 {
00140   QListViewItem *item = mHostListView->currentItem();
00141   if ( !item )
00142     return;
00143 
00144   mHostListView->takeItem( item );
00145   delete item;
00146 
00147   slotSelectionChanged( mHostListView->currentItem() );
00148 }
00149 
00150 void LDAPOptionsWidget::restoreSettings()
00151 {
00152   KConfig *config = kapp->config();
00153   config->setGroup( "LDAP" );
00154 
00155   QString host;
00156 
00157   uint count = config->readUnsignedNumEntry( "NumSelectedHosts");
00158   for ( uint i = 0; i < count; ++i ) {
00159     LDAPServer server;
00160     server.setHost( config->readEntry( QString( "SelectedHost%1").arg( i ) ) );
00161     server.setPort( config->readUnsignedNumEntry( QString( "SelectedPort%1" ).arg( i ) ) );
00162     server.setBaseDN( config->readEntry( QString( "SelectedBase%1" ).arg( i ) ) );
00163 
00164     LDAPItem *item = new LDAPItem( mHostListView, server );
00165     item->setOn( true );
00166   }
00167 
00168   count = config->readUnsignedNumEntry( "NumHosts" );
00169   for ( uint i = 0; i < count; ++i ) {
00170     LDAPServer server;
00171     server.setHost( config->readEntry( QString( "Host%1" ).arg( i ) ) );
00172     server.setPort( config->readUnsignedNumEntry( QString( "Port%1" ).arg( i ) ) );
00173     server.setBaseDN( config->readEntry( QString( "Base%1" ).arg( i ) ) );
00174     new LDAPItem( mHostListView, server );
00175   }
00176 }
00177 
00178 void LDAPOptionsWidget::saveSettings()
00179 {
00180   KConfig *config = kapp->config();
00181   config->deleteGroup( "LDAP" );
00182 
00183   config->setGroup("LDAP");
00184 
00185   uint selected = 0; uint unselected = 0;
00186   QListViewItemIterator it( mHostListView );
00187   for ( ; it.current(); ++it ) {
00188     LDAPItem *item = dynamic_cast<LDAPItem*>( it.current() );
00189     if ( !item )
00190       continue;
00191 
00192     LDAPServer server = item->server();
00193     if ( item->isOn() ) {
00194       config->writeEntry( QString( "SelectedHost%1" ).arg( selected ), server.host() );
00195       config->writeEntry( QString( "SelectedPort%1" ).arg( selected ), server.port() );
00196       config->writeEntry( QString( "SelectedBase%1" ).arg( selected ), server.baseDN() );
00197       selected++;
00198     } else {
00199       config->writeEntry( QString( "Host%1" ).arg( selected ), server.host() );
00200       config->writeEntry( QString( "Port%1" ).arg( selected ), server.port() );
00201       config->writeEntry( QString( "Base%1" ).arg( selected ), server.baseDN() );
00202       unselected++;
00203     }
00204   }
00205 
00206   config->writeEntry( "NumSelectedHosts", selected );
00207   config->writeEntry( "NumHosts", unselected );
00208   config->sync();
00209 }
00210 
00211 void LDAPOptionsWidget::initGUI()
00212 {
00213   QVBoxLayout *layout = new QVBoxLayout( this, KDialog::marginHint(), KDialog::spacingHint() );
00214 
00215   QGroupBox *groupBox = new QGroupBox( i18n( "LDAP Servers" ), this );
00216   groupBox->setColumnLayout( 0, Qt::Vertical );
00217   groupBox->layout()->setSpacing( KDialog::spacingHint() );
00218   groupBox->layout()->setMargin( KDialog::marginHint() );
00219 
00220   QVBoxLayout *groupBoxLayout = new QVBoxLayout( groupBox->layout() );
00221   groupBoxLayout->setAlignment( Qt::AlignTop );
00222 
00223   QLabel *label = new QLabel( i18n( "Check all servers that should be used:" ), groupBox );
00224   groupBoxLayout->addWidget( label );
00225 
00226   mHostListView = new KListView( groupBox );
00227   groupBoxLayout->addWidget( mHostListView );
00228 
00229   layout->addWidget( groupBox );
00230   layout->addStretch();
00231 
00232   KButtonBox *buttons = new KButtonBox( this );
00233   buttons->addStretch();
00234   buttons->addButton( i18n( "&Add Host..." ), this, SLOT( slotAddHost() ) );
00235   mEditButton = buttons->addButton( i18n( "&Edit Host..." ), this, SLOT( slotEditHost() ) );
00236   mEditButton->setEnabled( false );
00237   mRemoveButton = buttons->addButton( i18n( "&Remove Host" ), this, SLOT( slotRemoveHost() ) );
00238   mRemoveButton->setEnabled( false );
00239   buttons->layout();
00240 
00241   layout->addWidget( buttons );
00242 
00243   resize( QSize( 460, 300 ).expandedTo( sizeHint() ) );
00244 }
00245 
00246 #include "ldapoptionswidget.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:09 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001