landmarks/locationlandmarks/localaccess/src/EPos_CPosLmLocalRemoveLandmarksOp.cpp
changeset 0 667063e416a2
child 20 2b4ea9893b66
equal deleted inserted replaced
-1:000000000000 0:667063e416a2
       
     1 /*
       
     2 * Copyright (c) 2002-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: Operation for removing landmarks from a database.
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include    <epos_cposlmresourcereader.h>
       
    21 #include    <eposlmasyncops.rsg>
       
    22 #include    <EPos_LandmarksErrors.h>
       
    23 #include    <epos_cposlmdiskutilities.h>
       
    24 #include    <epos_cposlmlocaldbaccess.h>
       
    25 #include    <epos_poslmlandmarkhandler.h>
       
    26 
       
    27 #include    "epos_cposlmlocaldatabase.h"
       
    28 #include    "EPos_LocalLandmarks.h"
       
    29 #include    "EPos_CPosLmLocalLandmarkIterator.h"
       
    30 #include    "epos_rposlmlocalnameindex.h"
       
    31 #include    "EPos_RPosLmLocalAccessSubsession.h"
       
    32 #include    "EPos_CPosLmLocalRemoveLandmarksOp.h"
       
    33 
       
    34 // ============================ MEMBER FUNCTIONS ===============================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 CPosLmLocalRemoveLandmarksOp::CPosLmLocalRemoveLandmarksOp( CPosLmLocalDatabase& aDb ) :
       
    40     CPosLmLocalModifyOp(aDb)
       
    41     {
       
    42     iUsesServerData = ETrue;
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 void CPosLmLocalRemoveLandmarksOp::ConstructL(
       
    49     const RArray<TPosLmItemId>& aLandmarkIdArray,
       
    50     TInt aResourceId )
       
    51     {
       
    52     if ( aLandmarkIdArray.Count() == 0 )
       
    53         {
       
    54         User::Leave( KErrArgument );
       
    55         }
       
    56 
       
    57     BaseConstructL();
       
    58 
       
    59     ReadInfoFromResourceFileL( aResourceId );
       
    60 
       
    61     iLandmarkIdArray.ReserveL( aLandmarkIdArray.Count() ); // to avoid heap fragmentation
       
    62     for ( TInt i = 0; i < aLandmarkIdArray.Count(); i++ )
       
    63         {
       
    64         iLandmarkIdArray.Append( aLandmarkIdArray[i] );
       
    65         }
       
    66     }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 void CPosLmLocalRemoveLandmarksOp::ConstructL()
       
    72     {
       
    73     BaseConstructL();
       
    74 
       
    75     CPosLmItemIterator* iterator =
       
    76         CPosLmLocalLandmarkIterator::NewL( *iDb, ETrue );
       
    77     CleanupStack::PushL( iterator );
       
    78 
       
    79     if ( iterator->NumOfItemsL() > 0 )
       
    80         {
       
    81         iterator->GetItemIdsL( iLandmarkIdArray, 0, iterator->NumOfItemsL() );
       
    82         }
       
    83 
       
    84     CleanupStack::PopAndDestroy( iterator );
       
    85 
       
    86     if ( iLandmarkIdArray.Count() > 0 )
       
    87         {
       
    88         // Check disk space
       
    89         CPosLmDiskUtilities* diskutil = CPosLmDiskUtilities::NewL();
       
    90         CleanupStack::PushL(diskutil);
       
    91         diskutil->DiskSpaceBelowCriticalLevelL(
       
    92             diskutil->EstimatedDiskSizeOfLmOperation(
       
    93                 CPosLmDiskUtilities::ERemoveLandmarksOp,
       
    94                 iLandmarkIdArray), iDb->DatabaseDrive());
       
    95         CleanupStack::PopAndDestroy(diskutil);
       
    96 
       
    97         ReadInfoFromResourceFileL( R_POS_LM_LOCAL_REMOVE_ALL_LMS_OP );
       
    98         }
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 CPosLmLocalRemoveLandmarksOp* CPosLmLocalRemoveLandmarksOp::NewL(
       
   105     CPosLmLocalDatabase& aDb,
       
   106     const RArray<TPosLmItemId>& aLandmarkIdArray)
       
   107     {
       
   108     CPosLmLocalRemoveLandmarksOp* self = new (ELeave)
       
   109         CPosLmLocalRemoveLandmarksOp(aDb);
       
   110     CleanupStack::PushL(self);
       
   111     self->ConstructL(aLandmarkIdArray, R_POS_LM_LOCAL_REMOVE_LMS_OP);
       
   112     CleanupStack::Pop(self);
       
   113     return self;
       
   114     }
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 CPosLmLocalRemoveLandmarksOp* CPosLmLocalRemoveLandmarksOp::NewL(
       
   120     CPosLmLocalDatabase& aDb)
       
   121     {
       
   122     CPosLmLocalRemoveLandmarksOp* self = new (ELeave)
       
   123         CPosLmLocalRemoveLandmarksOp(aDb);
       
   124     CleanupStack::PushL(self);
       
   125     self->ConstructL();
       
   126     CleanupStack::Pop(self);
       
   127     return self;
       
   128     }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 CPosLmLocalRemoveLandmarksOp::~CPosLmLocalRemoveLandmarksOp()
       
   134     {
       
   135     RollbackAndGenerateEventIfNeeded(Progress());
       
   136 
       
   137     iLandmarkIdArray.Close();
       
   138     }
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 void CPosLmLocalRemoveLandmarksOp::DoNextStepPreL(
       
   144     TReal32& aProgress)
       
   145     {
       
   146     if ( iLandmarkIdArray.Count() == 0 )
       
   147         {
       
   148         aProgress = 1;
       
   149         }
       
   150     else
       
   151         {
       
   152         RArray<TPosLmItemId> removed;
       
   153         CleanupClosePushL( removed );
       
   154         for (TInt i = 0; i < iNoOfSubOperations &&
       
   155             iCurrent < iLandmarkIdArray.Count(); i++)
       
   156             {
       
   157             PosLmLandmarkHandler::RemoveLandmarkL(
       
   158                 *iDb->DatabaseAccess(), iLandmarkIdArray[iCurrent] );
       
   159             removed.AppendL( iLandmarkIdArray[iCurrent] );
       
   160 
       
   161             iCurrent++;
       
   162             }
       
   163         iDb->NameIndex().RemoveL( removed );
       
   164         CleanupStack::PopAndDestroy( &removed );
       
   165 
       
   166         // Set progress
       
   167         aProgress = TReal32(iCurrent)/iLandmarkIdArray.Count();
       
   168         }
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 TBool CPosLmLocalRemoveLandmarksOp::DoNextStepPostL(
       
   175     TReal32& aProgress)
       
   176     {
       
   177     if (iCurrent < iLandmarkIdArray.Count())
       
   178         {
       
   179         return ETrue;
       
   180         }
       
   181 
       
   182     User::LeaveIfError(GenerateEventIfNeeded(aProgress));
       
   183     iStatusFlag = KErrNone;
       
   184     return EFalse;
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // -----------------------------------------------------------------------------
       
   189 //
       
   190 TReal32 CPosLmLocalRemoveLandmarksOp::Step()
       
   191     {
       
   192     if (iLandmarkIdArray.Count() == 0)
       
   193         {
       
   194         return 1.0f;
       
   195         }
       
   196     return 1.0f/iLandmarkIdArray.Count();
       
   197     }
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 void CPosLmLocalRemoveLandmarksOp::CheckResourceValue(
       
   203     TInt /*aResourceId*/,
       
   204     TInt aNoOfSubOperations)
       
   205     {
       
   206     __ASSERT_ALWAYS(aNoOfSubOperations > 0, Panic(KPosLandmarksClientPanic,
       
   207         EPosInvalidValueSpecifiedInResourceFile));
       
   208     }
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 void CPosLmLocalRemoveLandmarksOp::HandleError(
       
   214     TInt& aError)
       
   215     {
       
   216     iStatusFlag = aError;
       
   217     RollbackAndGenerateEventIfNeeded(Progress());
       
   218     }
       
   219 
       
   220 // -----------------------------------------------------------------------------
       
   221 // -----------------------------------------------------------------------------
       
   222 //
       
   223 void CPosLmLocalRemoveLandmarksOp::RollbackAndGenerateEventIfNeeded(
       
   224     TReal32 aProgress)
       
   225     {
       
   226     RollbackIfNeeded();
       
   227 
       
   228     GenerateEventIfNeeded(aProgress);
       
   229 
       
   230     // Reset flag: rollback and event generation should not be done twice.
       
   231     iStatusFlag = KErrNone;
       
   232     }
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 TInt CPosLmLocalRemoveLandmarksOp::GenerateEventIfNeeded(
       
   238     TReal32 aProgress)
       
   239     {
       
   240     if (iStatusFlag != KErrNone && aProgress > 0)
       
   241         {
       
   242         // If only one landmark has been removed, a more specific event can be
       
   243         // reported.
       
   244         if (iCurrent == 1)
       
   245             {
       
   246             return iDb->RegisterEvent(EPosLmEventLandmarkDeleted,
       
   247                 iLandmarkIdArray[0]);
       
   248             }
       
   249 
       
   250         // If more than one landmark have been removed, a general event is
       
   251         // reported.
       
   252         if (iCurrent > 0)
       
   253             {
       
   254             return iDb->RegisterEvent(EPosLmEventLandmarkUnknownChanges);
       
   255             }
       
   256 
       
   257         }
       
   258     // Nothing has happend, don't report event.
       
   259     return KErrNone;
       
   260     }
       
   261