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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 00019 As a special exception, permission is given to link this program 00020 with any edition of TQt, and distribute the resulting executable, 00021 without including the source code for TQt in the source distribution. 00022 */ 00023 00024 #include <tqgroupbox.h> 00025 #include <tqheader.h> 00026 #include <tqlabel.h> 00027 #include <tqlayout.h> 00028 #include <tqpushbutton.h> 00029 #include <tqtoolbutton.h> 00030 #include <tqstring.h> 00031 00032 #include <tdeapplication.h> 00033 #include <kbuttonbox.h> 00034 #include <tdeconfig.h> 00035 #include <tdelistview.h> 00036 #include <tdelocale.h> 00037 00038 #include "addhostdialog.h" 00039 #include "ldapoptionswidget.h" 00040 #include <tqvgroupbox.h> 00041 #include <tqhbox.h> 00042 #include <tqvbox.h> 00043 #include <kiconloader.h> 00044 00045 class LDAPItem : public TQCheckListItem 00046 { 00047 public: 00048 LDAPItem( TQListView *parent, const KPIM::LdapServer &server, bool isActive = false ) 00049 : TQCheckListItem( parent, parent->lastItem(), TQString(), TQCheckListItem::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( TQWidget* parent, const char* name ) 00073 : TQWidget( parent, name ) 00074 { 00075 initGUI(); 00076 00077 mHostListView->setSorting( -1 ); 00078 mHostListView->setAllColumnsShowFocus( true ); 00079 mHostListView->setFullWidth( true ); 00080 mHostListView->addColumn( TQString() ); 00081 mHostListView->header()->hide(); 00082 00083 connect( mHostListView, TQT_SIGNAL( selectionChanged( TQListViewItem* ) ), 00084 TQT_SLOT( slotSelectionChanged( TQListViewItem* ) ) ); 00085 connect( mHostListView, TQT_SIGNAL(doubleClicked( TQListViewItem *, const TQPoint &, int )), this, TQT_SLOT(slotEditHost())); 00086 connect( mHostListView, TQT_SIGNAL( clicked( TQListViewItem* ) ), 00087 TQT_SLOT( slotItemClicked( TQListViewItem* ) ) ); 00088 00089 connect( mUpButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotMoveUp() ) ); 00090 connect( mDownButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotMoveDown() ) ); 00091 } 00092 00093 LDAPOptionsWidget::~LDAPOptionsWidget() 00094 { 00095 } 00096 00097 void LDAPOptionsWidget::slotSelectionChanged( TQListViewItem *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( TQListViewItem *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 TQListViewItem *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 TDEConfig *config = KPIM::LdapSearch::config(); 00201 TDEConfigGroupSaver saver( config, "LDAP" ); 00202 00203 TQString 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 TDEConfig *config = KPIM::LdapSearch::config(); 00226 config->deleteGroup( "LDAP" ); 00227 00228 TDEConfigGroupSaver saver( config, "LDAP" ); 00229 00230 uint selected = 0; uint unselected = 0; 00231 TQListViewItemIterator 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 // add default configuration here 00257 } 00258 00259 void LDAPOptionsWidget::initGUI() 00260 { 00261 TQVBoxLayout *layout = new TQVBoxLayout( this, 0, KDialog::spacingHint() ); 00262 00263 TQVGroupBox *groupBox = new TQVGroupBox( i18n( "LDAP Servers" ), this ); 00264 groupBox->setInsideSpacing( KDialog::spacingHint() ); 00265 groupBox->setInsideMargin( KDialog::marginHint() ); 00266 00267 // Contents of the TQVGroupBox: label and hbox 00268 /*TQLabel *label =*/ new TQLabel( i18n( "Check all servers that should be used:" ), groupBox ); 00269 00270 TQHBox* hBox = new TQHBox( groupBox ); 00271 hBox->setSpacing( 6 ); 00272 // Contents of the hbox: listview and up/down buttons on the right (vbox) 00273 mHostListView = new TDEListView( hBox ); 00274 00275 TQVBox* upDownBox = new TQVBox( hBox ); 00276 upDownBox->setSpacing( 6 ); 00277 mUpButton = new TQToolButton( upDownBox, "mUpButton" ); 00278 mUpButton->setIconSet( BarIconSet( "go-up", TDEIcon::SizeSmall ) ); 00279 mUpButton->setEnabled( false ); // b/c no item is selected yet 00280 00281 mDownButton = new TQToolButton( upDownBox, "mDownButton" ); 00282 mDownButton->setIconSet( BarIconSet( "go-down", TDEIcon::SizeSmall ) ); 00283 mDownButton->setEnabled( false ); // b/c no item is selected yet 00284 00285 TQWidget* spacer = new TQWidget( upDownBox ); 00286 upDownBox->setStretchFactor( spacer, 100 ); 00287 00288 layout->addWidget( groupBox ); 00289 00290 KButtonBox *buttons = new KButtonBox( this ); 00291 buttons->addButton( i18n( "&Add Host..." ), TQT_TQOBJECT(this), TQT_SLOT( slotAddHost() ) ); 00292 mEditButton = buttons->addButton( i18n( "&Edit Host..." ), TQT_TQOBJECT(this), TQT_SLOT( slotEditHost() ) ); 00293 mEditButton->setEnabled( false ); 00294 mRemoveButton = buttons->addButton( i18n( "&Remove Host" ), TQT_TQOBJECT(this), TQT_SLOT( slotRemoveHost() ) ); 00295 mRemoveButton->setEnabled( false ); 00296 buttons->layout(); 00297 00298 layout->addWidget( buttons ); 00299 00300 resize( TQSize( 460, 300 ).expandedTo( sizeHint() ) ); 00301 } 00302 00303 #include "ldapoptionswidget.moc"