emailservices/emailserver/cmailhandlerplugin/src/cmailcpssettings.cpp
branchRCL_3
changeset 64 3533d4323edc
child 80 726fba06891a
equal deleted inserted replaced
63:d189ee25cf9d 64:3533d4323edc
       
     1 /*
       
     2 * Copyright (c) 2008 - 2010 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:  handles interface to CenRep settings
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include <e32base.h>
       
    21 // CRepository
       
    22 #include <centralrepository.h>
       
    23 #include <connect/sbdefs.h>
       
    24 
       
    25 // Email Framework APIs
       
    26 //<cmail>
       
    27 #include "cfsmailcommon.h"
       
    28 #include "cfsmailclient.h"
       
    29 #include "cfsmailbox.h"
       
    30 #include "cfsmailfolder.h"
       
    31 //</cmail>
       
    32 
       
    33 #include "cmailcpssettings.h"
       
    34 #include "cmailwidgetcenrepkeysinternal.h"
       
    35 #include "cmailcpsifconsts.h"
       
    36 #include "cmailexternalaccount.h"
       
    37 
       
    38 // ======== MEMBER FUNCTIONS ========
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // CMailCpsSettings::NewL
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 CMailCpsSettings* CMailCpsSettings::NewL( CFSMailClient& aMailClient )
       
    45     {
       
    46     FUNC_LOG;
       
    47     CMailCpsSettings* self =  new(ELeave) CMailCpsSettings( aMailClient );
       
    48     CleanupStack::PushL( self );
       
    49     self->ConstructL();
       
    50     CleanupStack::Pop( self );
       
    51     return self;
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CMailCpsSettings::~CMailCpsSettings
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 CMailCpsSettings::~CMailCpsSettings()
       
    59     {
       
    60     FUNC_LOG;
       
    61     Cancel();
       
    62     iMailboxArray.Close();
       
    63     delete iBackupRestoreSubscriber;
       
    64     delete iCenRep;
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // CMailCpsSettings::CMailCpsSettings
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 CMailCpsSettings::CMailCpsSettings( CFSMailClient& aMailClient ) :
       
    72     CActive( EPriorityStandard ),
       
    73     iMailClient( aMailClient ),
       
    74     iCenRep( NULL ),
       
    75     iConfigData( 0 ),
       
    76     iRestoreStarted( EFalse ),
       
    77     iBackupOngoing( EFalse )
       
    78     {
       
    79     FUNC_LOG;
       
    80     CActiveScheduler::Add( this );
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // CMailCpsSettings::ConstructL
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 void CMailCpsSettings::ConstructL()
       
    88     {
       
    89     FUNC_LOG;
       
    90     // In case there is no repository available, following call will leave
       
    91     // Trapping is done by MailServer infrastructure, not by CPS handler
       
    92     // In practice, this is fatal to cps handling, and widget won't work
       
    93     iCenRep = CRepository::NewL( KCRUidCmailWidget );
       
    94     iBackupRestoreSubscriber = CPSSubscriber::NewL(
       
    95         *this, KUidSystemCategory, conn::KUidBackupRestoreKey );
       
    96     iBackupRestoreSubscriber->Subscribe();
       
    97     LoadSettingsL(); // mailboxes etc. user changeable data
       
    98     LoadConfigurationL(); // internal configuration data
       
    99     }
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // CMailCpsSettings::RunL
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 void CMailCpsSettings::RunL()
       
   106     {
       
   107     FUNC_LOG;
       
   108     if ( !BackupOrRestoreMode() )
       
   109         {
       
   110         StartObservingL();
       
   111         LoadSettingsL(); // mailboxes etc. user changeable data
       
   112         LoadConfigurationL(); // internal configuration data
       
   113         iObserver->SettingsChangedCallback();
       
   114         }
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // CMailCpsSettings::DoCancel
       
   119 // ---------------------------------------------------------------------------
       
   120 //
       
   121 void CMailCpsSettings::DoCancel()
       
   122     {
       
   123     FUNC_LOG;
       
   124     // Always returns KErrNone
       
   125     iCenRep->NotifyCancel( KCmailPartialKey, KCmailMask );
       
   126     }
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // CMailCpsSettings::LoadSettingsL
       
   130 // 
       
   131 // synchronizes the iMailboxArray with Central repository settings
       
   132 // only existing mailboxes are synchronized
       
   133 // usecases 
       
   134 // 1. delete widget from homesceen - removed item from cenrep array
       
   135 // 2. add widget to homescreen - added item to the end of cenrep array
       
   136 // 3. change mailbox for widget - changed value of cenrep array item 
       
   137 // 4. remove mailbox from server - mailbox absence for array item 
       
   138 // 5. add mailbox to the server (leads to widget adding)
       
   139 // ---------------------------------------------------------------------------
       
   140 
       
   141 void CMailCpsSettings::LoadSettingsL()
       
   142     {
       
   143     FUNC_LOG;
       
   144     TInt ret( KErrNone );
       
   145 
       
   146     // mailbox keys
       
   147     RArray<TUint> cenrepMailboxes; 
       
   148     CleanupClosePushL( cenrepMailboxes );
       
   149     RArray<TBool> cenrepMailboxesExistence; 
       
   150     CleanupClosePushL( cenrepMailboxesExistence );
       
   151 
       
   152     // cenrep keys
       
   153     RArray<TUint32> keys; 
       
   154     CleanupClosePushL( keys );
       
   155     GetMailboxNonZeroKeysL( keys );
       
   156     TInt dMax( keys.Count() );
       
   157     
       
   158     cenrepMailboxes.ReserveL( dMax ); // cenrep mailbox ids array
       
   159     cenrepMailboxesExistence.ReserveL( dMax ); // auxiliary if it was found in iMailboxArray looping
       
   160     
       
   161     // initialize array of mailbox keys
       
   162     TInt value( 0 );
       
   163     TInt i; // main loop
       
   164     TInt j; // auxiliary / inner loop
       
   165     for ( i = 0; i < dMax; i++ )
       
   166         {
       
   167          User::LeaveIfError( ret = iCenRep->Get( keys[i], value ));
       
   168          cenrepMailboxes.AppendL( static_cast<TUint>(value) );
       
   169          cenrepMailboxesExistence.AppendL( EFalse );
       
   170         }
       
   171     CleanupStack::PopAndDestroy(&keys); 
       
   172     
       
   173     // Sync array of cenrep keys with iMailboxArray
       
   174     // 1. remove from iMailboxArray what is not in cenrep, mark what is. Distinct existence. 
       
   175     // 2. remove mailbox ids not detected on the server
       
   176     TInt dFound( KErrNotFound );
       
   177     for ( i = iMailboxArray.Count()-1; i >= 0; i -- ) // iArray loop
       
   178         {
       
   179         dFound = cenrepMailboxes.Find( iMailboxArray[i].Id() );
       
   180         if ( KErrNotFound != dFound ) // if found in cenrep
       
   181             { 
       
   182                  // mailbox is in iMailboxArray and in cenrep => check provider
       
   183             INFO_1("Mailbox both in cenrep and iMailboxArray: i = %d ", i );
       
   184             if ( ! cenrepMailboxesExistence[dFound] )
       
   185                 { 
       
   186                 ret = CheckMailboxExistence( iMailboxArray[i] );
       
   187                 if ( KErrNotFound == ret)
       
   188                     {
       
   189                     // mailbox was removed from provider => remove from cenrep also
       
   190                     // cenrepMailboxes indexed the same way as keys => finding key not needed 
       
   191                     ret = iCenRep->Reset( KCMailMailboxIdBase + i );
       
   192                     ret = iCenRep->Reset( KCMailPluginIdBase + i );
       
   193                     ret = iCenRep->Reset( KCMailWidgetContentIdBase + i );
       
   194                     INFO_1("Mailbox removed from cenrep: dFound %d ", dFound );
       
   195                     // remove also from all arrays
       
   196                     iMailboxArray.Remove(i);
       
   197                     cenrepMailboxes.Remove( dFound );
       
   198                     cenrepMailboxesExistence.Remove( dFound );
       
   199                     dMax--;
       
   200                     }
       
   201                 else 
       
   202                     {
       
   203                     User::LeaveIfError(ret); // for instance if no memory
       
   204                     INFO_1("Mailbox provider check ok: dFound = %d ", dFound );
       
   205                     cenrepMailboxesExistence[dFound] = ETrue; // not remove to ensure indexes are the same as in keys
       
   206                     // handle also possible mailbox duplications
       
   207                     for (j = dFound+1; j<dMax; j++)
       
   208                         {
       
   209                         if ( cenrepMailboxes[dFound] == cenrepMailboxes[j] )
       
   210                             { 
       
   211                             cenrepMailboxesExistence[j] = ETrue; 
       
   212                             }
       
   213                         }
       
   214                     }
       
   215                  } // else already tested for existence
       
   216             }
       
   217         else // not found in cenrep
       
   218             {
       
   219             // mailbox was removed from cenrep => remove from iArray as well
       
   220             iMailboxArray.Remove(i);
       
   221             INFO_1("Mailbox removed from iMailboxArray: i = %d ", i );
       
   222             }
       
   223         } // iArray loop
       
   224     
       
   225     // Add mailboxes existing only in cenrep to iMailboxArray in correct order
       
   226     // or check and correct order of mailboxes (swap already dot checked, copy duplicated)
       
   227     for ( i = 0; i < dMax; i++ )
       
   228         {
       
   229         // firstly new mailboxes in cenrep needs to be added to iMailboxArray
       
   230         if ( ! cenrepMailboxesExistence[i] ) 
       
   231             {
       
   232             TFSMailMsgId mailbox; 
       
   233             // Find mailbox by this unefficient function because cenrep does not hold pluginID
       
   234             if ( KErrNone == ResolveMailbox( cenrepMailboxes[i], mailbox ) )
       
   235                 {
       
   236                 iMailboxArray.InsertL( mailbox, i );
       
   237                 INFO_1("Mailbox added to iMailboxArray: i = %d ", i );
       
   238                 // ensured iMailboxArray[i].Id() == cenrepMailboxes[i]
       
   239                 }
       
   240             else // KErrNotFound
       
   241                 {
       
   242                 // remove not valid mailbox from cenrep arrays
       
   243                 cenrepMailboxes.Remove( i );
       
   244                 cenrepMailboxesExistence.Remove( i );
       
   245                 // remove from cenrep
       
   246                 ret = iCenRep->Reset( KCMailMailboxIdBase + i );
       
   247                 ret = iCenRep->Reset( KCMailPluginIdBase + i );
       
   248                 ret = iCenRep->Reset( KCMailWidgetContentIdBase + i );
       
   249                 INFO_1("Mailbox removed from cenrep: i = %d ", i );
       
   250                 // update loop control expression
       
   251                 dMax--;
       
   252                 i--;
       
   253                 }
       
   254             continue; // ensured equation or removed item
       
   255             } // end of if mailbox not in cenrep
       
   256 
       
   257         // Check if iMailboxArray is in correct order on i position
       
   258         TInt jMax( iMailboxArray.Count() );
       
   259         j = i; // assume iMailboxArray[i].Id() == cenrepMailboxes[i]
       
   260         if ( ( i < jMax ) && ( iMailboxArray[i].Id() != cenrepMailboxes[i] ) )
       
   261             { // not in correct order try to swap
       
   262             for ( j = i+1; j < jMax; j++ )
       
   263                 {
       
   264                 if ( iMailboxArray[j].Id() == cenrepMailboxes[i] )
       
   265                     { // swap is better from efficiency point of view
       
   266                     TFSMailMsgId tmp( iMailboxArray[i] ); 
       
   267                     iMailboxArray[i] = iMailboxArray[j]; 
       
   268                     iMailboxArray[j] = tmp; 
       
   269                     break; // j < jMax here
       
   270                     }
       
   271                 }
       
   272             // as previous code does not detect existence of repeating id occurence
       
   273             // the loop may not find the repeated mailbox id - then j=jMax
       
   274             } 
       
   275         // if cenrep has multiplied mailbox widgets then add them 
       
   276         if ( j >= jMax ) // swapping did not help or item is missing
       
   277             {
       
   278             // if i=j=jMax then missing duplicated iArray element
       
   279             // if j>i then iArray contains not used element
       
   280             // two widgets shows the same mailbox then take already used iMailboxArray value
       
   281             dFound = cenrepMailboxes.Find( cenrepMailboxes[i] );
       
   282             if ( dFound < i )
       
   283                 {
       
   284                 // the arrays are synchronized below up to i, duplication to correct i place
       
   285                 iMailboxArray.InsertL( iMailboxArray[dFound], i ); 
       
   286                 }
       
   287             else
       
   288                 {
       
   289                 // unable to synchronize the arrays - algorithm is wrong
       
   290                 INFO_1("CMAIL: CMailCpsSettings::LoadSettingsL() : FATAL ALGORITHM ERROR - ARRAY NOT SYNCHRONIZED  i = %d ", i );
       
   291                 User::Leave( KErrNotFound );
       
   292                 }
       
   293             } // end of j >= jMax 
       
   294         
       
   295         } // end check order for i = 0..dMax loop 
       
   296 
       
   297     // if iMailboxArray has more items than cenrepMailboxes then remove these items
       
   298     for( i = iMailboxArray.Count()-1; i >= dMax; i-- )
       
   299         {
       
   300         iMailboxArray.Remove(i);
       
   301         }
       
   302 
       
   303     CleanupStack::PopAndDestroy(&cenrepMailboxesExistence);
       
   304     CleanupStack::PopAndDestroy(&cenrepMailboxes);
       
   305     }
       
   306 
       
   307 // ---------------------------------------------------------------------------
       
   308 // CMailCpsSettings::LoadConfigurationL
       
   309 // ---------------------------------------------------------------------------
       
   310 //
       
   311 void CMailCpsSettings::LoadConfigurationL()
       
   312     {
       
   313     FUNC_LOG;
       
   314     TInt ret( KErrNone );
       
   315     TInt value( 0 );
       
   316     ret = iCenRep->Get( KCmailCPConfiguration, value );
       
   317     if ( !ret )
       
   318         {
       
   319         INFO_2("iConfigData: %d -> %d", iConfigData, value);
       
   320         iConfigData = static_cast<TInt32>( value );
       
   321         }
       
   322     }
       
   323 
       
   324 // ---------------------------------------------------------------------------
       
   325 // CMailCpsSettings::GetMailboxNonZeroKeysL
       
   326 // ---------------------------------------------------------------------------
       
   327 //
       
   328 void CMailCpsSettings::GetMailboxNonZeroKeysL( RArray<TUint32>& aKeys )
       
   329     {
       
   330     FUNC_LOG;
       
   331     TInt ret = iCenRep->FindNeqL( KCmailPartialKeyRange, KCmailRangeMask, 0, aKeys );
       
   332     if ( ret != KErrNone && ret != KErrNotFound )
       
   333         {
       
   334         User::Leave( ret );
       
   335         }
       
   336     }
       
   337 
       
   338 // ---------------------------------------------------------------------------
       
   339 // CMailCpsSettings::GetMailboxNonZeroKeysL
       
   340 // ---------------------------------------------------------------------------
       
   341 //
       
   342 void CMailCpsSettings::GetExtMailboxNonZeroKeysL( RArray<TUint32>& aKeys )
       
   343     {
       
   344     FUNC_LOG;
       
   345     TInt ret = iCenRep->FindNeqL( KCmailExtMailboxKeyRange, KCmailExtMailboxRangeMask, 0, aKeys );
       
   346     if ( ret != KErrNone && ret != KErrNotFound )
       
   347         {
       
   348         User::Leave( ret );
       
   349         }
       
   350     }
       
   351 
       
   352 // ---------------------------------------------------------------------------
       
   353 // CMailCpsSettings::ResolveMailbox
       
   354 // ---------------------------------------------------------------------------
       
   355 //
       
   356 TInt CMailCpsSettings::ResolveMailbox( const TInt aMailboxId, TFSMailMsgId& aMsg )
       
   357     {
       
   358     FUNC_LOG;
       
   359     RPointerArray<CFSMailBox> mailboxarray;
       
   360     TInt err = iMailClient.ListMailBoxes( TFSMailMsgId(), mailboxarray );
       
   361 	INFO_1("CMAIL CMailCpsSettings::ResolveMailbox():: ListMailBoxes() returns %d", err);
       
   362     if( !err ) // KErrNone = 0
       
   363         {
       
   364         err = KErrNotFound;
       
   365         aMsg.SetId( aMailboxId );
       
   366         const TInt iiMax( mailboxarray.Count() );
       
   367         for ( TInt ii = 0; ii < iiMax; ii++ )
       
   368             {
       
   369             if ( mailboxarray[ii]->GetId().Id() == aMsg.Id() )
       
   370                 {
       
   371                 // Mailbox found
       
   372                 aMsg.SetPluginId( mailboxarray[ii]->GetId().PluginId() );
       
   373                 err = KErrNone; 
       
   374                 break;
       
   375                 }
       
   376             }
       
   377         }
       
   378     mailboxarray.ResetAndDestroy();
       
   379     return err;
       
   380     }
       
   381 
       
   382 TInt CMailCpsSettings::CheckMailboxExistence( TFSMailMsgId& aMsg )
       
   383     {
       
   384     FUNC_LOG;
       
   385     CFSMailBox *mbox( NULL );
       
   386     TRAPD(err, mbox = iMailClient.GetMailBoxByUidL(aMsg));
       
   387     if ( mbox ) // mail box exists
       
   388         {
       
   389         delete mbox;
       
   390         return KErrNone;
       
   391         }
       
   392     if ( ( KErrNotFound == err ) || ( KErrNone == err ) )
       
   393         {
       
   394     // mail box not exists, chek if it is not in different plugin 
       
   395     // very safe, maybe return KErrNotFound would be enough
       
   396         return ResolveMailbox( aMsg.Id(), aMsg );
       
   397         }
       
   398     return err;
       
   399     }
       
   400 
       
   401 
       
   402 
       
   403 
       
   404 // ---------------------------------------------------------------------------
       
   405 // CMailCpsSettings::StartObservingL
       
   406 // ---------------------------------------------------------------------------
       
   407 //
       
   408 void CMailCpsSettings::StartObservingL( MMailCpsSettingsCallback* aObserver )
       
   409     {
       
   410     FUNC_LOG;
       
   411     iObserver = aObserver;
       
   412     StartObservingL();
       
   413     }
       
   414 
       
   415 // ---------------------------------------------------------------------------
       
   416 // CMailCpsSettings::StartObservingL
       
   417 // ---------------------------------------------------------------------------
       
   418 //
       
   419 void CMailCpsSettings::StartObservingL()
       
   420     {
       
   421     FUNC_LOG;
       
   422     TInt ret = iCenRep->NotifyRequest( KCmailPartialKey, KCmailMask, iStatus );
       
   423     if ( ret )
       
   424         {
       
   425         User::Leave( ret );
       
   426         }
       
   427     SetActive();
       
   428     }
       
   429 
       
   430 // ---------------------------------------------------------------------------
       
   431 // CMailCpsSettings::StopObserving
       
   432 // ---------------------------------------------------------------------------
       
   433 //
       
   434 void CMailCpsSettings::StopObserving()
       
   435     {
       
   436     FUNC_LOG;
       
   437     Cancel();
       
   438     // observer not owned by settings
       
   439     iObserver = NULL;
       
   440     }
       
   441 
       
   442 // ---------------------------------------------------------------------------
       
   443 // CMailCpsSettings::Mailboxes
       
   444 // ---------------------------------------------------------------------------
       
   445 //
       
   446 RArray<TFSMailMsgId>& CMailCpsSettings::Mailboxes()
       
   447     {
       
   448     FUNC_LOG;
       
   449     return iMailboxArray;
       
   450     }
       
   451 
       
   452 // ---------------------------------------------------------------------------
       
   453 // CMailCpsSettings::ExternalMailboxes
       
   454 // ---------------------------------------------------------------------------
       
   455 //
       
   456 void CMailCpsSettings::GetExtMailboxesL( RPointerArray<CMailExternalAccount>& aAccounts )
       
   457     {
       
   458     FUNC_LOG;
       
   459     RArray<TUint32> keys;
       
   460     CleanupClosePushL( keys );
       
   461     GetExtMailboxNonZeroKeysL( keys );
       
   462 
       
   463     for ( TInt i = 0; i < keys.Count(); i++ )
       
   464         {
       
   465         CMailExternalAccount* account = GetExtMailboxL( keys[i] );
       
   466         CleanupStack::PushL( account );
       
   467         aAccounts.AppendL( account );
       
   468         CleanupStack::Pop( account );
       
   469         }
       
   470 
       
   471     CleanupStack::PopAndDestroy(&keys); // keys
       
   472     }
       
   473 
       
   474 // ---------------------------------------------------------------------------
       
   475 // CMailCpsSettings::GetExtMailboxL
       
   476 // ---------------------------------------------------------------------------
       
   477 //
       
   478 CMailExternalAccount* CMailCpsSettings::GetExtMailboxL( TInt aKey )
       
   479     {
       
   480     FUNC_LOG;
       
   481     TInt mailboxId( 0 );
       
   482     TInt pluginId( 0 );
       
   483     HBufC* contentIdBuf = HBufC::NewL( KMaxDescLen );
       
   484     TPtr contentId = contentIdBuf->Des();
       
   485 
       
   486     User::LeaveIfError( iCenRep->Get( aKey, mailboxId ) );
       
   487     User::LeaveIfError( iCenRep->Get( aKey + KCMailExtPluginIdOffset, pluginId ) );
       
   488     User::LeaveIfError( iCenRep->Get( aKey + KCMailExtWidgetCidOffset, contentId ) );
       
   489 
       
   490     return CMailExternalAccount::NewL(
       
   491         mailboxId, pluginId, contentIdBuf );
       
   492     }
       
   493 
       
   494 // ---------------------------------------------------------------------------
       
   495 // CMailCpsSettings::AddMailboxL
       
   496 // ---------------------------------------------------------------------------
       
   497 //
       
   498 void CMailCpsSettings::AddMailboxL( const TFSMailMsgId aMailbox )
       
   499     {
       
   500     FUNC_LOG;
       
   501     
       
   502     // Ensure message is not already in settings
       
   503     RArray<TUint32> keys;
       
   504     CleanupClosePushL( keys );
       
   505     GetMailboxNonZeroKeysL( keys );
       
   506     const TInt iiMax( keys.Count() );
       
   507     for ( TInt ii = 0; ii < iiMax; ii++ )
       
   508         {
       
   509         TInt value( 0 );
       
   510         iCenRep->Get( keys[ii], value );
       
   511         if( value == aMailbox.Id() )
       
   512             {
       
   513             // mailbox already in settings, simply return
       
   514             CleanupStack::PopAndDestroy( &keys );
       
   515             return;
       
   516             }
       
   517         }
       
   518 
       
   519     // Add mailbox to settings
       
   520     keys.Reset();
       
   521     TInt ret = iCenRep->FindEqL( KCmailPartialKeyRange, KCmailRangeMask, 0, keys );
       
   522     if ( ret != KErrNone && ret != KErrNotFound )
       
   523         {
       
   524         User::Leave( ret );
       
   525         }
       
   526     if( keys.Count() > 0 )
       
   527         {
       
   528         // Casting uint32 to int32 below is safe because following is always true:
       
   529         // aMailbox.Id() < 0x7FFFFFFF
       
   530         iCenRep->Set( keys[0], static_cast<TInt>( aMailbox.Id() ) );
       
   531         }
       
   532     else
       
   533         {
       
   534         }
       
   535 
       
   536     CleanupStack::PopAndDestroy( &keys ); // keys 
       
   537 
       
   538     }
       
   539 
       
   540 // ---------------------------------------------------------------------------
       
   541 // CMailCpsSettings::RemoveMailboxL
       
   542 // ---------------------------------------------------------------------------
       
   543 //
       
   544 void CMailCpsSettings::RemoveMailboxL( const TFSMailMsgId aMailbox )
       
   545     {
       
   546     FUNC_LOG;
       
   547     TInt iiMax( iMailboxArray.Count() );
       
   548     for ( TInt ii = 0; ii < iiMax; ii++ )
       
   549         {
       
   550         if( iMailboxArray[ii].Id() == aMailbox.Id() )
       
   551             {
       
   552             TBuf<KMaxDescLen> cid;
       
   553             GetContentId( aMailbox.Id(), 1, cid );
       
   554             DissociateWidgetFromSettingL( cid );
       
   555             iMailboxArray.Remove( ii );
       
   556             break;
       
   557             }
       
   558         }
       
   559     }
       
   560 
       
   561 // ---------------------------------------------------------------------------
       
   562 // CMailCpsSettings::MaxRowCount
       
   563 // ---------------------------------------------------------------------------
       
   564 //
       
   565 TInt CMailCpsSettings::MaxRowCount()
       
   566     {
       
   567     FUNC_LOG;
       
   568     return KMaxRowCount;
       
   569     }
       
   570 
       
   571 // ---------------------------------------------------------------------------
       
   572 // CMailCpsSettings::MaxMailboxCount
       
   573 // ---------------------------------------------------------------------------
       
   574 //
       
   575 TInt CMailCpsSettings::MaxMailboxCount()
       
   576     {
       
   577     FUNC_LOG;
       
   578     return KMaxMailboxCount;
       
   579     }
       
   580 
       
   581 // ---------------------------------------------------------------------------
       
   582 // CMailCpsSettings::AssociateWidgetToSetting
       
   583 // ---------------------------------------------------------------------------
       
   584 //
       
   585 void CMailCpsSettings::AssociateWidgetToSetting( const TDesC& aContentId,
       
   586                                                  const TFSMailMsgId aMailbox )
       
   587     {
       
   588     FUNC_LOG;
       
   589     TInt setId (GetSettingToAssociate(aContentId));
       
   590     if (setId>=0)
       
   591         {
       
   592         TInt id = aMailbox.Id();
       
   593         TUid pluginId = aMailbox.PluginId();
       
   594         TInt pId(pluginId.iUid);
       
   595         iCenRep->Set( KCMailMailboxIdBase+setId, id );
       
   596         iCenRep->Set( KCMailPluginIdBase+setId, pId );
       
   597         iCenRep->Set( KCMailWidgetContentIdBase+setId, aContentId );        
       
   598         }
       
   599     }
       
   600 
       
   601 // ---------------------------------------------------------------------------
       
   602 // CMailCpsSettings::DissociateWidgetFromSettingL
       
   603 // ---------------------------------------------------------------------------
       
   604 //
       
   605 void CMailCpsSettings::DissociateWidgetFromSettingL( const TDesC& aContentId )
       
   606     {
       
   607     FUNC_LOG;
       
   608     for ( TInt i = 0; i < KMaxMailboxCount; i++ )
       
   609         {
       
   610         TBuf<KMaxDescLen> value;
       
   611         TUint32 key( KCMailWidgetContentIdBase + i );
       
   612         iCenRep->Get( key, value );
       
   613         TInt result = value.Compare(aContentId);
       
   614         if (!result)
       
   615             {
       
   616             iCenRep->Reset( key );
       
   617             iCenRep->Reset( KCMailMailboxIdBase + i );
       
   618             iCenRep->Reset( KCMailPluginIdBase + i );
       
   619             break;
       
   620             }
       
   621         }        
       
   622     }
       
   623 
       
   624 // ---------------------------------------------------------------------------
       
   625 // CMailCpsSettings::GetSettingToAssociate
       
   626 // ---------------------------------------------------------------------------
       
   627 //
       
   628 TInt CMailCpsSettings::GetSettingToAssociate( const TDesC& aContentId )
       
   629     {
       
   630     FUNC_LOG;    
       
   631     TInt ret( KErrNotFound );
       
   632     
       
   633     for ( TInt i = 0; i < KMaxMailboxCount; i++ )
       
   634         {
       
   635         TUint32 key( KCMailWidgetContentIdBase+ i );
       
   636         TInt err = iCenRep->Get( key, iCenrepText );
       
   637         if ( err == KErrNone )
       
   638             {
       
   639             if ( iCenrepText.Compare( aContentId ) == 0 )
       
   640                 {
       
   641                 ret = i;
       
   642                 break;
       
   643                 }
       
   644             }
       
   645         }
       
   646     
       
   647     if ( ret < 0 )
       
   648         {
       
   649         for ( TInt i = 0; i < KMaxMailboxCount; i++ )
       
   650             {       
       
   651             TUint32 key( KCMailWidgetContentIdBase + i );
       
   652             TInt err = iCenRep->Get( key, iCenrepText );
       
   653             if ( err == KErrNone )
       
   654                 {
       
   655                 if ( iCenrepText.Compare( KDissociated ) == 0 )
       
   656                     {
       
   657                     ret = i;
       
   658                     break;
       
   659                     }
       
   660                 }
       
   661             }
       
   662         }
       
   663     
       
   664     return ret;
       
   665     }
       
   666 
       
   667 
       
   668 // ---------------------------------------------------------------------------
       
   669 // CMailCpsSettings::Associated
       
   670 // ---------------------------------------------------------------------------
       
   671 //
       
   672 TBool CMailCpsSettings::Associated( const TDesC& aContentId )
       
   673     {
       
   674     FUNC_LOG;    
       
   675     TBool ret( EFalse );
       
   676     
       
   677     for ( TInt i = 0; i < KMaxMailboxCount; i++ )
       
   678         {
       
   679         TUint32 key( KCMailWidgetContentIdBase + i );
       
   680         TInt err = iCenRep->Get( key, iCenrepText );
       
   681         if ( err == KErrNone )
       
   682             {
       
   683             if ( iCenrepText.Compare( aContentId ) == 0 )
       
   684                 {
       
   685                 ret = ETrue;
       
   686                 break;
       
   687                 }
       
   688             }
       
   689         }
       
   690     
       
   691     return ret;
       
   692     }
       
   693 
       
   694 // ---------------------------------------------------------------------------
       
   695 // CMailCpsSettings::GetContentId
       
   696 // ---------------------------------------------------------------------------
       
   697 //
       
   698 void CMailCpsSettings::GetContentId( TInt aMailboxId, TInt aNext, TDes16& aValue )
       
   699     {
       
   700     FUNC_LOG;
       
   701     TInt found(0);
       
   702     for (TInt i = 0; i < KMaxMailboxCount; i++)
       
   703         {       
       
   704         TInt value;
       
   705         TUint32 mailboxKey(KCMailMailboxIdBase+i);
       
   706         iCenRep->Get( mailboxKey, value );     
       
   707         if (aMailboxId == value)
       
   708             {
       
   709             found++;
       
   710             if ( found == aNext )
       
   711                 {
       
   712                 iCenRep->Get( KCMailWidgetContentIdBase+i, aValue );
       
   713                 break;
       
   714                 }            
       
   715             }
       
   716         }
       
   717     }
       
   718 
       
   719 // ---------------------------------------------------------------------------
       
   720 // CMailCpsSettings::WidgetCountByMailbox
       
   721 // ---------------------------------------------------------------------------
       
   722 //
       
   723 TInt CMailCpsSettings::WidgetCountByMailbox( TInt aMailboxId )
       
   724     {
       
   725     FUNC_LOG;
       
   726     TInt ret(0);
       
   727     for (TInt i = 0; i < KMaxMailboxCount; i++)
       
   728         {       
       
   729         TInt value;
       
   730         TUint32 mailboxKey(KCMailMailboxIdBase+i);
       
   731         iCenRep->Get( mailboxKey, value );     
       
   732         if (aMailboxId == value)
       
   733             {
       
   734             ret++;
       
   735             }
       
   736         }
       
   737     return ret;
       
   738     }
       
   739 
       
   740 // ---------------------------------------------------------------------------
       
   741 // CMailCpsSettings::IsSelected
       
   742 // ---------------------------------------------------------------------------
       
   743 //
       
   744 TBool CMailCpsSettings::IsSelected( TInt aId )
       
   745     {
       
   746     FUNC_LOG;    
       
   747     TBool ret(EFalse);
       
   748     for (TInt i = 0; i < KMaxMailboxCount; i++)
       
   749         {       
       
   750         TInt value;
       
   751         TUint32 mailboxKey(KCMailMailboxIdBase+i);
       
   752         iCenRep->Get( mailboxKey, value );
       
   753         if (aId == value)
       
   754             {
       
   755             ret = ETrue;
       
   756             break;
       
   757             }
       
   758         }
       
   759     return ret;
       
   760     }
       
   761 
       
   762 
       
   763 // ---------------------------------------------------------------------------
       
   764 // CMailCpsSettings::GetMailboxUidByContentId
       
   765 // ---------------------------------------------------------------------------
       
   766 //
       
   767 TUint CMailCpsSettings::GetMailboxUidByContentId( const TDesC& aContentId )
       
   768     {
       
   769     FUNC_LOG;
       
   770     
       
   771     TInt ret( KErrNone );
       
   772     for ( TInt i = 0; i < KMaxMailboxCount; i++ )
       
   773         {       
       
   774         TUint32 key( KCMailWidgetContentIdBase + i );
       
   775         TInt err = iCenRep->Get( key, iCenrepText );
       
   776         if ( err == KErrNone )
       
   777             {
       
   778             if ( iCenrepText.Compare( aContentId ) == 0 )
       
   779                 {
       
   780                 TUint32 key2( KCMailMailboxIdBase + i );
       
   781                 err = iCenRep->Get( key2, ret );
       
   782                 if ( err == KErrNone )
       
   783                     {
       
   784                     break;
       
   785                     }
       
   786                 }
       
   787             }
       
   788         }
       
   789     
       
   790     return ret;
       
   791     }
       
   792 
       
   793 
       
   794 
       
   795 // ---------------------------------------------------------------------------
       
   796 // CMailCpsSettings::GetPluginUidByContentId
       
   797 // ---------------------------------------------------------------------------
       
   798 //
       
   799 TUint CMailCpsSettings::GetPluginUidByContentId( const TDesC& aContentId )
       
   800     {
       
   801     FUNC_LOG;
       
   802   
       
   803     TInt ret( KErrNone );
       
   804     for ( TInt i = 0; i < KMaxMailboxCount; i++ )
       
   805         {       
       
   806         TUint32 key( KCMailWidgetContentIdBase + i );
       
   807         TInt err = iCenRep->Get( key, iCenrepText );
       
   808         if ( err == KErrNone )
       
   809             {
       
   810             if ( iCenrepText.Compare( aContentId ) == 0 )
       
   811                 {
       
   812                 TUint32 key2( KCMailPluginIdBase + i );
       
   813                 err = iCenRep->Get( key2, ret );
       
   814                 if ( err == KErrNone )
       
   815                     {
       
   816                     break;
       
   817                     }
       
   818                 }
       
   819             }
       
   820         }
       
   821     
       
   822     return ret;
       
   823     }
       
   824 
       
   825 
       
   826 // ---------------------------------------------------------------------------
       
   827 // CMailCpsSettings::Configuration
       
   828 // ---------------------------------------------------------------------------
       
   829 //
       
   830 TInt32 CMailCpsSettings::Configuration()
       
   831     {
       
   832     FUNC_LOG;
       
   833     return iConfigData;
       
   834     }
       
   835 
       
   836 // ---------------------------------------------------------------------------
       
   837 // CMailCpsSettings::TotalIntMailboxCount
       
   838 // ---------------------------------------------------------------------------
       
   839 //
       
   840 TInt CMailCpsSettings::TotalIntMailboxCount()
       
   841     {
       
   842     FUNC_LOG;
       
   843     RPointerArray<CFSMailBox> mailboxarray;
       
   844     iMailClient.ListMailBoxes( TFSMailMsgId(), mailboxarray );    
       
   845     TInt ret = mailboxarray.Count(); 
       
   846     mailboxarray.ResetAndDestroy();
       
   847     return ret;
       
   848     }
       
   849 
       
   850 // -----------------------------------------------------------------------------
       
   851 // CMailCpsSettings::ToggleWidgetNewMailIconL
       
   852 // -----------------------------------------------------------------------------
       
   853 void CMailCpsSettings::ToggleWidgetNewMailIconL( TBool aIconOn, const TFSMailMsgId& aMailBox )
       
   854     {
       
   855     FUNC_LOG;
       
   856  
       
   857     _LIT( KFormat, "%S%d%S" );
       
   858     const TInt KStrLen = 64;
       
   859         
       
   860     TBuf<KStrLen> mailbox;
       
   861     mailbox.Format( KFormat, &KStartSeparator, aMailBox.Id(), &KEndSeparator );
       
   862     
       
   863     TUint32 key( KCMailMailboxesWithNewMail );
       
   864     TInt err = iCenRep->Get( key, iCenrepText );
       
   865     
       
   866     if ( err == KErrNone )
       
   867         {
       
   868         TInt pos = iCenrepText.Find( mailbox );
       
   869     
       
   870         if ( aIconOn )
       
   871             {
       
   872             if ( pos < 0 ) // Not found
       
   873                 {
       
   874                 iCenrepText.Append( mailbox );
       
   875                 iCenRep->Set( key, iCenrepText );
       
   876                 }
       
   877             }
       
   878         else
       
   879             {
       
   880             if ( pos >= 0 )
       
   881                 {
       
   882                 iCenrepText.Delete( pos, mailbox.Length() );
       
   883                 iCenRep->Set( key, iCenrepText );
       
   884                 }
       
   885             }
       
   886         }
       
   887     }
       
   888 
       
   889 
       
   890 
       
   891 // -----------------------------------------------------------------------------
       
   892 // CMailCpsSettings::GetNewMailStateL
       
   893 // -----------------------------------------------------------------------------
       
   894 TBool CMailCpsSettings::GetNewMailStateL( const TFSMailMsgId& aMailBox, TInt aUnreadCount )
       
   895     {
       
   896     FUNC_LOG;
       
   897 
       
   898     _LIT( KFormat, "%S%d%S" );
       
   899     const TInt KStrLen = 64;
       
   900 
       
   901 
       
   902     TBool ret(EFalse);
       
   903     if ( aUnreadCount )
       
   904         {
       
   905         TBuf<KStrLen> mailbox;
       
   906         mailbox.Format( KFormat, &KStartSeparator, aMailBox.Id(), &KEndSeparator );
       
   907 
       
   908         TUint32 key( KCMailMailboxesWithNewMail );
       
   909         TInt err = iCenRep->Get( key, iCenrepText );
       
   910         if ( err == KErrNone )
       
   911             {
       
   912             TInt pos = iCenrepText.Find( mailbox );
       
   913             if ( pos >= 0 )
       
   914                 {
       
   915                 ret = ETrue;
       
   916                 }
       
   917             }
       
   918         }
       
   919     else
       
   920         {
       
   921         ToggleWidgetNewMailIconL( EFalse, aMailBox );
       
   922         }
       
   923     
       
   924     return ret;
       
   925     }
       
   926 
       
   927 // ---------------------------------------------------------------------------
       
   928 // CMailCpsSettings::HandlePropertyChangedL
       
   929 // ---------------------------------------------------------------------------
       
   930 //
       
   931 void CMailCpsSettings::HandlePropertyChangedL( const TUid& aCategory, TInt aKey )
       
   932     {
       
   933     FUNC_LOG;
       
   934 
       
   935     if ( aCategory == KUidSystemCategory && aKey == conn::KUidBackupRestoreKey )
       
   936         {
       
   937         TInt keyVal = 0;
       
   938         const TInt error = RProperty::Get(  KUidSystemCategory, conn::KUidBackupRestoreKey, keyVal );
       
   939         if( error == KErrNone )
       
   940             {
       
   941             const conn::TBURPartType partType = 
       
   942                     static_cast< conn::TBURPartType >( keyVal & conn::KBURPartTypeMask );
       
   943 
       
   944             if ( keyVal != 0 )
       
   945                 {
       
   946                 switch( partType )
       
   947                     {
       
   948                     case conn::EBURRestoreFull:
       
   949                     case conn::EBURRestorePartial:
       
   950                         iRestoreStarted = ETrue;
       
   951                         break;
       
   952                     case conn::EBURBackupFull:
       
   953                     case conn::EBURBackupPartial:                        
       
   954                         iBackupOngoing = ETrue;
       
   955                         break;
       
   956                     case conn::EBURUnset:
       
   957                     case conn::EBURNormal:
       
   958                     default:
       
   959                         iBackupOngoing = EFalse;
       
   960                         break;
       
   961                     }
       
   962                 }
       
   963             }
       
   964         }
       
   965     }
       
   966 
       
   967 // ----------------------------------------------------------------------------
       
   968 // CMailCpsHandler::BackupOrRestoreMode()
       
   969 // Check if phone is in backup/restore mode
       
   970 // ----------------------------------------------------------------------------
       
   971 //
       
   972 TBool CMailCpsSettings::BackupOrRestoreMode()
       
   973     {
       
   974     FUNC_LOG;
       
   975 
       
   976     TBool backupOrRestore = EFalse;
       
   977     
       
   978     if ( iRestoreStarted || iBackupOngoing )
       
   979         {
       
   980         backupOrRestore = ETrue;
       
   981         }
       
   982     return backupOrRestore;
       
   983     }