S60 3rd Edition SDK for Symbian OS
Example Applications Guide

LandmarksCategoriesEngine.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : LandmarksCategoriesEngine.cpp
00004 *  Part of     : Landmark Reference Application
00005 *  Interface   : -
00006 *  Description : Implementation of CLandmarksCategoriesEngine
00007 *  Version     : 1.0
00008 *
00009 *  Copyright (c) 2004-2006 Nokia Corporation.
00010 *  This material, including documentation and any related 
00011 *  computer programs, is protected by copyright controlled by 
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 // INCLUDE FILES
00017 #include <EPos_CPosLandmarkDatabase.h>
00018 #include <EPos_CPosLmCatNameCriteria.h>
00019 #include <EPos_CPosLandmarkSearch.h>
00020 #include <EPos_CPosLmCategoryManager.h>
00021 
00022 #include "LandmarksCategoriesEngine.h"
00023 #include "LandmarksLmOpWrapper.h"
00024 
00025 // ============================ MEMBER FUNCTIONS ===============================
00026 
00027 // -----------------------------------------------------------------------------
00028 // CLandmarksCategoriesEngine::CLandmarksCategoriesEngine
00029 // C++ constructor can NOT contain any code, that
00030 // might leave.
00031 // -----------------------------------------------------------------------------
00032 //
00033 CLandmarksCategoriesEngine::CLandmarksCategoriesEngine(
00034     CPosLandmarkDatabase& aDb)
00035 :   CLandmarksEngineBase(aDb)
00036     {
00037     }
00038 
00039 // -----------------------------------------------------------------------------
00040 // CLandmarksCategoriesEngine::ConstructL
00041 // Symbian 2nd phase constructor can leave.
00042 // -----------------------------------------------------------------------------
00043 //
00044 void CLandmarksCategoriesEngine::ConstructL()
00045     {
00046     CLandmarksEngineBase::ConstructL();
00047     iCatManager = CPosLmCategoryManager::NewL(iDb);
00048     }
00049 
00050 // -----------------------------------------------------------------------------
00051 // CLandmarksCategoriesEngine::NewL
00052 // Two-phased constructor.
00053 // -----------------------------------------------------------------------------
00054 //
00055 CLandmarksCategoriesEngine* CLandmarksCategoriesEngine::NewL(
00056     CPosLandmarkDatabase& aDb)
00057     {
00058     CLandmarksCategoriesEngine* self = 
00059         new (ELeave) CLandmarksCategoriesEngine(aDb);
00060     CleanupStack::PushL(self);
00061     self->ConstructL();
00062     CleanupStack::Pop(self);
00063     return self;
00064     }
00065 
00066 // Destructor
00067 CLandmarksCategoriesEngine::~CLandmarksCategoriesEngine()
00068     {
00069     Cancel();
00070     delete iCatManager;
00071     iSubSetOfIds.Close();
00072     iItemIds.Close();
00073     }
00074 
00075 // -----------------------------------------------------------------------------
00076 // CLandmarksCategoriesEngine::AddCategoryL
00077 // 
00078 // (other items were commented in a header).
00079 // -----------------------------------------------------------------------------
00080 //
00081 void CLandmarksCategoriesEngine::AddCategoryL(CPosLandmarkCategory& aCategory)
00082     {
00083     iCatManager->AddCategoryL(aCategory);
00084     }
00085 
00086 // -----------------------------------------------------------------------------
00087 // CLandmarksCategoriesEngine::UpdateCategoryL
00088 // 
00089 // (other items were commented in a header).
00090 // -----------------------------------------------------------------------------
00091 //
00092 void CLandmarksCategoriesEngine::UpdateCategoryL(const CPosLandmarkCategory& aCategory)
00093     {
00094     iCatManager->UpdateCategoryL(aCategory);
00095     }
00096 
00097 // -----------------------------------------------------------------------------
00098 // CLandmarksCategoriesEngine::DeleteCategoryL
00099 // 
00100 // (other items were commented in a header).
00101 // -----------------------------------------------------------------------------
00102 //
00103 void CLandmarksCategoriesEngine::DeleteCategoryL(TPosLmItemId aItemId)
00104     {
00105     ExecuteAndDeleteLD(iCatManager->RemoveCategoryL(aItemId));
00106     }
00107 
00108 // -----------------------------------------------------------------------------
00109 // CLandmarksCategoriesEngine::CategoryLC
00110 // 
00111 // (other items were commented in a header).
00112 // -----------------------------------------------------------------------------
00113 //
00114 CPosLandmarkCategory* CLandmarksCategoriesEngine::CategoryLC(TPosLmItemId aItemId)
00115     {
00116     return iCatManager->ReadCategoryLC(aItemId);
00117     }
00118 
00119 // -----------------------------------------------------------------------------
00120 // CLandmarksCategoriesEngine::StartSearchingCategoriesL
00121 // 
00122 // (other items were commented in a header).
00123 // -----------------------------------------------------------------------------
00124 //
00125 void CLandmarksCategoriesEngine::StartSearchingCategoriesL(
00126     MLandmarksOperationObserver* aObserver)
00127     {
00128     // We need to cancel if we are searching/reading categories.
00129     Cancel();
00130 
00131     iObserver = aObserver;
00132     iSearchResultExists = EFalse;
00133     iFilterSearch = EFalse;
00134 
00135     // Complete ourselves and start search for all categories in db in RunL().
00136     iStatus = KRequestPending;
00137     SetPriorityAndSetActive();
00138     iActiveOperation = ECategorySearch;
00139     TRequestStatus* status = &iStatus;
00140     User::RequestComplete(status, KErrNone);
00141     }
00142 
00143 // -----------------------------------------------------------------------------
00144 // CLandmarksCategoriesEngine::StartSearchingCategoriesL
00145 // 
00146 // (other items were commented in a header).
00147 // -----------------------------------------------------------------------------
00148 //
00149 void CLandmarksCategoriesEngine::StartSearchingCategoriesL(
00150     const TDesC& aSearchPattern, 
00151     TBool aSearchOnlyInPreviousMatches,
00152     MLandmarksOperationObserver* aObserver)
00153     {
00154     // We need to cancel if we are searching/reading categories.
00155     Cancel();
00156 
00157     if (!iSearchResultExists)
00158         {
00159         // There are no previous results or previous search did not complete.
00160         // We cannot use the result from it.
00161         aSearchOnlyInPreviousMatches = EFalse;
00162         }
00163     iSearchResultExists = EFalse;
00164     iFilterSearch = ETrue;
00165 
00166     // Set search criteria
00167     CPosLmCatNameCriteria* criteria = CPosLmCatNameCriteria::NewLC();
00168     criteria->SetSearchPatternL(aSearchPattern);
00169 
00170     // Create search operation
00171     CPosLmOperation* operation = iSearcher->StartCategorySearchL(
00172         *criteria, 
00173         CPosLmCategoryManager::ECategorySortOrderNameAscending, 
00174         aSearchOnlyInPreviousMatches);
00175     CleanupStack::PopAndDestroy(criteria);
00176 
00177     // Start search operation
00178     iObserver = aObserver;
00179     iStatus = KRequestPending;
00180     iLmOpWrapper->StartOperation(operation, iStatus);
00181     SetPriorityAndSetActive();
00182     iActiveOperation = ECategorySearch;
00183     }
00184 
00185 // -----------------------------------------------------------------------------
00186 // CLandmarksCategoriesEngine::FetchSearchResult
00187 // 
00188 // (other items were commented in a header).
00189 // -----------------------------------------------------------------------------
00190 //
00191 RArray<TPosLmItemId>* CLandmarksCategoriesEngine::FetchSearchResult()
00192     {
00193     return &iItemIds;
00194     }
00195 
00196 // -----------------------------------------------------------------------------
00197 // CLandmarksCategoriesEngine::StartReadingCategoriesL
00198 // 
00199 // (other items were commented in a header).
00200 // -----------------------------------------------------------------------------
00201 //
00202 void CLandmarksCategoriesEngine::StartReadingCategoriesL(
00203     TInt aNrOfItemsToReadPerBundle,
00204     MLandmarksOperationObserver* aObserver)
00205     {
00206     // We need to cancel if we are searching/reading landmarks.
00207     Cancel();
00208 
00209     iNrOfItemsToRead = aNrOfItemsToReadPerBundle;
00210     iObserver = aObserver;
00211     iCurrentItemId = 0;
00212 
00213     // Start reading a bundle of categories
00214     ReadSomeCategoriesL();
00215     iActiveOperation = ECategoryRead;
00216     }
00217 
00218 // -----------------------------------------------------------------------------
00219 // CLandmarksCategoriesEngine::FetchCategoriesLC
00220 // 
00221 // (other items were commented in a header).
00222 // -----------------------------------------------------------------------------
00223 //
00224 CArrayPtr<CPosLandmarkCategory>* CLandmarksCategoriesEngine::FetchCategoriesLC()
00225     {
00226     TInt nrOfItems = iSubSetOfIds.Count();
00227     CArrayPtr<CPosLandmarkCategory>* categories = 
00228         new (ELeave) CArrayPtrFlat<CPosLandmarkCategory>(nrOfItems);
00229     CleanupStack::PushL(categories);
00230 
00231     // Start the actual read of landmarks. This is done synchronously since 
00232     // there is no asynchronous read method as there is for landmarks:
00233     // (Compare TakePreparedPartialLandmarksL()) 
00234     for (TInt i = 0; i < nrOfItems; i++)
00235         {
00236         CPosLandmarkCategory* category = 
00237             iCatManager->ReadCategoryLC(iSubSetOfIds[i]);
00238         categories->AppendL(category);
00239         CleanupStack::Pop(category);
00240         }
00241 
00242     return categories;
00243     }
00244 
00245 // -----------------------------------------------------------------------------
00246 // CLandmarksCategoriesEngine::CategoriesL
00247 // 
00248 // (other items were commented in a header).
00249 // -----------------------------------------------------------------------------
00250 //
00251 CArrayPtr<CPosLandmarkCategory>* CLandmarksCategoriesEngine::CategoriesL()
00252     {
00253     const TInt KGranularity = 8;
00254     CArrayPtrFlat<CPosLandmarkCategory>* categories = 
00255         new (ELeave) CArrayPtrFlat<CPosLandmarkCategory>(KGranularity);
00256     CleanupStack::PushL(categories);
00257     
00258     // Get an iterator for all existing categories sorted ascending by name
00259     CPosLmItemIterator* itemIterator = iCatManager->CategoryIteratorL(
00260         CPosLmCategoryManager::ECategorySortOrderNameAscending);
00261     CleanupStack::PushL(itemIterator);
00262 
00263     // Read all categories and add them to an array
00264     TPosLmItemId catId = itemIterator->NextL();
00265     while (catId != KPosLmNullItemId)
00266         {
00267         CPosLandmarkCategory* category = iCatManager->ReadCategoryLC(catId);
00268         categories->AppendL(category);
00269         CleanupStack::Pop(category);
00270 
00271         catId = itemIterator->NextL();
00272         }
00273     CleanupStack::PopAndDestroy(itemIterator);
00274     CleanupStack::Pop(categories);
00275 
00276     return categories;
00277     }
00278 
00279 // -----------------------------------------------------------------------------
00280 // CLandmarksCategoriesEngine::RunL
00281 // 
00282 // (other items were commented in a header).
00283 // -----------------------------------------------------------------------------
00284 //
00285 void CLandmarksCategoriesEngine::RunL()
00286     {
00287     if (iStatus == KErrNone)
00288         {
00289         if (iActiveOperation == ECategorySearch)
00290             {
00291             // Fetch search result
00292             PopulateItemIdArrayL();
00293             }
00294         else if (iActiveOperation == ECategoryRead)
00295             {
00296             if (iCurrentItemId < iItemIds.Count())
00297                 {
00298                 // There are still found landmarks to read
00299                 iObserver->NotifyOperationProgressL(
00300                     iActiveOperation, KOperationNotReady, KErrNone);
00301                 ReadSomeCategoriesL();
00302                 }
00303             else
00304                 {
00305                 // No more matches to read
00306                 NotifyOperationReadyL(iActiveOperation, KErrNone);
00307                 }
00308             }
00309         }
00310     else
00311         {
00312         // Operation failed. Forward error code
00313         NotifyOperationReadyL(iActiveOperation, iStatus.Int());
00314         }
00315     }
00316 
00317 // -----------------------------------------------------------------------------
00318 // CLandmarksCategoriesEngine::DoCancel
00319 // 
00320 // (other items were commented in a header).
00321 // -----------------------------------------------------------------------------
00322 //
00323 void CLandmarksCategoriesEngine::DoCancel()
00324     {
00325     iLmOpWrapper->Cancel();
00326     if (iActiveOperation == ECategorySearch)
00327         {
00328         iSearchResultExists = EFalse;
00329         }
00330     iActiveOperation = ENoOperation;
00331     }
00332 
00333 // -----------------------------------------------------------------------------
00334 // CLandmarksCategoriesEngine::RunError
00335 // 
00336 // (other items were commented in a header).
00337 // -----------------------------------------------------------------------------
00338 //
00339 TInt CLandmarksCategoriesEngine::RunError(TInt aError)
00340     {
00341     // Notify user and ignore error code
00342     TInt err;
00343     TRAP(err, NotifyOperationReadyL(iActiveOperation, aError));
00344 
00345     return KErrNone;
00346     }
00347 
00348 // -----------------------------------------------------------------------------
00349 // CLandmarksCategoriesEngine::ReadSomeCategoriesL
00350 // 
00351 // (other items were commented in a header).
00352 // -----------------------------------------------------------------------------
00353 //
00354 void CLandmarksCategoriesEngine::ReadSomeCategoriesL()
00355     {
00356     // Create a subset of the found matches to read
00357     iSubSetOfIds.Reset();
00358     TInt nrOfMatches = iItemIds.Count();
00359     TInt i = 0;
00360     while (i < iNrOfItemsToRead && iCurrentItemId < nrOfMatches)
00361         {
00362         User::LeaveIfError(
00363             iSubSetOfIds.Append(iItemIds[iCurrentItemId]));
00364         iCurrentItemId++;
00365         i++;
00366         }
00367 
00368     // Simulate start reading found matches. There is no asynchronous search 
00369     // functionality for categories as there is for landmarks, 
00370     // (Compare PreparePartialLandmarksL())
00371     iStatus = KRequestPending;
00372     SetPriorityAndSetActive();
00373     TRequestStatus* status = &iStatus;
00374     User::RequestComplete(status, KErrNone);
00375     }
00376 
00377 // -----------------------------------------------------------------------------
00378 // CLandmarksCategoriesEngine::NotifyOperationReadyL
00379 // 
00380 // (other items were commented in a header).
00381 // -----------------------------------------------------------------------------
00382 //
00383 void CLandmarksCategoriesEngine::NotifyOperationReadyL(
00384     TOperation aOperation, 
00385     TInt aErrorCode)
00386     {
00387     iActiveOperation = ENoOperation;
00388     iObserver->NotifyOperationProgressL(
00389         aOperation, 
00390         KOperationReady, 
00391         aErrorCode);
00392     }
00393 
00394 // -----------------------------------------------------------------------------
00395 // CLandmarksCategoriesEngine::PopulateItemIdArrayL
00396 // 
00397 // (other items were commented in a header).
00398 // -----------------------------------------------------------------------------
00399 //
00400 void CLandmarksCategoriesEngine::PopulateItemIdArrayL()
00401     {
00402     // Fetch itemIterator
00403     CPosLmItemIterator* itemIterator = NULL;
00404     if (iFilterSearch)
00405         {
00406         itemIterator = iSearcher->MatchIteratorL();
00407         iSearchResultExists = ETrue;
00408         }
00409     else
00410         {
00411         itemIterator = iCatManager->CategoryIteratorL(
00412             CPosLmCategoryManager::ECategorySortOrderNameAscending);
00413         }
00414     CleanupStack::PushL(itemIterator);
00415     itemIterator->Reset();
00416 
00417     // Fetch iItemIds
00418     TUint nrOfItems = itemIterator->NumOfItemsL();
00419     if (nrOfItems > 0)
00420         {
00421         TInt startIndex = 0;
00422         iItemIds.Reset();
00423         itemIterator->GetItemIdsL(iItemIds, startIndex, nrOfItems);
00424         NotifyOperationReadyL(iActiveOperation, KErrNone);
00425         }
00426     else
00427         {
00428         // No matches found
00429         NotifyOperationReadyL(iActiveOperation, KErrNotFound);
00430         }
00431     CleanupStack::PopAndDestroy(itemIterator);
00432     }
00433 
00434 //  End of File  
00435 

© Nokia 2006

Back to top