locationlandmarksrefappfors60/Src/LandmarksCategoriesEngine.cpp
branchRCL_3
changeset 17 1fc85118c3ae
parent 16 8173571d354e
child 18 870918037e16
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
     1 /*
       
     2 * Copyright (c) 2004-2005 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:  Implementation of CLandmarksCategoriesEngine
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <EPos_CPosLandmarkDatabase.h>
       
    21 #include <EPos_CPosLmCatNameCriteria.h>
       
    22 #include <EPos_CPosLandmarkSearch.h>
       
    23 #include <EPos_CPosLmCategoryManager.h>
       
    24 
       
    25 #include "LandmarksCategoriesEngine.h"
       
    26 #include "LandmarksLmOpWrapper.h"
       
    27 
       
    28 // ============================ MEMBER FUNCTIONS ===============================
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 CLandmarksCategoriesEngine::CLandmarksCategoriesEngine(
       
    34     CPosLandmarkDatabase& aDb)
       
    35 :   CLandmarksEngineBase(aDb)
       
    36     {
       
    37     }
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 void CLandmarksCategoriesEngine::ConstructL()
       
    43     {
       
    44     CLandmarksEngineBase::ConstructL();
       
    45     iCatManager = CPosLmCategoryManager::NewL(iDb);
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 CLandmarksCategoriesEngine* CLandmarksCategoriesEngine::NewL(
       
    52     CPosLandmarkDatabase& aDb)
       
    53     {
       
    54     CLandmarksCategoriesEngine* self = 
       
    55         new (ELeave) CLandmarksCategoriesEngine(aDb);
       
    56     CleanupStack::PushL(self);
       
    57     self->ConstructL();
       
    58     CleanupStack::Pop(self);
       
    59     return self;
       
    60     }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 CLandmarksCategoriesEngine::~CLandmarksCategoriesEngine()
       
    66     {
       
    67     Cancel();
       
    68     delete iCatManager;
       
    69     iSubSetOfIds.Close();
       
    70     iItemIds.Close();
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 void CLandmarksCategoriesEngine::AddCategoryL(CPosLandmarkCategory& aCategory)
       
    77     {
       
    78     iCatManager->AddCategoryL(aCategory);
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 void CLandmarksCategoriesEngine::UpdateCategoryL(const CPosLandmarkCategory& aCategory)
       
    85     {
       
    86     iCatManager->UpdateCategoryL(aCategory);
       
    87     }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 void CLandmarksCategoriesEngine::DeleteCategoryL(TPosLmItemId aItemId)
       
    93     {
       
    94     ExecuteAndDeleteLD(iCatManager->RemoveCategoryL(aItemId));
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 CPosLandmarkCategory* CLandmarksCategoriesEngine::CategoryLC(TPosLmItemId aItemId)
       
   101     {
       
   102     return iCatManager->ReadCategoryLC(aItemId);
       
   103     }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 void CLandmarksCategoriesEngine::StartSearchingCategoriesL(
       
   109     MLandmarksOperationObserver* aObserver)
       
   110     {
       
   111     // We need to cancel if we are searching/reading categories.
       
   112     Cancel();
       
   113 
       
   114     iObserver = aObserver;
       
   115     iSearchResultExists = EFalse;
       
   116     iFilterSearch = EFalse;
       
   117 
       
   118     // Complete ourselves and start search for all categories in db in RunL().
       
   119     iStatus = KRequestPending;
       
   120     SetPriorityAndSetActive();
       
   121     iActiveOperation = ECategorySearch;
       
   122     TRequestStatus* status = &iStatus;
       
   123     User::RequestComplete(status, KErrNone);
       
   124     }
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 void CLandmarksCategoriesEngine::StartSearchingCategoriesL(
       
   130     const TDesC& aSearchPattern, 
       
   131     TBool aSearchOnlyInPreviousMatches,
       
   132     MLandmarksOperationObserver* aObserver)
       
   133     {
       
   134     // We need to cancel if we are searching/reading categories.
       
   135     Cancel();
       
   136 
       
   137     if (!iSearchResultExists)
       
   138         {
       
   139         // There are no previous results or previous search did not complete.
       
   140         // We cannot use the result from it.
       
   141         aSearchOnlyInPreviousMatches = EFalse;
       
   142         }
       
   143     iSearchResultExists = EFalse;
       
   144     iFilterSearch = ETrue;
       
   145 
       
   146     // Set search criteria
       
   147     CPosLmCatNameCriteria* criteria = CPosLmCatNameCriteria::NewLC();
       
   148     criteria->SetSearchPatternL(aSearchPattern);
       
   149 
       
   150     // Create search operation
       
   151     CPosLmOperation* operation = iSearcher->StartCategorySearchL(
       
   152         *criteria, 
       
   153         CPosLmCategoryManager::ECategorySortOrderNameAscending, 
       
   154         aSearchOnlyInPreviousMatches);
       
   155     CleanupStack::PopAndDestroy(criteria);
       
   156 
       
   157     // Start search operation
       
   158     iObserver = aObserver;
       
   159     iStatus = KRequestPending;
       
   160     iLmOpWrapper->StartOperation(operation, iStatus);
       
   161     SetPriorityAndSetActive();
       
   162     iActiveOperation = ECategorySearch;
       
   163     }
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // -----------------------------------------------------------------------------
       
   167 //
       
   168 RArray<TPosLmItemId>* CLandmarksCategoriesEngine::FetchSearchResult()
       
   169     {
       
   170     return &iItemIds;
       
   171     }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // -----------------------------------------------------------------------------
       
   175 //
       
   176 void CLandmarksCategoriesEngine::StartReadingCategoriesL(
       
   177     TInt aNrOfItemsToReadPerBundle,
       
   178     MLandmarksOperationObserver* aObserver)
       
   179     {
       
   180     // We need to cancel if we are searching/reading landmarks.
       
   181     Cancel();
       
   182 
       
   183     iNrOfItemsToRead = aNrOfItemsToReadPerBundle;
       
   184     iObserver = aObserver;
       
   185     iCurrentItemId = 0;
       
   186 
       
   187     // Start reading a bundle of categories
       
   188     ReadSomeCategoriesL();
       
   189     iActiveOperation = ECategoryRead;
       
   190     }
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 CArrayPtr<CPosLandmarkCategory>* CLandmarksCategoriesEngine::FetchCategoriesLC()
       
   196     {
       
   197     TInt nrOfItems = iSubSetOfIds.Count();
       
   198     CArrayPtr<CPosLandmarkCategory>* categories = 
       
   199         new (ELeave) CArrayPtrFlat<CPosLandmarkCategory>(nrOfItems);
       
   200     CleanupStack::PushL(categories);
       
   201 
       
   202     // Start the actual read of landmarks. This is done synchronously since 
       
   203     // there is no asynchronous read method as there is for landmarks:
       
   204     // (Compare TakePreparedPartialLandmarksL()) 
       
   205     for (TInt i = 0; i < nrOfItems; i++)
       
   206         {
       
   207         CPosLandmarkCategory* category = 
       
   208             iCatManager->ReadCategoryLC(iSubSetOfIds[i]);
       
   209         categories->AppendL(category);
       
   210         CleanupStack::Pop(category);
       
   211         }
       
   212 
       
   213     return categories;
       
   214     }
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // -----------------------------------------------------------------------------
       
   218 //
       
   219 CArrayPtr<CPosLandmarkCategory>* CLandmarksCategoriesEngine::CategoriesL()
       
   220     {
       
   221     const TInt KGranularity = 8;
       
   222     CArrayPtrFlat<CPosLandmarkCategory>* categories = 
       
   223         new (ELeave) CArrayPtrFlat<CPosLandmarkCategory>(KGranularity);
       
   224     CleanupStack::PushL(categories);
       
   225     
       
   226     // Get an iterator for all existing categories sorted ascending by name
       
   227     CPosLmItemIterator* itemIterator = iCatManager->CategoryIteratorL(
       
   228         CPosLmCategoryManager::ECategorySortOrderNameAscending);
       
   229     CleanupStack::PushL(itemIterator);
       
   230 
       
   231     // Read all categories and add them to an array
       
   232     TPosLmItemId catId = itemIterator->NextL();
       
   233     while (catId != KPosLmNullItemId)
       
   234         {
       
   235         CPosLandmarkCategory* category = iCatManager->ReadCategoryLC(catId);
       
   236         categories->AppendL(category);
       
   237         CleanupStack::Pop(category);
       
   238 
       
   239         catId = itemIterator->NextL();
       
   240         }
       
   241     CleanupStack::PopAndDestroy(itemIterator);
       
   242     CleanupStack::Pop(categories);
       
   243 
       
   244     return categories;
       
   245     }
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 void CLandmarksCategoriesEngine::RunL()
       
   251     {
       
   252     if (iStatus == KErrNone)
       
   253         {
       
   254         if (iActiveOperation == ECategorySearch)
       
   255             {
       
   256             // Fetch search result
       
   257             PopulateItemIdArrayL();
       
   258             }
       
   259         else if (iActiveOperation == ECategoryRead)
       
   260             {
       
   261             if (iCurrentItemId < iItemIds.Count())
       
   262                 {
       
   263                 // There are still found landmarks to read
       
   264                 iObserver->NotifyOperationProgressL(
       
   265                     iActiveOperation, KOperationNotReady, KErrNone);
       
   266                 ReadSomeCategoriesL();
       
   267                 }
       
   268             else
       
   269                 {
       
   270                 // No more matches to read
       
   271                 NotifyOperationReadyL(iActiveOperation, KErrNone);
       
   272                 }
       
   273             }
       
   274         }
       
   275     else
       
   276         {
       
   277         // Operation failed. Forward error code
       
   278         NotifyOperationReadyL(iActiveOperation, iStatus.Int());
       
   279         }
       
   280     }
       
   281 
       
   282 // -----------------------------------------------------------------------------
       
   283 // -----------------------------------------------------------------------------
       
   284 //
       
   285 void CLandmarksCategoriesEngine::DoCancel()
       
   286     {
       
   287     iLmOpWrapper->Cancel();
       
   288     if (iActiveOperation == ECategorySearch)
       
   289         {
       
   290         iSearchResultExists = EFalse;
       
   291         }
       
   292     iActiveOperation = ENoOperation;
       
   293     }
       
   294 
       
   295 // -----------------------------------------------------------------------------
       
   296 // -----------------------------------------------------------------------------
       
   297 //
       
   298 TInt CLandmarksCategoriesEngine::RunError(TInt aError)
       
   299     {
       
   300     // Notify user and ignore error code
       
   301     TInt err;
       
   302     TRAP(err, NotifyOperationReadyL(iActiveOperation, aError));
       
   303 
       
   304     return KErrNone;
       
   305     }
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // -----------------------------------------------------------------------------
       
   309 //
       
   310 void CLandmarksCategoriesEngine::ReadSomeCategoriesL()
       
   311     {
       
   312     // Create a subset of the found matches to read
       
   313     iSubSetOfIds.Reset();
       
   314     TInt nrOfMatches = iItemIds.Count();
       
   315     TInt i = 0;
       
   316     while (i < iNrOfItemsToRead && iCurrentItemId < nrOfMatches)
       
   317         {
       
   318         User::LeaveIfError(
       
   319             iSubSetOfIds.Append(iItemIds[iCurrentItemId]));
       
   320         iCurrentItemId++;
       
   321         i++;
       
   322         }
       
   323 
       
   324     // Simulate start reading found matches. There is no asynchronous search 
       
   325     // functionality for categories as there is for landmarks, 
       
   326     // (Compare PreparePartialLandmarksL())
       
   327     iStatus = KRequestPending;
       
   328     SetPriorityAndSetActive();
       
   329     TRequestStatus* status = &iStatus;
       
   330     User::RequestComplete(status, KErrNone);
       
   331     }
       
   332 
       
   333 // -----------------------------------------------------------------------------
       
   334 // -----------------------------------------------------------------------------
       
   335 //
       
   336 void CLandmarksCategoriesEngine::NotifyOperationReadyL(
       
   337     TOperation aOperation, 
       
   338     TInt aErrorCode)
       
   339     {
       
   340     iActiveOperation = ENoOperation;
       
   341     iObserver->NotifyOperationProgressL(
       
   342         aOperation, 
       
   343         KOperationReady, 
       
   344         aErrorCode);
       
   345     }
       
   346 
       
   347 // -----------------------------------------------------------------------------
       
   348 // -----------------------------------------------------------------------------
       
   349 //
       
   350 void CLandmarksCategoriesEngine::PopulateItemIdArrayL()
       
   351     {
       
   352     // Fetch itemIterator
       
   353     CPosLmItemIterator* itemIterator = NULL;
       
   354     if (iFilterSearch)
       
   355         {
       
   356         itemIterator = iSearcher->MatchIteratorL();
       
   357         iSearchResultExists = ETrue;
       
   358         }
       
   359     else
       
   360         {
       
   361         itemIterator = iCatManager->CategoryIteratorL(
       
   362             CPosLmCategoryManager::ECategorySortOrderNameAscending);
       
   363         }
       
   364     CleanupStack::PushL(itemIterator);
       
   365     itemIterator->Reset();
       
   366 
       
   367     // Fetch iItemIds
       
   368     TUint nrOfItems = itemIterator->NumOfItemsL();
       
   369     if (nrOfItems > 0)
       
   370         {
       
   371         TInt startIndex = 0;
       
   372         iItemIds.Reset();
       
   373         itemIterator->GetItemIdsL(iItemIds, startIndex, nrOfItems);
       
   374         NotifyOperationReadyL(iActiveOperation, KErrNone);
       
   375         }
       
   376     else
       
   377         {
       
   378         // No matches found
       
   379         NotifyOperationReadyL(iActiveOperation, KErrNotFound);
       
   380         }
       
   381     CleanupStack::PopAndDestroy(itemIterator);
       
   382     }
       
   383 
       
   384