Build with non-standard boost locations.
[kdepim.git] / wizards / kmailchanges.cpp
blobbccf2ec6264c7e972773b4a69a2c3927a4a1f664
1 /*
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>
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>
37 #include <QtGui/QWidget>
39 using namespace KWallet;
41 static const char* s_folderContentsType[] = {
42 I18N_NOOP( "Calendar" ),
43 I18N_NOOP( "Contacts" ),
44 I18N_NOOP( "Notes" ),
45 I18N_NOOP( "Tasks" ),
46 I18N_NOOP( "Journal" ) };
48 Wallet* CreateImapAccount::mWallet = 0;
50 CreateImapAccount::CreateImapAccount( const QString &accountName, const QString &title )
51 : KConfigPropagator::Change( title ),
52 mAccountName( accountName ), mPort( 993 ), mEnableSieve( false ), mEnableSavePassword( true ),
53 mEncryption( None ), mAuthentication( NONE ), mAuthenticationSend( PLAIN ), mSmtpPort( 25 ),
54 mExistingAccountId( -1 ), mExistingTransportId( -1 ),
55 mCustomWriter( 0 )
59 CreateImapAccount::~CreateImapAccount()
61 delete mCustomWriter;
64 void CreateImapAccount::setServer( const QString &s )
66 mServer = s;
69 void CreateImapAccount::setUser( const QString &s )
71 mUser = s;
74 void CreateImapAccount::setPassword( const QString &s )
76 mPassword = s;
79 void CreateImapAccount::setRealName( const QString &s )
81 mRealName = s;
84 void CreateImapAccount::setPort( int port )
86 mPort = port;
89 void CreateImapAccount::setEmail( const QString &s )
91 mEmail = s;
94 void CreateImapAccount::enableSieve( bool b )
96 mEnableSieve = b;
99 void CreateImapAccount::setSieveVacationFileName( const QString& f )
101 mSieveVacationFileName = f;
104 void CreateImapAccount::enableSavePassword( bool b )
106 mEnableSavePassword = b;
109 void CreateImapAccount::setEncryption(
110 CreateImapAccount::Encryption e )
112 mEncryption = e;
115 void CreateImapAccount::setAuthentication(
116 CreateImapAccount::Authentication a )
118 mAuthentication = a;
121 void CreateImapAccount::setDefaultDomain(const QString &d)
123 mDefaultDomain = d;
126 void CreateImapAccount::setAuthenticationSend(
127 CreateImapAccount::Authentication a )
129 mAuthenticationSend = a;
132 void CreateImapAccount::setSmtpPort( int port )
134 mSmtpPort = port;
137 void CreateImapAccount::setExistingAccountId( int id )
139 mExistingAccountId = id;
142 void CreateImapAccount::setExistingTransportId( int id )
144 mExistingTransportId = id;
147 void CreateImapAccount::setCustomWriter(
148 CreateImapAccount::CustomWriter *writer )
150 mCustomWriter = writer;
154 CreateDisconnectedImapAccount::CreateDisconnectedImapAccount(const QString & accountName) :
155 CreateImapAccount( accountName, i18n("Create Disconnected IMAP Account for KMail") ),
156 mLocalSubscription( false ), mGroupwareType( GroupwareKolab )
160 void CreateDisconnectedImapAccount::apply()
162 if ( mEmail.isEmpty() ) mEmail = mUser + '@' + mServer;
164 KConfig c( "kmailrc" );
165 KConfigGroup group = c.group( "General" );
166 group.writeEntry( "Default domain", mDefaultDomain );
167 int accountId;
168 if ( mExistingAccountId < 0 ) {
169 uint accCnt = group.readEntry( "accounts", 0 );
170 accountId = accCnt + 1;
171 group.writeEntry( "accounts", accountId );
172 } else {
173 accountId = mExistingAccountId;
175 int transportId;
176 if ( mExistingTransportId < 0 ) {
177 uint transCnt = group.readEntry( "transports", 0 );
178 transportId = transCnt + 1;
179 group.writeEntry( "transports", transportId );
180 } else {
181 transportId = mExistingTransportId;
184 group = c.group( QString("Account %1").arg( accountId ) );
185 int uid;
186 if ( mExistingAccountId < 0 ) {
187 uid = KRandom::random();
188 group.writeEntry( "Folder", uid );
189 } else {
190 uid = group.readEntry( "Folder",0 );
192 group.writeEntry( "Id", uid );
193 group.writeEntry( "Type", "DImap");
194 switch ( mAuthentication ) {
195 case NONE:
196 group.writeEntry( "auth", "*" );
197 break;
198 case PLAIN:
199 group.writeEntry( "auth", "PLAIN" );
200 break;
201 case LOGIN:
202 group.writeEntry( "auth", "LOGIN" );
203 break;
204 case NTLM_SPA:
205 group.writeEntry( "auth", "NTLM" );
206 break;
207 case GSSAPI:
208 group.writeEntry( "auth", "GSSAPI" );
209 break;
210 case DIGEST_MD5:
211 group.writeEntry( "auth", "DIGEST-MD5" );
212 break;
213 case CRAM_MD5:
214 group.writeEntry( "auth", "CRAM-MD5" );
215 break;
217 group.writeEntry( "Name", mAccountName );
218 group.writeEntry( "host", mServer );
219 group.writeEntry( "port", mPort );
221 group.writeEntry( "groupwareType", (int)mGroupwareType );
223 // in case the user wants to get rid of some groupware folders
224 group.writeEntry( "locally-subscribed-folders", mLocalSubscription );
226 group.writeEntry( "login", mUser );
228 group.writeEntry( "sieve-support", mEnableSieve ? "true" : "false" );
229 if ( !mSieveVacationFileName.isEmpty() )
230 group.writeEntry( "sieve-vacation-filename", mSieveVacationFileName );
232 if ( mEncryption == SSL ) {
233 group.writeEntry( "use-ssl", true );
234 } else if ( mEncryption == TLS ) {
235 group.writeEntry( "use-tls", true );
238 if ( mEnableSavePassword ) {
239 if ( !writeToWallet( "account", accountId ) ) {
240 group.writeEntry( "pass", KStringHandler::obscure( mPassword ) );
241 group.writeEntry( "store-passwd", true );
246 group = c.group( QString("Folder-%1").arg( uid ) );
247 group.writeEntry( "isOpen", true );
249 group = c.group( QLatin1String( "AccountWizard" ) );
250 group.writeEntry( QLatin1String( "ShowOnStartup" ), false );
252 group = c.group( QLatin1String( "Composer" ) );
253 group.writeEntry( "default-transport", mAccountName );
255 KConfig transport( "mailtransports" );
256 group = transport.group( QString("Transport %1").arg( transportId ) );
257 group.writeEntry( "name", mAccountName );
258 group.writeEntry( "host", mServer );
259 group.writeEntry( "type", "smtp" );
260 group.writeEntry( "port", mSmtpPort );
261 if ( mEncryption == SSL ) {
262 group.writeEntry( "encryption", "SSL" );
263 } else if ( mEncryption == TLS ) {
264 group.writeEntry( "encryption", "TLS" );
266 group.writeEntry( "auth", true );
267 if ( mAuthenticationSend == PLAIN ) {
268 group.writeEntry( "authtype", "PLAIN" );
269 } else if ( mAuthenticationSend == LOGIN ) {
270 group.writeEntry( "authtype", "LOGIN" );
272 group.writeEntry( "id", transportId );
273 group.writeEntry( "user", mUser );
274 if ( mEnableSavePassword ) {
275 if ( !writeToWallet( "transport", transportId ) ) {
276 group.writeEntry( "pass", KStringHandler::obscure( mPassword ) );
277 group.writeEntry( "storepass", true );
281 // Write email in "default kcontrol settings", used by IdentityManager
282 // if it has to create a default identity.
283 KEMailSettings es;
284 es.setSetting( KEMailSettings::RealName, mRealName );
285 es.setSetting( KEMailSettings::EmailAddress, mEmail );
287 KPIMIdentities::IdentityManager identityManager;
288 if ( !identityManager.allEmails().contains( mEmail ) ) {
289 // Not sure how to name the identity. First one is "Default", next one mAccountName, but then...
290 // let's use the server name after that.
291 QString accountName = mAccountName;
292 const QStringList identities = identityManager.identities();
293 if ( identities.contains( accountName ) ) {
294 accountName = mServer;
295 int i = 2;
296 // And if there's already one, number them
297 while ( identities.contains( accountName ) ) {
298 accountName = mServer + ' ' + QString::number( i++ );
302 KPIMIdentities::Identity& identity = identityManager.newFromScratch( accountName );
303 identity.setFullName( mRealName );
304 identity.setEmailAddr( mEmail );
305 identityManager.commit();
308 if ( mCustomWriter ) {
309 mCustomWriter->writeFolder( c, uid );
310 mCustomWriter->writeIds( accountId, transportId );
314 CreateOnlineImapAccount::CreateOnlineImapAccount(const QString & accountName) :
315 CreateImapAccount( accountName, i18n("Create Online IMAP Account for KMail") )
319 void CreateOnlineImapAccount::apply()
321 KConfig c( "kmailrc" );
322 KConfigGroup group = c.group( "General" );
323 uint accCnt = group.readEntry( "accounts", 0 );
324 group.writeEntry( "accounts", accCnt+1 );
326 group = c.group( QString("Account %1").arg(accCnt+1) );
327 int uid = KRandom::random();
328 group.writeEntry( "Folder", uid );
329 group.writeEntry( "Id", uid );
330 group.writeEntry( "Type", "Imap" );
331 group.writeEntry( "auth", "*" );
332 group.writeEntry( "Name", mAccountName );
333 group.writeEntry( "host", mServer );
335 group.writeEntry( "login", mUser );
337 if ( mEnableSavePassword ) {
338 if ( !writeToWallet( "account", accCnt+1 ) ) {
339 group.writeEntry( "pass", KStringHandler::obscure( mPassword ) );
340 group.writeEntry( "store-passwd", true );
343 group.writeEntry( "port", "993" );
345 if ( mEncryption == SSL ) {
346 group.writeEntry( "use-ssl", true );
347 } else if ( mEncryption == TLS ) {
348 group.writeEntry( "use-tls", true );
351 if ( mAuthenticationSend == PLAIN ) {
352 group.writeEntry( "authtype", "PLAIN" );
353 } else if ( mAuthenticationSend == LOGIN ) {
354 group.writeEntry( "authtype", "LOGIN" );
357 group.writeEntry( "sieve-support", mEnableSieve );
359 // locally unsubscribe the default folders
360 group.writeEntry( "locally-subscribed-folders", true );
361 QString groupwareFolders = QString("/INBOX/%1/,/INBOX/%2/,/INBOX/%3/,/INBOX/%4/,/INBOX/%5/")
362 .arg( i18n(s_folderContentsType[0]) ).arg( i18n(s_folderContentsType[1]) )
363 .arg( i18n(s_folderContentsType[2]) ).arg( i18n(s_folderContentsType[3]) )
364 .arg( i18n(s_folderContentsType[4]) );
365 group.writeEntry( "locallyUnsubscribedFolders", groupwareFolders );
367 group = c.group( QString("Folder-%1").arg( uid ) );
368 group.writeEntry( "isOpen", true );
370 group = c.group( QLatin1String( "AccountWizard" ) );
371 group.writeEntry( QLatin1String( "ShowOnStartup" ), false );
374 bool CreateImapAccount::writeToWallet(const QString & type, int id)
376 if ( !Wallet::isEnabled() )
377 return false;
378 if ( !mWallet || !mWallet->isOpen() ) {
379 delete mWallet;
380 WId window = 0;
381 if ( qApp->activeWindow() )
382 window = qApp->activeWindow()->winId();
383 mWallet = Wallet::openWallet( Wallet::NetworkWallet(), window );
384 if ( !mWallet )
385 return false;
387 QString folder, str_id;
388 if ( type=="transport" ) {
389 folder = "mailtransports";
390 str_id = QString::number( id );
391 } else {
392 folder = "kmail";
393 str_id = type + '-' + QString::number( id );
395 if ( !mWallet->hasFolder( folder ) )
396 mWallet->createFolder( folder );
397 mWallet->setFolder( folder );
398 return mWallet->writePassword( str_id, mPassword ) == 0;