meetingrequest/mrgui/mrfieldbuilderpluginextension/src/cesmrclslistshandler.cpp
changeset 0 8466d47a6819
child 8 e1b6206813b4
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2007-2009 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:  CLS lists class implementation
       
    15  *
       
    16 */
       
    17 
       
    18 #include "emailtrace.h"
       
    19 #include "cesmrclslistshandler.h"
       
    20 
       
    21 #include <eikenv.h>                                 // CEikonEnv
       
    22 #include <centralrepository.h>                      // CRepository
       
    23 #include <AknFepInternalCRKeys.h>                   // KCRUidAknFep
       
    24 #include <CPbkContactEngine.h>                      // CPbkContactEngine
       
    25 #include <CPbkContactItem.h>                        // CPbkContactItem, TPbkContactItemField
       
    26 #include <aknnotewrappers.h>                        // CAknErrorNote
       
    27 //<cmail>
       
    28 #include "CFSMailBox.h"                             // cfsmailbox
       
    29 //</cmail>
       
    30 #include <CPsRequestHandler.h>
       
    31 #include <CPsSettings.h>
       
    32 #include <CPcsDefs.h>
       
    33 #include <VPbkEng.rsg>
       
    34 #include <e32const.h>                               // TLanguage
       
    35 
       
    36 #include <CVPbkContactManager.h>
       
    37 
       
    38 #include "mesmrclslistsobserver.h"
       
    39 #include "cesmrclsmatchobserver.h"
       
    40 #include "cesmrinputmodeobserver.h"
       
    41 #include "esmrfieldbuilderdef.h"
       
    42 
       
    43 // CONSTANTS
       
    44 
       
    45 // Unnamed namespace for local definitionsm functions and templates
       
    46 namespace{ // codescanner::namespace
       
    47 
       
    48 const TInt KMaxMRUMatches = 50;
       
    49 const TInt KNumDisplayFields = 6;
       
    50 const TInt KMaxUriLen = 50;
       
    51 
       
    52 template<typename T> class CleanupResetAndDestroyClose
       
    53     {
       
    54     public:
       
    55         inline static void PushL( T& aRef );
       
    56     private:
       
    57         static void Close( TAny *aPtr );
       
    58     };
       
    59 
       
    60 template<typename T> inline void CleanupResetAndDestroyClosePushL( T& aRef );
       
    61 
       
    62 template<typename T> inline void CleanupResetAndDestroyClose<T>::PushL( T& aRef )
       
    63     {
       
    64     CleanupStack::PushL( TCleanupItem( &Close, &aRef ) );
       
    65     }
       
    66 
       
    67 template<typename T> void CleanupResetAndDestroyClose<T>::Close( TAny *aPtr )
       
    68     {
       
    69     static_cast<T*>(aPtr)->ResetAndDestroy();
       
    70     static_cast<T*>(aPtr)->Close();
       
    71     }
       
    72 
       
    73 template<typename T> inline void CleanupResetAndDestroyClosePushL( T& aRef )
       
    74     {
       
    75     CleanupResetAndDestroyClose<T>::PushL( aRef );
       
    76     }
       
    77 
       
    78 }//namespace
       
    79 
       
    80 // ======== MEMBER FUNCTIONS ========
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CESMRClsListsHandler::NewL
       
    84 // -----------------------------------------------------------------------------
       
    85 CESMRClsListsHandler* CESMRClsListsHandler::NewL( RFs& aFs, CVPbkContactManager* aContactManager )
       
    86 
       
    87     {
       
    88     FUNC_LOG;
       
    89     CESMRClsListsHandler* object =  CESMRClsListsHandler::NewLC( aFs, aContactManager );
       
    90     CleanupStack::Pop( object );
       
    91     return object;
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CESMRClsListsHandler::NewLC
       
    96 // -----------------------------------------------------------------------------
       
    97 CESMRClsListsHandler* CESMRClsListsHandler::NewLC( RFs& aFs, CVPbkContactManager* aContactManager )
       
    98     {
       
    99     FUNC_LOG;
       
   100     CESMRClsListsHandler* object = new (ELeave) CESMRClsListsHandler( aFs, aContactManager );
       
   101     CleanupStack::PushL( object );
       
   102     object->ConstructL();
       
   103     return object;
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CESMRClsListsHandler::~CESMRClsListsHandler
       
   108 // -----------------------------------------------------------------------------
       
   109 CESMRClsListsHandler::~CESMRClsListsHandler( )
       
   110     {
       
   111     FUNC_LOG;
       
   112     delete iSearchedText;
       
   113     delete iPcsMatchObserver;
       
   114 
       
   115     iMatchingCompleteContacts.Reset();
       
   116     iMatchingMRUContacts.Reset();
       
   117     iMatchingMissingEmailContacts.Reset();
       
   118 
       
   119     iMatchingCompleteContacts.Close();
       
   120     iMatchingMRUContacts.Close();
       
   121     iMatchingMissingEmailContacts.Close();
       
   122 
       
   123     delete iAllMRUContacts;
       
   124     delete iInputObserver;
       
   125     delete iAknFepCenRep;
       
   126     delete iRequestHandler;
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CESMRClsListsHandler::UpdateContactMatchListsL
       
   131 // -----------------------------------------------------------------------------
       
   132 void CESMRClsListsHandler::UpdateContactMatchListsL( const RPointerArray<CESMRClsItem>& aMatches )
       
   133     {
       
   134     FUNC_LOG;
       
   135     for( TInt i = 0 ; i < aMatches.Count() ; ++i )
       
   136         {
       
   137         if( aMatches[i]->IsMruItem() )
       
   138             {
       
   139             // If this is MRU item, we need to check that it doesn't
       
   140             // already exist in iMatchingCompleteContacts
       
   141             TInt itemIndex = FindDuplicate( *aMatches[i], iMatchingCompleteContacts );
       
   142             if( itemIndex != KErrNotFound )
       
   143                 {
       
   144                 // Found, no need to add this, free memory
       
   145                 }
       
   146             else
       
   147                 {
       
   148                 // Not found, add new to MRU list
       
   149                 iMatchingMRUContacts.AppendL( aMatches[i] );
       
   150                 }
       
   151             }
       
   152         else if( aMatches[i]->EmailAddress().Length() > 0 )
       
   153             {
       
   154             // If this is Phonebook item, we need to check that it doesn't
       
   155             // already exist in iMatchingMRUContacts
       
   156             TInt itemIndex = FindDuplicate( *aMatches[i], iMatchingMRUContacts );
       
   157             if( itemIndex != KErrNotFound )
       
   158                 {
       
   159                 // Found, this needs to be removed from the MRU list
       
   160                 iMatchingMRUContacts.Remove( itemIndex );
       
   161                 }
       
   162 
       
   163             // Phonebook items are always added to the top of the list
       
   164             iMatchingCompleteContacts.AppendL( aMatches[i] );
       
   165             }
       
   166         else
       
   167             {
       
   168             // No email, nothing to compare, so add this always to the end of the list
       
   169             iMatchingMissingEmailContacts.AppendL( aMatches[i] );
       
   170             }
       
   171         }
       
   172 
       
   173     RPointerArray<CESMRClsItem> allMatches = ConstructOneListL( iMatchingCompleteContacts,
       
   174                                                                       iMatchingMRUContacts,
       
   175                                                                       iMatchingMissingEmailContacts );
       
   176     if(iClsListObserver)
       
   177         {
       
   178         iClsListObserver->ArrayUpdatedL( allMatches );
       
   179         }
       
   180     
       
   181     allMatches.ResetAndDestroy();
       
   182     }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CESMRClsListsHandler::InputModeChangedL
       
   186 // -----------------------------------------------------------------------------
       
   187 void CESMRClsListsHandler::InputModeChangedL( TKeyboardModes aNewInputMode )
       
   188     {
       
   189     FUNC_LOG;
       
   190     iPcsMatchObserver->SetInputMode( aNewInputMode );
       
   191     }
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 // CESMRClsListsHandler::CurrentSearchText
       
   195 // -----------------------------------------------------------------------------
       
   196 const TDesC& CESMRClsListsHandler::CurrentSearchText( )
       
   197     {
       
   198     FUNC_LOG;
       
   199     return *iSearchedText;
       
   200     }
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // CESMRClsListsHandler::SearchMatchesL
       
   204 // -----------------------------------------------------------------------------
       
   205 void CESMRClsListsHandler::SearchMatchesL( const TDesC& aText )
       
   206     {
       
   207     FUNC_LOG;
       
   208     iMatchingCompleteContacts.Reset();
       
   209     iMatchingMRUContacts.Reset();
       
   210     iMatchingMissingEmailContacts.Reset();
       
   211 
       
   212     delete iSearchedText;
       
   213     iSearchedText = NULL;
       
   214     iSearchedText = aText.AllocL();
       
   215 
       
   216     iPcsMatchObserver->SearchMatchesL( aText );
       
   217     }
       
   218 
       
   219 // -----------------------------------------------------------------------------
       
   220 // CESMRClsListsHandler::CESMRClsListsHandler
       
   221 // -----------------------------------------------------------------------------
       
   222 CESMRClsListsHandler::CESMRClsListsHandler( RFs& aFs, CVPbkContactManager* aContactManager ) :
       
   223     iContactManager( aContactManager ),
       
   224     iFs( aFs )
       
   225     {
       
   226     FUNC_LOG;
       
   227     // do nothing
       
   228     }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CESMRClsListsHandler::ConstructL
       
   232 // -----------------------------------------------------------------------------
       
   233 void CESMRClsListsHandler::ConstructL( )
       
   234     {
       
   235     FUNC_LOG;
       
   236     iRequestHandler = CPSRequestHandler::NewL();
       
   237 
       
   238     iAknFepCenRep = CRepository::NewL( KCRUidAknFep );
       
   239 
       
   240     iPcsMatchObserver = CESMRClsMatchObserver::NewL( *iAknFepCenRep, *this, *iRequestHandler, iContactManager );
       
   241     SetSearchSettingsForPcsMatchObserverL();
       
   242 
       
   243     // Monitors inputMode changes (predictive vs. non-predictive
       
   244     iInputObserver = CESMRInputModeObserver::NewL( *iAknFepCenRep, *this );
       
   245 
       
   246     if( !iSearchedText )
       
   247         iSearchedText = KNullDesC().AllocL();
       
   248     }
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // CESMRClsListsHandler::ReadCLSInfoFromMRUListIndexL
       
   252 // -----------------------------------------------------------------------------
       
   253 TBool CESMRClsListsHandler::ReadCLSInfoFromMRUListIndexL(
       
   254         MDesCArray& aTextArray, CESMRClsItem& aClsItem,
       
   255         const TInt aCurrentMatchIndex, const TInt aPreviousMatchIndex )
       
   256     {
       
   257     FUNC_LOG;
       
   258     TBool retVal = EFalse;
       
   259     // This should be even number
       
   260     TInt currentRealItemIndex = aCurrentMatchIndex - ( aCurrentMatchIndex % 2 );
       
   261     // Here we need to check if match is found both from the display name and email address
       
   262     if( currentRealItemIndex != aPreviousMatchIndex )
       
   263         {
       
   264         aClsItem.SetDisplayNameL( aTextArray.MdcaPoint( currentRealItemIndex )  );
       
   265         aClsItem.SetEmailAddressL( aTextArray.MdcaPoint( currentRealItemIndex + 1 )  );
       
   266         retVal = ETrue;
       
   267         }
       
   268     return retVal;
       
   269     }
       
   270 
       
   271 // -----------------------------------------------------------------------------
       
   272 // CESMRClsListsHandler::FindDuplicate
       
   273 // -----------------------------------------------------------------------------
       
   274 TInt CESMRClsListsHandler::FindDuplicate( const CESMRClsItem& aClsItem,
       
   275           RPointerArray<CESMRClsItem>& aContacts )
       
   276     {
       
   277     FUNC_LOG;
       
   278     // find duplicate email addresses from aContacts
       
   279     const TDesC& emailAddress = aClsItem.EmailAddress();
       
   280 
       
   281     for( TInt i = 0; i < aContacts.Count(); ++i )
       
   282         {
       
   283         const TDesC& currentItemEmailAddress = aContacts[i]->EmailAddress();
       
   284 
       
   285         if( emailAddress.CompareF( currentItemEmailAddress ) == 0 )
       
   286             {
       
   287             return i;
       
   288             }
       
   289         }
       
   290 
       
   291     return KErrNotFound;
       
   292     }
       
   293 
       
   294 // -----------------------------------------------------------------------------
       
   295 // CESMRClsListsHandler::ConstructOneListL
       
   296 // -----------------------------------------------------------------------------
       
   297 RPointerArray<CESMRClsItem> CESMRClsListsHandler::ConstructOneListL(
       
   298         const RPointerArray<CESMRClsItem>& aContactMatchesWithEmail,
       
   299         const RPointerArray<CESMRClsItem>& aMRUMatches,
       
   300         const RPointerArray<CESMRClsItem>& aContactMatchesWithoutEmail )
       
   301     {
       
   302     RPointerArray<CESMRClsItem> allMatches;
       
   303     CleanupResetAndDestroyClosePushL( allMatches );
       
   304 
       
   305     // Copy all the objects from three other lists to the all matches list
       
   306     TInt matchingComleteItemsCount = aContactMatchesWithEmail.Count();
       
   307     for( TInt i = 0 ; i <  matchingComleteItemsCount; i++ )
       
   308         {
       
   309         CESMRClsItem* newClsItem = CopyClsItemLC( *aContactMatchesWithEmail[i] );
       
   310         allMatches.AppendL( newClsItem );
       
   311         CleanupStack::Pop( newClsItem );
       
   312         }
       
   313 
       
   314     // max MRU matches count limited to 50
       
   315     TInt matchingMRUItemsCount = aMRUMatches.Count();
       
   316     for( TInt i = 0; i < matchingMRUItemsCount && i < KMaxMRUMatches; i++ )
       
   317         {
       
   318         CESMRClsItem* newClsItem = CopyClsItemLC( *aMRUMatches[i] );
       
   319         allMatches.AppendL( newClsItem );
       
   320         CleanupStack::Pop( newClsItem );
       
   321         }
       
   322 
       
   323     TInt mathingContactItemsWithoutEmailCount = aContactMatchesWithoutEmail.Count();
       
   324     for( TInt i = 0 ; i < mathingContactItemsWithoutEmailCount ; i++ )
       
   325         {
       
   326         CESMRClsItem* newClsItem = CopyClsItemLC( *aContactMatchesWithoutEmail[i] );
       
   327         allMatches.AppendL( newClsItem );
       
   328         CleanupStack::Pop( newClsItem );
       
   329         }
       
   330 
       
   331     CleanupStack::Pop( &allMatches );
       
   332     return allMatches;
       
   333     }
       
   334 
       
   335 // -----------------------------------------------------------------------------
       
   336 // CESMRClsListsHandler::CopyClsItemL
       
   337 // -----------------------------------------------------------------------------
       
   338 CESMRClsItem* CESMRClsListsHandler::CopyClsItemLC( const CESMRClsItem& aClsItem )
       
   339     {
       
   340     FUNC_LOG;
       
   341     return aClsItem.CloneLC();
       
   342     }
       
   343 
       
   344 // -----------------------------------------------------------------------------
       
   345 // CESMRClsListsHandler::SetSearchSettingsForPcsMatchObserverL()
       
   346 // -----------------------------------------------------------------------------
       
   347 void CESMRClsListsHandler::SetSearchSettingsForPcsMatchObserverL()
       
   348     {
       
   349     FUNC_LOG;
       
   350     // Create predictive search settings
       
   351     CPsSettings* searchSettings = CPsSettings::NewL();
       
   352     CleanupStack::PushL(searchSettings);
       
   353 
       
   354     RPointerArray<TDesC> databases;
       
   355     CleanupClosePushL(databases);
       
   356 
       
   357     HBufC* store1 = HBufC::NewLC(KMaxUriLen);
       
   358     store1->Des().Copy( KVPbkDefaultCntDbURI ); // To specify phone contacts store
       
   359     databases.AppendL(store1);
       
   360     HBufC* store2 = NULL;
       
   361     if ( iMailBox ) 
       
   362         {
       
   363         store2 = HBufC::NewLC(KMaxUriLen);
       
   364         GetMruDatastoreUriFromMailbox( *iMailBox, *store2 );
       
   365         databases.AppendL( store2 );
       
   366         }
       
   367     searchSettings->SetSearchUrisL(databases);
       
   368 
       
   369     // Set displayfields (first name, last name, email addresses)
       
   370     RArray<TInt> displayFields(KNumDisplayFields);
       
   371     CleanupClosePushL(displayFields);
       
   372     displayFields.AppendL(R_VPBK_FIELD_TYPE_FIRSTNAME);
       
   373     displayFields.AppendL(R_VPBK_FIELD_TYPE_LASTNAME);
       
   374     displayFields.AppendL(R_VPBK_FIELD_TYPE_EMAILGEN);
       
   375     displayFields.AppendL(R_VPBK_FIELD_TYPE_EMAILHOME);
       
   376     displayFields.AppendL(R_VPBK_FIELD_TYPE_EMAILWORK);
       
   377     searchSettings->SetDisplayFieldsL(displayFields);
       
   378     CleanupStack::Pop(&displayFields);
       
   379     displayFields.Close();
       
   380 
       
   381     // Set the new sort order of data fields
       
   382     RArray<TInt> sortOrder;
       
   383     CleanupClosePushL(sortOrder);
       
   384     sortOrder.AppendL(R_VPBK_FIELD_TYPE_FIRSTNAME);
       
   385     sortOrder.AppendL(R_VPBK_FIELD_TYPE_LASTNAME);
       
   386     sortOrder.AppendL(R_VPBK_FIELD_TYPE_EMAILGEN);
       
   387     sortOrder.AppendL(R_VPBK_FIELD_TYPE_EMAILHOME);
       
   388     sortOrder.AppendL(R_VPBK_FIELD_TYPE_EMAILWORK);
       
   389     iRequestHandler->ChangeSortOrderL(*store1, sortOrder);
       
   390     if ( store2 )
       
   391         {
       
   392         iRequestHandler->ChangeSortOrderL(*store2, sortOrder);
       
   393         }
       
   394 
       
   395     CleanupStack::Pop(&sortOrder);
       
   396     sortOrder.Close();
       
   397     
       
   398     // Set the new search settings
       
   399     iRequestHandler->SetSearchSettingsL(*searchSettings);
       
   400 
       
   401     if ( store2 )
       
   402         {
       
   403         CleanupStack::PopAndDestroy(store2);
       
   404         }
       
   405     CleanupStack::PopAndDestroy(store1);
       
   406     CleanupStack::Pop(&databases);
       
   407     databases.Close();
       
   408     CleanupStack::PopAndDestroy(searchSettings);
       
   409     }
       
   410 
       
   411 // -----------------------------------------------------------------------------
       
   412 // CESMRClsListsHandler::isLanguageSupportedL()
       
   413 // -----------------------------------------------------------------------------
       
   414 TBool CESMRClsListsHandler::IsLanguageSupportedL()
       
   415     {
       
   416     FUNC_LOG;
       
   417     // Get the current language
       
   418     TLanguage lang = User::Language();
       
   419 
       
   420     // Check for language support
       
   421     return iRequestHandler->IsLanguageSupportedL(lang);
       
   422     }
       
   423 // -----------------------------------------------------------------------------
       
   424 // CESMRClsListsHandler::GetMruDatastoreUriFromMailbox()
       
   425 // -----------------------------------------------------------------------------
       
   426 void CESMRClsListsHandler::GetMruDatastoreUriFromMailbox( CFSMailBox& aMailbox, HBufC& aUri )
       
   427     {
       
   428     FUNC_LOG;
       
   429     aUri.Des().Copy( KDefaultMailBoxURI );
       
   430     aUri.Des().AppendNum( aMailbox.GetId().PluginId().iUid );
       
   431     aUri.Des().Append( KDefaultMailBoxURISeparator );
       
   432     aUri.Des().AppendNum( aMailbox.GetId().Id() );
       
   433     }
       
   434 // -----------------------------------------------------------------------------
       
   435 // CESMRClsListsHandler::SetObserver()
       
   436 // -----------------------------------------------------------------------------
       
   437 void CESMRClsListsHandler::SetObserver( MESMRClsListsObserver* aClsListObserver )
       
   438     {
       
   439     FUNC_LOG;
       
   440     iClsListObserver = aClsListObserver;
       
   441     }
       
   442 // -----------------------------------------------------------------------------
       
   443 // CESMRClsListsHandler::SetCurrentMailboxL()
       
   444 // -----------------------------------------------------------------------------
       
   445 void CESMRClsListsHandler::SetCurrentMailboxL( CFSMailBox* aMailBox )
       
   446     {
       
   447     FUNC_LOG;
       
   448     TFSMailMsgId currentMailboxId;
       
   449     TFSMailMsgId newMailboxId;
       
   450 
       
   451     if( iMailBox )
       
   452         {
       
   453         currentMailboxId = iMailBox->GetId();
       
   454         }
       
   455     if( aMailBox )
       
   456         {
       
   457         newMailboxId = aMailBox->GetId();
       
   458         }
       
   459 
       
   460     if( currentMailboxId != newMailboxId )
       
   461         {
       
   462         iMailBox = aMailBox;
       
   463         SetSearchSettingsForPcsMatchObserverL();
       
   464         }
       
   465     }
       
   466 // -----------------------------------------------------------------------------
       
   467 // CESMRClsListsHandler::OperationErrorL()
       
   468 // -----------------------------------------------------------------------------
       
   469 void CESMRClsListsHandler::OperationErrorL( TInt aErrorCode )
       
   470     {
       
   471     FUNC_LOG;
       
   472     if ( iClsListObserver  )
       
   473         {
       
   474         iClsListObserver->OperationErrorL( aErrorCode );
       
   475         }
       
   476     }
       
   477