ipsservices/ipssosplugin/src/ipsplgmrulist.cpp
changeset 0 8466d47a6819
child 24 d189ee25cf9d
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 CIpsPlgMruList.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "emailtrace.h"
       
    21 #include "ipsplgheaders.h"
       
    22 
       
    23 const TUint32 KMruListMailboxMask = 0x0000FFFF;
       
    24 const TUint32 KMruListMailboxPartKey = 0x0;
       
    25 const TUint32 KMruListValueMask = 0xFFFF0000;
       
    26 const TUint32 KMruListIncorrectMBoxKey = 0xFFFFFFFF;
       
    27 const TUint32 KMruListKeyBaseValue = 0x00010000;
       
    28 // what are actual size in Freestyle framework?
       
    29 const TInt KMruListPrefDesSize = 40;
       
    30 const TInt KMruListMaxDesSize = 200;
       
    31 const TInt KMruListArrayInitSize = 8;
       
    32 
       
    33 // <cmail>
       
    34 const TInt KIpsSmtpOperationCharLessThan = '<';
       
    35 const TInt KIpsSmtpOperationCharMoreThan = '>';
       
    36 // </cmail>
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // CIpsPlgMruList::NewL( )
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 CIpsPlgMruList* CIpsPlgMruList::NewL( )
       
    43     {
       
    44     FUNC_LOG;
       
    45     CIpsPlgMruList* self = new( ELeave ) CIpsPlgMruList( );
       
    46     CleanupStack::PushL( self );
       
    47     self->ConstructL( );
       
    48     CleanupStack::Pop( self );
       
    49     return self;
       
    50     }
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // CIpsPlgMruList::~CIpsPlgMruList()
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 CIpsPlgMruList::~CIpsPlgMruList()
       
    57     {
       
    58     FUNC_LOG;
       
    59     delete iRepository;
       
    60     iHbufArray.ResetAndDestroy();
       
    61     iHbufArray.Close();
       
    62     iKeys.Reset();
       
    63     iKeys.Close();
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // CIpsPlgMruList::CIpsPlgMruList()
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 CIpsPlgMruList::CIpsPlgMruList( )
       
    71     : iRepository( NULL )
       
    72     {
       
    73     FUNC_LOG;
       
    74 
       
    75     }
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // CIpsPlgMruList::ConstructL( )
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 void CIpsPlgMruList::ConstructL( )
       
    82     {
       
    83     FUNC_LOG;
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // CIpsPlgMruList::GetMruListL
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90 CDesCArrayFlat* CIpsPlgMruList::GetMruListL( const TFSMailMsgId& aMailbox )
       
    91     {
       
    92     FUNC_LOG;
       
    93     // create iRepository if not yet created
       
    94     CreateRepositoryL( iRepository );
       
    95 
       
    96     TBool returnEmpty( EFalse );
       
    97 
       
    98     // Do not cache anything, there are multiple plugin instances
       
    99     iKeys.Reset();
       
   100     TUint32 mboxKey;
       
   101     if ( KErrNone != GetMailboxKeyFromRepositoryL( aMailbox.Id(), mboxKey, iKeys ) )
       
   102         {
       
   103         returnEmpty = ETrue;
       
   104         }
       
   105     iKeys.Reset();
       
   106 
       
   107     // get correct mailbox key and fill corresponding name and address keys
       
   108     // in keys array
       
   109     if ( !returnEmpty )
       
   110         {
       
   111         if ( KErrNone != iRepository->FindL( mboxKey, KMruListValueMask, iKeys ) )
       
   112             {
       
   113             returnEmpty = ETrue;
       
   114             }
       
   115         }
       
   116 
       
   117     // if we have found correct key find all keys that
       
   118     // maches pattern 0xMKEY**** key array should contain
       
   119     // at least 3 items, and size shoud be odd
       
   120     // or array is empty or corrupted
       
   121     TInt keyCount( iKeys.Count() );
       
   122     if ( !returnEmpty && keyCount >= 3 && ( keyCount%2 ) == 1 )
       
   123         {
       
   124         iHbufArray.ResetAndDestroy();
       
   125         iKeys.SortUnsigned();
       
   126         TInt i(0);
       
   127 
       
   128         // first item in array should be mbox key
       
   129         if ( iKeys[0] == mboxKey )
       
   130             {
       
   131             // index to first name key
       
   132             i = 1;
       
   133             // store name and address in one loop cycle
       
   134             while ( !returnEmpty && i <= ( keyCount-2 ) )
       
   135                 {
       
   136                 TUint key = iKeys[i];
       
   137                 // name is always in odd and address in even key
       
   138                 if ( (key%2==1) && (iKeys[i+1]==key+1) )
       
   139                     {
       
   140                     // adding name and email to hbuf array
       
   141                     if ( StoreToArrayL(iKeys[i]) != KErrNone )
       
   142                         {
       
   143                         // something went wrong in name store
       
   144                         // just break the loop and return empty
       
   145                         HandleRepositoryError( mboxKey, KErrCorrupt );
       
   146                         returnEmpty = ETrue;
       
   147                         }
       
   148                     if ( StoreToArrayL(iKeys[i+1]) != KErrNone )
       
   149                         {
       
   150                         // store is corrupted
       
   151                         HandleRepositoryError( mboxKey, KErrCorrupt );
       
   152                         returnEmpty = ETrue;
       
   153                         }
       
   154                     }
       
   155                 i = i+2;
       
   156                 }
       
   157             }
       
   158         else
       
   159             {
       
   160             // first item was not mailbox key
       
   161             // centrep data is corrupted
       
   162             HandleRepositoryError( mboxKey, KErrCorrupt );
       
   163             returnEmpty = ETrue;
       
   164             }
       
   165         }
       
   166     else if ( !returnEmpty )
       
   167         {
       
   168         // mailbox found but there is no data or centrep is corrupted,
       
   169         // return empty
       
   170         HandleRepositoryError( mboxKey, KErrCorrupt );
       
   171         returnEmpty = ETrue;
       
   172         }
       
   173 
       
   174     // create and return des array
       
   175     CDesCArrayFlat* retArr = NULL;
       
   176 
       
   177     if ( !returnEmpty )
       
   178         {
       
   179         retArr = new ( ELeave ) CDesCArrayFlat( KMruListArrayInitSize );
       
   180         CleanupStack::PushL( retArr );
       
   181 
       
   182         TInt count( iHbufArray.Count() );
       
   183         // fill descriptor array
       
   184         for ( TInt i(0); i < count; i++ )
       
   185             {
       
   186             HBufC* app = iHbufArray[i];
       
   187             retArr->AppendL(*app);
       
   188             }
       
   189         CleanupStack::Pop( retArr );
       
   190         }
       
   191     else
       
   192         {
       
   193         iHbufArray.ResetAndDestroy();
       
   194         retArr = new ( ELeave ) CDesCArrayFlat( KMruListArrayInitSize );
       
   195         }
       
   196 
       
   197     return retArr;
       
   198     }
       
   199 
       
   200 // ---------------------------------------------------------------------------
       
   201 // CIpsPlgMruList::SetMruListL
       
   202 // ---------------------------------------------------------------------------
       
   203 //
       
   204 void CIpsPlgMruList::SetMruListL( const TFSMailMsgId& aMailBoxId,
       
   205     MDesCArray*  aNewMruList )
       
   206     {
       
   207     FUNC_LOG;
       
   208     // create iRepository if not yet created
       
   209     CreateRepositoryL( iRepository );
       
   210 
       
   211     TUint32 mboxKey = KMruListIncorrectMBoxKey;
       
   212     iKeys.Reset();
       
   213     // get mailbox keys to keys array and
       
   214     // check if mailboxid is already stored in some key
       
   215     TInt error = GetMailboxKeyFromRepositoryL(
       
   216         aMailBoxId.Id(), mboxKey, iKeys );
       
   217 
       
   218     if ( KErrNone == error )
       
   219         {
       
   220         // mailbox found
       
   221         TUint32 errorKey;
       
   222         error = iRepository->Delete( mboxKey, KMruListValueMask, errorKey );
       
   223         HandleRepositoryError( mboxKey, error );
       
   224         }
       
   225     else
       
   226         {
       
   227         // mailbox not found, create new key
       
   228         mboxKey = GetUnusedMboxKey( iKeys );
       
   229         }
       
   230 
       
   231     // set new values to central repository
       
   232     // create mailboxid key
       
   233     error = iRepository->Create( mboxKey, TInt( aMailBoxId.Id() ) );
       
   234     // handle error
       
   235     TInt count( aNewMruList->MdcaCount() );
       
   236     // <cmail>
       
   237     for ( TInt i(0); error == KErrNone && i < count; i += 2 )
       
   238         {
       
   239         if( i + 1 < count )
       
   240             {
       
   241             TPtrC displayName  = aNewMruList->MdcaPoint(i);
       
   242             TPtrC emailAddress = aNewMruList->MdcaPoint(i + 1);
       
   243 
       
   244             // Need to locate angle brackets from end of the data, if there
       
   245             // is display name with special characters
       
   246             TInt start( emailAddress.LocateReverse( KIpsSmtpOperationCharLessThan ) ); 
       
   247             TInt end( emailAddress.LocateReverse( KIpsSmtpOperationCharMoreThan ) );
       
   248             
       
   249             if ( KErrNotFound != start && KErrNotFound != end )
       
   250                 {
       
   251                 start += 1;
       
   252                 emailAddress.Set( aNewMruList->MdcaPoint(i + 1).Mid( start, ( end - start ) ) );
       
   253                 }
       
   254 
       
   255             // check validity of email before saving diplay name and address
       
   256             if( IpsSetUtils::IsValidEmailAddressL(emailAddress) )
       
   257                 {
       
   258                 // set values to ascending keys
       
   259                 mboxKey++;
       
   260 
       
   261                 if ( aNewMruList->MdcaPoint(i).Length() <= KMruListMaxDesSize )
       
   262                     {
       
   263                     error = iRepository->Create( mboxKey, aNewMruList->MdcaPoint(i) );
       
   264                     // handle errors
       
   265                     }
       
   266                 else
       
   267                     {
       
   268                     TDesC des = aNewMruList->MdcaPoint(i);
       
   269                     error = iRepository->Create(
       
   270                         mboxKey, des.Mid(0,KMruListMaxDesSize) );
       
   271                     }
       
   272                 
       
   273                 mboxKey++;
       
   274 
       
   275                 if( error == KErrNone )
       
   276                     {
       
   277                     if ( aNewMruList->MdcaPoint(i + 1).Length() <= KMruListMaxDesSize )
       
   278                         {
       
   279                         error = iRepository->Create( mboxKey, aNewMruList->MdcaPoint(i + 1) );
       
   280                         }
       
   281                     else
       
   282                         {
       
   283                         TDesC des = aNewMruList->MdcaPoint(i + 1);
       
   284                         error = iRepository->Create(
       
   285                             mboxKey, des.Mid(0,KMruListMaxDesSize) );
       
   286                         }
       
   287                     }
       
   288                 }
       
   289             }
       
   290         }
       
   291     // </cmail>
       
   292 
       
   293     if ( KErrNone != error )
       
   294         {
       
   295         HandleRepositoryError( mboxKey, error );
       
   296         }
       
   297     }
       
   298 
       
   299 // ---------------------------------------------------------------------------
       
   300 // CIpsPlgMruList::ClearDataL
       
   301 // ---------------------------------------------------------------------------
       
   302 //
       
   303 void CIpsPlgMruList::ClearDataL( const TFSMailMsgId& aMailBoxId )
       
   304 	{
       
   305     FUNC_LOG;
       
   306 	CreateRepositoryL( iRepository );
       
   307 
       
   308     TUint32 mboxKey = KMruListIncorrectMBoxKey;
       
   309     iKeys.Reset();
       
   310     // get mailbox keys to keys array and
       
   311     // check if mailboxid is already sotred in some key
       
   312     TInt error = GetMailboxKeyFromRepositoryL(
       
   313         aMailBoxId.Id(), mboxKey, iKeys );
       
   314 
       
   315     // we have found correct mboxKey
       
   316     if ( KErrNone == error )
       
   317         {
       
   318         TUint32 errorKey;
       
   319         error = iRepository->Delete( mboxKey, KMruListValueMask, errorKey );
       
   320         HandleRepositoryError( mboxKey, error );
       
   321         }
       
   322 	}
       
   323 
       
   324 // ---------------------------------------------------------------------------
       
   325 // CIpsPlgMruList::StoreToArrayL
       
   326 // ---------------------------------------------------------------------------
       
   327 //
       
   328 TInt CIpsPlgMruList::StoreToArrayL( TUint32 aKey )
       
   329     {
       
   330     FUNC_LOG;
       
   331     HBufC* hbuf = HBufC::NewLC( KMruListPrefDesSize );
       
   332 
       
   333     TPtr pdes = hbuf->Des();
       
   334     TInt actualSize(0);
       
   335 
       
   336     TInt error = iRepository->Get( aKey, pdes, actualSize );
       
   337 
       
   338     // the case descriptor is larger than pref size
       
   339     if ( error == KErrOverflow )
       
   340         {
       
   341         if ( actualSize > KMruListMaxDesSize )
       
   342             {
       
   343             // centrep contains too large descriptors
       
   344             // impossible if centrep is edited only by this code,
       
   345             // set corrupted
       
   346             error = KErrCorrupt;
       
   347             }
       
   348         else
       
   349             {
       
   350             CleanupStack::PopAndDestroy( hbuf );
       
   351             hbuf = HBufC::NewLC( actualSize );
       
   352             TPtr pdes2 = hbuf->Des();
       
   353             error = iRepository->Get( aKey, pdes2 );
       
   354             }
       
   355         }
       
   356 
       
   357     // add or delete
       
   358     if ( KErrNone == error )
       
   359         {
       
   360         iHbufArray.AppendL( hbuf );
       
   361         CleanupStack::Pop( hbuf );
       
   362         }
       
   363     else
       
   364         {
       
   365         CleanupStack::PopAndDestroy( hbuf );
       
   366         }
       
   367     return error;
       
   368     }
       
   369 
       
   370 // ---------------------------------------------------------------------------
       
   371 // CIpsPlgMruList::GetMailboxKeyFromRepositoryL
       
   372 // ---------------------------------------------------------------------------
       
   373 //
       
   374 TInt CIpsPlgMruList::GetMailboxKeyFromRepositoryL(
       
   375     TInt aMboxId,
       
   376     TUint32& aMboxKey,
       
   377     RArray<TUint32>& aArray )
       
   378     {
       
   379     FUNC_LOG;
       
   380     // keys is found from centrep with partkey 0x0 and mask 0x0000FFFF
       
   381     // result is array of keys that maches to pattern 0x****0000
       
   382     TInt error = iRepository->FindL(
       
   383         KMruListMailboxPartKey, KMruListMailboxMask, aArray );
       
   384 
       
   385     // No need to handle other errors than not found,
       
   386     // this function should only return KErrNone or KErrNotFound
       
   387     TInt tempId;
       
   388     error = KErrNotFound;
       
   389 
       
   390     //find correct mailbox key from array:
       
   391     for ( TInt i = 0; error == KErrNotFound && i < aArray.Count(); i++ )
       
   392         {
       
   393         if ( iRepository->Get( aArray[i], tempId ) == KErrNone
       
   394             && tempId == aMboxId )
       
   395             {
       
   396             aMboxKey = aArray[i];
       
   397             error = KErrNone;
       
   398             }
       
   399         }
       
   400 
       
   401     return error;
       
   402     }
       
   403 
       
   404 // ---------------------------------------------------------------------------
       
   405 // CIpsPlgMruList::GetUnusedMboxKey
       
   406 // ---------------------------------------------------------------------------
       
   407 //
       
   408 TUint32 CIpsPlgMruList::GetUnusedMboxKey( RArray<TUint32>& aKeys )
       
   409     {
       
   410     FUNC_LOG;
       
   411     // Warning: this function is complex and may contain bugs!!!
       
   412 
       
   413     TUint32 newKey = aKeys.Count()*KMruListKeyBaseValue;
       
   414 
       
   415     if ( aKeys.Count() > 1 )
       
   416         {
       
   417         // this loop finds smallest unused key from sorted key array,
       
   418         // if array[i] == i*basekey at every i when i == 0...count-1
       
   419         // then newKey is not changed
       
   420         aKeys.SortUnsigned();
       
   421         TUint32 tempKey = newKey;
       
   422         for ( TInt i = 0; tempKey == newKey && i < ( aKeys.Count() - 1); i++ )
       
   423             {
       
   424             if ( aKeys[i] + KMruListKeyBaseValue != aKeys[i+1] )
       
   425                 {
       
   426                 tempKey = aKeys[i] + KMruListKeyBaseValue;
       
   427                 }
       
   428             }
       
   429         newKey = tempKey;
       
   430         }
       
   431     else if ( aKeys.Count() == 1 && aKeys[0] == KMruListKeyBaseValue )
       
   432         {
       
   433         newKey = 0;
       
   434         }
       
   435 
       
   436 
       
   437     return newKey;
       
   438     }
       
   439 
       
   440 // ---------------------------------------------------------------------------
       
   441 // CIpsPlgMruList::HandleRepositoryError
       
   442 // ---------------------------------------------------------------------------
       
   443 //
       
   444 void CIpsPlgMruList::HandleRepositoryError( TUint32 aMboxKey, TInt aError )
       
   445     {
       
   446     FUNC_LOG;
       
   447     TBool doClear = EFalse;
       
   448     switch ( aError )
       
   449         {
       
   450             // log these error cases
       
   451         case KErrNone:
       
   452             // no error
       
   453             break;
       
   454         case KErrNotFound:
       
   455             // no serious error
       
   456             break;
       
   457         case KErrPermissionDenied:
       
   458             // this should be panic because application has no
       
   459             // privileges for mru repository
       
   460             break;
       
   461         case KErrAbort:
       
   462             // transaction has previously failed
       
   463             // clear mailbox data if it helps...
       
   464             doClear = ETrue;
       
   465             break;
       
   466         case KErrArgument:
       
   467             // error in argument, for ex. trying to put integer to descriptor
       
   468             // key, solution clear all keys matching aMailbox
       
   469             doClear = ETrue;
       
   470             break;
       
   471         case KErrCorrupt:
       
   472             // just clear all keys matching aMailbox
       
   473             doClear = ETrue;
       
   474             break;
       
   475         case KErrAlreadyExists:
       
   476             // should handle elsewhere, if ends here clear data
       
   477             doClear = ETrue;
       
   478             break;
       
   479         default:
       
   480             // something wrong , clear data
       
   481             doClear = ETrue;
       
   482             break;
       
   483         }
       
   484 
       
   485     if ( doClear )
       
   486         {
       
   487         // don't care about errors...
       
   488         TUint32 errorKey;
       
   489         TInt error = iRepository->Delete( aMboxKey, KMruListValueMask, errorKey );
       
   490         }
       
   491     }
       
   492 
       
   493 
       
   494 // End of file
       
   495 
       
   496