• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

libkdepim

  • sources
  • kde-4.12
  • kdepim
  • libkdepim
  • ldap
kcmldap.cpp
Go to the documentation of this file.
1 /*
2  This file is part of libkldap.
3 
4  Copyright (c) 2002-2009 Tobias Koenig <tokoe@kde.org>
5  Copyright (c) 2013 Laurent Montel <montel@kde.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include "kcmldap_p.h"
24 
25 #include <QGroupBox>
26 #include <QLabel>
27 #include <QListWidget>
28 #include <QListWidgetItem>
29 #include <QToolButton>
30 #include <QVBoxLayout>
31 #include <QPushButton>
32 
33 #include <kaboutdata.h>
34 #include <kapplication.h>
35 #include <kcomponentdata.h>
36 #include <kconfig.h>
37 #include <kconfiggroup.h>
38 #include <kdemacros.h>
39 #include <kdialogbuttonbox.h>
40 #include <kgenericfactory.h>
41 #include <khbox.h>
42 #include <kiconloader.h>
43 #include <klocale.h>
44 #include <kvbox.h>
45 #include <KMessageBox>
46 
47 #include "ldapclientsearch.h"
48 #include "ldapclientsearchconfig.h"
49 #include <kldap/ldapserver.h>
50 
51 #include "addhostdialog_p.h"
52 
53 #ifndef Q_OS_WINCE
54 K_PLUGIN_FACTORY( KCMLdapFactory, registerPlugin<KCMLdap>(); )
55 K_EXPORT_PLUGIN( KCMLdapFactory( "kcmldap" ) )
56 #endif
57 
58 class LDAPItem : public QListWidgetItem
59 {
60  public:
61  LDAPItem( QListWidget *parent, const KLDAP::LdapServer &server, bool isActive = false )
62  : QListWidgetItem( parent, QListWidgetItem::UserType ),
63  mIsActive( isActive )
64  {
65  setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable );
66  setCheckState( isActive ? Qt::Checked : Qt::Unchecked );
67  setServer( server );
68  }
69 
70  void setServer( const KLDAP::LdapServer &server )
71  {
72  mServer = server;
73 
74  setText( mServer.host() );
75  }
76 
77  const KLDAP::LdapServer &server() const { return mServer; }
78 
79  void setIsActive( bool isActive ) { mIsActive = isActive; }
80  bool isActive() const { return mIsActive; }
81 
82  private:
83  KLDAP::LdapServer mServer;
84  bool mIsActive;
85 };
86 
87 KCMLdap::KCMLdap( QWidget *parent, const QVariantList& )
88 #ifdef Q_OS_WINCE
89  : KCModule( KGlobal::activeComponent(), parent )
90 #else
91  : KCModule( KCMLdapFactory::componentData(), parent )
92 #endif // Q_OS_WINCE
93 {
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" ) );
99 
100  about->addAuthor( ki18n( "Tobias Koenig" ), KLocalizedString(), "tokoe@kde.org" );
101  KGlobal::locale()->insertCatalog(QLatin1String("libkdepim"));
102  setAboutData( about );
103  mClientSearchConfig = new KLDAP::LdapClientSearchConfig;
104  initGUI();
105 
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*)) );
112 
113  connect( mUpButton, SIGNAL(clicked()), this, SLOT(slotMoveUp()) );
114  connect( mDownButton, SIGNAL(clicked()), this, SLOT(slotMoveDown()) );
115 }
116 
117 KCMLdap::~KCMLdap()
118 {
119  delete mClientSearchConfig;
120 }
121 
122 void KCMLdap::slotSelectionChanged( QListWidgetItem *item )
123 {
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) );
129 }
130 
131 void KCMLdap::slotItemClicked( QListWidgetItem *item )
132 {
133  LDAPItem *ldapItem = dynamic_cast<LDAPItem*>( item );
134  if ( !ldapItem ) {
135  return;
136  }
137 
138  if ( (ldapItem->checkState() == Qt::Checked) != ldapItem->isActive() ) {
139  emit changed( true );
140  ldapItem->setIsActive( ldapItem->checkState() == Qt::Checked );
141  }
142 }
143 
144 void KCMLdap::slotAddHost()
145 {
146  KLDAP::LdapServer server;
147  AddHostDialog dlg( &server, dialogParent() );
148 
149  if ( dlg.exec() && !server.host().isEmpty() ) { //krazy:exclude=crashy
150  new LDAPItem( mHostListView, server );
151 
152  emit changed( true );
153  }
154 }
155 
156 void KCMLdap::slotEditHost()
157 {
158  LDAPItem *item = dynamic_cast<LDAPItem*>( mHostListView->currentItem() );
159  if ( !item ) {
160  return;
161  }
162 
163  KLDAP::LdapServer server = item->server();
164  AddHostDialog dlg( &server, dialogParent() );
165  dlg.setCaption( i18n( "Edit Host" ) );
166 
167  if ( dlg.exec() && !server.host().isEmpty() ) { //krazy:exclude=crashy
168  item->setServer( server );
169 
170  emit changed( true );
171  }
172 }
173 
174 void KCMLdap::slotRemoveHost()
175 {
176  QListWidgetItem *item = mHostListView->currentItem();
177  if (!item)
178  return;
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")))
181  return;
182 
183  delete mHostListView->takeItem( mHostListView->currentRow() );
184 
185  slotSelectionChanged( mHostListView->currentItem() );
186 
187  emit changed( true );
188 }
189 
190 static void swapItems( LDAPItem *item, LDAPItem *other )
191 {
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 );
200 }
201 
202 void KCMLdap::slotMoveUp()
203 {
204  const QList<QListWidgetItem*> selectedItems = mHostListView->selectedItems();
205  if ( selectedItems.count() == 0 ) {
206  return;
207  }
208 
209  LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItems().first() );
210  if ( !item ) {
211  return;
212  }
213 
214  LDAPItem *above = static_cast<LDAPItem *>( mHostListView->item( mHostListView->row( item ) - 1 ) );
215  if ( !above ) {
216  return;
217  }
218 
219  swapItems( item, above );
220 
221  mHostListView->setCurrentItem( above );
222  above->setSelected( true );
223 
224  emit changed( true );
225 }
226 
227 void KCMLdap::slotMoveDown()
228 {
229  const QList<QListWidgetItem*> selectedItems = mHostListView->selectedItems();
230  if ( selectedItems.count() == 0 ) {
231  return;
232  }
233 
234  LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItems().first() );
235  if ( !item ) {
236  return;
237  }
238 
239  LDAPItem *below = static_cast<LDAPItem *>( mHostListView->item( mHostListView->row( item ) + 1 ) );
240  if ( !below ) {
241  return;
242  }
243 
244  swapItems( item, below );
245 
246  mHostListView->setCurrentItem( below );
247  below->setSelected( true );
248 
249  emit changed( true );
250 }
251 
252 void KCMLdap::load()
253 {
254  mHostListView->clear();
255  KConfig *config = KLDAP::LdapClientSearchConfig::config();
256  KConfigGroup group( config, "LDAP" );
257 
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 );
264  }
265 
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 );
271  }
272 
273  emit changed( false );
274 }
275 
276 void KCMLdap::save()
277 {
278  KConfig *config = KLDAP::LdapClientSearchConfig::config();
279  config->deleteGroup( "LDAP" );
280 
281  KConfigGroup group( config, "LDAP" );
282 
283  uint selected = 0;
284  uint unselected = 0;
285  for ( int i = 0; i < mHostListView->count(); ++i ) {
286  LDAPItem *item = dynamic_cast<LDAPItem*>( mHostListView->item( i ) );
287  if ( !item ) {
288  continue;
289  }
290 
291  KLDAP::LdapServer server = item->server();
292  if ( item->checkState() == Qt::Checked ) {
293  mClientSearchConfig->writeConfig( server, group, selected, true );
294  selected++;
295  } else {
296  mClientSearchConfig->writeConfig( server, group, unselected, false );
297  unselected++;
298  }
299  }
300 
301  group.writeEntry( "NumSelectedHosts", selected );
302  group.writeEntry( "NumHosts", unselected );
303  config->sync();
304 
305  emit changed( false );
306 }
307 
308 void KCMLdap::defaults()
309 {
310  // add default configuration here
311 }
312 
313 void KCMLdap::initGUI()
314 {
315  QVBoxLayout *layout = new QVBoxLayout;
316  layout->setSpacing( KDialog::spacingHint() );
317  layout->setMargin( 0 );
318  setLayout(layout);
319 
320  QGroupBox *groupBox = new QGroupBox( i18n( "LDAP Servers" ), this );
321  QVBoxLayout *mainLayout = new QVBoxLayout( groupBox );
322 
323  // Contents of the QVGroupBox: label and hbox
324  QLabel *label = new QLabel( i18n( "Check all servers that should be used:" ) );
325  mainLayout->addWidget( label );
326 
327  KHBox *hBox = new KHBox;
328  hBox->setSpacing( 6 );
329  mainLayout->addWidget(hBox);
330  // Contents of the hbox: listview and up/down buttons on the right (vbox)
331  mHostListView = new QListWidget( hBox );
332  mHostListView->setSortingEnabled( false );
333 
334  KVBox *upDownBox = new KVBox( hBox );
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 ); // b/c no item is selected yet
340 
341  mDownButton = new QToolButton( upDownBox );
342  mDownButton->setIcon( KIcon( QLatin1String("go-down") ) );
343  mDownButton->setIconSize( QSize( KIconLoader::SizeSmall, KIconLoader::SizeSmall ) );
344  mDownButton->setEnabled( false ); // b/c no item is selected yet
345 
346  QWidget *spacer = new QWidget( upDownBox );
347  upDownBox->setStretchFactor( spacer, 100 );
348 
349  layout->addWidget( groupBox );
350 
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 );
360  buttons->layout();
361 
362  layout->addWidget( buttons );
363 
364  resize( QSize( 460, 300 ).expandedTo( sizeHint() ) );
365 }
366 
367 QWidget* KCMLdap::dialogParent()
368 {
369 #ifdef Q_WS_MAEMO_5
370  return 0;
371 #else
372  return this;
373 #endif
374 }
375 
376 #include "kcmldap_p.moc"
KVBox
ldapclientsearch.h
KCMLdap::defaults
void defaults()
KCMLdap::~KCMLdap
~KCMLdap()
QWidget
KCMLdap::save
void save()
QListWidget
ldapclientsearchconfig.h
swapItems
static void swapItems(CompletionViewItem *one, CompletionViewItem *other)
Definition: completionordereditor.cpp:332
addhostdialog_p.h
kcmldap_p.h
AddHostDialog
Definition: addhostdialog_p.h:32
KCMLdap::load
void load()
QLabel
KCMLdap::KCMLdap
KCMLdap(QWidget *parent, const QVariantList &args)
KHBox
KCModule
QList
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:03 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdepim

Skip menu "libkdepim"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal