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 if ( mEnableSavePassword
) {
247 group
.writeEntry( "pass", KStringHandler::obscure( mPassword
) );
248 group
.writeEntry( "store-passwd", true );
251 group
= c
.group( QLatin1String( "AccountWizard" ) );
252 group
.writeEntry( QLatin1String( "ShowOnStartup" ), false );
254 KConfig
transport( "mailtransports" );
255 group
= transport
.group( QString("Transport %1").arg( transportId
) );
256 group
.writeEntry( "name", mAccountName
);
257 group
.writeEntry( "host", mServer
);
258 group
.writeEntry( "type", "smtp" );
259 group
.writeEntry( "port", mSmtpPort
);
260 if ( mEncryption
== SSL
) {
261 group
.writeEntry( "encryption", "SSL" );
262 } else if ( mEncryption
== TLS
) {
263 group
.writeEntry( "encryption", "TLS" );
265 group
.writeEntry( "auth", true );
266 if ( mAuthenticationSend
== PLAIN
) {
267 group
.writeEntry( "authtype", "PLAIN" );
268 } else if ( mAuthenticationSend
== LOGIN
) {
269 group
.writeEntry( "authtype", "LOGIN" );
271 group
.writeEntry( "id", transportId
);
272 group
.writeEntry( "user", mUser
);
273 if ( mEnableSavePassword
) {
274 if ( !writeToWallet( "transport", transportId
) ) {
275 group
.writeEntry( "pass", KStringHandler::obscure( mPassword
) );
276 group
.writeEntry( "storepass", true );
280 // Write email in "default kcontrol settings", used by IdentityManager
281 // if it has to create a default identity.
283 es
.setSetting( KEMailSettings::RealName
, mRealName
);
284 es
.setSetting( KEMailSettings::EmailAddress
, mEmail
);
286 KPIMIdentities::IdentityManager identityManager
;
287 if ( !identityManager
.allEmails().contains( mEmail
) ) {
288 // Not sure how to name the identity. First one is "Default", next one mAccountName, but then...
289 // let's use the server name after that.
290 QString accountName
= mAccountName
;
291 const QStringList identities
= identityManager
.identities();
292 if ( identities
.contains( accountName
) ) {
293 accountName
= mServer
;
295 // And if there's already one, number them
296 while ( identities
.contains( accountName
) ) {
297 accountName
= mServer
+ ' ' + QString::number( i
++ );
301 KPIMIdentities::Identity
& identity
= identityManager
.newFromScratch( accountName
);
302 identity
.setFullName( mRealName
);
303 identity
.setEmailAddr( mEmail
);
304 identityManager
.commit();
307 if ( mCustomWriter
) {
308 mCustomWriter
->writeFolder( c
, uid
);
309 mCustomWriter
->writeIds( accountId
, transportId
);
313 CreateOnlineImapAccount::CreateOnlineImapAccount(const QString
& accountName
) :
314 CreateImapAccount( accountName
, i18n("Create Online IMAP Account for KMail") )
318 void CreateOnlineImapAccount::apply()
320 KConfig
c( "kmailrc" );
321 KConfigGroup group
= c
.group( "General" );
322 uint accCnt
= group
.readEntry( "accounts", 0 );
323 group
.writeEntry( "accounts", accCnt
+1 );
325 group
= c
.group( QString("Account %1").arg(accCnt
+1) );
326 int uid
= KRandom::random();
327 group
.writeEntry( "Folder", uid
);
328 group
.writeEntry( "Id", uid
);
329 group
.writeEntry( "Type", "Imap" );
330 group
.writeEntry( "auth", "*" );
331 group
.writeEntry( "Name", mAccountName
);
332 group
.writeEntry( "host", mServer
);
334 group
.writeEntry( "login", mUser
);
336 if ( mEnableSavePassword
) {
337 if ( !writeToWallet( "account", accCnt
+1 ) ) {
338 group
.writeEntry( "pass", KStringHandler::obscure( mPassword
) );
339 group
.writeEntry( "store-passwd", true );
342 group
.writeEntry( "port", "993" );
344 if ( mEncryption
== SSL
) {
345 group
.writeEntry( "use-ssl", true );
346 } else if ( mEncryption
== TLS
) {
347 group
.writeEntry( "use-tls", true );
350 if ( mAuthenticationSend
== PLAIN
) {
351 group
.writeEntry( "authtype", "PLAIN" );
352 } else if ( mAuthenticationSend
== LOGIN
) {
353 group
.writeEntry( "authtype", "LOGIN" );
356 group
.writeEntry( "sieve-support", mEnableSieve
);
358 // locally unsubscribe the default folders
359 group
.writeEntry( "locally-subscribed-folders", true );
360 QString groupwareFolders
= QString("/INBOX/%1/,/INBOX/%2/,/INBOX/%3/,/INBOX/%4/,/INBOX/%5/")
361 .arg( i18n(s_folderContentsType
[0]) ).arg( i18n(s_folderContentsType
[1]) )
362 .arg( i18n(s_folderContentsType
[2]) ).arg( i18n(s_folderContentsType
[3]) )
363 .arg( i18n(s_folderContentsType
[4]) );
364 group
.writeEntry( "locallyUnsubscribedFolders", groupwareFolders
);
366 group
= c
.group( QString("Folder-%1").arg( uid
) );
367 group
.writeEntry( "isOpen", true );
369 group
= c
.group( QLatin1String( "AccountWizard" ) );
370 group
.writeEntry( QLatin1String( "ShowOnStartup" ), false );
373 bool CreateImapAccount::writeToWallet(const QString
& type
, int id
)
375 if ( !Wallet::isEnabled() )
377 if ( !mWallet
|| !mWallet
->isOpen() ) {
380 if ( qApp
->activeWindow() )
381 window
= qApp
->activeWindow()->winId();
382 mWallet
= Wallet::openWallet( Wallet::NetworkWallet(), window
);
386 QString folder
, str_id
;
387 if ( type
=="transport" ) {
388 folder
= "mailtransports";
389 str_id
= QString::number( id
);
392 str_id
= type
+ '-' + QString::number( id
);
394 if ( !mWallet
->hasFolder( folder
) )
395 mWallet
->createFolder( folder
);
396 mWallet
->setFolder( folder
);
397 return mWallet
->writePassword( str_id
, mPassword
);