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