summaryrefslogtreecommitdiffstats
path: root/certmanager/conf/dirservconfigpage.cpp
blob: fb367fd32e1ae62d5122309ca7536415dac1aff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
    dirservconfigpage.cpp

    This file is part of kleopatra
    Copyright (c) 2004 Klarälvdalens Datakonsult AB

    Libkleopatra is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License,
    version 2, as published by the Free Software Foundation.

    Libkleopatra is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the TQt library by Trolltech AS, Norway (or with modified versions
    of TQt that use the same license as TQt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    TQt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
*/

#include "dirservconfigpage.h"
#include "directoryserviceswidget.h"

#include <kleo/cryptobackendfactory.h>

#include <kmessagebox.h>
#include <klocale.h>
#include <kdebug.h>
#include <kconfig.h>
#include <knuminput.h>
#include <kdialog.h>

#include <tqhbox.h>
#include <tqlabel.h>
#include <tqdatetimeedit.h>
#include <tqcheckbox.h>
#include <tqlayout.h>

#include <kdepimmacros.h>

#if 0 // disabled, since it is aptqparently confusing
// For sync'ing kabldaprc
class KABSynchronizer
{
public:
  KABSynchronizer()
    : mConfig( "kabldaprc" ) {
    mConfig.setGroup( "LDAP" );
  }

  KURL::List readCurrentList() const {

    KURL::List lst;
    // stolen from kabc/ldapclient.cpp
    const uint numHosts = mConfig.readUnsignedNumEntry( "NumSelectedHosts" );
    for ( uint j = 0; j < numHosts; j++ ) {
      const TQString num = TQString::number( j );

      KURL url;
      url.setProtocol( "ldap" );
      url.setPath( "/" ); // workaround KURL parsing bug
      const TQString host = mConfig.readEntry( TQString( "SelectedHost" ) + num ).stripWhiteSpace();
      url.setHost( host );

      const int port = mConfig.readUnsignedNumEntry( TQString( "SelectedPort" ) + num );
      if ( port != 0 )
        url.setPort( port );

      const TQString base = mConfig.readEntry( TQString( "SelectedBase" ) + num ).stripWhiteSpace();
      url.setQuery( base );

      const TQString bindDN = mConfig.readEntry( TQString( "SelectedBind" ) + num ).stripWhiteSpace();
      url.setUser( bindDN );

      const TQString pwdBindDN = mConfig.readEntry( TQString( "SelectedPwdBind" ) + num ).stripWhiteSpace();
      url.setPass( pwdBindDN );
      lst.append( url );
    }
    return lst;
  }

  void writeList( const KURL::List& lst ) {

    mConfig.writeEntry( "NumSelectedHosts", lst.count() );

    KURL::List::const_iterator it = lst.begin();
    KURL::List::const_iterator end = lst.end();
    unsigned j = 0;
    for( ; it != end; ++it, ++j ) {
      const TQString num = TQString::number( j );
      KURL url = *it;

      Q_ASSERT( url.protocol() == "ldap" );
      mConfig.writeEntry( TQString( "SelectedHost" ) + num, url.host() );
      mConfig.writeEntry( TQString( "SelectedPort" ) + num, url.port() );

      // KURL automatically encoded the query (e.g. for spaces inside it),
      // so decode it before writing it out
      const TQString base = KURL::decode_string( url.query().mid(1) );
      mConfig.writeEntry( TQString( "SelectedBase" ) + num, base );
      mConfig.writeEntry( TQString( "SelectedBind" ) + num, url.user() );
      mConfig.writeEntry( TQString( "SelectedPwdBind" ) + num, url.pass() );
    }
    mConfig.sync();
  }

private:
  KConfig mConfig;
};

#endif

static const char s_dirserv_componentName[] = "dirmngr";
static const char s_dirserv_groupName[] = "LDAP";
static const char s_dirserv_entryName[] = "LDAP Server";

static const char s_timeout_componentName[] = "dirmngr";
static const char s_timeout_groupName[] = "LDAP";
static const char s_timeout_entryName[] = "ldaptimeout";

static const char s_maxitems_componentName[] = "dirmngr";
static const char s_maxitems_groupName[] = "LDAP";
static const char s_maxitems_entryName[] = "max-replies";

static const char s_addnewservers_componentName[] = "dirmngr";
static const char s_addnewservers_groupName[] = "LDAP";
static const char s_addnewservers_entryName[] = "add-servers";

DirectoryServicesConfigurationPage::DirectoryServicesConfigurationPage( TQWidget * tqparent, const char * name )
    : KCModule( tqparent, name )
{
  mConfig = Kleo::CryptoBackendFactory::instance()->config();
  TQVBoxLayout* lay = new TQVBoxLayout( this, 0, KDialog::spacingHint() );
  Kleo::CryptoConfigEntry* entry = configEntry( s_dirserv_componentName, s_dirserv_groupName, s_dirserv_entryName,
                                                Kleo::CryptoConfigEntry::ArgType_LDAPURL, true );
  mWidget = new Kleo::DirectoryServicesWidget( entry, this );
  lay->addWidget( mWidget );
  connect( mWidget, TQT_SIGNAL( changed() ), this, TQT_SLOT( slotChanged() ) );

  // LDAP timeout
  TQHBox* box = new TQHBox( this );
  box->setSpacing( KDialog::spacingHint() );
  lay->addWidget( box );
  TQLabel* label = new TQLabel( i18n( "LDAP &timeout (minutes:seconds)" ), box );
  mTimeout = new TQTimeEdit( box );
  mTimeout->setDisplay( TQTimeEdit::Minutes | TQTimeEdit::Seconds );
  connect( mTimeout, TQT_SIGNAL( valueChanged( const TQTime& ) ), this, TQT_SLOT( slotChanged() ) );
  label->setBuddy( mTimeout );
  TQWidget* stretch = new TQWidget( box );
  box->setStretchFactor( stretch, 2 );

  // Max number of items returned by queries
  box = new TQHBox( this );
  box->setSpacing( KDialog::spacingHint() );
  lay->addWidget( box );
  mMaxItems = new KIntNumInput( box );
  mMaxItems->setLabel( i18n( "&Maximum number of items returned by query" ), TQt::AlignLeft | TQt::AlignVCenter );
  mMaxItems->setMinValue( 0 );
  connect( mMaxItems, TQT_SIGNAL( valueChanged(int) ), this, TQT_SLOT( slotChanged() ) );
  stretch = new TQWidget( box );
  box->setStretchFactor( stretch, 2 );

#ifdef NOT_USEFUL_CURRENTLY
  mAddNewServersCB = new TQCheckBox( i18n( "Automatically add &new servers discovered in CRL distribution points" ), this );
  connect( mAddNewServersCB, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotChanged() ) );
  lay->addWidget( mAddNewServersCB );
#endif

#ifndef HAVE_UNBROKEN_KCMULTIDIALOG
  load();
#endif
}

