kaddressbook

ldapoptionswidget.cpp
1/*
2 This file is part of KAddressBook.
3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of TQt, and distribute the resulting executable,
21 without including the source code for TQt in the source distribution.
22*/
23
24#include <tqgroupbox.h>
25#include <tqheader.h>
26#include <tqlabel.h>
27#include <tqlayout.h>
28#include <tqpushbutton.h>
29#include <tqtoolbutton.h>
30#include <tqstring.h>
31
32#include <tdeapplication.h>
33#include <kbuttonbox.h>
34#include <tdeconfig.h>
35#include <tdelistview.h>
36#include <tdelocale.h>
37
38#include "addhostdialog.h"
39#include "ldapoptionswidget.h"
40#include <tqvgroupbox.h>
41#include <tqhbox.h>
42#include <tqvbox.h>
43#include <kiconloader.h>
44
45class LDAPItem : public TQCheckListItem
46{
47 public:
48 LDAPItem( TQListView *parent, const KPIM::LdapServer &server, bool isActive = false )
49 : TQCheckListItem( parent, parent->lastItem(), TQString(), TQCheckListItem::CheckBox ),
50 mIsActive( isActive )
51 {
52 setServer( server );
53 }
54
55 void setServer( const KPIM::LdapServer &server )
56 {
57 mServer = server;
58
59 setText( 0, mServer.host() );
60 }
61
62 const KPIM::LdapServer &server() const { return mServer; }
63
64 void setIsActive( bool isActive ) { mIsActive = isActive; }
65 bool isActive() const { return mIsActive; }
66
67 private:
68 KPIM::LdapServer mServer;
69 bool mIsActive;
70};
71
72LDAPOptionsWidget::LDAPOptionsWidget( TQWidget* parent, const char* name )
73 : TQWidget( parent, name )
74{
75 initGUI();
76
77 mHostListView->setSorting( -1 );
78 mHostListView->setAllColumnsShowFocus( true );
79 mHostListView->setFullWidth( true );
80 mHostListView->addColumn( TQString() );
81 mHostListView->header()->hide();
82
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* ) ) );
88
89 connect( mUpButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotMoveUp() ) );
90 connect( mDownButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotMoveDown() ) );
91}
92
93LDAPOptionsWidget::~LDAPOptionsWidget()
94{
95}
96
97void LDAPOptionsWidget::slotSelectionChanged( TQListViewItem *item )
98{
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() );
104}
105
106void LDAPOptionsWidget::slotItemClicked( TQListViewItem *item )
107{
108 LDAPItem *ldapItem = dynamic_cast<LDAPItem*>( item );
109 if ( !ldapItem )
110 return;
111
112 if ( ldapItem->isOn() != ldapItem->isActive() ) {
113 emit changed( true );
114 ldapItem->setIsActive( ldapItem->isOn() );
115 }
116}
117
118void LDAPOptionsWidget::slotAddHost()
119{
120 KPIM::LdapServer server;
121 AddHostDialog dlg( &server, this );
122
123 if ( dlg.exec() && !server.host().isEmpty() ) {
124 new LDAPItem( mHostListView, server );
125
126 emit changed( true );
127 }
128}
129
130void LDAPOptionsWidget::slotEditHost()
131{
132 LDAPItem *item = dynamic_cast<LDAPItem*>( mHostListView->currentItem() );
133 if ( !item )
134 return;
135
136 KPIM::LdapServer server = item->server();
137 AddHostDialog dlg( &server, this );
138 dlg.setCaption( i18n( "Edit Host" ) );
139
140 if ( dlg.exec() && !server.host().isEmpty() ) {
141 item->setServer( server );
142
143 emit changed( true );
144 }
145}
146
147void LDAPOptionsWidget::slotRemoveHost()
148{
149 TQListViewItem *item = mHostListView->currentItem();
150 if ( !item )
151 return;
152
153 mHostListView->takeItem( item );
154 delete item;
155
156 slotSelectionChanged( mHostListView->currentItem() );
157
158 emit changed( true );
159}
160
161static void swapItems( LDAPItem *item, LDAPItem *other )
162{
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 );
171}
172
173void LDAPOptionsWidget::slotMoveUp()
174{
175 LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItem() );
176 if ( !item ) return;
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 );
183}
184
185void LDAPOptionsWidget::slotMoveDown()
186{
187 LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItem() );
188 if ( !item ) return;
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 );
195}
196
197void LDAPOptionsWidget::restoreSettings()
198{
199 mHostListView->clear();
200 TDEConfig *config = KPIM::LdapSearch::config();
201 TDEConfigGroupSaver saver( config, "LDAP" );
202
203 TQString host;
204
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 );
210 item->setOn( true );
211 }
212
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 );
218 }
219
220 emit changed( false );
221}
222
223void LDAPOptionsWidget::saveSettings()
224{
225 TDEConfig *config = KPIM::LdapSearch::config();
226 config->deleteGroup( "LDAP" );
227
228 TDEConfigGroupSaver saver( config, "LDAP" );
229
230 uint selected = 0; uint unselected = 0;
231 TQListViewItemIterator it( mHostListView );
232 for ( ; it.current(); ++it ) {
233 LDAPItem *item = dynamic_cast<LDAPItem*>( it.current() );
234 if ( !item )
235 continue;
236
237 KPIM::LdapServer server = item->server();
238 if ( item->isOn() ) {
239 KPIM::LdapSearch::writeConfig( server, config, selected, true );
240 selected++;
241 } else {
242 KPIM::LdapSearch::writeConfig( server, config, unselected, false );
243 unselected++;
244 }
245 }
246
247 config->writeEntry( "NumSelectedHosts", selected );
248 config->writeEntry( "NumHosts", unselected );
249 config->sync();
250
251 emit changed( false );
252}
253
254void LDAPOptionsWidget::defaults()
255{
256 // add default configuration here
257}
258
259void LDAPOptionsWidget::initGUI()
260{
261 TQVBoxLayout *layout = new TQVBoxLayout( this, 0, KDialog::spacingHint() );
262
263 TQVGroupBox *groupBox = new TQVGroupBox( i18n( "LDAP Servers" ), this );
264 groupBox->setInsideSpacing( KDialog::spacingHint() );
265 groupBox->setInsideMargin( KDialog::marginHint() );
266
267 // Contents of the TQVGroupBox: label and hbox
268 /*TQLabel *label =*/ new TQLabel( i18n( "Check all servers that should be used:" ), groupBox );
269
270 TQHBox* hBox = new TQHBox( groupBox );
271 hBox->setSpacing( 6 );
272 // Contents of the hbox: listview and up/down buttons on the right (vbox)
273 mHostListView = new TDEListView( hBox );
274
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 ); // b/c no item is selected yet
280
281 mDownButton = new TQToolButton( upDownBox, "mDownButton" );
282 mDownButton->setIconSet( BarIconSet( "go-down", TDEIcon::SizeSmall ) );
283 mDownButton->setEnabled( false ); // b/c no item is selected yet
284
285 TQWidget* spacer = new TQWidget( upDownBox );
286 upDownBox->setStretchFactor( spacer, 100 );
287
288 layout->addWidget( groupBox );
289
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 );
296 buttons->layout();
297
298 layout->addWidget( buttons );
299
300 resize( TQSize( 460, 300 ).expandedTo( sizeHint() ) );
301}
302
303#include "ldapoptionswidget.moc"