2
This file is part of KAddressBook.
3
Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
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.
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.
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.
19
As a special exception, permission is given to link this program
20
with any edition of Qt, and distribute the resulting executable,
21
without including the source code for Qt in the source distribution.
24
#include "ldapoptionswidget.h"
26
#include <QtCore/QString>
27
#include <QtGui/QGroupBox>
28
#include <QtGui/QLabel>
29
#include <QtGui/QListWidget>
30
#include <QtGui/QListWidgetItem>
31
#include <QtGui/QPushButton>
32
#include <QtGui/QToolButton>
33
#include <QtGui/QVBoxLayout>
35
#include <kapplication.h>
37
#include <kconfiggroup.h>
38
#include <KDialogButtonBox>
40
#include <kiconloader.h>
43
#include <libkdepim/ldapclient.h>
45
#include "addhostdialog.h"
47
class LDAPItem : public QListWidgetItem
50
LDAPItem( QListWidget *parent, const KLDAP::LdapServer &server, bool isActive = false )
51
: QListWidgetItem( parent, QListWidgetItem::UserType ),
54
setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable );
55
setCheckState( isActive ? Qt::Checked : Qt::Unchecked );
59
void setServer( const KLDAP::LdapServer &server )
63
setText( mServer.host() );
66
const KLDAP::LdapServer &server() const { return mServer; }
68
void setIsActive( bool isActive ) { mIsActive = isActive; }
69
bool isActive() const { return mIsActive; }
72
KLDAP::LdapServer mServer;
76
LDAPOptionsWidget::LDAPOptionsWidget( QWidget* parent, const char* name )
79
setObjectName( name );
82
connect( mHostListView, SIGNAL( currentItemChanged( QListWidgetItem*, QListWidgetItem* ) ),
83
this, SLOT( slotSelectionChanged( QListWidgetItem* ) ) );
84
connect( mHostListView, SIGNAL( itemDoubleClicked( QListWidgetItem* ) ),
85
this, SLOT( slotEditHost() ) );
86
connect( mHostListView, SIGNAL( itemClicked( QListWidgetItem* ) ),
87
this, SLOT( slotItemClicked( QListWidgetItem* ) ) );
89
connect( mUpButton, SIGNAL( clicked() ), this, SLOT( slotMoveUp() ) );
90
connect( mDownButton, SIGNAL( clicked() ), this, SLOT( slotMoveDown() ) );
93
LDAPOptionsWidget::~LDAPOptionsWidget()
97
void LDAPOptionsWidget::slotSelectionChanged( QListWidgetItem *item )
99
bool state = ( item != 0 );
100
mEditButton->setEnabled( state );
101
mRemoveButton->setEnabled( state );
102
mDownButton->setEnabled( item && (mHostListView->row( item ) != (mHostListView->count() - 1)) );
103
mUpButton->setEnabled( item && (mHostListView->row( item ) != 0) );
106
void LDAPOptionsWidget::slotItemClicked( QListWidgetItem *item )
108
LDAPItem *ldapItem = dynamic_cast<LDAPItem*>( item );
112
if ( (ldapItem->checkState() == Qt::Checked) != ldapItem->isActive() ) {
113
emit changed( true );
114
ldapItem->setIsActive( ldapItem->checkState() == Qt::Checked );
118
void LDAPOptionsWidget::slotAddHost()
120
KLDAP::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
KLDAP::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
QListWidgetItem *item = mHostListView->takeItem( mHostListView->currentRow() );
155
slotSelectionChanged( mHostListView->currentItem() );
157
emit changed( true );
160
static void swapItems( LDAPItem *item, LDAPItem *other )
162
KLDAP::LdapServer server = item->server();
163
bool isActive = item->isActive();
164
item->setServer( other->server() );
165
item->setIsActive( other->isActive() );
166
item->setCheckState( other->isActive() ? Qt::Checked : Qt::Unchecked );
167
other->setServer( server );
168
other->setIsActive( isActive );
169
other->setCheckState( isActive ? Qt::Checked : Qt::Unchecked );
172
void LDAPOptionsWidget::slotMoveUp()
174
const QList<QListWidgetItem*> selectedItems = mHostListView->selectedItems();
175
if ( selectedItems.count() == 0 )
178
LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItems().first() );
182
LDAPItem *above = static_cast<LDAPItem *>( mHostListView->item( mHostListView->row( item ) - 1 ) );
186
swapItems( item, above );
188
mHostListView->setCurrentItem( above );
189
above->setSelected( true );
191
emit changed( true );
194
void LDAPOptionsWidget::slotMoveDown()
196
const QList<QListWidgetItem*> selectedItems = mHostListView->selectedItems();
197
if ( selectedItems.count() == 0 )
200
LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItems().first() );
204
LDAPItem *below = static_cast<LDAPItem *>( mHostListView->item( mHostListView->row( item ) + 1 ) );
208
swapItems( item, below );
210
mHostListView->setCurrentItem( below );
211
below->setSelected( true );
213
emit changed( true );
216
void LDAPOptionsWidget::restoreSettings()
218
mHostListView->clear();
219
KConfig *config = KPIM::LdapSearch::config();
220
KConfigGroup group( config, "LDAP" );
224
uint count = group.readEntry( "NumSelectedHosts", 0);
225
for ( uint i = 0; i < count; ++i ) {
226
KLDAP::LdapServer server;
227
KPIM::LdapSearch::readConfig( server, group, i, true );
228
LDAPItem *item = new LDAPItem( mHostListView, server, true );
229
item->setCheckState( Qt::Checked );
232
count = group.readEntry( "NumHosts",0 );
233
for ( uint i = 0; i < count; ++i ) {
234
KLDAP::LdapServer server;
235
KPIM::LdapSearch::readConfig( server, group, i, false );
236
new LDAPItem( mHostListView, server );
239
emit changed( false );
242
void LDAPOptionsWidget::saveSettings()
244
KConfig *config = KPIM::LdapSearch::config();
245
config->deleteGroup( "LDAP" );
247
KConfigGroup group( config, "LDAP" );
249
uint selected = 0; uint unselected = 0;
250
for ( int i = 0; i < mHostListView->count(); ++i ) {
251
LDAPItem *item = dynamic_cast<LDAPItem*>( mHostListView->item( i ) );
255
KLDAP::LdapServer server = item->server();
256
if ( item->checkState() == Qt::Checked ) {
257
KPIM::LdapSearch::writeConfig( server, group, selected, true );
260
KPIM::LdapSearch::writeConfig( server, group, unselected, false );
265
group.writeEntry( "NumSelectedHosts", selected );
266
group.writeEntry( "NumHosts", unselected );
269
emit changed( false );
272
void LDAPOptionsWidget::defaults()
274
// add default configuration here
277
void LDAPOptionsWidget::initGUI()
279
QVBoxLayout *layout = new QVBoxLayout;
280
layout->setSpacing( KDialog::spacingHint() );
281
layout->setMargin( 0 );
284
QGroupBox *groupBox = new QGroupBox( i18n( "LDAP Servers" ), this );
285
QVBoxLayout *mainLayout = new QVBoxLayout( groupBox );
287
// Contents of the QVGroupBox: label and hbox
288
QLabel *label = new QLabel( i18n( "Check all servers that should be used:" ));
289
mainLayout->addWidget(label);
291
KHBox* hBox = new KHBox;
292
hBox->setSpacing( 6 );
293
mainLayout->addWidget(hBox);
294
// Contents of the hbox: listview and up/down buttons on the right (vbox)
295
mHostListView = new QListWidget( hBox );
296
mHostListView->setSortingEnabled( false );
298
KVBox* upDownBox = new KVBox( hBox );
299
upDownBox->setSpacing( 6 );
300
mUpButton = new QToolButton( upDownBox );
301
mUpButton->setIcon( KIcon( "go-up" ) );
302
mUpButton->setIconSize( QSize( KIconLoader::SizeSmall, KIconLoader::SizeSmall ) );
303
mUpButton->setEnabled( false ); // b/c no item is selected yet
305
mDownButton = new QToolButton( upDownBox );
306
mDownButton->setIcon( KIcon( "go-down" ) );
307
mDownButton->setIconSize( QSize( KIconLoader::SizeSmall, KIconLoader::SizeSmall ) );
308
mDownButton->setEnabled( false ); // b/c no item is selected yet
310
QWidget* spacer = new QWidget( upDownBox );
311
upDownBox->setStretchFactor( spacer, 100 );
313
layout->addWidget( groupBox );
315
KDialogButtonBox *buttons = new KDialogButtonBox( this );
316
buttons->addButton( i18n( "&Add Host..." ), QDialogButtonBox::ActionRole, this, SLOT( slotAddHost() ) );
317
mEditButton = buttons->addButton( i18n( "&Edit Host..." ), QDialogButtonBox::ActionRole, this, SLOT( slotEditHost() ) );
318
mEditButton->setEnabled( false );
319
mRemoveButton = buttons->addButton( i18n( "&Remove Host" ), QDialogButtonBox::ActionRole, this, SLOT( slotRemoveHost() ) );
320
mRemoveButton->setEnabled( false );
323
layout->addWidget( buttons );
325
resize( QSize( 460, 300 ).expandedTo( sizeHint() ) );
328
#include "ldapoptionswidget.moc"