summaryrefslogtreecommitdiffstats
path: root/wizards/scalixwizard.cpp
blob: 3aa5634c08ff02cd8dba3e10387197f8264ba9b6 (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
    This file is part of tdepim.

    Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
    Copyright (c) 2004 Daniel Molkentin <molkentin@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "scalixwizard.h"
#include "scalixconfig.h"

#include "scalixkmailchanges.h"

#include "tderesources/scalix/kcal/resourcescalix.h"
#include "tderesources/scalix/tdeabc/resourcescalix.h"

#include <libkcal/resourcecalendar.h>
#include <tdeabc/resource.h>

#include <dcopref.h>
#include <kcombobox.h>
#include <kdcopservicestarter.h>
#include <klineedit.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kstringhandler.h>

#include <tqapplication.h>
#include <tqcheckbox.h>
#include <tqhbuttongroup.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqwhatsthis.h>

#include <unistd.h>

class SetupLDAPSearchAccount : public TDEConfigPropagator::Change
{
  public:
    SetupLDAPSearchAccount()
      : TDEConfigPropagator::Change( i18n("Setup LDAP Search Account") )
    {
    }

    void apply()
    {
      const TQString host = ScalixConfig::self()->server();

      TQString basedn( "o=Scalix" );

      { // while we're here, write default domain
        TDEConfig c( "kmailrc" );
        c.setGroup( "General" );
        c.writeEntry( "Default domain", basedn );
      }

      // Set the changes
      TDEConfig c( "kabldaprc" );
      c.setGroup( "LDAP" );
      bool hasMyServer = false;
      uint selHosts = c.readNumEntry("NumSelectedHosts", 0);
      for ( uint i = 0 ; i < selHosts && !hasMyServer; ++i )
        if ( c.readEntry( TQString("SelectedHost%1").arg(i) ) == host )
          hasMyServer = true;
      if ( !hasMyServer ) {
        c.writeEntry( "NumSelectedHosts", selHosts + 1 );
        c.writeEntry( TQString("SelectedHost%1").arg(selHosts), host);
        c.writeEntry( TQString("SelectedBase%1").arg(selHosts), basedn);
        c.writeEntry( TQString("SelectedPort%1").arg(selHosts), "389");
      }
    }

};

class SetupScalixAdmin : public TDEConfigPropagator::Change
{
  public:
    SetupScalixAdmin()
      : TDEConfigPropagator::Change( i18n( "Setup ScalixAdmin Account" ) )
    {
    }

    void apply()
    {
      TDEConfig c( "scalixadminrc" );
      c.setGroup( "Account" );

      c.writeEntry( "user", ScalixConfig::self()->user() );
      c.writeEntry( "pass", KStringHandler::obscure( ScalixConfig::self()->password() ) );
      c.writeEntry( "host", ScalixConfig::self()->server() );
      if ( ScalixConfig::self()->security() == ScalixConfig::None )
        c.writeEntry( "port", 143 );
      else
        c.writeEntry( "port", 993 );

      switch ( ScalixConfig::self()->security() ) {
        case ScalixConfig::None:
          c.writeEntry( "use-ssl", "false" );
          c.writeEntry( "use-tls", "false" );
          break;
        case ScalixConfig::TLS:
          c.writeEntry( "use-ssl", "false" );
          c.writeEntry( "use-tls", "true" );
          break;
        case ScalixConfig::SSL:
          c.writeEntry( "use-ssl", "true" );
          c.writeEntry( "use-tls", "false" );
          break;
      }
      switch ( ScalixConfig::self()->authentication() ) {
        case ScalixConfig::Password:
          c.writeEntry( "auth", "*" );
          break;
        case ScalixConfig::NTLM_SPA:
          c.writeEntry( "auth", "NTLM" );
          break;
        case ScalixConfig::GSSAPI:
          c.writeEntry( "auth", "GSSAPI" );
          break;
        case ScalixConfig::DIGEST_MD5:
          c.writeEntry( "auth", "DIGEST-MD5" );
          break;
        case ScalixConfig::CRAM_MD5:
          c.writeEntry( "auth", "CRAM-MD5" );
          break;
      }

      c.setGroup( "LDAP" );

      c.writeEntry( "host", ScalixConfig::self()->server() );
      c.writeEntry( "port", "389" );
      c.writeEntry( "base", "o=Scalix" );
      c.writeEntry( "bindDn", "" );
      c.writeEntry( "password", "" );
    }
};

class CreateCalendarImapResource : public TDEConfigPropagator::Change
{
  public:
    CreateCalendarImapResource()
      : TDEConfigPropagator::Change( i18n("Create Calendar IMAP Resource") )
    {
    }

    void apply()
    {
      KCal::CalendarResourceManager m( "calendar" );
      m.readConfig();
      KCal::ResourceScalix *r = new KCal::ResourceScalix( 0 );
      r->setResourceName( i18n("Scalix Server") );
      m.add( r );
      m.setStandardResource( r );
      m.writeConfig();
    }
};

class CreateContactImapResource : public TDEConfigPropagator::Change
{
  public:
    CreateContactImapResource()
      : TDEConfigPropagator::Change( i18n("Create Contact IMAP Resource") )
    {
    }

    void apply()
    {
      KRES::Manager<TDEABC::Resource> m( "contact" );
      m.readConfig();
      TDEABC::ResourceScalix *r = new TDEABC::ResourceScalix( 0 );
      r->setResourceName( i18n("Scalix Server") );
      m.add( r );
      m.setStandardResource( r );
      m.writeConfig();
    }

};

class SynchronizeScalixAccount : public TDEConfigPropagator::Change
{
  public:
    SynchronizeScalixAccount()
      : TDEConfigPropagator::Change( i18n("Synchronize Scalix Account") )
    {
    }

    void apply()
    {
      TQMessageBox *msg = new TQMessageBox( tqApp->mainWidget() );
      msg->setText( "Preparing initial synchronization with Scalix server..." );
      msg->show();
      tqApp->processEvents();
      sleep( 1 );
      tqApp->processEvents();

      TQString error;
      TQCString dcopService;
      int result = KDCOPServiceStarter::self()->
        findServiceFor( "DCOP/ResourceBackend/IMAP", TQString(),
                        TQString(), &error, &dcopService );
      if ( result != 0 ) {
        KMessageBox::error( 0, i18n( "Unable to start KMail to trigger initial synchronization with Scalix server" ) );
        delete msg;
        return;
      }

      DCOPRef ref( dcopService, "KMailIface" );

      // loop until dcop iface is set up correctly
      TQStringList list;
      while ( list.isEmpty() ) {
        ref.call( "accounts()" ).get( list );
      }

      ref.call( "checkAccount(TQString)", i18n( "Scalix Server" ) );

      // ugly hack, but kmail needs a second before accepting the second dcop call
      sleep( 5 );
      ref.call( "checkAccount(TQString)", i18n( "Scalix Server" ) );

      delete msg;
    }

};


class ScalixPropagator : public TDEConfigPropagator
{
  public:
    ScalixPropagator()
      : TDEConfigPropagator( ScalixConfig::self(), "scalix.kcfg" )
    {
    }

  protected:
    void addKorganizerChanges( Change::List &changes )
    {
      KURL freeBusyBaseUrl = "scalix://" + ScalixConfig::self()->server() + "/freebusy/";
      freeBusyBaseUrl.setUser( ScalixConfig::self()->user() );

      ChangeConfig *c = new ChangeConfig;
      c->file = "korganizerrc";
      c->group = "FreeBusy";
      c->name = "FreeBusyRetrieveUrl";
      c->value = freeBusyBaseUrl.url() + ScalixConfig::self()->eMail();
      changes.append( c );

      c = new ChangeConfig;
      c->file = "korganizerrc";
      c->group = "FreeBusy";
      c->name = "FreeBusyRetrieveUser";
      c->value = ScalixConfig::self()->user();
      changes.append( c );

      c = new ChangeConfig;
      c->file = "korganizerrc";
      c->group = "FreeBusy";
      c->name = "FreeBusyRetrievePassword";
      c->value = ScalixConfig::self()->password();
      changes.append( c );

      c = new ChangeConfig;
      c->file = "korganizerrc";
      c->group = "FreeBusy";
      c->name = "FreeBusyPublishUrl";
      c->value = freeBusyBaseUrl.url() + "Calendar/" + ScalixConfig::self()->eMail();
      changes.append( c );

      c = new ChangeConfig;
      c->file = "korganizerrc";
      c->group = "FreeBusy";
      c->name = "FreeBusyPublishUser";
      c->value = ScalixConfig::self()->user();
      changes.append( c );

      c = new ChangeConfig;
      c->file = "korganizerrc";
      c->group = "FreeBusy";
      c->name = "FreeBusyPublishPassword";
      c->value = ScalixConfig::self()->password();
      changes.append( c );

      // Use full email address for retrieval of free/busy lists
      c = new ChangeConfig;
      c->file = "korganizerrc";
      c->group = "FreeBusy";
      c->name = "FreeBusyFullDomainRetrieval";
      c->value = "true";
      changes.append( c );

      // Disable hostname checking
      c = new ChangeConfig;
      c->file = "korganizerrc";
      c->group = "FreeBusy";
      c->name = "FreeBusyCheckHostname";
      c->value = "false";
      changes.append( c );

      // Enable automatic retrieval
      c = new ChangeConfig;
      c->file = "korganizerrc";
      c->group = "FreeBusy";
      c->name = "FreeBusyRetrieveAuto";
      c->value = "true";
      changes.append( c );

      c = new ChangeConfig;
      c->file = "korganizerrc";
      c->group = "Group Scheduling";
      c->name = "Use Groupware Communication";
      c->value = "true";
      changes.append( c );

      // Use identity "from control center", i.e. from emaildefaults
      c = new ChangeConfig;
      c->file = "korganizerrc";
      c->group = "Personal Settings";
      c->name = "Use Control Center Email";
      c->value = "true";
      changes.append( c );
    }

    virtual void addCustomChanges( Change::List &changes )
    {
      addKorganizerChanges( changes );

      // KMail cruft has been outsourced to kolabkmailchanges.cpp
      createKMailChanges( changes );

      changes.append( new SetupLDAPSearchAccount );

      KCal::CalendarResourceManager m( "calendar" );
      m.readConfig();
      KCal::CalendarResourceManager::Iterator it;
      for ( it = m.begin(); it != m.end(); ++it ) {
        if ( (*it)->type() == "scalix" ) break;
      }
      if ( it == m.end() ) {
        changes.append( new CreateCalendarImapResource );
        changes.append( new CreateContactImapResource );
      }

      changes.append( new SetupScalixAdmin );

      changes.append( new SynchronizeScalixAccount );
    }
};

ScalixWizard::ScalixWizard() : TDEConfigWizard( new ScalixPropagator )
{
  TQFrame *page = createWizardPage( i18n("Scalix Server") );

  TQGridLayout *topLayout = new TQGridLayout( page );
  topLayout->setSpacing( spacingHint() );

  TQLabel *label = new TQLabel( i18n( "Full name:" ), page );
  topLayout->addWidget( label, 0, 0 );
  mRealNameEdit = new KLineEdit( page );
  topLayout->addWidget( mRealNameEdit, 0, 1 );
  label->setBuddy( mRealNameEdit );
  TQWhatsThis::add( mRealNameEdit, i18n( "Your full name. "
                                        "Example: <i>Joe User</i>" ) );

  label = new TQLabel( i18n( "Email address:" ), page );
  topLayout->addWidget( label, 1, 0 );
  mEMailEdit = new KLineEdit( page );
  topLayout->addWidget( mEMailEdit, 1, 1 );
  label->setBuddy( mEMailEdit );
  TQWhatsThis::add( mEMailEdit, i18n( "Your email address on the Scalix Server. "
                                     "Example: <i>name@crossplatform.com</i>" ) );

  label = new TQLabel( i18n( "Server:" ), page );
  topLayout->addWidget( label, 2, 0 );
  mServerEdit = new KLineEdit( page );
  topLayout->addWidget( mServerEdit, 2, 1 );
  label->setBuddy( mServerEdit );
  TQWhatsThis::add( mServerEdit, i18n( "The name or IP of the Scalix Server. "
                                      "Example: <i>scalix.domain.com</i>" ) );

  label = new TQLabel( i18n("Username:"), page );
  topLayout->addWidget( label, 3, 0 );
  mUserEdit = new KLineEdit( page );
  topLayout->addWidget( mUserEdit, 3, 1 );
  label->setBuddy( mUserEdit );
  TQWhatsThis::add( mUserEdit, i18n( "The user respectively login name. "
                                    "Example: <i>joe</i>" ) );

  label = new TQLabel( i18n("Password:"), page );
  topLayout->addWidget( label, 4, 0 );
  mPasswordEdit = new KLineEdit( page );
  mPasswordEdit->setEchoMode( KLineEdit::Password );
  topLayout->addWidget( mPasswordEdit, 4, 1 );
  label->setBuddy( mPasswordEdit );
  TQWhatsThis::add( mPasswordEdit, i18n( "The password to your login." ) );

  mSavePasswordCheck = new TQCheckBox( i18n("Save password"), page );
  topLayout->addMultiCellWidget( mSavePasswordCheck, 5, 5, 0, 1 );
  TQWhatsThis::add( mSavePasswordCheck, i18n( "Shall the password be saved in TDEWallet?." ) );

  label = new TQLabel( i18n( "Use Secure Connection:" ), page );
  topLayout->addWidget( label, 6, 0 );
  mSecurity = new KComboBox( page );
  mSecurity->insertItem( i18n( "No encryption" ) );
  mSecurity->insertItem( i18n( "TLS encryption" ) );
  mSecurity->insertItem( i18n( "SSL encryption" ) );
  topLayout->addWidget( mSecurity, 6, 1 );
  label->setBuddy( mSecurity );
  TQWhatsThis::add( mSecurity, i18n( "Choose the encryption type that is supported by your server." ) );

  label = new TQLabel( i18n( "Authentication Type:" ), page );
  topLayout->addWidget( label, 7, 0 );
  mAuthentication = new KComboBox( page );
  mAuthentication->insertItem( i18n( "Password" ) );
  mAuthentication->insertItem( i18n( "NTLM / SPA" ) );
  mAuthentication->insertItem( i18n( "GSSAPI" ) );
  mAuthentication->insertItem( i18n( "DIGEST-MD5" ) );
  mAuthentication->insertItem( i18n( "CRAM-MD5" ) );
  topLayout->addWidget( mAuthentication, 7, 1 );
  label->setBuddy( mAuthentication );
  TQWhatsThis::add( mAuthentication, i18n( "Choose the authentication type that is supported by your server." ) );

  topLayout->setRowStretch( 8, 1 );

  //DF: I don't see the point in showing the user those pages.
  //They are very 'internal' and of no use to anyone other than developers.
  //(This is even more true for the rules page. The changes page is sort of OK)

  setupRulesPage();
  setupChangesPage();

  setInitialSize( TQSize( 600, 300 ) );
}

ScalixWizard::~ScalixWizard()
{
}

TQString ScalixWizard::validate()
{
  if ( mRealNameEdit->text().isEmpty() ||
       mEMailEdit->text().isEmpty() ||
       mServerEdit->text().isEmpty() ||
       mUserEdit->text().isEmpty() ||
       mPasswordEdit->text().isEmpty() )
    return i18n( "Please fill in all fields." );

  return TQString();
}

void ScalixWizard::usrReadConfig()
{
  mRealNameEdit->setText( ScalixConfig::self()->realName() );
  mEMailEdit->setText( ScalixConfig::self()->eMail() );
  mServerEdit->setText( ScalixConfig::self()->server() );
  mUserEdit->setText( ScalixConfig::self()->user() );
  mPasswordEdit->setText( ScalixConfig::self()->password() );
  mSavePasswordCheck->setChecked( ScalixConfig::self()->savePassword() );

  switch ( ScalixConfig::self()->security() ) {
    default:
    case ScalixConfig::None:
      mSecurity->setCurrentItem( 0 );
      break;
    case ScalixConfig::TLS:
      mSecurity->setCurrentItem( 1 );
      break;
    case ScalixConfig::SSL:
      mSecurity->setCurrentItem( 2 );
      break;
  }

  switch ( ScalixConfig::self()->authentication() ) {
    default:
    case ScalixConfig::Password:
      mAuthentication->setCurrentItem( 0 );
      break;
    case ScalixConfig::NTLM_SPA:
      mAuthentication->setCurrentItem( 1 );
      break;
    case ScalixConfig::GSSAPI:
      mAuthentication->setCurrentItem( 2 );
      break;
    case ScalixConfig::DIGEST_MD5:
      mAuthentication->setCurrentItem( 3 );
      break;
    case ScalixConfig::CRAM_MD5:
      mAuthentication->setCurrentItem( 4 );
      break;
  }
}

void ScalixWizard::usrWriteConfig()
{
  ScalixConfig::self()->setRealName( mRealNameEdit->text() );
  ScalixConfig::self()->setEMail( mEMailEdit->text() );
  ScalixConfig::self()->setServer( mServerEdit->text() );
  ScalixConfig::self()->setUser( mUserEdit->text() );
  ScalixConfig::self()->setPassword( mPasswordEdit->text() );
  ScalixConfig::self()->setSavePassword( mSavePasswordCheck->isChecked() );

  switch ( mSecurity->currentItem() ) {
    default:
    case 0:
      ScalixConfig::self()->setSecurity( ScalixConfig::None );
      break;
    case 1:
      ScalixConfig::self()->setSecurity( ScalixConfig::TLS );
      break;
    case 2:
      ScalixConfig::self()->setSecurity( ScalixConfig::SSL );
      break;
  }

  switch ( mAuthentication->currentItem() ) {
    default:
    case 0:
      ScalixConfig::self()->setAuthentication( ScalixConfig::Password );
      break;
    case 1:
      ScalixConfig::self()->setAuthentication( ScalixConfig::NTLM_SPA );
      break;
    case 2:
      ScalixConfig::self()->setAuthentication( ScalixConfig::GSSAPI );
      break;
    case 3:
      ScalixConfig::self()->setAuthentication( ScalixConfig::DIGEST_MD5 );
      break;
    case 4:
      ScalixConfig::self()->setAuthentication( ScalixConfig::CRAM_MD5 );
      break;
  }
}