~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to wizards/kmailchanges.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    This file is part of kdepim.
3
 
 
4
 
    Copyright (c) 2004 Bo Thorsen <bo@sonofthor.dk>
5
 
    Copyright (c) 2004 Cornelius Schumacher <schumacher@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 "kmailchanges.h"
24
 
 
25
 
#include <kapplication.h>
26
 
#include <klocale.h>
27
 
#include <kconfiggroup.h>
28
 
#include <kstandarddirs.h>
29
 
#include <kemailsettings.h>
30
 
#include <kpimidentities/identitymanager.h>
31
 
#include <kpimidentities/identity.h>
32
 
#include <kdebug.h>
33
 
#include <kstringhandler.h>
34
 
#include <krandom.h>
35
 
#include <kwallet.h>
36
 
using namespace KWallet;
37
 
 
38
 
static const char* s_folderContentsType[] = {
39
 
  I18N_NOOP( "Calendar" ),
40
 
  I18N_NOOP( "Contacts" ),
41
 
  I18N_NOOP( "Notes" ),
42
 
  I18N_NOOP( "Tasks" ),
43
 
  I18N_NOOP( "Journal" ) };
44
 
 
45
 
Wallet* CreateImapAccount::mWallet = 0;
46
 
 
47
 
CreateImapAccount::CreateImapAccount( const QString &accountName, const QString &title )
48
 
  : KConfigPropagator::Change( title ),
49
 
    mAccountName( accountName ), mPort( 993 ), mEnableSieve( false ), mEnableSavePassword( true ),
50
 
    mEncryption( None ), mAuthentication( NONE ), mAuthenticationSend( PLAIN ), mSmtpPort( 25 ),
51
 
    mExistingAccountId( -1 ), mExistingTransportId( -1 ),
52
 
    mCustomWriter( 0 )
53
 
{
54
 
}
55
 
 
56
 
CreateImapAccount::~CreateImapAccount()
57
 
{
58
 
  delete mCustomWriter;
59
 
}
60
 
 
61
 
void CreateImapAccount::setServer( const QString &s )
62
 
{
63
 
  mServer = s;
64
 
}
65
 
 
66
 
void CreateImapAccount::setUser( const QString &s )
67
 
{
68
 
  mUser = s;
69
 
}
70
 
 
71
 
void CreateImapAccount::setPassword( const QString &s )
72
 
{
73
 
  mPassword = s;
74
 
}
75
 
 
76
 
void CreateImapAccount::setRealName( const QString &s )
77
 
{
78
 
  mRealName = s;
79
 
}
80
 
 
81
 
void CreateImapAccount::setPort( int port )
82
 
{
83
 
  mPort = port;
84
 
}
85
 
 
86
 
void CreateImapAccount::setEmail( const QString &s )
87
 
{
88
 
  mEmail = s;
89
 
}
90
 
 
91
 
void CreateImapAccount::enableSieve( bool b )
92
 
{
93
 
  mEnableSieve = b;
94
 
}
95
 
 
96
 
void CreateImapAccount::setSieveVacationFileName( const QString& f )
97
 
{
98
 
  mSieveVacationFileName = f;
99
 
}
100
 
 
101
 
void CreateImapAccount::enableSavePassword( bool b )
102
 
{
103
 
  mEnableSavePassword = b;
104
 
}
105
 
 
106
 
void CreateImapAccount::setEncryption(
107
 
  CreateImapAccount::Encryption e )
108
 
{
109
 
  mEncryption = e;
110
 
}
111
 
 
112
 
void CreateImapAccount::setAuthentication(
113
 
  CreateImapAccount::Authentication a )
114
 
{
115
 
  mAuthentication = a;
116
 
}
117
 
 
118
 
void CreateImapAccount::setDefaultDomain(const QString &d)
119
 
{
120
 
  mDefaultDomain = d;
121
 
}
122
 
 
123
 
void CreateImapAccount::setAuthenticationSend(
124
 
  CreateImapAccount::Authentication a )
125
 
{
126
 
  mAuthenticationSend = a;
127
 
}
128
 
 
129
 
void CreateImapAccount::setSmtpPort( int port )
130
 
