email/pop3andsmtpmtm/clientmtms/src/cemailaccounts.cpp
changeset 0 72b543305e3a
child 19 7e4e4bcc75b6
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 #include <msvuids.h>
       
    18 #include <msvids.h>
       
    19 #include <centralrepository.h>
       
    20 #include <s32mem.h>
       
    21 
       
    22 #include "POP3SET.H"
       
    23 #include "IMAPSET.H"
       
    24 #include "SMTPSET.H"
       
    25 #include <iapprefs.h>
       
    26 #include <cemailaccounts.h>
       
    27 #include "MIUTSET.H"
       
    28 #include "cimapsyncdownloadrules.h"
       
    29 #include "cimaptransportbuffersizes.h"
       
    30 #include "cimsmtpmobilitysettings.h"
       
    31 
       
    32 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS  
       
    33 #include "msvconsts.h"
       
    34 #endif
       
    35 
       
    36 const TUint32 KNullId = 0x00000000;
       
    37 const TUint32 KIncrementAccount = 0x00100000;
       
    38 const TUint32 KInitialiseSettings = 0x00000000; // Initial settings are in account 0
       
    39 const TInt KMaxAccount = 2048;
       
    40 const TUint32 KDefaultAccountId = 0x80000000; // set top bit
       
    41 const TInt KNullSmtpMobilityAccountEntryPos = -1;
       
    42 
       
    43 const TInt KMaxIAPChoices = 4096;
       
    44 const TInt KShiftAccountId = 20;
       
    45 const TInt KPOPSettingsVersion = 2;
       
    46 const TInt KIMAPSettingsVersion = 2;
       
    47 const TInt KSMTPSettingsVersion = 3;
       
    48 const TInt KIAPPrefSettingsVersion = 2;
       
    49 const TInt KImapSyncDownloadRulesVersion = 1;
       
    50 const TInt KImapTransportBufferSizesVersion = 1;
       
    51 const TInt KSmtpMobilityAccountsListVersion = 1;
       
    52 const TInt KSmtpLinkedMobilityAccountsListVersion = 1;
       
    53 
       
    54 /** 
       
    55 Allocates and constructs an Email accounts object.
       
    56 
       
    57 Initialises all member data to their default values.
       
    58 
       
    59 @return
       
    60 The newly constructed Email accounts object.
       
    61 */
       
    62 EXPORT_C CEmailAccounts* CEmailAccounts::NewL()
       
    63 	{
       
    64 	CEmailAccounts* self = new (ELeave) CEmailAccounts();
       
    65 	return self;
       
    66 	}
       
    67 
       
    68 /** 
       
    69 Allocates and constructs an Email accounts object.
       
    70 
       
    71 Initialises all member data to their default values.
       
    72 
       
    73 @return
       
    74 The newly constructed Email accounts object.
       
    75 */
       
    76 EXPORT_C CEmailAccounts* CEmailAccounts::NewLC()
       
    77 	{
       
    78 	CEmailAccounts* self = new (ELeave) CEmailAccounts();
       
    79 	CleanupStack::PushL(self);
       
    80 	return self;
       
    81 	}
       
    82 
       
    83 /**
       
    84 Constructor.
       
    85 */
       
    86 CEmailAccounts::CEmailAccounts()
       
    87 	{
       
    88 	}
       
    89 
       
    90 /** 
       
    91 Destructor. 
       
    92 */
       
    93 EXPORT_C CEmailAccounts::~CEmailAccounts()
       
    94 	{
       
    95 	delete iIMAPRepository;
       
    96 	delete iSMTPRepository;
       
    97 	delete iPOPRepository;
       
    98 	delete iMsvSession;
       
    99 	}
       
   100 
       
   101 /**
       
   102 Creates a POP Account:
       
   103 
       
   104 1. Creates a POP service entry in the message store
       
   105 
       
   106 2. Creates settings in CenRep with values from the supplied objects
       
   107 
       
   108 3. Returns a POP account ID
       
   109 
       
   110 @param	aAccountName
       
   111 Account Name
       
   112 
       
   113 @param	aPopSettings
       
   114 POP Service Settings
       
   115 
       
   116 @param	aPopIAP
       
   117 POP IAP preferences
       
   118 
       
   119 @param	aReadOnly
       
   120 Specify ETrue if it is a read-only account. 
       
   121 Note: Read-only accounts should not be modified, however we don't enforce it.
       
   122 
       
   123 @return
       
   124 POP account ID of the new account
       
   125 @leave
       
   126 KErrOverflow if the account name is longer than 256 characters.
       
   127 KErrArgument if settings fields contained non-ascii printable characters.
       
   128 */
       
   129 EXPORT_C TPopAccount CEmailAccounts::CreatePopAccountL(const TDesC& aAccountName, const CImPop3Settings& aPopSettings,
       
   130 	const CImIAPPreferences& aPopIAP, TBool aReadOnly)
       
   131 	{
       
   132 	// Check the incoming account name, if its too long then leave with KErrOverflow
       
   133 	if(aAccountName.Length() > KEmailAccountNameSize)
       
   134 		{
       
   135 		User::Leave(KErrOverflow);
       
   136 		}
       
   137 	
       
   138 	// Check for alpha numerics for the various strings
       
   139 	if(!IsUnicodePrintable(aAccountName) ||
       
   140 		!IsAsciiPrintable(aPopSettings.ServerAddress()) ||
       
   141 	   	!IsAsciiPrintable(aPopSettings.Password()) ||
       
   142 		!IsAsciiPrintable(aPopSettings.LoginName()))
       
   143 		{
       
   144 		User::Leave(KErrArgument);
       
   145 		}
       
   146 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   147 	// Create  POP header table .
       
   148 	CreateHeaderStoreL(KUidMsgTypePOP3);
       
   149 #endif	
       
   150 	// Create POP service entry in the message store
       
   151 	TMsvId popService = KMsvNullIndexEntryId;
       
   152 	CMsvEntry* msvEntry = SessionL().GetEntryL(KMsvRootIndexEntryId);
       
   153     CleanupStack::PushL(msvEntry);		
       
   154 	popService = CreateEmailServiceL(*msvEntry, aAccountName, KUidMsgTypePOP3, ETrue);
       
   155 
       
   156 	// Save settings to CenRep
       
   157 	TUint32 popAccountId = 0;
       
   158 	TRAPD(error, popAccountId = DoCreatePopAccountL(aAccountName, aPopSettings, aPopIAP, popService, 0, aReadOnly));
       
   159 	if (error != KErrNone)
       
   160 		{
       
   161 		// saving settings to CenRep failed, so delete pop service and leave
       
   162 		DeleteServiceEntryL(*msvEntry, popService);		
       
   163 		User::Leave(error);
       
   164 		}
       
   165 
       
   166 	// Create account Id
       
   167 	TPopAccount account;
       
   168 	account.iPopAccountId = popAccountId >> KShiftAccountId; 
       
   169 	account.iPopAccountName = aAccountName; 							
       
   170 	account.iPopService = popService;
       
   171 	account.iSmtpService = KMsvNullIndexEntryId; // Related smtp service not created yet 
       
   172 	
       
   173 
       
   174 
       
   175 	// settings saved, so un-set InPreparation Flags	
       
   176 	UnSetInPreparationFlagsL(*msvEntry, popService, account.iPopAccountId);
       
   177 	CleanupStack::PopAndDestroy(msvEntry);	
       
   178 	return account;				
       
   179 	}
       
   180 
       
   181 TUint32 CEmailAccounts::DoCreatePopAccountL(const TDesC& aAccountName, const CImPop3Settings& aPopSettings, 
       
   182 		const CImIAPPreferences& aPopIAP, const TMsvId aPopService, TMsvId aRelatedService, TBool aReadOnly)
       
   183 	{
       
   184 	CRepository& popRepository = PopRepositoryL();
       
   185 	TUint32 popAccountId = GetNextEmptyAccountSlotL(popRepository);
       
   186 	
       
   187 	User::LeaveIfError(popRepository.StartTransaction(CRepository::EReadWriteTransaction));					
       
   188 	popRepository.CleanupRollbackTransactionPushL();
       
   189 	popRepository.Set(popAccountId + EPOPServiceId, static_cast<TInt>(aPopService)); 
       
   190 	popRepository.Set(popAccountId + EPOPAccountNameId, aAccountName);
       
   191 	popRepository.Set(popAccountId + EPOPRelatedSMTPServiceId, static_cast<TInt>(aRelatedService));
       
   192 	popRepository.Set(popAccountId + EPOPReadOnlyAccountId, static_cast<TInt>(aReadOnly));
       
   193  	DoSavePopSettingsL(popAccountId, aPopSettings);
       
   194 	SaveIAPPrefSettingsL(popAccountId, aPopIAP, popRepository);
       
   195 	CleanupStack::Pop();
       
   196 	TUint32 errorId = 0;	
       
   197 	User::LeaveIfError(popRepository.CommitTransaction(errorId));	
       
   198 	
       
   199 	return popAccountId;
       
   200 	}
       
   201 
       
   202 TMsvId CEmailAccounts::CreateEmailServiceL(CMsvEntry& aMsvEntry, const TDesC& aAccountName, TUid aMTMType, TBool aVisible)
       
   203     {
       
   204 	TMsvEntry entry;
       
   205 	entry.iMtm = aMTMType;
       
   206 	entry.iType = KUidMsvServiceEntry;
       
   207     entry.iServiceId = KMsvNullIndexEntryId;
       
   208 	entry.SetReadOnly(EFalse);
       
   209 	entry.SetVisible(aVisible);	
       
   210     entry.iDate.UniversalTime();
       
   211 	entry.iDetails.Set(aAccountName);
       
   212 	entry.iDescription.Set(KNullDesC);
       
   213 	entry.SetInPreparation(ETrue);
       
   214 	aMsvEntry.SetEntryL(KMsvRootIndexEntryId);
       
   215 
       
   216 	aMsvEntry.CreateL(entry);
       
   217 	return (entry.Id());
       
   218 	}
       
   219 
       
   220 void CEmailAccounts::SetRelatedIdL(CMsvEntry& aMsvEntry, TMsvId aId, TMsvId aRelatedId)
       
   221 	{	
       
   222 	aMsvEntry.SetEntryL(aId);	
       
   223 	TMsvEntry entry(aMsvEntry.Entry());
       
   224 	entry.iRelatedId = aRelatedId;	
       
   225 	aMsvEntry.ChangeL(entry);
       
   226 	}
       
   227 
       
   228 void CEmailAccounts::SetEntryDetailsL(TMsvId aId, const TDesC& aAccountName)
       
   229 	{	
       
   230 	CMsvEntry* msvEntry = SessionL().GetEntryL(aId);
       
   231     CleanupStack::PushL(msvEntry);
       
   232 
       
   233 	TMsvEntry entry(msvEntry->Entry());
       
   234 	entry.iDetails.Set(aAccountName);	
       
   235 	msvEntry->ChangeL(entry);
       
   236 
       
   237 	CleanupStack::PopAndDestroy(msvEntry);	
       
   238 	}
       
   239 
       
   240 void CEmailAccounts::UnSetInPreparationFlagsL(CMsvEntry& aMsvEntry, TMsvId aId, TInt aAccountId)
       
   241 	{
       
   242 	aMsvEntry.SetEntryL(aId);	
       
   243 	TMsvEntry entry = aMsvEntry.Entry();
       
   244 	entry.SetMtmData2(aAccountId);
       
   245 	entry.SetInPreparation(EFalse);	
       
   246 	aMsvEntry.ChangeL(entry);
       
   247 	}
       
   248 
       
   249 void CEmailAccounts::DeleteServiceEntryL(CMsvEntry& aMsvEntry, TMsvId aId)
       
   250 	{
       
   251 	aMsvEntry.SetEntryL(KMsvRootIndexEntryId);	
       
   252 	TRAPD (error, aMsvEntry.DeleteL(aId));
       
   253 	if (error != KErrNotFound)
       
   254 		{
       
   255 		User::LeaveIfError(error);		
       
   256 		}
       
   257 	}
       
   258 
       
   259 /**
       
   260 Creates an IMAP Account:
       
   261 
       
   262 1. Creates an IMAP service entry in the message store
       
   263 
       
   264 2. Creates settings in CenRep with values from the supplied objects
       
   265 
       
   266 3. Returns an IMAP account ID
       
   267 
       
   268 @param	aAccountName
       
   269 Account Name
       
   270 
       
   271 @param	aImapSettings
       
   272 IMAP Service Settings
       
   273 
       
   274 @param	aImapIAP
       
   275 IMAP IAP preferences
       
   276 
       
   277 @param	aReadOnly
       
   278 Specify ETrue if it is a read-only account
       
   279 Note: Read-only accounts should not be modified, however we don't enforce it.
       
   280 
       
   281 @return
       
   282 IMAP account ID
       
   283 @leave
       
   284 KErrOverflow if the account name is longer than 256 characters.
       
   285 KErrArgument if settings fields contained non-ascii printable characters.
       
   286 */
       
   287 EXPORT_C TImapAccount CEmailAccounts::CreateImapAccountL(const TDesC& aAccountName,
       
   288 	const CImImap4Settings& aImapSettings, const CImIAPPreferences& aImapIAP, TBool aReadOnly)
       
   289 	{
       
   290 	return CreateImapAccountL(aAccountName, aImapSettings, aImapIAP, NULL, aReadOnly);
       
   291 	}
       
   292 
       
   293 /**
       
   294 Creates an IMAP Account:
       
   295 
       
   296 1. Creates an IMAP service entry in the message store
       
   297 
       
   298 2. Creates settings in CenRep with values from the supplied objects
       
   299 
       
   300 3. Returns an IMAP account ID
       
   301 
       
   302 @param	aAccountName
       
   303 Account Name
       
   304 
       
   305 @param	aImapSettings
       
   306 IMAP Service Settings
       
   307 
       
   308 @param	aImapIAP
       
   309 IMAP IAP preferences
       
   310 
       
   311 @param	aSyncDownloadRules
       
   312 IMAP synchronisation download rules
       
   313 
       
   314 @param	aReadOnly
       
   315 Specify ETrue if it is a read-only account
       
   316 Note: Read-only accounts should not be modified, however we don't enforce it.
       
   317 
       
   318 @return
       
   319 IMAP account ID
       
   320 
       
   321 @publishedPartner
       
   322 */	
       
   323 EXPORT_C TImapAccount CEmailAccounts::CreateImapAccountL(const TDesC& aAccountName,
       
   324 	const CImImap4Settings& aImapSettings, const CImIAPPreferences& aImapIAP,
       
   325 	const CImapSyncDownloadRules& aSyncDownloadRules, TBool aReadOnly)
       
   326 	{
       
   327 	return CreateImapAccountL(aAccountName, aImapSettings, aImapIAP, &aSyncDownloadRules, aReadOnly);
       
   328 	}
       
   329 
       
   330 /**
       
   331 Creates an IMAP Account:
       
   332 
       
   333 1. Creates an IMAP service entry in the message store
       
   334 
       
   335 2. Creates settings in CenRep with values from the supplied objects
       
   336 
       
   337 3. Returns an IMAP account ID
       
   338 
       
   339 @param	aAccountName
       
   340 Account Name
       
   341 
       
   342 @param	aImapSettings
       
   343 IMAP Service Settings
       
   344 
       
   345 @param	aImapIAP
       
   346 IMAP IAP preferences
       
   347 
       
   348 @param	aReadOnly
       
   349 Specify ETrue if it is a read-only account
       
   350 Note: Read-only accounts should not be modified, however we don't enforce it.
       
   351 
       
   352 @param	aSyncDownloadRules
       
   353 Synchronisation download rules (can be NULL)
       
   354 
       
   355 @return
       
   356 IMAP account ID
       
   357 */	
       
   358 TImapAccount CEmailAccounts::CreateImapAccountL(const TDesC& aAccountName,
       
   359 	const CImImap4Settings& aImapSettings, const CImIAPPreferences& aImapIAP,
       
   360 	const CImapSyncDownloadRules* aSyncDownloadRules, TBool aReadOnly)
       
   361 	{
       
   362 	// Check the incoming account name, if its too long then leave with KErrOverflow
       
   363 	if(aAccountName.Length() > KEmailAccountNameSize)
       
   364 		{
       
   365 		User::Leave(KErrOverflow);
       
   366 		}
       
   367 
       
   368 	// Check for alpha numerics for the various strings
       
   369 	if(!IsUnicodePrintable(aAccountName) ||
       
   370 	   !IsAsciiPrintable(aImapSettings.ServerAddress()) ||
       
   371 	   !IsAsciiPrintable(aImapSettings.FolderPath()))
       
   372 		{
       
   373 		User::Leave(KErrArgument);
       
   374 		}
       
   375 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   376 	// Create  IMAP header table .
       
   377 	CreateHeaderStoreL(KUidMsgTypeIMAP4);
       
   378 #endif
       
   379 	// Create IMAP service entry in the message store
       
   380 	TMsvId imapService = KMsvNullIndexEntryId;
       
   381 	CMsvEntry* msvEntry = SessionL().GetEntryL(KMsvRootIndexEntryId);
       
   382 	CleanupStack::PushL(msvEntry);		
       
   383 	imapService = CreateEmailServiceL(*msvEntry, aAccountName, KUidMsgTypeIMAP4, ETrue);
       
   384 
       
   385 	// Save settings to CenRep
       
   386 	TUint32 imapAccountId = 0;
       
   387 	TRAPD(error, imapAccountId = DoCreateImapAccountL(aAccountName, aImapSettings, aImapIAP, aSyncDownloadRules, imapService, 0, aReadOnly));
       
   388 	if (error != KErrNone)
       
   389 		{
       
   390 		// saving settings to CenRep failed, so delete imap service and leave
       
   391 		DeleteServiceEntryL(*msvEntry, imapService);	
       
   392 		User::Leave(error);
       
   393 		}
       
   394 
       
   395 	TImapAccount account;
       
   396 	account.iImapAccountId = imapAccountId >> KShiftAccountId;
       
   397 	account.iImapAccountName = aAccountName; 				
       
   398 	account.iImapService = imapService; 
       
   399 	account.iSmtpService = KMsvNullIndexEntryId; 	// Related smtp service not created yet		
       
   400 
       
   401 	// settings saved, so un-set InPreparation Flags	
       
   402 	UnSetInPreparationFlagsL(*msvEntry, imapService, account.iImapAccountId);
       
   403 	CleanupStack::PopAndDestroy(msvEntry);	
       
   404 	return account;				
       
   405 	}
       
   406 
       
   407 TUint32 CEmailAccounts::DoCreateImapAccountL(const TDesC& aAccountName,
       
   408 	const CImImap4Settings& aImapSettings, const CImIAPPreferences& aImapIAP,
       
   409 	const CImapSyncDownloadRules* aSyncDownloadRules, TMsvId aImapService,
       
   410 	TMsvId aRelatedService, TBool aReadOnly)
       
   411 	{
       
   412 	CRepository& imapRepository = ImapRepositoryL();
       
   413 	TUint32 imapAccountId = GetNextEmptyAccountSlotL(imapRepository);
       
   414 
       
   415 	User::LeaveIfError(imapRepository.StartTransaction(CRepository::EReadWriteTransaction));						
       
   416 	imapRepository.CleanupRollbackTransactionPushL();
       
   417 	imapRepository.Set(imapAccountId + EIMAPServiceId, static_cast<TInt>(aImapService));
       
   418 	imapRepository.Set(imapAccountId + EIMAPAccountNameId, aAccountName);
       
   419 	imapRepository.Set(imapAccountId + EIMAPRelatedSMTPServiceId, static_cast<TInt>(aRelatedService));
       
   420 	imapRepository.Set(imapAccountId + EIMAPReadOnlyAccountId, static_cast<TInt>(aReadOnly));
       
   421 	DoSaveImapSettingsL(imapAccountId, aImapSettings);
       
   422 	if (aSyncDownloadRules)
       
   423 		{
       
   424 		DoSaveImapSyncDownloadRulesL(imapAccountId, *aSyncDownloadRules, imapRepository);
       
   425 		}
       
   426 	SaveIAPPrefSettingsL(imapAccountId, aImapIAP, imapRepository);
       
   427 	CleanupStack::Pop();
       
   428 	TUint32 errorId = 0;	
       
   429 	User::LeaveIfError(imapRepository.CommitTransaction(errorId));
       
   430 	
       
   431 	return imapAccountId;			
       
   432 	}
       
   433 
       
   434 /**
       
   435 Creates a SMTP Account:
       
   436 
       
   437 1. Creates a SMTP service entry in the message store
       
   438 
       
   439 2. Creates settings in CenRep with values from the supplied objects
       
   440 
       
   441 3. Relates this account with the supplied POP account
       
   442 
       
   443 4. Returns a SMTP account ID
       
   444 
       
   445 @param	aPopAccount
       
   446 POP Account Id
       
   447 
       
   448 @param	aSmtpSettings
       
   449 SMTP Service Settings
       
   450 
       
   451 @param	aSmtpIAP
       
   452 SMTP IAP preferences
       
   453 
       
   454 @param	aReadOnly
       
   455 Specify ETrue if it is a read-only account
       
   456 Note: Read-only accounts should not be modified, however we don't enforce it.
       
   457 
       
   458 @return
       
   459 SMTP account ID
       
   460 @leave
       
   461 KErrArgument if settings fields contained non-ascii printable characters.
       
   462 */
       
   463 EXPORT_C TSmtpAccount CEmailAccounts::CreateSmtpAccountL(TPopAccount& aPopAccount, const CImSmtpSettings& aSmtpSettings,
       
   464 		const CImIAPPreferences& aSmtpIAP, TBool aReadOnly)
       
   465 	{
       
   466  	// Check for alpha numerics for the various strings
       
   467  	if(!IsAsciiPrintable(aSmtpSettings.Password()) ||
       
   468  	   !IsAsciiPrintable(aSmtpSettings.LoginName())||
       
   469  	   !IsAsciiPrintable(aSmtpSettings.ServerAddress()))
       
   470  		{
       
   471  		User::Leave(KErrArgument);
       
   472  		}
       
   473 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   474  	// Create  SMTP header table .
       
   475  	CreateHeaderStoreL(KUidMsgTypeSMTP);
       
   476 #endif
       
   477 	// Create SMTP service entries in the message store
       
   478 	TMsvId smtpService = KMsvNullIndexEntryId;
       
   479 	CMsvEntry* msvEntry = SessionL().GetEntryL(KMsvRootIndexEntryId);
       
   480     CleanupStack::PushL(msvEntry);		
       
   481 	smtpService = CreateEmailServiceL(*msvEntry, aPopAccount.iPopAccountName, KUidMsgTypeSMTP, EFalse);		
       
   482 
       
   483 	// Save settings to CenRep
       
   484 	TUint32 smtpAccountId = 0;
       
   485 	TUint32 popAccountId = aPopAccount.iPopAccountId << KShiftAccountId;
       
   486 	TRAPD(error, smtpAccountId = DoCreateSmtpAccountL(aPopAccount, aSmtpSettings, 
       
   487 			aSmtpIAP, smtpService, *msvEntry, popAccountId, aReadOnly));
       
   488 	if (error != KErrNone)
       
   489 		{
       
   490 		// saving settings to CenRep failed, so delete smtp service and leave
       
   491 		PopRepositoryL().Set(popAccountId + EPOPRelatedSMTPServiceId, 0);
       
   492 		SetRelatedIdL(*msvEntry, aPopAccount.iPopService, 0);				
       
   493 		DeleteServiceEntryL(*msvEntry, smtpService);
       
   494 		User::Leave(error);
       
   495 		}
       
   496 
       
   497 	aPopAccount.iSmtpService = smtpService;
       
   498 
       
   499 	TSmtpAccount account;
       
   500 	account.iSmtpAccountId = smtpAccountId >> KShiftAccountId;
       
   501 	account.iSmtpAccountName = aPopAccount.iPopAccountName; 				
       
   502 	account.iSmtpService = smtpService; 
       
   503 	account.iRelatedService = aPopAccount.iPopService;	
       
   504 
       
   505 	// settings saved, so un-set InPreparation Flags
       
   506 	UnSetInPreparationFlagsL(*msvEntry, smtpService, account.iSmtpAccountId);
       
   507 	CleanupStack::PopAndDestroy(msvEntry);	
       
   508 	return account;				
       
   509 	}
       
   510 
       
   511 TUint32 CEmailAccounts::DoCreateSmtpAccountL(TPopAccount& aPopAccount, const CImSmtpSettings& aSmtpSettings, 
       
   512 		const CImIAPPreferences& aSmtpIAP, TMsvId aSmtpService, CMsvEntry& aMsvEntry, TUint32& aPopAccountId, TBool aReadOnly)
       
   513 	{
       
   514 	// set related smtp service
       
   515 	SetRelatedIdL(aMsvEntry, aSmtpService, aPopAccount.iPopService);
       
   516 		
       
   517 	// update pop account
       
   518 	SetRelatedIdL(aMsvEntry, aPopAccount.iPopService, aSmtpService);		
       
   519 	User::LeaveIfError(PopRepositoryL().Set(aPopAccountId + EPOPRelatedSMTPServiceId, static_cast<TInt>(aSmtpService)));
       
   520 
       
   521 	return DoCreateSmtpAccountL(aPopAccount.iPopAccountName, aSmtpSettings, aSmtpIAP, aSmtpService, aPopAccount.iPopService, aReadOnly);
       
   522 	}
       
   523 
       
   524 /**
       
   525 Creates a SMTP Account:
       
   526 
       
   527 1. Creates a SMTP service entry in the message store
       
   528 
       
   529 2. Creates settings in CenRep with values from the supplied objects
       
   530 
       
   531 3. Relates this account with the supplied IMAP account
       
   532 
       
   533 4. Returns a SMTP account ID
       
   534 
       
   535 @param	aImapAccount
       
   536 IMAP account ID
       
   537 
       
   538 @param	aSmtpSettings
       
   539 SMTP Service Settings
       
   540 
       
   541 @param	aSmtpIAP
       
   542 SMTP IAP preferences
       
   543 
       
   544 @param	aReadOnly
       
   545 Specify ETrue if it is a read-only account
       
   546 Note: Read-only accounts should not be modified, however we don't enforce it.
       
   547 
       
   548 @return
       
   549 SMTP account ID
       
   550 @leave
       
   551 KErrArgument if settings fields contained non-ascii printable characters.
       
   552 */
       
   553 EXPORT_C TSmtpAccount CEmailAccounts::CreateSmtpAccountL(TImapAccount& aImapAccount, const CImSmtpSettings& aSmtpSettings,
       
   554 		const CImIAPPreferences& aSmtpIAP, TBool aReadOnly)
       
   555 	{
       
   556  	// Check for alpha numerics for the various strings
       
   557  	if(!IsAsciiPrintable(aSmtpSettings.Password()) ||
       
   558  	   !IsAsciiPrintable(aSmtpSettings.LoginName())||
       
   559  	   !IsAsciiPrintable(aSmtpSettings.ServerAddress()))
       
   560  		{
       
   561  		User::Leave(KErrArgument);
       
   562  		}
       
   563 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   564 
       
   565 	// Create  SMTP header table .
       
   566   	CreateHeaderStoreL(KUidMsgTypeSMTP);
       
   567 #endif 	
       
   568 	// Create SMTP service entries in the message store
       
   569 	TMsvId smtpService = KMsvNullIndexEntryId;
       
   570 	CMsvEntry* msvEntry = SessionL().GetEntryL(KMsvRootIndexEntryId);
       
   571     CleanupStack::PushL(msvEntry);		
       
   572 	smtpService = CreateEmailServiceL(*msvEntry, aImapAccount.iImapAccountName, KUidMsgTypeSMTP, EFalse);		
       
   573 
       
   574 	// Save settings to CenRep
       
   575 	TUint32 smtpAccountId = 0;
       
   576 	TUint32 imapAccountId = aImapAccount.iImapAccountId << KShiftAccountId;
       
   577 	TRAPD(error, smtpAccountId = DoCreateSmtpAccountL(aImapAccount, aSmtpSettings, 
       
   578 			aSmtpIAP, smtpService, *msvEntry, imapAccountId, aReadOnly));
       
   579 	if (error != KErrNone)
       
   580 		{
       
   581 		// saving settings to CenRep failed, so delete smtp service and leave
       
   582 		ImapRepositoryL().Set(imapAccountId + EIMAPRelatedSMTPServiceId, 0);
       
   583 		SetRelatedIdL(*msvEntry, aImapAccount.iImapService, 0);				
       
   584 		DeleteServiceEntryL(*msvEntry, smtpService);		
       
   585 		User::Leave(error);
       
   586 		}
       
   587 
       
   588 	aImapAccount.iSmtpService = smtpService;
       
   589 
       
   590 	TSmtpAccount account;
       
   591 	account.iSmtpAccountId = smtpAccountId >> KShiftAccountId;
       
   592 	account.iSmtpAccountName = aImapAccount.iImapAccountName; 				
       
   593 	account.iSmtpService = smtpService; 
       
   594 	account.iRelatedService = aImapAccount.iImapService;	
       
   595 
       
   596 	// settings saved, so un-set InPreparation Flags
       
   597 	UnSetInPreparationFlagsL(*msvEntry, smtpService, account.iSmtpAccountId);
       
   598 	CleanupStack::PopAndDestroy(msvEntry);	
       
   599 	return account;				
       
   600 	}
       
   601 
       
   602 TUint32 CEmailAccounts::DoCreateSmtpAccountL(TImapAccount& aImapAccount, const CImSmtpSettings& aSmtpSettings, 
       
   603 		const CImIAPPreferences& aSmtpIAP, TMsvId aSmtpService, CMsvEntry& aMsvEntry, TUint32& aImapAccountId, TBool aReadOnly)
       
   604 	{
       
   605 	// set related smtp service
       
   606 	SetRelatedIdL(aMsvEntry, aSmtpService, aImapAccount.iImapService);
       
   607 		
       
   608 	// update imap account
       
   609 	SetRelatedIdL(aMsvEntry, aImapAccount.iImapService, aSmtpService);		
       
   610 	User::LeaveIfError(ImapRepositoryL().Set(aImapAccountId + EIMAPRelatedSMTPServiceId, static_cast<TInt>(aSmtpService)));
       
   611 
       
   612 	return DoCreateSmtpAccountL(aImapAccount.iImapAccountName, aSmtpSettings, aSmtpIAP, aSmtpService, aImapAccount.iImapService, aReadOnly);
       
   613 	}
       
   614 
       
   615 /**
       
   616 Creates an SMTP Account:
       
   617 
       
   618 1. Creates an SMTP service entry in the message store
       
   619 
       
   620 2. Creates settings in CenRep with values from the supplied objects
       
   621 
       
   622 3. Returns an SMTP account ID
       
   623 
       
   624 @param	aAccountName
       
   625 Account Name
       
   626 
       
   627 @param	aSmtpSettings
       
   628 SMTP Service Setting
       
   629 
       
   630 @param	aSmtpIAP
       
   631 SMTP IAP preferences
       
   632 
       
   633 @param	aReadOnly
       
   634 Specify ETrue if it is a read-only account
       
   635 Note: Read-only accounts should not be modified, however we don't enforce it.
       
   636 
       
   637 @return
       
   638 SMTP account ID
       
   639 @leave
       
   640 KErrOverflow if the account name is longer than 256 characters.
       
   641 KErrArgument if settings fields contained non-ascii printable characters.
       
   642 */	
       
   643 EXPORT_C TSmtpAccount CEmailAccounts::CreateSmtpAccountL(const TDesC& aAccountName, const CImSmtpSettings& aSmtpSettings, 
       
   644 		const CImIAPPreferences& aSmtpIAP, TBool aReadOnly)
       
   645 	{
       
   646 	// Check the incoming account name, if its too long then leave with KErrOverflow
       
   647 	if(aAccountName.Length() > KEmailAccountNameSize)
       
   648 		{
       
   649 		User::Leave(KErrOverflow);
       
   650 		}
       
   651 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   652 	// Create  SMTP header table .
       
   653   	CreateHeaderStoreL(KUidMsgTypeSMTP);
       
   654 #endif 	
       
   655 
       
   656  	// Check for alpha numerics for the various strings
       
   657  	if(!IsUnicodePrintable(aAccountName) ||
       
   658  	   !IsAsciiPrintable(aSmtpSettings.Password()) ||
       
   659  	   !IsAsciiPrintable(aSmtpSettings.LoginName())||
       
   660  	   !IsAsciiPrintable(aSmtpSettings.ServerAddress()))
       
   661  		{
       
   662  		User::Leave(KErrArgument);
       
   663  		}
       
   664 
       
   665 	// Create SMTP service entry in the message store
       
   666 	TMsvId smtpService = KMsvNullIndexEntryId;
       
   667 	CMsvEntry* msvEntry = SessionL().GetEntryL(KMsvRootIndexEntryId);
       
   668     CleanupStack::PushL(msvEntry);		
       
   669 	smtpService = CreateEmailServiceL(*msvEntry, aAccountName, KUidMsgTypeSMTP, EFalse);	
       
   670 
       
   671 	// Save settings to CenRep
       
   672 	TUint32 smtpAccountId = 0;
       
   673 	TRAPD(error, smtpAccountId = DoCreateSmtpAccountL(aAccountName, aSmtpSettings, aSmtpIAP, smtpService, 0, aReadOnly));
       
   674 	if (error != KErrNone)
       
   675 		{
       
   676 		// saving settings to CenRep failed, so delete smtp service and leave
       
   677 		DeleteServiceEntryL(*msvEntry, smtpService);
       
   678 		User::Leave(error);
       
   679 		}
       
   680 
       
   681 	// Create account Id
       
   682 	TSmtpAccount account;
       
   683 	account.iSmtpAccountId = smtpAccountId >> KShiftAccountId; 
       
   684 	account.iSmtpAccountName = aAccountName; 						
       
   685 	account.iSmtpService = smtpService; 
       
   686 	account.iRelatedService = KMsvNullIndexEntryId;   // Related pop/imap service not created yet
       
   687 
       
   688 	// settings saved, so un-set InPreparation Flags	
       
   689 	UnSetInPreparationFlagsL(*msvEntry, smtpService, account.iSmtpAccountId);
       
   690 	CleanupStack::PopAndDestroy(msvEntry);	
       
   691 	return account;					
       
   692 	}
       
   693 
       
   694 TUint32 CEmailAccounts::DoCreateSmtpAccountL(const TDesC& aAccountName, const CImSmtpSettings& aSmtpSettings, 
       
   695 		const CImIAPPreferences& aSmtpIAP, TMsvId aSmtpService, TMsvId aRelatedService, TBool aReadOnly)
       
   696 	{
       
   697 	CRepository& smtpRepository = SmtpRepositoryL();	
       
   698 	TUint32 smtpAccountId = GetNextEmptyAccountSlotL(smtpRepository);
       
   699 		 	
       
   700  	User::LeaveIfError(smtpRepository.StartTransaction(CRepository::EReadWriteTransaction));					 	
       
   701 	smtpRepository.CleanupRollbackTransactionPushL();
       
   702 	smtpRepository.Set(smtpAccountId, static_cast<TInt>(aSmtpService));
       
   703 	smtpRepository.Set(smtpAccountId + ESMTPAccountNameId, aAccountName);
       
   704 	smtpRepository.Set(smtpAccountId + ESMTPRelatedServiceId, static_cast<TInt>(aRelatedService));
       
   705 	smtpRepository.Set(smtpAccountId + ESMTPReadOnlyAccountId, static_cast<TInt>(aReadOnly));
       
   706 	smtpRepository.Set(smtpAccountId + ESMTPParentAccountId, KNullSmtpAccountId);
       
   707 	DoSaveSmtpSettingsL(smtpAccountId, aSmtpSettings);
       
   708 	SaveIAPPrefSettingsL(smtpAccountId, aSmtpIAP, smtpRepository);	
       
   709 	CleanupStack::Pop();
       
   710 	TUint32 errorId = 0;
       
   711 	User::LeaveIfError(smtpRepository.CommitTransaction(errorId));
       
   712 	
       
   713 	return smtpAccountId;
       
   714 	}
       
   715 
       
   716 /**
       
   717 Creates a SMTP mobility account.
       
   718 
       
   719 An SMTP mobility account allows different send settings to be used by SMTP
       
   720 accounts depending on what access point is being used by the SMTP connection.
       
   721 When using this API to create a SMTP mobility account, a list of IAPs and a
       
   722 set of settings are specified. The SMTP account which owns the SMTP mobility
       
   723 account is also specified. A SMTP account can own multiple SMTP mobility
       
   724 accounts.
       
   725 When a connection is made for a SMTP account, its associated list of SMTP
       
   726 mobility accounts is examined to see if any of them have an IAP list which
       
   727 contains the IAP that the connection is using. If so, the settings for that
       
   728 SMTP mobility account will be used to send emails.
       
   729 If an SMTP account has no associated SMTP mobility accounts, or there is no
       
   730 SMTP mobility account with a matching IAP, then the settings for the SMTP
       
   731 account are used when sending emails.
       
   732 
       
   733 @param aAccountName Account name
       
   734 @param aIapList List of IAPs that will use the account settings
       
   735 @param aSmtpAccount The SMTP account to add the mobility account to
       
   736 @param aSettings Settings to use for the account
       
   737 @param aReadOnly Flag to indicate if account is read only. Note that while read
       
   738 only accounts should not be modified, we do not enforce this.
       
   739 
       
   740 @publishedPartner
       
   741 */
       
   742 EXPORT_C TSmtpMobilityAccount CEmailAccounts::CreateSmtpMobilityAccountL(const TDesC& aAccountName, const RArrayIAP& aIapList, const TSmtpAccount& aSmtpAccount, const CImSmtpMobilitySettings& aSettings, TBool aReadOnly)
       
   743 	{
       
   744 	return DoCreateSmtpMobilityAccountL(aAccountName, aIapList, aSettings, aSmtpAccount, KNullSmtpAccountId, aReadOnly);
       
   745 	}
       
   746 
       
   747 /**
       
   748 Creates a SMTP mobility account using settings from an SMTP account.
       
   749 
       
   750 An SMTP mobility account allows different send settings to be used by SMTP
       
   751 accounts depending on what access point is being used by the SMTP connection.
       
   752 When using this API to create a SMTP mobility account, a list of IAPs and a
       
   753 linked SMTP account are specified. The settings for the SMTP mobility account
       
   754 are then taken from that linked SMTP account. The SMTP account which owns the
       
   755 SMTP mobility account is also specified. A SMTP account can own multiple SMTP
       
   756 mobility accounts.
       
   757 When a connection is made for a SMTP account, its associated list of SMTP
       
   758 mobility accounts is examined to see if any of them have an IAP list which
       
   759 contains the IAP that the connection is using. If so, the settings for that
       
   760 SMTP mobility account will be used to send emails.
       
   761 If an SMTP account has no associated SMTP mobility accounts, or there is no
       
   762 SMTP mobility account with a matching IAP, then the settings for the SMTP
       
   763 account are used when sending emails.
       
   764 Note that a SMTP mobility account created in this way will always have the
       
   765 same settings as the linked SMTP account. If the settings for the linked
       
   766 SMTP account change then the settings for the SMTP mobility account also
       
   767 change to match them. It is not possible to update the settings for a SMTP
       
   768 mobility account created in this way other than through changing the settings
       
   769 for the linked SMTP account.
       
   770 
       
   771 @param aAccountName Account name
       
   772 @param aIapList List of IAPs that will use the account settings
       
   773 @param aSmtpAccount The SMTP account to add the mobility account to
       
   774 @param aLinkedSmtpAccount SMTP account to get settings from
       
   775 @param aReadOnly Flag to indicate if account is read only. Note that while read
       
   776 only accounts should not be modified, we do not enforce this.
       
   777 
       
   778 @leave KErrArgument Attempted to add to an SMTP account which is the same as
       
   779        the linked SMTP account
       
   780 
       
   781 @publishedPartner
       
   782 */
       
   783 EXPORT_C TSmtpMobilityAccount CEmailAccounts::CreateSmtpMobilityAccountL(const TDesC& aAccountName, const RArrayIAP& aIapList, const TSmtpAccount& aSmtpAccount, const TSmtpAccount& aLinkedSmtpAccount, TBool aReadOnly)
       
   784 	{
       
   785 	if (aSmtpAccount.iSmtpAccountId == aLinkedSmtpAccount.iSmtpAccountId)
       
   786 		{
       
   787 		User::Leave(KErrArgument);
       
   788 		}
       
   789 
       
   790 	CImSmtpMobilitySettings* mobilitySettings = CImSmtpMobilitySettings::NewLC();
       
   791 
       
   792 	CImSmtpSettings* smtpSettings = new(ELeave) CImSmtpSettings;
       
   793 	CleanupStack::PushL(smtpSettings);
       
   794 	LoadSmtpSettingsL(aLinkedSmtpAccount, *smtpSettings);
       
   795 	mobilitySettings->SetSmtpSettings(smtpSettings);
       
   796 	CleanupStack::Pop(smtpSettings);
       
   797 
       
   798 	TSmtpMobilityAccount account = DoCreateSmtpMobilityAccountL(aAccountName, aIapList, *mobilitySettings, aSmtpAccount, aLinkedSmtpAccount.iSmtpAccountId, aReadOnly);
       
   799 
       
   800 	CleanupStack::PopAndDestroy(mobilitySettings);
       
   801 	return account;
       
   802 	}
       
   803 
       
   804 /**
       
   805 Gets a list of POP account IDs that exists in the current message store. 
       
   806 
       
   807 @param	aAccounts
       
   808 On return, an array of POP account IDs
       
   809 */ 
       
   810 EXPORT_C void CEmailAccounts::GetPopAccountsL(RArray<TPopAccount>& aAccounts)
       
   811 	{
       
   812 	aAccounts.Reset();
       
   813 	CRepository& popRepository = PopRepositoryL();
       
   814 	
       
   815 	// Get a list of pop accounts from CenRep
       
   816 	RArray<TUint32> popAccountIds;	
       
   817 	CleanupClosePushL(popAccountIds);	
       
   818 	TInt error = popRepository.FindL(KNullId, static_cast<TUint32>(EAccountMask), popAccountIds);
       
   819 	if (error != KErrNotFound)
       
   820 		{
       
   821 		User::LeaveIfError(error);
       
   822 		}
       
   823 
       
   824 	// Get a list of pop service entries from the message store
       
   825 	CMsvEntry* entry = SessionL().GetEntryL(KMsvRootIndexEntryId);
       
   826 	CleanupStack::PushL(entry);
       
   827 
       
   828 	CMsvEntrySelection* popServiceEntries = new (ELeave) CMsvEntrySelection;
       
   829 	CleanupStack::PushL(popServiceEntries);
       
   830 	GetEmailServicesL(*popServiceEntries, KUidMsgTypePOP3, *entry);
       
   831 	
       
   832 	// Append Ids to the array	
       
   833 	TPopAccount accountId;
       
   834 	TInt temp = 0;
       
   835 	TInt count = popAccountIds.Count();
       
   836 	for(TInt i = 1; i < count; ++i)
       
   837 		{
       
   838 		// create account Id
       
   839 		TUint32 settingId = popAccountIds[i];
       
   840 		User::LeaveIfError(popRepository.Get(settingId + EPOPAccountNameId, accountId.iPopAccountName));	
       
   841 		User::LeaveIfError(popRepository.Get(settingId + EPOPServiceId, temp));	
       
   842 		accountId.iPopService = static_cast<TMsvId>(temp);
       
   843 		User::LeaveIfError(popRepository.Get(settingId + EPOPRelatedSMTPServiceId, temp));	
       
   844 		accountId.iSmtpService = static_cast<TMsvId>(temp);
       
   845 		accountId.iPopAccountId = popAccountIds[i] >> KShiftAccountId;
       
   846 		
       
   847 		TInt index = popServiceEntries->Find(accountId.iPopService);		
       
   848 		if (index != KErrNotFound)
       
   849 			{
       
   850 			entry->SetEntryL(popServiceEntries->At(index));
       
   851 			if (entry->Entry().MtmData2() == accountId.iPopAccountId)
       
   852 				{
       
   853 				aAccounts.AppendL(accountId);
       
   854 				}				
       
   855 			}		
       
   856 		}
       
   857 		
       
   858 	CleanupStack::PopAndDestroy(3, &popAccountIds);				
       
   859 	}
       
   860 
       
   861 /**
       
   862 Gets a POP account ID for the specified service.
       
   863 
       
   864 @param	aServiceId
       
   865 Service ID
       
   866 
       
   867 @param	aAccount
       
   868 POP account ID
       
   869 */ 	
       
   870 EXPORT_C void CEmailAccounts::GetPopAccountL(TMsvId aServiceId, TPopAccount& aAccount)
       
   871 	{
       
   872 	CRepository& popRepository = PopRepositoryL();
       
   873 	TUint32 account = FindEmailAccountL(aServiceId, popRepository);	
       
   874 	aAccount.iPopAccountId = account >> KShiftAccountId;	
       
   875 	
       
   876 	User::LeaveIfError(popRepository.Get(account + EPOPAccountNameId, aAccount.iPopAccountName));	
       
   877  	TInt temp = 0;
       
   878  	User::LeaveIfError(popRepository.Get(account + EPOPServiceId, temp));	
       
   879  	aAccount.iPopService = static_cast<TMsvId>(temp);
       
   880  	User::LeaveIfError(popRepository.Get(account + EPOPRelatedSMTPServiceId, temp));	
       
   881  	aAccount.iSmtpService = static_cast<TMsvId>(temp);
       
   882 	}
       
   883 
       
   884 /**
       
   885 Deletes the specified POP account.
       
   886 
       
   887 @param	aAccount
       
   888 POP account ID
       
   889 */    
       
   890 EXPORT_C void CEmailAccounts::DeletePopAccountL(const TPopAccount& aAccount)
       
   891 	{
       
   892 	CRepository& popRepository = PopRepositoryL();
       
   893 	TUint32 popAccountId = aAccount.iPopAccountId << KShiftAccountId;
       
   894 
       
   895 	// Get POP service Id
       
   896 	TInt temp = 0;
       
   897 	User::LeaveIfError(popRepository.Get(popAccountId + EPOPServiceId, temp));	
       
   898 	TMsvId popService = static_cast<TMsvId>(temp);
       
   899 
       
   900 	// Delete service pop service entry
       
   901 	CMsvEntry* entry = CMsvEntry::NewL(SessionL(), KMsvRootIndexEntryId, TMsvSelectionOrdering(KMsvNoGrouping, EMsvSortByNone, ETrue));
       
   902 	CleanupStack::PushL(entry);
       
   903 	DeleteServiceEntryL(*entry, popService);		
       
   904 	CleanupStack::PopAndDestroy(entry);		
       
   905 
       
   906 	// Delete settings in CenRep
       
   907 	User::LeaveIfError(popRepository.StartTransaction(CRepository::EReadWriteTransaction));						
       
   908 	popRepository.CleanupRollbackTransactionPushL();
       
   909 	DeletePopSettings(popAccountId, popRepository);
       
   910 	DeleteIAPPrefSettingsL(popAccountId, popRepository);
       
   911 	CleanupStack::Pop();
       
   912 	TUint32 errorId = 0;
       
   913 	User::LeaveIfError(popRepository.CommitTransaction(errorId));
       
   914 	}
       
   915 
       
   916 /**
       
   917 Gets a list of IMAP account IDs that exists in the current message store.
       
   918 
       
   919 @param	aAccounts
       
   920 On return, an array of IMAP account IDs
       
   921 */    			
       
   922 EXPORT_C void CEmailAccounts::GetImapAccountsL(RArray<TImapAccount>& aAccounts)
       
   923 	{
       
   924 	aAccounts.Reset();
       
   925 	CRepository& imapRepository = ImapRepositoryL();
       
   926 
       
   927 	// Get a list of imap accounts from CenRep
       
   928 	RArray<TUint32> imapAccountIds;
       
   929 	CleanupClosePushL(imapAccountIds);	
       
   930 	TInt error = imapRepository.FindL(KNullId, static_cast<TUint32>(EAccountMask), imapAccountIds);
       
   931 	if (error != KErrNotFound)
       
   932 		{
       
   933 		User::LeaveIfError(error);
       
   934 		}
       
   935 
       
   936 	// Get a list of imap service entries from the message store
       
   937 	CMsvEntry* entry = SessionL().GetEntryL(KMsvRootIndexEntryId);
       
   938 	CleanupStack::PushL(entry);
       
   939 
       
   940 	CMsvEntrySelection* imapServiceEntries = new (ELeave) CMsvEntrySelection;
       
   941 	CleanupStack::PushL(imapServiceEntries);
       
   942 	GetEmailServicesL(*imapServiceEntries, KUidMsgTypeIMAP4, *entry);
       
   943 		
       
   944 	// Append Ids to the array	
       
   945 	TImapAccount accountId;	
       
   946 	TInt temp = 0;
       
   947 	TInt count = imapAccountIds.Count();
       
   948 	for (TInt i = 1; i < count; ++i)
       
   949 		{
       
   950 		// create account Id
       
   951 		TUint32 settingId = imapAccountIds[i];
       
   952 		User::LeaveIfError(imapRepository.Get(settingId + EIMAPAccountNameId, accountId.iImapAccountName));	
       
   953  		User::LeaveIfError(imapRepository.Get(settingId + EIMAPServiceId, temp));	
       
   954  		accountId.iImapService = static_cast<TMsvId>(temp);
       
   955  		User::LeaveIfError(imapRepository.Get(settingId + EIMAPRelatedSMTPServiceId, temp));	
       
   956  		accountId.iSmtpService = static_cast<TMsvId>(temp);
       
   957 		accountId.iImapAccountId = imapAccountIds[i] >> KShiftAccountId;
       
   958 
       
   959 		TInt index = imapServiceEntries->Find(accountId.iImapService);		
       
   960 		if (index != KErrNotFound)
       
   961 			{
       
   962 			entry->SetEntryL(imapServiceEntries->At(index));
       
   963 			if (entry->Entry().MtmData2() == accountId.iImapAccountId)
       
   964 				{
       
   965 				aAccounts.AppendL(accountId);
       
   966 				}				
       
   967 			}		
       
   968 		}
       
   969 
       
   970 	CleanupStack::PopAndDestroy(3, &imapAccountIds);			
       
   971 	}
       
   972 
       
   973 void CEmailAccounts::GetEmailServicesL(CMsvEntrySelection& aServices, TUid aMTMType, CMsvEntry& aMsvEntry)
       
   974 	{
       
   975 	aServices.Reset();
       
   976 	TMsvSelectionOrdering order;
       
   977 	order.SetShowInvisibleEntries(ETrue);
       
   978 	aMsvEntry.SetSortTypeL(order);
       
   979 	
       
   980 	CMsvEntrySelection* services = aMsvEntry.ChildrenWithTypeL(KUidMsvServiceEntry);
       
   981 	CleanupStack::PushL(services);
       
   982 
       
   983 	TInt serviceCount = services->Count();
       
   984 	for (TInt ii=0; ii<serviceCount; ++ii)
       
   985 		{
       
   986 		aMsvEntry.SetEntryL(services->At(ii));
       
   987 		if (aMsvEntry.Entry().iMtm == aMTMType)
       
   988 			{
       
   989 			aServices.AppendL(aMsvEntry.Entry().Id());
       
   990 			}
       
   991 		}	
       
   992 
       
   993 	CleanupStack::PopAndDestroy(services);			
       
   994 	}
       
   995 
       
   996 /**
       
   997 Gets a IMAP account ID for the specified service.
       
   998 
       
   999 @param	aServiceId
       
  1000 Service ID
       
  1001 
       
  1002 @param	aAccount
       
  1003 IMAP account ID
       
  1004 */ 		
       
  1005 EXPORT_C void CEmailAccounts::GetImapAccountL(TMsvId aServiceId, TImapAccount& aAccount)
       
  1006 	{
       
  1007 	CRepository& imapRepository = ImapRepositoryL();
       
  1008 	TUint32 account = FindEmailAccountL(aServiceId, imapRepository);	
       
  1009 	aAccount.iImapAccountId = account >> KShiftAccountId;	
       
  1010 	
       
  1011 	User::LeaveIfError(imapRepository.Get(account + EIMAPAccountNameId, aAccount.iImapAccountName));	
       
  1012  	TInt temp = 0;
       
  1013  	User::LeaveIfError(imapRepository.Get(account + EIMAPServiceId, temp));	
       
  1014  	aAccount.iImapService = static_cast<TMsvId>(temp);
       
  1015  	User::LeaveIfError(imapRepository.Get(account + EIMAPRelatedSMTPServiceId, temp));	
       
  1016  	aAccount.iSmtpService = static_cast<TMsvId>(temp);		
       
  1017 	}
       
  1018 	
       
  1019 /**
       
  1020 Deletes the specified IMAP account.
       
  1021 
       
  1022 @param	aAccount
       
  1023 IMAP account ID
       
  1024 */
       
  1025 EXPORT_C void CEmailAccounts::DeleteImapAccountL(const TImapAccount& aAccount)
       
  1026 	{
       
  1027 	TUint32 imapAccountId = aAccount.iImapAccountId << KShiftAccountId;
       
  1028 	CRepository& imapRepository = ImapRepositoryL();
       
  1029 
       
  1030 	// Get IMAP service Id
       
  1031 	TInt temp = 0;
       
  1032 	User::LeaveIfError(imapRepository.Get(imapAccountId + EIMAPServiceId, temp));	
       
  1033 	TMsvId imapService = static_cast<TMsvId>(temp);
       
  1034 				
       
  1035 	// Delete service entries
       
  1036 	CMsvEntry* entry = CMsvEntry::NewL(SessionL(), KMsvRootIndexEntryId, TMsvSelectionOrdering(KMsvNoGrouping, EMsvSortByNone, ETrue));
       
  1037 	CleanupStack::PushL(entry);
       
  1038 	DeleteServiceEntryL(*entry, imapService);		
       
  1039 	CleanupStack::PopAndDestroy(entry);	
       
  1040 
       
  1041 	// Delete settings in CenRep
       
  1042 	User::LeaveIfError(imapRepository.StartTransaction(CRepository::EReadWriteTransaction));						
       
  1043 	imapRepository.CleanupRollbackTransactionPushL();
       
  1044 	DeleteImapSettings(imapAccountId, imapRepository);
       
  1045 	DeleteIAPPrefSettingsL(imapAccountId, imapRepository);
       
  1046 	DeleteImapSyncDownloadRulesL(imapAccountId, imapRepository);
       
  1047 	CleanupStack::Pop();
       
  1048 	TUint32 errorId = 0;	
       
  1049 	User::LeaveIfError(imapRepository.CommitTransaction(errorId));
       
  1050 	}
       
  1051 
       
  1052 /**
       
  1053 Gets a list of SMTP account IDs that exists in the current message store.
       
  1054 
       
  1055 @param	aAccounts
       
  1056 On return, an array of SMTP account IDs
       
  1057 */    			
       
  1058 EXPORT_C void CEmailAccounts::GetSmtpAccountsL(RArray<TSmtpAccount>& aAccounts)
       
  1059 	{
       
  1060 	aAccounts.Reset();
       
  1061 	CRepository& smtpRepository = SmtpRepositoryL();
       
  1062 	
       
  1063 	// Get a list of smtp accounts from CenRep
       
  1064 	RArray<TUint32> smtpAccountIds;
       
  1065 	CleanupClosePushL(smtpAccountIds);	
       
  1066 	TInt error = smtpRepository.FindL(KNullId, static_cast<TUint32>(EAccountMask), smtpAccountIds);
       
  1067 	if (error != KErrNotFound)
       
  1068 		{
       
  1069 		User::LeaveIfError(error);
       
  1070 		}
       
  1071 
       
  1072 	// Get a list of smtp service entries from the message store
       
  1073 	CMsvEntry* entry = SessionL().GetEntryL(KMsvRootIndexEntryId);
       
  1074 	CleanupStack::PushL(entry);
       
  1075 
       
  1076 	CMsvEntrySelection* smtpServiceEntries = new (ELeave) CMsvEntrySelection;
       
  1077 	CleanupStack::PushL(smtpServiceEntries);
       
  1078 	GetEmailServicesL(*smtpServiceEntries, KUidMsgTypeSMTP, *entry);
       
  1079 		
       
  1080 	// Append Ids to the array	
       
  1081 	TSmtpAccount accountId;	
       
  1082 	TInt temp = 0;
       
  1083 	TInt count = smtpAccountIds.Count();
       
  1084 	for (TInt i = 1; i < count; ++i)
       
  1085 		{
       
  1086 		TUint32 settingId = smtpAccountIds[i];
       
  1087 
       
  1088 		User::LeaveIfError(smtpRepository.Get(settingId + ESMTPAccountNameId, accountId.iSmtpAccountName));	
       
  1089  		User::LeaveIfError(smtpRepository.Get(settingId + ESMTPServiceId, temp));	
       
  1090  		accountId.iSmtpService = static_cast<TMsvId>(temp);
       
  1091  		User::LeaveIfError(smtpRepository.Get(settingId + ESMTPRelatedServiceId, temp));	
       
  1092  		accountId.iRelatedService = static_cast<TMsvId>(temp);
       
  1093 		accountId.iSmtpAccountId = smtpAccountIds[i] >> KShiftAccountId;
       
  1094 
       
  1095 		TInt index = smtpServiceEntries->Find(accountId.iSmtpService);		
       
  1096 		if (index != KErrNotFound)
       
  1097 			{
       
  1098 			entry->SetEntryL(smtpServiceEntries->At(index));
       
  1099 			if (entry->Entry().MtmData2() == accountId.iSmtpAccountId)
       
  1100 				{
       
  1101 				aAccounts.AppendL(accountId);
       
  1102 				}				
       
  1103 			}		
       
  1104 		}
       
  1105 
       
  1106 	CleanupStack::PopAndDestroy(3, &smtpAccountIds);				
       
  1107 	}
       
  1108 
       
  1109 /**
       
  1110 Get a list of SMTP mobility accounts for a given SMTP account
       
  1111 
       
  1112 @param aSmtpAccount SMTP account
       
  1113 @param aAccounts Stores the list of SMTP mobility accounts
       
  1114 
       
  1115 @publishedPartner
       
  1116 */
       
  1117 EXPORT_C void CEmailAccounts::GetSmtpMobilityAccountsL(const TSmtpAccount& aSmtpAccount, RArray<TSmtpMobilityAccount>& aAccounts)
       
  1118 	{
       
  1119 	aAccounts.Reset();
       
  1120 
       
  1121 	CRepository& repository = SmtpRepositoryL();
       
  1122 	TUint32 smtpAccRepId = aSmtpAccount.iSmtpAccountId << KShiftAccountId;
       
  1123 
       
  1124 	TInt numEntries = 0;
       
  1125 	TInt err = repository.Get(smtpAccRepId + ESMTPMobilityAccountListNumEntriesId, numEntries);	
       
  1126 	if (err == KErrNone)
       
  1127 		{
       
  1128 		TSmtpMobilityAccount mobAccount;
       
  1129 		TUint32 mobAccRepId = 0;
       
  1130 		TInt tempInt = 0;
       
  1131 		TUint32 offset = 0;
       
  1132 
       
  1133 		for (TInt entryLoop = 0; entryLoop < numEntries; ++entryLoop)
       
  1134 			{
       
  1135 			offset = smtpAccRepId + (entryLoop * ESMTPMobilityAccountListOffset);
       
  1136 
       
  1137 			User::LeaveIfError(repository.Get(offset + ESMTPMobilityAccountListAccountId, tempInt));
       
  1138 			mobAccRepId = tempInt;
       
  1139 
       
  1140 			User::LeaveIfError(repository.Get(mobAccRepId + ESMTPAccountNameId, mobAccount.iName));
       
  1141 			User::LeaveIfError(repository.Get(mobAccRepId + ESMTPParentAccountId, tempInt));
       
  1142 			mobAccount.iParentSmtpAccountId = tempInt;
       
  1143 			User::LeaveIfError(repository.Get(mobAccRepId + ESMTPLinkedAccountId, tempInt));
       
  1144 			mobAccount.iLinkedSmtpAccountId = tempInt;
       
  1145 			mobAccount.iAccountId = mobAccRepId >> KShiftAccountId;
       
  1146 			aAccounts.AppendL(mobAccount);
       
  1147 			}
       
  1148 		}
       
  1149 	else if (err != KErrNotFound)
       
  1150 		{
       
  1151 		User::Leave(err);
       
  1152 		}
       
  1153 	}
       
  1154 
       
  1155 /**
       
  1156 Get a list of all SMTP mobility accounts in the repository
       
  1157 
       
  1158 @param aAccounts Stores the list of SMTP mobility accounts
       
  1159 
       
  1160 @internalTechnology
       
  1161 */
       
  1162 EXPORT_C void CEmailAccounts::GetSmtpMobilityAccountsL(RArray<TSmtpMobilityAccount>& aAccounts)
       
  1163 	{
       
  1164 	aAccounts.Reset();
       
  1165 
       
  1166 	CRepository& repository = SmtpRepositoryL();
       
  1167 
       
  1168 	RArray<TUint32> smtpAccountIds;
       
  1169 	CleanupClosePushL(smtpAccountIds);	
       
  1170 	TInt error = repository.FindL(KNullId, static_cast<TUint32>(EAccountMask), smtpAccountIds);
       
  1171 	if (error != KErrNotFound)
       
  1172 		{
       
  1173 		User::LeaveIfError(error);
       
  1174 		}
       
  1175 
       
  1176 	TSmtpMobilityAccount mobAccount;
       
  1177 	TInt tempInt;
       
  1178 	for (TInt count = 1; count < smtpAccountIds.Count(); ++count)
       
  1179 		{
       
  1180 		if (IsSmtpMobilityAccount(repository, smtpAccountIds[count]))
       
  1181 			{
       
  1182 			User::LeaveIfError(repository.Get(smtpAccountIds[count] + ESMTPAccountNameId, mobAccount.iName));
       
  1183 			User::LeaveIfError(repository.Get(smtpAccountIds[count] + ESMTPParentAccountId, tempInt));
       
  1184 			mobAccount.iParentSmtpAccountId = tempInt;
       
  1185 			User::LeaveIfError(repository.Get(smtpAccountIds[count] + ESMTPLinkedAccountId, tempInt));
       
  1186 			mobAccount.iLinkedSmtpAccountId = tempInt;
       
  1187 			mobAccount.iAccountId = smtpAccountIds[count] >> KShiftAccountId;
       
  1188 			aAccounts.AppendL(mobAccount);
       
  1189 			}
       
  1190 		}
       
  1191 
       
  1192 	CleanupStack::PopAndDestroy(&smtpAccountIds);
       
  1193 	}
       
  1194 
       
  1195 /**
       
  1196 Gets a SMTP account ID for the specified service.
       
  1197 
       
  1198 @param	aServiceId
       
  1199 Service ID
       
  1200 
       
  1201 @param	aAccount
       
  1202 SMTP account ID
       
  1203 */ 			
       
  1204 EXPORT_C void CEmailAccounts::GetSmtpAccountL(TMsvId aServiceId, TSmtpAccount& aAccount)
       
  1205 	{
       
  1206 	CRepository& smtpRepository = SmtpRepositoryL();	
       
  1207 	TUint32 account = FindEmailAccountL(aServiceId, smtpRepository);	
       
  1208 	aAccount.iSmtpAccountId = account >> KShiftAccountId;	
       
  1209 	
       
  1210 	User::LeaveIfError(smtpRepository.Get(account + ESMTPAccountNameId, aAccount.iSmtpAccountName));	
       
  1211  	TInt temp = 0;
       
  1212  	User::LeaveIfError(smtpRepository.Get(account + ESMTPServiceId, temp));	
       
  1213  	aAccount.iSmtpService = static_cast<TMsvId>(temp);
       
  1214  	User::LeaveIfError(smtpRepository.Get(account + ESMTPRelatedServiceId, temp));	
       
  1215  	aAccount.iRelatedService = static_cast<TMsvId>(temp);			
       
  1216 	}
       
  1217 
       
  1218 
       
  1219 /**
       
  1220 Gets a SMTP account for the specified account ID.
       
  1221 
       
  1222 @param aAccountId Account ID
       
  1223 @param aAccount Stores account details
       
  1224 
       
  1225 @leave KErrNotFound Account ID specifies a SMTP mobility account
       
  1226 
       
  1227 @publishedPartner
       
  1228 */
       
  1229 EXPORT_C void CEmailAccounts::GetSmtpAccountL(TSmtpAccountId aAccountId, TSmtpAccount& aAccount)
       
  1230 	{
       
  1231 	CRepository& repository = SmtpRepositoryL();	
       
  1232 	TUint32 smtpAccRepId = aAccountId << KShiftAccountId;
       
  1233 
       
  1234 	if (IsSmtpMobilityAccount(repository, smtpAccRepId))
       
  1235 		{
       
  1236 		User::Leave(KErrNotFound);
       
  1237 		}
       
  1238 
       
  1239 	aAccount.iSmtpAccountId = aAccountId;
       
  1240 
       
  1241 	TInt tempInt = 0;
       
  1242 	User::LeaveIfError(repository.Get(smtpAccRepId + ESMTPAccountNameId, aAccount.iSmtpAccountName));
       
  1243  	User::LeaveIfError(repository.Get(smtpAccRepId + ESMTPServiceId, tempInt));
       
  1244  	aAccount.iSmtpService = static_cast<TMsvId>(tempInt);
       
  1245  	User::LeaveIfError(repository.Get(smtpAccRepId + ESMTPRelatedServiceId, tempInt));
       
  1246  	aAccount.iRelatedService = static_cast<TMsvId>(tempInt);
       
  1247 	}
       
  1248 
       
  1249 /**
       
  1250 Deletes the specified SMTP account.
       
  1251 
       
  1252 @param	aAccount
       
  1253 SMTP account ID
       
  1254 
       
  1255 @leave KErrNotFound Account specified is a SMTP mobility account
       
  1256 */
       
  1257 EXPORT_C void CEmailAccounts::DeleteSmtpAccountL(const TSmtpAccount& aAccount)
       
  1258 	{
       
  1259 	TUint32 smtpAccountId = aAccount.iSmtpAccountId << KShiftAccountId;
       
  1260 	CRepository& smtpRepository = SmtpRepositoryL();	
       
  1261 
       
  1262 	if (IsSmtpMobilityAccount(smtpRepository, smtpAccountId))
       
  1263 		{
       
  1264 		User::Leave(KErrNotFound);
       
  1265 		}
       
  1266 	
       
  1267 	// Get SMTP service Id
       
  1268 	TInt temp = 0;
       
  1269 	User::LeaveIfError(smtpRepository.Get(smtpAccountId + ESMTPServiceId, temp));	
       
  1270 	TMsvId smtpService = static_cast<TMsvId>(temp);
       
  1271 	
       
  1272 	// Check if it is the default account that is being deleted
       
  1273 	TBool isDefault = EFalse;
       
  1274 	TInt defaultAccount;
       
  1275 	TInt error = smtpRepository.Get(KDefaultAccountId, defaultAccount);
       
  1276 	if (error == KErrNone && defaultAccount == aAccount.iSmtpAccountId)
       
  1277 		{
       
  1278 		isDefault = ETrue;
       
  1279 		}
       
  1280 	else if (error != KErrNotFound)
       
  1281 		{
       
  1282 		User::LeaveIfError(error);
       
  1283 		}
       
  1284 		
       
  1285 	// Delete service entries
       
  1286 	CMsvEntry* entry = CMsvEntry::NewL(SessionL(), KMsvRootIndexEntryId, TMsvSelectionOrdering(KMsvNoGrouping, EMsvSortByNone, ETrue));
       
  1287 	CleanupStack::PushL(entry);	
       
  1288 	DeleteServiceEntryL(*entry, smtpService);	
       
  1289 	CleanupStack::PopAndDestroy(entry);	
       
  1290 
       
  1291 	// Delete settings in CenRep		
       
  1292 	User::LeaveIfError(smtpRepository.StartTransaction(CRepository::EReadWriteTransaction));
       
  1293 	smtpRepository.CleanupRollbackTransactionPushL();
       
  1294 	DeleteSmtpSettings(smtpAccountId, smtpRepository);
       
  1295 	DeleteIAPPrefSettingsL(smtpAccountId, smtpRepository);
       
  1296 	DeleteAllOwnedMobilityAccountsL(smtpAccountId, smtpRepository);
       
  1297 	DeleteAllLinkedMobilityAccountsL(smtpAccountId, smtpRepository);
       
  1298 	if (isDefault)
       
  1299 		{		
       
  1300 		// Deleting default account, so remove the default account setting
       
  1301 		smtpRepository.Delete(KDefaultAccountId);	
       
  1302 		}
       
  1303 	CleanupStack::Pop();
       
  1304 	TUint32 errorId = 0;	
       
  1305 	User::LeaveIfError(smtpRepository.CommitTransaction(errorId));
       
  1306 	}
       
  1307 
       
  1308 /**
       
  1309 Deletes the default SMTP account information from repository.
       
  1310 */
       
  1311 EXPORT_C void CEmailAccounts::DeleteDefaultSmtpAccountL()
       
  1312 	{
       
  1313 	CRepository& smtpRepository = SmtpRepositoryL();	
       
  1314 	// Deleting default account info
       
  1315 	User::LeaveIfError(smtpRepository.Delete(KDefaultAccountId));	
       
  1316 	}
       
  1317 
       
  1318 /**
       
  1319 Deletes the specified SMTP bearer mobility account
       
  1320 
       
  1321 @param aAccount SMTP mobility account
       
  1322 
       
  1323 @leave KErrNotFound Specified account is not a SMTP mobility account
       
  1324 
       
  1325 @publishedPartner
       
  1326 */
       
  1327 EXPORT_C void CEmailAccounts::DeleteSmtpMobilityAccountL(const TSmtpMobilityAccount& aAccount)
       
  1328 	{
       
  1329 	TUint32 mobAccRepId = aAccount.iAccountId << KShiftAccountId;
       
  1330 	CRepository& repository = SmtpRepositoryL();	
       
  1331 
       
  1332 	if (!IsSmtpMobilityAccount(repository, mobAccRepId))
       
  1333 		{
       
  1334 		User::Leave(KErrNotFound);
       
  1335 		}
       
  1336 
       
  1337 	TInt tempInt = 0;
       
  1338 	User::LeaveIfError(repository.Get(mobAccRepId + ESMTPParentAccountId, tempInt));
       
  1339 	TUint32 smtpAccRepId = static_cast<TUint32>(tempInt) << KShiftAccountId;
       
  1340 
       
  1341 	User::LeaveIfError(repository.StartTransaction(CRepository::EReadWriteTransaction));
       
  1342 	repository.CleanupRollbackTransactionPushL();
       
  1343 
       
  1344 	DeleteSmtpMobilityAccountListEntryL(mobAccRepId, repository, smtpAccRepId);
       
  1345 
       
  1346 	DeleteSmtpMobilityAccountFromLinkedSmtpAccountL(mobAccRepId, repository);
       
  1347 
       
  1348 	DeleteSmtpSettings(mobAccRepId, repository);
       
  1349 
       
  1350 	CleanupStack::Pop();
       
  1351 	TUint32 keyInfo = 0;	
       
  1352 	User::LeaveIfError(repository.CommitTransaction(keyInfo));
       
  1353 	}
       
  1354 
       
  1355 /**
       
  1356 Populates the supplied setting objects with default values.
       
  1357 
       
  1358 @param	aPopSettings
       
  1359 POP Service Setting
       
  1360 
       
  1361 @param	aPopIap
       
  1362 POP IAP preferences
       
  1363 */			
       
  1364 EXPORT_C void CEmailAccounts::PopulateDefaultPopSettingsL(CImPop3Settings& aPopSettings,
       
  1365 	CImIAPPreferences& aPopIap)
       
  1366 	{
       
  1367 	DoLoadPopSettingsL(KInitialiseSettings, aPopSettings);		
       
  1368 	LoadIAPPrefSettingsL(KInitialiseSettings, aPopIap, PopRepositoryL());	
       
  1369 	}
       
  1370 
       
  1371 /**
       
  1372 Populates the supplied setting objects with default values.
       
  1373 
       
  1374 @param	aImapSettings
       
  1375 IMAP Service Setting
       
  1376 
       
  1377 @param	aImapIap
       
  1378 IMAP IAP preferences
       
  1379 */			
       
  1380 EXPORT_C void CEmailAccounts::PopulateDefaultImapSettingsL(CImImap4Settings& aImapSettings,
       
  1381 	CImIAPPreferences& aImapIap)
       
  1382 	{
       
  1383 	DoLoadImapSettingsL(KInitialiseSettings, aImapSettings);		
       
  1384 	LoadIAPPrefSettingsL(KInitialiseSettings, aImapIap, ImapRepositoryL());	
       
  1385 	}
       
  1386 
       
  1387 /**Populates the supplied setting objects with default values.
       
  1388 
       
  1389 @param	aSmtpSettings
       
  1390 SMTP Service Settings 
       
  1391 
       
  1392 @param	aSmtpIap
       
  1393 SMTP IAP preferences
       
  1394 */			
       
  1395 EXPORT_C void CEmailAccounts::PopulateDefaultSmtpSettingsL(CImSmtpSettings& aSmtpSettings,
       
  1396 	CImIAPPreferences& aSmtpIap)
       
  1397 	{
       
  1398 	DoLoadSmtpSettingsL(KInitialiseSettings, aSmtpSettings);			
       
  1399 	LoadIAPPrefSettingsL(KInitialiseSettings, aSmtpIap, SmtpRepositoryL());	
       
  1400 	}
       
  1401 
       
  1402 /**
       
  1403 Populates the supplied settings object with default values
       
  1404 
       
  1405 @param aSettings SMTP mobility account settings
       
  1406 
       
  1407 @publishedPartner
       
  1408 */			
       
  1409 EXPORT_C void CEmailAccounts::PopulateDefaultSmtpMobilitySettingsL(CImSmtpMobilitySettings& aSettings)
       
  1410 	{
       
  1411 	// Get default settings for a SMTP account and use these to populate the
       
  1412 	// settings from the SMTP mobility account.
       
  1413 	CImSmtpSettings* smtpSettings = new (ELeave) CImSmtpSettings();
       
  1414 	CleanupStack::PushL(smtpSettings);
       
  1415 	DoLoadSmtpSettingsL(KInitialiseSettings, *smtpSettings);
       
  1416 	CleanupStack::Pop(smtpSettings);
       
  1417 	aSettings.SetSmtpSettings(smtpSettings);
       
  1418 	}
       
  1419 
       
  1420 /**
       
  1421 Returns the default SMTP account.
       
  1422 
       
  1423 @param	aAccount
       
  1424 Default account ID
       
  1425 
       
  1426 @return KErrNone if default account found, KErrNotFound if not found
       
  1427 */			
       
  1428 EXPORT_C TInt CEmailAccounts::DefaultSmtpAccountL(TSmtpAccount& aAccount)
       
  1429 	{
       
  1430 	CRepository& smtpRepository = SmtpRepositoryL();	
       
  1431 	TInt error = smtpRepository.Get(KDefaultAccountId, aAccount.iSmtpAccountId);
       
  1432 	if (error == KErrNone)
       
  1433 		{
       
  1434 		// default account found, retrieve account details
       
  1435 		TUint32 cenrepAccountId = aAccount.iSmtpAccountId << KShiftAccountId;
       
  1436 		User::LeaveIfError(smtpRepository.Get(cenrepAccountId + ESMTPAccountNameId, aAccount.iSmtpAccountName));	
       
  1437 	 	TInt temp = 0;
       
  1438 	 	User::LeaveIfError(smtpRepository.Get(cenrepAccountId + ESMTPServiceId, temp));	
       
  1439 	 	aAccount.iSmtpService = static_cast<TMsvId>(temp);
       
  1440 	 	User::LeaveIfError(smtpRepository.Get(cenrepAccountId + ESMTPRelatedServiceId, temp));	
       
  1441 	 	aAccount.iRelatedService = static_cast<TMsvId>(temp);
       
  1442 		}
       
  1443 	else if (error != KErrNotFound)
       
  1444 		{		
       
  1445 		User::Leave(error);
       
  1446 		}				
       
  1447 	return error;		
       
  1448 	}
       
  1449 	
       
  1450 /**
       
  1451 Sets the default smtp account.
       
  1452 
       
  1453 @param	aAccount
       
  1454 Account ID to set as default 
       
  1455 */				
       
  1456 EXPORT_C void CEmailAccounts::SetDefaultSmtpAccountL(const TSmtpAccount& aAccount)
       
  1457 	{
       
  1458 	CRepository& smtpRepository = SmtpRepositoryL();	
       
  1459 
       
  1460 	TUint32 smtpAccountId = aAccount.iSmtpAccountId << KShiftAccountId;
       
  1461 
       
  1462 	TInt temp = 0;
       
  1463 
       
  1464 	// Check the account actually exists
       
  1465 	User::LeaveIfError(smtpRepository.Get(smtpAccountId + ESMTPServiceId, temp));	
       
  1466 
       
  1467 	if (IsSmtpMobilityAccount(smtpRepository, smtpAccountId))
       
  1468 		{
       
  1469 		User::Leave(KErrNotFound);
       
  1470 		}
       
  1471 
       
  1472 	User::LeaveIfError(smtpRepository.Set(KDefaultAccountId, aAccount.iSmtpAccountId));
       
  1473 	}
       
  1474 
       
  1475 /**
       
  1476 Loads POP service settings for the specified account. 
       
  1477 
       
  1478 @param	aAccount
       
  1479 POP account ID
       
  1480 
       
  1481 @param	aPopSettings
       
  1482 POP service settings
       
  1483 */	
       
  1484 EXPORT_C void CEmailAccounts::LoadPopSettingsL(const TPopAccount& aAccount, CImPop3Settings& aPopSettings)
       
  1485 	{
       
  1486 	TUint32 accountId = aAccount.iPopAccountId << KShiftAccountId;	
       
  1487 	DoLoadPopSettingsL(accountId, aPopSettings);	
       
  1488 	}
       
  1489 
       
  1490 /**
       
  1491 Loads POP IAP settings for the specified account. 
       
  1492 
       
  1493 @param	aAccount
       
  1494 POP account ID
       
  1495 
       
  1496 @param	aIAP
       
  1497 POP IAP Pref settings
       
  1498 */		
       
  1499 EXPORT_C void CEmailAccounts::LoadPopIapSettingsL(const TPopAccount& aAccount, CImIAPPreferences& aIAP)
       
  1500 	{
       
  1501 	TUint32 accountId = aAccount.iPopAccountId << KShiftAccountId;		
       
  1502 	LoadIAPPrefSettingsL(accountId, aIAP, PopRepositoryL());
       
  1503 	}
       
  1504 
       
  1505 /**
       
  1506 Loads IMAP service settings for the specified account. 
       
  1507 
       
  1508 @param	aAccount
       
  1509 IMAP account ID
       
  1510 
       
  1511 @param	aImapSettings
       
  1512 IMAP service settings
       
  1513 */	
       
  1514 EXPORT_C void CEmailAccounts::LoadImapSettingsL(const TImapAccount& aAccount, CImImap4Settings& aImapSettings)
       
  1515 	{	
       
  1516 	TUint32 accountId = aAccount.iImapAccountId << KShiftAccountId;	
       
  1517 	DoLoadImapSettingsL(accountId, aImapSettings);		
       
  1518 	}
       
  1519 
       
  1520 /**
       
  1521 Loads IMAP synchronisation download rules
       
  1522 
       
  1523 @param aAccount IMAP account ID
       
  1524 @param aRules Synchronisation download rules
       
  1525 
       
  1526 @publishedPartner
       
  1527 */
       
  1528 EXPORT_C void CEmailAccounts::LoadImapSyncDownloadRulesL(const TImapAccount& aAccount, CImapSyncDownloadRules& aRules)
       
  1529 	{
       
  1530 	aRules.Reset();
       
  1531 
       
  1532 	TUint32 accountId = aAccount.iImapAccountId << KShiftAccountId;
       
  1533 
       
  1534 	CRepository& imapRepository = ImapRepositoryL();
       
  1535 
       
  1536 	TInt version;
       
  1537 	
       
  1538 	if (imapRepository.Get(accountId + EIMAPSyncDownloadRulesVersionId, version) != KErrNone)
       
  1539 		{
       
  1540 		return;
       
  1541 		}
       
  1542 
       
  1543 	TInt numRules;
       
  1544 	User::LeaveIfError(imapRepository.Get(accountId + EIMAPNumInboxSyncDownloadRulesId, numRules));
       
  1545 
       
  1546 	TInt ruleOffset;
       
  1547 	TUint32 bearerTypes;
       
  1548 	TImImap4GetPartialMailInfo mailInfo;
       
  1549 	TInt tempInt;
       
  1550 
       
  1551 	for (TInt rule = 0; rule < numRules; ++rule)
       
  1552 		{
       
  1553 		ruleOffset = accountId + (rule * EIMAPSyncDownloadRulesOffset);
       
  1554 
       
  1555 		User::LeaveIfError(imapRepository.Get(ruleOffset + EIMAPInboxSyncDownloadRulesBearerTypesId, tempInt));
       
  1556 		bearerTypes = tempInt;
       
  1557 
       
  1558 		User::LeaveIfError(imapRepository.Get(ruleOffset + EIMAPInboxSyncDownloadRulesMailOptionsId, tempInt));
       
  1559 		mailInfo.iPartialMailOptions = static_cast<TImImap4PartialMailOptions>(tempInt);
       
  1560 
       
  1561 		User::LeaveIfError(imapRepository.Get(ruleOffset + EIMAPInboxSyncDownloadRulesTotalSizeLimitId, tempInt));
       
  1562 		mailInfo.iTotalSizeLimit = tempInt;
       
  1563 
       
  1564 		User::LeaveIfError(imapRepository.Get(ruleOffset + EIMAPInboxSyncDownloadRulesBodyTextSizeLimitId, tempInt));
       
  1565 		mailInfo.iBodyTextSizeLimit = tempInt;
       
  1566 
       
  1567 		User::LeaveIfError(imapRepository.Get(ruleOffset + EIMAPInboxSyncDownloadRulesAttachmentSizeLimitId, tempInt));
       
  1568 		mailInfo.iAttachmentSizeLimit = tempInt;
       
  1569 
       
  1570 		User::LeaveIfError(imapRepository.Get(ruleOffset + EIMAPInboxSyncDownloadRulesGetMailBodyPartsId, tempInt));
       
  1571 		mailInfo.iGetMailBodyParts = static_cast<TImap4GetMailOptions>(tempInt);
       
  1572 
       
  1573 		User::LeaveIfError(imapRepository.Get(ruleOffset + EIMAPInboxSyncDownloadRulesMaxEmailSizeId, tempInt));
       
  1574 		mailInfo.iMaxEmailSize = tempInt;
       
  1575 
       
  1576 		aRules.AddRuleL(CImapSyncDownloadRules::EInboxRulesType, rule, bearerTypes, mailInfo);
       
  1577 		}
       
  1578 
       
  1579 	User::LeaveIfError(imapRepository.Get(accountId + EIMAPNumFolderSyncDownloadRulesId, numRules));
       
  1580 
       
  1581 	for (TInt rule = 0; rule < numRules; ++rule)
       
  1582 		{
       
  1583 		ruleOffset = accountId + (rule * EIMAPSyncDownloadRulesOffset);
       
  1584 
       
  1585 		User::LeaveIfError(imapRepository.Get(ruleOffset + EIMAPFolderSyncDownloadRulesBearerTypesId, tempInt));
       
  1586 		bearerTypes = tempInt;
       
  1587 
       
  1588 		User::LeaveIfError(imapRepository.Get(ruleOffset + EIMAPFolderSyncDownloadRulesMailOptionsId, tempInt));
       
  1589 		mailInfo.iPartialMailOptions = static_cast<TImImap4PartialMailOptions>(tempInt);
       
  1590 
       
  1591 		User::LeaveIfError(imapRepository.Get(ruleOffset + EIMAPFolderSyncDownloadRulesTotalSizeLimitId, tempInt));
       
  1592 		mailInfo.iTotalSizeLimit = tempInt;
       
  1593 
       
  1594 		User::LeaveIfError(imapRepository.Get(ruleOffset + EIMAPFolderSyncDownloadRulesBodyTextSizeLimitId, tempInt));
       
  1595 		mailInfo.iBodyTextSizeLimit = tempInt;
       
  1596 
       
  1597 		User::LeaveIfError(imapRepository.Get(ruleOffset + EIMAPFolderSyncDownloadRulesAttachmentSizeLimitId, tempInt));
       
  1598 		mailInfo.iAttachmentSizeLimit = tempInt;
       
  1599 
       
  1600 		User::LeaveIfError(imapRepository.Get(ruleOffset + EIMAPFolderSyncDownloadRulesGetMailBodyPartsId, tempInt));
       
  1601 		mailInfo.iGetMailBodyParts = static_cast<TImap4GetMailOptions>(tempInt);
       
  1602 
       
  1603 		User::LeaveIfError(imapRepository.Get(ruleOffset + EIMAPFolderSyncDownloadRulesMaxEmailSizeId, tempInt));
       
  1604 		mailInfo.iMaxEmailSize = tempInt;
       
  1605 
       
  1606 		aRules.AddRuleL(CImapSyncDownloadRules::EFolderRulesType, rule, bearerTypes, mailInfo);
       
  1607 		}
       
  1608 	}
       
  1609 
       
  1610 /**
       
  1611 Loads IMAP IAP settings for the specified account. 
       
  1612 
       
  1613 @param	aAccount
       
  1614 IMAP account ID
       
  1615 
       
  1616 @param	aIAP
       
  1617 IMAP IAP Pref settings
       
  1618 */			
       
  1619 EXPORT_C void CEmailAccounts::LoadImapIapSettingsL(const TImapAccount& aAccount, CImIAPPreferences& aIAP)
       
  1620 	{	
       
  1621 	TUint32 accountId = aAccount.iImapAccountId << KShiftAccountId;		
       
  1622 	LoadIAPPrefSettingsL(accountId, aIAP, ImapRepositoryL());	
       
  1623 	}
       
  1624 
       
  1625 /**
       
  1626 Loads IMAP transport buffer sizes
       
  1627 
       
  1628 @param aBufferSizes Transport buffer sizes
       
  1629 
       
  1630 @publishedPartner
       
  1631 */
       
  1632 EXPORT_C void CEmailAccounts::LoadImapTransportBufferSizesL(CImapTransportBufferSizes& aBufferSizes)
       
  1633 	{
       
  1634 	aBufferSizes.Reset();
       
  1635 
       
  1636 	CRepository& imapRepository = ImapRepositoryL();
       
  1637 
       
  1638 	TInt version;
       
  1639 
       
  1640 	if (imapRepository.Get(EIMAPTransportBufferSizesVersionId, version) != KErrNone)
       
  1641 		{
       
  1642 		return;
       
  1643 		}
       
  1644 
       
  1645 	TInt numEntries;
       
  1646 	User::LeaveIfError(imapRepository.Get(EIMAPNumTransportBufferSizesId, numEntries));
       
  1647 
       
  1648 	TInt entryOffset;
       
  1649 	TUint32 bearerTypes;
       
  1650 	TInt fetchRequestSize;
       
  1651 	TInt maxOutstandingFetchResponses;
       
  1652 	TInt tempInt;
       
  1653 
       
  1654 	for (TInt entry = 0; entry < numEntries; ++entry)
       
  1655 		{
       
  1656 		entryOffset = entry * EIMAPTransportBufferSizesOffset;
       
  1657 
       
  1658 		User::LeaveIfError(imapRepository.Get(entryOffset + EIMAPTransportBufferSizesBearerTypesId, tempInt));
       
  1659 		bearerTypes = tempInt;
       
  1660 
       
  1661 		User::LeaveIfError(imapRepository.Get(entryOffset + EIMAPTransportBufferSizesFetchRequestSizeId, fetchRequestSize));
       
  1662 
       
  1663 		User::LeaveIfError(imapRepository.Get(entryOffset + EIMAPTransportBufferSizesMaxOutstandingFetchResponsesId, maxOutstandingFetchResponses));
       
  1664 
       
  1665 		aBufferSizes.AddSizesL(entry, bearerTypes, fetchRequestSize, maxOutstandingFetchResponses);
       
  1666 		}
       
  1667 	}
       
  1668 
       
  1669 /**
       
  1670 Loads SMTP service settings for the specified account. 
       
  1671 
       
  1672 @param	aAccount
       
  1673 SMTP account ID
       
  1674 
       
  1675 @param	aSmtpSettings
       
  1676 SMTP service settings
       
  1677 
       
  1678 @leave KErrNotFound Account specified is a SMTP mobility account
       
  1679 */	
       
  1680 EXPORT_C void CEmailAccounts::LoadSmtpSettingsL(const TSmtpAccount& aAccount, CImSmtpSettings& aSmtpSettings)
       
  1681 	{
       
  1682 	TUint32 accountId = aAccount.iSmtpAccountId << KShiftAccountId;
       
  1683 
       
  1684 	if (IsSmtpMobilityAccount(SmtpRepositoryL(), accountId))
       
  1685 		{
       
  1686 		User::Leave(KErrNotFound);
       
  1687 		}
       
  1688 
       
  1689 	DoLoadSmtpSettingsL(accountId, aSmtpSettings);			
       
  1690 	}
       
  1691 
       
  1692 /**
       
  1693 Loads SMTP IAP settings for the specified account. 
       
  1694 
       
  1695 @param	aAccount
       
  1696 SMTP account ID
       
  1697 
       
  1698 @param	aIAP
       
  1699 SMTP IAP Pref settings
       
  1700 */				
       
  1701 EXPORT_C void CEmailAccounts::LoadSmtpIapSettingsL(const TSmtpAccount& aAccount, CImIAPPreferences& aIAP)
       
  1702 	{
       
  1703 	TUint32 accountId = aAccount.iSmtpAccountId << KShiftAccountId;
       
  1704 	CRepository& smtpRepository = SmtpRepositoryL();
       
  1705 
       
  1706 	if (IsSmtpMobilityAccount(smtpRepository, accountId))
       
  1707 		{
       
  1708 		User::Leave(KErrNotFound);
       
  1709 		}
       
  1710 
       
  1711 	LoadIAPPrefSettingsL(accountId, aIAP, smtpRepository);			
       
  1712 	}
       
  1713 
       
  1714 /**
       
  1715 Loads SMTP mobility account settings
       
  1716 
       
  1717 @param aAccount SMTP mobility account
       
  1718 @param aSettings SMTP mobility account settings
       
  1719 
       
  1720 @leave KErrNotFound Specified account is not a SMTP mobility account
       
  1721 
       
  1722 @publishedPartner
       
  1723 */
       
  1724 EXPORT_C void CEmailAccounts::LoadSmtpMobilitySettingsL(const TSmtpMobilityAccount& aAccount, CImSmtpMobilitySettings& aSettings)
       
  1725 	{
       
  1726 	TUint32 mobAccRepId = aAccount.iAccountId << KShiftAccountId;
       
  1727 
       
  1728 	if (!IsSmtpMobilityAccount(SmtpRepositoryL(), mobAccRepId))
       
  1729 		{
       
  1730 		User::Leave(KErrNotFound);
       
  1731 		}
       
  1732 
       
  1733 	CImSmtpSettings* smtpSettings = new (ELeave) CImSmtpSettings();
       
  1734 	CleanupStack::PushL(smtpSettings);
       
  1735 	DoLoadSmtpSettingsL(mobAccRepId, *smtpSettings);
       
  1736 	CleanupStack::Pop(smtpSettings);
       
  1737 	aSettings.SetSmtpSettings(smtpSettings);
       
  1738 	}
       
  1739 
       
  1740 /**
       
  1741 Loads SMTP mobility account settings for a given SMTP account and IAP
       
  1742 
       
  1743 @param aSmtpAccountId SMTP account ID
       
  1744 @param aIap IAP
       
  1745 @param aSettings SMTP mobility account settings
       
  1746 
       
  1747 @return ETrue if account found, EFalse if not
       
  1748 
       
  1749 @internalComponent
       
  1750 */
       
  1751 EXPORT_C TBool CEmailAccounts::LoadSmtpMobilitySettingsL(TSmtpAccountId& aSmtpAccountId, TUint32 aIap, CImSmtpMobilitySettings& aSettings)
       
  1752 	{
       
  1753 	TUint32 smtpAccRepId = aSmtpAccountId << KShiftAccountId;
       
  1754 	CRepository& repository = SmtpRepositoryL();
       
  1755 
       
  1756 	RArrayIAP iapList;
       
  1757 	CleanupClosePushL(iapList);
       
  1758 	iapList.AppendL(aIap);
       
  1759 
       
  1760 	TInt numEntries = 0;
       
  1761 	TInt pos = KErrNotFound;
       
  1762 
       
  1763 	TInt err = 	repository.Get(smtpAccRepId + ESMTPMobilityAccountListNumEntriesId, numEntries);
       
  1764 	if (err == KErrNone)
       
  1765 		{
       
  1766 		pos = FindIapListEntryInSmtpMobilityAccountListL(iapList, repository, smtpAccRepId, numEntries, KNullSmtpMobilityAccountEntryPos);
       
  1767 		}
       
  1768 	else if (err != KErrNotFound)
       
  1769 		{
       
  1770 		User::Leave(err);
       
  1771 		}
       
  1772 
       
  1773 	CleanupStack::PopAndDestroy(&iapList);
       
  1774 
       
  1775 	if (pos != KErrNotFound)
       
  1776 		{
       
  1777 		TInt tempInt = 0;
       
  1778 		User::LeaveIfError(repository.Get(smtpAccRepId + (pos * ESMTPMobilityAccountListOffset) + ESMTPMobilityAccountListAccountId, tempInt));
       
  1779 		TUint32 mobAccRepId = tempInt;
       
  1780 
       
  1781 		CImSmtpSettings* smtpSettings = new (ELeave) CImSmtpSettings();
       
  1782 		CleanupStack::PushL(smtpSettings);
       
  1783 		DoLoadSmtpSettingsL(mobAccRepId, *smtpSettings);
       
  1784 		CleanupStack::Pop(smtpSettings);
       
  1785 		aSettings.SetSmtpSettings(smtpSettings);
       
  1786 
       
  1787 		return ETrue;
       
  1788 		}
       
  1789 
       
  1790 	return EFalse;
       
  1791 	}
       
  1792 
       
  1793 /**
       
  1794 Loads the IAP list associated with a specified SMTP mobility account
       
  1795 
       
  1796 @param aAccount SMTP mobility account
       
  1797 @param aIapList Stores the IAP list
       
  1798 
       
  1799 @publishedPartner
       
  1800 */
       
  1801 EXPORT_C void CEmailAccounts::LoadSmtpMobilityAccountIapListL(const TSmtpMobilityAccount& aAccount, RArrayIAP& aIapList)
       
  1802 	{
       
  1803 	aIapList.Reset();
       
  1804 
       
  1805 	TUint32 mobAccRepId = aAccount.iAccountId << KShiftAccountId;
       
  1806 	CRepository& repository = SmtpRepositoryL();
       
  1807 
       
  1808 	TUint32 smtpAccRepId = 0;
       
  1809 	TInt pos = 0;
       
  1810 	GetSmtpAccountInfoForMobilityAccountL(mobAccRepId, repository, smtpAccRepId, pos);
       
  1811 
       
  1812 	TUint32 offset = smtpAccRepId + (pos * ESMTPMobilityAccountListOffset);
       
  1813 
       
  1814 	TInt numIaps = 0;
       
  1815 	User::LeaveIfError(repository.Get(offset + ESMTPMobilityAccountListNumIapsId, numIaps));
       
  1816 
       
  1817 	TInt iap = 0;
       
  1818 	for (TInt iapLoop = 0; iapLoop < numIaps; ++iapLoop)
       
  1819 		{
       
  1820 		User::LeaveIfError(repository.Get(offset + ESMTPMobilityAccountListIapId + iapLoop, iap));
       
  1821 
       
  1822 		aIapList.AppendL(iap);
       
  1823 		}
       
  1824 	}
       
  1825 
       
  1826 /**
       
  1827 Saves POP service settings for the specified account. 
       
  1828 
       
  1829 @param	aAccount
       
  1830 POP account ID, if the account name is zero-length, the account name is not updated
       
  1831 
       
  1832 @param	aPopSettings
       
  1833 POP service settings
       
  1834 */	
       
  1835 EXPORT_C void CEmailAccounts::SavePopSettingsL(const TPopAccount& aAccount, const CImPop3Settings& aPopSettings)
       
  1836 	{
       
  1837 	TUint32 accountId = aAccount.iPopAccountId << KShiftAccountId;
       
  1838 	CRepository& popRepository = PopRepositoryL();	
       
  1839 	
       
  1840 	TInt temp = 0;
       
  1841 	User::LeaveIfError(popRepository.Get(accountId + EPOPServiceId, temp));
       
  1842 	TMsvId service = static_cast<TMsvId>(temp);		
       
  1843 
       
  1844 	// Save settings to CenRep
       
  1845 	User::LeaveIfError(popRepository.StartTransaction(CRepository::EReadWriteTransaction));		
       
  1846 	popRepository.CleanupRollbackTransactionPushL();
       
  1847 	if (aAccount.iPopAccountName.Length() > 0)
       
  1848 		{
       
  1849 		TBuf<KEmailAccountNameSize> accountName;
       
  1850 		User::LeaveIfError(popRepository.Get(accountId + EPOPAccountNameId, accountName));
       
  1851 		if(accountName.Compare(aAccount.iPopAccountName) != 0)
       
  1852 			{
       
  1853 			// Update account name
       
  1854 			popRepository.Set(accountId + EPOPAccountNameId, aAccount.iPopAccountName);
       
  1855 			SetEntryDetailsL(service, aAccount.iPopAccountName);			
       
  1856 			}
       
  1857 		}					
       
  1858 	DoSavePopSettingsL(accountId, aPopSettings);
       
  1859 	CleanupStack::Pop();
       
  1860 	TUint32 errorId = 0;	
       
  1861 	User::LeaveIfError(popRepository.CommitTransaction(errorId));
       
  1862 	}
       
  1863 
       
  1864 /**
       
  1865 Saves POP IAP settings for the specified account. 
       
  1866 
       
  1867 @param	aAccount
       
  1868 POP account ID, if the account name is zero-length, the account name is not updated
       
  1869 
       
  1870 @param	aIAP
       
  1871 POP IAP Pref settings
       
  1872 */			
       
  1873 EXPORT_C void CEmailAccounts::SavePopIapSettingsL(const TPopAccount& aAccount, const CImIAPPreferences& aIAP)
       
  1874 	{
       
  1875 	TUint32 accountId = aAccount.iPopAccountId << KShiftAccountId;
       
  1876 	CRepository& popRepository = PopRepositoryL();	
       
  1877 
       
  1878 	TInt temp = 0;
       
  1879 	User::LeaveIfError(popRepository.Get(accountId + EPOPServiceId, temp));
       
  1880 	TMsvId service = static_cast<TMsvId>(temp);		
       
  1881 
       
  1882 	User::LeaveIfError(popRepository.StartTransaction(CRepository::EReadWriteTransaction));		
       
  1883 	popRepository.CleanupRollbackTransactionPushL();
       
  1884 	if (aAccount.iPopAccountName.Length() > 0)
       
  1885 		{
       
  1886 		TBuf<KEmailAccountNameSize> accountName;
       
  1887 		User::LeaveIfError(popRepository.Get(accountId + EPOPAccountNameId, accountName));
       
  1888 		if(accountName.Compare(aAccount.iPopAccountName) != 0)
       
  1889 			{
       
  1890 			// Update account name
       
  1891 			popRepository.Set(accountId + EPOPAccountNameId, aAccount.iPopAccountName);
       
  1892 			SetEntryDetailsL(service, aAccount.iPopAccountName);			
       
  1893 			}
       
  1894 		}					
       
  1895 	SaveIAPPrefSettingsL(accountId, aIAP, popRepository);	
       
  1896 	CleanupStack::Pop();
       
  1897 	TUint32 errorId = 0;	
       
  1898 	User::LeaveIfError(popRepository.CommitTransaction(errorId));
       
  1899 	}
       
  1900 
       
  1901 /**
       
  1902 Saves IMAP service settings for the specified account. 
       
  1903 
       
  1904 @param	aAccount
       
  1905 IMAP account ID, if the account name is zero-length, the account name is not updated
       
  1906 
       
  1907 @param	aImapSettings
       
  1908 IMAP service settings
       
  1909 */	
       
  1910 EXPORT_C void CEmailAccounts::SaveImapSettingsL(const TImapAccount& aAccount, const CImImap4Settings& aImapSettings)
       
  1911 	{
       
  1912 	TUint32 accountId = aAccount.iImapAccountId << KShiftAccountId;
       
  1913 	CRepository& imapRepository = ImapRepositoryL();
       
  1914 
       
  1915 	TInt temp = 0;
       
  1916 	User::LeaveIfError(imapRepository.Get(accountId + EIMAPServiceId, temp));
       
  1917 	TMsvId service = static_cast<TMsvId>(temp);		
       
  1918 
       
  1919 	User::LeaveIfError(imapRepository.StartTransaction(CRepository::EReadWriteTransaction));
       
  1920 	imapRepository.CleanupRollbackTransactionPushL();
       
  1921 	if (aAccount.iImapAccountName.Length() > 0)
       
  1922 		{
       
  1923 		TBuf<KEmailAccountNameSize> accountName;
       
  1924 		User::LeaveIfError(imapRepository.Get(accountId + EIMAPAccountNameId, accountName));
       
  1925 		if(accountName.Compare(aAccount.iImapAccountName) != 0)
       
  1926 			{
       
  1927 			// Update account name
       
  1928 			imapRepository.Set(accountId + EIMAPAccountNameId, aAccount.iImapAccountName);
       
  1929 			SetEntryDetailsL(service, aAccount.iImapAccountName);
       
  1930 			}
       
  1931 		}			  							
       
  1932 	DoSaveImapSettingsL(accountId, aImapSettings);
       
  1933 	CleanupStack::Pop();
       
  1934 	TUint32 errorId = 0;	
       
  1935 	User::LeaveIfError(imapRepository.CommitTransaction(errorId));
       
  1936 	}
       
  1937 
       
  1938 /**
       
  1939 Saves IMAP synchronisation download rules
       
  1940 
       
  1941 @param aAccount IMAP account ID
       
  1942 @param aRules Synchronisation download rules
       
  1943 
       
  1944 @publishedPartner
       
  1945 */
       
  1946 EXPORT_C void CEmailAccounts::SaveImapSyncDownloadRulesL(const TImapAccount& aAccount, const CImapSyncDownloadRules& aRules)
       
  1947 	{
       
  1948 	TUint32 accountId = aAccount.iImapAccountId << KShiftAccountId;
       
  1949 	CRepository& imapRepository = ImapRepositoryL();
       
  1950 
       
  1951 	User::LeaveIfError(imapRepository.StartTransaction(CRepository::EReadWriteTransaction));
       
  1952 	imapRepository.CleanupRollbackTransactionPushL();
       
  1953 
       
  1954 	DoSaveImapSyncDownloadRulesL(accountId, aRules, imapRepository);
       
  1955 
       
  1956 	CleanupStack::Pop();
       
  1957 	TUint32 errorId = 0;	
       
  1958 	User::LeaveIfError(imapRepository.CommitTransaction(errorId));
       
  1959 	}
       
  1960 
       
  1961 /**
       
  1962 Saves IMAP IAP settings for the specified account. 
       
  1963 
       
  1964 @param	aAccount
       
  1965 IMAP account ID, if the account name is zero-length, the account name is not updated
       
  1966 
       
  1967 @param	aIAP
       
  1968 IMAP IAP Pref settings
       
  1969 */				
       
  1970 EXPORT_C void CEmailAccounts::SaveImapIapSettingsL(const TImapAccount& aAccount, const CImIAPPreferences& aIAP)
       
  1971 	{
       
  1972 	TUint32 accountId = aAccount.iImapAccountId << KShiftAccountId;
       
  1973 	CRepository& imapRepository = ImapRepositoryL();
       
  1974 
       
  1975 	TInt temp = 0;
       
  1976 	User::LeaveIfError(imapRepository.Get(accountId + EIMAPServiceId, temp));
       
  1977 	TMsvId service = static_cast<TMsvId>(temp);		
       
  1978 
       
  1979 	User::LeaveIfError(imapRepository.StartTransaction(CRepository::EReadWriteTransaction));
       
  1980 	imapRepository.CleanupRollbackTransactionPushL();
       
  1981 	if (aAccount.iImapAccountName.Length() > 0)
       
  1982 		{
       
  1983 		TBuf<KEmailAccountNameSize> accountName;
       
  1984 		User::LeaveIfError(imapRepository.Get(accountId + EIMAPAccountNameId, accountName));
       
  1985 		if(accountName.Compare(aAccount.iImapAccountName) != 0)
       
  1986 			{
       
  1987 			// Update account name
       
  1988 			imapRepository.Set(accountId + EIMAPAccountNameId, aAccount.iImapAccountName);
       
  1989 			SetEntryDetailsL(service, aAccount.iImapAccountName);
       
  1990 			}
       
  1991 		}										
       
  1992 	SaveIAPPrefSettingsL(accountId, aIAP, imapRepository);	
       
  1993 	CleanupStack::Pop();
       
  1994 	TUint32 errorId = 0;	
       
  1995 	User::LeaveIfError(imapRepository.CommitTransaction(errorId));		
       
  1996 	}
       
  1997 
       
  1998 /**
       
  1999 Saves IMAP transport buffer sizes
       
  2000 
       
  2001 @param aBufferSizes Transport buffer sizes
       
  2002 
       
  2003 @publishedPartner
       
  2004 */
       
  2005 EXPORT_C void CEmailAccounts::SaveImapTransportBufferSizesL(const CImapTransportBufferSizes& aBufferSizes)
       
  2006 	{
       
  2007 	CRepository& imapRepository = ImapRepositoryL();
       
  2008 
       
  2009 	User::LeaveIfError(imapRepository.StartTransaction(CRepository::EReadWriteTransaction));
       
  2010 	imapRepository.CleanupRollbackTransactionPushL();
       
  2011 
       
  2012 	imapRepository.Set(EIMAPTransportBufferSizesVersionId, KImapTransportBufferSizesVersion);
       
  2013 
       
  2014 	TInt entryOffset;
       
  2015 	TUint32 bearerTypes;
       
  2016 	TInt fetchRequestSize;
       
  2017 	TInt maxOutstandingFetchResponses;
       
  2018 	TInt entryLoop = 0;
       
  2019 
       
  2020 	while (entryLoop < KMaxImapTransportBufferSizes &&
       
  2021 	       aBufferSizes.GetSizesL(entryLoop, bearerTypes, fetchRequestSize, maxOutstandingFetchResponses))
       
  2022 		{
       
  2023 		entryOffset = entryLoop * EIMAPTransportBufferSizesOffset;
       
  2024 
       
  2025 		imapRepository.Set(entryOffset + EIMAPTransportBufferSizesBearerTypesId, static_cast<TInt>(bearerTypes));
       
  2026 		imapRepository.Set(entryOffset + EIMAPTransportBufferSizesFetchRequestSizeId, fetchRequestSize);
       
  2027 		imapRepository.Set(entryOffset + EIMAPTransportBufferSizesMaxOutstandingFetchResponsesId, maxOutstandingFetchResponses);	
       
  2028 
       
  2029 		++entryLoop;
       
  2030 		}
       
  2031 
       
  2032 	imapRepository.Set(EIMAPNumTransportBufferSizesId, entryLoop);
       
  2033 
       
  2034 	// Clear any old rules that are no longer required
       
  2035 	while (entryLoop < KMaxImapTransportBufferSizes)
       
  2036 		{
       
  2037 		entryOffset = entryLoop * EIMAPTransportBufferSizesOffset;
       
  2038 
       
  2039 		imapRepository.Delete(entryOffset + EIMAPTransportBufferSizesBearerTypesId);
       
  2040 		imapRepository.Delete(entryOffset + EIMAPTransportBufferSizesFetchRequestSizeId);
       
  2041 		imapRepository.Delete(entryOffset + EIMAPTransportBufferSizesMaxOutstandingFetchResponsesId);		
       
  2042 
       
  2043 		++entryLoop;
       
  2044 		}
       
  2045 
       
  2046 	CleanupStack::Pop();
       
  2047 	TUint32 errorId = 0;	
       
  2048 	User::LeaveIfError(imapRepository.CommitTransaction(errorId));
       
  2049 	}
       
  2050 
       
  2051 
       
  2052 /**
       
  2053 Saves SMTP service settings for the specified account. 
       
  2054 
       
  2055 @param	aAccount
       
  2056 SMTP account ID, if the account name is zero-length, the account name is not updated
       
  2057 
       
  2058 @param	aSmtpSettings
       
  2059 SMTP service settings
       
  2060 */	
       
  2061 EXPORT_C void CEmailAccounts::SaveSmtpSettingsL(const TSmtpAccount& aAccount, const CImSmtpSettings& aSmtpSettings)
       
  2062 	{
       
  2063 	TUint32 accountId = aAccount.iSmtpAccountId << KShiftAccountId;
       
  2064 	CRepository& smtpRepository = SmtpRepositoryL();	
       
  2065 
       
  2066 	TInt temp = 0;
       
  2067 	User::LeaveIfError(smtpRepository.Get(accountId + ESMTPServiceId, temp));
       
  2068 	TMsvId service = static_cast<TMsvId>(temp);		
       
  2069 
       
  2070 	if (IsSmtpMobilityAccount(smtpRepository, accountId))
       
  2071 		{
       
  2072 		User::Leave(KErrNotFound);
       
  2073 		}
       
  2074 
       
  2075 	User::LeaveIfError(smtpRepository.StartTransaction(CRepository::EReadWriteTransaction));	
       
  2076 	smtpRepository.CleanupRollbackTransactionPushL();
       
  2077 	if (aAccount.iSmtpAccountName.Length() > 0)
       
  2078 		{
       
  2079 		TBuf<KEmailAccountNameSize> accountName;
       
  2080 		User::LeaveIfError(smtpRepository.Get(accountId + ESMTPAccountNameId, accountName));
       
  2081 		if(accountName.Compare(aAccount.iSmtpAccountName) != 0)
       
  2082 			{
       
  2083 			// Update account name
       
  2084 			smtpRepository.Set(accountId + ESMTPAccountNameId, aAccount.iSmtpAccountName);
       
  2085 			SetEntryDetailsL(service, aAccount.iSmtpAccountName);
       
  2086 			}
       
  2087 		}									
       
  2088 	DoSaveSmtpSettingsL(accountId, aSmtpSettings);
       
  2089 	CleanupStack::Pop();
       
  2090 	TUint32 errorId = 0;	
       
  2091 	User::LeaveIfError(smtpRepository.CommitTransaction(errorId));	
       
  2092 	}
       
  2093 
       
  2094 /**
       
  2095 Saves SMTP IAP settings for the specified account. 
       
  2096 
       
  2097 @param	aAccount
       
  2098 SMTP account ID, if the account name is zero-length, the account name is not updated
       
  2099 
       
  2100 @param	aIAP
       
  2101 SMTP IAP Pref settings
       
  2102 */					
       
  2103 EXPORT_C void CEmailAccounts::SaveSmtpIapSettingsL(const TSmtpAccount& aAccount, const CImIAPPreferences& aIAP)
       
  2104 	{
       
  2105 	TUint32 accountId = aAccount.iSmtpAccountId << KShiftAccountId;
       
  2106 	CRepository& smtpRepository = SmtpRepositoryL();	
       
  2107 
       
  2108 	TInt temp = 0;
       
  2109 	User::LeaveIfError(smtpRepository.Get(accountId + ESMTPServiceId, temp));
       
  2110 	TMsvId service = static_cast<TMsvId>(temp);		
       
  2111 
       
  2112 	if (IsSmtpMobilityAccount(smtpRepository, accountId))
       
  2113 		{
       
  2114 		User::Leave(KErrNotFound);
       
  2115 		}
       
  2116 
       
  2117 	User::LeaveIfError(smtpRepository.StartTransaction(CRepository::EReadWriteTransaction));	
       
  2118 	smtpRepository.CleanupRollbackTransactionPushL();
       
  2119 	if (aAccount.iSmtpAccountName.Length() > 0)
       
  2120 		{
       
  2121 		TBuf<KEmailAccountNameSize> accountName;
       
  2122 		User::LeaveIfError(smtpRepository.Get(accountId + ESMTPAccountNameId, accountName));
       
  2123 		if(accountName.Compare(aAccount.iSmtpAccountName) != 0)
       
  2124 			{
       
  2125 			// Update account name
       
  2126 			smtpRepository.Set(accountId + ESMTPAccountNameId, aAccount.iSmtpAccountName);
       
  2127 			SetEntryDetailsL(service, aAccount.iSmtpAccountName);
       
  2128 			}
       
  2129 		}									
       
  2130 	SaveIAPPrefSettingsL(accountId, aIAP, smtpRepository);	
       
  2131 	CleanupStack::Pop();
       
  2132 	TUint32 errorId = 0;	
       
  2133 	User::LeaveIfError(smtpRepository.CommitTransaction(errorId));			
       
  2134 	}
       
  2135 
       
  2136 /**
       
  2137 Saves the settings for a SMTP mobility account
       
  2138 
       
  2139 @param aAccount SMTP mobility account
       
  2140 @param aSettings Settings to save
       
  2141 
       
  2142 @leave KErrNotFound Specified account is not a SMTP mobility account
       
  2143 @leave KErrNotSupported SMTP mobility account uses settings of a linked
       
  2144        SMTP account and changing the settings is not supported
       
  2145 
       
  2146 @publishedPartner
       
  2147 */	
       
  2148 EXPORT_C void CEmailAccounts::SaveSmtpMobilitySettingsL(const TSmtpMobilityAccount& aAccount, const CImSmtpMobilitySettings& aSettings)
       
  2149 	{
       
  2150 	TUint32 mobAccRepId = aAccount.iAccountId << KShiftAccountId;
       
  2151 	CRepository& repository = SmtpRepositoryL();	
       
  2152 
       
  2153 	if (!IsSmtpMobilityAccount(repository, mobAccRepId))
       
  2154 		{
       
  2155 		User::Leave(KErrNotFound);
       
  2156 		}
       
  2157 
       
  2158 	TInt tempInt = 0;
       
  2159 	User::LeaveIfError(repository.Get(mobAccRepId + ESMTPLinkedAccountId, tempInt));
       
  2160 	if (tempInt != KNullSmtpAccountId)
       
  2161 		{
       
  2162 		User::Leave(KErrNotSupported);
       
  2163 		}
       
  2164 
       
  2165 	User::LeaveIfError(repository.StartTransaction(CRepository::EReadWriteTransaction));
       
  2166 	repository.CleanupRollbackTransactionPushL();
       
  2167 	if (aAccount.iName.Length() > 0)
       
  2168 		{
       
  2169 		// Update account name
       
  2170 		repository.Set(mobAccRepId + ESMTPAccountNameId, aAccount.iName);
       
  2171 		}
       
  2172 
       
  2173 	DoSaveSmtpSettingsL(mobAccRepId, aSettings.SmtpSettings());
       
  2174 
       
  2175 	CleanupStack::Pop();
       
  2176 	TUint32 keyInfo = 0;	
       
  2177 	User::LeaveIfError(repository.CommitTransaction(keyInfo));	
       
  2178 	}
       
  2179 
       
  2180 /**
       
  2181 Sets the IAP list for a SMTP mobility account
       
  2182 
       
  2183 @param aAccount SMTP mobility account
       
  2184 @param aIapList IAP list to save
       
  2185 
       
  2186 @leave KErrArgument No entries or too many entries in the IAP list
       
  2187 
       
  2188 @publishedPartner
       
  2189 */
       
  2190 EXPORT_C void CEmailAccounts::SaveSmtpMobilityAccountIapListL(const TSmtpMobilityAccount& aAccount, const RArrayIAP& aIapList)
       
  2191 	{
       
  2192 	TInt iapListCount = aIapList.Count();
       
  2193 
       
  2194 	if (iapListCount < 1 || iapListCount > KMaxSmtpMobilityAccountIAPListEntries)
       
  2195 		{
       
  2196 		User::Leave(KErrArgument);
       
  2197 		}
       
  2198 
       
  2199 	TUint32 mobAccRepId = aAccount.iAccountId << KShiftAccountId;
       
  2200 	CRepository& repository = SmtpRepositoryL();
       
  2201 
       
  2202 	TUint32 smtpAccRepId = 0;
       
  2203 	TInt pos = 0;
       
  2204 	GetSmtpAccountInfoForMobilityAccountL(mobAccRepId, repository, smtpAccRepId, pos);
       
  2205 
       
  2206 	TInt numEntries = 0;
       
  2207 	User::LeaveIfError(repository.Get(smtpAccRepId + ESMTPMobilityAccountListNumEntriesId, numEntries));
       
  2208 
       
  2209 	if (FindIapListEntryInSmtpMobilityAccountListL(aIapList, repository, smtpAccRepId, numEntries, pos) != KErrNotFound)
       
  2210 		{
       
  2211 		User::Leave(KErrAlreadyExists);
       
  2212 		}
       
  2213 
       
  2214 	User::LeaveIfError(repository.StartTransaction(CRepository::EReadWriteTransaction));
       
  2215 	repository.CleanupRollbackTransactionPushL();
       
  2216 
       
  2217 	TUint32 offset = smtpAccRepId + (pos * ESMTPMobilityAccountListOffset);
       
  2218 
       
  2219 	User::LeaveIfError(repository.Set(offset + ESMTPMobilityAccountListNumIapsId, iapListCount));
       
  2220 
       
  2221 	TInt iapLoop = 0;
       
  2222 	
       
  2223 	for (; iapLoop < iapListCount; ++iapLoop)
       
  2224 		{
       
  2225 		User::LeaveIfError(repository.Set(offset + ESMTPMobilityAccountListIapId + iapLoop, static_cast<TInt>(aIapList[iapLoop])));
       
  2226 		}
       
  2227 
       
  2228 	for (; iapLoop < KMaxSmtpMobilityAccountIAPListEntries; ++iapLoop)
       
  2229 		{
       
  2230 		repository.Delete(offset + ESMTPMobilityAccountListIapId + iapLoop);
       
  2231 		}
       
  2232 
       
  2233 	CleanupStack::Pop();
       
  2234 	TUint32 keyInfo = 0;
       
  2235 	User::LeaveIfError(repository.CommitTransaction(keyInfo));
       
  2236 	}
       
  2237 
       
  2238 TUint CEmailAccounts::GetNextEmptyAccountSlotL(CRepository& aRepository)
       
  2239 	{
       
  2240 	TUint32 accountId = KNullId;
       
  2241 	TInt serviceId = 0;
       
  2242 	TInt error = 0;
       
  2243 	TBool found = EFalse;
       
  2244 	
       
  2245 	for (TInt count = 0; count < KMaxAccount; ++count)
       
  2246 		{		
       
  2247 		accountId = accountId + KIncrementAccount;
       
  2248 		error = aRepository.Get(accountId, serviceId);		
       
  2249 		if (error == KErrNotFound)
       
  2250 			{
       
  2251 			found = ETrue;				
       
  2252 			break;
       
  2253 			}
       
  2254 		else
       
  2255 			{
       
  2256 			User::LeaveIfError(error);
       
  2257 			}
       
  2258 		}
       
  2259 
       
  2260 	if (found == EFalse)
       
  2261 		{
       
  2262 		// No empty slot available		
       
  2263 		User::Leave(KErrNotFound);
       
  2264 		}
       
  2265 	
       
  2266 	return accountId;
       
  2267 	}
       
  2268 
       
  2269 TUint32 CEmailAccounts::FindEmailAccountL(TMsvId aService, CRepository& aRepository)
       
  2270 	{	
       
  2271 	// Get a list of accounts in CenRep that matches the service
       
  2272 	RArray<TUint32> accounts;
       
  2273 	CleanupClosePushL(accounts);	
       
  2274 	User::LeaveIfError(aRepository.FindEqL(KNullId, static_cast<TUint32>(EAccountMask), static_cast<TInt>(aService), accounts));
       
  2275 
       
  2276 	// Get the account Id from the service entry
       
  2277 	CMsvEntry* msvEntry = SessionL().GetEntryL(KMsvRootIndexEntryId);
       
  2278     CleanupStack::PushL(msvEntry);
       
  2279 
       
  2280 	msvEntry->SetEntryL(aService);	
       
  2281 	TMsvEntry entry = msvEntry->Entry();
       
  2282 	TInt accountIdInService = entry.MtmData2();
       
  2283     CleanupStack::PopAndDestroy(msvEntry);			
       
  2284 
       
  2285 	// Return the account that matches with the Id in the service entry
       
  2286 	TUint32 cenrepId = static_cast<TUint32>(accountIdInService) << KShiftAccountId;
       
  2287 	TInt index = accounts.FindL(cenrepId);	
       
  2288 	TUint32 account = accounts[index];
       
  2289 	
       
  2290 	CleanupStack::PopAndDestroy(&accounts);
       
  2291 	return account;
       
  2292 	}
       
  2293 			
       
  2294 void CEmailAccounts::DoLoadPopSettingsL(TUint32 aAccountId, CImPop3Settings& aPopSettings)
       
  2295 	{
       
  2296 	aPopSettings.Reset();	
       
  2297 	CRepository& popRepository = PopRepositoryL();
       
  2298 
       
  2299 	TInt temp = 0;
       
  2300 	TBuf<KMaxSettingStringLength> tempDesc;			
       
  2301 	TInt error = popRepository.Get(aAccountId + EPOPServerAddressId, tempDesc);
       
  2302 	if (error == KErrNone)
       
  2303 		{		
       
  2304 		aPopSettings.SetServerAddressL(tempDesc);
       
  2305 		}
       
  2306 	else if (error == KErrPermissionDenied)
       
  2307 		{
       
  2308 		aPopSettings.SetServerAddressL(KNullDesC());		
       
  2309 		}
       
  2310 	else
       
  2311 		{
       
  2312 		User::Leave(error);
       
  2313 		}
       
  2314 
       
  2315 	error = popRepository.Get(aAccountId + EPOPPortNumberId, temp);	
       
  2316 	if (error == KErrNone)
       
  2317 		{		
       
  2318 		aPopSettings.SetPort(static_cast<TUint>(temp));
       
  2319 		}
       
  2320 	else if (error == KErrPermissionDenied)
       
  2321 		{
       
  2322 		aPopSettings.SetPort(0);		
       
  2323 		}
       
  2324 	else
       
  2325 		{
       
  2326 		User::Leave(error);
       
  2327 		}
       
  2328 
       
  2329 	User::LeaveIfError(popRepository.Get(aAccountId + EPOPFlagsId, temp));	
       
  2330 	aPopSettings.SetSettingsFlags(static_cast<TUint32>(temp));
       
  2331 		
       
  2332 	TBuf8<KMaxSettingStringLength> tempDesc8;			
       
  2333 	error = popRepository.Get(aAccountId + EPOPLoginNameId, tempDesc8);	
       
  2334 	if (error == KErrNone)
       
  2335 		{		
       
  2336 		aPopSettings.SetLoginNameL(tempDesc8);
       
  2337 		}
       
  2338 	else if (error == KErrPermissionDenied)
       
  2339 		{
       
  2340 		aPopSettings.SetLoginNameL(KNullDesC8());		
       
  2341 		}
       
  2342 	else
       
  2343 		{
       
  2344 		User::Leave(error);
       
  2345 		}
       
  2346 
       
  2347 	error = popRepository.Get(aAccountId + EPOPPasswordId, tempDesc8);	
       
  2348 	if (error == KErrNone)
       
  2349 		{		
       
  2350 		aPopSettings.SetPasswordL(tempDesc8);
       
  2351 		}
       
  2352 	else if (error == KErrPermissionDenied)
       
  2353 		{
       
  2354 		aPopSettings.SetPasswordL(KNullDesC8());		
       
  2355 		}
       
  2356 	else
       
  2357 		{
       
  2358 		User::Leave(error);
       
  2359 		}
       
  2360 
       
  2361 	User::LeaveIfError(popRepository.Get(aAccountId + EPOPMaxEmailSizeId, temp));	
       
  2362 	aPopSettings.SetMaxEmailSize(temp);
       
  2363 
       
  2364 	User::LeaveIfError(popRepository.Get(aAccountId + EPOPGetEmailOptionsId, temp));	
       
  2365 	aPopSettings.SetGetMailOptions(static_cast<TPop3GetMailOptions>(temp));
       
  2366 	
       
  2367 	User::LeaveIfError(popRepository.Get(aAccountId + EPOPInboxSynchronisationLimitId, temp));	
       
  2368 	aPopSettings.SetInboxSynchronisationLimit(temp);
       
  2369 
       
  2370 	User::LeaveIfError(popRepository.Get(aAccountId + EPOPPopulationLimitId, temp));	
       
  2371 	aPopSettings.SetPopulationLimitL(temp);
       
  2372 	
       
  2373 	TInt settingsVersion;
       
  2374 	error = popRepository.Get(aAccountId + EPOPSettingsVersionId, settingsVersion);
       
  2375 
       
  2376 	if (error == KErrNone)
       
  2377 		{
       
  2378 		if (settingsVersion > 1)
       
  2379 			{
       
  2380 			User::LeaveIfError(popRepository.Get(aAccountId + EPOPTlsSslDomainId, tempDesc8));
       
  2381 			aPopSettings.SetTlsSslDomainL(tempDesc8);
       
  2382 			}
       
  2383 		}
       
  2384 	}
       
  2385 
       
  2386 void CEmailAccounts::DoLoadSmtpSettingsL(TUint32 aAccountId, CImSmtpSettings& aSmtpSettings)
       
  2387 	{
       
  2388 	aSmtpSettings.Reset();
       
  2389 	CRepository& smtpRepository = SmtpRepositoryL();
       
  2390 
       
  2391 	TInt temp = 0;
       
  2392 	TBuf<KMaxSettingStringLength> tempDesc;			
       
  2393 	TInt error = smtpRepository.Get(aAccountId + ESMTPServerAddressId, tempDesc);	
       
  2394 	if (error == KErrNone)
       
  2395 		{		
       
  2396 		aSmtpSettings.SetServerAddressL(tempDesc);
       
  2397 		}
       
  2398 	else if (error == KErrPermissionDenied)
       
  2399 		{
       
  2400 		aSmtpSettings.SetServerAddressL(KNullDesC());		
       
  2401 		}
       
  2402 	else
       
  2403 		{
       
  2404 		User::Leave(error);
       
  2405 		}
       
  2406 
       
  2407 	error = smtpRepository.Get(aAccountId + ESMTPPortNumberId, temp);	
       
  2408 	if (error == KErrNone)
       
  2409 		{		
       
  2410 		aSmtpSettings.SetPort(static_cast<TUint>(temp));
       
  2411 		}
       
  2412 	else if (error == KErrPermissionDenied)
       
  2413 		{
       
  2414 		aSmtpSettings.SetPort(0);		
       
  2415 		}
       
  2416 	else
       
  2417 		{
       
  2418 		User::Leave(error);
       
  2419 		}
       
  2420 
       
  2421 	User::LeaveIfError(smtpRepository.Get(aAccountId + ESMTPFlagsId, temp));	
       
  2422 	aSmtpSettings.SetSettingsFlags(static_cast<TUint32>(temp));
       
  2423 		
       
  2424 	error = smtpRepository.Get(aAccountId + ESMTPEmailAliasId, tempDesc);	
       
  2425 	if (error == KErrNone)
       
  2426 		{		
       
  2427 		aSmtpSettings.SetEmailAliasL(tempDesc);
       
  2428 		}
       
  2429 	else if (error == KErrPermissionDenied)
       
  2430 		{
       
  2431 		aSmtpSettings.SetEmailAliasL(KNullDesC());		
       
  2432 		}
       
  2433 	else
       
  2434 		{
       
  2435 		User::Leave(error);
       
  2436 		}
       
  2437 
       
  2438 	error = smtpRepository.Get(aAccountId + ESMTPEmailAddressId, tempDesc);	
       
  2439 	if (error == KErrNone)
       
  2440 		{		
       
  2441 		aSmtpSettings.SetEmailAddressL(tempDesc);
       
  2442 		}
       
  2443 	else if (error == KErrPermissionDenied)
       
  2444 		{
       
  2445 		aSmtpSettings.SetEmailAddressL(KNullDesC());		
       
  2446 		}
       
  2447 	else
       
  2448 		{
       
  2449 		User::Leave(error);
       
  2450 		}
       
  2451 
       
  2452 	error = smtpRepository.Get(aAccountId + ESMTPReplyToAddressId, tempDesc);	
       
  2453 	if (error == KErrNone)
       
  2454 		{		
       
  2455 		aSmtpSettings.SetReplyToAddressL(tempDesc);
       
  2456 		}
       
  2457 	else if (error == KErrPermissionDenied)
       
  2458 		{
       
  2459 		aSmtpSettings.SetReplyToAddressL(KNullDesC());		
       
  2460 		}
       
  2461 	else
       
  2462 		{
       
  2463 		User::Leave(error);
       
  2464 		}
       
  2465 
       
  2466 	error = smtpRepository.Get(aAccountId + ESMTPReceiptAddressId, tempDesc);	
       
  2467 	if (error == KErrNone)
       
  2468 		{		
       
  2469 		aSmtpSettings.SetReceiptAddressL(tempDesc);
       
  2470 		}
       
  2471 	else if (error == KErrPermissionDenied)
       
  2472 		{
       
  2473 		aSmtpSettings.SetReceiptAddressL(KNullDesC());		
       
  2474 		}
       
  2475 	else
       
  2476 		{
       
  2477 		User::Leave(error);
       
  2478 		}
       
  2479 
       
  2480 	User::LeaveIfError(smtpRepository.Get(aAccountId + ESMTPBodyEncodingId, temp));	
       
  2481 	aSmtpSettings.SetBodyEncoding(static_cast<TMsgOutboxBodyEncoding>(temp));
       
  2482 
       
  2483 	User::LeaveIfError(smtpRepository.Get(aAccountId + ESMTPDefaultMsgCharSetId, temp));	
       
  2484 	aSmtpSettings.SetDefaultMsgCharSet(TUid::Uid(temp));
       
  2485 
       
  2486 	User::LeaveIfError(smtpRepository.Get(aAccountId + ESMTPSendCopyToSelfId, temp));	
       
  2487 	aSmtpSettings.SetSendCopyToSelf(static_cast<TImSMTPSendCopyToSelf>(temp));
       
  2488 
       
  2489 	User::LeaveIfError(smtpRepository.Get(aAccountId + ESMTPSendMessageOptionId, temp));	
       
  2490 	aSmtpSettings.SetSendMessageOption(static_cast<TImSMTPSendMessageOption>(temp));
       
  2491 
       
  2492 	TBuf8<KMaxSettingStringLength> tempDesc8;			
       
  2493 	error = smtpRepository.Get(aAccountId + ESMTPLoginNameId, tempDesc8);	
       
  2494 	if (error == KErrNone)
       
  2495 		{		
       
  2496 		aSmtpSettings.SetLoginNameL(tempDesc8);
       
  2497 		}
       
  2498 	else if (error == KErrPermissionDenied)
       
  2499 		{
       
  2500 		aSmtpSettings.SetLoginNameL(KNullDesC8());		
       
  2501 		}
       
  2502 	else
       
  2503 		{
       
  2504 		User::Leave(error);
       
  2505 		}
       
  2506 
       
  2507 	error = smtpRepository.Get(aAccountId + ESMTPPasswordId, tempDesc8);	
       
  2508 	if (error == KErrNone)
       
  2509 		{		
       
  2510 		aSmtpSettings.SetPasswordL(tempDesc8);
       
  2511 		}
       
  2512 	else if (error == KErrPermissionDenied)
       
  2513 		{
       
  2514 		aSmtpSettings.SetPasswordL(KNullDesC8());		
       
  2515 		}
       
  2516 	else
       
  2517 		{
       
  2518 		User::Leave(error);
       
  2519 		}
       
  2520 	
       
  2521 	TInt smtpSettingsVersion;
       
  2522 	error = smtpRepository.Get(aAccountId + ESMTPSettingsVersionId, smtpSettingsVersion);
       
  2523 
       
  2524 	if (error == KErrNone)
       
  2525 		{
       
  2526 		if (smtpSettingsVersion > 1)
       
  2527 			{		
       
  2528 			error = smtpRepository.Get(aAccountId + ESMTPIncludeToCcId, temp);	
       
  2529 			if (error == KErrNone)
       
  2530 				{		
       
  2531 				aSmtpSettings.SetToCcIncludeLimitL(static_cast<TUint>(temp));
       
  2532 				}
       
  2533 			else if (error == KErrPermissionDenied)
       
  2534 				{
       
  2535 				aSmtpSettings.SetToCcIncludeLimitL(0);
       
  2536 				}
       
  2537 			else
       
  2538 				{
       
  2539 				User::Leave(error);
       
  2540 				}
       
  2541 			}
       
  2542 
       
  2543 		if (smtpSettingsVersion > 2)
       
  2544 			{
       
  2545 			User::LeaveIfError(smtpRepository.Get(aAccountId + ESMTPTlsSslDomainId, tempDesc8));
       
  2546 			aSmtpSettings.SetTlsSslDomainL(tempDesc8);
       
  2547 			}
       
  2548 		}	
       
  2549 	}
       
  2550 
       
  2551 void CEmailAccounts::DoLoadImapSettingsL(TUint32 aAccountId, CImImap4Settings& aImapSettings)
       
  2552 	{
       
  2553 	aImapSettings.Reset();
       
  2554 	CRepository& imapRepository = ImapRepositoryL();
       
  2555 
       
  2556 	TInt temp = 0;
       
  2557 	TBuf<KMaxSettingStringLength> tempDesc;			
       
  2558 	TInt error = imapRepository.Get(aAccountId + EIMAPServerAddressId, tempDesc);	
       
  2559 	if (error == KErrNone)
       
  2560 		{		
       
  2561 		aImapSettings.SetServerAddressL(tempDesc);
       
  2562 		}
       
  2563 	else if (error == KErrPermissionDenied)
       
  2564 		{
       
  2565 		aImapSettings.SetServerAddressL(KNullDesC());		
       
  2566 		}
       
  2567 	else
       
  2568 		{
       
  2569 		User::Leave(error);
       
  2570 		}
       
  2571 
       
  2572 	error = imapRepository.Get(aAccountId + EIMAPPortNumberId, temp);	
       
  2573 	if (error == KErrNone)
       
  2574 		{		
       
  2575 		aImapSettings.SetPort(static_cast<TUint>(temp));
       
  2576 		}
       
  2577 	else if (error == KErrPermissionDenied)
       
  2578 		{
       
  2579 		aImapSettings.SetPort(0);		
       
  2580 		}
       
  2581 	else
       
  2582 		{
       
  2583 		User::Leave(error);
       
  2584 		}
       
  2585 
       
  2586 	User::LeaveIfError(imapRepository.Get(aAccountId + EIMAPFlagsId, temp));	
       
  2587 	aImapSettings.SetSettingsFlags(static_cast<TUint32>(temp));
       
  2588 		
       
  2589 	TBuf8<KMaxSettingStringLength> tempDesc8;			
       
  2590 	error = imapRepository.Get(aAccountId + EIMAPLoginNameId, tempDesc8);	
       
  2591 	if (error == KErrNone)
       
  2592 		{		
       
  2593 		aImapSettings.SetLoginNameL(tempDesc8);
       
  2594 		}
       
  2595 	else if (error == KErrPermissionDenied)
       
  2596 		{
       
  2597 		aImapSettings.SetLoginNameL(KNullDesC8());		
       
  2598 		}
       
  2599 	else
       
  2600 		{
       
  2601 		User::Leave(error);
       
  2602 		}
       
  2603 
       
  2604 	error = imapRepository.Get(aAccountId + EIMAPPasswordId, tempDesc8);	
       
  2605 	if (error == KErrNone)
       
  2606 		{		
       
  2607 		aImapSettings.SetPasswordL(tempDesc8);
       
  2608 		}
       
  2609 	else if (error == KErrPermissionDenied)
       
  2610 		{
       
  2611 		aImapSettings.SetPasswordL(KNullDesC8());		
       
  2612 		}
       
  2613 	else
       
  2614 		{
       
  2615 		User::Leave(error);
       
  2616 		}
       
  2617 
       
  2618 	User::LeaveIfError(imapRepository.Get(aAccountId + EIMAPFolderPathId, tempDesc8));	
       
  2619 	aImapSettings.SetFolderPathL(tempDesc8);
       
  2620 
       
  2621 	User::LeaveIfError(imapRepository.Get(aAccountId + EIMAPSearchStringId, tempDesc8));	
       
  2622 	aImapSettings.SetSearchStringL(tempDesc8);
       
  2623 
       
  2624 	User::LeaveIfError(imapRepository.Get(aAccountId + EIMAPPathSeparatorId, temp));
       
  2625 	aImapSettings.SetPathSeparator(temp);	
       
  2626 	
       
  2627 	User::LeaveIfError(imapRepository.Get(aAccountId + EIMAPSynchronisationStrategyId, temp));	
       
  2628 	aImapSettings.SetSynchronise(static_cast<TFolderSyncType>(temp));
       
  2629 
       
  2630 	User::LeaveIfError(imapRepository.Get(aAccountId + EIMAPSubscriptionStrategyId, temp));	
       
  2631 	aImapSettings.SetSubscribe(static_cast<TFolderSubscribeType>(temp));
       
  2632 
       
  2633 	User::LeaveIfError(imapRepository.Get(aAccountId + EIMAPMaxEmailSizeId, temp));	
       
  2634 	aImapSettings.SetMaxEmailSize(static_cast<TUint>(temp));
       
  2635 
       
  2636 	User::LeaveIfError(imapRepository.Get(aAccountId + EIMAPGetMailOptionsId, temp));	
       
  2637 	aImapSettings.SetGetMailOptions(static_cast<TImap4GetMailOptions>(temp));
       
  2638 
       
  2639 	User::LeaveIfError(imapRepository.Get(aAccountId + EIMAPInboxSynchronisationLimitId, temp));	
       
  2640 	aImapSettings.SetInboxSynchronisationLimit(temp);
       
  2641 		
       
  2642 	User::LeaveIfError(imapRepository.Get(aAccountId + EIMAPMailboxSynchronisationLimitId, temp));	
       
  2643 	aImapSettings.SetMailboxSynchronisationLimit(temp);
       
  2644 
       
  2645 	User::LeaveIfError(imapRepository.Get(aAccountId + EIMAPSyncRateId, temp));	
       
  2646 	aImapSettings.SetSyncRateL(temp);
       
  2647 
       
  2648 	User::LeaveIfError(imapRepository.Get(aAccountId + EIMAPFetchSizeId, temp));	
       
  2649 	aImapSettings.SetFetchSizeL(static_cast<TUint>(temp));
       
  2650 
       
  2651 	User::LeaveIfError(imapRepository.Get(aAccountId + EIMAPIdleTimeoutId, temp));	
       
  2652 	aImapSettings.SetImapIdleTimeoutL(temp);
       
  2653 
       
  2654 	User::LeaveIfError(imapRepository.Get(aAccountId + EPartialMailOptionsId, temp));	
       
  2655 	aImapSettings.SetPartialMailOptionsL(static_cast<TImImap4PartialMailOptions>(temp));
       
  2656 
       
  2657 	User::LeaveIfError(imapRepository.Get(aAccountId + EBodyTextSizeLimitId, temp));	
       
  2658 	aImapSettings.SetBodyTextSizeLimitL(temp);
       
  2659 
       
  2660 	User::LeaveIfError(imapRepository.Get(aAccountId + EAttachmentSizeLimitId, temp));	
       
  2661 	aImapSettings.SetAttachmentSizeLimitL(temp);
       
  2662 
       
  2663 	TInt settingsVersion;
       
  2664 	error = imapRepository.Get(aAccountId + EIMAPSettingsVersionId, settingsVersion);
       
  2665 
       
  2666 	if (error == KErrNone)
       
  2667 		{
       
  2668 		if (settingsVersion > 1)
       
  2669 			{
       
  2670 			User::LeaveIfError(imapRepository.Get(aAccountId + EIMAPTlsSslDomainId, tempDesc8));
       
  2671 			aImapSettings.SetTlsSslDomainL(tempDesc8);
       
  2672 			}
       
  2673 		}
       
  2674 	}
       
  2675 
       
  2676 void CEmailAccounts::LoadIAPPrefSettingsL(TUint32 aAccountId, CImIAPPreferences& aIAP, CRepository& aRepository)
       
  2677 	{
       
  2678 	// Remove existing settings.
       
  2679 	aIAP.Reset();
       
  2680 
       
  2681 	// temp variable for reading from CenRep
       
  2682 	TInt temp = 0;
       
  2683 
       
  2684 	// obtain the settings version
       
  2685 	TInt settingsVersion;
       
  2686 	TInt error = aRepository.Get(aAccountId + EIAPPrefVersion, settingsVersion);
       
  2687 
       
  2688 	// if a SNAP has been defined, use it...
       
  2689 	TBool useSnap = EFalse;
       
  2690 	if (error == KErrNone)
       
  2691 		{
       
  2692 		if (settingsVersion > 1)
       
  2693 			{
       
  2694 			User::LeaveIfError(aRepository.Get(aAccountId + EIAPSnapId, temp));
       
  2695 			if (temp != 0)
       
  2696 				{
       
  2697 				aIAP.SetSNAPL(temp);
       
  2698 				useSnap = ETrue;
       
  2699 				}
       
  2700 			}
       
  2701 		}
       
  2702 	
       
  2703 	// ... otherwise read in the IAP choices.
       
  2704 	if (!useSnap)
       
  2705 		{
       
  2706 		TInt nChoices = 0;
       
  2707 		User::LeaveIfError(aRepository.Get(aAccountId + EIAPChoicesCount, nChoices));	
       
  2708 			
       
  2709 		TUint32 iapID = aAccountId + EIAPListIDs;				// 0x00011000	
       
  2710 		TUint32 dialogPrefs = aAccountId + EIAPListDialogPrefs;	// 0x00012000
       
  2711 
       
  2712 		while (nChoices--)
       
  2713 			{
       
  2714 			// get IAP settings from CenRep
       
  2715 			TImIAPChoice choice;
       
  2716 			User::LeaveIfError(aRepository.Get(iapID, temp));	
       
  2717 			choice.iIAP = static_cast<TUint32>(temp);
       
  2718 
       
  2719 			User::LeaveIfError(aRepository.Get(dialogPrefs, temp));	
       
  2720 			choice.iDialogPref = static_cast<TCommDbDialogPref>(temp);
       
  2721 				
       
  2722 			// append IAP at the end of the list
       
  2723 			TInt nIAPs = aIAP.NumberOfIAPs();
       
  2724 			aIAP.AddIAPL(choice, nIAPs);
       
  2725 				
       
  2726 			++iapID;
       
  2727 			++dialogPrefs;
       
  2728 			}
       
  2729 		}
       
  2730 	}
       
  2731 
       
  2732 void CEmailAccounts::DoSavePopSettingsL(TUint32 aAccountId, const CImPop3Settings& aPopSettings)
       
  2733 	{
       
  2734 	CRepository& popRepository = PopRepositoryL();
       
  2735 
       
  2736 	popRepository.Set(aAccountId + EPOPSettingsVersionId, KPOPSettingsVersion);
       
  2737 	popRepository.Set(aAccountId + EPOPServerAddressId, aPopSettings.ServerAddress());
       
  2738 	popRepository.Set(aAccountId + EPOPPortNumberId, static_cast<TInt>(aPopSettings.Port()));
       
  2739 	popRepository.Set(aAccountId + EPOPFlagsId, static_cast<TInt>(aPopSettings.SettingsFlags()));
       
  2740 	popRepository.Set(aAccountId + EPOPLoginNameId, aPopSettings.LoginName());
       
  2741 	popRepository.Set(aAccountId + EPOPPasswordId, aPopSettings.Password());
       
  2742 	popRepository.Set(aAccountId + EPOPMaxEmailSizeId, aPopSettings.MaxEmailSize());
       
  2743 	popRepository.Set(aAccountId + EPOPGetEmailOptionsId, static_cast<TInt>(aPopSettings.GetMailOptions()));
       
  2744 	popRepository.Set(aAccountId + EPOPInboxSynchronisationLimitId, static_cast<TInt>(aPopSettings.InboxSynchronisationLimit()));
       
  2745 	popRepository.Set(aAccountId + EPOPPopulationLimitId, static_cast<TInt>(aPopSettings.PopulationLimit()));
       
  2746 	popRepository.Set(aAccountId + EPOPTlsSslDomainId, aPopSettings.TlsSslDomain());
       
  2747 	}
       
  2748 
       
  2749 void CEmailAccounts::DoSaveSmtpSettingsL(TUint32 aAccountId, const CImSmtpSettings& aSmtpSettings)
       
  2750 	{
       
  2751 	CRepository& smtpRepository = SmtpRepositoryL();
       
  2752 
       
  2753 	smtpRepository.Set(aAccountId + ESMTPSettingsVersionId, KSMTPSettingsVersion);
       
  2754 	smtpRepository.Set(aAccountId + ESMTPServerAddressId, aSmtpSettings.ServerAddress());
       
  2755 	smtpRepository.Set(aAccountId + ESMTPPortNumberId, static_cast<TInt>(aSmtpSettings.Port()));
       
  2756 	smtpRepository.Set(aAccountId + ESMTPFlagsId, static_cast<TInt>(aSmtpSettings.SettingsFlags()));
       
  2757 	smtpRepository.Set(aAccountId + ESMTPEmailAliasId, aSmtpSettings.EmailAlias());
       
  2758 	smtpRepository.Set(aAccountId + ESMTPEmailAddressId, aSmtpSettings.EmailAddress());
       
  2759 	smtpRepository.Set(aAccountId + ESMTPReplyToAddressId, aSmtpSettings.ReplyToAddress());
       
  2760 	smtpRepository.Set(aAccountId + ESMTPReceiptAddressId, aSmtpSettings.ReceiptAddress());
       
  2761 	smtpRepository.Set(aAccountId + ESMTPBodyEncodingId, static_cast<TInt>(aSmtpSettings.BodyEncoding()));
       
  2762 	smtpRepository.Set(aAccountId + ESMTPDefaultMsgCharSetId, static_cast<TInt>(aSmtpSettings.DefaultMsgCharSet().iUid));
       
  2763 	smtpRepository.Set(aAccountId + ESMTPSendCopyToSelfId, static_cast<TInt>(aSmtpSettings.SendCopyToSelf()));
       
  2764 	smtpRepository.Set(aAccountId + ESMTPSendMessageOptionId, static_cast<TInt>(aSmtpSettings.SendMessageOption()));
       
  2765 	smtpRepository.Set(aAccountId + ESMTPLoginNameId, aSmtpSettings.LoginName());
       
  2766 	smtpRepository.Set(aAccountId + ESMTPPasswordId, aSmtpSettings.Password());
       
  2767 	smtpRepository.Set(aAccountId + ESMTPIncludeToCcId, aSmtpSettings.ToCcIncludeLimit());
       
  2768 	smtpRepository.Set(aAccountId + ESMTPTlsSslDomainId, aSmtpSettings.TlsSslDomain());
       
  2769 	}
       
  2770 
       
  2771 void CEmailAccounts::SaveIAPPrefSettingsL(TUint32 aAccountId, const CImIAPPreferences& aIAP, CRepository& aRepository)
       
  2772 	{
       
  2773 	aRepository.Set(aAccountId + EIAPPrefVersion, KIAPPrefSettingsVersion);
       
  2774 	TInt nChoices = aIAP.NumberOfIAPs();	
       
  2775 	if (nChoices>KMaxIAPChoices)
       
  2776 		{
       
  2777 		User::Leave(KErrOverflow);
       
  2778 		}
       
  2779 
       
  2780 	aRepository.Set(aAccountId + EIAPChoicesCount,  nChoices);
       
  2781 	TUint32 iapID = aAccountId + EIAPListIDs;				// 0x00011000	
       
  2782 	TUint32 dialogPrefs = aAccountId + EIAPListDialogPrefs;	// 0x00012000
       
  2783 	for (TInt i=0; i<nChoices; ++i)
       
  2784 		{
       
  2785 		aRepository.Set(iapID, static_cast<TInt>(aIAP.IAPPreference(i).iIAP));		
       
  2786 		aRepository.Set(dialogPrefs, static_cast<TInt>(aIAP.IAPPreference(i).iDialogPref));		
       
  2787 									
       
  2788 		++iapID;
       
  2789 		++dialogPrefs;
       
  2790 		}
       
  2791 	aRepository.Set(aAccountId + EIAPSnapId, static_cast<TInt>(aIAP.SNAPPreference()));
       
  2792 	}
       
  2793 
       
  2794 void CEmailAccounts::DoSaveImapSettingsL(TUint32 aAccountId, const CImImap4Settings& aImapSettings)
       
  2795 	{
       
  2796 	CRepository& imapRepository = ImapRepositoryL();
       
  2797 
       
  2798 	imapRepository.Set(aAccountId + EIMAPSettingsVersionId, KIMAPSettingsVersion);
       
  2799 	imapRepository.Set(aAccountId + EIMAPServerAddressId, aImapSettings.ServerAddress());
       
  2800 	imapRepository.Set(aAccountId + EIMAPPortNumberId, static_cast<TInt>(aImapSettings.Port()));
       
  2801 	imapRepository.Set(aAccountId + EIMAPFlagsId, static_cast<TInt>(aImapSettings.SettingsFlags()));
       
  2802 	imapRepository.Set(aAccountId + EIMAPLoginNameId, aImapSettings.LoginName());
       
  2803 	imapRepository.Set(aAccountId + EIMAPPasswordId, aImapSettings.Password());
       
  2804 	imapRepository.Set(aAccountId + EIMAPFolderPathId, aImapSettings.FolderPath());
       
  2805 	imapRepository.Set(aAccountId + EIMAPPathSeparatorId, static_cast<TInt>(aImapSettings.PathSeparator()));
       
  2806 	imapRepository.Set(aAccountId + EIMAPSynchronisationStrategyId, static_cast<TInt>(aImapSettings.Synchronise()));
       
  2807 	imapRepository.Set(aAccountId + EIMAPSubscriptionStrategyId, static_cast<TInt>(aImapSettings.Subscribe()));
       
  2808 	imapRepository.Set(aAccountId + EIMAPMaxEmailSizeId, static_cast<TInt>(aImapSettings.MaxEmailSize()));
       
  2809 	imapRepository.Set(aAccountId + EIMAPGetMailOptionsId, static_cast<TInt>(aImapSettings.GetMailOptions()));
       
  2810 	imapRepository.Set(aAccountId + EIMAPInboxSynchronisationLimitId, static_cast<TInt>(aImapSettings.InboxSynchronisationLimit()));
       
  2811 	imapRepository.Set(aAccountId + EIMAPMailboxSynchronisationLimitId, static_cast<TInt>(aImapSettings.MailboxSynchronisationLimit()));
       
  2812 	imapRepository.Set(aAccountId + EIMAPTlsSslDomainId, aImapSettings.TlsSslDomain());
       
  2813 	imapRepository.Set(aAccountId + EIMAPSyncRateId, aImapSettings.SyncRate());
       
  2814 	imapRepository.Set(aAccountId + EIMAPFetchSizeId, static_cast<TInt>(aImapSettings.FetchSize()));
       
  2815 	imapRepository.Set(aAccountId + EIMAPIdleTimeoutId, aImapSettings.ImapIdleTimeout());
       
  2816 	imapRepository.Set(aAccountId + EPartialMailOptionsId, static_cast<TInt>(aImapSettings.PartialMailOptions()));
       
  2817 	imapRepository.Set(aAccountId + EBodyTextSizeLimitId, static_cast<TInt>(aImapSettings.BodyTextSizeLimit()));
       
  2818 	imapRepository.Set(aAccountId + EAttachmentSizeLimitId, static_cast<TInt>(aImapSettings.AttachmentSizeLimit()));
       
  2819 	imapRepository.Set(aAccountId + EIMAPSearchStringId, aImapSettings.SearchString());
       
  2820 	}
       
  2821 
       
  2822 /**
       
  2823 Saves the IMAP sync download rules
       
  2824 
       
  2825 @param aAccountId IMAP account ID
       
  2826 @param aRules Rules to save
       
  2827 @param aRepository IMAP repository
       
  2828 */
       
  2829 void CEmailAccounts::DoSaveImapSyncDownloadRulesL(TUint32 aAccountId, const CImapSyncDownloadRules& aRules, CRepository& aRepository)
       
  2830 	{
       
  2831 	aRepository.Set(aAccountId + EIMAPSyncDownloadRulesVersionId, KImapSyncDownloadRulesVersion);
       
  2832 
       
  2833 	TInt ruleOffset;
       
  2834 	TUint32 bearerTypes;
       
  2835 	TImImap4GetPartialMailInfo mailInfo;
       
  2836 	TInt rule = 0;
       
  2837 
       
  2838 	while (rule < KMaxImapSyncDownloadRules &&
       
  2839 	       aRules.GetRuleL(CImapSyncDownloadRules::EInboxRulesType, rule, bearerTypes, mailInfo))
       
  2840 		{
       
  2841 		ruleOffset = aAccountId + (rule * EIMAPSyncDownloadRulesOffset);
       
  2842 
       
  2843 		aRepository.Set(ruleOffset + EIMAPInboxSyncDownloadRulesBearerTypesId, static_cast<TInt>(bearerTypes));
       
  2844 		aRepository.Set(ruleOffset + EIMAPInboxSyncDownloadRulesMailOptionsId, mailInfo.iPartialMailOptions);
       
  2845 		aRepository.Set(ruleOffset + EIMAPInboxSyncDownloadRulesTotalSizeLimitId, static_cast<TInt>(mailInfo.iTotalSizeLimit));
       
  2846 		aRepository.Set(ruleOffset + EIMAPInboxSyncDownloadRulesBodyTextSizeLimitId, static_cast<TInt>(mailInfo.iBodyTextSizeLimit));
       
  2847 		aRepository.Set(ruleOffset + EIMAPInboxSyncDownloadRulesAttachmentSizeLimitId, static_cast<TInt>(mailInfo.iAttachmentSizeLimit));
       
  2848 		aRepository.Set(ruleOffset + EIMAPInboxSyncDownloadRulesGetMailBodyPartsId, mailInfo.iGetMailBodyParts);
       
  2849 		aRepository.Set(ruleOffset + EIMAPInboxSyncDownloadRulesMaxEmailSizeId, static_cast<TInt>(mailInfo.iMaxEmailSize));
       
  2850 
       
  2851 		++rule;
       
  2852 		}
       
  2853 
       
  2854 	aRepository.Set(aAccountId + EIMAPNumInboxSyncDownloadRulesId, rule);
       
  2855 
       
  2856 	// Delete any old rules that are no longer required
       
  2857 	while (rule < KMaxImapSyncDownloadRules)
       
  2858 		{
       
  2859 		ruleOffset = aAccountId + (rule * EIMAPSyncDownloadRulesOffset);
       
  2860 
       
  2861 		aRepository.Delete(ruleOffset + EIMAPInboxSyncDownloadRulesBearerTypesId);
       
  2862 		aRepository.Delete(ruleOffset + EIMAPInboxSyncDownloadRulesMailOptionsId);
       
  2863 		aRepository.Delete(ruleOffset + EIMAPInboxSyncDownloadRulesTotalSizeLimitId);
       
  2864 		aRepository.Delete(ruleOffset + EIMAPInboxSyncDownloadRulesBodyTextSizeLimitId);
       
  2865 		aRepository.Delete(ruleOffset + EIMAPInboxSyncDownloadRulesAttachmentSizeLimitId);
       
  2866 		aRepository.Delete(ruleOffset + EIMAPInboxSyncDownloadRulesGetMailBodyPartsId);
       
  2867 		aRepository.Delete(ruleOffset + EIMAPInboxSyncDownloadRulesMaxEmailSizeId);
       
  2868 
       
  2869 		++rule;
       
  2870 		}
       
  2871 
       
  2872 	rule = 0;
       
  2873 
       
  2874 	while (rule < KMaxImapSyncDownloadRules &&
       
  2875 	       aRules.GetRuleL(CImapSyncDownloadRules::EFolderRulesType, rule, bearerTypes, mailInfo))
       
  2876 		{
       
  2877 		ruleOffset = aAccountId + (rule * EIMAPSyncDownloadRulesOffset);
       
  2878 
       
  2879 		aRepository.Set(ruleOffset + EIMAPFolderSyncDownloadRulesBearerTypesId, static_cast<TInt>(bearerTypes));
       
  2880 		aRepository.Set(ruleOffset + EIMAPFolderSyncDownloadRulesMailOptionsId, mailInfo.iPartialMailOptions);
       
  2881 		aRepository.Set(ruleOffset + EIMAPFolderSyncDownloadRulesTotalSizeLimitId, static_cast<TInt>(mailInfo.iTotalSizeLimit));
       
  2882 		aRepository.Set(ruleOffset + EIMAPFolderSyncDownloadRulesBodyTextSizeLimitId, static_cast<TInt>(mailInfo.iBodyTextSizeLimit));
       
  2883 		aRepository.Set(ruleOffset + EIMAPFolderSyncDownloadRulesAttachmentSizeLimitId, static_cast<TInt>(mailInfo.iAttachmentSizeLimit));
       
  2884 		aRepository.Set(ruleOffset + EIMAPFolderSyncDownloadRulesGetMailBodyPartsId, mailInfo.iGetMailBodyParts);
       
  2885 		aRepository.Set(ruleOffset + EIMAPFolderSyncDownloadRulesMaxEmailSizeId, static_cast<TInt>(mailInfo.iMaxEmailSize));
       
  2886 
       
  2887 		++rule;
       
  2888 		}
       
  2889 
       
  2890 	aRepository.Set(aAccountId + EIMAPNumFolderSyncDownloadRulesId, rule);
       
  2891 
       
  2892 	// Delete any old rules that are no longer required
       
  2893 	while (rule < KMaxImapSyncDownloadRules)
       
  2894 		{
       
  2895 		ruleOffset = aAccountId + (rule * EIMAPSyncDownloadRulesOffset);
       
  2896 
       
  2897 		aRepository.Delete(ruleOffset + EIMAPFolderSyncDownloadRulesBearerTypesId);
       
  2898 		aRepository.Delete(ruleOffset + EIMAPFolderSyncDownloadRulesMailOptionsId);
       
  2899 		aRepository.Delete(ruleOffset + EIMAPFolderSyncDownloadRulesTotalSizeLimitId);
       
  2900 		aRepository.Delete(ruleOffset + EIMAPFolderSyncDownloadRulesBodyTextSizeLimitId);
       
  2901 		aRepository.Delete(ruleOffset + EIMAPFolderSyncDownloadRulesAttachmentSizeLimitId);
       
  2902 		aRepository.Delete(ruleOffset + EIMAPFolderSyncDownloadRulesGetMailBodyPartsId);
       
  2903 		aRepository.Delete(ruleOffset + EIMAPFolderSyncDownloadRulesMaxEmailSizeId);
       
  2904 
       
  2905 		++rule;
       
  2906 		}
       
  2907 	}
       
  2908 
       
  2909 void CEmailAccounts::DeletePopSettings(TUint32 aAccountId, CRepository& aRepository)
       
  2910 	{
       
  2911 	aRepository.Delete(aAccountId + EPOPServiceId);	
       
  2912 	aRepository.Delete(aAccountId + EPOPAccountNameId);		
       
  2913 	aRepository.Delete(aAccountId + EPOPRelatedSMTPServiceId);	
       
  2914 	aRepository.Delete(aAccountId + EPOPSettingsVersionId);	
       
  2915 	aRepository.Delete(aAccountId + EPOPServerAddressId);	
       
  2916 	aRepository.Delete(aAccountId + EPOPPortNumberId);	
       
  2917 	aRepository.Delete(aAccountId + EPOPFlagsId);	
       
  2918 	aRepository.Delete(aAccountId + EPOPLoginNameId);	
       
  2919 	aRepository.Delete(aAccountId + EPOPPasswordId);	
       
  2920 	aRepository.Delete(aAccountId + EPOPMaxEmailSizeId);	
       
  2921 	aRepository.Delete(aAccountId + EPOPGetEmailOptionsId);	
       
  2922 	aRepository.Delete(aAccountId + EPOPInboxSynchronisationLimitId);	
       
  2923 	aRepository.Delete(aAccountId + EPOPPopulationLimitId);	
       
  2924 	aRepository.Delete(aAccountId + EPOPReadOnlyAccountId);
       
  2925 	aRepository.Delete(aAccountId + EPOPTlsSslDomainId);
       
  2926 	}
       
  2927 
       
  2928 void CEmailAccounts::DeleteIAPPrefSettingsL(TUint32 aAccountId, CRepository& aRepository)
       
  2929 	{
       
  2930 	aRepository.Delete(aAccountId + EIAPPrefVersion);	
       
  2931 	
       
  2932 	// Read in the number of choices.
       
  2933 	TInt nChoices = 0;
       
  2934 	User::LeaveIfError(aRepository.Get(aAccountId + EIAPChoicesCount, nChoices));	
       
  2935 	aRepository.Delete(aAccountId + EIAPChoicesCount);	
       
  2936 		
       
  2937 	TUint32 iapID = aAccountId + EIAPListIDs;				// 0x00011000	
       
  2938 	TUint32 dialogPrefs = aAccountId + EIAPListDialogPrefs;	// 0x00012000
       
  2939 	while (nChoices--)
       
  2940 		{
       
  2941 		// Delete IAP settings in CenRep
       
  2942 		aRepository.Delete(iapID);	
       
  2943 		aRepository.Delete(dialogPrefs);	
       
  2944 		
       
  2945 		++iapID;				
       
  2946 		++dialogPrefs;
       
  2947 		}
       
  2948 	}
       
  2949 
       
  2950 void CEmailAccounts::DeleteSmtpSettings(TUint32 aAccountId, CRepository& aRepository)
       
  2951 	{
       
  2952 	aRepository.Delete(aAccountId + ESMTPServiceId);	
       
  2953 	aRepository.Delete(aAccountId + ESMTPAccountNameId);	
       
  2954 	aRepository.Delete(aAccountId + ESMTPSettingsVersionId);	
       
  2955 	aRepository.Delete(aAccountId + ESMTPServerAddressId);	
       
  2956 	aRepository.Delete(aAccountId + ESMTPPortNumberId);	
       
  2957 	aRepository.Delete(aAccountId + ESMTPFlagsId);	
       
  2958 	aRepository.Delete(aAccountId + ESMTPEmailAliasId);	
       
  2959 	aRepository.Delete(aAccountId + ESMTPEmailAddressId);	
       
  2960 	aRepository.Delete(aAccountId + ESMTPReplyToAddressId);	
       
  2961 	aRepository.Delete(aAccountId + ESMTPReceiptAddressId);	
       
  2962 	aRepository.Delete(aAccountId + ESMTPBodyEncodingId);	
       
  2963 	aRepository.Delete(aAccountId + ESMTPDefaultMsgCharSetId);	
       
  2964 	aRepository.Delete(aAccountId + ESMTPSendCopyToSelfId);	
       
  2965 	aRepository.Delete(aAccountId + ESMTPSendMessageOptionId);	
       
  2966 	aRepository.Delete(aAccountId + ESMTPLoginNameId);	
       
  2967 	aRepository.Delete(aAccountId + ESMTPPasswordId);
       
  2968 	aRepository.Delete(aAccountId + ESMTPRelatedServiceId);	
       
  2969 	aRepository.Delete(aAccountId + ESMTPIncludeToCcId);
       
  2970 	aRepository.Delete(aAccountId + ESMTPReadOnlyAccountId);
       
  2971 	aRepository.Delete(aAccountId + ESMTPParentAccountId);
       
  2972 	aRepository.Delete(aAccountId + ESMTPTlsSslDomainId);
       
  2973 	aRepository.Delete(aAccountId + ESMTPLinkedAccountId);
       
  2974 	}
       
  2975 
       
  2976 void CEmailAccounts::DeleteImapSettings(TUint32 aAccountId, CRepository& aRepository)
       
  2977 	{
       
  2978 	aRepository.Delete(aAccountId + EIMAPServiceId);	
       
  2979 	aRepository.Delete(aAccountId + EIMAPAccountNameId);	
       
  2980 	aRepository.Delete(aAccountId + EIMAPRelatedSMTPServiceId);	
       
  2981 	aRepository.Delete(aAccountId + EIMAPSettingsVersionId);	
       
  2982 	aRepository.Delete(aAccountId + EIMAPServerAddressId);	
       
  2983 	aRepository.Delete(aAccountId + EIMAPPortNumberId);	
       
  2984 	aRepository.Delete(aAccountId + EIMAPFlagsId);	
       
  2985 	aRepository.Delete(aAccountId + EIMAPLoginNameId);	
       
  2986 	aRepository.Delete(aAccountId + EIMAPPasswordId);	
       
  2987 	aRepository.Delete(aAccountId + EIMAPFolderPathId);	
       
  2988 	aRepository.Delete(aAccountId + EIMAPPathSeparatorId);	
       
  2989 	aRepository.Delete(aAccountId + EIMAPSynchronisationStrategyId);	
       
  2990 	aRepository.Delete(aAccountId + EIMAPSubscriptionStrategyId);	
       
  2991 	aRepository.Delete(aAccountId + EIMAPMaxEmailSizeId);	
       
  2992 	aRepository.Delete(aAccountId + EIMAPGetMailOptionsId);	
       
  2993 	aRepository.Delete(aAccountId + EIMAPInboxSynchronisationLimitId);	
       
  2994 	aRepository.Delete(aAccountId + EIMAPMailboxSynchronisationLimitId);	
       
  2995 	aRepository.Delete(aAccountId + EIMAPTlsSslDomainId);
       
  2996 	aRepository.Delete(aAccountId + EIMAPSyncRateId);
       
  2997 	aRepository.Delete(aAccountId + EIMAPFetchSizeId);	
       
  2998 	aRepository.Delete(aAccountId + EIMAPIdleTimeoutId);	
       
  2999 	aRepository.Delete(aAccountId + EPartialMailOptionsId);	
       
  3000 	aRepository.Delete(aAccountId + EBodyTextSizeLimitId);	
       
  3001 	aRepository.Delete(aAccountId + EAttachmentSizeLimitId);	
       
  3002 	aRepository.Delete(aAccountId + EIMAPSearchStringId);	
       
  3003 	aRepository.Delete(aAccountId + EIMAPReadOnlyAccountId);	
       
  3004 	}
       
  3005 
       
  3006 /**
       
  3007 Deletes IMAP synchronisation download rules
       
  3008 
       
  3009 @param aAccountId IMAP account ID
       
  3010 @param aRepository IMAP repository
       
  3011 */
       
  3012 void CEmailAccounts::DeleteImapSyncDownloadRulesL(TUint32 aAccountId, CRepository& aRepository)
       
  3013 	{
       
  3014 	if (aRepository.Delete(aAccountId + EIMAPSyncDownloadRulesVersionId) == KErrNone)
       
  3015 		{
       
  3016 		TInt numRules;
       
  3017 		User::LeaveIfError(aRepository.Get(aAccountId + EIMAPNumInboxSyncDownloadRulesId, numRules));
       
  3018 
       
  3019 		aRepository.Delete(aAccountId + EIMAPNumInboxSyncDownloadRulesId);
       
  3020 
       
  3021 		TUint32 ruleOffset;
       
  3022 		for (TInt rule = 0; rule < numRules; ++rule)
       
  3023 			{
       
  3024 			ruleOffset = aAccountId + (rule * EIMAPSyncDownloadRulesOffset);
       
  3025 			aRepository.Delete(ruleOffset + EIMAPInboxSyncDownloadRulesBearerTypesId);
       
  3026 			aRepository.Delete(ruleOffset + EIMAPInboxSyncDownloadRulesMailOptionsId);
       
  3027 			aRepository.Delete(ruleOffset + EIMAPInboxSyncDownloadRulesTotalSizeLimitId);
       
  3028 			aRepository.Delete(ruleOffset + EIMAPInboxSyncDownloadRulesBodyTextSizeLimitId);
       
  3029 			aRepository.Delete(ruleOffset + EIMAPInboxSyncDownloadRulesAttachmentSizeLimitId);
       
  3030 			aRepository.Delete(ruleOffset + EIMAPInboxSyncDownloadRulesGetMailBodyPartsId);
       
  3031 			aRepository.Delete(ruleOffset + EIMAPInboxSyncDownloadRulesMaxEmailSizeId);
       
  3032 			}
       
  3033 
       
  3034 		User::LeaveIfError(aRepository.Get(aAccountId + EIMAPNumFolderSyncDownloadRulesId, numRules));
       
  3035 
       
  3036 		aRepository.Delete(aAccountId + EIMAPNumFolderSyncDownloadRulesId);
       
  3037 
       
  3038 		for (TInt rule = 0; rule < numRules; ++rule)
       
  3039 			{
       
  3040 			ruleOffset = aAccountId + (rule * EIMAPSyncDownloadRulesOffset);
       
  3041 			aRepository.Delete(ruleOffset + EIMAPFolderSyncDownloadRulesBearerTypesId);
       
  3042 			aRepository.Delete(ruleOffset + EIMAPFolderSyncDownloadRulesMailOptionsId);
       
  3043 			aRepository.Delete(ruleOffset + EIMAPFolderSyncDownloadRulesTotalSizeLimitId);
       
  3044 			aRepository.Delete(ruleOffset + EIMAPFolderSyncDownloadRulesBodyTextSizeLimitId);
       
  3045 			aRepository.Delete(ruleOffset + EIMAPFolderSyncDownloadRulesAttachmentSizeLimitId);
       
  3046 			aRepository.Delete(ruleOffset + EIMAPFolderSyncDownloadRulesGetMailBodyPartsId);
       
  3047 			aRepository.Delete(ruleOffset + EIMAPFolderSyncDownloadRulesMaxEmailSizeId);
       
  3048 			}
       
  3049 		}
       
  3050 	}
       
  3051 
       
  3052 /**
       
  3053 Deletes all the IMAP transport buffer sizes
       
  3054 
       
  3055 @publishedPartner
       
  3056 */
       
  3057 EXPORT_C void CEmailAccounts::DeleteImapTransportBufferSizesL()
       
  3058 	{
       
  3059 	CRepository& imapRepository = ImapRepositoryL();
       
  3060 
       
  3061 	if (imapRepository.Delete(EIMAPTransportBufferSizesVersionId) == KErrNone)
       
  3062 		{
       
  3063 		TInt numEntries;
       
  3064 		User::LeaveIfError(imapRepository.Get(EIMAPNumTransportBufferSizesId, numEntries));
       
  3065 
       
  3066 		imapRepository.Delete(EIMAPNumTransportBufferSizesId);
       
  3067 
       
  3068 		TUint32 entryOffset;
       
  3069 		for (TInt entry = 0; entry < numEntries; ++entry)
       
  3070 			{
       
  3071 			entryOffset = entry * EIMAPTransportBufferSizesOffset;
       
  3072 			imapRepository.Delete(entryOffset + EIMAPTransportBufferSizesBearerTypesId);
       
  3073 			imapRepository.Delete(entryOffset + EIMAPTransportBufferSizesFetchRequestSizeId);
       
  3074 			imapRepository.Delete(entryOffset + EIMAPTransportBufferSizesMaxOutstandingFetchResponsesId);
       
  3075 			}
       
  3076 		}
       
  3077 	}
       
  3078 
       
  3079 CMsvSession& CEmailAccounts::SessionL()
       
  3080 	{
       
  3081 	if (iMsvSession == NULL)
       
  3082 		{
       
  3083 		iMsvSession = CMsvSession::OpenSyncL(*this);		
       
  3084 		}
       
  3085 		
       
  3086 	return *iMsvSession;	
       
  3087 	}
       
  3088 
       
  3089 CRepository& CEmailAccounts::PopRepositoryL()
       
  3090 	{
       
  3091 	if (iPOPRepository == NULL)
       
  3092 		{
       
  3093 		iPOPRepository = CRepository::NewL(KUidMsgTypePOP3);
       
  3094 		}
       
  3095 		
       
  3096 	return *iPOPRepository;	
       
  3097 	}
       
  3098 
       
  3099 CRepository& CEmailAccounts::ImapRepositoryL()
       
  3100 	{
       
  3101 	if (iIMAPRepository == NULL)
       
  3102 		{
       
  3103 		iIMAPRepository = CRepository::NewL(KUidMsgTypeIMAP4);
       
  3104 		}
       
  3105 		
       
  3106 	return *iIMAPRepository;	
       
  3107 	}
       
  3108 
       
  3109 CRepository& CEmailAccounts::SmtpRepositoryL()
       
  3110 	{
       
  3111 	if (iSMTPRepository == NULL)
       
  3112 		{
       
  3113 		iSMTPRepository = CRepository::NewL(KUidMsgTypeSMTP);
       
  3114 		}
       
  3115 		
       
  3116 	return *iSMTPRepository;	
       
  3117 	}
       
  3118 
       
  3119 void CEmailAccounts::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* , TAny* , TAny* ) 
       
  3120 	{
       
  3121   	if (aEvent == EMsvCloseSession)
       
  3122     	{
       
  3123 		delete iMsvSession;
       
  3124 		iMsvSession = NULL;
       
  3125     	}    	
       
  3126    }
       
  3127 
       
  3128 /**
       
  3129 Checks if the account is read-only
       
  3130 
       
  3131 @param	aAccount
       
  3132 POP account Id
       
  3133 
       
  3134 @return
       
  3135 ETrue if it is a read-only account
       
  3136 Note: Read-only accounts should not be modified, however we don't enforce it.
       
  3137 */	
       
  3138 EXPORT_C TBool CEmailAccounts::IsAccountReadOnlyL(const TPopAccount& aAccount)
       
  3139 	{
       
  3140 	TUint32 accountId = aAccount.iPopAccountId << KShiftAccountId;	
       
  3141 
       
  3142 	TInt temp = 0;
       
  3143 	User::LeaveIfError(PopRepositoryL().Get(accountId + EPOPReadOnlyAccountId, temp));	
       
  3144 	TBool isReadOnly = static_cast<TBool>(temp);
       
  3145 	return isReadOnly;
       
  3146 	}
       
  3147 
       
  3148 /**
       
  3149 Checks if the account is read-only
       
  3150 
       
  3151 @param	aAccount
       
  3152 IMAP account Id
       
  3153 
       
  3154 @return
       
  3155 ETrue if it is a read-only account
       
  3156 Note: Read-only accounts should not be modified, however we don't enforce it.
       
  3157 */	
       
  3158 EXPORT_C TBool CEmailAccounts::IsAccountReadOnlyL(const TImapAccount& aAccount)
       
  3159 	{
       
  3160 	TUint32 accountId = aAccount.iImapAccountId << KShiftAccountId;	
       
  3161 
       
  3162 	TInt temp = 0;
       
  3163 	User::LeaveIfError(ImapRepositoryL().Get(accountId + EIMAPReadOnlyAccountId, temp));	
       
  3164 	TBool isReadOnly = static_cast<TBool>(temp);
       
  3165 	return isReadOnly;	
       
  3166 	}
       
  3167 
       
  3168 /**
       
  3169 Checks if the account is read-only
       
  3170 
       
  3171 @param	aAccount
       
  3172 SMTP account Id
       
  3173 
       
  3174 @return
       
  3175 ETrue if it is a read-only account
       
  3176 Note: Read-only accounts should not be modified, however we don't enforce it.
       
  3177 */	
       
  3178 EXPORT_C TBool CEmailAccounts::IsAccountReadOnlyL(const TSmtpAccount& aAccount)
       
  3179 	{
       
  3180 	TUint32 smtpAccRepId = aAccount.iSmtpAccountId << KShiftAccountId;
       
  3181 
       
  3182 	return IsSmtpTypeAccountReadOnlyL(smtpAccRepId);
       
  3183 	}
       
  3184 
       
  3185 /**
       
  3186 Checks if SMTP mobility account is read only
       
  3187 
       
  3188 @param aAccount SMTP mobility account
       
  3189 
       
  3190 @publishedPartner
       
  3191 */
       
  3192 EXPORT_C TBool CEmailAccounts::IsAccountReadOnlyL(const TSmtpMobilityAccount& aAccount)
       
  3193 	{
       
  3194 	TUint32 mobAccRepId = aAccount.iAccountId << KShiftAccountId;
       
  3195 
       
  3196 	return IsSmtpTypeAccountReadOnlyL(mobAccRepId);
       
  3197 	}
       
  3198 
       
  3199 /**
       
  3200 Creates a SMTP mobility account
       
  3201 
       
  3202 @param aAccountName Account name
       
  3203 @param aIapList List of IAPs that will use the account settings
       
  3204 @param aSettings Settings to use for the account
       
  3205 @param aSmtpAccount The SMTP account to add the mobility account to
       
  3206 @param aLinkedSmtpAccountId Linked SMTP account ID
       
  3207 @param aReadOnly Flag to indicate if account is read only. Note that while read
       
  3208 only accounts should not be modified, we do not enforce this.
       
  3209 
       
  3210 @leave KErrArgument Account name too long
       
  3211 
       
  3212 @return SMTP mobility account
       
  3213 */
       
  3214 TSmtpMobilityAccount CEmailAccounts::DoCreateSmtpMobilityAccountL(const TDesC& aAccountName, const RArrayIAP& aIapList, const CImSmtpMobilitySettings& aSettings, const TSmtpAccount& aSmtpAccount, TSmtpAccountId aLinkedSmtpAccountId, TBool aReadOnly)
       
  3215 	{
       
  3216 	if (aAccountName.Length() > KEmailAccountNameSize)
       
  3217 		{
       
  3218 		User::Leave(KErrArgument);
       
  3219 		}
       
  3220 
       
  3221 	CRepository& repository = SmtpRepositoryL();
       
  3222 	TUint32 mobAccRepId = GetNextEmptyAccountSlotL(repository);
       
  3223 
       
  3224 	User::LeaveIfError(repository.StartTransaction(CRepository::EReadWriteTransaction));
       
  3225 	repository.CleanupRollbackTransactionPushL();
       
  3226 
       
  3227 	repository.Set(mobAccRepId + ESMTPServiceId, static_cast<TInt>(aSmtpAccount.iSmtpService));
       
  3228 	repository.Set(mobAccRepId + ESMTPAccountNameId, aAccountName);
       
  3229 	repository.Set(mobAccRepId + ESMTPRelatedServiceId, 0);
       
  3230 	repository.Set(mobAccRepId + ESMTPReadOnlyAccountId, static_cast<TInt>(aReadOnly));
       
  3231 	repository.Set(mobAccRepId + ESMTPParentAccountId, static_cast<TInt>(aSmtpAccount.iSmtpAccountId));
       
  3232 	repository.Set(mobAccRepId + ESMTPLinkedAccountId, static_cast<TInt>(aLinkedSmtpAccountId));
       
  3233 
       
  3234 	DoSaveSmtpSettingsL(mobAccRepId, aSettings.SmtpSettings());
       
  3235 
       
  3236 	AddMobilityAccountToSmtpAccountL(aSmtpAccount, aIapList, mobAccRepId, repository);
       
  3237 
       
  3238 	if (aLinkedSmtpAccountId != KNullSmtpAccountId)
       
  3239 		{
       
  3240 		AddLinkToMobilityAccountL(aLinkedSmtpAccountId, mobAccRepId, repository);
       
  3241 		}
       
  3242 
       
  3243 	CleanupStack::Pop();
       
  3244 	TUint32 keyInfo = 0;
       
  3245 	User::LeaveIfError(repository.CommitTransaction(keyInfo));
       
  3246 
       
  3247 	TSmtpMobilityAccount account;
       
  3248 	account.iAccountId = mobAccRepId >> KShiftAccountId;
       
  3249 	account.iName = aAccountName;
       
  3250 	account.iParentSmtpAccountId = aSmtpAccount.iSmtpAccountId;
       
  3251 	account.iLinkedSmtpAccountId = aLinkedSmtpAccountId;
       
  3252 
       
  3253 	return account;
       
  3254 	}
       
  3255 
       
  3256 /**
       
  3257 Adds a SMTP mobility account to a SMTP account
       
  3258 
       
  3259 @param aSmtpAccount SMTP account
       
  3260 @param aIapList IAP list associated with SMTP mobility account
       
  3261 @param aMobAccRepId SMTP mobility account repository ID
       
  3262 @param aRepository SMTP repository
       
  3263 */
       
  3264 void CEmailAccounts::AddMobilityAccountToSmtpAccountL(const TSmtpAccount& aSmtpAccount, const RArrayIAP& aIapList, TUint32 aMobAccRepId, CRepository& aRepository)
       
  3265 	{
       
  3266 	TInt iapListCount = aIapList.Count();
       
  3267 
       
  3268 	if (iapListCount < 1 || iapListCount > KMaxSmtpMobilityAccountIAPListEntries)
       
  3269 		{
       
  3270 		User::Leave(KErrArgument);
       
  3271 		}
       
  3272 
       
  3273 	TUint32 smtpAccRepId = aSmtpAccount.iSmtpAccountId << KShiftAccountId;
       
  3274 
       
  3275 	// Check the SMTP account actually exists
       
  3276 	TInt tempInt = 0;
       
  3277 	User::LeaveIfError(aRepository.Get(smtpAccRepId + ESMTPServiceId, tempInt));
       
  3278 
       
  3279 	TInt numEntries = 0;
       
  3280 	TInt err = aRepository.Get(smtpAccRepId + ESMTPMobilityAccountListNumEntriesId, numEntries);
       
  3281 	if (err == KErrNone)
       
  3282 		{
       
  3283 		if (numEntries >= ESMTPMobilityAccountMaxEntries)
       
  3284 			{
       
  3285 			User::Leave(KErrOverflow);
       
  3286 			}
       
  3287 
       
  3288 		// Check that none of the specified IAPs already exist for another entry
       
  3289 		if (FindIapListEntryInSmtpMobilityAccountListL(aIapList, aRepository, smtpAccRepId, numEntries, KNullSmtpMobilityAccountEntryPos) != KErrNotFound)
       
  3290 			{
       
  3291 			User::Leave(KErrAlreadyExists);
       
  3292 			}
       
  3293 		}
       
  3294 	else if (err != KErrNotFound)
       
  3295 		{
       
  3296 		User::Leave(err);
       
  3297 		}
       
  3298 
       
  3299 	User::LeaveIfError(aRepository.Set(smtpAccRepId + ESMTPMobilityAccountListVersionId, KSmtpMobilityAccountsListVersion));
       
  3300 
       
  3301 	User::LeaveIfError(aRepository.Set(smtpAccRepId + ESMTPMobilityAccountListNumEntriesId, numEntries + 1));
       
  3302 
       
  3303 	TUint32 offset = smtpAccRepId + (numEntries * ESMTPMobilityAccountListOffset);
       
  3304 
       
  3305 	User::LeaveIfError(aRepository.Set(offset + ESMTPMobilityAccountListAccountId, static_cast<TInt>(aMobAccRepId)));
       
  3306 	User::LeaveIfError(aRepository.Set(offset + ESMTPMobilityAccountListNumIapsId, iapListCount));
       
  3307 
       
  3308 	for (TInt iapLoop = 0; iapLoop < iapListCount; ++iapLoop)
       
  3309 		{
       
  3310 		User::LeaveIfError(aRepository.Set(offset + ESMTPMobilityAccountListIapId + iapLoop, static_cast<TInt>(aIapList[iapLoop])));
       
  3311 		}
       
  3312 	}
       
  3313 
       
  3314 /**
       
  3315 Add a link from a SMTP account to a linked SMTP mobility account
       
  3316 
       
  3317 @param aLinkedSmtpAccountId
       
  3318 
       
  3319 @leave KErrOverflow Maximum number of linked accounts already provisioned
       
  3320 */
       
  3321 void CEmailAccounts::AddLinkToMobilityAccountL(TSmtpAccountId aLinkedSmtpAccountId, TUint32 aMobAccRepId, CRepository& aRepository)
       
  3322 	{
       
  3323 	TUint32 smtpAccRepId = aLinkedSmtpAccountId << KShiftAccountId;
       
  3324 	TInt numEntries = 0;
       
  3325 	TInt err = aRepository.Get(smtpAccRepId + ESMTPLinkedMobilityAccountListNumEntriesId, numEntries);
       
  3326 	if (err == KErrNone || err == KErrNotFound)
       
  3327 		{
       
  3328 		if (numEntries < ESMTPLinkedMobilityAccountListMaxEntries)
       
  3329 			{
       
  3330 			User::LeaveIfError(aRepository.Set(smtpAccRepId + ESMTPLinkedMobilityAccountListVersionId, KSmtpLinkedMobilityAccountsListVersion));
       
  3331 			User::LeaveIfError(aRepository.Set(smtpAccRepId + ESMTPLinkedMobilityAccountListNumEntriesId, numEntries + 1));
       
  3332 			User::LeaveIfError(aRepository.Set(smtpAccRepId + ESMTPLinkedMobilityAccountListEntryId + numEntries, static_cast<TInt>(aMobAccRepId)));
       
  3333 			}
       
  3334 		else
       
  3335 			{
       
  3336 			User::Leave(KErrOverflow);
       
  3337 			}
       
  3338 		}
       
  3339 	else
       
  3340 		{
       
  3341 		User::Leave(err);
       
  3342 		}
       
  3343 	}
       
  3344 
       
  3345 /**
       
  3346 Finds an entry in a SMTP account's mobility account list which has an IAP
       
  3347 that matches one of the passed IAPs
       
  3348 
       
  3349 @param aIapList List of IAPs to try to match
       
  3350 @param aRepository SMTP repository
       
  3351 @param aSmtpAccRepId SMTP account repository ID
       
  3352 @param aNumEntries Number of entries in mobility account list
       
  3353 @param aIgnorePos Position of entry to ignore during the search.
       
  3354 
       
  3355 @return Position of entry in SMTP mobility account list with matching IAP or
       
  3356         KErrNotFound
       
  3357 */
       
  3358 TInt CEmailAccounts::FindIapListEntryInSmtpMobilityAccountListL(const RArrayIAP& aIapList, CRepository& aRepository, TUint32 aSmtpAccRepId, TInt aNumEntries, TInt aIgnorePos)
       
  3359 	{
       
  3360 	TUint32 offset = 0;
       
  3361 	TInt numRepositoryIaps = 0;
       
  3362 	TInt repositoryIap = 0;
       
  3363 
       
  3364 	for (TInt entryLoop = 0; entryLoop < aNumEntries; ++entryLoop)
       
  3365 		{
       
  3366 		// If this is the entry that we have been told to ignore, skip over it
       
  3367 		if (entryLoop == aIgnorePos)
       
  3368 			{
       
  3369 			continue;
       
  3370 			}
       
  3371 
       
  3372 		offset = aSmtpAccRepId + (entryLoop * ESMTPMobilityAccountListOffset);
       
  3373 
       
  3374 		User::LeaveIfError(aRepository.Get(offset + ESMTPMobilityAccountListNumIapsId, numRepositoryIaps));
       
  3375 
       
  3376 		for (TInt repositoryIapLoop = 0; repositoryIapLoop < numRepositoryIaps; ++repositoryIapLoop)
       
  3377 			{
       
  3378 			User::LeaveIfError(aRepository.Get(offset + ESMTPMobilityAccountListIapId + repositoryIapLoop, repositoryIap));
       
  3379 
       
  3380 			for (TInt iapListLoop = 0; iapListLoop < aIapList.Count(); ++iapListLoop)
       
  3381 				{
       
  3382 				if (aIapList[iapListLoop] == repositoryIap)
       
  3383 					{
       
  3384 					return entryLoop;
       
  3385 					}
       
  3386 				}
       
  3387 			}
       
  3388 		}
       
  3389 	
       
  3390 	return KErrNotFound;
       
  3391 	}
       
  3392 
       
  3393 /**
       
  3394 Finds a SMTP mobility account ID in the SMTP mobility account list for a
       
  3395 SMTP account
       
  3396 
       
  3397 @param aMobAccRepId SMTP mobility account repository ID
       
  3398 @param aRepository SMTP repository
       
  3399 @param aSmtpAccRepId SMTP account repository ID
       
  3400 @param aNumEntries Number of entries in SMTP mobility account list
       
  3401 
       
  3402 @return Position of entry in list or KErrNotFound
       
  3403 */
       
  3404 TInt CEmailAccounts::FindAccountIdInSmtpMobilityAccountListL(TUint32 aMobAccRepId, CRepository& aRepository, TUint32 aSmtpAccRepId, TInt aNumEntries)
       
  3405 	{
       
  3406 	TUint32 offset = 0;
       
  3407 	TInt tempInt = 0;
       
  3408 
       
  3409 	for (TInt entryLoop = 0; entryLoop < aNumEntries; ++entryLoop)
       
  3410 		{
       
  3411 		offset = aSmtpAccRepId + (entryLoop * ESMTPMobilityAccountListOffset);
       
  3412 
       
  3413 		User::LeaveIfError(aRepository.Get(offset + ESMTPMobilityAccountListAccountId, tempInt));
       
  3414 
       
  3415 		if (tempInt == aMobAccRepId)
       
  3416 			{
       
  3417 			return entryLoop;
       
  3418 			}
       
  3419 		}
       
  3420 
       
  3421 	return KErrNotFound;
       
  3422 	}
       
  3423 
       
  3424 /**
       
  3425 Gets SMTP Account information for a SMTP mobility account
       
  3426 
       
  3427 @param aMobAccRepId SMTP mobility account repository ID
       
  3428 @param aRepository SMTP repository
       
  3429 @param aSmtpAccRepId SMTP account repository ID
       
  3430 @param aPos On return holds the position of the SMTP mobility account in the
       
  3431        SMTP account's mobility account list.
       
  3432 
       
  3433 @leave KErrNotFound SMTP mobility account not found in list
       
  3434 */
       
  3435 void CEmailAccounts::GetSmtpAccountInfoForMobilityAccountL(TUint32 aMobAccRepId, CRepository& aRepository, TUint32& aSmtpAccRepId, TInt& aPos)
       
  3436 	{
       
  3437 	if (!IsSmtpMobilityAccount(aRepository, aMobAccRepId))
       
  3438 		{
       
  3439 		User::Leave(KErrNotFound);
       
  3440 		}
       
  3441 
       
  3442 	TInt tempInt = 0;
       
  3443 	User::LeaveIfError(aRepository.Get(aMobAccRepId + ESMTPParentAccountId, tempInt));
       
  3444 	aSmtpAccRepId = static_cast<TUint32>(tempInt) << KShiftAccountId;
       
  3445 
       
  3446 	TInt numEntries = 0;
       
  3447 	User::LeaveIfError(aRepository.Get(aSmtpAccRepId + ESMTPMobilityAccountListNumEntriesId, numEntries));
       
  3448 
       
  3449 	aPos = FindAccountIdInSmtpMobilityAccountListL(aMobAccRepId, aRepository, aSmtpAccRepId, numEntries);
       
  3450 	if (aPos == KErrNotFound)
       
  3451 		{
       
  3452 		User::Leave(KErrNotFound);
       
  3453 		}
       
  3454 	}
       
  3455 
       
  3456 /**
       
  3457 Deletes all the SMTP mobility accounts that are owned by a given SMTP account
       
  3458 
       
  3459 @param aSmtpAccRepId SMTP account repository ID
       
  3460 @param aRepository SMTP repository
       
  3461 */
       
  3462 void CEmailAccounts::DeleteAllOwnedMobilityAccountsL(TUint32 aSmtpAccRepId, CRepository& aRepository)
       
  3463 	{
       
  3464 	aRepository.Delete(aSmtpAccRepId + ESMTPMobilityAccountListVersionId);
       
  3465 
       
  3466 	TInt numEntries = 0;
       
  3467 	TInt err = aRepository.Get(aSmtpAccRepId + ESMTPMobilityAccountListNumEntriesId, numEntries);
       
  3468 	if (err == KErrNone)
       
  3469 		{
       
  3470 		aRepository.Delete(aSmtpAccRepId + ESMTPMobilityAccountListNumEntriesId);
       
  3471 
       
  3472 		TUint32 offset = 0;
       
  3473 		TInt tempInt = 0;
       
  3474 		TInt numIaps = 0;
       
  3475 
       
  3476 		for (TInt entryLoop = 0; entryLoop < numEntries; ++entryLoop)
       
  3477 			{
       
  3478 			offset = aSmtpAccRepId + (entryLoop * ESMTPMobilityAccountListOffset);
       
  3479 
       
  3480 			User::LeaveIfError(aRepository.Get(offset + ESMTPMobilityAccountListNumIapsId, numIaps));
       
  3481 			aRepository.Delete(offset + ESMTPMobilityAccountListNumIapsId);
       
  3482 
       
  3483 			for (TInt iapLoop = 0; iapLoop < numIaps; ++iapLoop)
       
  3484 				{
       
  3485 				aRepository.Delete(offset + ESMTPMobilityAccountListIapId + iapLoop);
       
  3486 				}
       
  3487 
       
  3488 			User::LeaveIfError(aRepository.Get(offset + ESMTPMobilityAccountListAccountId, tempInt));
       
  3489 
       
  3490 			TUint32 mobAccRepId = tempInt;
       
  3491 
       
  3492 			DeleteSmtpMobilityAccountFromLinkedSmtpAccountL(mobAccRepId, aRepository);
       
  3493 
       
  3494 			DeleteSmtpSettings(mobAccRepId, aRepository);
       
  3495 			}
       
  3496 		}
       
  3497 	else if (err != KErrNotFound)
       
  3498 		{
       
  3499 		User::Leave(err);
       
  3500 		}
       
  3501 	}
       
  3502 
       
  3503 /**
       
  3504 Deletes all the SMTP mobility accounts that are linked to a given SMTP account
       
  3505 
       
  3506 @param aSmtpAccRepId SMTP account repository ID
       
  3507 @param aRepository SMTP repository
       
  3508 */
       
  3509 void CEmailAccounts::DeleteAllLinkedMobilityAccountsL(TUint32 aLinkedSmtpAccRepId, CRepository& aRepository)
       
  3510 	{
       
  3511 	aRepository.Delete(aLinkedSmtpAccRepId + ESMTPLinkedMobilityAccountListVersionId);
       
  3512 
       
  3513 	TInt numEntries = 0;
       
  3514 	TInt err = aRepository.Get(aLinkedSmtpAccRepId + ESMTPLinkedMobilityAccountListNumEntriesId, numEntries);
       
  3515 	if (err == KErrNone)
       
  3516 		{
       
  3517 		aRepository.Delete(aLinkedSmtpAccRepId + ESMTPLinkedMobilityAccountListNumEntriesId);
       
  3518 
       
  3519 		TUint32 mobAccRepId = 0;
       
  3520 		TUint32 parentSmtpAccRepId = 0;
       
  3521 		TInt tempInt = 0;
       
  3522 
       
  3523 		for (TInt entryLoop = 0; entryLoop < numEntries; ++entryLoop)
       
  3524 			{
       
  3525 			User::LeaveIfError(aRepository.Get(aLinkedSmtpAccRepId + ESMTPLinkedMobilityAccountListEntryId + entryLoop, tempInt));
       
  3526 			mobAccRepId = tempInt;
       
  3527 
       
  3528 			aRepository.Delete(aLinkedSmtpAccRepId + ESMTPLinkedMobilityAccountListEntryId + entryLoop);
       
  3529 
       
  3530 			User::LeaveIfError(aRepository.Get(mobAccRepId + ESMTPParentAccountId, tempInt));
       
  3531 			parentSmtpAccRepId = static_cast<TUint32>(tempInt) << KShiftAccountId;
       
  3532 
       
  3533 			DeleteSmtpMobilityAccountListEntryL(mobAccRepId, aRepository, parentSmtpAccRepId);
       
  3534 
       
  3535 			DeleteSmtpSettings(mobAccRepId, aRepository);
       
  3536 			}
       
  3537 		}
       
  3538 	else if (err != KErrNotFound)
       
  3539 		{
       
  3540 		User::Leave(err);
       
  3541 		}
       
  3542 	}
       
  3543 
       
  3544 /**
       
  3545 Deletes a SMTP mobility account entry from a SMTP account
       
  3546 
       
  3547 @param aMobAccRepId SMTP mobility account repository ID
       
  3548 @param aRepository SMTP repository
       
  3549 @param aSmtpAccRepId SMTP account repository ID
       
  3550 */
       
  3551 void CEmailAccounts::DeleteSmtpMobilityAccountListEntryL(TUint32 aMobAccRepId, CRepository& aRepository, TUint32 aSmtpAccRepId)
       
  3552 	{
       
  3553 	TInt numEntries = 0;
       
  3554 	TInt err = aRepository.Get(aSmtpAccRepId + ESMTPMobilityAccountListNumEntriesId, numEntries);
       
  3555 	if (err == KErrNone)
       
  3556 		{
       
  3557 		TInt entryPos = FindAccountIdInSmtpMobilityAccountListL(aMobAccRepId, aRepository, aSmtpAccRepId, numEntries);
       
  3558 
       
  3559 		if (entryPos != KErrNotFound)
       
  3560 			{
       
  3561 			TUint32 offset = 0;
       
  3562 			TUint32 nextOffset = 0;
       
  3563 			TInt tempInt = 0;
       
  3564 			TInt numIaps = 0;
       
  3565 
       
  3566 			// The current entry is deleted by moving every entry below it one
       
  3567 			// place up in the list
       
  3568 			for (TInt entryLoop = entryPos; entryLoop < numEntries; ++entryLoop)
       
  3569 				{
       
  3570 				offset = aSmtpAccRepId + (entryLoop * ESMTPMobilityAccountListOffset);
       
  3571 
       
  3572 				// Clear the current entry
       
  3573 				aRepository.Delete(offset + ESMTPMobilityAccountListAccountId);
       
  3574 				aRepository.Delete(offset + ESMTPMobilityAccountListNumIapsId);
       
  3575 				for (TInt iapLoop = 0; iapLoop < KMaxSmtpMobilityAccountIAPListEntries; ++iapLoop)
       
  3576 					{
       
  3577 					aRepository.Delete(offset + ESMTPMobilityAccountListIapId + iapLoop);
       
  3578 					}
       
  3579 
       
  3580 				// Copy next entry over the top of this one
       
  3581 				if (entryLoop < numEntries - 1)
       
  3582 					{
       
  3583 					nextOffset = offset + ESMTPMobilityAccountListOffset;
       
  3584 					User::LeaveIfError(aRepository.Get(nextOffset + ESMTPMobilityAccountListAccountId, tempInt));
       
  3585 					User::LeaveIfError(aRepository.Set(offset + ESMTPMobilityAccountListAccountId, tempInt));
       
  3586 
       
  3587 					User::LeaveIfError(aRepository.Get(nextOffset + ESMTPMobilityAccountListNumIapsId, numIaps));
       
  3588 					User::LeaveIfError(aRepository.Set(offset + ESMTPMobilityAccountListNumIapsId, numIaps));
       
  3589 					
       
  3590 					for (TInt iapLoop = 0; iapLoop < numIaps; ++iapLoop)
       
  3591 						{
       
  3592 						User::LeaveIfError(aRepository.Get(nextOffset + ESMTPMobilityAccountListIapId + iapLoop, tempInt));
       
  3593 						User::LeaveIfError(aRepository.Set(offset + ESMTPMobilityAccountListIapId + iapLoop, tempInt));
       
  3594 						}
       
  3595 					}
       
  3596 				}
       
  3597 
       
  3598 			User::LeaveIfError(aRepository.Set(aSmtpAccRepId + ESMTPMobilityAccountListNumEntriesId, numEntries - 1));
       
  3599 			}
       
  3600 		}
       
  3601 	else if (err != KErrNotFound)
       
  3602 		{
       
  3603 		User::Leave(err);
       
  3604 		}
       
  3605 	}
       
  3606 
       
  3607 /**
       
  3608 If this mobility account is linked to a SMTP account, remove it from the
       
  3609 list of linked accounts.
       
  3610 
       
  3611 @param aMobAccRepId SMTP mobility account repository ID
       
  3612 @param aRepository SMTP repository
       
  3613 */
       
  3614 void CEmailAccounts::DeleteSmtpMobilityAccountFromLinkedSmtpAccountL(TUint32 aMobAccRepId, CRepository& aRepository)
       
  3615 	{
       
  3616 	// If this mobility account is linked to a SMTP account, then we need to
       
  3617 	// remove it from the list of linked accounts maintained by that SMTP
       
  3618 	// account.
       
  3619 	TInt tempInt = 0;
       
  3620 	User::LeaveIfError(aRepository.Get(aMobAccRepId + ESMTPLinkedAccountId, tempInt));
       
  3621 
       
  3622 	if (tempInt != KNullSmtpAccountId)
       
  3623 		{
       
  3624 		// This mobility account is linked to a SMTP account
       
  3625 
       
  3626 		TUint32 smtpAccRepId = static_cast<TUint32>(tempInt) << KShiftAccountId;
       
  3627 
       
  3628 		TInt numEntries = 0;		
       
  3629 		User::LeaveIfError(aRepository.Get(smtpAccRepId + ESMTPLinkedMobilityAccountListNumEntriesId, numEntries));
       
  3630 
       
  3631 		for (TInt entryLoop = 0; entryLoop < numEntries; entryLoop++)
       
  3632 			{
       
  3633 			User::LeaveIfError(aRepository.Get(smtpAccRepId + ESMTPLinkedMobilityAccountListEntryId + entryLoop, tempInt));
       
  3634 
       
  3635 			// Check if the entry matches the account we are looking for
       
  3636 			if (aMobAccRepId == tempInt)
       
  3637 				{
       
  3638 				// This entry matches so remove it. We remove it by overwriting it with
       
  3639 				// the last entry in the list, and then deleting the last entry in the
       
  3640 				// list.
       
  3641 				if (entryLoop < numEntries - 1)
       
  3642 					{
       
  3643 					// Get last entry from the list and overwrite this entry
       
  3644 					User::LeaveIfError(aRepository.Get(smtpAccRepId + ESMTPLinkedMobilityAccountListEntryId + numEntries - 1, tempInt));
       
  3645 					User::LeaveIfError(aRepository.Set(smtpAccRepId + ESMTPLinkedMobilityAccountListEntryId + entryLoop, tempInt));
       
  3646 					}
       
  3647 
       
  3648 				// Delete last entry from list
       
  3649 				User::LeaveIfError(aRepository.Delete(smtpAccRepId + ESMTPLinkedMobilityAccountListEntryId + numEntries - 1));
       
  3650 
       
  3651 				User::LeaveIfError(aRepository.Set(smtpAccRepId + ESMTPLinkedMobilityAccountListNumEntriesId, numEntries - 1));
       
  3652 
       
  3653 				// Exit loop now as we have found the linked entry
       
  3654 				break;
       
  3655 				}
       
  3656 			}
       
  3657 		}
       
  3658 	}
       
  3659 
       
  3660 /**
       
  3661 Checks if an SMTP account or SMTP mobility account is read only
       
  3662 
       
  3663 @param aAccRepId SMTP account / SMTP mobility account ID
       
  3664 
       
  3665 @return ETrue if read only, EFalse if not read only
       
  3666 */
       
  3667 TBool CEmailAccounts::IsSmtpTypeAccountReadOnlyL(TUint32 aAccRepId)
       
  3668 	{
       
  3669 	TInt tempInt = 0;
       
  3670 	User::LeaveIfError(SmtpRepositoryL().Get(aAccRepId + ESMTPReadOnlyAccountId, tempInt));
       
  3671 	TBool isReadOnly = static_cast<TBool>(tempInt);
       
  3672 
       
  3673 	return isReadOnly;
       
  3674 	}
       
  3675 
       
  3676 /**
       
  3677 Checks if the account is a SMTP mobility account
       
  3678 
       
  3679 @param aRepository SMTP repository
       
  3680 @param aAccountId Account ID
       
  3681 
       
  3682 @return ETrue if it is a SMTP mobility account, EFalse if not
       
  3683 */
       
  3684 TBool CEmailAccounts::IsSmtpMobilityAccount(CRepository& aRepository, TUint32 aAccRepId)
       
  3685 	{
       
  3686  	TInt parent = 0;
       
  3687 
       
  3688 	if (aRepository.Get(aAccRepId + ESMTPParentAccountId, parent) == KErrNone)
       
  3689 		{
       
  3690 		if (parent != KNullSmtpAccountId)
       
  3691 			{
       
  3692 			return ETrue;
       
  3693 			}
       
  3694 		}
       
  3695 
       
  3696 	return EFalse;
       
  3697 	}
       
  3698 
       
  3699 
       
  3700 /** Tests whether all of the characters in the specified text can be represented 
       
  3701 in 7-bit ASCII. 
       
  3702 
       
  3703 @param aText Text to be tested.
       
  3704 @return ETrue if all of the characters in the specified text is printable, otherwise EFalse. */
       
  3705 TBool CEmailAccounts::IsAsciiPrintable(const TDesC16& aText)
       
  3706      {
       
  3707      const TInt KMax7BitValue = 126;
       
  3708      const TInt KMin7BitValue = 32;
       
  3709 
       
  3710      const TInt length = aText.Length();
       
  3711      const TText16* pText = aText.Ptr();
       
  3712 
       
  3713      for(TInt i=0; i<length; i++)
       
  3714 		{
       
  3715 		if  ((pText[i] > KMax7BitValue) || (pText[i] < KMin7BitValue))
       
  3716 			return EFalse;
       
  3717        	}
       
  3718 
       
  3719     // Is a valid printable ascii string
       
  3720     return ETrue;
       
  3721     }
       
  3722 
       
  3723 /** Tests whether all of the characters in the specified text can be represented 
       
  3724 in 7-bit ASCII. 
       
  3725 
       
  3726 @param aText Text to be tested.
       
  3727 @return ETrue if all of the characters in the specified text is printable, otherwise EFalse. */
       
  3728 TBool CEmailAccounts::IsAsciiPrintable(const TDesC8& aText)
       
  3729      {
       
  3730      const TInt KMax7BitValue = 126;
       
  3731      const TInt KMin7BitValue = 32;
       
  3732      
       
  3733      const TInt length = aText.Length();
       
  3734      const TText8* pText = aText.Ptr();
       
  3735 
       
  3736      for(TInt i=0; i<length; i++)
       
  3737 		{
       
  3738 		if  ((pText[i] > KMax7BitValue) || (pText[i] < KMin7BitValue))
       
  3739 			return EFalse;
       
  3740        	}
       
  3741 
       
  3742     // Is a valid printable ascii string
       
  3743     return ETrue;
       
  3744     }
       
  3745 
       
  3746 
       
  3747 /** Tests whether all of the characters in the specified text are printable UniCode.    
       
  3748     
       
  3749 @param aText Text to be tested.
       
  3750 @return ETrue if all of the characters in the specified text is printable, otherwise EFalse. */
       
  3751 TBool CEmailAccounts::IsUnicodePrintable(const TDesC16& aText)
       
  3752      {
       
  3753   	  const TText16* pText = aText.Ptr();
       
  3754       for(TInt i=0; i<aText.Length(); ++i)
       
  3755 		{
       
  3756 	     TChar aChar = pText[i];
       
  3757 	  	 if(!(aChar.IsPrint()))  
       
  3758 		  	{
       
  3759 			return EFalse;  
       
  3760 		  	}
       
  3761        	}
       
  3762 
       
  3763     // Is a valid printable unicode string
       
  3764     return ETrue;
       
  3765     }
       
  3766 
       
  3767 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
  3768 /**
       
  3769 * Send the header structure to message Server to create  header table.
       
  3770  
       
  3771 * @param  aMTMid  MTM ID for which header to be created.
       
  3772 * @return  None.
       
  3773 
       
  3774 */
       
  3775 void CEmailAccounts::CreateHeaderStoreL(TUid aMTMid)
       
  3776 	{
       
  3777 	CMsvHeaderStore* aHeader = CMsvHeaderStore::NewL(aMTMid ,SessionL());
       
  3778 	CleanupStack::PushL(aHeader);
       
  3779 	
       
  3780 	_LIT (KEmailHeadearversion , "Version");
       
  3781 	aHeader->AddFieldL(KEmailHeadearversion().AllocL(),EIntegerField);
       
  3782 
       
  3783 	_LIT (KReceiptAddress , "ReceiptAddress");
       
  3784 	aHeader->AddFieldL(KReceiptAddress().AllocL(),ETextField);
       
  3785 	
       
  3786 	_LIT (KImMsgId , "ImMsgId");
       
  3787 	aHeader->AddFieldL(KImMsgId().AllocL(),ETextField);
       
  3788 
       
  3789 	aHeader->AddFieldL(CMsvHeaderStore::EFrom);
       
  3790 
       
  3791 	_LIT (KReplyTo , "ReplyTo");
       
  3792 	aHeader->AddFieldL(KReplyTo().AllocL(),ETextField);
       
  3793 
       
  3794 	aHeader->AddFieldL(CMsvHeaderStore::ESubject);
       
  3795 
       
  3796 	_LIT (KiRemoteSize , "iRemoteSize");
       
  3797 	aHeader->AddFieldL(KiRemoteSize().AllocL(),EIntegerField);
       
  3798 
       
  3799 	aHeader->AddFieldL(CMsvHeaderStore::ETo);
       
  3800 
       
  3801 	aHeader->AddFieldL(CMsvHeaderStore::ECC);
       
  3802 
       
  3803 	aHeader->AddFieldL(CMsvHeaderStore::EBCC);
       
  3804 
       
  3805 	_LIT (KEncodingInfo , "EncodingInfo");
       
  3806 	aHeader->AddFieldL(KEncodingInfo().AllocL(),ETextField);
       
  3807 	
       
  3808 	//----------------------Used for forwarding/replying to an email ----------------------	
       
  3809 	
       
  3810 	_LIT (KResentMsgId , "ResentMsgId");
       
  3811 	aHeader->AddFieldL(KResentMsgId().AllocL(),ETextField);
       
  3812 	
       
  3813 	_LIT (KResentFrom , "ResentFrom");
       
  3814 	aHeader->AddFieldL(KResentFrom().AllocL(),ETextField);
       
  3815 	
       
  3816 	_LIT (KResentToRecipients , "ResentToRecipients");
       
  3817 	aHeader->AddFieldL(KResentToRecipients().AllocL(),ETextField);
       
  3818 	
       
  3819 	_LIT (KResentCcRecipients , "ResentCcRecipients");
       
  3820 	aHeader->AddFieldL(KResentCcRecipients().AllocL(),ETextField);
       
  3821 	
       
  3822 	_LIT (KResentBccRecipients , "ResentBccRecipients");
       
  3823 	aHeader->AddFieldL(KResentBccRecipients().AllocL(),ETextField);
       
  3824 
       
  3825 	_LIT (KInReplyTo , "InReplyTo");
       
  3826 	aHeader->AddFieldL(KInReplyTo().AllocL(),ETextField);
       
  3827 
       
  3828 	_LIT (KBodyEncoding , "BodyEncoding");
       
  3829 	aHeader->AddFieldL(KBodyEncoding().AllocL(),EIntegerField);
       
  3830 
       
  3831 	_LIT (KCharset , "Charset");
       
  3832 	aHeader->AddFieldL(KCharset().AllocL(),EIntegerField);
       
  3833 
       
  3834 	aHeader->CreateStoreL();
       
  3835 	
       
  3836 	CleanupStack::PopAndDestroy(aHeader);  //aHeader
       
  3837 	}
       
  3838 
       
  3839 #endif