ipsservices/ipssossettings/src/ipssetdataapi.cpp
branchRCL_3
changeset 25 3533d4323edc
child 26 968773a0b6ef
equal deleted inserted replaced
24:d189ee25cf9d 25:3533d4323edc
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 *  Description: This file implements class CIpsSetDataApi.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "emailtrace.h"
       
    19 #include <e32base.h>
       
    20 #include <e32cmn.h>
       
    21 #include <SendUiConsts.h>
       
    22 #include <pop3set.h>
       
    23 #include <imapset.h>
       
    24 #include <smtpset.h>
       
    25 #include <centralrepository.h>      // CRepository
       
    26 #include <iapprefs.h>
       
    27 // <cmail> #include <ApUtils.h> // CApUtils
       
    28 #include <commdb.h>
       
    29 #include <ipssossettings.rsg>
       
    30 #include <etel.h>                       // RTelServer
       
    31 #include <etelmm.h>                     // RMobilePhone
       
    32 #include <cemailaccounts.h>
       
    33 #include <txtrich.h>
       
    34 
       
    35 #include "ipssetdatastorer.h"
       
    36 #include "ipssetdata.h"
       
    37 #include "ipssetdatamanager.h"
       
    38 #include "ipssetdataextension.h"
       
    39 #include "ipssetdatastorer.h"
       
    40 #include "ipssetwizardadapter.h"
       
    41 #include "ipssetui.h"
       
    42 #include "ipssetdatactrlaccount.h"
       
    43 #include "ipssetutilsconsts.h"
       
    44 #include "ipsplgcommon.h"
       
    45 #include "ipssetdataapi.h"
       
    46 #include "ipssetwizardadapter.h"
       
    47 
       
    48 #include "ipssetwizardsettingscenrepkeys.h"
       
    49 #include "freestyleemailcenrepkeys.h"
       
    50 #include "ipssetdatasignature.h"
       
    51 
       
    52 const TInt KIpsDataApiMaxPassLen = 256;
       
    53 const TInt KWizardDataPopIndicator = 0;
       
    54 const TInt KWizardDataImapIndicator = 1;
       
    55 
       
    56 #ifdef __WINS__
       
    57 _LIT( KIMAEmulatorImei, "123456789012345" );
       
    58 #endif // __WINS__
       
    59 
       
    60 
       
    61 #ifdef _DEBUG
       
    62 _LIT( KIpsSetApi, "IpsSetApi");
       
    63 #endif
       
    64 // ----------------------------------------------------------------------------
       
    65 // ----------------------------------------------------------------------------
       
    66 EXPORT_C CIpsSetDataApi* CIpsSetDataApi::NewL( CMsvSession& aSession )
       
    67     {
       
    68     FUNC_LOG;
       
    69     CIpsSetDataApi* self = new(ELeave)CIpsSetDataApi( aSession );
       
    70     CleanupStack::PushL(self);
       
    71     self->ConstructL();
       
    72     CleanupStack::Pop(self);
       
    73     return self;
       
    74     }
       
    75 
       
    76 // ----------------------------------------------------------------------------
       
    77 // ----------------------------------------------------------------------------
       
    78 CIpsSetDataApi::CIpsSetDataApi( CMsvSession& aSession )
       
    79     : iSession( aSession ),
       
    80     iServerAddress( NULL )
       
    81     {
       
    82     FUNC_LOG;
       
    83     }
       
    84 
       
    85 // ----------------------------------------------------------------------------
       
    86 // ----------------------------------------------------------------------------
       
    87 void CIpsSetDataApi::ConstructL()
       
    88     {
       
    89     FUNC_LOG;
       
    90     }
       
    91 
       
    92 // ----------------------------------------------------------------------------
       
    93 // ----------------------------------------------------------------------------    
       
    94 CIpsSetDataApi::~CIpsSetDataApi()
       
    95     {
       
    96     FUNC_LOG;
       
    97     delete iServerAddress;
       
    98     }
       
    99 
       
   100 // ----------------------------------------------------------------------------
       
   101 // ----------------------------------------------------------------------------    
       
   102 EXPORT_C TInt CIpsSetDataApi::LaunchSettingsL( const TMsvId aMailboxId )
       
   103     {
       
   104     FUNC_LOG;
       
   105     CIpsSetUi* ui = CIpsSetUi::NewL( aMailboxId );
       
   106     TInt ret = ui->ExecuteLD( R_IPS_SET_SETTINGS_DIALOG );
       
   107 
       
   108     return ret;
       
   109     }
       
   110 
       
   111 // ----------------------------------------------------------------------------
       
   112 // ----------------------------------------------------------------------------    
       
   113 EXPORT_C TDesC& CIpsSetDataApi::GetServerAddressL( const CMsvEntry& aEntry )
       
   114     {
       
   115     FUNC_LOG;
       
   116     delete iServerAddress;
       
   117     iServerAddress = NULL;
       
   118     
       
   119     if( aEntry.Entry().iMtm.iUid == KSenduiMtmImap4UidValue )
       
   120         {
       
   121         CImImap4Settings* settings = new(ELeave) CImImap4Settings();
       
   122         CleanupStack::PushL( settings );
       
   123         
       
   124         TImapAccount imapAccountId;
       
   125         CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   126         accounts->GetImapAccountL( aEntry.Entry().Id(), imapAccountId );
       
   127         accounts->LoadImapSettingsL( imapAccountId, *settings );
       
   128         
       
   129         iServerAddress = HBufC::NewL( settings->ServerAddress().Length() );
       
   130         iServerAddress->Des().Copy( settings->ServerAddress() );
       
   131         
       
   132         CleanupStack::PopAndDestroy( 2, settings );
       
   133         }
       
   134     else if( aEntry.Entry().iMtm.iUid == KSenduiMtmPop3UidValue )
       
   135         {
       
   136         CImPop3Settings* settings = new(ELeave) CImPop3Settings();
       
   137         CleanupStack::PushL( settings );
       
   138         
       
   139         TPopAccount popAccountId;
       
   140         CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   141         accounts->GetPopAccountL( aEntry.Entry().Id(), popAccountId );
       
   142         accounts->LoadPopSettingsL( popAccountId, *settings );
       
   143         
       
   144         iServerAddress = HBufC::NewL( settings->ServerAddress().Length() );
       
   145         iServerAddress->Des().Copy( settings->ServerAddress() );
       
   146         
       
   147         CleanupStack::PopAndDestroy( 2, settings );
       
   148         }
       
   149     return *iServerAddress;
       
   150     }
       
   151     
       
   152 // ----------------------------------------------------------------------------
       
   153 // ----------------------------------------------------------------------------    
       
   154 EXPORT_C void CIpsSetDataApi::SetNewPasswordL( 
       
   155     CMsvEntry& aService, 
       
   156     const TDesC& aPassword )
       
   157     {
       
   158     FUNC_LOG;
       
   159     if ( aService.Entry().iMtm.iUid == KSenduiMtmPop3UidValue )
       
   160         {
       
   161         SetNewPop3PasswordL( aService, aPassword );
       
   162         }
       
   163     else if ( aService.Entry().iMtm.iUid == KSenduiMtmImap4UidValue )
       
   164         {
       
   165         SetNewImap4PasswordL( aService, aPassword );
       
   166         }
       
   167     else if ( aService.Entry().iMtm.iUid == KSenduiMtmSmtpUidValue )
       
   168         {
       
   169         CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   170 
       
   171         TBuf8<KIpsDataApiMaxPassLen> temp;
       
   172         temp.Copy( aPassword );
       
   173 
       
   174         SetNewSmtpPasswordL( aService.EntryId(), temp, *accounts );
       
   175         CleanupStack::PopAndDestroy( 1, accounts );
       
   176         }
       
   177     }
       
   178 
       
   179 // ----------------------------------------------------------------------------
       
   180 // function returns KErrNotSupported if wizard data is not 
       
   181 // not match for the uid parameter. All leaving function calls are
       
   182 // trapped to prevent KErrNotSupported error code to return
       
   183 // in case of real error situation
       
   184 // ----------------------------------------------------------------------------        
       
   185 EXPORT_C TInt CIpsSetDataApi::HandleMailboxCreation( 
       
   186     const TUid aUid, 
       
   187     CMsvSession& aSession )
       
   188     {
       
   189     FUNC_LOG;
       
   190     // NOTE: this function should not leave, CleanupStack not used
       
   191     
       
   192     _LIT( KFsBasicPluginStr, "fsp" );
       
   193     
       
   194     TInt error = KErrNone;
       
   195     CRepository* repository = NULL;
       
   196     CIpsSetWizardAdapter* adapter = NULL;
       
   197     TInt protocol = 0; // <cmail>
       
   198     TIpsSetUtilsTextPlain accountType;
       
   199     
       
   200     TRAP( error, repository = CRepository::NewL( KCRUidWizardSettings ) );
       
   201     if ( error != KErrNone )
       
   202         {
       
   203         error = KErrGeneral;
       
   204         }
       
   205     else
       
   206         {
       
   207         // protocol 0= POP, 1= IMAP
       
   208         error = repository->Get( 
       
   209             ECRKSetupWizAccountType, accountType );
       
   210         if ( error != KErrNone )
       
   211             {
       
   212             error = KErrNotSupported;
       
   213             }
       
   214         }
       
   215     
       
   216     // check that protocol match until go further...    
       
   217     TInt diff = accountType.Compare( KFsBasicPluginStr );
       
   218     if ( error == KErrNone && diff != 0 )
       
   219         {
       
   220         error = KErrNotSupported;
       
   221         }
       
   222     
       
   223     if ( error == KErrNone )
       
   224         {
       
   225         error = repository->Get( 
       
   226             ECRKPopImapProtocolIndicatorId, protocol );
       
   227         }
       
   228     
       
   229     if ( error != KErrNone )
       
   230         {
       
   231         error = KErrNotSupported;
       
   232         }
       
   233     else if ( error == KErrNone 
       
   234             && protocol == KWizardDataPopIndicator 
       
   235             && aUid.iUid == KSenduiMtmImap4UidValue )
       
   236         {
       
   237         error = KErrNotSupported;
       
   238         }
       
   239     else if ( error == KErrNone 
       
   240             && protocol == KWizardDataImapIndicator 
       
   241             && aUid.iUid == KSenduiMtmPop3UidValue )
       
   242         {
       
   243         error = KErrNotSupported;
       
   244         }
       
   245     
       
   246     if ( error == KErrNone )
       
   247         {
       
   248         TRAP( error, adapter = CIpsSetWizardAdapter::NewL(
       
   249             *repository, aSession ) );
       
   250         if ( error != KErrNone )
       
   251             {
       
   252             error = KErrGeneral;
       
   253             }
       
   254         }
       
   255     if ( error == KErrNone )
       
   256         {
       
   257         // before creating mailbox clear common wizard fields to prevent another 
       
   258         // ips plugin instances to create (dublicate) mailboxes
       
   259         TInt setErr = repository->Set( 
       
   260                     ECRKSetupWizAccountType, KNullDesC );
       
   261         TRAP( error, adapter->HandleWizardDataL() );
       
   262         if ( error != KErrNone )
       
   263             {
       
   264             error = KErrGeneral;
       
   265             }
       
   266         }
       
   267 
       
   268     delete repository;
       
   269     delete adapter;
       
   270     return error;
       
   271     }
       
   272 
       
   273 
       
   274     
       
   275 // ----------------------------------------------------------------------------
       
   276 // maybe these could be combined...SetNewImap4PasswordL & SetNewPop3PasswordL
       
   277 // ----------------------------------------------------------------------------        
       
   278 void CIpsSetDataApi::SetNewImap4PasswordL( 
       
   279     CMsvEntry& aService, 
       
   280     const TDesC& aPassword )
       
   281     {
       
   282     FUNC_LOG;
       
   283     CImImap4Settings* settings = new(ELeave) CImImap4Settings();
       
   284     CleanupStack::PushL( settings );
       
   285 
       
   286     TImapAccount imapAccountId;
       
   287     CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   288     accounts->GetImapAccountL( aService.Entry().Id(), imapAccountId );
       
   289     accounts->LoadImapSettingsL( imapAccountId, *settings );
       
   290     
       
   291     TBuf8<KIpsDataApiMaxPassLen> temp;
       
   292     temp.Copy( aPassword );
       
   293     settings->SetPasswordL( temp );
       
   294     accounts->SaveImapSettingsL( imapAccountId, *settings );
       
   295     
       
   296     // Possibly update also outgoing service password
       
   297     if ( IsOutgoingLoginSameAsIncomingL( 
       
   298          TIpsSetDataStorerParams( imapAccountId.iImapAccountId, KSenduiMtmImap4Uid ) ) )
       
   299         {
       
   300         SetNewSmtpPasswordL( imapAccountId.iSmtpService, temp, *accounts );
       
   301         }
       
   302     
       
   303     CleanupStack::PopAndDestroy( 2, settings );
       
   304     }
       
   305     
       
   306 // ----------------------------------------------------------------------------
       
   307 // ----------------------------------------------------------------------------    
       
   308 void CIpsSetDataApi::SetNewPop3PasswordL( 
       
   309     CMsvEntry& aService, 
       
   310     const TDesC& aPassword )
       
   311     {
       
   312     FUNC_LOG;
       
   313     CImPop3Settings* settings = new(ELeave) CImPop3Settings();
       
   314     CleanupStack::PushL( settings );
       
   315     
       
   316     TPopAccount popAccountId;
       
   317     CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   318     accounts->GetPopAccountL( aService.Entry().Id(), popAccountId );
       
   319     
       
   320     accounts->LoadPopSettingsL( popAccountId, *settings );
       
   321     
       
   322     TBuf8<KIpsDataApiMaxPassLen> temp;
       
   323     temp.Copy( aPassword );
       
   324     
       
   325     settings->SetPasswordL( temp );
       
   326     
       
   327     accounts->SavePopSettingsL( popAccountId, *settings );
       
   328     
       
   329     // Possibly update also outgoing service password
       
   330     if ( IsOutgoingLoginSameAsIncomingL(
       
   331          TIpsSetDataStorerParams( popAccountId.iPopAccountId, KSenduiMtmPop3Uid ) ) )
       
   332         {
       
   333         SetNewSmtpPasswordL( popAccountId.iSmtpService, temp, *accounts );
       
   334         }
       
   335 
       
   336     CleanupStack::PopAndDestroy( 2, settings );
       
   337     }
       
   338 
       
   339 //<cmail>
       
   340 //This method is not used anywhere
       
   341 // ----------------------------------------------------------------------------
       
   342 // ----------------------------------------------------------------------------   
       
   343 //  
       
   344 /*EXPORT_C TBool CIpsSetDataApi::IsSmtpAllowedToConnectL( TMsvId aSmtpService, TBool aIsRoaming,
       
   345                                                         CMsvSession& aSession )
       
   346     {
       
   347 	FUNC_LOG;
       
   348     // get correct imap or pop entry from smtp service
       
   349     TMsvId serv;
       
   350     TMsvEntry entry; 
       
   351     TInt error = KErrNone;
       
   352     error = aSession.GetEntry( aSmtpService, serv, entry );
       
   353     
       
   354     if( error == KErrNone )
       
   355         {
       
   356         error = aSession.GetEntry( entry.iRelatedId, serv, entry );
       
   357         }
       
   358     // service might be deleted
       
   359     if( error != KErrNone )
       
   360         {
       
   361         return EFalse;
       
   362         }
       
   363     
       
   364     CIpsSetData* setData = CIpsSetData::NewLC();    // << setData
       
   365     CIpsSetDataManager* dataMan = CIpsSetDataManager::NewLC( aSession );// << dataMan
       
   366     TRAP( error, dataMan->LoadEmailSettingsL( entry, *setData ) );
       
   367             
       
   368     if( error != KErrNone )
       
   369         {
       
   370         CleanupStack::PopAndDestroy( 2 ); // >>> setData, dataMan
       
   371         return EFalse;
       
   372         }
       
   373    
       
   374     TBool isAllowed( ETrue );
       
   375     CIpsSetDataExtension* extented = setData->ExtendedSettings();
       
   376     
       
   377     if( extented->AlwaysOnlineState() == EMailAoOff )
       
   378         {
       
   379         //isAllowed = EFalse;
       
   380         }
       
   381     else if ( aIsRoaming && extented->AlwaysOnlineState() == EMailAoHomeOnly )
       
   382         {
       
   383         //isAllowed = EFalse;
       
   384         }
       
   385     
       
   386     // Always ask check, do not check in emulator because
       
   387     // lan connection works only with always ask access point
       
   388 #ifndef __WINS__
       
   389     CImIAPPreferences* iapOut = setData->OutgoingIapPref();
       
   390     TImIAPChoice iapChoice = iapOut->IAPPreference( 0 );
       
   391     isAllowed = ( !iapChoice.iIAP == KIpsServiceIdNoServiceSpecified );
       
   392 #endif
       
   393     CleanupStack::PopAndDestroy( 2 );  //>>> setData, dataMan
       
   394     return isAllowed;
       
   395     }*/
       
   396 //</cmail>
       
   397 
       
   398 // ----------------------------------------------------------------------------
       
   399 // ----------------------------------------------------------------------------    
       
   400 EXPORT_C void CIpsSetDataApi::GoOnlineL( TMsvEntry /*aMailbox*/ )
       
   401     {
       
   402     FUNC_LOG;
       
   403     // remove this
       
   404     }
       
   405 
       
   406 // ----------------------------------------------------------------------------
       
   407 // ----------------------------------------------------------------------------    
       
   408 EXPORT_C void CIpsSetDataApi::GoOfflineL( TMsvEntry /*aMailbox*/ )
       
   409     {
       
   410     FUNC_LOG;
       
   411     // remove this
       
   412     }
       
   413     
       
   414 // ----------------------------------------------------------------------------
       
   415 // ----------------------------------------------------------------------------    
       
   416 EXPORT_C void CIpsSetDataApi::RemoveAccountL( 
       
   417     const TMsvEntry& aMailbox, 
       
   418     CMsvSession& aSession )
       
   419     {
       
   420     FUNC_LOG;
       
   421     
       
   422     // First let the data manager to delete the account.
       
   423     CIpsSetDataManager* manager = CIpsSetDataManager::NewL( aSession );
       
   424     
       
   425     TRAPD( err, manager->RemoveAccountL( aMailbox ) );
       
   426     
       
   427     delete manager;
       
   428     manager = NULL;
       
   429 
       
   430     if ( err != KErrNone )
       
   431         {
       
   432         __ASSERT_DEBUG(EFalse, User::Panic( 
       
   433             KIpsSetApi , KErrNotFound ) );
       
   434         }
       
   435     // Then update the max account number flag.
       
   436     CRepository* repository = NULL;
       
   437 
       
   438     TRAPD( err2, repository = CRepository::NewL( KCRUidWizardSettings ) );
       
   439 
       
   440     // Do not assume repository is always available and working correctly.
       
   441     if ( err2 == KErrNone )
       
   442         {
       
   443         //always reset the max account flag when a mailbox is deleted
       
   444         TInt err3 = repository->Set(
       
   445             ECRKMaxAccountsReached, EFalse );
       
   446 
       
   447        
       
   448         delete repository;
       
   449         repository = NULL;
       
   450         }
       
   451     }
       
   452     
       
   453 // ----------------------------------------------------------------------------
       
   454 // ----------------------------------------------------------------------------
       
   455 EXPORT_C TUint32 CIpsSetDataApi::CreateCenRepKeyL( 
       
   456         const TUint32 aAccountId,
       
   457         const TUid& aProtocol,
       
   458         const TUint32 aSettingKey )
       
   459     {
       
   460     FUNC_LOG;
       
   461     return IpsSetDataCtrlAccount::CreateSettingKey( 
       
   462         aAccountId, 
       
   463         aSettingKey, 
       
   464         aProtocol );    
       
   465     }
       
   466 
       
   467 // ----------------------------------------------------------------------------
       
   468 // ----------------------------------------------------------------------------
       
   469 EXPORT_C void CIpsSetDataApi::GetMailboxAddressL(
       
   470     const TMsvEntry& aMailboxEntry,
       
   471     HBufC*& aMailboxAddress )
       
   472     {
       
   473     FUNC_LOG;
       
   474     /*CIpsSetData* settingsData = CIpsSetData::NewLC();
       
   475     CIpsSetDataManager* dataManager = CIpsSetDataManager::NewLC( iSession );
       
   476     TRAP_IGNORE( 
       
   477         dataManager->LoadEmailSettingsL( aMailboxEntry, *settingsData ) );
       
   478       
       
   479     CIpsSetDataExtension* extentedSettings = settingsData->ExtendedSettings();
       
   480     
       
   481     aMailboxAddress = extentedSettings->EmailAddress().Alloc();
       
   482     CleanupStack::PopAndDestroy( 2, settingsData ); // dataManager   */
       
   483     
       
   484     CEmailAccounts* acc = CEmailAccounts::NewLC();
       
   485     TSmtpAccount smtpAcc;
       
   486     acc->GetSmtpAccountL( aMailboxEntry.iRelatedId , smtpAcc );
       
   487     CImSmtpSettings* smtpSet = new (ELeave) CImSmtpSettings();
       
   488     CleanupStack::PushL( smtpSet );
       
   489     acc->LoadSmtpSettingsL( smtpAcc, *smtpSet );
       
   490     aMailboxAddress = smtpSet->EmailAddress().AllocL();
       
   491     CleanupStack::PopAndDestroy( 2, acc );
       
   492     }
       
   493 
       
   494 // ----------------------------------------------------------------------------
       
   495 // ----------------------------------------------------------------------------
       
   496 EXPORT_C void CIpsSetDataApi::SaveSyncStatusL( TMsvEntry aMailbox, TInt aState )
       
   497     {
       
   498     FUNC_LOG;
       
   499     CIpsSetDataStorer* storer = CIpsSetDataStorer::NewLC();
       
   500     CIpsSetData* settingsData = CIpsSetData::NewLC();
       
   501     CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   502     TUint32 accountId;
       
   503     
       
   504     if( aMailbox.iMtm.iUid == KSenduiMtmImap4UidValue )
       
   505     	{
       
   506     	accounts->GetImapAccountL( aMailbox.Id(), settingsData->ImapAccount() );
       
   507     	accountId = settingsData->ImapAccount().iImapAccountId;
       
   508     	}
       
   509     else
       
   510     	{
       
   511     	accounts->GetPopAccountL( aMailbox.Id(), settingsData->PopAccount() );
       
   512     	accountId = settingsData->PopAccount().iPopAccountId;
       
   513     	}
       
   514     
       
   515     TInt error = storer->LoadExtendedSettings( 
       
   516     		TIpsSetDataStorerParams( accountId, aMailbox.iMtm ),
       
   517     		*settingsData->ExtendedSettings() );
       
   518 
       
   519     settingsData->ExtendedSettings()->SetSyncStatus( aState );
       
   520     
       
   521     error = storer->SaveExtendedSettings( *settingsData->ExtendedSettings() );
       
   522     
       
   523     CleanupStack::PopAndDestroy( 3, storer );
       
   524     }
       
   525     
       
   526 // ----------------------------------------------------------------------------
       
   527 // ----------------------------------------------------------------------------    
       
   528 EXPORT_C TInt CIpsSetDataApi::GetLastSyncStatusL( TMsvEntry aMailbox )
       
   529     {
       
   530     FUNC_LOG;
       
   531     CIpsSetDataStorer* storer = CIpsSetDataStorer::NewLC();
       
   532     CIpsSetData* settingsData = CIpsSetData::NewLC();
       
   533     CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   534     TUint32 accountId;
       
   535     
       
   536     if( aMailbox.iMtm.iUid == KSenduiMtmImap4UidValue )
       
   537     	{
       
   538     	accounts->GetImapAccountL( aMailbox.Id(), settingsData->ImapAccount() );
       
   539     	accountId = settingsData->ImapAccount().iImapAccountId;
       
   540     	}
       
   541     else
       
   542     	{
       
   543     	accounts->GetPopAccountL( aMailbox.Id(), settingsData->PopAccount() );
       
   544     	accountId = settingsData->PopAccount().iPopAccountId;
       
   545     	}
       
   546     
       
   547     TInt error = storer->LoadExtendedSettings( 
       
   548     		TIpsSetDataStorerParams( accountId, aMailbox.iMtm ),
       
   549     		*settingsData->ExtendedSettings() );
       
   550     
       
   551     TInt lastSyncStatus = settingsData->ExtendedSettings()->LastSyncStatus( );
       
   552     
       
   553     CleanupStack::PopAndDestroy( 3, storer );
       
   554     
       
   555     return lastSyncStatus;
       
   556     }
       
   557 
       
   558 // ----------------------------------------------------------------------------
       
   559 // ----------------------------------------------------------------------------
       
   560 EXPORT_C void CIpsSetDataApi::GetIMEIFromThePhoneL( TBuf<KIpsSetDataMaxPhoneIdLength> &aIMEI )
       
   561     {
       
   562     FUNC_LOG;
       
   563     // In Emulator version, the imei cannot be fetched, so the basic number
       
   564     // is used instead
       
   565     #ifndef __WINS__
       
   566 
       
   567         RTelServer telServer;
       
   568         User::LeaveIfError( telServer.Connect() );
       
   569         CleanupClosePushL(telServer);
       
   570 
       
   571         TInt numPhones = 0;
       
   572         User::LeaveIfError( telServer.EnumeratePhones( numPhones ) );
       
   573         if ( numPhones < 1 )
       
   574             {
       
   575             User::Leave( KErrNotFound );
       
   576             }
       
   577         RTelServer::TPhoneInfo info;
       
   578         User::LeaveIfError( telServer.GetPhoneInfo( 0, info ) );
       
   579         RMobilePhone mobilePhone;
       
   580         User::LeaveIfError( mobilePhone.Open( telServer, info.iName ) );
       
   581         CleanupClosePushL( mobilePhone );
       
   582 
       
   583         TUint32 identityCaps;
       
   584         User::LeaveIfError( mobilePhone.GetIdentityCaps( identityCaps ) );
       
   585 
       
   586         if ( identityCaps & RMobilePhone::KCapsGetSerialNumber )
       
   587             {
       
   588             TRequestStatus status;
       
   589             RMobilePhone::TMobilePhoneIdentityV1 mobilePhoneIdentity;
       
   590 
       
   591             mobilePhone.GetPhoneId( status, mobilePhoneIdentity );
       
   592 
       
   593             User::WaitForRequest( status );
       
   594             User::LeaveIfError( status.Int() );
       
   595 
       
   596             aIMEI.Copy( mobilePhoneIdentity.iSerialNumber );
       
   597             }
       
   598         else
       
   599             {
       
   600             User::Leave( KErrNotSupported );
       
   601             }
       
   602 
       
   603         CleanupStack::PopAndDestroy( &mobilePhone );
       
   604         CleanupStack::PopAndDestroy( &telServer );
       
   605     #else
       
   606         aIMEI.Copy( KIMAEmulatorImei );
       
   607     #endif // __WINS__
       
   608     }
       
   609 // ----------------------------------------------------------------------------
       
   610 // ----------------------------------------------------------------------------
       
   611 EXPORT_C void CIpsSetDataApi::LoadExtendedSettingsL(
       
   612         TMsvId aServiceId,
       
   613         CIpsSetDataExtension& aExtendedSettings )
       
   614     {
       
   615     FUNC_LOG;
       
   616     TMsvEntry mboxEntry;
       
   617     TMsvId dummy;
       
   618     User::LeaveIfError( iSession.GetEntry( aServiceId, dummy, mboxEntry ) );
       
   619     CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   620     TInt accountId( KErrNotFound );
       
   621     
       
   622     if ( mboxEntry.iMtm.iUid == KSenduiMtmImap4UidValue )
       
   623         {
       
   624         TImapAccount imapAccount;
       
   625         accounts->GetImapAccountL( mboxEntry.Id(), imapAccount );
       
   626         accountId = imapAccount.iImapAccountId;
       
   627         }
       
   628     else if ( mboxEntry.iMtm.iUid == KSenduiMtmPop3UidValue )
       
   629         {
       
   630         TPopAccount popAccount;
       
   631         accounts->GetPopAccountL( mboxEntry.Id(), popAccount );
       
   632         accountId = popAccount.iPopAccountId;
       
   633         }
       
   634     else
       
   635         {
       
   636         __ASSERT_DEBUG(EFalse, User::Panic( 
       
   637             KIpsSetApi , KErrNotFound ) );
       
   638         }
       
   639     
       
   640     CIpsSetDataStorer* storer = CIpsSetDataStorer::NewLC();
       
   641     storer->LoadExtendedSettingsL(
       
   642             accountId, mboxEntry.iMtm, aExtendedSettings );
       
   643     
       
   644     CleanupStack::PopAndDestroy( 2, accounts );
       
   645     }
       
   646 
       
   647 // ----------------------------------------------------------------------------
       
   648 // ----------------------------------------------------------------------------
       
   649 EXPORT_C void CIpsSetDataApi::SaveExtendedSettingsL(
       
   650         const CIpsSetDataExtension& aExtendedSettings )
       
   651     {
       
   652     FUNC_LOG;
       
   653     CIpsSetDataStorer* storer = CIpsSetDataStorer::NewLC();
       
   654     storer->SaveExtendedSettingsL( aExtendedSettings );
       
   655     
       
   656     CleanupStack::PopAndDestroy( storer );
       
   657     }
       
   658 
       
   659 // ----------------------------------------------------------------------------
       
   660 // ----------------------------------------------------------------------------
       
   661 EXPORT_C void CIpsSetDataApi::ConstructImapPartialFetchInfo( 
       
   662         TImImap4GetPartialMailInfo& aInfo, CImImap4Settings& aImap4Settings )
       
   663 
       
   664     {
       
   665     FUNC_LOG;
       
   666     TInt sizeLimit = aImap4Settings.BodyTextSizeLimit();
       
   667     
       
   668 	
       
   669     CRepository* centRep = NULL;
       
   670     TRAP_IGNORE( centRep = CRepository::NewL( KFreestyleEmailCenRep ) );
       
   671     
       
   672     TInt limit( 0 );
       
   673     TInt err = centRep->Get( KFreestyleMaxBodySize , limit );
       
   674 	limit *= KKilo;
       
   675 	if ( limit == 0 || err )
       
   676         {
       
   677         limit = KMaxTInt;
       
   678         }
       
   679     
       
   680     if ( sizeLimit == KIpsSetDataHeadersOnly )
       
   681         {
       
   682         aInfo.iTotalSizeLimit = KIpsSetDataHeadersOnly;
       
   683         }
       
   684     else if ( sizeLimit == KIpsSetDataFullBodyAndAttas )
       
   685         {        
       
   686         aInfo.iTotalSizeLimit = KMaxTInt;
       
   687         aInfo.iAttachmentSizeLimit = KMaxTInt;
       
   688         aInfo.iBodyTextSizeLimit = limit;
       
   689         aInfo.iMaxEmailSize = KMaxTInt;
       
   690         aInfo.iPartialMailOptions = ENoSizeLimits;
       
   691         aInfo.iGetMailBodyParts = EGetImap4EmailBodyTextAndAttachments;
       
   692         }
       
   693     else if ( sizeLimit == KIpsSetDataFullBodyOnly )
       
   694         {
       
   695         aInfo.iTotalSizeLimit = KMaxTInt; 
       
   696         aInfo.iAttachmentSizeLimit = 0;
       
   697         aInfo.iBodyTextSizeLimit = limit;
       
   698         aInfo.iMaxEmailSize = KMaxTInt;
       
   699         aInfo.iPartialMailOptions = EBodyAlternativeText;
       
   700         aInfo.iGetMailBodyParts = EGetImap4EmailBodyAlternativeText;
       
   701         }
       
   702     else
       
   703         {
       
   704         aInfo.iTotalSizeLimit = sizeLimit*1024; 
       
   705         // set zero when it not documentated does total size overrides these 
       
   706         aInfo.iAttachmentSizeLimit = 0;
       
   707         aInfo.iMaxEmailSize = sizeLimit*1024;
       
   708         aInfo.iBodyTextSizeLimit = Min(sizeLimit*1024, limit);
       
   709         aInfo.iPartialMailOptions = EBodyAlternativeText;
       
   710         aInfo.iGetMailBodyParts = EGetImap4EmailBodyAlternativeText;
       
   711         }
       
   712     }
       
   713 
       
   714 // ----------------------------------------------------------------------------
       
   715 // ----------------------------------------------------------------------------
       
   716 EXPORT_C void CIpsSetDataApi::GetSubscribedImapFoldersL( 
       
   717         TMsvId aServiceId, RArray<TMsvId>& aFoldersArray )
       
   718     {
       
   719     FUNC_LOG;
       
   720     CMsvEntry* cEntry = iSession.GetEntryL( aServiceId );
       
   721     CleanupStack::PushL( cEntry );
       
   722     CMsvEntrySelection* foldersUnderService = cEntry->ChildrenWithTypeL( 
       
   723             KUidMsvFolderEntry );
       
   724     CleanupStack::PushL( foldersUnderService );
       
   725     CIpsSetDataApi::GetImapChildFoldersL( 
       
   726             iSession, *foldersUnderService, aFoldersArray );
       
   727     CleanupStack::PopAndDestroy( foldersUnderService );
       
   728     CleanupStack::PopAndDestroy( cEntry );
       
   729     }
       
   730 
       
   731 // ----------------------------------------------------------------------------
       
   732 // ----------------------------------------------------------------------------
       
   733 EXPORT_C void CIpsSetDataApi::SetMailboxNameL( 
       
   734         TMsvEntry& aService, 
       
   735         const TDesC& aMailboxName)
       
   736     {
       
   737     FUNC_LOG;
       
   738     CIpsSetData* settingsData = CIpsSetData::NewLC();
       
   739     CIpsSetDataManager* dataManager = CIpsSetDataManager::NewLC( iSession );
       
   740     TRAP_IGNORE( 
       
   741         dataManager->LoadEmailSettingsL( aService, *settingsData ) );
       
   742     
       
   743     settingsData->SetMailboxName( aMailboxName );
       
   744     
       
   745     TRAP_IGNORE( dataManager->SaveEmailSettingsL( *settingsData ) );
       
   746     
       
   747     CleanupStack::PopAndDestroy( 2, settingsData ); // dataManager
       
   748     }
       
   749 
       
   750 // ----------------------------------------------------------------------------
       
   751 // ----------------------------------------------------------------------------
       
   752 EXPORT_C HBufC* CIpsSetDataApi::SignatureTextL( const TMsvEntry& aService )
       
   753     {
       
   754     FUNC_LOG;
       
   755     HBufC* signatureText( NULL );
       
   756     CIpsSetData* settingsData = CIpsSetData::NewLC();
       
   757     CIpsSetDataManager* dataManager = CIpsSetDataManager::NewLC( iSession );
       
   758     dataManager->LoadEmailSettingsL( aService, *settingsData );
       
   759     // the IncludeSignature returns zero in case that inclusion is ON..
       
   760     if ( !settingsData->IncludeSignature() )
       
   761         {
       
   762         CRichText* rText = settingsData->Signature().iRichText;
       
   763         if ( rText )
       
   764             {
       
   765             signatureText = HBufC::NewL( KIpsSetUiMaxSettingsSignatureLength );
       
   766             TPtr sPtr = signatureText->Des();
       
   767             rText->Extract( sPtr, 0, KIpsSetUiMaxSettingsSignatureLength );
       
   768             }
       
   769         }
       
   770     CleanupStack::PopAndDestroy( 2, settingsData ); // dataManager
       
   771     return signatureText;
       
   772     }
       
   773 
       
   774 // ----------------------------------------------------------------------------
       
   775 // ----------------------------------------------------------------------------
       
   776 void CIpsSetDataApi::GetImapChildFoldersL(
       
   777         CMsvSession& aSession,
       
   778         const CMsvEntrySelection& aFolders, 
       
   779         RArray<TMsvId>& aFillArray )
       
   780     {
       
   781     FUNC_LOG;
       
   782     for ( TInt folderLoop=0; folderLoop<aFolders.Count(); folderLoop++ )
       
   783         {
       
   784         TBool match( EFalse );
       
   785         TMsvEntry tEntry;
       
   786         TMsvId service;
       
   787         aSession.GetEntry( aFolders.At(folderLoop), service, tEntry );
       
   788         TMsvEmailEntry emailEntry( tEntry );
       
   789 
       
   790         if( emailEntry.LocalSubscription() )
       
   791             {
       
   792             for( TInt arrayLoop=0; arrayLoop<aFillArray.Count(); arrayLoop++ )
       
   793                 {
       
   794                 if( aFillArray[arrayLoop] == emailEntry.Id() )
       
   795                     {
       
   796                     match = ETrue;
       
   797                     }
       
   798                 }
       
   799             
       
   800             if( !aFillArray.Count() || !match )
       
   801                 {
       
   802                 aFillArray.AppendL( aFolders[folderLoop] );
       
   803                 }
       
   804             }
       
   805         }
       
   806     
       
   807     for ( TInt i = 0; i < aFolders.Count(); i++ )
       
   808         {
       
   809         CMsvEntry* cEntry = aSession.GetEntryL( aFolders[i] );
       
   810         CleanupStack::PushL( cEntry );
       
   811         CMsvEntrySelection* folders = cEntry->ChildrenWithTypeL( 
       
   812                 KUidMsvFolderEntry );
       
   813         CleanupStack::PopAndDestroy( cEntry );
       
   814         CleanupStack::PushL( folders );
       
   815         // recursive
       
   816         GetImapChildFoldersL( aSession, *folders, aFillArray );
       
   817         CleanupStack::PopAndDestroy( folders );
       
   818         }
       
   819     }
       
   820 
       
   821 // ----------------------------------------------------------------------------
       
   822 // ----------------------------------------------------------------------------
       
   823 TBool CIpsSetDataApi::IsOutgoingLoginSameAsIncomingL(
       
   824     const TIpsSetDataStorerParams& aParams )
       
   825     {
       
   826     FUNC_LOG;
       
   827     CIpsSetDataStorer* storer = CIpsSetDataStorer::NewLC();
       
   828     CIpsSetDataExtension* extSettings = CIpsSetDataExtension::NewLC();
       
   829     User::LeaveIfError( storer->LoadExtendedSettings(
       
   830         TIpsSetDataStorerParams( aParams.AccountId(), aParams.MtmId() ),
       
   831         *extSettings ) );
       
   832     TInt outgoingLogin = extSettings->OutgoingLogin();
       
   833     CleanupStack::PopAndDestroy( 2 ); // extSettings, storer
       
   834     return outgoingLogin == 1; // == Same as incoming
       
   835     }
       
   836 
       
   837 // ----------------------------------------------------------------------------
       
   838 // ----------------------------------------------------------------------------
       
   839 void CIpsSetDataApi::SetNewSmtpPasswordL(
       
   840     const TMsvId aSmtpServiceId,
       
   841     const TDesC8& aPassword,
       
   842     CEmailAccounts& aEmailAccounts )
       
   843     {
       
   844     FUNC_LOG;
       
   845     CImSmtpSettings* smtpSettings = new(ELeave) CImSmtpSettings();
       
   846     CleanupStack::PushL( smtpSettings );
       
   847     TSmtpAccount smtpAccount;
       
   848     aEmailAccounts.GetSmtpAccountL( aSmtpServiceId, smtpAccount );
       
   849     aEmailAccounts.LoadSmtpSettingsL( smtpAccount, *smtpSettings );
       
   850     smtpSettings->SetPasswordL( aPassword );
       
   851     aEmailAccounts.SaveSmtpSettingsL( smtpAccount, *smtpSettings );
       
   852     CleanupStack::PopAndDestroy( smtpSettings );
       
   853     }
       
   854 
       
   855 // End of File
       
   856