{
131
 
  mSmtpPort = port;
132
 
}
133
 
 
134
 
void CreateImapAccount::setExistingAccountId( int id )
135
 
{
136
 
  mExistingAccountId = id;
137
 
}
138
 
 
139
 
void CreateImapAccount::setExistingTransportId( int id )
140
 
{
141
 
  mExistingTransportId = id;
142
 
}
143
 
 
144
 
void CreateImapAccount::setCustomWriter(
145
 
  CreateImapAccount::CustomWriter *writer )
146
 
{
147
 
  mCustomWriter = writer;
148
 
}
149
 
 
150
 
 
151
 
CreateDisconnectedImapAccount::CreateDisconnectedImapAccount(const QString & accountName) :
152
 
    CreateImapAccount( accountName, i18n("Create Disconnected IMAP Account for KMail") ),
153
 
    mLocalSubscription( false ), mGroupwareType( GroupwareKolab )
154
 
{
155
 
}
156
 
 
157
 
void CreateDisconnectedImapAccount::apply()
158
 
{
159
 
  if ( mEmail.isEmpty() ) mEmail = mUser + '@' + mServer;
160
 
 
161
 
  KConfig c( "kmailrc" );
162
 
  KConfigGroup group = c.group( "General" );
163
 
  group.writeEntry( "Default domain", mDefaultDomain );
164
 
  int accountId;
165
 
  if ( mExistingAccountId < 0 ) {
166
 
    uint accCnt = group.readEntry( "accounts", 0 );
167
 
    accountId = accCnt + 1;
168
 
    group.writeEntry( "accounts", accountId );
169
 
  } else {
170
 
    accountId = mExistingAccountId;
171
 
  }
172
 
  int transportId;
173
 
  if ( mExistingTransportId < 0 ) {
174
 
    uint transCnt = group.readEntry( "transports", 0 );
175
 
    transportId = transCnt + 1;
176
 
    group.writeEntry( "transports", transportId );
177
 
  } else {
178
 
    transportId = mExistingTransportId;
179
 
  }
180
 
 
181
 
  group = c.group( QString("Account %1").arg( accountId ) );
182
 
  int uid;
183
 
  if ( mExistingAccountId < 0 ) {
184
 
    uid = KRandom::random();
185
 
    group.writeEntry( "Folder", uid );
186
 
  } else {
187
 
    uid = group.readEntry( "Folder",0 );
188
 
  }
189
 
  group.writeEntry( "Id", uid );
190
 
  group.writeEntry( "Type", "DImap");
191
 
  switch ( mAuthentication ) {
192
 
    case NONE:
193
 
      group.writeEntry( "auth", "*" );
194
 
      break;
195
 
    case PLAIN:
196
 
      group.writeEntry( "auth", "PLAIN" );
197
 
      break;
198
 
    case LOGIN:
199
 
      group.writeEntry( "auth", "LOGIN" );
200
 
      break;
201
 
    case NTLM_SPA:
202
 
      group.writeEntry( "auth", "NTLM" );
203
 
      break;
204
 
    case GSSAPI:
205
 
      group.writeEntry( "auth", "GSSAPI" );
206
 
      break;
207
 
    case DIGEST_MD5:
208
 
      group.writeEntry( "auth", "DIGEST-MD5" );
209
 
      break;
210
 
    case CRAM_MD5:
211
 
      group.writeEntry( "auth", "CRAM-MD5" );
212
 
      break;
213
 
  }
214
 
  group.writeEntry( "Name", mAccountName );
215
 
  group.writeEntry( "host", mServer );
216
 
  group.writeEntry( "port", mPort );
217
 
 
218
 
  group.writeEntry( "groupwareType", (int)mGroupwareType );
219
 
 
220
 
  // in case the user wants to get rid of some groupware folders
221
 
  group.writeEntry( "locally-subscribed-folders", mLocalSubscription );
222
 
 
223
 
  group.writeEntry( "login", mUser );
224
 
 
225
 
  group.writeEntry( "sieve-support", mEnableSieve ? "true" : "false" );
226
 
  if ( !mSieveVacationFileName.isEmpty() )
227
 
    group.writeEntry( "sieve-vacation-filename", mSieveVacationFileName );
228
 
 
229
 
  if ( mEncryption == SSL ) {
230
 
    group.writeEntry( "use-ssl", true );
231
 
  } else if ( mEncryption == TLS ) {
232
 
    group.writeEntry( "use-tls", true );
233
 
  }
234
 
 
235
 
  if ( mEnableSavePassword ) {
236
 
    if ( !writeToWallet( "account", accountId ) ) {
237
 
      group.writeEntry( "pass", KStringHandler::obscure( mPassword ) );
238
 
      group.writeEntry( "store-passwd", true );
239
 
    }
240
 
  }
241
 
 
242
 
 
243
 
  group = c.group( QString("Folder-%1").arg( uid ) );
244
 
  group.writeEntry( "isOpen", true );
245
 
 
246
 
  group = c.group( QLatin1String( "AccountWizard" ) );
247
 
  group.writeEntry( QLatin1String( "ShowOnStartup" ), false );
248
 
 
249
 
  group = c.group( QLatin1String( "Composer" ) );
250
 
  group.writeEntry( "default-transport", mAccountName );
251
 
 
252
 
  KConfig transport( "mailtransports" );
253
 
  group = transport.group( QString("Transport %1").arg( transportId ) );
254
 
  group.writeEntry( "name", mAccountName );
255
 
  group.writeEntry( "host", mServer );
256
 
  group.writeEntry( "type", "smtp" );
257
 
  group.writeEntry( "port", mSmtpPort );
258
 
  if ( mEncryption == SSL ) {
259
 
    group.writeEntry( "encryption", "SSL" );
260
 
  } else if ( mEncryption == TLS ) {
261
 
    group.writeEntry( "encryption", "TLS" );
262
 
  }
263
 
  group.writeEntry( "auth", true );
264
 
  if ( mAuthenticationSend == PLAIN ) {
265
 
    group.writeEntry( "authtype", "PLAIN" );
266
 
  } else if ( mAuthenticationSend == LOGIN ) {
267
 
    group.writeEntry( "authtype", "LOGIN" );
268
 
  }
269
 
  group.writeEntry( "id", transportId );
270
 
  group.writeEntry( "user", mUser );
271
 
  if ( mEnableSavePassword ) {
272
 
    if ( !writeToWallet( "transport", transportId ) ) {
273
 
      group.writeEntry( "pass", KStringHandler::obscure( mPassword ) );
274
 
      group.writeEntry( "storepass", true );
275
 
    }
276
 
  }
277
 
 
278
 
  // Write email in "default kcontrol settings", used by IdentityManager
279
 
  // if it has to create a default identity.
280
 
  KEMailSettings es;
281
 
  es.setSetting( KEMailSettings::RealName, mRealName );
282
 
  es.setSetting( KEMailSettings::EmailAddress, mEmail );
283
 
 
284
 
  KPIMIdentities::IdentityManager identityManager;
285
 
  if ( !identityManager.allEmails().contains( mEmail ) ) {
286
 
    // Not sure how to name the identity. First one is "Default", next one mAccountName, but then...
287
 
    // let's use the server name after that.
288
 
    QString accountName = mAccountName;
289
 
    const QStringList identities = identityManager.identities();
290
 
    if ( identities.contains( accountName )  ) {
291
 
      accountName = mServer;
292
 
      int i = 2;
293
 
      // And if there's already one, number them
294
 
      while ( identities.contains( accountName )  ) {
295
 
        accountName = mServer + ' ' + QString::number( i++ );
296
 
      }
297
 
    }
298
 
 
299
 
    KPIMIdentities::Identity& identity = identityManager.newFromScratch( accountName );
300
 
    identity.setFullName( mRealName );
301
 
    identity.setEmailAddr( mEmail );
302
 
    identityManager.commit();
303
 
  }
304
 
 
305
 
  if ( mCustomWriter ) {
306
 
    mCustomWriter->writeFolder( c, uid );
307
 
    mCustomWriter->writeIds( accountId, transportId );
308
 
  }
309
 
}
310
 
 
311
 
