landmarksui/engine/src/CLmkDbUtils.cpp
branchRCL_3
changeset 17 1fc85118c3ae
parent 16 8173571d354e
child 18 870918037e16
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
     1 /*
       
     2 * Copyright (c) 2002-2006 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:   This file contains utility methods for performing
       
    15 *                landmark database related operations like adding, deleting
       
    16 *                or updating a landmark and category etc.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 
       
    24 #include <eikenv.h>
       
    25 #include <eikcapc.h>
       
    26 #include <EPos_CPosLmCategoryManager.h>
       
    27 #include "LmkConsts.h"
       
    28 #include "landmarks.hrh"
       
    29 #include "Debug.h"
       
    30 #include "CLmkAOOperation.h"
       
    31 #include "MLmkAOOperationObserver.h"
       
    32 #include "CLmkIconOperation.h"
       
    33 #include "CLmkAddToCatOperation.h"
       
    34 #include "CLmkDbUtils.h"
       
    35 #include "CLmkFields.h"
       
    36 #include <lmkerrors.h>
       
    37 
       
    38 
       
    39 // CONSTANTS
       
    40 /// Unnamed namespace for local definitions
       
    41 namespace {
       
    42 #if defined(_DEBUG)
       
    43 _LIT( KPanicMsg, "CLmkDbUtils" );
       
    44 
       
    45 void Panic( TPanicCode aReason )
       
    46     {
       
    47     User::Panic( KPanicMsg, aReason );
       
    48     }
       
    49 #endif
       
    50 const TInt KUrlProtocolEnds (3);
       
    51 _LIT( KDoubleSlash, "//"); // Empty Mime Type indicator
       
    52 }  // namespace
       
    53 
       
    54 // ============================ MEMBER FUNCTIONS ===============================
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CLmkDbUtils::CLmkDbUtils
       
    58 // C++ default constructor can NOT contain any code, that
       
    59 // might leave.
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 CLmkDbUtils::CLmkDbUtils(
       
    63 CPosLandmarkDatabase& aDb,
       
    64 MLmkAOOperationObserver& aObserver,
       
    65 TBool aProgressNote )
       
    66 : iDb( aDb ),
       
    67   iObserver( &aObserver ),
       
    68   iProgressNote( aProgressNote )
       
    69 	{
       
    70 	}
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CLmkDbUtils::NewL()
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 EXPORT_C CLmkDbUtils* CLmkDbUtils::NewL(
       
    77 CPosLandmarkDatabase& aDb,
       
    78 MLmkAOOperationObserver& aObserver,
       
    79 TBool aProgressNote )
       
    80 	{
       
    81 	CLmkDbUtils* self =
       
    82 	    new ( ELeave ) CLmkDbUtils( aDb, aObserver, aProgressNote );
       
    83 	CleanupStack::PushL( self );
       
    84 	self->ConstructL();
       
    85 	CleanupStack::Pop(); //self
       
    86 	return self;
       
    87 	}
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CLmkDbUtils::ConstructL()
       
    91 // -----------------------------------------------------------------------------
       
    92 //
       
    93 void CLmkDbUtils::ConstructL()
       
    94 	{
       
    95 	iCategoryMgr = CPosLmCategoryManager::NewL( iDb );
       
    96 	}
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CLmkDbUtils::~CLmkDbUtils()
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 CLmkDbUtils::~CLmkDbUtils()
       
   103 	{
       
   104 	CancelLmOperation();
       
   105 	delete iCategoryMgr;
       
   106 	}
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CLmkDbUtils::HandleOperationL
       
   110 // -----------------------------------------------------------------------------
       
   111 //
       
   112 void CLmkDbUtils::HandleOperationL(
       
   113 TOperationTypes aType,
       
   114 TReal32 aProgress,
       
   115 TInt aStatus )
       
   116 	{
       
   117 	DEBUG2( CLmkDbUtils::HandleOperationL entered: progress=%d status=%d,
       
   118 	    aProgress, aStatus );
       
   119 	if ( aStatus != KPosLmOperationNotComplete )
       
   120 		{
       
   121 		// iAOOperation usually always exists when this method is called.
       
   122 		// Exception may be e.g. that a leave is trapped by CLmkDbUtils user.
       
   123 		delete iAOOperation;
       
   124 		iAOOperation = NULL;
       
   125 		}
       
   126 	//this handle operation performs db compact operation.
       
   127 	TRAPD(err,iObserver->HandleOperationL( aType, aProgress, aStatus ));
       
   128 	if (err == KErrLocked)
       
   129 		{
       
   130 		//since db compact operation failed, db has to be re-initialized.
       
   131 		InitializeDbL();
       
   132 		}
       
   133 	}
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // CLmkDbUtils::InitializeDbL
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 EXPORT_C void CLmkDbUtils::InitializeDbL()
       
   140 	{
       
   141 	DEBUG( CLmkDbUtils::InitializeDbL entered );
       
   142 	LeaveIfInUseL();
       
   143 
       
   144 	// We do not check IsInitializingNeeded() since it is better to do this
       
   145 	// always asynchronously to ensure consistent program flow.
       
   146 
       
   147 	CPosLmOperation* operation = NULL;
       
   148 	operation = iDb.InitializeL();
       
   149 
       
   150 	SetupAndStartOperationL( operation, EInitialize );
       
   151 	}
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // CLmkDbUtils::CompactDbL
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 EXPORT_C void CLmkDbUtils::CompactDbL()
       
   158 	{
       
   159 	DEBUG( CLmkDbUtils::CompactDbL entered );
       
   160 	LeaveIfInUseL();
       
   161 
       
   162 	CPosLmOperation* operation = iDb.CompactL();
       
   163 
       
   164 	SetupAndStartOperationL( operation, ECompact );
       
   165 	}
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // CLmkDbUtils::AddCategoryL
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 EXPORT_C void CLmkDbUtils::AddCategoryL(
       
   172 	CPosLandmarkCategory& aCategory,
       
   173 	CPosLandmarkDatabase& aDb )
       
   174 	{
       
   175 	CPosLmCategoryManager* categoryMgr = CPosLmCategoryManager::NewL( aDb );
       
   176 	CleanupStack::PushL( categoryMgr );
       
   177 
       
   178 	categoryMgr->AddCategoryL( aCategory );
       
   179 
       
   180 	CleanupStack::PopAndDestroy( categoryMgr );
       
   181 	}
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // CLmkDbUtils::RenameCategoryL
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 	EXPORT_C void CLmkDbUtils::RenameCategoryL(
       
   188 	TDesC& aCategoryName,
       
   189 	TPosLmItemId aId,
       
   190 	CPosLandmarkDatabase& aDb )
       
   191 	{
       
   192 	CPosLmCategoryManager* categoryMgr = CPosLmCategoryManager::NewL( aDb );
       
   193 	CleanupStack::PushL( categoryMgr );
       
   194 
       
   195 	CPosLandmarkCategory* category = categoryMgr->ReadCategoryLC( aId );
       
   196 	category->SetCategoryNameL( aCategoryName );
       
   197 	categoryMgr->UpdateCategoryL( *category );
       
   198 
       
   199 	CleanupStack::PopAndDestroy( 2 ); //category, categoryMgr
       
   200 	}
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // CLmkDbUtils::CategoryNameL
       
   204 // -----------------------------------------------------------------------------
       
   205 //
       
   206 EXPORT_C HBufC* CLmkDbUtils::CategoryNameL(
       
   207 	CPosLandmarkDatabase& aDb,
       
   208 	TPosLmItemId  aCategoryId )
       
   209 	{
       
   210 	CPosLmCategoryManager* categoryMgr = CPosLmCategoryManager::NewL( aDb );
       
   211 	CleanupStack::PushL( categoryMgr );
       
   212 
       
   213 	CPosLandmarkCategory* category = categoryMgr->ReadCategoryLC(aCategoryId);
       
   214 	TPtrC catName;
       
   215 	User::LeaveIfError( category->GetCategoryName ( catName ) );
       
   216 	HBufC* result = catName.AllocL();
       
   217 	CleanupStack::PopAndDestroy( 2 ); //category, categoryMgr
       
   218 	return result;
       
   219 	}
       
   220 
       
   221 // -----------------------------------------------------------------------------
       
   222 // CLmkDbUtils::ChangeIconL
       
   223 // -----------------------------------------------------------------------------
       
   224 //
       
   225 EXPORT_C void CLmkDbUtils::ChangeIconL(
       
   226 	CPosLandmarkDatabase& aDb,
       
   227 	const TPosLmItemId aId,
       
   228 	TLmkItemType aItemType,
       
   229 	const TDesC& aMbmFileName,
       
   230 	TInt aIconIndex, TInt aMaskIndex)
       
   231 	{
       
   232 
       
   233 	if ( aItemType == ELmkItemTypeLandmark )
       
   234 		{
       
   235 		CPosLandmark* landmark = aDb.ReadLandmarkLC( aId );
       
   236 
       
   237 		landmark->SetIconL( aMbmFileName, aIconIndex, aMaskIndex); //KLmkDefaultId+1 );
       
   238 		aDb.UpdateLandmarkL( *landmark );
       
   239 		CleanupStack::PopAndDestroy(); // landmark
       
   240 		}
       
   241 	else
       
   242 		{
       
   243 		__ASSERT_DEBUG( aItemType == ELmkItemTypeCategory,
       
   244 		            Panic( KLmkPanicUnknownItemType ) );
       
   245 
       
   246 		CPosLmCategoryManager* categoryMgr = CPosLmCategoryManager::NewL(aDb);
       
   247 		CleanupStack::PushL( categoryMgr );
       
   248 		CPosLandmarkCategory* category = categoryMgr->ReadCategoryLC( aId );
       
   249 		category->SetIconL( aMbmFileName, aIconIndex, aMaskIndex);//KCategoryDefaultId+1);
       
   250 
       
   251 		categoryMgr->UpdateCategoryL( *category );
       
   252 		CleanupStack::PopAndDestroy( 2 ); //category, categoryMgr
       
   253 		}
       
   254 	}
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // CLmkDbUtils::ChangeIconsL
       
   258 // -----------------------------------------------------------------------------
       
   259 //
       
   260 EXPORT_C void CLmkDbUtils::ChangeIconsL(
       
   261 	const RArray<TPosLmItemId>& aIdArray,
       
   262 	TLmkItemType aItemsType,
       
   263 	const TDesC& aMbmFileName,
       
   264 	TInt aIconIndex, TInt /*aMaskIndex*/)
       
   265 	{
       
   266 	LeaveIfInUseL();
       
   267 
       
   268 	CPosLmOperation* operation = CLmkIconOperation::NewL( iDb,
       
   269 	                                                      aIdArray,
       
   270 	                                                      aItemsType,
       
   271 	                                                      aMbmFileName,
       
   272 	                                                      aIconIndex );
       
   273 
       
   274 	SetupAndStartOperationL( operation, EChangeIcons );
       
   275 	}
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // CLmkDbUtils::DeleteCategoriesL
       
   279 // -----------------------------------------------------------------------------
       
   280 //
       
   281 EXPORT_C void CLmkDbUtils::DeleteCategoriesL(
       
   282 	const RArray<TPosLmItemId>&  aCategoryIdArray )
       
   283 	{
       
   284 	LeaveIfInUseL();
       
   285 
       
   286 	CPosLmOperation* operation =
       
   287 	                iCategoryMgr->RemoveCategoriesL( aCategoryIdArray );
       
   288 
       
   289 	SetupAndStartOperationL( operation, EDeleteCategories );
       
   290 	}
       
   291 
       
   292 // -----------------------------------------------------------------------------
       
   293 // CLmkDbUtils::DeleteLandmarkL
       
   294 // -----------------------------------------------------------------------------
       
   295 //
       
   296 EXPORT_C void CLmkDbUtils::DeleteLandmarkL(
       
   297 	TPosLmItemId aId,
       
   298 	CPosLandmarkDatabase& aDb )
       
   299 	{
       
   300 	aDb.RemoveLandmarkL( aId );
       
   301 	}
       
   302 
       
   303 // -----------------------------------------------------------------------------
       
   304 // CLmkDbUtils::DeleteLandmarksL
       
   305 // -----------------------------------------------------------------------------
       
   306 //
       
   307 EXPORT_C void CLmkDbUtils::DeleteLandmarksL(
       
   308 	const RArray<TPosLmItemId>& aLandmarkIdArray )
       
   309 	{
       
   310 	LeaveIfInUseL();
       
   311 
       
   312 	CPosLmOperation* operation =
       
   313 	                    iDb.RemoveLandmarksL( aLandmarkIdArray );
       
   314 
       
   315 	SetupAndStartOperationL( operation, EDeleteLandmarks );
       
   316 	}
       
   317 
       
   318 // -----------------------------------------------------------------------------
       
   319 // CLmkDbUtils::AddLmToCategoryL
       
   320 // -----------------------------------------------------------------------------
       
   321 //
       
   322 EXPORT_C void CLmkDbUtils::AddLmToCategoryL(
       
   323 	TPosLmItemId aLandmarkId,
       
   324 	TPosLmItemId aCategoryId,
       
   325 	CPosLandmarkDatabase& aDb )
       
   326 	{
       
   327 	CPosLandmark* landmark = aDb.ReadLandmarkLC( aLandmarkId );
       
   328 	landmark->AddCategoryL( aCategoryId );
       
   329 	aDb.UpdateLandmarkL( *landmark );
       
   330 	CleanupStack::PopAndDestroy(); // landmark
       
   331 	}
       
   332 
       
   333 // -----------------------------------------------------------------------------
       
   334 // CLmkDbUtils::AddLmToCategoriesL
       
   335 // -----------------------------------------------------------------------------
       
   336 //
       
   337 EXPORT_C void CLmkDbUtils::AddLmToCategoriesL(
       
   338 	TPosLmItemId aLandmarkId,
       
   339 	const RArray<TPosLmItemId>& aCategoryIdArray )
       
   340 	{
       
   341 	LeaveIfInUseL();
       
   342 
       
   343 	CPosLmOperation* operation =
       
   344 	    CLmkAddToCatOperation::NewL( *iCategoryMgr,
       
   345 	                                 aLandmarkId,
       
   346 	                                 aCategoryIdArray );
       
   347 
       
   348 	SetupAndStartOperationL( operation, EAddToCategory );
       
   349 	}
       
   350 
       
   351 // -----------------------------------------------------------------------------
       
   352 // CLmkDbUtils::AddLmsToCategoriesL
       
   353 // -----------------------------------------------------------------------------
       
   354 //
       
   355 EXPORT_C void CLmkDbUtils::AddLmsToCategoriesL(
       
   356 	const RArray<TPosLmItemId>& aLandmarkIdArray,
       
   357 	const RArray<TPosLmItemId>& aCategoryIdArray )
       
   358 	{
       
   359 	LeaveIfInUseL();
       
   360 
       
   361 	CPosLmOperation* operation =
       
   362 	    CLmkAddToCatOperation::NewL( *iCategoryMgr,
       
   363 	                                 aLandmarkIdArray,
       
   364 	                                 aCategoryIdArray );
       
   365 
       
   366 	SetupAndStartOperationL( operation, EAddToCategory );
       
   367 	}
       
   368 
       
   369 // -----------------------------------------------------------------------------
       
   370 // CLmkDbUtils::CancelLmOperation
       
   371 // -----------------------------------------------------------------------------
       
   372 //
       
   373 EXPORT_C void CLmkDbUtils::CancelLmOperation()
       
   374 	{
       
   375 	if ( iAOOperation )
       
   376 	    {
       
   377 	    iAOOperation->Cancel();
       
   378 	    delete iAOOperation;
       
   379 	    iAOOperation = NULL;
       
   380 	    }
       
   381 	}
       
   382 
       
   383 // -----------------------------------------------------------------------------
       
   384 // CLmkDbUtils::LeaveIfInUseL
       
   385 // -----------------------------------------------------------------------------
       
   386 //
       
   387 void CLmkDbUtils::LeaveIfInUseL()
       
   388 	{
       
   389 	if ( iAOOperation )
       
   390 		{
       
   391 		User::Leave( KErrInUse );
       
   392 		}
       
   393 	}
       
   394 
       
   395 // -----------------------------------------------------------------------------
       
   396 // CLmkDbUtils::SetupAndStartOperationL
       
   397 // -----------------------------------------------------------------------------
       
   398 //
       
   399 void CLmkDbUtils::SetupAndStartOperationL(
       
   400 	CPosLmOperation* aOperation,
       
   401 	MLmkAOOperationObserver::TOperationTypes aOpType )
       
   402 	{
       
   403 	CleanupStack::PushL( aOperation );
       
   404 	iAOOperation =
       
   405 	CLmkAOOperation::NewL( aOperation,
       
   406 	               *this,
       
   407 	               aOpType,
       
   408 	               iProgressNote );
       
   409 	CleanupStack::Pop( aOperation ); // ownership transferred
       
   410 
       
   411 	iAOOperation->StartOperation();
       
   412 	}
       
   413 
       
   414 // -----------------------------------------------------------------------------
       
   415 // CLmkDbUtils::CategoryIdL()
       
   416 // -----------------------------------------------------------------------------
       
   417 //
       
   418 EXPORT_C  TPosLmItemId CLmkDbUtils::CategoryIdL(
       
   419 	CPosLandmarkDatabase& aDb,
       
   420 	const TDesC&  aCategoryName )
       
   421 	{
       
   422 
       
   423 	CPosLmCategoryManager* categoryMgr = CPosLmCategoryManager::NewL( aDb );
       
   424 	CleanupStack::PushL( categoryMgr );
       
   425 
       
   426 	TPosLmItemId  categoryId = categoryMgr->GetCategoryL(aCategoryName);
       
   427 	if (categoryId == KPosLmNullItemId)
       
   428 		{
       
   429 		    User::Leave(KErrArgument);
       
   430 		}
       
   431 
       
   432 	CleanupStack::PopAndDestroy(categoryMgr); //, categoryMgr
       
   433 	return categoryId;
       
   434 	}
       
   435 
       
   436 
       
   437 // -----------------------------------------------------------------------------
       
   438 // CLmkDbUtils::IsLmWithThisFieldExist
       
   439 // -----------------------------------------------------------------------------
       
   440 //
       
   441 EXPORT_C TBool CLmkDbUtils::IsLmWithThisFieldExistL( TPositionFieldId  aFieldId )
       
   442 	{
       
   443 	RArray<TPosLmItemId> lmIdArray;
       
   444 	CleanupClosePushL( lmIdArray );
       
   445 	lmIdArray.Reset();
       
   446 
       
   447 	TPosLmSortPref sortOrder( CPosLandmark::ELandmarkName,
       
   448 	                          TPosLmSortPref::EAscending );
       
   449 	CPosLmItemIterator* iterator = iDb.LandmarkIteratorL( sortOrder );
       
   450 	CleanupStack::PushL( iterator );
       
   451 	TUint count = iterator->NumOfItemsL();
       
   452 	if ( count > 0 )
       
   453 		{ // can only be called if there are some items
       
   454 		iterator->GetItemIdsL( lmIdArray, 0, count );
       
   455 		}
       
   456 	CleanupStack::PopAndDestroy( iterator );
       
   457 
       
   458 	// search for lm with given position field
       
   459 	TBool  lmkFieldDefined = EFalse;
       
   460 	for (TInt i = 0; i< lmIdArray.Count(); i++)
       
   461 		{
       
   462 		CPosLandmark* landmark = iDb.ReadLandmarkLC(lmIdArray[i]);
       
   463 		if ( landmark->IsPositionFieldAvailable( aFieldId ) )
       
   464 			{
       
   465 			// Check if field info availabe or not
       
   466 			TPtrC  fieldValue;
       
   467 			if (landmark->GetPositionField( aFieldId, fieldValue ) == KErrNone )
       
   468 				{
       
   469 				if (fieldValue.Length() > 0 )
       
   470 					{
       
   471 					lmkFieldDefined = ETrue;
       
   472 					CleanupStack::PopAndDestroy ( landmark );
       
   473 					break;
       
   474 					}
       
   475 				}
       
   476 			}
       
   477 		CleanupStack::PopAndDestroy ( landmark );
       
   478 		}
       
   479 	CleanupStack::PopAndDestroy( ); // LmIdArray
       
   480 	return lmkFieldDefined;
       
   481 	}
       
   482 
       
   483 
       
   484 // -----------------------------------------------------------------------------
       
   485 // CLmkDbUtils::CheckToTrimUrlProtocol
       
   486 // -----------------------------------------------------------------------------
       
   487 //
       
   488 EXPORT_C void CLmkDbUtils::RemoveDefaultProtocolL(TPtr& aWebUrl)
       
   489 	{
       
   490  	TInt emptySlashPos(0);
       
   491     if ( aWebUrl.Length() > 0 )
       
   492         {
       
   493         emptySlashPos = aWebUrl.Find( KDoubleSlash );
       
   494         if ( emptySlashPos == 0 )
       
   495             {
       
   496             aWebUrl.Delete(emptySlashPos, 2);
       
   497             }
       
   498         if(aWebUrl.Length() > KMaxUrlFieldLen)
       
   499 	    	{
       
   500 	    	TInt pos = aWebUrl.Find(KProtocol);
       
   501 	    	aWebUrl.Delete(emptySlashPos, (pos + KUrlProtocolEnds));
       
   502 	    	}
       
   503         }
       
   504 	}
       
   505 
       
   506 // -----------------------------------------------------------------------------
       
   507 // CLmkDbUtils::AddDefaultProtocolL
       
   508 // -----------------------------------------------------------------------------
       
   509 //
       
   510 EXPORT_C void CLmkDbUtils::AddDefaultProtocolL(CPosLandmark* landmark)
       
   511 	{
       
   512  	HBufC* webUrl = HBufC::NewL(KMaxBufferLen);
       
   513     CleanupStack::PushL(webUrl);
       
   514     TPtrC urlDes = webUrl->Des();
       
   515     landmark->GetPositionField(ELmkPositionFieldWebAddress,urlDes);
       
   516     if(urlDes.Length() > KMaxUrlFieldLenWithDefaultProtocol)
       
   517     	{
       
   518     	TInt pos = urlDes.Find(KProtocol);
       
   519     	if (pos == KErrNotFound)
       
   520     		{
       
   521     		webUrl = webUrl->ReAllocL( KHttp().Length() +  urlDes.Length() );
       
   522     		TPtr url = webUrl->Des();
       
   523     		webUrl->Des().Copy(urlDes);
       
   524     		webUrl->Des().Insert(0, KHttp);
       
   525     		landmark->SetPositionFieldL(ELmkPositionFieldWebAddress,webUrl->Des());
       
   526     		}
       
   527     	}
       
   528     CleanupStack::PopAndDestroy();//webUrl
       
   529 	}
       
   530 //  End of File