~ubuntu-branches/ubuntu/karmic/kdepim/karmic-backports

« back to all changes in this revision

Viewing changes to kcontactmanager/xxport/ldap/ldapoptionswidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi, Alessandro Ghersi, Harald Sitter
  • Date: 2009-06-27 04:40:05 UTC
  • mfrom: (1.1.39 upstream)
  • Revision ID: james.westby@ubuntu.com-20090627044005-4y2vm9xz7rvmzi4p
Tags: 4:4.2.95svn20090701-0ubuntu1
[ Alessandro Ghersi ]
* New upstream release
  - Bump build-deps
* Remove akonadi-kde and libmaildir4 packages
  - remove akonadi-kde.install and libmaildir4.install
  - remove libmaildir4 from debian/rules
  - remove akonadi-kde and libmaildir4 from depends
  - remove akonadi-kde and libmaildir4 from installgen
* Update kdepim-dev.install
* Update kpilot.install
* Add akonadi-kde and libmaildir4 transitional packages

[ Harald Sitter ]
* KAddressbook replaces Kontact << 4.2.85 (LP: #378373)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    This file is part of KAddressBook.
3
 
    Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
4
 
 
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.
9
 
 
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.
14
 
 
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.
18
 
 
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.
22
 
*/
23
 
 
24
 
#include "ldapoptionswidget.h"
25
 
 
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>
34
 
 
35
 
#include <kapplication.h>
36
 
#include <kconfig.h>
37
 
#include <kconfiggroup.h>
38
 
#include <KDialogButtonBox>
39
 
#include <khbox.h>
40
 
#include <kiconloader.h>
41
 
#include <klocale.h>
42
 
#include <kvbox.h>
43
 
#include <libkdepim/ldapclient.h>
44
 
 
45
 
#include "addhostdialog.h"
46
 
 
47
 
class LDAPItem : public QListWidgetItem
48
 
{
49
 
  public:
50
 
    LDAPItem( QListWidget *parent, const KLDAP::LdapServer &server, bool isActive = false )
51
 
      : QListWidgetItem( parent, QListWidgetItem::UserType ),
52
 
        mIsActive( isActive )
53
 
    {
54
 
      setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable );
55
 
      setCheckState( isActive ? Qt::Checked : Qt::Unchecked );
56
 
      setServer( server );
57
 
    }
58
 
 
59
 
    void setServer( const KLDAP::LdapServer &server )
60
 
    {
61
 
      mServer = server;
62
 
 
63
 
      setText( mServer.host() );
64
 
    }
65
 
 
66
 
    const KLDAP::LdapServer &server() const { return mServer; }
67
 
 
68
 
    void setIsActive( bool isActive ) { mIsActive = isActive; }
69
 
    bool isActive() const { return mIsActive; }
70
 
 
71
 
  private:
72
 
    KLDAP::LdapServer mServer;
73
 
    bool mIsActive;
74
 
};
75
 
 
76
 
LDAPOptionsWidget::LDAPOptionsWidget( QWidget* parent,  const char* name )
77
 
  : QWidget( parent )
78
 
{
79
 
  setObjectName( name );
80
 
  initGUI();
81
 
 
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* ) ) );
88
 
 
89
 
  connect( mUpButton, SIGNAL( clicked() ), this, SLOT( slotMoveUp() ) );
90
 
  connect( mDownButton, SIGNAL( clicked() ), this, SLOT( slotMoveDown() ) );
91
 
}
92
 
 
93
 
LDAPOptionsWidget::~LDAPOptionsWidget()
94
 
{
95
 
}
96
 
 
97
 
void LDAPOptionsWidget::slotSelectionChanged( QListWidgetItem *item )
98
 
{
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) );
104
 
}
105
 
 
106
 
void LDAPOptionsWidget::slotItemClicked( QListWidgetItem *item )
107
 
{
108
 
  LDAPItem *ldapItem = dynamic_cast<LDAPItem*>( item );
109
 
  if ( !ldapItem )
110
 
    return;
111
 
 
112
 
  if ( (ldapItem->checkState() == Qt::Checked) != ldapItem->isActive() ) {
113
 
    emit changed( true );
114
 
    ldapItem->setIsActive( ldapItem->checkState() == Qt::Checked );
115
 
  }
116
 
}
117
 
 
118
 
void LDAPOptionsWidget::slotAddHost()
119
 
{
120
 
  KLDAP::LdapServer server;
121
 
  AddHostDialog dlg( &server, this );
122
 
 
123
 
  if ( dlg.exec() && !server.host().isEmpty() ) {
124
 
    new LDAPItem( mHostListView, server );
125
 
 
126
 
    emit changed( true );
127
 
  }
128
 
}
129
 
 
130
 
void LDAPOptionsWidget::slotEditHost()
131
 
{
132
 
  LDAPItem *item = dynamic_cast<LDAPItem*>( mHostListView->currentItem() );
133
 
  if ( !item )
134
 
    return;
135
 
 
136
 
  KLDAP::LdapServer server = item->server();
137
 
  AddHostDialog dlg( &server, this );
138
 
  dlg.setCaption( i18n( "Edit Host" ) );
139
 
 
140
 
  if ( dlg.exec() && !server.host().isEmpty() ) {
141
 
    item->setServer( server );
142
 
 
143
 
    emit changed( true );
144
 
  }
145
 
}
146
 
 
147
 
