emailservices/emailserver/cmailhandlerplugin/src/cmailcpssettings.cpp
branchRCL_3
changeset 17 67369d1b217f
parent 16 b5fbb9b25d57
child 20 efd4f1afd43e
equal deleted inserted replaced
16:b5fbb9b25d57 17:67369d1b217f
   115     iCenRep->NotifyCancel( KCmailPartialKey, KCmailMask );
   115     iCenRep->NotifyCancel( KCmailPartialKey, KCmailMask );
   116     }
   116     }
   117 
   117 
   118 // ---------------------------------------------------------------------------
   118 // ---------------------------------------------------------------------------
   119 // CMailCpsSettings::LoadSettingsL
   119 // CMailCpsSettings::LoadSettingsL
   120 // ---------------------------------------------------------------------------
   120 // 
   121 //
   121 // synchronizes the iMailboxArray with Central repository settings
       
   122 // only existing mailboxes are synchronized
       
   123 // usecases 
       
   124 // 1. delete widget from homesceen - removed item from cenrep array
       
   125 // 2. add widget to homescreen - added item to the end of cenrep array
       
   126 // 3. change mailbox for widget - changed value of cenrep array item 
       
   127 // 4. remove mailbox from server - mailbox absence for array item 
       
   128 // 5. add mailbox to the server (leads to widget adding)
       
   129 // ---------------------------------------------------------------------------
       
   130 
   122 void CMailCpsSettings::LoadSettingsL()
   131 void CMailCpsSettings::LoadSettingsL()
   123     {
   132     {
   124     FUNC_LOG;
   133     FUNC_LOG;
   125     TInt ret( KErrNone );
   134     TInt ret( KErrNone );
   126 
   135 
   134     RArray<TUint32> keys; 
   143     RArray<TUint32> keys; 
   135     CleanupClosePushL( keys );
   144     CleanupClosePushL( keys );
   136     GetMailboxNonZeroKeysL( keys );
   145     GetMailboxNonZeroKeysL( keys );
   137     TInt dMax( keys.Count() );
   146     TInt dMax( keys.Count() );
   138     
   147     
   139     cenrepMailboxes.ReserveL( dMax );
   148     cenrepMailboxes.ReserveL( dMax ); // cenrep mailbox ids array
   140     cenrepMailboxesExistence.ReserveL( dMax );
   149     cenrepMailboxesExistence.ReserveL( dMax ); // auxiliary if it was found in iMailboxArray looping
   141     
   150     
   142     // make array of mailbox keys
   151     // initialize array of mailbox keys
   143     TInt value( 0 );
   152     TInt value( 0 );
   144     TInt i;
   153     TInt i; // main loop
       
   154     TInt j; // auxiliary / inner loop
   145     for ( i = 0; i < dMax; i++ )
   155     for ( i = 0; i < dMax; i++ )
   146         {
   156         {
   147          User::LeaveIfError( ret = iCenRep->Get( keys[i], value ));
   157          User::LeaveIfError( ret = iCenRep->Get( keys[i], value ));
   148          cenrepMailboxes.AppendL( static_cast<TUint>(value) );
   158          cenrepMailboxes.AppendL( static_cast<TUint>(value) );
   149          cenrepMailboxesExistence.AppendL( EFalse );
   159          cenrepMailboxesExistence.AppendL( EFalse );
   150         }
   160         }
   151     CleanupStack::PopAndDestroy(&keys); 
   161     CleanupStack::PopAndDestroy(&keys); 
   152     
   162     
   153     // Sync array of cenrep keys with iMailboxArray
   163     // Sync array of cenrep keys with iMailboxArray
   154     // remove from array what is not in cenrep
   164     // 1. remove from iMailboxArray what is not in cenrep, mark what is. Distinct existence. 
       
   165     // 2. remove mailbox ids not detected on the server
   155     TInt dFound( KErrNotFound );
   166     TInt dFound( KErrNotFound );
   156     for ( i = iMailboxArray.Count()-1; i >= 0; i -- )
   167     for ( i = iMailboxArray.Count()-1; i >= 0; i -- ) // iArray loop
   157         {
   168         {
   158         dFound = cenrepMailboxes.Find( iMailboxArray[i].Id() );
   169         dFound = cenrepMailboxes.Find( iMailboxArray[i].Id() );
   159         if ( KErrNotFound != dFound ) 
   170         if ( KErrNotFound != dFound ) // if found in cenrep
   160             { 
   171             { 
   161              // mailbox is in iMailboxArray and in cenrep => check provider
   172                  // mailbox is in iMailboxArray and in cenrep => check provider
   162             INFO_1("Mailbox both in cenrep and iMailboxArray: i = %d ", i );
   173             INFO_1("Mailbox both in cenrep and iMailboxArray: i = %d ", i );
   163             ret = CheckMailboxExistence( iMailboxArray[i] );
   174             if ( ! cenrepMailboxesExistence[dFound] )
   164             if ( KErrNotFound == ret)
   175                 { 
   165                 {
   176                 ret = CheckMailboxExistence( iMailboxArray[i] );
   166                 // mailbox was removed from provider => remove from cenrep also
   177                 if ( KErrNotFound == ret)
   167                 // cenrepMailboxes indexed the same way as keys => finding key not needed 
   178                     {
       
   179                     // mailbox was removed from provider => remove from cenrep also
       
   180                     // cenrepMailboxes indexed the same way as keys => finding key not needed 
       
   181                     ret = iCenRep->Reset( KCMailMailboxIdBase + i );
       
   182                     ret = iCenRep->Reset( KCMailPluginIdBase + i );
       
   183                     ret = iCenRep->Reset( KCMailWidgetContentIdBase + i );
       
   184                     INFO_1("Mailbox removed from cenrep: dFound %d ", dFound );
       
   185                     // remove also from all arrays
       
   186                     iMailboxArray.Remove(i);
       
   187                     cenrepMailboxes.Remove( dFound );
       
   188                     cenrepMailboxesExistence.Remove( dFound );
       
   189                     dMax--;
       
   190                     }
       
   191                 else 
       
   192                     {
       
   193                     User::LeaveIfError(ret); // for instance if no memory
       
   194                     INFO_1("Mailbox provider check ok: dFound = %d ", dFound );
       
   195                     cenrepMailboxesExistence[dFound] = ETrue; // not remove to ensure indexes are the same as in keys
       
   196                     // handle also possible mailbox duplications
       
   197                     for (j = dFound+1; j<dMax; j++)
       
   198                         {
       
   199                         if ( cenrepMailboxes[dFound] == cenrepMailboxes[j] )
       
   200                             { 
       
   201                             cenrepMailboxesExistence[j] = ETrue; 
       
   202                             }
       
   203                         }
       
   204                     }
       
   205                  } // else already tested for existence
       
   206             }
       
   207         else // not found in cenrep
       
   208             {
       
   209             // mailbox was removed from cenrep => remove from iArray as well
       
   210             iMailboxArray.Remove(i);
       
   211             INFO_1("Mailbox removed from iMailboxArray: i = %d ", i );
       
   212             }
       
   213         } // iArray loop
       
   214     
       
   215     // Add mailboxes existing only in cenrep to iMailboxArray in correct order
       
   216     // or check and correct order of mailboxes (swap already dot checked, copy duplicated)
       
   217     for ( i = 0; i < dMax; i++ )
       
   218         {
       
   219         // firstly new mailboxes in cenrep needs to be added to iMailboxArray
       
   220         if ( ! cenrepMailboxesExistence[i] ) 
       
   221             {
       
   222             TFSMailMsgId mailbox; 
       
   223             // Find mailbox by this unefficient function because cenrep does not hold pluginID
       
   224             if ( KErrNone == ResolveMailbox( cenrepMailboxes[i], mailbox ) )
       
   225                 {
       
   226                 iMailboxArray.InsertL( mailbox, i );
       
   227                 INFO_1("Mailbox added to iMailboxArray: i = %d ", i );
       
   228                 // ensured iMailboxArray[i].Id() == cenrepMailboxes[i]
       
   229                 }
       
   230             else // KErrNotFound
       
   231                 {
       
   232                 // remove not valid mailbox from cenrep arrays
       
   233                 cenrepMailboxes.Remove( i );
       
   234                 cenrepMailboxesExistence.Remove( i );
       
   235                 // remove from cenrep
   168                 ret = iCenRep->Reset( KCMailMailboxIdBase + i );
   236                 ret = iCenRep->Reset( KCMailMailboxIdBase + i );
   169                 ret = iCenRep->Reset( KCMailPluginIdBase + i );
   237                 ret = iCenRep->Reset( KCMailPluginIdBase + i );
   170                 ret = iCenRep->Reset( KCMailWidgetContentIdBase + i );
   238                 ret = iCenRep->Reset( KCMailWidgetContentIdBase + i );
   171                 INFO_1("Mailbox removed from cenrep: dFound %d ", dFound );
   239                 INFO_1("Mailbox removed from cenrep: i = %d ", i );
       
   240                 // update loop control expression
       
   241                 dMax--;
       
   242                 i--;
       
   243                 }
       
   244             continue; // ensured equation or removed item
       
   245             } // end of if mailbox not in cenrep
       
   246 
       
   247         // Check if iMailboxArray is in correct order on i position
       
   248         TInt jMax( iMailboxArray.Count() );
       
   249         j = i; // assume iMailboxArray[i].Id() == cenrepMailboxes[i]
       
   250         if ( ( i < jMax ) && ( iMailboxArray[i].Id() != cenrepMailboxes[i] ) )
       
   251             { // not in correct order try to swap
       
   252             for ( j = i+1; j < jMax; j++ )
       
   253                 {
       
   254                 if ( iMailboxArray[j].Id() == cenrepMailboxes[i] )
       
   255                     { // swap is better from efficiency point of view
       
   256                     TFSMailMsgId tmp( iMailboxArray[i] ); 
       
   257                     iMailboxArray[i] = iMailboxArray[j]; 
       
   258                     iMailboxArray[j] = tmp; 
       
   259                     break; // j < jMax here
       
   260                     }
       
   261                 }
       
   262             // as previous code does not detect existence of repeating id occurence
       
   263             // the loop may not find the repeated mailbox id - then j=jMax
       
   264             } 
       
   265         // if cenrep has multiplied mailbox widgets then add them 
       
   266         if ( j >= jMax ) // swapping did not help or item is missing
       
   267             {
       
   268             // if i=j=jMax then missing duplicated iArray element
       
   269             // if j>i then iArray contains not used element
       
   270             // two widgets shows the same mailbox then take already used iMailboxArray value
       
   271             dFound = cenrepMailboxes.Find( cenrepMailboxes[i] );
       
   272             if ( dFound < i )
       
   273                 {
       
   274                 // the arrays are synchronized below up to i, duplication to correct i place
       
   275                 iMailboxArray.InsertL( iMailboxArray[dFound], i ); 
   172                 }
   276                 }
   173             else
   277             else
   174                 {
   278                 {
   175                 User::LeaveIfError(ret); // for instance if no memory
   279                 // unable to synchronize the arrays - algorithm is wrong
   176                 INFO_1("Mailbox provider check ok: dFound = %d ", dFound );
   280                 INFO_1("CMAIL: CMailCpsSettings::LoadSettingsL() : FATAL ALGORITHM ERROR - ARRAY NOT SYNCHRONIZED  i = %d ", i );
   177                 }
       
   178             cenrepMailboxesExistence[dFound] = ETrue; // not remove to ensure indexes are the same as in keys
       
   179             }
       
   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 );
   281                 User::Leave( KErrNotFound );
   218                 }
   282                 }
   219             }
   283             } // end of j >= jMax 
   220         }
   284         
       
   285         } // end check order for i = 0..dMax loop 
       
   286 
       
   287     // if iMailboxArray has more items than cenrepMailboxes then remove these items
       
   288     for( i = iMailboxArray.Count()-1; i >= dMax; i-- )
       
   289         {
       
   290         iMailboxArray.Remove(i);
       
   291         }
       
   292 
   221     CleanupStack::PopAndDestroy(&cenrepMailboxesExistence);
   293     CleanupStack::PopAndDestroy(&cenrepMailboxesExistence);
   222     CleanupStack::PopAndDestroy(&cenrepMailboxes);
   294     CleanupStack::PopAndDestroy(&cenrepMailboxes);
   223     }
   295     }
   224 
   296 
   225 // ---------------------------------------------------------------------------
   297 // ---------------------------------------------------------------------------