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

libkdepim

  • sources
  • kde-4.14
  • 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-2015 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 K_PLUGIN_FACTORY( KCMLdapFactory, registerPlugin<KCMLdap>(); )
54 K_EXPORT_PLUGIN( KCMLdapFactory( "kcmldap" ) )
55 
56 class LDAPItem : public QListWidgetItem
57 {
58 public:
59  LDAPItem( QListWidget *parent, const KLDAP::LdapServer &server, bool isActive = false )
60  : QListWidgetItem( parent, QListWidgetItem::UserType ),
61  mIsActive( isActive )
62  {
63  setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable );
64  setCheckState( isActive ? Qt::Checked : Qt::Unchecked );
65  setServer( server );
66  }
67 
68  void setServer( const KLDAP::LdapServer &server )
69  {
70  mServer = server;
71 
72  setText( mServer.host() );
73  }
74 
75  const KLDAP::LdapServer &server() const { return mServer; }
76 
77  void setIsActive( bool isActive ) { mIsActive = isActive; }
78  bool isActive() const { return mIsActive; }
79 
80 private:
81  KLDAP::LdapServer mServer;
82  bool mIsActive;
83 };
84 
85 KCMLdap::KCMLdap( QWidget *parent, const QVariantList& )
86  : KCModule( KCMLdapFactory::componentData(), parent )
87 {
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" ) );
93 
94  about->addAuthor( ki18n( "Tobias Koenig" ), KLocalizedString(), "tokoe@kde.org" );
95  KGlobal::locale()->insertCatalog(QLatin1String("libkdepim"));
96  setAboutData( about );
97  mClientSearchConfig = new KLDAP::LdapClientSearchConfig;
98  initGUI();
99 
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*)) );
106 
107  connect( mUpButton, SIGNAL(clicked()), this, SLOT(slotMoveUp()) );
108  connect( mDownButton, SIGNAL(clicked()), this, SLOT(slotMoveDown()) );
109 }
110 
111 KCMLdap::~KCMLdap()
112 {
113  delete mClientSearchConfig;
114 }
115 
116 void KCMLdap::slotSelectionChanged( QListWidgetItem *item )
117 {
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) );
123 }
124 
125 void KCMLdap::slotItemClicked( QListWidgetItem *item )
126 {
127  LDAPItem *ldapItem = dynamic_cast<LDAPItem*>( item );
128  if ( !ldapItem ) {
129  return;
130  }
131 
132  if ( (ldapItem->checkState() == Qt::Checked) != ldapItem->isActive() ) {
133  emit changed( true );
134  ldapItem->setIsActive( ldapItem->checkState() == Qt::Checked );
135  }
136 }
137 
138 void KCMLdap::slotAddHost()
139 {
140  KLDAP::LdapServer server;
141  AddHostDialog dlg( &server, dialogParent() );
142 
143  if ( dlg.exec() && !server.host().isEmpty() ) { //krazy:exclude=crashy
144  new LDAPItem( mHostListView, server );
145 
146  emit changed( true );
147  }
148 }
149 
150 void KCMLdap::slotEditHost()
151 {
152  LDAPItem *item = dynamic_cast<LDAPItem*>( mHostListView->currentItem() );
153  if ( !item ) {
154  return;
155  }
156 
157  KLDAP::LdapServer server = item->server();
158  AddHostDialog dlg( &server, dialogParent() );
159  dlg.setCaption( i18n( "Edit Host" ) );
160 
161  if ( dlg.exec() && !server.host().isEmpty() ) { //krazy:exclude=crashy
162  item->setServer( server );
163 
164  emit changed( true );
165  }
166 }
167 
168 void KCMLdap::slotRemoveHost()
169 {
170  QListWidgetItem *item = mHostListView->currentItem();
171  if (!item)
172  return;
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")))
175  return;
176 
177  delete mHostListView->takeItem( mHostListView->currentRow() );
178 
179  slotSelectionChanged( mHostListView->currentItem() );
180 
181  emit changed( true );
182 }
183 
184 static void swapItems( LDAPItem *item, LDAPItem *other )
185 {
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 );
194 }
195 
196 void KCMLdap::slotMoveUp()
197 {
198  const QList<QListWidgetItem*> selectedItems = mHostListView->selectedItems();
199  if ( selectedItems.count() == 0 ) {
200  return;
201  }
202 
203  LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItems().first() );
204  if ( !item ) {
205  return;
206  }
207 
208  LDAPItem *above = static_cast<LDAPItem *>( mHostListView->item( mHostListView->row( item ) - 1 ) );
209  if ( !above ) {
210  return;
211  }
212 
213  swapItems( item, above );
214 
215  mHostListView->setCurrentItem( above );
216  above->setSelected( true );
217 
218  emit changed( true );
219 }
220 
221 void KCMLdap::slotMoveDown()
222 {
223  const QList<QListWidgetItem*> selectedItems = mHostListView->selectedItems();
224  if ( selectedItems.count() == 0 ) {
225  return;
226  }
227 
228  LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItems().first() );
229  if ( !item ) {
230  return;
231  }
232 
233  LDAPItem *below = static_cast<LDAPItem *>( mHostListView->item( mHostListView->row( item ) + 1 ) );
234  if ( !below ) {
235  return;
236  }
237 
238  swapItems( item, below );
239 
240  mHostListView->setCurrentItem( below );
241  below->setSelected( true );
242 
243  emit changed( true );
244 }
245 
246 void KCMLdap::load()
247 {
248  mHostListView->clear();
249  KConfig *config = KLDAP::LdapClientSearchConfig::config();
250  KConfigGroup group( config, "LDAP" );
251 
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 );
258  }
259 
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 );
265  }
266 
267  emit changed( false );
268 }
269 
270 void KCMLdap::save()
271 {
272  KConfig *config = KLDAP::LdapClientSearchConfig::config();
273  config->deleteGroup( "LDAP" );
274 
275  KConfigGroup group( config, "LDAP" );
276 
277  uint selected = 0;
278  uint unselected = 0;
279  for ( int i = 0; i < mHostListView->count(); ++i ) {
280  LDAPItem *item = dynamic_cast<LDAPItem*>( mHostListView->item( i ) );
281  if ( !item ) {
282  continue;
283  }
284 
285  KLDAP::LdapServer server = item->server();
286  if ( item->checkState() == Qt::Checked ) {
287  mClientSearchConfig->writeConfig( server, group, selected, true );
288  selected++;
289  } else {
290  mClientSearchConfig->writeConfig( server, group, unselected, false );
291  unselected++;
292  }
293  }
294 
295  group.writeEntry( "NumSelectedHosts", selected );
296  group.writeEntry( "NumHosts", unselected );
297  config->sync();
298 
299  emit changed( false );
300 }
301 
302 void KCMLdap::defaults()
303 {
304  // add default configuration here
305 }
306 
307 void KCMLdap::initGUI()
308 {
309  QVBoxLayout *layout = new QVBoxLayout;
310  layout->setSpacing( KDialog::spacingHint() );
311  layout->setMargin( 0 );
312  setLayout(layout);
313 
314  QGroupBox *groupBox = new QGroupBox( i18n( "LDAP Servers" ), this );
315  QVBoxLayout *mainLayout = new QVBoxLayout( groupBox );
316 
317  // Contents of the QVGroupBox: label and hbox
318  QLabel *label = new QLabel( i18n( "Check all servers that should be used:" ) );
319  mainLayout->addWidget( label );
320 
321  KHBox *hBox = new KHBox;
322  hBox->setSpacing( 6 );
323  mainLayout->addWidget(hBox);
324  // Contents of the hbox: listview and up/down buttons on the right (vbox)
325  mHostListView = new QListWidget( hBox );
326  mHostListView->setSortingEnabled( false );
327 
328  KVBox *upDownBox = new KVBox( hBox );
329  upDownBox->setSpacing( 6 );
330  mUpButton = new QToolButton( upDownBox );
331  mUpButton->setIcon( KIcon( QLatin1String("go-up") ) );
332  mUpButton->setIconSize( QSize( KIconLoader::SizeSmall, KIconLoader::SizeSmall ) );
333  mUpButton->setEnabled( false ); // b/c no item is selected yet
334 
335  mDownButton = new QToolButton( upDownBox );
336  mDownButton->setIcon( KIcon( QLatin1String("go-down") ) );
337  mDownButton->setIconSize( QSize( KIconLoader::SizeSmall, KIconLoader::SizeSmall ) );
338  mDownButton->setEnabled( false ); // b/c no item is selected yet
339 
340  QWidget *spacer = new QWidget( upDownBox );
341  upDownBox->setStretchFactor( spacer, 100 );
342 
343  layout->addWidget( groupBox );
344 
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 );
354  buttons->layout();
355 
356  layout->addWidget( buttons );
357 
358  resize( QSize( 460, 300 ).expandedTo( sizeHint() ) );
359 }
360 
361 QWidget* KCMLdap::dialogParent()
362 {
363 #ifdef Q_WS_MAEMO_5
364  return 0;
365 #else
366  return this;
367 #endif
368 }
369 
370 #include "moc_kcmldap_p.cpp"
QWidget
KVBox
swapItems
static void swapItems(CompletionViewItem *one, CompletionViewItem *other)
Definition: completionorderwidget.cpp:339
ldapclientsearch.h
KCMLdap::defaults
void defaults()
KCMLdap::~KCMLdap
~KCMLdap()
KCMLdap::save
void save()
QListWidgetItem
QListWidget
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QList::count
int count(const T &value) const
QGroupBox
ldapclientsearchconfig.h
addhostdialog_p.h
kcmldap_p.h
QVBoxLayout
QList
QLayout::setMargin
void setMargin(int margin)
QToolButton
QSize
QLatin1String
AddHostDialog
Definition: addhostdialog_p.h:32
KCMLdap::load
void load()
KCMLdap::KCMLdap
KCMLdap(QWidget *parent, const QVariantList &args)
KHBox
QLabel
KCModule
QBoxLayout::setSpacing
void setSpacing(int spacing)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:50 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
  • pimprint

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