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>
53 K_PLUGIN_FACTORY( KCMLdapFactory, registerPlugin<KCMLdap>(); )
54 K_EXPORT_PLUGIN( KCMLdapFactory(
"kcmldap" ) )
59 LDAPItem(
QListWidget *parent,
const KLDAP::LdapServer &server,
bool isActive =
false )
60 : QListWidgetItem( parent, QListWidgetItem::UserType ),
63 setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable );
64 setCheckState( isActive ? Qt::Checked : Qt::Unchecked );
68 void setServer(
const KLDAP::LdapServer &server )
72 setText( mServer.host() );
75 const KLDAP::LdapServer &server()
const {
return mServer; }
77 void setIsActive(
bool isActive ) { mIsActive = isActive; }
78 bool isActive()
const {
return mIsActive; }
81 KLDAP::LdapServer mServer;
86 :
KCModule( KCMLdapFactory::componentData(), parent )
88 setButtons(KCModule::Apply);
89 KAboutData *about =
new KAboutData( I18N_NOOP(
"kcmldap" ), 0,
90 ki18n(
"LDAP Server Settings" ),
91 0, KLocalizedString(), KAboutData::License_LGPL,
92 ki18n(
"(c) 2009 - 2010 Tobias Koenig" ) );
94 about->addAuthor( ki18n(
"Tobias Koenig" ), KLocalizedString(),
"tokoe@kde.org" );
96 setAboutData( about );
97 mClientSearchConfig =
new KLDAP::LdapClientSearchConfig;
100 connect( mHostListView, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
101 this, SLOT(slotSelectionChanged(QListWidgetItem*)) );
102 connect( mHostListView, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
103 this, SLOT(slotEditHost()) );
104 connect( mHostListView, SIGNAL(itemClicked(QListWidgetItem*)),
105 this, SLOT(slotItemClicked(QListWidgetItem*)) );
107 connect( mUpButton, SIGNAL(clicked()),
this, SLOT(slotMoveUp()) );
108 connect( mDownButton, SIGNAL(clicked()),
this, SLOT(slotMoveDown()) );
113 delete mClientSearchConfig;
116 void KCMLdap::slotSelectionChanged( QListWidgetItem *item )
118 bool state = ( item != 0 );
119 mEditButton->setEnabled( state );
120 mRemoveButton->setEnabled( state );
121 mDownButton->setEnabled( item && (mHostListView->row( item ) != (mHostListView->count() - 1)) );
122 mUpButton->setEnabled( item && (mHostListView->row( item ) != 0) );
125 void KCMLdap::slotItemClicked( QListWidgetItem *item )
127 LDAPItem *ldapItem =
dynamic_cast<LDAPItem*
>( item );
132 if ( (ldapItem->checkState() == Qt::Checked) != ldapItem->isActive() ) {
133 emit changed(
true );
134 ldapItem->setIsActive( ldapItem->checkState() == Qt::Checked );
138 void KCMLdap::slotAddHost()
140 KLDAP::LdapServer server;
143 if ( dlg.exec() && !server.host().isEmpty() ) {
144 new LDAPItem( mHostListView, server );
146 emit changed(
true );
150 void KCMLdap::slotEditHost()
152 LDAPItem *item =
dynamic_cast<LDAPItem*
>( mHostListView->currentItem() );
157 KLDAP::LdapServer server = item->server();
159 dlg.setCaption( i18n(
"Edit Host" ) );
161 if ( dlg.exec() && !server.host().isEmpty() ) {
162 item->setServer( server );
164 emit changed(
true );
168 void KCMLdap::slotRemoveHost()
170 QListWidgetItem *item = mHostListView->currentItem();
173 LDAPItem *ldapItem =
dynamic_cast<LDAPItem*
>( item );
174 if (KMessageBox::No == KMessageBox::questionYesNo(
this, i18n(
"Do you want to remove setting for host \"%1\"?", ldapItem->server().host() ), i18n(
"Remove Host")))
177 delete mHostListView->takeItem( mHostListView->currentRow() );
179 slotSelectionChanged( mHostListView->currentItem() );
181 emit changed(
true );
184 static void swapItems( LDAPItem *item, LDAPItem *other )
186 KLDAP::LdapServer server = item->server();
187 bool isActive = item->isActive();
188 item->setServer( other->server() );
189 item->setIsActive( other->isActive() );
190 item->setCheckState( other->isActive() ? Qt::Checked : Qt::Unchecked );
191 other->setServer( server );
192 other->setIsActive( isActive );
193 other->setCheckState( isActive ? Qt::Checked : Qt::Unchecked );
196 void KCMLdap::slotMoveUp()
199 if ( selectedItems.
count() == 0 ) {
203 LDAPItem *item =
static_cast<LDAPItem *
>( mHostListView->selectedItems().first() );
208 LDAPItem *above =
static_cast<LDAPItem *
>( mHostListView->item( mHostListView->row( item ) - 1 ) );
215 mHostListView->setCurrentItem( above );
216 above->setSelected(
true );
218 emit changed(
true );
221 void KCMLdap::slotMoveDown()
224 if ( selectedItems.
count() == 0 ) {
228 LDAPItem *item =
static_cast<LDAPItem *
>( mHostListView->selectedItems().first() );
233 LDAPItem *below =
static_cast<LDAPItem *
>( mHostListView->item( mHostListView->row( item ) + 1 ) );
240 mHostListView->setCurrentItem( below );
241 below->setSelected(
true );
243 emit changed(
true );
248 mHostListView->clear();
249 KConfig *config = KLDAP::LdapClientSearchConfig::config();
250 KConfigGroup group( config,
"LDAP" );
252 uint count = group.readEntry(
"NumSelectedHosts", 0 );
253 for ( uint i = 0; i < count; ++i ) {
254 KLDAP::LdapServer server;
255 mClientSearchConfig->readConfig( server, group, i,
true );
256 LDAPItem *item =
new LDAPItem( mHostListView, server,
true );
257 item->setCheckState( Qt::Checked );
260 count = group.readEntry(
"NumHosts", 0 );
261 for ( uint i = 0; i < count; ++i ) {
262 KLDAP::LdapServer server;
263 mClientSearchConfig->readConfig( server, group, i,
false );
264 new LDAPItem( mHostListView, server );
267 emit changed(
false );
272 KConfig *config = KLDAP::LdapClientSearchConfig::config();
273 config->deleteGroup(
"LDAP" );
275 KConfigGroup group( config,
"LDAP" );
279 for (
int i = 0; i < mHostListView->count(); ++i ) {
280 LDAPItem *item =
dynamic_cast<LDAPItem*
>( mHostListView->item( i ) );
285 KLDAP::LdapServer server = item->server();
286 if ( item->checkState() == Qt::Checked ) {
287 mClientSearchConfig->writeConfig( server, group, selected,
true );
290 mClientSearchConfig->writeConfig( server, group, unselected,
false );
295 group.writeEntry(
"NumSelectedHosts", selected );
296 group.writeEntry(
"NumHosts", unselected );
299 emit changed(
false );
307 void KCMLdap::initGUI()
318 QLabel *label =
new QLabel( i18n(
"Check all servers that should be used:" ) );
322 hBox->setSpacing( 6 );
326 mHostListView->setSortingEnabled(
false );
329 upDownBox->setSpacing( 6 );
332 mUpButton->setIconSize(
QSize( KIconLoader::SizeSmall, KIconLoader::SizeSmall ) );
333 mUpButton->setEnabled(
false );
337 mDownButton->setIconSize(
QSize( KIconLoader::SizeSmall, KIconLoader::SizeSmall ) );
338 mDownButton->setEnabled(
false );
341 upDownBox->setStretchFactor( spacer, 100 );
345 KDialogButtonBox *buttons =
new KDialogButtonBox(
this );
346 buttons->addButton( i18n(
"&Add Host..." ),
347 QDialogButtonBox::ActionRole,
this, SLOT(slotAddHost()) );
348 mEditButton = buttons->addButton( i18n(
"&Edit Host..." ),
349 QDialogButtonBox::ActionRole,
this, SLOT(slotEditHost()) );
350 mEditButton->setEnabled(
false );
351 mRemoveButton = buttons->addButton( i18n(
"&Remove Host" ),
352 QDialogButtonBox::ActionRole,
this, SLOT(slotRemoveHost()) );
353 mRemoveButton->setEnabled(
false );
358 resize(
QSize( 460, 300 ).expandedTo( sizeHint() ) );
361 QWidget* KCMLdap::dialogParent()
370 #include "moc_kcmldap_p.cpp"
int count(const T &value) const
void setMargin(int margin)
KCMLdap(QWidget *parent, const QVariantList &args)
void setSpacing(int spacing)