void DirectoryServicesConfigurationPage::load()
{
  mWidget->load();

  mTimeoutConfigEntry = configEntry( s_timeout_componentName, s_timeout_groupName, s_timeout_entryName, Kleo::CryptoConfigEntry::ArgType_UInt, false );
  if ( mTimeoutConfigEntry ) {
    TQTime time = TQTime().addSecs( mTimeoutConfigEntry->uintValue() );
    //kdDebug() << "timeout:" << mTimeoutConfigEntry->uintValue() << "  -> " << time << endl;
    mTimeout->setTime( time );
  }

  mMaxItemsConfigEntry = configEntry( s_maxitems_componentName, s_maxitems_groupName, s_maxitems_entryName, Kleo::CryptoConfigEntry::ArgType_UInt, false );
  if ( mMaxItemsConfigEntry ) {
    mMaxItems->blockSignals( true ); // KNumInput emits valueChanged from setValue!
    mMaxItems->setValue( mMaxItemsConfigEntry->uintValue() );
    mMaxItems->blockSignals( false );
  }

#ifdef NOT_USEFUL_CURRENTLY
  mAddNewServersConfigEntry = configEntry( s_addnewservers_componentName, s_addnewservers_groupName, s_addnewservers_entryName, Kleo::CryptoConfigEntry::ArgType_None, false );
  if ( mAddNewServersConfigEntry ) {
    mAddNewServersCB->setChecked( mAddNewServersConfigEntry->boolValue() );
  }
#endif
}

