locationmanager/locationtrail/src/locationremappingao.cpp
changeset 0 c53acadfccc6
child 15 3cebc1a84278
child 21 50bf9db68373
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2006-2009 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:  Remap location data to location objects*
       
    15 */
       
    16 
       
    17 #include "locationremappingao.h"
       
    18 #include "mdequery.h"
       
    19 #include "mdccommon.h"
       
    20 #include "mdeconstants.h"
       
    21 #include "mdenamespacedef.h"
       
    22 #include "mdeobjectdef.h" 
       
    23 #include "mdeobjectquery.h"
       
    24 
       
    25 using namespace MdeConstants;
       
    26 
       
    27 CLocationRemappingAO* CLocationRemappingAO::NewL()
       
    28 	{
       
    29 	LOG( "CLocationRemappingAO::NewL" ); // DEBUG INFO
       
    30 	
       
    31 	CLocationRemappingAO* self = new (ELeave) CLocationRemappingAO();
       
    32 	CleanupStack::PushL( self );
       
    33 	self->ConstructL();
       
    34 	CleanupStack::Pop( self );
       
    35 	return self;
       
    36 	}
       
    37 
       
    38 void CLocationRemappingAO::ConstructL()
       
    39 	{
       
    40 	LOG( "CLocationRemappingAO::ConstructL" ); // DEBUG INFO
       
    41 	CActiveScheduler::Add( this );
       
    42 	iMdEClient = NULL;
       
    43 	
       
    44 	TRAPD(err, ReadTimeFromCenRepL());
       
    45 	if (err)
       
    46 		{
       
    47 		LOG( "CLocationRemappingAO::ConstructL, Can not read timelimit value from CenRep");
       
    48 		iTimeLimit = TTimeIntervalSeconds( KRemappingTime );
       
    49 		}
       
    50 	}
       
    51 
       
    52 CLocationRemappingAO::~CLocationRemappingAO()
       
    53 	{
       
    54 	LOG( "CLocationRemappingAO::~CLocationRemappingAO" ); // DEBUG INFO
       
    55 
       
    56 	StopRemapping();
       
    57 	Cancel();
       
    58 	
       
    59 	iObjects.ResetAndDestroy();
       
    60 	}
       
    61 
       
    62 CLocationRemappingAO::CLocationRemappingAO() : CActive( CActive::EPriorityStandard ),
       
    63 	iState ( EIdle ),
       
    64 	iContinue( EFalse ),
       
    65 	iTimed( EFalse ),
       
    66 	iRequested( EFalse ),
       
    67 	iStartTime( 0 ),
       
    68 	iEndTime( 0 ),
       
    69 	iTimeLimit( 0 )
       
    70 	{
       
    71 	// No implementation required
       
    72 	}
       
    73 
       
    74 
       
    75 void CLocationRemappingAO::InitialiseL(CMdESession* aMdEClient)
       
    76 	{
       
    77 	LOG( "CLocationRemappingAO::Initialise start" ); // DEBUG INFO
       
    78 	iMdEClient = aMdEClient;
       
    79 	
       
    80 	// namespace defaults
       
    81 	iNamespaceDef = &iMdEClient->GetDefaultNamespaceDefL();
       
    82 	
       
    83 	// media object definitions
       
    84     iObjImageDef = &iNamespaceDef->GetObjectDefL( Image::KImageObject );
       
    85     iObjVideoDef = &iNamespaceDef->GetObjectDefL( Video::KVideoObject );
       
    86     iPropDateDef = &iObjImageDef->GetPropertyDefL( Object::KCreationDateProperty );
       
    87     iPropModifiedDef = &iObjImageDef->GetPropertyDefL( Object::KLastModifiedDateProperty );
       
    88 	
       
    89     // location object definitions
       
    90 	iObjLocationDef = &iNamespaceDef->GetObjectDefL( Location::KLocationObject );
       
    91 	iPropLatDef = &iObjLocationDef->GetPropertyDefL( Location::KLatitudeProperty );
       
    92     iPropLongDef = &iObjLocationDef->GetPropertyDefL( Location::KLongitudeProperty );
       
    93 	iPropAltDef = &iObjLocationDef->GetPropertyDefL( Location::KAltitudeProperty );
       
    94 	
       
    95 	iContainsLocationRelDef = &iNamespaceDef->GetRelationDefL( Relations::KContainsLocation );
       
    96 	
       
    97 	LOG( "CLocationRemappingAO::Initialise end" );
       
    98 	}
       
    99 
       
   100 void CLocationRemappingAO::Append( TRemapItem aItem )
       
   101 	{
       
   102 	iRemapItems.Append( aItem );
       
   103 	}
       
   104 
       
   105 void CLocationRemappingAO::ResetQueue()
       
   106 	{
       
   107 	iRemapItems.Reset();
       
   108 	}
       
   109 
       
   110 TBool CLocationRemappingAO::ItemsInQueue()
       
   111 	{
       
   112 	return iRemapItems.Count() > 0;
       
   113 	}
       
   114 
       
   115 TBool CLocationRemappingAO::CheckQueue()
       
   116 	{
       
   117 	LOG( "CLocationRemappingAO::CheckQueue - start" );
       
   118 	TBool create( EFalse );
       
   119 	TInt index = iRemapItems.Count() - 1;
       
   120 	TTime startTime( 0 );
       
   121 	startTime.UniversalTime();
       
   122 	startTime = startTime - iTimeLimit;
       
   123 	RArray<TItemId> removeLocations; 
       
   124 	
       
   125 	for( TInt i = index; i >= 0; i-- )
       
   126 		{
       
   127 		if( iRemapItems[i].iLocationId == 0 )
       
   128 			{
       
   129 			create = ETrue;
       
   130 			}
       
   131 		
       
   132 		if( iRemapItems[i].iTime < startTime )
       
   133 			{
       
   134 			LOG( "CLocationRemappingAO::CheckQueue - old item found" );
       
   135 			if ( iRemapItems[i].iLocationId != 0 )
       
   136 				{
       
   137 				// save old location id 
       
   138 				TInt err = removeLocations.Find( iRemapItems[i].iLocationId );
       
   139 				if ( err == KErrNotFound )
       
   140 					{
       
   141 					removeLocations.Append( iRemapItems[i].iLocationId );
       
   142 					}
       
   143 				}
       
   144 			iRemapItems.Remove( i );
       
   145 			create = ETrue;
       
   146 			}
       
   147 		}
       
   148 	
       
   149 	// clear all "old" location id's from remap items
       
   150 	if( removeLocations.Count() < 0 )
       
   151 		{
       
   152 		index = iRemapItems.Count();
       
   153 		for( TInt i = 0; i < index; i++ )
       
   154 			{
       
   155 			TInt err = removeLocations.Find( iRemapItems[i].iLocationId );
       
   156 			if ( err != KErrNotFound )
       
   157 				{
       
   158 				LOG( "CLocationRemappingAO::CheckQueue - old item found" );
       
   159 				iRemapItems[i].iLocationId = 0;
       
   160 				}
       
   161 			}
       
   162 		}
       
   163 	
       
   164 	removeLocations.Close();
       
   165 	LOG( "CLocationRemappingAO::CheckQueue - end" );
       
   166 	return create;
       
   167 	}
       
   168 
       
   169 
       
   170 void CLocationRemappingAO::StopRemapping()
       
   171 	{
       
   172 	LOG( "CLocationRemappingAO::StopRemapping" ); // DEBUG INFO
       
   173 	NextState(EIdle);
       
   174 	}
       
   175 
       
   176 void CLocationRemappingAO::StartRemappingObjects( const TLocationData& aLocationData )
       
   177 	{
       
   178 	iLocationData = aLocationData;
       
   179 	LOG( "CLocationRemappingAO::StartRemappingObjects" ); // DEBUG INFO
       
   180 	TInt count = iRemapItems.Count();
       
   181 	if ( count == 0 )
       
   182 		{
       
   183 		LOG("CLocationRemappingAO::StartRemappingObjects No need to remap location objects");
       
   184 		return;
       
   185 		}
       
   186 	NextState( ERemapObjects );
       
   187 	}
       
   188 
       
   189 void CLocationRemappingAO::NextState(TMappingState aState)
       
   190 	{
       
   191 	LOG1( "CLocationRemappingAO::NextState - state: %d", aState ); // DEBUG INFO
       
   192 	iState = aState;
       
   193 	// Request complete if not idling
       
   194 	if (iState != EIdle)
       
   195 		{
       
   196 		TRequestStatus* pStatus = &iStatus;
       
   197 		User::RequestComplete( pStatus, KErrNone );
       
   198 		SetActive();
       
   199 		}	
       
   200 	}
       
   201 
       
   202 void CLocationRemappingAO::DoCancel()
       
   203 	{
       
   204 	LOG( "CLocationRemappingAO::DoCancel" ); // DEBUG INFO
       
   205 	// back to idle
       
   206 	NextState(EIdle);
       
   207 	}
       
   208 
       
   209 TInt CLocationRemappingAO::RunError( TInt aError )
       
   210 	{
       
   211 	if (aError != KErrNone)
       
   212 		{
       
   213 		LOG1( "CLocationRemappingAO::RunError with error code: %d", aError ); // DEBUG INFO
       
   214 		}
       
   215 	// back to idle
       
   216 	NextState(EIdle);
       
   217 	return KErrNone;
       
   218 	}
       
   219 
       
   220 void CLocationRemappingAO::RunL()
       
   221 	{
       
   222 	LOG1( "CCameraTrailMonitorAO::RunL iStatus: %d", iStatus.Int() ); // DEBUG INFO
       
   223 	
       
   224 	switch(iState)
       
   225 		{
       
   226 		case EIdle:			
       
   227 			{
       
   228 			LOG( "CLocationRemappingAO::RunL() - EIdle" );			
       
   229 			break;
       
   230 			}
       
   231 
       
   232 		case ERemapObjects:
       
   233 			{
       
   234 			// start remapping object data if Location Object request succeed
       
   235 			RemapObjectsL();
       
   236 			NextState(ECommitObjects);
       
   237 			break;			
       
   238 			}
       
   239 
       
   240 		case ECommitObjects:
       
   241 			{
       
   242 			// commit location object data
       
   243 			CommitObjectsL();
       
   244 			NextState( EIdle );
       
   245 			break;
       
   246 			}
       
   247 			
       
   248 		default:		
       
   249 			{
       
   250 			User::Leave( KErrUnknown );		
       
   251 			break;
       
   252 			}
       
   253 		}
       
   254 	}
       
   255 
       
   256 
       
   257 void CLocationRemappingAO::RemapObjectsL()
       
   258 	{	
       
   259 	LOG( "CLocationRemappingAO::RemapObjects - start" ); // DEBUG INFO
       
   260 	// remap location data to location objects
       
   261 	TInt count = iRemapItems.Count();
       
   262 	
       
   263 	for( TInt i = 0 ; i < count; i++ )
       
   264 		{
       
   265 		TInt err = iObjectIds.Find( iRemapItems[i].iLocationId );
       
   266 		if ( err == KErrNotFound )
       
   267 			{
       
   268 			iObjectIds.Append( iRemapItems[i].iLocationId );
       
   269 			}
       
   270 		}
       
   271 
       
   272 	count = iObjectIds.Count();
       
   273 	for (TInt i = 0; i < count; i++)
       
   274 		{
       
   275 		CMdEObject* location = iMdEClient->OpenObjectL(iObjectIds[i], *iObjLocationDef);
       
   276 		CleanupStack::PushL( location );
       
   277 		
       
   278 		if (location->PropertyCount(*iPropLatDef) == 0)
       
   279 			{
       
   280 			location->AddReal64PropertyL(*iPropLatDef, iLocationData.iPosition.Latitude());
       
   281 			LOG( "CLocationRemappingAO::RemapObjects - wrote latitude" );
       
   282 			}
       
   283 		if (location->PropertyCount(*iPropLongDef) == 0)
       
   284 			{
       
   285 			location->AddReal64PropertyL(*iPropLongDef, iLocationData.iPosition.Longitude());
       
   286 			LOG( "CLocationRemappingAO::RemapObjects - wrote longitude" );
       
   287 			}
       
   288 		if (location->PropertyCount(*iPropAltDef) == 0)
       
   289 			{
       
   290 			location->AddReal64PropertyL(*iPropAltDef, iLocationData.iPosition.Altitude());
       
   291 			LOG( "CLocationRemappingAO::RemapObjects - wrote altitude" );
       
   292 			}
       
   293 		CMdEProperty* modProp = NULL;
       
   294 		location->Property( *iPropModifiedDef, modProp, 0 );
       
   295 		if ( modProp )
       
   296 			{
       
   297 			TTime timestamp( 0 );
       
   298 			timestamp.UniversalTime();
       
   299 			modProp->SetTimeValueL( timestamp );
       
   300 			}
       
   301 		iObjects.AppendL(location);
       
   302 		CleanupStack::Pop( location );
       
   303 		}
       
   304 	count = iRemapItems.Count();
       
   305 	LOG1("CLocationRemappingAO::RemapObjectsL - updating relations, count:%d", count);
       
   306 	// update relation timestamp, composer will then update exif data
       
   307 	for( TInt i = 0; i < count; i++ )
       
   308 		{
       
   309 		CMdERelation* relation = NULL;
       
   310 		relation = iMdEClient->GetRelationL( iRemapItems[i].iRelationId );
       
   311 		
       
   312     	TTime timestamp( 0 );
       
   313     	timestamp.UniversalTime();
       
   314     	relation->SetLastModifiedDate( timestamp );
       
   315     	
       
   316     	iMdEClient->UpdateRelationL( *relation );
       
   317 		}
       
   318 	LOG("CLocationRemappingAO::RemapObjectsL - relations updated");
       
   319 	
       
   320 	iObjectIds.Reset();
       
   321 	ResetQueue();
       
   322 	
       
   323 	LOG( "CLocationRemappingAO::RemapObjects - end" );
       
   324 	}
       
   325 
       
   326 void CLocationRemappingAO::CommitObjectsL()
       
   327 	{
       
   328 	LOG( "CLocationRemappingAO::CommitObjects" ); // DEBUG INFO
       
   329 	// commit location objects
       
   330 	if( iObjects.Count() > 0 )
       
   331 		{
       
   332 		iMdEClient->CommitObjectsL(iObjects);
       
   333 		iObjects.ResetAndDestroy();
       
   334 		}	
       
   335 	}
       
   336 	    	
       
   337 // --------------------------------------------------------------------------
       
   338 // CLocationManagerServer::ReadCenRepValueL
       
   339 // --------------------------------------------------------------------------
       
   340 //
       
   341 void CLocationRemappingAO::ReadTimeFromCenRepL()
       
   342 	{
       
   343 	LOG( "CLocationRemappingAO::ReadTimeFromCenRepL(), begin" );
       
   344 	CRepository* repository = CRepository::NewLC( KRepositoryUid ); 
       
   345 	TInt value( 0 );
       
   346 	User::LeaveIfError( repository->Get(KRemappingTimeKey, value) );
       
   347 	CleanupStack::PopAndDestroy(repository);
       
   348 
       
   349 	LOG1( "CLocationRemappingAO::ReadTimeFromCenRepL(), Using timelimit value:%d seconds", value );
       
   350 	iTimeLimit = TTimeIntervalSeconds( value );
       
   351 
       
   352     LOG( "CLocationRemappingAO::ReadTimeFromCenRepL(), end" );   
       
   353 	}
       
   354 
       
   355 
       
   356 void CLocationRemappingAO::UpdateRelationsL( TItemId aLocationId )
       
   357 	{ 
       
   358 	LOG("CLocationRemappingAO::UpdateRelationsL - start");
       
   359 	CMdENamespaceDef& namespaceDef = iMdEClient->GetDefaultNamespaceDefL();
       
   360 	
       
   361 	// "contains" relation definition
       
   362 	CMdERelationDef& containsRelDef = namespaceDef.GetRelationDefL( 
       
   363 			Relations::KContainsLocation );
       
   364 
       
   365 	TInt count = iRemapItems.Count() - 1;
       
   366 	for( TInt i = count; i >= 0; i-- )
       
   367 		{
       
   368 		if( iRemapItems[i].iLocationId == 0 )
       
   369 			{
       
   370 			if( iRemapItems[i].iRelationId == 0 )
       
   371 				{
       
   372 				CMdERelation* relationObject = iMdEClient->NewRelationLC( containsRelDef,
       
   373 						iRemapItems[i].iObjectId, aLocationId, 0 );
       
   374 				iMdEClient->AddRelationL( *relationObject );
       
   375 				CleanupStack::PopAndDestroy( relationObject );
       
   376 				LOG("CLocationRemappingAO::UpdateRelationsL - new relation created");
       
   377 				}
       
   378 			else
       
   379 				{
       
   380 				CMdERelation* relationObject = iMdEClient->GetRelationL( iRemapItems[i].iRelationId );
       
   381 				relationObject->SetRightObjectIdL( aLocationId );
       
   382 				iMdEClient->UpdateRelationL( *relationObject );
       
   383 				LOG("CLocationRemappingAO::UpdateRelationsL - old relation updated");
       
   384 				}
       
   385 			iRemapItems.Remove( i );
       
   386 			}
       
   387 		}
       
   388 	LOG("CLocationRemappingAO::UpdateRelationsL - end");
       
   389 	}
       
   390 
       
   391 // --------------------------------------------------------------------------
       
   392 // TRemapItem constructor
       
   393 // --------------------------------------------------------------------------
       
   394 //
       
   395 TRemapItem::TRemapItem() :
       
   396 	iObjectId( 0 ),
       
   397 	iLocationId( 0 ),
       
   398 	iRelationId( 0 ),
       
   399 	iTime( 0 )
       
   400 	{
       
   401 	
       
   402 	}
       
   403