27 #include <QListWidget>
28 #include <QListWidgetItem>
29 #include <QToolButton>
30 #include <QVBoxLayout>
31 #include <QPushButton>
33 #include <kaboutdata.h>
34 #include <kapplication.h>
35 #include <kcomponentdata.h>
37 #include <kconfiggroup.h>
38 #include <kdemacros.h>
39 #include <kdialogbuttonbox.h>
40 #include <kgenericfactory.h>
42 #include <kiconloader.h>
45 #include <KMessageBox>
49 #include <kldap/ldapserver.h>
54 K_PLUGIN_FACTORY( KCMLdapFactory, registerPlugin<KCMLdap>(); )
55 K_EXPORT_PLUGIN( KCMLdapFactory(
"kcmldap" ) )
58 class LDAPItem :
public QListWidgetItem
61 LDAPItem(
QListWidget *parent,
const KLDAP::LdapServer &server,
bool isActive =
false )
62 : QListWidgetItem( parent, QListWidgetItem::UserType ),
65 setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable );
66 setCheckState( isActive ? Qt::Checked : Qt::Unchecked );
70 void setServer(
const KLDAP::LdapServer &server )
74 setText( mServer.host() );
77 const KLDAP::LdapServer &server()
const {
return mServer; }
79 void setIsActive(
bool isActive ) { mIsActive = isActive; }
80 bool isActive()
const {
return mIsActive; }
83 KLDAP::LdapServer mServer;
89 :
KCModule( KGlobal::activeComponent(), parent )
91 :
KCModule( KCMLdapFactory::componentData(), parent )
94 setButtons(KCModule::Apply);
95 KAboutData *about =
new KAboutData( I18N_NOOP(
"kcmldap" ), 0,
96 ki18n(
"LDAP Server Settings" ),
97 0, KLocalizedString(), KAboutData::License_LGPL,
98 ki18n(
"(c) 2009 - 2010 Tobias Koenig" ) );
100 about->addAuthor( ki18n(
"Tobias Koenig" ), KLocalizedString(),
"tokoe@kde.org" );
101 KGlobal::locale()->insertCatalog(QLatin1String(
"libkdepim"));
102 setAboutData( about );
103 mClientSearchConfig =
new KLDAP::LdapClientSearchConfig;
106 connect( mHostListView, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
107 this, SLOT(slotSelectionChanged(QListWidgetItem*)) );
108 connect( mHostListView, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
109 this, SLOT(slotEditHost()) );
110 connect( mHostListView, SIGNAL(itemClicked(QListWidgetItem*)),
111 this, SLOT(slotItemClicked(QListWidgetItem*)) );
113 connect( mUpButton, SIGNAL(clicked()),
this, SLOT(slotMoveUp()) );
114 connect( mDownButton, SIGNAL(clicked()),
this, SLOT(slotMoveDown()) );
119 delete mClientSearchConfig;
122 void KCMLdap::slotSelectionChanged( QListWidgetItem *item )
124 bool state = ( item != 0 );
125 mEditButton->setEnabled( state );
126 mRemoveButton->setEnabled( state );
127 mDownButton->setEnabled( item && (mHostListView->row( item ) != (mHostListView->count() - 1)) );
128 mUpButton->setEnabled( item && (mHostListView->row( item ) != 0) );
131 void KCMLdap::slotItemClicked( QListWidgetItem *item )
133 LDAPItem *ldapItem =
dynamic_cast<LDAPItem*
>( item );
138 if ( (ldapItem->checkState() == Qt::Checked) != ldapItem->isActive() ) {
139 emit changed(
true );
140 ldapItem->setIsActive( ldapItem->checkState() == Qt::Checked );
144 void KCMLdap::slotAddHost()
146 KLDAP::LdapServer server;
149 if ( dlg.exec() && !server.host().isEmpty() ) {
150 new LDAPItem( mHostListView, server );
152 emit changed(
true );
156 void KCMLdap::slotEditHost()
158 LDAPItem *item =
dynamic_cast<LDAPItem*
>( mHostListView->currentItem() );
163 KLDAP::LdapServer server = item->server();
165 dlg.setCaption( i18n(
"Edit Host" ) );
167 if ( dlg.exec() && !server.host().isEmpty() ) {
168 item->setServer( server );
170 emit changed(
true );
174 void KCMLdap::slotRemoveHost()
176 QListWidgetItem *item = mHostListView->currentItem();
179 LDAPItem *ldapItem =
dynamic_cast<LDAPItem*
>( item );
180 if (KMessageBox::No == KMessageBox::questionYesNo(
this, i18n(
"Do you want to remove setting for host \"%1\"?", ldapItem->server().host() ), i18n(
"Remove Host")))
183 delete mHostListView->takeItem( mHostListView->currentRow() );
185 slotSelectionChanged( mHostListView->currentItem() );
187 emit changed(
true );
190 static void swapItems( LDAPItem *item, LDAPItem *other )
192 KLDAP::LdapServer server = item->server();
193 bool isActive = item->isActive();
194 item->setServer( other->server() );
195 item->setIsActive( other->isActive() );
196 item->setCheckState( other->isActive() ? Qt::Checked : Qt::Unchecked );
197 other->setServer( server );
198 other->setIsActive( isActive );
199 other->setCheckState( isActive ? Qt::Checked : Qt::Unchecked );
202 void KCMLdap::slotMoveUp()
205 if ( selectedItems.count() == 0 ) {
209 LDAPItem *item =
static_cast<LDAPItem *
>( mHostListView->selectedItems().first() );
214 LDAPItem *above =
static_cast<LDAPItem *
>( mHostListView->item( mHostListView->row( item ) - 1 ) );
221 mHostListView->setCurrentItem( above );
222 above->setSelected(
true );
224 emit changed(
true );
227 void KCMLdap::slotMoveDown()
230 if ( selectedItems.count() == 0 ) {
234 LDAPItem *item =
static_cast<LDAPItem *
>( mHostListView->selectedItems().first() );
239 LDAPItem *below =
static_cast<LDAPItem *
>( mHostListView->item( mHostListView->row( item ) + 1 ) );
246 mHostListView->setCurrentItem( below );
247 below->setSelected(
true );
249 emit changed(
true );
254 mHostListView->clear();
255 KConfig *config = KLDAP::LdapClientSearchConfig::config();
256 KConfigGroup group( config,
"LDAP" );
258 uint count = group.readEntry(
"NumSelectedHosts", 0 );
259 for ( uint i = 0; i < count; ++i ) {
260 KLDAP::LdapServer server;
261 mClientSearchConfig->readConfig( server, group, i,
true );
262 LDAPItem *item =
new LDAPItem( mHostListView, server,
true );
263 item->setCheckState( Qt::Checked );
266 count = group.readEntry(
"NumHosts", 0 );
267 for ( uint i = 0; i < count; ++i ) {
268 KLDAP::LdapServer server;
269 mClientSearchConfig->readConfig( server, group, i,
false );
270 new LDAPItem( mHostListView, server );
273 emit changed(
false );
278 KConfig *config = KLDAP::LdapClientSearchConfig::config();
279 config->deleteGroup(
"LDAP" );
281 KConfigGroup group( config,
"LDAP" );
285 for (
int i = 0; i < mHostListView->count(); ++i ) {
286 LDAPItem *item =
dynamic_cast<LDAPItem*
>( mHostListView->item( i ) );
291 KLDAP::LdapServer server = item->server();
292 if ( item->checkState() == Qt::Checked ) {
293 mClientSearchConfig->writeConfig( server, group, selected,
true );
296 mClientSearchConfig->writeConfig( server, group, unselected,
false );
301 group.writeEntry(
"NumSelectedHosts", selected );
302 group.writeEntry(
"NumHosts", unselected );
305 emit changed(
false );
313 void KCMLdap::initGUI()
315 QVBoxLayout *layout =
new QVBoxLayout;
316 layout->setSpacing( KDialog::spacingHint() );
317 layout->setMargin( 0 );
320 QGroupBox *groupBox =
new QGroupBox( i18n(
"LDAP Servers" ),
this );
321 QVBoxLayout *mainLayout =
new QVBoxLayout( groupBox );
324 QLabel *label =
new QLabel( i18n(
"Check all servers that should be used:" ) );
325 mainLayout->addWidget( label );
328 hBox->setSpacing( 6 );
329 mainLayout->addWidget(hBox);
332 mHostListView->setSortingEnabled(
false );
335 upDownBox->setSpacing( 6 );
336 mUpButton =
new QToolButton( upDownBox );
337 mUpButton->setIcon( KIcon( QLatin1String(
"go-up") ) );
338 mUpButton->setIconSize( QSize( KIconLoader::SizeSmall, KIconLoader::SizeSmall ) );
339 mUpButton->setEnabled(
false );
341 mDownButton =
new QToolButton( upDownBox );
342 mDownButton->setIcon( KIcon( QLatin1String(
"go-down") ) );
343 mDownButton->setIconSize( QSize( KIconLoader::SizeSmall, KIconLoader::SizeSmall ) );
344 mDownButton->setEnabled(
false );
347 upDownBox->setStretchFactor( spacer, 100 );
349 layout->addWidget( groupBox );
351 KDialogButtonBox *buttons =
new KDialogButtonBox(
this );
352 buttons->addButton( i18n(
"&Add Host..." ),
353 QDialogButtonBox::ActionRole,
this, SLOT(slotAddHost()) );
354 mEditButton = buttons->addButton( i18n(
"&Edit Host..." ),
355 QDialogButtonBox::ActionRole,
this, SLOT(slotEditHost()) );
356 mEditButton->setEnabled(
false );
357 mRemoveButton = buttons->addButton( i18n(
"&Remove Host" ),
358 QDialogButtonBox::ActionRole,
this, SLOT(slotRemoveHost()) );
359 mRemoveButton->setEnabled(
false );
362 layout->addWidget( buttons );
364 resize( QSize( 460, 300 ).expandedTo( sizeHint() ) );
367 QWidget* KCMLdap::dialogParent()
376 #include "kcmldap_p.moc"
static void swapItems(CompletionViewItem *one, CompletionViewItem *other)
KCMLdap(QWidget *parent, const QVariantList &args)