void DirectoryServicesConfigurationPage::save()
{
  mWidget->save();

  TQTime time( mTimeout->time() );
  unsigned int timeout = time.minute() * 60 + time.second();
  if ( mTimeoutConfigEntry && mTimeoutConfigEntry->uintValue() != timeout )
    mTimeoutConfigEntry->setUIntValue( timeout );
  if ( mMaxItemsConfigEntry && mMaxItemsConfigEntry->uintValue() != (uint)mMaxItems->value() )
    mMaxItemsConfigEntry->setUIntValue( mMaxItems->value() );
#ifdef NOT_USEFUL_CURRENTLY
  if ( mAddNewServersConfigEntry && mAddNewServersConfigEntry->boolValue() != mAddNewServersCB->isChecked() )
    mAddNewServersConfigEntry->setBoolValue( mAddNewServersCB->isChecked() );
#endif

  mConfig->sync( true );

#if 0
  // Also write the LDAP URLs to kabldaprc so that they are used by kaddressbook
  KABSynchronizer sync;
  const KURL::List toAdd = mWidget->urlList();
  KURL::List currentList = sync.readCurrentList();

  KURL::List::const_iterator it = toAdd.begin();
  KURL::List::const_iterator end = toAdd.end();
  for( ; it != end; ++it ) {
    // check if the URL is already in currentList
    if ( currentList.tqfind( *it ) == currentList.end() )
      // if not, add it
      currentList.append( *it );
  }
  sync.writeList( currentList );
#endif
}

void DirectoryServicesConfigurationPage::defaults()
{
  mWidget->defaults();
  if ( mTimeoutConfigEntry )
    mTimeoutConfigEntry->resetToDefault();
  if ( mMaxItemsConfigEntry )
    mMaxItemsConfigEntry->resetToDefault();
#ifdef NOT_USEFUL_CURRENTLY
  if ( mAddNewServersConfigEntry )
    mAddNewServersConfigEntry->resetToDefault();
#endif
  load();
}

extern "C"
{
  KDE_EXPORT KCModule *create_kleopatra_config_dirserv( TQWidget *tqparent, const char * )
  {
    DirectoryServicesConfigurationPage *page =
      new DirectoryServicesConfigurationPage( tqparent, "kleopatra_config_dirserv" );
    return page;
  }
}

// kdelibs-3.2 didn't have the changed signal in KCModule...
void DirectoryServicesConfigurationPage::slotChanged()
{
  emit changed(true);
}


// Find config entry for ldap servers. Implements runtime checks on the configuration option.
Kleo::CryptoConfigEntry* DirectoryServicesConfigurationPage::configEntry( const char* componentName,
                                                                          const char* groupName,
                                                                          const char* entryName,
                                                                          Kleo::CryptoConfigEntry::ArgType argType,
                                                                          bool isList )
{
    Kleo::CryptoConfigEntry* entry = mConfig->entry( componentName, groupName, entryName );
    if ( !entry ) {
        KMessageBox::error( this, i18n( "Backend error: gpgconf does not seem to know the entry for %1/%2/%3" ).arg( componentName, groupName, entryName ) );
        return 0;
    }
    if( entry->argType() != argType || entry->isList() != isList ) {
        KMessageBox::error( this, i18n( "Backend error: gpgconf has wrong type for %1/%2/%3: %4 %5" ).arg( componentName, groupName, entryName ).arg( entry->argType() ).arg( entry->isList() ) );
        return 0;
    }
    return entry;
}

#include "dirservconfigpage.moc"