24 #include <tqgroupbox.h>
28 #include <tqpushbutton.h>
29 #include <tqtoolbutton.h>
32 #include <tdeapplication.h>
33 #include <kbuttonbox.h>
34 #include <tdeconfig.h>
35 #include <tdelistview.h>
36 #include <tdelocale.h>
38 #include "addhostdialog.h"
39 #include "ldapoptionswidget.h"
40 #include <tqvgroupbox.h>
43 #include <kiconloader.h>
45 class LDAPItem :
public TQCheckListItem
48 LDAPItem( TQListView *parent,
const KPIM::LdapServer &server,
bool isActive =
false )
49 : TQCheckListItem( parent, parent->lastItem(), TQString(), TQCheckListItem::CheckBox ),
55 void setServer(
const KPIM::LdapServer &server )
59 setText( 0, mServer.host() );
62 const KPIM::LdapServer &server()
const {
return mServer; }
64 void setIsActive(
bool isActive ) { mIsActive = isActive; }
65 bool isActive()
const {
return mIsActive; }
68 KPIM::LdapServer mServer;
72 LDAPOptionsWidget::LDAPOptionsWidget( TQWidget* parent,
const char* name )
73 : TQWidget( parent, name )
77 mHostListView->setSorting( -1 );
78 mHostListView->setAllColumnsShowFocus(
true );
79 mHostListView->setFullWidth(
true );
80 mHostListView->addColumn( TQString() );
81 mHostListView->header()->hide();
83 connect( mHostListView, TQ_SIGNAL( selectionChanged( TQListViewItem* ) ),
84 TQ_SLOT( slotSelectionChanged( TQListViewItem* ) ) );
85 connect( mHostListView, TQ_SIGNAL(doubleClicked( TQListViewItem *,
const TQPoint &,
int )),
this, TQ_SLOT(slotEditHost()));
86 connect( mHostListView, TQ_SIGNAL( clicked( TQListViewItem* ) ),
87 TQ_SLOT( slotItemClicked( TQListViewItem* ) ) );
89 connect( mUpButton, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( slotMoveUp() ) );
90 connect( mDownButton, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( slotMoveDown() ) );
93 LDAPOptionsWidget::~LDAPOptionsWidget()
97 void LDAPOptionsWidget::slotSelectionChanged( TQListViewItem *item )
99 bool state = ( item != 0 );
100 mEditButton->setEnabled( state );
101 mRemoveButton->setEnabled( state );
102 mDownButton->setEnabled( item && item->itemBelow() );
103 mUpButton->setEnabled( item && item->itemAbove() );
106 void LDAPOptionsWidget::slotItemClicked( TQListViewItem *item )
108 LDAPItem *ldapItem =
dynamic_cast<LDAPItem*
>( item );
112 if ( ldapItem->isOn() != ldapItem->isActive() ) {
113 emit changed(
true );
114 ldapItem->setIsActive( ldapItem->isOn() );
118 void LDAPOptionsWidget::slotAddHost()
120 KPIM::LdapServer server;
121 AddHostDialog dlg( &server,
this );
123 if ( dlg.exec() && !server.host().isEmpty() ) {
124 new LDAPItem( mHostListView, server );
126 emit changed(
true );
130 void LDAPOptionsWidget::slotEditHost()
132 LDAPItem *item =
dynamic_cast<LDAPItem*
>( mHostListView->currentItem() );
136 KPIM::LdapServer server = item->server();
137 AddHostDialog dlg( &server,
this );
138 dlg.setCaption( i18n(
"Edit Host" ) );
140 if ( dlg.exec() && !server.host().isEmpty() ) {
141 item->setServer( server );
143 emit changed(
true );
147 void LDAPOptionsWidget::slotRemoveHost()
149 TQListViewItem *item = mHostListView->currentItem();
153 mHostListView->takeItem( item );
156 slotSelectionChanged( mHostListView->currentItem() );
158 emit changed(
true );
161 static void swapItems( LDAPItem *item, LDAPItem *other )
163 KPIM::LdapServer server = item->server();
164 bool isActive = item->isActive();
165 item->setServer( other->server() );
166 item->setIsActive( other->isActive() );
167 item->setOn( other->isActive() );
168 other->setServer( server );
169 other->setIsActive( isActive );
170 other->setOn( isActive );
173 void LDAPOptionsWidget::slotMoveUp()
175 LDAPItem *item =
static_cast<LDAPItem *
>( mHostListView->selectedItem() );
177 LDAPItem *above =
static_cast<LDAPItem *
>( item->itemAbove() );
178 if ( !above )
return;
179 swapItems( item, above );
180 mHostListView->setCurrentItem( above );
181 mHostListView->setSelected( above,
true );
182 emit changed(
true );
185 void LDAPOptionsWidget::slotMoveDown()
187 LDAPItem *item =
static_cast<LDAPItem *
>( mHostListView->selectedItem() );
189 LDAPItem *below =
static_cast<LDAPItem *
>( item->itemBelow() );
190 if ( !below )
return;
191 swapItems( item, below );
192 mHostListView->setCurrentItem( below );
193 mHostListView->setSelected( below,
true );
194 emit changed(
true );
197 void LDAPOptionsWidget::restoreSettings()
199 mHostListView->clear();
200 TDEConfig *config = KPIM::LdapSearch::config();
201 TDEConfigGroupSaver saver( config,
"LDAP" );
205 uint count = config->readUnsignedNumEntry(
"NumSelectedHosts");
206 for ( uint i = 0; i < count; ++i ) {
207 KPIM::LdapServer server;
208 KPIM::LdapSearch::readConfig( server, config, i,
true );
209 LDAPItem *item =
new LDAPItem( mHostListView, server,
true );
213 count = config->readUnsignedNumEntry(
"NumHosts" );
214 for ( uint i = 0; i < count; ++i ) {
215 KPIM::LdapServer server;
216 KPIM::LdapSearch::readConfig( server, config, i,
false );
217 new LDAPItem( mHostListView, server );
220 emit changed(
false );
223 void LDAPOptionsWidget::saveSettings()
225 TDEConfig *config = KPIM::LdapSearch::config();
226 config->deleteGroup(
"LDAP" );
228 TDEConfigGroupSaver saver( config,
"LDAP" );
230 uint selected = 0; uint unselected = 0;
231 TQListViewItemIterator it( mHostListView );
232 for ( ; it.current(); ++it ) {
233 LDAPItem *item =
dynamic_cast<LDAPItem*
>( it.current() );
237 KPIM::LdapServer server = item->server();
238 if ( item->isOn() ) {
239 KPIM::LdapSearch::writeConfig( server, config, selected,
true );
242 KPIM::LdapSearch::writeConfig( server, config, unselected,
false );
247 config->writeEntry(
"NumSelectedHosts", selected );
248 config->writeEntry(
"NumHosts", unselected );
251 emit changed(
false );
254 void LDAPOptionsWidget::defaults()
259 void LDAPOptionsWidget::initGUI()
261 TQVBoxLayout *layout =
new TQVBoxLayout(
this, 0, KDialog::spacingHint() );
263 TQVGroupBox *groupBox =
new TQVGroupBox( i18n(
"LDAP Servers" ),
this );
264 groupBox->setInsideSpacing( KDialog::spacingHint() );
265 groupBox->setInsideMargin( KDialog::marginHint() );
268 new TQLabel( i18n(
"Check all servers that should be used:" ), groupBox );
270 TQHBox* hBox =
new TQHBox( groupBox );
271 hBox->setSpacing( 6 );
273 mHostListView =
new TDEListView( hBox );
275 TQVBox* upDownBox =
new TQVBox( hBox );
276 upDownBox->setSpacing( 6 );
277 mUpButton =
new TQToolButton( upDownBox,
"mUpButton" );
278 mUpButton->setIconSet( BarIconSet(
"go-up", TDEIcon::SizeSmall ) );
279 mUpButton->setEnabled(
false );
281 mDownButton =
new TQToolButton( upDownBox,
"mDownButton" );
282 mDownButton->setIconSet( BarIconSet(
"go-down", TDEIcon::SizeSmall ) );
283 mDownButton->setEnabled(
false );
285 TQWidget* spacer =
new TQWidget( upDownBox );
286 upDownBox->setStretchFactor( spacer, 100 );
288 layout->addWidget( groupBox );
290 KButtonBox *buttons =
new KButtonBox(
this );
291 buttons->addButton( i18n(
"&Add Host..." ),
this, TQ_SLOT( slotAddHost() ) );
292 mEditButton = buttons->addButton( i18n(
"&Edit Host..." ),
this, TQ_SLOT( slotEditHost() ) );
293 mEditButton->setEnabled(
false );
294 mRemoveButton = buttons->addButton( i18n(
"&Remove Host" ),
this, TQ_SLOT( slotRemoveHost() ) );
295 mRemoveButton->setEnabled(
false );
298 layout->addWidget( buttons );
300 resize( TQSize( 460, 300 ).expandedTo( sizeHint() ) );
303 #include "ldapoptionswidget.moc"