2
This file is part of kdepim.
4
Copyright (c) 2004 Bo Thorsen <bo@sonofthor.dk>
5
Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
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.
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.
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.
23
#include "kmailchanges.h"
25
#include <kapplication.h>
27
#include <kconfiggroup.h>
28
#include <kstandarddirs.h>
29
#include <kemailsettings.h>
30
#include <kpimidentities/identitymanager.h>
31
#include <kpimidentities/identity.h>
33
#include <kstringhandler.h>
36
using namespace KWallet;
38
static const char* s_folderContentsType[] = {
39
I18N_NOOP( "Calendar" ),
40
I18N_NOOP( "Contacts" ),
43
I18N_NOOP( "Journal" ) };
45
Wallet* CreateImapAccount::mWallet = 0;
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 ),
56
CreateImapAccount::~CreateImapAccount()
61
void CreateImapAccount::setServer( const QString &s )
66
void CreateImapAccount::setUser( const QString &s )
71
void CreateImapAccount::setPassword( const QString &s )
76
void CreateImapAccount::setRealName( const QString &s )
81
void CreateImapAccount::setPort( int port )
86
void CreateImapAccount::setEmail( const QString &s )
91
void CreateImapAccount::enableSieve( bool b )
96
void CreateImapAccount::setSieveVacationFileName( const QString& f )
98
mSieveVacationFileName = f;
101
void CreateImapAccount::enableSavePassword( bool b )
103
mEnableSavePassword = b;
106
void CreateImapAccount::setEncryption(
107
CreateImapAccount::Encryption e )
112
void CreateImapAccount::setAuthentication(
113
CreateImapAccount::Authentication a )
118
void CreateImapAccount::setDefaultDomain(const QString &d)
123
void CreateImapAccount::setAuthenticationSend(
124
CreateImapAccount::Authentication a )
126
mAuthenticationSend = a;
129
void CreateImapAccount::setSmtpPort( int port )
134
void CreateImapAccount::setExistingAccountId( int id )
136
mExistingAccountId = id;
139
void CreateImapAccount::setExistingTransportId( int id )
141
mExistingTransportId = id;
144
void CreateImapAccount::setCustomWriter(
145
CreateImapAccount::CustomWriter *writer )
147
mCustomWriter = writer;
151
CreateDisconnectedImapAccount::CreateDisconnectedImapAccount(const QString & accountName) :
152
CreateImapAccount( accountName, i18n("Create Disconnected IMAP Account for KMail") ),
153
mLocalSubscription( false ), mGroupwareType( GroupwareKolab )
157
void CreateDisconnectedImapAccount::apply()
159
if ( mEmail.isEmpty() ) mEmail = mUser + '@' + mServer;
161
KConfig c( "kmailrc" );
162
KConfigGroup group = c.group( "General" );
163
group.writeEntry( "Default domain", mDefaultDomain );
165
if ( mExistingAccountId < 0 ) {
166
uint accCnt = group.readEntry( "accounts", 0 );
167
accountId = accCnt + 1;
168
group.writeEntry( "accounts", accountId );
170
accountId = mExistingAccountId;
173
if ( mExistingTransportId < 0 ) {
174
uint transCnt = group.readEntry( "transports", 0 );
175
transportId = transCnt + 1;
176
group.writeEntry( "transports", transportId );
178
transportId = mExistingTransportId;
181
group = c.group( QString("Account %1").arg( accountId ) );
183
if ( mExistingAccountId < 0 ) {
184
uid = KRandom::random();
185
group.writeEntry( "Folder", uid );
187
uid = group.readEntry( "Folder",0 );
189
group.writeEntry( "Id", uid );
190
group.writeEntry( "Type", "DImap");
191
switch ( mAuthentication ) {
193
group.writeEntry( "auth", "*" );
196
group.writeEntry( "auth", "PLAIN" );
199
group.writeEntry( "auth", "LOGIN" );
202
group.writeEntry( "auth", "NTLM" );
205
group.writeEntry( "auth", "GSSAPI" );
208
group.writeEntry( "auth", "DIGEST-MD5" );
211
group.writeEntry( "auth", "CRAM-MD5" );
214
group.writeEntry( "Name", mAccountName );
215
group.writeEntry( "host", mServer );
216
group.writeEntry( "port", mPort );
218
group.writeEntry( "groupwareType", (int)mGroupwareType );
220
// in case the user wants to get rid of some groupware folders
221
group.writeEntry( "locally-subscribed-folders", mLocalSubscription );
223
group.writeEntry( "login", mUser );
225
group.writeEntry( "sieve-support", mEnableSieve ? "true" : "false" );
226
if ( !mSieveVacationFileName.isEmpty() )
227
group.writeEntry( "sieve-vacation-filename", mSieveVacationFileName );
229
if ( mEncryption == SSL ) {
230
group.writeEntry( "use-ssl", true );
231
} else if ( mEncryption == TLS ) {
232
group.writeEntry( "use-tls", true );
235
if ( mEnableSavePassword ) {
236
if ( !writeToWallet( "account", accountId ) ) {
237
group.writeEntry( "pass", KStringHandler::obscure( mPassword ) );
238
group.writeEntry( "store-passwd", true );
243
group = c.group( QString("Folder-%1").arg( uid ) );
244
group.writeEntry( "isOpen", true );
246
group = c.group( QLatin1String( "AccountWizard" ) );
247
group.writeEntry( QLatin1String( "ShowOnStartup" ), false );
249
group = c.group( QLatin1String( "Composer" ) );
250
group.writeEntry( "default-transport", mAccountName );
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" );
263
group.writeEntry( "auth", true );
264
if ( mAuthenticationSend == PLAIN ) {
265
group.writeEntry( "authtype", "PLAIN" );
266
} else if ( mAuthenticationSend == LOGIN ) {
267
group.writeEntry( "authtype", "LOGIN" );
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 );
278
// Write email in "default kcontrol settings", used by IdentityManager
279
// if it has to create a default identity.
281
es.setSetting( KEMailSettings::RealName, mRealName );
282
es.setSetting( KEMailSettings::EmailAddress, mEmail );
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;
293
// And if there's already one, number them
294
while ( identities.contains( accountName ) ) {
295
accountName = mServer + ' ' + QString::number( i++ );
299
KPIMIdentities::Identity& identity = identityManager.newFromScratch( accountName );
300
identity.setFullName( mRealName );
301
identity.setEmailAddr( mEmail );
302
identityManager.commit();
305
if ( mCustomWriter ) {
306
mCustomWriter->writeFolder( c, uid );
307
mCustomWriter->writeIds( accountId, transportId );
311
CreateOnlineImapAccount::CreateOnlineImapAccount(const QString & accountName) :
312
CreateImapAccount( accountName, i18n("Create Online IMAP Account for KMail") )
316
void CreateOnlineImapAccount::apply()
318
KConfig c( "kmailrc" );
319
KConfigGroup group = c.group( "General" );
320
uint accCnt = group.readEntry( "accounts", 0 );
321
group.writeEntry( "accounts", accCnt+1 );
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 );
332
group.writeEntry( "login", mUser );
334
if ( mEnableSavePassword ) {
335
if ( !writeToWallet( "account", accCnt+1 ) ) {
336
group.writeEntry( "pass", KStringHandler::obscure( mPassword ) );
337
group.writeEntry( "store-passwd", true );
340
group.writeEntry( "port", "993" );
342
if ( mEncryption == SSL ) {
343
group.writeEntry( "use-ssl", true );
344
} else if ( mEncryption == TLS ) {
345
group.writeEntry( "use-tls", true );
348
if ( mAuthenticationSend == PLAIN ) {
349
group.writeEntry( "authtype", "PLAIN" );
350
} else if ( mAuthenticationSend == LOGIN ) {
351
group.writeEntry( "authtype", "LOGIN" );
354
group.writeEntry( "sieve-support", mEnableSieve );
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 );
364
group = c.group( QString("Folder-%1").arg( uid ) );
365
group.writeEntry( "isOpen", true );
367
group = c.group( QLatin1String( "AccountWizard" ) );
368
group.writeEntry( QLatin1String( "ShowOnStartup" ), false );
371
bool CreateImapAccount::writeToWallet(const QString & type, int id)
373
if ( !Wallet::isEnabled() )
375
if ( !mWallet || !mWallet->isOpen() ) {
378
if ( qApp->activeWindow() )
379
window = qApp->activeWindow()->winId();
380
mWallet = Wallet::openWallet( Wallet::NetworkWallet(), window );
384
QString folder, str_id;
385
if ( type=="transport" ) {
386
folder = "mailtransports";
387
str_id = QString::number( id );
390
str_id = type + '-' + QString::number( id );
392
if ( !mWallet->hasFolder( folder ) )
393
mWallet->createFolder( folder );
394
mWallet->setFolder( folder );
395
return mWallet->writePassword( str_id, mPassword ) == 0;