locationlandmarksrefappfors60/Src/LandmarksEngine.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:  Implements the CLandmarksEngine class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <aknview.h>
       
    21 #include <EPos_CPosLandmarkDatabase.h>
       
    22 #include <EPos_CPosLmPartialReadParameters.h>
       
    23 #include <EPos_CPosLandmarkSearch.h>
       
    24 #include <EPos_CPosLmTextCriteria.h>
       
    25 
       
    26 #include "LandmarksEngine.h"
       
    27 #include "LandmarksLmOpWrapper.h"
       
    28 
       
    29 // ============================ MEMBER FUNCTIONS ===============================
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 CLandmarksEngine::CLandmarksEngine(
       
    35     CPosLandmarkDatabase& aDb) 
       
    36 :   CLandmarksEngineBase(aDb),
       
    37     iSortPref(CPosLandmark::ELandmarkName, TPosLmSortPref::EAscending)
       
    38     {
       
    39     }
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 void CLandmarksEngine::ConstructL()
       
    45     {
       
    46     CLandmarksEngineBase::ConstructL();
       
    47 
       
    48     // Set partial read parameters
       
    49     CPosLmPartialReadParameters* partialReadParams = 
       
    50         CPosLmPartialReadParameters::NewLC();
       
    51     partialReadParams->SetRequestedAttributes(
       
    52         CPosLandmark::ELandmarkName | CPosLandmark::EIcon);
       
    53     iDb.SetPartialReadParametersL(*partialReadParams);
       
    54     CleanupStack::PopAndDestroy(partialReadParams);
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 CLandmarksEngine* CLandmarksEngine::NewL(
       
    61     CPosLandmarkDatabase& aDb)
       
    62     {
       
    63     CLandmarksEngine* self = new (ELeave) CLandmarksEngine(aDb);
       
    64     CleanupStack::PushL(self);
       
    65     self->ConstructL();
       
    66     CleanupStack::Pop(self);
       
    67     return self;
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 CLandmarksEngine::~CLandmarksEngine()
       
    74     {
       
    75     Cancel();
       
    76     iItemIds.Close();
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 TBool CLandmarksEngine::StartInitializeDbIfNecessaryL(
       
    83     MLandmarksOperationObserver* aObserver)
       
    84     {
       
    85     if (iDb.IsInitializingNeeded())
       
    86         {
       
    87         // Create initialize operation
       
    88         CPosLmOperation* operation = iDb.InitializeL();
       
    89 
       
    90         iObserver = aObserver;
       
    91         iActiveOperation = EInitializeDb;
       
    92         iStatus = KRequestPending;
       
    93         SetPriorityAndSetActive();
       
    94 
       
    95         // Start asynchronous initialization
       
    96         TBool reportProgress = ETrue;
       
    97         iLmOpWrapper->StartOperation(operation, iStatus, reportProgress);
       
    98 
       
    99         return ETrue;
       
   100         }
       
   101     else
       
   102         {
       
   103         return EFalse;
       
   104         }
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 void CLandmarksEngine::AddLandmarkL(CPosLandmark& aLandmark)
       
   111     {
       
   112     iDb.AddLandmarkL(aLandmark);
       
   113     }
       
   114 
       
   115 // -----------------------------------------------------------------------------
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 void CLandmarksEngine::CommitLandmarkL(const CPosLandmark& aLandmark)
       
   119     {
       
   120     iDb.UpdateLandmarkL(aLandmark);
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 void CLandmarksEngine::DeleteLandmarkL(TPosLmItemId aItemId)
       
   127     {
       
   128     iDb.RemoveLandmarkL(aItemId);
       
   129     }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 CPosLandmark* CLandmarksEngine::LandmarkLC(TPosLmItemId aItemId)
       
   135     {
       
   136     return iDb.ReadLandmarkLC(aItemId);
       
   137     }
       
   138 
       
   139 // -----------------------------------------------------------------------------
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 void CLandmarksEngine::StartSearchingLandmarksL(
       
   143     MLandmarksOperationObserver* aObserver)
       
   144     {
       
   145     // We need to cancel if we are searching/reading landmarks.
       
   146     Cancel(); 
       
   147 
       
   148     iObserver = aObserver;
       
   149     iSearchResultExists = EFalse;
       
   150     iFilterSearch = EFalse;
       
   151     
       
   152     // Complete ourselves and start search for all landmarks in db in RunL().
       
   153     iStatus = KRequestPending;
       
   154     SetPriorityAndSetActive();
       
   155     iActiveOperation = ELandmarkSearch;
       
   156     TRequestStatus* status = &iStatus;
       
   157     User::RequestComplete(status, KErrNone);
       
   158     }
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 void CLandmarksEngine::StartSearchingLandmarksL(
       
   164     const TDesC& aSearchPattern, 
       
   165     TBool aSearchOnlyInPreviousMatches,
       
   166     MLandmarksOperationObserver* aObserver)
       
   167     {
       
   168     // We need to cancel if we are searching/reading landmarks.
       
   169     Cancel();
       
   170 
       
   171     if (!iSearchResultExists)
       
   172         {
       
   173         // There are no previous results or previous search did not complete.
       
   174         // We cannot use the result from it.
       
   175         aSearchOnlyInPreviousMatches = EFalse;
       
   176         }
       
   177     iSearchResultExists = EFalse;
       
   178     iFilterSearch = ETrue;
       
   179 
       
   180     // Set search criteria
       
   181     CPosLmTextCriteria* criteria = CPosLmTextCriteria::NewLC();
       
   182     criteria->SetTextL(aSearchPattern);
       
   183     criteria->SetAttributesToSearch(CPosLandmark::ELandmarkName);
       
   184 
       
   185     // Create search operation
       
   186     CPosLmOperation* operation = iSearcher->StartLandmarkSearchL(
       
   187         *criteria, iSortPref, aSearchOnlyInPreviousMatches);
       
   188     CleanupStack::PopAndDestroy(criteria);
       
   189 
       
   190     // Start search operation
       
   191     iObserver = aObserver;
       
   192     iStatus = KRequestPending;
       
   193     iLmOpWrapper->StartOperation(operation, iStatus);
       
   194     SetPriorityAndSetActive();
       
   195     iActiveOperation = ELandmarkSearch;
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 RArray<TPosLmItemId>* CLandmarksEngine::FetchSearchResult()
       
   202     {
       
   203     return &iItemIds;
       
   204     }
       
   205 
       
   206 // -----------------------------------------------------------------------------
       
   207 // -----------------------------------------------------------------------------
       
   208 //
       
   209 void CLandmarksEngine::StartReadingLandmarksL(
       
   210     TInt aNrOfItemsToReadPerBundle,
       
   211     MLandmarksOperationObserver* aObserver)
       
   212     {
       
   213     // We need to cancel if we are searching/reading landmarks.
       
   214     Cancel();
       
   215 
       
   216     iNrOfItemsToRead = aNrOfItemsToReadPerBundle;
       
   217     iObserver = aObserver;
       
   218     iCurrentItemId = 0;
       
   219 
       
   220     // Start reading a bundle of landmarks
       
   221     ReadSomeLandmarksL();
       
   222     iActiveOperation = ELandmarkRead;
       
   223     }
       
   224 
       
   225 // -----------------------------------------------------------------------------
       
   226 // -----------------------------------------------------------------------------
       
   227 //
       
   228 CArrayPtr<CPosLandmark>* CLandmarksEngine::FetchLandmarksLC()
       
   229     {
       
   230     // Fetch the partially read landmarks.
       
   231     CArrayPtr<CPosLandmark>* result = 
       
   232         iDb.TakePreparedPartialLandmarksL(iLmOpWrapper->LmOperationPtr());
       
   233     CleanupStack::PushL(result);
       
   234 
       
   235     return result;
       
   236     }
       
   237 
       
   238 // -----------------------------------------------------------------------------
       
   239 // -----------------------------------------------------------------------------
       
   240 //
       
   241 void CLandmarksEngine::RunL()
       
   242     {
       
   243     if (iActiveOperation == EInitializeDb)
       
   244         {
       
   245         // Report progress
       
   246         iObserver->NotifyOperationProgressL(
       
   247             iActiveOperation, iLmOpWrapper->Progress(), iStatus.Int());
       
   248 
       
   249         if (iStatus == KPosLmOperationNotComplete)
       
   250             {
       
   251             // Continue initializing database until finished
       
   252             iStatus = KRequestPending;
       
   253             SetPriorityAndSetActive();
       
   254             iLmOpWrapper->ExecuteNextStep(iStatus);
       
   255             }
       
   256         }
       
   257     else if (iStatus != KErrNone)
       
   258         {
       
   259         // Operation failed. Report error code
       
   260         NotifyOperationReadyL(iActiveOperation, iStatus.Int());
       
   261         }
       
   262     else if (iActiveOperation == ELandmarkSearch)
       
   263         {
       
   264         // Fetch search result
       
   265         PopulateItemIdArrayL();
       
   266         }
       
   267     else if (iActiveOperation == ELandmarkRead)
       
   268         {
       
   269         if (iCurrentItemId < iItemIds.Count())
       
   270             {
       
   271             // There are still found landmarks to read
       
   272             iObserver->NotifyOperationProgressL(iActiveOperation, KOperationNotReady, KErrNone);
       
   273             ReadSomeLandmarksL();
       
   274             }
       
   275         else
       
   276             {
       
   277             // No more matches to read
       
   278             NotifyOperationReadyL(iActiveOperation, KErrNone);
       
   279             }
       
   280         }
       
   281     }
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // -----------------------------------------------------------------------------
       
   285 //
       
   286 void CLandmarksEngine::DoCancel()
       
   287     {
       
   288     iLmOpWrapper->Cancel();
       
   289 
       
   290     if (iActiveOperation == ELandmarkSearch)
       
   291         {
       
   292         iSearchResultExists = EFalse;
       
   293         }
       
   294     iActiveOperation = ENoOperation;
       
   295     }
       
   296 
       
   297 // -----------------------------------------------------------------------------
       
   298 // -----------------------------------------------------------------------------
       
   299 //
       
   300 TInt CLandmarksEngine::RunError(TInt aError)
       
   301     {
       
   302     // Notify user and ignore error code
       
   303     TInt err;
       
   304     TRAP(err, NotifyOperationReadyL(iActiveOperation, aError));
       
   305 
       
   306     return KErrNone;
       
   307     }
       
   308 
       
   309 // -----------------------------------------------------------------------------
       
   310 // -----------------------------------------------------------------------------
       
   311 //
       
   312 void CLandmarksEngine::ReadSomeLandmarksL()
       
   313     {
       
   314     RArray<TPosLmItemId> subSetOfIds;
       
   315     CleanupClosePushL(subSetOfIds);
       
   316 
       
   317     // Create a subset of the found matches to read
       
   318     TInt nrOfMatches = iItemIds.Count();
       
   319     TInt i = 0;
       
   320     while (i < iNrOfItemsToRead && iCurrentItemId < nrOfMatches)
       
   321         {
       
   322         User::LeaveIfError(subSetOfIds.Append(iItemIds[iCurrentItemId]));
       
   323         iCurrentItemId++;
       
   324         i++;
       
   325         }
       
   326 
       
   327     // Start reading subset of found matches
       
   328     iStatus = KRequestPending;
       
   329     CPosLmOperation* operation = iDb.PreparePartialLandmarksL(subSetOfIds);
       
   330     iLmOpWrapper->StartOperation(operation, iStatus);
       
   331     SetPriorityAndSetActive();
       
   332 
       
   333     CleanupStack::PopAndDestroy(&subSetOfIds);
       
   334     }
       
   335 
       
   336 // -----------------------------------------------------------------------------
       
   337 // -----------------------------------------------------------------------------
       
   338 //
       
   339 void CLandmarksEngine::NotifyOperationReadyL(
       
   340     TOperation aOperation, 
       
   341     TInt aErrorCode)
       
   342     {
       
   343     iActiveOperation = ENoOperation;
       
   344     iObserver->NotifyOperationProgressL(
       
   345         aOperation, 
       
   346         KOperationReady, 
       
   347         aErrorCode);
       
   348     }
       
   349 
       
   350 // -----------------------------------------------------------------------------
       
   351 // -----------------------------------------------------------------------------
       
   352 //
       
   353 void CLandmarksEngine::PopulateItemIdArrayL()
       
   354     {
       
   355     // Fetch itemIterator
       
   356     CPosLmItemIterator* itemIterator = NULL;
       
   357     if (iFilterSearch)
       
   358         {
       
   359         itemIterator = iSearcher->MatchIteratorL();
       
   360         iSearchResultExists = ETrue;
       
   361         }
       
   362     else
       
   363         {
       
   364         itemIterator = iDb.LandmarkIteratorL(iSortPref);
       
   365         }
       
   366     CleanupStack::PushL(itemIterator);
       
   367     itemIterator->Reset();
       
   368 
       
   369     // Fetch iItemIds
       
   370     TUint nrOfItems = itemIterator->NumOfItemsL();
       
   371     if (nrOfItems > 0)
       
   372         {
       
   373         TInt startIndex = 0;
       
   374         iItemIds.Reset();
       
   375         itemIterator->GetItemIdsL(iItemIds, startIndex, nrOfItems);
       
   376         NotifyOperationReadyL(iActiveOperation, KErrNone);
       
   377         }
       
   378     else
       
   379         {
       
   380         // No matches found
       
   381         NotifyOperationReadyL(iActiveOperation, KErrNotFound);
       
   382         }
       
   383     CleanupStack::PopAndDestroy(itemIterator);
       
   384     }
       
   385 
       
   386