emailservices/emailserver/cmailhandlerplugin/src/cmailcpssettings.cpp
branchRCL_3
changeset 19 b13141f05c3d
parent 17 8592a65ad3fb
child 24 b5fbb9b25d57
equal deleted inserted replaced
17:8592a65ad3fb 19:b13141f05c3d
   120 // ---------------------------------------------------------------------------
   120 // ---------------------------------------------------------------------------
   121 //
   121 //
   122 void CMailCpsSettings::LoadSettingsL()
   122 void CMailCpsSettings::LoadSettingsL()
   123     {
   123     {
   124     FUNC_LOG;
   124     FUNC_LOG;
   125     // Clean up local settings cache
       
   126     iMailboxArray.Reset();
       
   127     TInt ret( KErrNone );
   125     TInt ret( KErrNone );
   128     
   126 
   129     // Load mailbox array
   127     // mailbox keys
   130     RArray<TUint32> keys;
   128     RArray<TUint> cenrepMailboxes; 
       
   129     CleanupClosePushL( cenrepMailboxes );
       
   130     RArray<TBool> cenrepMailboxesExistence; 
       
   131     CleanupClosePushL( cenrepMailboxesExistence );
       
   132 
       
   133     // cenrep keys
       
   134     RArray<TUint32> keys; 
   131     CleanupClosePushL( keys );
   135     CleanupClosePushL( keys );
   132     GetMailboxNonZeroKeysL( keys );
   136     GetMailboxNonZeroKeysL( keys );
   133     const TInt iiMax( keys.Count() );
   137     TInt dMax( keys.Count() );
   134     for ( TInt ii = 0; ii < iiMax; ii++ )
   138     
   135         {
   139     cenrepMailboxes.ReserveL( dMax );
   136         TInt value( 0 );
   140     cenrepMailboxesExistence.ReserveL( dMax );
   137         ret = iCenRep->Get( keys[ii], value );
   141     
   138         if ( ret )
   142     // make array of mailbox keys
   139             {
   143     TInt value( 0 );
   140             User::Leave( ret );
   144     TInt i;
   141             }
   145     for ( i = 0; i < dMax; i++ )
   142         else
   146         {
   143             {
   147          User::LeaveIfError( ret = iCenRep->Get( keys[i], value ));
   144             TFSMailMsgId mailbox; 
   148          cenrepMailboxes.AppendL( static_cast<TUint>(value) );
   145             ret = ResolveMailbox( value, mailbox );
   149          cenrepMailboxesExistence.AppendL( EFalse );
   146             if ( ret )
   150         }
       
   151     CleanupStack::PopAndDestroy(&keys); 
       
   152     
       
   153     // Sync array of cenrep keys with iMailboxArray
       
   154     // remove from array what is not in cenrep
       
   155     TInt dFound( KErrNotFound );
       
   156     for ( i = iMailboxArray.Count()-1; i >= 0; i -- )
       
   157         {
       
   158         dFound = cenrepMailboxes.Find( iMailboxArray[i].Id() );
       
   159         if ( KErrNotFound != dFound ) 
       
   160             { 
       
   161              // mailbox is in iMailboxArray and in cenrep => check provider
       
   162             INFO_1("Mailbox both in cenrep and iMailboxArray: i = %d ", i );
       
   163             ret = CheckMailboxExistence( iMailboxArray[i] );
       
   164             if ( KErrNotFound == ret)
   147                 {
   165                 {
   148 				INFO("CMailCpsSettings::LoadSettingsL(): Error: ignore this entry");
   166                 // mailbox was removed from provider => remove from cenrep also
   149                 // Resolving encountered error, ignore this entry
   167                 // cenrepMailboxes indexed the same way as keys => finding key not needed 
   150                 ret = iCenRep->Reset( KCMailMailboxIdBase+ii );
   168                 ret = iCenRep->Reset( KCMailMailboxIdBase + i );
   151                 ret = iCenRep->Reset( KCMailPluginIdBase+ii );
   169                 ret = iCenRep->Reset( KCMailPluginIdBase + i );
   152                 ret = iCenRep->Reset( KCMailWidgetContentIdBase+ii );
   170                 ret = iCenRep->Reset( KCMailWidgetContentIdBase + i );
   153                 if ( ret )
   171                 INFO_1("Mailbox removed from cenrep: dFound %d ", dFound );
   154                     {
       
   155                     }
       
   156                 }
   172                 }
   157             else
   173             else
   158                 {
   174                 {
   159                 iMailboxArray.AppendL( mailbox );
   175                 User::LeaveIfError(ret); // for instance if no memory
       
   176                 INFO_1("Mailbox provider check ok: dFound = %d ", dFound );
   160                 }
   177                 }
   161             }
   178             cenrepMailboxesExistence[dFound] = ETrue; // not remove to ensure indexes are the same as in keys
   162         }
   179             }
   163     CleanupStack::PopAndDestroy(); // CleanupClosePushL( keys )
   180         else
       
   181             {
       
   182             // mailbox was removed from cenrep => remove from array
       
   183             iMailboxArray.Remove(i);
       
   184             INFO_1("Mailbox removed from iMailboxArray: i = %d ", i );
       
   185             }
       
   186         }
       
   187     // Check order and new mailboxes 
       
   188     TInt j(0);
       
   189     for ( i = 0; i < dMax; i++ )
       
   190         {
       
   191         // new mailboxes in cenrep needs to be added to iMailboxArray
       
   192         if ( ! cenrepMailboxesExistence[i] ) 
       
   193             {
       
   194             TFSMailMsgId mailbox; 
       
   195             // Find mailbox by this function because cenrep does not hold pluginID
       
   196             if ( KErrNone == ResolveMailbox( cenrepMailboxes[i], mailbox ) )
       
   197                 {
       
   198                 iMailboxArray.Insert( mailbox, i );
       
   199                 INFO_1("Mailbox added to iMailboxArray: i = %d ", i );
       
   200                 }
       
   201             }
       
   202         // Check if order is OK
       
   203         if ( iMailboxArray[i].Id() != cenrepMailboxes[i] )
       
   204             {
       
   205             TInt jMax( iMailboxArray.Count() );
       
   206             for ( j = i+1; j <= jMax; j++ )
       
   207                 {
       
   208                 if ( iMailboxArray[j].Id() == cenrepMailboxes[i])
       
   209                     {
       
   210                     iMailboxArray.Insert( iMailboxArray[j], i ); 
       
   211                     iMailboxArray.Remove( j+1 );// insertion increased indices 
       
   212                     break;
       
   213                     }
       
   214                 }
       
   215             if (j == jMax) // arrays not in sync error
       
   216                 {
       
   217                 User::Leave( KErrNotFound );
       
   218                 }
       
   219             }
       
   220         }
       
   221     CleanupStack::PopAndDestroy(&cenrepMailboxesExistence);
       
   222     CleanupStack::PopAndDestroy(&cenrepMailboxes);
   164     }
   223     }
   165 
   224 
   166 // ---------------------------------------------------------------------------
   225 // ---------------------------------------------------------------------------
   167 // CMailCpsSettings::LoadConfigurationL
   226 // CMailCpsSettings::LoadConfigurationL
   168 // ---------------------------------------------------------------------------
   227 // ---------------------------------------------------------------------------
   215 TInt CMailCpsSettings::ResolveMailbox( const TInt aMailboxId, TFSMailMsgId& aMsg )
   274 TInt CMailCpsSettings::ResolveMailbox( const TInt aMailboxId, TFSMailMsgId& aMsg )
   216     {
   275     {
   217     FUNC_LOG;
   276     FUNC_LOG;
   218     RPointerArray<CFSMailBox> mailboxarray;
   277     RPointerArray<CFSMailBox> mailboxarray;
   219     TInt err = iMailClient.ListMailBoxes( TFSMailMsgId(), mailboxarray );
   278     TInt err = iMailClient.ListMailBoxes( TFSMailMsgId(), mailboxarray );
   220 	INFO_1("CMailCpsSettings::ResolveMailbox(): : ListMailBoxes() returns %d", err);
   279 	INFO_1("CMailCpsSettings::ResolveMailbox():: ListMailBoxes() returns %d", err);
   221     if( !err ) // KErrNone = 0
   280     if( !err ) // KErrNone = 0
   222         {
   281         {
   223         err = KErrNotFound;
   282         err = KErrNotFound;
   224         aMsg.SetId( aMailboxId );
   283         aMsg.SetId( aMailboxId );
   225         const TInt iiMax( mailboxarray.Count() );
   284         const TInt iiMax( mailboxarray.Count() );
   236         }
   295         }
   237     mailboxarray.ResetAndDestroy();
   296     mailboxarray.ResetAndDestroy();
   238     return err;
   297     return err;
   239     }
   298     }
   240 
   299 
       
   300 TInt CMailCpsSettings::CheckMailboxExistence( TFSMailMsgId& aMsg )
       
   301     {
       
   302     FUNC_LOG;
       
   303     CFSMailBox *mbox( NULL );
       
   304     TRAPD(err, mbox = iMailClient.GetMailBoxByUidL(aMsg));
       
   305     if ( mbox ) // mail box exists
       
   306         {
       
   307         delete mbox;
       
   308         return KErrNone;
       
   309         }
       
   310     if ( ( KErrNotFound == err ) || ( KErrNone == err ) )
       
   311         {
       
   312     // mail box not exists, chek if it is not in different plugin 
       
   313     // very safe, maybe return KErrNotFound would be enough
       
   314         return ResolveMailbox( aMsg.Id(), aMsg );
       
   315         }
       
   316     return err;
       
   317     }
       
   318 
       
   319 
       
   320 
       
   321 
   241 // ---------------------------------------------------------------------------
   322 // ---------------------------------------------------------------------------
   242 // CMailCpsSettings::StartObservingL
   323 // CMailCpsSettings::StartObservingL
   243 // ---------------------------------------------------------------------------
   324 // ---------------------------------------------------------------------------
   244 //
   325 //
   245 void CMailCpsSettings::StartObservingL( MMailCpsSettingsCallback* aObserver )
   326 void CMailCpsSettings::StartObservingL( MMailCpsSettingsCallback* aObserver )
   303         CleanupStack::PushL( account );
   384         CleanupStack::PushL( account );
   304         aAccounts.AppendL( account );
   385         aAccounts.AppendL( account );
   305         CleanupStack::Pop( account );
   386         CleanupStack::Pop( account );
   306         }
   387         }
   307 
   388 
   308     CleanupStack::PopAndDestroy(); // keys
   389     CleanupStack::PopAndDestroy(&keys); // keys
   309     }
   390     }
   310 
   391 
   311 // ---------------------------------------------------------------------------
   392 // ---------------------------------------------------------------------------
   312 // CMailCpsSettings::GetExtMailboxL
   393 // CMailCpsSettings::GetExtMailboxL
   313 // ---------------------------------------------------------------------------
   394 // ---------------------------------------------------------------------------
   368         }
   449         }
   369     else
   450     else
   370         {
   451         {
   371         }
   452         }
   372 
   453 
   373     CleanupStack::PopAndDestroy(); // CleanupClosePushL( keys );
   454     CleanupStack::PopAndDestroy( &keys ); // keys 
   374 
   455 
   375     }
   456     }
   376 
   457 
   377 // ---------------------------------------------------------------------------
   458 // ---------------------------------------------------------------------------
   378 // CMailCpsSettings::RemoveMailboxL
   459 // CMailCpsSettings::RemoveMailboxL
   545             }
   626             }
   546         }
   627         }
   547     }
   628     }
   548 
   629 
   549 // ---------------------------------------------------------------------------
   630 // ---------------------------------------------------------------------------
       
   631 // CMailCpsSettings::WidgetCountByMailbox
       
   632 // ---------------------------------------------------------------------------
       
   633 //
       
   634 TInt CMailCpsSettings::WidgetCountByMailbox( TInt aMailboxId )
       
   635     {
       
   636     FUNC_LOG;
       
   637     TInt ret(0);
       
   638     for (TInt i = 0; i < KMaxMailboxCount; i++)
       
   639         {       
       
   640         TInt value;
       
   641         TUint32 mailboxKey(KCMailMailboxIdBase+i);
       
   642         iCenRep->Get( mailboxKey, value );     
       
   643         if (aMailboxId == value)
       
   644             {
       
   645             ret++;
       
   646             }
       
   647         }
       
   648     return ret;
       
   649     }
       
   650 
       
   651 // ---------------------------------------------------------------------------
   550 // CMailCpsSettings::IsSelected
   652 // CMailCpsSettings::IsSelected
   551 // ---------------------------------------------------------------------------
   653 // ---------------------------------------------------------------------------
   552 //
   654 //
   553 TBool CMailCpsSettings::IsSelected( TInt aId )
   655 TBool CMailCpsSettings::IsSelected( TInt aId )
   554     {
   656     {
   634     {
   736     {
   635     FUNC_LOG;
   737     FUNC_LOG;
   636     RPointerArray<CFSMailBox> mailboxarray;
   738     RPointerArray<CFSMailBox> mailboxarray;
   637     iMailClient.ListMailBoxes( TFSMailMsgId(), mailboxarray );    
   739     iMailClient.ListMailBoxes( TFSMailMsgId(), mailboxarray );    
   638     TInt ret = mailboxarray.Count(); 
   740     TInt ret = mailboxarray.Count(); 
       
   741     mailboxarray.ResetAndDestroy();
   639     return ret;
   742     return ret;
   640     }
   743     }
   641 
   744 
   642 // -----------------------------------------------------------------------------
   745 // -----------------------------------------------------------------------------
   643 // CMailCpsSettings::ToggleWidgetNewMailIconL
   746 // CMailCpsSettings::ToggleWidgetNewMailIconL
   676             }
   779             }
   677         }
   780         }
   678     }
   781     }
   679 
   782 
   680 // -----------------------------------------------------------------------------
   783 // -----------------------------------------------------------------------------
   681 // CMailCpsSettings::GetNewMailState
   784 // CMailCpsSettings::GetNewMailStateL
   682 // -----------------------------------------------------------------------------
   785 // -----------------------------------------------------------------------------
   683 TBool CMailCpsSettings::GetNewMailState( const TFSMailMsgId& aMailBox )
   786 TBool CMailCpsSettings::GetNewMailStateL( const TFSMailMsgId& aMailBox, TInt aUnreadCount )
   684     {
   787     {
   685     FUNC_LOG;
   788     FUNC_LOG;
   686     TBool ret(EFalse);
   789     TBool ret(EFalse);
   687     TBuf<KMaxDescLen> mailbox;
   790     if ( aUnreadCount )
   688     mailbox.Num(aMailBox.Id());
   791         {
   689 
   792         TBuf<KMaxDescLen> mailbox;
   690     TBuf<KMaxDescLen> str;
   793         mailbox.Num(aMailBox.Id());
   691     str.Copy(KStartSeparator);    
   794 
   692     str.Append(mailbox);
   795         TBuf<KMaxDescLen> str;
   693     str.Append(KEndSeparator);    
   796         str.Copy(KStartSeparator);    
   694 
   797         str.Append(mailbox);
   695     TBuf<KMaxDescLen> stored;
   798         str.Append(KEndSeparator);    
   696     TUint32 key(KCMailMailboxesWithNewMail);
   799 
   697     iCenRep->Get( key, stored );
   800         TBuf<KMaxDescLen> stored;
       
   801         TUint32 key(KCMailMailboxesWithNewMail);
       
   802         iCenRep->Get( key, stored );
   698     
   803     
   699     TInt result = stored.Find(str);
   804         TInt result = stored.Find(str);
   700     if (result >= 0)
   805         if (result >= 0)
   701         {
   806             {
   702         ret = ETrue;
   807             ret = ETrue;
       
   808             }
       
   809         }
       
   810     else
       
   811         {
       
   812         ToggleWidgetNewMailIconL( EFalse, aMailBox );
   703         }
   813         }
   704     return ret;
   814     return ret;
   705     }
   815     }