00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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 <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 #include <qvgroupbox.h>
00041 #include <qhbox.h>
00042 #include <qvbox.h>
00043 #include <kiconloader.h>
00044
00045 class LDAPItem : public QCheckListItem
00046 {
00047 public:
00048 LDAPItem( QListView *parent, const KPIM::LdapServer &server, bool isActive = false )
00049 : QCheckListItem( parent, parent->lastItem(), QString::null, QCheckListItem::CheckBox ),
00050 mIsActive( isActive )
00051 {
00052 setServer( server );
00053 }
00054
00055 void setServer( const KPIM::LdapServer &server )
00056 {
00057 mServer = server;
00058
00059 setText( 0, mServer.host() );
00060 }
00061
00062 const KPIM::LdapServer &server() const { return mServer; }
00063
00064 void setIsActive( bool isActive ) { mIsActive = isActive; }
00065 bool isActive() const { return mIsActive; }
00066
00067 private:
00068 KPIM::LdapServer mServer;
00069 bool mIsActive;
00070 };
00071
00072 LDAPOptionsWidget::LDAPOptionsWidget( QWidget* parent, const char* name )
00073 : QWidget( parent, name )
00074 {
00075 initGUI();
00076
00077 mHostListView->setSorting( -1 );
00078 mHostListView->setAllColumnsShowFocus( true );
00079 mHostListView->setFullWidth( true );
00080 mHostListView->addColumn( QString::null );
00081 mHostListView->header()->hide();
00082
00083 connect( mHostListView, SIGNAL( selectionChanged( QListViewItem* ) ),
00084 SLOT( slotSelectionChanged( QListViewItem* ) ) );
00085 connect( mHostListView, SIGNAL(doubleClicked( QListViewItem *, const QPoint &, int )), this, SLOT(slotEditHost()));
00086 connect( mHostListView, SIGNAL( clicked( QListViewItem* ) ),
00087 SLOT( slotItemClicked( QListViewItem* ) ) );
00088
00089 connect( mUpButton, SIGNAL( clicked() ), this, SLOT( slotMoveUp() ) );
00090 connect( mDownButton, SIGNAL( clicked() ), this, SLOT( slotMoveDown() ) );
00091 }
00092
00093 LDAPOptionsWidget::~LDAPOptionsWidget()
00094 {
00095 }
00096
00097 void LDAPOptionsWidget::slotSelectionChanged( QListViewItem *item )
00098 {
00099 bool state = ( item != 0 );
00100 mEditButton->setEnabled( state );
00101 mRemoveButton->setEnabled( state );
00102 mDownButton->setEnabled( item && item->itemBelow() );
00103 mUpButton->setEnabled( item && item->itemAbove() );
00104 }
00105
00106 void LDAPOptionsWidget::slotItemClicked( QListViewItem *item )
00107 {
00108 LDAPItem *ldapItem = dynamic_cast<LDAPItem*>( item );
00109 if ( !ldapItem )
00110 return;
00111
00112 if ( ldapItem->isOn() != ldapItem->isActive() ) {
00113 emit changed( true );
00114 ldapItem->setIsActive( ldapItem->isOn() );
00115 }
00116 }
00117
00118 void LDAPOptionsWidget::slotAddHost()
00119 {
00120 KPIM::LdapServer server;
00121 AddHostDialog dlg( &server, this );
00122
00123 if ( dlg.exec() && !server.host().isEmpty() ) {
00124 new LDAPItem( mHostListView, server );
00125
00126 emit changed( true );
00127 }
00128 }
00129
00130 void LDAPOptionsWidget::slotEditHost()
00131 {
00132 LDAPItem *item = dynamic_cast<LDAPItem*>( mHostListView->currentItem() );
00133 if ( !item )
00134 return;
00135
00136 KPIM::LdapServer server = item->server();
00137 AddHostDialog dlg( &server, this );
00138 dlg.setCaption( i18n( "Edit Host" ) );
00139
00140 if ( dlg.exec() && !server.host().isEmpty() ) {
00141 item->setServer( server );
00142
00143 emit changed( true );
00144 }
00145 }
00146
00147 void LDAPOptionsWidget::slotRemoveHost()
00148 {
00149 QListViewItem *item = mHostListView->currentItem();
00150 if ( !item )
00151 return;
00152
00153 mHostListView->takeItem( item );
00154 delete item;
00155
00156 slotSelectionChanged( mHostListView->currentItem() );
00157
00158 emit changed( true );
00159 }
00160
00161 static void swapItems( LDAPItem *item, LDAPItem *other )
00162 {
00163 KPIM::LdapServer server = item->server();
00164 bool isActive = item->isActive();
00165 item->setServer( other->server() );
00166 item->setIsActive( other->isActive() );
00167 item->setOn( other->isActive() );
00168 other->setServer( server );
00169 other->setIsActive( isActive );
00170 other->setOn( isActive );
00171 }
00172
00173 void LDAPOptionsWidget::slotMoveUp()
00174 {
00175 LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItem() );
00176 if ( !item ) return;
00177 LDAPItem *above = static_cast<LDAPItem *>( item->itemAbove() );
00178 if ( !above ) return;
00179 swapItems( item, above );
00180 mHostListView->setCurrentItem( above );
00181 mHostListView->setSelected( above, true );
00182 emit changed( true );
00183 }
00184
00185 void LDAPOptionsWidget::slotMoveDown()
00186 {
00187 LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItem() );
00188 if ( !item ) return;
00189 LDAPItem *below = static_cast<LDAPItem *>( item->itemBelow() );
00190 if ( !below ) return;
00191 swapItems( item, below );
00192 mHostListView->setCurrentItem( below );
00193 mHostListView->setSelected( below, true );
00194 emit changed( true );
00195 }
00196
00197 void LDAPOptionsWidget::restoreSettings()
00198 {
00199 mHostListView->clear();
00200 KConfig *config = KPIM::LdapSearch::config();
00201 KConfigGroupSaver saver( config, "LDAP" );
00202
00203 QString host;
00204
00205 uint count = config->readUnsignedNumEntry( "NumSelectedHosts");
00206 for ( uint i = 0; i < count; ++i ) {
00207 KPIM::LdapServer server;
00208 KPIM::LdapSearch::readConfig( server, config, i, true );
00209 LDAPItem *item = new LDAPItem( mHostListView, server, true );
00210 item->setOn( true );
00211 }
00212
00213 count = config->readUnsignedNumEntry( "NumHosts" );
00214 for ( uint i = 0; i < count; ++i ) {
00215 KPIM::LdapServer server;
00216 KPIM::LdapSearch::readConfig( server, config, i, false );
00217 new LDAPItem( mHostListView, server );
00218 }
00219
00220 emit changed( false );
00221 }
00222
00223 void LDAPOptionsWidget::saveSettings()
00224 {
00225 KConfig *config = KPIM::LdapSearch::config();
00226 config->deleteGroup( "LDAP" );
00227
00228 KConfigGroupSaver saver( config, "LDAP" );
00229
00230 uint selected = 0; uint unselected = 0;
00231 QListViewItemIterator it( mHostListView );
00232 for ( ; it.current(); ++it ) {
00233 LDAPItem *item = dynamic_cast<LDAPItem*>( it.current() );
00234 if ( !item )
00235 continue;
00236
00237 KPIM::LdapServer server = item->server();
00238 if ( item->isOn() ) {
00239 KPIM::LdapSearch::writeConfig( server, config, selected, true );
00240 selected++;
00241 } else {
00242 KPIM::LdapSearch::writeConfig( server, config, unselected, false );
00243 unselected++;
00244 }
00245 }
00246
00247 config->writeEntry( "NumSelectedHosts", selected );
00248 config->writeEntry( "NumHosts", unselected );
00249 config->sync();
00250
00251 emit changed( false );
00252 }
00253
00254 void LDAPOptionsWidget::defaults()
00255 {
00256
00257 }
00258
00259 void LDAPOptionsWidget::initGUI()
00260 {
00261 QVBoxLayout *layout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
00262
00263 QVGroupBox *groupBox = new QVGroupBox( i18n( "LDAP Servers" ), this );
00264 groupBox->setInsideSpacing( KDialog::spacingHint() );
00265 groupBox->setInsideMargin( KDialog::marginHint() );
00266
00267
00268 new QLabel( i18n( "Check all servers that should be used:" ), groupBox );
00269
00270 QHBox* hBox = new QHBox( groupBox );
00271 hBox->setSpacing( 6 );
00272
00273 mHostListView = new KListView( hBox );
00274
00275 QVBox* upDownBox = new QVBox( hBox );
00276 upDownBox->setSpacing( 6 );
00277 mUpButton = new QToolButton( upDownBox, "mUpButton" );
00278 mUpButton->setIconSet( BarIconSet( "up", KIcon::SizeSmall ) );
00279 mUpButton->setEnabled( false );
00280
00281 mDownButton = new QToolButton( upDownBox, "mDownButton" );
00282 mDownButton->setIconSet( BarIconSet( "down", KIcon::SizeSmall ) );
00283 mDownButton->setEnabled( false );
00284
00285 QWidget* spacer = new QWidget( upDownBox );
00286 upDownBox->setStretchFactor( spacer, 100 );
00287
00288 layout->addWidget( groupBox );
00289
00290 KButtonBox *buttons = new KButtonBox( this );
00291 buttons->addButton( i18n( "&Add Host..." ), this, SLOT( slotAddHost() ) );
00292 mEditButton = buttons->addButton( i18n( "&Edit Host..." ), this, SLOT( slotEditHost() ) );
00293 mEditButton->setEnabled( false );
00294 mRemoveButton = buttons->addButton( i18n( "&Remove Host" ), this, SLOT( slotRemoveHost() ) );
00295 mRemoveButton->setEnabled( false );
00296 buttons->layout();
00297
00298 layout->addWidget( buttons );
00299
00300 resize( QSize( 460, 300 ).expandedTo( sizeHint() ) );
00301 }
00302
00303 #include "ldapoptionswidget.moc"