void LDAPOptionsWidget::slotRemoveHost()
148
 
{
149
 
  QListWidgetItem *item = mHostListView->takeItem( mHostListView->currentRow() );
150
 
  if ( !item )
151
 
    return;
152
 
 
153
 
  delete item;
154
 
 
155
 
  slotSelectionChanged( mHostListView->currentItem() );
156
 
 
157
 
  emit changed( true );
158
 
}
159
 
 
160
 
static void swapItems( LDAPItem *item, LDAPItem *other )
161
 
{
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 );
170
 
}
171
 
 
172
 
void LDAPOptionsWidget::slotMoveUp()
173
 
{
174
 
  const QList<QListWidgetItem*> selectedItems = mHostListView->selectedItems();
175
 
  if ( selectedItems.count() == 0 )
176
 
    return;
177
 
 
178
 
  LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItems().first() );
179
 
  if ( !item )
180
 
    return;
181
 
 
182
 
  LDAPItem *above = static_cast<LDAPItem *>( mHostListView->item( mHostListView->row( item ) - 1 ) );
183
 
  if ( !above )
184
 
    return;
185
 
 
186
 
  swapItems( item, above );
187
 
 
188
 
  mHostListView->setCurrentItem( above );
189
 
  above->setSelected( true );
190
 
 
191
 
  emit changed( true );
192
 
}
193
 
 
194
 
void LDAPOptionsWidget::slotMoveDown()
195
 
{
196
 
  const QList<QListWidgetItem*> selectedItems = mHostListView->selectedItems();
197
 
  if ( selectedItems.count() == 0 )
198
 
    return;
199
 
 
200
 
  LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItems().first() );
201
 
  if ( !item )
202
 
    return;
203
 
 
204
 
  LDAPItem *below = static_cast<LDAPItem *>( mHostListView->item( mHostListView->row( item ) + 1 ) );
205
 
  if ( !below )
206
 
    return;
207
 
 
208
 
  swapItems( item, below );
209
 
 
210
 
  mHostListView->setCurrentItem( below );
211
 
  below->setSelected( true );
212
 
 
213
 
  emit changed( true );
214
 
}
215
 
 
216
 
void LDAPOptionsWidget::restoreSettings()
217
 
{
218
 
  mHostListView->clear();
219
 
  KConfig *config = KPIM::LdapSearch::config();
220
 
  KConfigGroup group( config, "LDAP" );
221
 
 
222
 
  QString host;
223
 
 
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 );
230
 
  }
231
 
 
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 );
237
 
  }
238
 
 
239
 
  emit changed( false );
240
 
}
241
 
 
242
 
void LDAPOptionsWidget::saveSettings()
243
 
{
244
 
  KConfig *config = KPIM::LdapSearch::config();
245
 
  config->deleteGroup( "LDAP" );
246
 
 
247
 
  KConfigGroup group( config, "LDAP" );
248
 
 
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 ) );
252
 
    if ( !item )
253
 
      continue;
254
 
 
255
 
    KLDAP::LdapServer server = item->server();
256
 
    if ( item->checkState() == Qt::Checked ) {
257
 
      KPIM::LdapSearch::writeConfig( server, group, selected, true );
258
 
      selected++;
259
 
    } else {
260
 
      KPIM::LdapSearch::writeConfig( server, group, unselected, false );
261
 
      unselected++;
262
 
    }
263
 
  }
264
 
 
265
 
  group.writeEntry( "NumSelectedHosts", selected );
266
 
  group.writeEntry( "NumHosts", unselected );
267
 
  config->sync();
268
 
 
269
 
  emit changed( false );
270
 
}
271
 
 
272
 
void LDAPOptionsWidget::defaults()
273
 
{
274
 
  // add default configuration here
275
 
}
276
 
 
277
 
void LDAPOptionsWidget::initGUI()
278
 
{
279
 
  QVBoxLayout *layout = new QVBoxLayout;
280
 
  layout->setSpacing( KDialog::spacingHint() );
281
 
  layout->setMargin( 0 );
282
 
  setLayout(layout);
283
 
 
284
 
  QGroupBox *groupBox = new QGroupBox( i18n( "LDAP Servers" ), this );
285
 
  QVBoxLayout *mainLayout = new QVBoxLayout( groupBox );
286
 
 
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);
290
 
 
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 );
297
 
 
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
304
 
 
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
309
 
 
310
 
  QWidget* spacer = new QWidget( upDownBox );
311
 
  upDownBox->setStretchFactor( spacer, 100 );
312
 
 
313
 
  layout->addWidget( groupBox );
314
 
 
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 );
321
 
  buttons->layout();
322
 
 
323
 
  layout->addWidget( buttons );
324
 
 
325
 
  resize( QSize( 460, 300 ).expandedTo( sizeHint() ) );
326
 
}
327
 
 
328
 
#include "ldapoptionswidget.moc"