CreateOnlineImapAccount::CreateOnlineImapAccount(const QString & accountName) :
312
 
    CreateImapAccount( accountName, i18n("Create Online IMAP Account for KMail") )
313
 
{
314
 
}
315
 
 
316
 
void CreateOnlineImapAccount::apply()
317
 
{
318
 
  KConfig c( "kmailrc" );
319
 
  KConfigGroup group = c.group( "General" );
320
 
  uint accCnt = group.readEntry( "accounts", 0 );
321
 
  group.writeEntry( "accounts", accCnt+1 );
322
 
 
323
 
  group = c.group( QString("Account %1").arg(accCnt+1) );
324
 
  int uid = KRandom::random();
325
 
  group.writeEntry( "Folder", uid );
326
 
  group.writeEntry( "Id", uid );
327
 
  group.writeEntry( "Type", "Imap" );
328
 
  group.writeEntry( "auth", "*" );
329
 
  group.writeEntry( "Name", mAccountName );
330
 
  group.writeEntry( "host", mServer );
331
 
 
332
 
  group.writeEntry( "login", mUser );
333
 
 
334
 
  if ( mEnableSavePassword ) {
335
 
    if ( !writeToWallet( "account", accCnt+1 ) ) {
336
 
      group.writeEntry( "pass", KStringHandler::obscure( mPassword ) );
337
 
      group.writeEntry( "store-passwd", true );
338
 
    }
339
 
  }
340
 
  group.writeEntry( "port", "993" );
341
 
 
342
 
  if ( mEncryption == SSL ) {
343
 
    group.writeEntry( "use-ssl", true );
344
 
  } else if ( mEncryption == TLS ) {
345
 
    group.writeEntry( "use-tls", true );
346
 
  }
347
 
 
348
 
  if ( mAuthenticationSend == PLAIN ) {
349
 
    group.writeEntry( "authtype", "PLAIN" );
350
 
  } else if ( mAuthenticationSend == LOGIN ) {
351
 
    group.writeEntry( "authtype", "LOGIN" );
352
 
  }
353
 
 
354
 
  group.writeEntry( "sieve-support", mEnableSieve );
355
 
 
356
 
  // locally unsubscribe the default folders
357
 
  group.writeEntry( "locally-subscribed-folders", true );
358
 
  QString groupwareFolders = QString("/INBOX/%1/,/INBOX/%2/,/INBOX/%3/,/INBOX/%4/,/INBOX/%5/")
359
 
      .arg( i18n(s_folderContentsType[0]) ).arg( i18n(s_folderContentsType[1]) )
360
 
      .arg( i18n(s_folderContentsType[2]) ).arg( i18n(s_folderContentsType[3]) )
361
 
      .arg( i18n(s_folderContentsType[4]) );
362
 
  group.writeEntry( "locallyUnsubscribedFolders", groupwareFolders );
363
 
 
364
 
  group = c.group( QString("Folder-%1").arg( uid ) );
365
 
  group.writeEntry( "isOpen", true );
366
 
 
367
 
  group = c.group( QLatin1String( "AccountWizard" ) );
368
 
  group.writeEntry( QLatin1String( "ShowOnStartup" ), false );
369
 
}
370
 
 
371
 
bool CreateImapAccount::writeToWallet(const QString & type, int id)
372
 
{
373
 
  if ( !Wallet::isEnabled() )
374
 
    return false;
375
 
  if ( !mWallet || !mWallet->isOpen() ) {
376
 
    delete mWallet;
377
 
    WId window = 0;
378
 
    if ( qApp->activeWindow() )
379
 
      window = qApp->activeWindow()->winId();
380
 
    mWallet = Wallet::openWallet( Wallet::NetworkWallet(), window );
381
 
    if ( !mWallet )
382
 
      return false;
383
 
  }
384
 
  QString folder, str_id;
385
 
  if ( type=="transport" ) {
386
 
    folder = "mailtransports";
387
 
    str_id = QString::number( id );
388
 
  } else {
389
 
    folder = "kmail";
390
 
    str_id = type + '-' + QString::number( id );
391
 
  }
392
 
  if ( !mWallet->hasFolder( folder ) )
393
 
    mWallet->createFolder( folder );
394
 
  mWallet->setFolder( folder );
395
 
  return mWallet->writePassword( str_id, mPassword ) == 0;
396
 
}