S60 3rd Edition SDK for Symbian OS
Example Applications Guide

LandmarksEngine.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : LandmarksEngine.cpp
00004 *  Part of     : Landmark Reference Application
00005 *  Interface   : -
00006 *  Description : Implements the CLandmarksEngine class
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 <aknview.h>
00018 #include <EPos_CPosLandmarkDatabase.h>
00019 #include <EPos_CPosLmPartialReadParameters.h>
00020 #include <EPos_CPosLandmarkSearch.h>
00021 #include <EPos_CPosLmTextCriteria.h>
00022 
00023 #include "LandmarksEngine.h"
00024 #include "LandmarksLmOpWrapper.h"
00025 
00026 // ============================ MEMBER FUNCTIONS ===============================
00027 
00028 // -----------------------------------------------------------------------------
00029 // CLandmarksEngine::CLandmarksEngine
00030 // C++ default constructor can NOT contain any code, that
00031 // might leave.
00032 // -----------------------------------------------------------------------------
00033 //
00034 CLandmarksEngine::CLandmarksEngine(
00035     CPosLandmarkDatabase& aDb) 
00036 :   CLandmarksEngineBase(aDb),
00037     iSortPref(CPosLandmark::ELandmarkName, TPosLmSortPref::EAscending)
00038     {
00039     }
00040 
00041 // -----------------------------------------------------------------------------
00042 // CLandmarksEngine::ConstructL
00043 // Symbian 2nd phase constructor can leave.
00044 // -----------------------------------------------------------------------------
00045 //
00046 void CLandmarksEngine::ConstructL()
00047     {
00048     CLandmarksEngineBase::ConstructL();
00049 
00050     // Set partial read parameters
00051     CPosLmPartialReadParameters* partialReadParams = 
00052         CPosLmPartialReadParameters::NewLC();
00053     partialReadParams->SetRequestedAttributes(
00054         CPosLandmark::ELandmarkName | CPosLandmark::EIcon);
00055     iDb.SetPartialReadParametersL(*partialReadParams);
00056     CleanupStack::PopAndDestroy(partialReadParams);
00057     }
00058 
00059 // -----------------------------------------------------------------------------
00060 // CLandmarksEngine::NewL
00061 // Two-phased constructor.
00062 // -----------------------------------------------------------------------------
00063 //
00064 CLandmarksEngine* CLandmarksEngine::NewL(
00065     CPosLandmarkDatabase& aDb)
00066     {
00067     CLandmarksEngine* self = new (ELeave) CLandmarksEngine(aDb);
00068     CleanupStack::PushL(self);
00069     self->ConstructL();
00070     CleanupStack::Pop(self);
00071     return self;
00072     }
00073 
00074 // Destructor
00075 CLandmarksEngine::~CLandmarksEngine()
00076     {
00077     Cancel();
00078     iItemIds.Close();
00079     }
00080 
00081 // -----------------------------------------------------------------------------
00082 // CLandmarksEngine::StartInitializeDbIfNecessaryL
00083 // 
00084 // (other items were commented in a header).
00085 // -----------------------------------------------------------------------------
00086 //
00087 TBool CLandmarksEngine::StartInitializeDbIfNecessaryL(
00088     MLandmarksOperationObserver* aObserver)
00089     {
00090     if (iDb.IsInitializingNeeded())
00091         {
00092         // Create initialize operation
00093         CPosLmOperation* operation = iDb.InitializeL();
00094 
00095         iObserver = aObserver;
00096         iActiveOperation = EInitializeDb;
00097         iStatus = KRequestPending;
00098         SetPriorityAndSetActive();
00099 
00100         // Start asynchronous initialization
00101         TBool reportProgress = ETrue;
00102         iLmOpWrapper->StartOperation(operation, iStatus, reportProgress);
00103 
00104         return ETrue;
00105         }
00106     else
00107         {
00108         return EFalse;
00109         }
00110     }
00111 
00112 // -----------------------------------------------------------------------------
00113 // CLandmarksEngine::AddLandmarkL
00114 // 
00115 // (other items were commented in a header).
00116 // -----------------------------------------------------------------------------
00117 //
00118 void CLandmarksEngine::AddLandmarkL(CPosLandmark& aLandmark)
00119     {
00120     iDb.AddLandmarkL(aLandmark);
00121     }
00122 
00123 // -----------------------------------------------------------------------------
00124 // CLandmarksEngine::CommitLandmarkL
00125 // 
00126 // (other items were commented in a header).
00127 // -----------------------------------------------------------------------------
00128 //
00129 void CLandmarksEngine::CommitLandmarkL(const CPosLandmark& aLandmark)
00130     {
00131     iDb.UpdateLandmarkL(aLandmark);
00132     }
00133 
00134 // -----------------------------------------------------------------------------
00135 // CLandmarksEngine::DeleteLandmarkL
00136 // 
00137 // (other items were commented in a header).
00138 // -----------------------------------------------------------------------------
00139 //
00140 void CLandmarksEngine::DeleteLandmarkL(TPosLmItemId aItemId)
00141     {
00142     iDb.RemoveLandmarkL(aItemId);
00143     }
00144 
00145 // -----------------------------------------------------------------------------
00146 // CLandmarksEngine::LandmarkLC
00147 // 
00148 // (other items were commented in a header).
00149 // -----------------------------------------------------------------------------
00150 //
00151 CPosLandmark* CLandmarksEngine::LandmarkLC(TPosLmItemId aItemId)
00152     {
00153     return iDb.ReadLandmarkLC(aItemId);
00154     }
00155 
00156 // -----------------------------------------------------------------------------
00157 // CLandmarksEngine::StartSearchingLandmarksL
00158 // 
00159 // (other items were commented in a header).
00160 // -----------------------------------------------------------------------------
00161 //
00162 void CLandmarksEngine::StartSearchingLandmarksL(
00163     MLandmarksOperationObserver* aObserver)
00164     {
00165     // We need to cancel if we are searching/reading landmarks.
00166     Cancel(); 
00167 
00168     iObserver = aObserver;
00169     iSearchResultExists = EFalse;
00170     iFilterSearch = EFalse;
00171     
00172     // Complete ourselves and start search for all landmarks in db in RunL().
00173     iStatus = KRequestPending;
00174     SetPriorityAndSetActive();
00175     iActiveOperation = ELandmarkSearch;
00176     TRequestStatus* status = &iStatus;
00177     User::RequestComplete(status, KErrNone);
00178     }
00179 
00180 // -----------------------------------------------------------------------------
00181 // CLandmarksEngine::StartSearchingLandmarksL
00182 // 
00183 // (other items were commented in a header).
00184 // -----------------------------------------------------------------------------
00185 //
00186 void CLandmarksEngine::StartSearchingLandmarksL(
00187     const TDesC& aSearchPattern, 
00188     TBool aSearchOnlyInPreviousMatches,
00189     MLandmarksOperationObserver* aObserver)
00190     {
00191     // We need to cancel if we are searching/reading landmarks.
00192     Cancel();
00193 
00194     if (!iSearchResultExists)
00195         {
00196         // There are no previous results or previous search did not complete.
00197         // We cannot use the result from it.
00198         aSearchOnlyInPreviousMatches = EFalse;
00199         }
00200     iSearchResultExists = EFalse;
00201     iFilterSearch = ETrue;
00202 
00203     // Set search criteria
00204     CPosLmTextCriteria* criteria = CPosLmTextCriteria::NewLC();
00205     criteria->SetTextL(aSearchPattern);
00206     criteria->SetAttributesToSearch(CPosLandmark::ELandmarkName);
00207 
00208     // Create search operation
00209     CPosLmOperation* operation = iSearcher->StartLandmarkSearchL(
00210         *criteria, iSortPref, aSearchOnlyInPreviousMatches);
00211     CleanupStack::PopAndDestroy(criteria);
00212 
00213     // Start search operation
00214     iObserver = aObserver;
00215     iStatus = KRequestPending;
00216     iLmOpWrapper->StartOperation(operation, iStatus);
00217     SetPriorityAndSetActive();
00218     iActiveOperation = ELandmarkSearch;
00219     }
00220 
00221 // -----------------------------------------------------------------------------
00222 // CLandmarksEngine::FetchSearchResult
00223 // 
00224 // (other items were commented in a header).
00225 // -----------------------------------------------------------------------------
00226 //
00227 RArray<TPosLmItemId>* CLandmarksEngine::FetchSearchResult()
00228     {
00229     return &iItemIds;
00230     }
00231 
00232 // -----------------------------------------------------------------------------
00233 // CLandmarksEngine::StartReadingLandmarksL
00234 // 
00235 // (other items were commented in a header).
00236 // -----------------------------------------------------------------------------
00237 //
00238 void CLandmarksEngine::StartReadingLandmarksL(
00239     TInt aNrOfItemsToReadPerBundle,
00240     MLandmarksOperationObserver* aObserver)
00241     {
00242     // We need to cancel if we are searching/reading landmarks.
00243     Cancel();
00244 
00245     iNrOfItemsToRead = aNrOfItemsToReadPerBundle;
00246     iObserver = aObserver;
00247     iCurrentItemId = 0;
00248 
00249     // Start reading a bundle of landmarks
00250     ReadSomeLandmarksL();
00251     iActiveOperation = ELandmarkRead;
00252     }
00253 
00254 // -----------------------------------------------------------------------------
00255 // CLandmarksEngine::FetchLandmarksLC
00256 // 
00257 // (other items were commented in a header).
00258 // -----------------------------------------------------------------------------
00259 //
00260 CArrayPtr<CPosLandmark>* CLandmarksEngine::FetchLandmarksLC()
00261     {
00262     // Fetch the partially read landmarks.
00263     CArrayPtr<CPosLandmark>* result = 
00264         iDb.TakePreparedPartialLandmarksL(iLmOpWrapper->LmOperationPtr());
00265     CleanupStack::PushL(result);
00266 
00267     return result;
00268     }
00269 
00270 // -----------------------------------------------------------------------------
00271 // CLandmarksEngine::RunL
00272 // 
00273 // (other items were commented in a header).
00274 // -----------------------------------------------------------------------------
00275 //
00276 void CLandmarksEngine::RunL()
00277     {
00278     if (iActiveOperation == EInitializeDb)
00279         {
00280         // Report progress
00281         iObserver->NotifyOperationProgressL(
00282             iActiveOperation, iLmOpWrapper->Progress(), iStatus.Int());
00283 
00284         if (iStatus == KPosLmOperationNotComplete)
00285             {
00286             // Continue initializing database until finished
00287             iStatus = KRequestPending;
00288             SetPriorityAndSetActive();
00289             iLmOpWrapper->ExecuteNextStep(iStatus);
00290             }
00291         }
00292     else if (iStatus != KErrNone)
00293         {
00294         // Operation failed. Report error code
00295         NotifyOperationReadyL(iActiveOperation, iStatus.Int());
00296         }
00297     else if (iActiveOperation == ELandmarkSearch)
00298         {
00299         // Fetch search result
00300         PopulateItemIdArrayL();
00301         }
00302     else if (iActiveOperation == ELandmarkRead)
00303         {
00304         if (iCurrentItemId < iItemIds.Count())
00305             {
00306             // There are still found landmarks to read
00307             iObserver->NotifyOperationProgressL(iActiveOperation, KOperationNotReady, KErrNone);
00308             ReadSomeLandmarksL();
00309             }
00310         else
00311             {
00312             // No more matches to read
00313             NotifyOperationReadyL(iActiveOperation, KErrNone);
00314             }
00315         }
00316     }
00317 
00318 // -----------------------------------------------------------------------------
00319 // CLandmarksEngine::DoCancel
00320 // 
00321 // (other items were commented in a header).
00322 // -----------------------------------------------------------------------------
00323 //
00324 void CLandmarksEngine::DoCancel()
00325     {
00326     iLmOpWrapper->Cancel();
00327 
00328     if (iActiveOperation == ELandmarkSearch)
00329         {
00330         iSearchResultExists = EFalse;
00331         }
00332     iActiveOperation = ENoOperation;
00333     }
00334 
00335 // -----------------------------------------------------------------------------
00336 // CLandmarksEngine::RunError
00337 // 
00338 // (other items were commented in a header).
00339 // -----------------------------------------------------------------------------
00340 //
00341 TInt CLandmarksEngine::RunError(TInt aError)
00342     {
00343     // Notify user and ignore error code
00344     TInt err;
00345     TRAP(err, NotifyOperationReadyL(iActiveOperation, aError));
00346 
00347     return KErrNone;
00348     }
00349 
00350 // -----------------------------------------------------------------------------
00351 // CLandmarksEngine::ReadSomeLandmarksL
00352 // 
00353 // (other items were commented in a header).
00354 // -----------------------------------------------------------------------------
00355 //
00356 void CLandmarksEngine::ReadSomeLandmarksL()
00357     {
00358     RArray<TPosLmItemId> subSetOfIds;
00359     CleanupClosePushL(subSetOfIds);
00360 
00361     // Create a subset of the found matches to read
00362     TInt nrOfMatches = iItemIds.Count();
00363     TInt i = 0;
00364     while (i < iNrOfItemsToRead && iCurrentItemId < nrOfMatches)
00365         {
00366         User::LeaveIfError(subSetOfIds.Append(iItemIds[iCurrentItemId]));
00367         iCurrentItemId++;
00368         i++;
00369         }
00370 
00371     // Start reading subset of found matches
00372     iStatus = KRequestPending;
00373     CPosLmOperation* operation = iDb.PreparePartialLandmarksL(subSetOfIds);
00374     iLmOpWrapper->StartOperation(operation, iStatus);
00375     SetPriorityAndSetActive();
00376 
00377     CleanupStack::PopAndDestroy(&subSetOfIds);
00378     }
00379 
00380 // -----------------------------------------------------------------------------
00381 // CLandmarksEngine::NotifyOperationReadyL
00382 // 
00383 // (other items were commented in a header).
00384 // -----------------------------------------------------------------------------
00385 //
00386 void CLandmarksEngine::NotifyOperationReadyL(
00387     TOperation aOperation, 
00388     TInt aErrorCode)
00389     {
00390     iActiveOperation = ENoOperation;
00391     iObserver->NotifyOperationProgressL(
00392         aOperation, 
00393         KOperationReady, 
00394         aErrorCode);
00395     }
00396 
00397 // -----------------------------------------------------------------------------
00398 // CLandmarksEngine::PopulateItemIdArrayL
00399 // 
00400 // (other items were commented in a header).
00401 // -----------------------------------------------------------------------------
00402 //
00403 void CLandmarksEngine::PopulateItemIdArrayL()
00404     {
00405     // Fetch itemIterator
00406     CPosLmItemIterator* itemIterator = NULL;
00407     if (iFilterSearch)
00408         {
00409         itemIterator = iSearcher->MatchIteratorL();
00410         iSearchResultExists = ETrue;
00411         }
00412     else
00413         {
00414         itemIterator = iDb.LandmarkIteratorL(iSortPref);
00415         }
00416     CleanupStack::PushL(itemIterator);
00417     itemIterator->Reset();
00418 
00419     // Fetch iItemIds
00420     TUint nrOfItems = itemIterator->NumOfItemsL();
00421     if (nrOfItems > 0)
00422         {
00423         TInt startIndex = 0;
00424         iItemIds.Reset();
00425         itemIterator->GetItemIdsL(iItemIds, startIndex, nrOfItems);
00426         NotifyOperationReadyL(iActiveOperation, KErrNone);
00427         }
00428     else
00429         {
00430         // No matches found
00431         NotifyOperationReadyL(iActiveOperation, KErrNotFound);
00432         }
00433     CleanupStack::PopAndDestroy(itemIterator);
00434     }
00435 
00436 //  End of File
00437 

© Nokia 2006

Back to top