locationmanager/locationtrail/src/clocationrecord.cpp
changeset 0 c53acadfccc6
child 1 acef663c1218
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:  A class for recording and storing locations.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32cmn.h> 
       
    19 #include <LbsErrors.h>
       
    20 #include <LbsSatellite.h>
       
    21 
       
    22 #include "rlocationtrail.h"
       
    23 #include "clocationrecord.h"
       
    24 #include "cnetworkinfo.h"
       
    25 #include "locationmanagerdebug.h"
       
    26 #include "locationtraildefs.h"
       
    27 #include "locationtrailpskeys.h"
       
    28 #include "mdeconstants.h"
       
    29 #include <centralrepository.h>
       
    30 
       
    31 
       
    32 using namespace MdeConstants;
       
    33 
       
    34 // --------------------------------------------------------------------------
       
    35 // CLocationRecord::NewL
       
    36 // --------------------------------------------------------------------------
       
    37 //
       
    38 EXPORT_C CLocationRecord* CLocationRecord::NewL()
       
    39     {
       
    40     CLocationRecord* self = new (ELeave) CLocationRecord();
       
    41     CleanupStack::PushL( self );
       
    42     self->ConstructL();
       
    43     CleanupStack::Pop( self );
       
    44     return self;
       
    45     }
       
    46         
       
    47 // --------------------------------------------------------------------------
       
    48 // CLocationRecord::CLocationRecord
       
    49 // --------------------------------------------------------------------------
       
    50 //  
       
    51 CLocationRecord::CLocationRecord()
       
    52     : iNetworkInfoTimer( NULL ),
       
    53     iState( RLocationTrail::ETrailStopped ),
       
    54     iTrailCaptureSetting( RLocationTrail::ECaptureAll ),
       
    55     iLocationCounter( 0 ),
       
    56     iRequestCurrentLoc( EFalse ),
       
    57     iTrailStarted( EFalse ),
       
    58     iLastGPSFixState( EFalse ),
       
    59     iLastLocationId( 0 )
       
    60     {
       
    61     iMaxTrailSize = KMaxTrailLength / KUpdateInterval;
       
    62     }
       
    63 
       
    64 // --------------------------------------------------------------------------
       
    65 // CLocationRecord::ConstructL
       
    66 // --------------------------------------------------------------------------
       
    67 //    
       
    68 void CLocationRecord::ConstructL()
       
    69     {
       
    70     const TInt KMillion = 1000000;
       
    71     TInt err = iProperty.Define( KPSUidLocationTrail, KLocationTrailState, RProperty::EInt );
       
    72     if ( err != KErrNone && err != KErrAlreadyExists )
       
    73         {
       
    74         User::Leave( err );
       
    75         }
       
    76     User::LeaveIfError( iProperty.Set( KPSUidLocationTrail,
       
    77         KLocationTrailState, (TInt) RLocationTrail::ETrailStopped ) ); 
       
    78 
       
    79     iNetworkInfo = CNetworkInfo::NewL( this );
       
    80     iPositionInfo = CPositionInfo::NewL( this );
       
    81 	iRemapper = CLocationRemappingAO::NewL();
       
    82     iNetworkInfoTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
    83     
       
    84     TInt interval( 0 );
       
    85     TRAP(err, ReadCenRepValueL(KIntervalKey, interval));
       
    86     LOG1("CLocationManagerServer::ConstructL, cenrep interval value:%d", interval);
       
    87     
       
    88     if (interval == 0)
       
    89     	{
       
    90         LOG1("CLocationManagerServer::ConstructL, cenrep interval err:%d", err);
       
    91     	iInterval = KUpdateInterval;
       
    92     	}
       
    93     else 
       
    94     	{
       
    95     	iInterval = interval * KMillion;
       
    96     	}
       
    97     
       
    98     TInt trailLength( 0 );
       
    99     TRAP(err, ReadCenRepValueL(KTrailLengthKey, trailLength));
       
   100     LOG1("CLocationManagerServer::ConstructL, cenrep trail length value:%d", trailLength);
       
   101     
       
   102     if ( err != KErrNone )
       
   103     	{
       
   104         LOG1("CLocationManagerServer::ConstructL, cenrep trail length err:%d", err);
       
   105     	iBufferSize = KMaxTrailLength / iInterval;
       
   106     	}
       
   107     else
       
   108     	{
       
   109     	iBufferSize = trailLength * KMillion / iInterval;
       
   110     	}
       
   111 
       
   112     TRAP(err, ReadCenRepValueL(KLocationDeltaKey, iLocationDelta));
       
   113     LOG1("CLocationManagerServer::ConstructL, location delta value:%d", iLocationDelta);
       
   114     
       
   115     if (iLocationDelta == 0)
       
   116     	{
       
   117         LOG1("CLocationManagerServer::ConstructL, location delta err:%d", err);
       
   118         iLocationDelta = KLocationDelta;
       
   119     	}
       
   120 
       
   121     }
       
   122     
       
   123 // --------------------------------------------------------------------------
       
   124 // CLocationRecord::~CLocationRecord
       
   125 // --------------------------------------------------------------------------
       
   126 //    
       
   127 CLocationRecord::~CLocationRecord()
       
   128     {
       
   129     Stop();
       
   130     iProperty.Delete( KPSUidLocationTrail, KLocationTrailState );
       
   131     iProperty.Close();
       
   132     iTrail.Close();
       
   133     
       
   134     delete iNetworkInfo;
       
   135     delete iPositionInfo;
       
   136     delete iNetworkInfoTimer;
       
   137 	if (iRemapper)
       
   138 		{
       
   139 		iRemapper->StopRemapping();
       
   140 		delete iRemapper;
       
   141 		}
       
   142     }
       
   143 
       
   144 // --------------------------------------------------------------------------
       
   145 // CLocationRecord::CurrentState
       
   146 // --------------------------------------------------------------------------
       
   147 //
       
   148 EXPORT_C void CLocationRecord::LocationTrailState( TLocTrailState& aState )
       
   149     {
       
   150     aState = iState;
       
   151     }
       
   152 
       
   153 // --------------------------------------------------------------------------
       
   154 // CLocationRecord::StartL
       
   155 // --------------------------------------------------------------------------
       
   156 //
       
   157 EXPORT_C void CLocationRecord::StartL( RLocationTrail::TTrailCaptureSetting aCaptureSetting )
       
   158     {
       
   159     LOG( "CLocationRecord::StartL(), begin" );
       
   160     iTrailCaptureSetting = aCaptureSetting;
       
   161     if ( aCaptureSetting == RLocationTrail::ECaptureAll && !iPositionInfo->IsActive() )
       
   162         {
       
   163         iPositionInfo->StartL( aCaptureSetting, iInterval );
       
   164         }
       
   165     else if ( aCaptureSetting == RLocationTrail::ECaptureNetworkInfo )
       
   166     	{
       
   167     	// Update and store network info in location trail immediately.
       
   168     	// Timer will trigger the update again later.
       
   169     	UpdateNetworkInfo( this );
       
   170     	
       
   171         if ( iNetworkInfoTimer && iNetworkInfoTimer->IsActive() )
       
   172         	{
       
   173         	iNetworkInfoTimer->Cancel();
       
   174         	}
       
   175         	
       
   176         StartTimerL();
       
   177     	}
       
   178     
       
   179     iLastLocationId = 0;
       
   180     
       
   181     SetCurrentState( RLocationTrail::ETrailStarting );
       
   182     
       
   183     iTrailStarted = ETrue;
       
   184     LOG( "CLocationRecord::StartL(), end" );
       
   185     }
       
   186 
       
   187 // --------------------------------------------------------------------------
       
   188 // CLocationRecord::Stop
       
   189 // --------------------------------------------------------------------------
       
   190 //
       
   191 EXPORT_C void CLocationRecord::Stop()
       
   192     {
       
   193     LOG( "CLocationRecord::StopL(), begin" );
       
   194     iPositionInfo->Stop();
       
   195     iTrailStarted = EFalse;
       
   196     
       
   197     if ( iNetworkInfoTimer && iNetworkInfoTimer->IsActive() )
       
   198     	{
       
   199     	iNetworkInfoTimer->Cancel();
       
   200     	}
       
   201 
       
   202     if ( iRemapper )
       
   203     	{
       
   204     	iRemapper->ResetQueue();
       
   205     	}
       
   206     SetCurrentState( RLocationTrail::ETrailStopped );
       
   207     LOG( "CLocationRecord::StopL(), end" );
       
   208     }
       
   209 
       
   210 // --------------------------------------------------------------------------
       
   211 // CLocationRecord::SetStateToStop
       
   212 // --------------------------------------------------------------------------
       
   213 //
       
   214 EXPORT_C void CLocationRecord::SetStateToStopping()
       
   215 	{
       
   216 	SetCurrentState( RLocationTrail::ETrailStopping );
       
   217 	}
       
   218 
       
   219 // --------------------------------------------------------------------------
       
   220 // CLocationRecord::GetLocationByTimeL
       
   221 // --------------------------------------------------------------------------
       
   222 //
       
   223 EXPORT_C void CLocationRecord::GetLocationByTimeL( const TTime aTime, 
       
   224 												   TLocationData& aLocationData,
       
   225                                                    TLocTrailState& aState )
       
   226     {
       
   227     LOG( "CLocationRecord::GetLocationByTimeL(), begin" );
       
   228     TInt posFound( EFalse );
       
   229 
       
   230 #ifdef _DEBUG
       
   231     _LIT( KDateTimeFormat, "%Y/%M/%D %H:%T:%S" );
       
   232     const TInt DateTimeStrMaxLength = 20;
       
   233     LOG1( "CLocationRecord::GetLocationByTimeL - aTime: %Ld", aTime.Int64() );
       
   234     TBuf<DateTimeStrMaxLength> str1;
       
   235     aTime.FormatL( str1, KDateTimeFormat );
       
   236     LOG1( "CLocationRecord::GetLocationByTimeL - aTime: %S", &str1 );
       
   237 #endif
       
   238 
       
   239     TTimeIntervalSeconds interval;
       
   240     TTimeIntervalSeconds nextInterval;
       
   241     for ( TInt i(iTrail.Count()-1) ; i >= 0 && !posFound ; i-- )
       
   242         {
       
   243         TInt err = iTrail[i].iTimeStamp.SecondsFrom( aTime, interval );
       
   244         
       
   245         TInt timeDiff = Abs( interval.Int() );
       
   246 
       
   247 #ifdef _DEBUG
       
   248         LOG1( "CLocationRecord::GetLocationByTimeL - Trail timestamp: %Ld", iTrail[i].iTimeStamp.Int64() );
       
   249         TBuf<DateTimeStrMaxLength> str;
       
   250         iTrail[i].iTimeStamp.FormatL( str, KDateTimeFormat );
       
   251         LOG1( "CLocationRecord::GetLocationByTimeL - Trail timestamp: %S", &str );
       
   252         LOG1( "CLocationRecord::GetLocationByTimeL - timeDiff: %d", timeDiff );
       
   253 #endif
       
   254 
       
   255         if ( err == KErrNone && timeDiff <= KIntervalSeconds )
       
   256             {
       
   257             // The nearest time is in iTrail[i] or in iTrail[i-1].
       
   258             if ( i > 0 )
       
   259                 {
       
   260                 iTrail[i-1].iTimeStamp.SecondsFrom( aTime, nextInterval );
       
   261                 
       
   262                 TInt nextDiff = Abs( nextInterval.Int() );
       
   263                     
       
   264                 if ( nextDiff < timeDiff )
       
   265                     {
       
   266                     aLocationData = iTrail[i-1].iLocationData;
       
   267                     aState = iTrail[i-1].iTrailState;
       
   268                     }
       
   269                 else
       
   270                     {
       
   271                     aLocationData = iTrail[i].iLocationData;
       
   272                     aState = iTrail[i].iTrailState;
       
   273                     }                    
       
   274                 }            
       
   275             else
       
   276                 {
       
   277                 aLocationData = iTrail[i].iLocationData;
       
   278                 aState = iTrail[i].iTrailState;
       
   279                 }
       
   280             posFound = ETrue;
       
   281             }
       
   282         }
       
   283     if ( !posFound )
       
   284         {
       
   285         User::Leave( KErrNotFound );
       
   286         }
       
   287     LOG( "CLocationRecord::GetLocationByTimeL(), end" );
       
   288     }
       
   289     
       
   290 // --------------------------------------------------------------------------
       
   291 // CLocationRecord::RequestLocationL
       
   292 // --------------------------------------------------------------------------
       
   293 //
       
   294 EXPORT_C void CLocationRecord::RequestLocationL()
       
   295     {
       
   296     iRequestCurrentLoc = ETrue;
       
   297     if ( iTrailCaptureSetting != RLocationTrail::ECaptureNetworkInfo &&
       
   298     	!iPositionInfo->IsActive() )
       
   299         {
       
   300         iPositionInfo->StartL( iTrailCaptureSetting, iInterval );
       
   301         }
       
   302     else if ( iTrailCaptureSetting == RLocationTrail::ECaptureNetworkInfo )
       
   303     	{
       
   304     	TPositionSatelliteInfo posInfo;
       
   305     	CTelephony::TNetworkInfoV1 network = CTelephony::TNetworkInfoV1();
       
   306     	GetNetworkInfo( network );
       
   307        	iObserver->CurrentLocation( posInfo, network, KErrNone );
       
   308         iRequestCurrentLoc = EFalse;
       
   309     	}
       
   310     }
       
   311 
       
   312 // --------------------------------------------------------------------------
       
   313 // CLocationRecord::CancelLocationRequest
       
   314 // --------------------------------------------------------------------------
       
   315 //    
       
   316 EXPORT_C void CLocationRecord::CancelLocationRequest()
       
   317     {
       
   318     iRequestCurrentLoc = EFalse;
       
   319     if ( !iTrailStarted )
       
   320         {
       
   321         iPositionInfo->Stop();
       
   322         }
       
   323     }
       
   324         
       
   325         
       
   326 // --------------------------------------------------------------------------
       
   327 // CLocationRecord::GetNetworkInfo
       
   328 // --------------------------------------------------------------------------
       
   329 //
       
   330 EXPORT_C void CLocationRecord::GetNetworkInfo( CTelephony::TNetworkInfoV1& aNetworkInfo )
       
   331     {
       
   332     LOG("CLocationRecord::GetNetworkInfo");
       
   333 
       
   334     aNetworkInfo = iNetwork;
       
   335     }
       
   336     
       
   337 // --------------------------------------------------------------------------
       
   338 // CLocationRecord::SetObserver
       
   339 // --------------------------------------------------------------------------
       
   340 //
       
   341 EXPORT_C void CLocationRecord::SetObserver( MLocationTrailObserver* aObserver)
       
   342     {
       
   343     iObserver = aObserver;
       
   344     }
       
   345 
       
   346 // --------------------------------------------------------------------------
       
   347 // CLocationRecord::SetAddObserver
       
   348 // --------------------------------------------------------------------------
       
   349 //
       
   350 EXPORT_C void CLocationRecord::SetAddObserver( MLocationAddObserver* aObserver)
       
   351     {
       
   352     iAddObserver = aObserver;
       
   353     }
       
   354 
       
   355 // --------------------------------------------------------------------------
       
   356 // From MNetworkInfoObserver.
       
   357 // CLocationRecord::Position
       
   358 // --------------------------------------------------------------------------
       
   359 //    
       
   360 void CLocationRecord::Position( const TPositionInfo& aPositionInfo,
       
   361                                 const TInt aError  )
       
   362     {    
       
   363     const TPositionSatelliteInfo& positionSatelliteInfo = 
       
   364     	static_cast<const TPositionSatelliteInfo&>(aPositionInfo);
       
   365 
       
   366     if ( iRequestCurrentLoc )
       
   367         {
       
   368         HandleLocationRequest( positionSatelliteInfo, aError );
       
   369         }
       
   370     if( iState == RLocationTrail::ETrailStopped )
       
   371     	{
       
   372     	LOG("CLocationRecord::Position - trail stopped");
       
   373     	return;
       
   374     	}
       
   375     
       
   376     if ( !iTrailStarted )
       
   377         {
       
   378         iPositionInfo->NextPosition();
       
   379         return;
       
   380         }
       
   381     switch ( aError )
       
   382         {
       
   383         case KPositionPartialUpdate: // fall through
       
   384         case KPositionQualityLoss: 
       
   385             {
       
   386             // Location is stored, even if it may not be valid.
       
   387             StoreLocation( positionSatelliteInfo ); 
       
   388             LOG("CLocationRecord::Position - partial update");
       
   389             if ( iState != RLocationTrail::EWaitingGPSData && 
       
   390             	 iState != RLocationTrail::ETrailStopping ) 
       
   391                 {
       
   392                 SetCurrentState( RLocationTrail::EWaitingGPSData );
       
   393             	LOG("CLocationRecord::Position trail waiting for gps");
       
   394                 }
       
   395             break;
       
   396             }
       
   397         case KErrNone:
       
   398             {
       
   399             StoreLocation( positionSatelliteInfo );
       
   400             LOG("CLocationRecord::Position - good GPS coordinates");
       
   401             if ( iState != RLocationTrail::ETrailStarted ) 
       
   402                 {
       
   403                 if ( iRemapper )
       
   404                 	{
       
   405                 	LOG("CLocationRecord::Position start remapping");
       
   406                 	iLastLocationId = 0;
       
   407                 	TBool createLocation = iRemapper->CheckQueue();
       
   408                 	if( createLocation )
       
   409                 		{
       
   410                 		TRAP_IGNORE(	
       
   411                 		TItemId locationId = DoCreateLocationL( iNewItem.iLocationData );
       
   412                 		iRemapper->UpdateRelationsL( locationId );
       
   413                 		)
       
   414                 		}
       
   415                		iRemapper->StartRemappingObjects( iNewItem.iLocationData );
       
   416                 	}
       
   417                 if ( iState != RLocationTrail::ETrailStopping )
       
   418                 	{
       
   419                     SetCurrentState( RLocationTrail::ETrailStarted );
       
   420                 	LOG("CLocationRecord::Position trail started");
       
   421                 	}
       
   422                 }
       
   423             break;
       
   424             }
       
   425         default:
       
   426             {
       
   427             StoreLocation( positionSatelliteInfo );
       
   428             LOG1("CLocationRecord::Position - searching GPS, aError %d", aError );
       
   429             if ( iState != RLocationTrail::ESearchingGPS &&
       
   430                	 iState != RLocationTrail::ETrailStopping ) 
       
   431                 {
       
   432                 SetCurrentState( RLocationTrail::ESearchingGPS );
       
   433             	LOG("CLocationRecord::Position trail searching gps");
       
   434                 }
       
   435             break;
       
   436             }      
       
   437         }
       
   438     TBool fixState = CheckGPSFix( positionSatelliteInfo );
       
   439     LOG1( "CLocationRecord::Position fixState %d", fixState );
       
   440     LOG1( "CLocationRecord::Position iLastGPSFixState %d", iLastGPSFixState );
       
   441     
       
   442     if ( iObserver && iLastGPSFixState != fixState )
       
   443     	{
       
   444     	LOG("CLocationRecord::Position quality changed");
       
   445     	iObserver->GPSSignalQualityChanged( positionSatelliteInfo );
       
   446     	}
       
   447     
       
   448    	iLastGPSFixState = fixState;
       
   449     
       
   450     iPositionInfo->NextPosition();
       
   451     }
       
   452 
       
   453 TBool CLocationRecord::CheckGPSFix( const TPositionSatelliteInfo& aSatelliteInfo )
       
   454 	{
       
   455 	TPosition position;
       
   456 	aSatelliteInfo.GetPosition( position );
       
   457 	LOG1( "CLocationRecord::CheckGPSFix latitude %f", position.Latitude() );
       
   458 	LOG1( "CLocationRecord::CheckGPSFix longitude %f", position.Longitude() );
       
   459 	TBool ret = ( Math::IsNaN(position.Latitude()) || Math::IsNaN(position.Longitude()) ) 
       
   460 		? EFalse : ETrue;
       
   461    	return ret;
       
   462 	}
       
   463     
       
   464 // --------------------------------------------------------------------------
       
   465 // From MPositionerObserver.
       
   466 // CLocationRecord::NetworkInfo
       
   467 // --------------------------------------------------------------------------
       
   468 //    
       
   469 void CLocationRecord::NetworkInfo( const CTelephony::TNetworkInfoV1 &aNetworkInfo, 
       
   470 		const TInt aError )
       
   471     {
       
   472     LOG("CLocationRecord::NetworkInfo");
       
   473     if ( aError == KErrNone )
       
   474         {
       
   475         LOG("CLocationRecord::NetworkInfo - KErrNone");
       
   476         iNetwork = aNetworkInfo;
       
   477         if (iNetwork.iAccess == CTelephony::ENetworkAccessUtran)
       
   478         	{
       
   479         	iNetwork.iLocationAreaCode = 0;
       
   480         	}
       
   481         if ( iState == RLocationTrail::ETrailStarting && iTrailStarted )
       
   482         	{
       
   483         	SetCurrentState( RLocationTrail::ETrailStarted );
       
   484         	}
       
   485         }
       
   486     else
       
   487         {
       
   488         LOG1("CLocationRecord::NetworkInfo - %d", aError );
       
   489         iNetwork = CTelephony::TNetworkInfoV1();
       
   490         iNetwork.iAreaKnown = EFalse;
       
   491         iNetwork.iAccess = CTelephony::ENetworkAccessUnknown;
       
   492         iNetwork.iCellId = 0;
       
   493         iNetwork.iLocationAreaCode = 0;
       
   494         iNetwork.iCountryCode.Zero();
       
   495         iNetwork.iNetworkId.Zero();
       
   496         }
       
   497     }
       
   498 
       
   499 // --------------------------------------------------------------------------
       
   500 // CLocationRecord::StoreLocationL
       
   501 // --------------------------------------------------------------------------
       
   502 //    
       
   503 void CLocationRecord::StoreLocation( const TPositionSatelliteInfo& aSatelliteInfo )
       
   504     {
       
   505     aSatelliteInfo.GetPosition( iNewItem.iLocationData.iPosition );
       
   506     aSatelliteInfo.GetCourse( iNewItem.iLocationData.iCourse );
       
   507     iNewItem.iLocationData.iSatellites = aSatelliteInfo.NumSatellitesUsed();
       
   508     iNewItem.iLocationData.iQuality = aSatelliteInfo.HorizontalDoP();
       
   509     
       
   510     // Network info
       
   511     GetNetworkInfo( iNewItem.iLocationData.iNetworkInfo );
       
   512     // Get Universal time
       
   513     iNewItem.iTimeStamp.UniversalTime();
       
   514     iNewItem.iTrailState = iState;
       
   515     
       
   516     TInt error = iTrail.Append( iNewItem );
       
   517     
       
   518     // If appending an item to the trail fails because of OOM, remove oldest trail items
       
   519     // until the new item fits or there's only one item left in the trail.
       
   520     while ( error == KErrNoMemory && iTrail.Count() > 1 )
       
   521 		{
       
   522 		LOG("CLocationRecord::StoreLocation - Out of memory! Shortening trail!");
       
   523 		iTrail.Remove( 0 );
       
   524 		error = iTrail.Append( iNewItem );
       
   525 		}
       
   526     
       
   527     if ( iTrail.Count() > iMaxTrailSize )
       
   528         {
       
   529         iTrail.Remove( 0 );
       
   530         }
       
   531     
       
   532     if( iAddObserver )
       
   533     	{
       
   534     	iAddObserver->LocationAdded( iNewItem, aSatelliteInfo );
       
   535     	}
       
   536     }
       
   537     
       
   538 // --------------------------------------------------------------------------
       
   539 // CLocationRecord::SetCurrentState
       
   540 // --------------------------------------------------------------------------
       
   541 //        
       
   542 void CLocationRecord::SetCurrentState( TLocTrailState aState )    
       
   543     {
       
   544     LOG1( "CLocationRecord::SetCurrentState(), begin, state:%d", aState );
       
   545     iState = aState;
       
   546     iProperty.Set( KPSUidLocationTrail, KLocationTrailState, (TInt) aState );
       
   547     if ( iObserver )
       
   548         {
       
   549         iObserver->LocationTrailStateChange();
       
   550         }
       
   551     LOG( "CLocationRecord::SetCurrentState(), end" );
       
   552     }
       
   553     
       
   554 // --------------------------------------------------------------------------
       
   555 // CLocationRecord::HandleLocationRequest
       
   556 // --------------------------------------------------------------------------
       
   557 //
       
   558 void CLocationRecord::HandleLocationRequest( const TPositionSatelliteInfo& aSatelliteInfo, 
       
   559                                              const TInt aError )    
       
   560     {
       
   561 	CTelephony::TNetworkInfoV1 network = CTelephony::TNetworkInfoV1();
       
   562     if ( aError == KErrNone )
       
   563         {
       
   564        	GetNetworkInfo( network );
       
   565         iObserver->CurrentLocation( aSatelliteInfo, network, aError );
       
   566         iRequestCurrentLoc = EFalse;
       
   567         if ( !iTrailStarted )
       
   568             {
       
   569             iPositionInfo->Stop();
       
   570             }
       
   571         }
       
   572     else
       
   573         {
       
   574         iLocationCounter++;
       
   575         if ( iLocationCounter > KCurrentLocTimeoutCount )
       
   576             {
       
   577             iObserver->CurrentLocation( aSatelliteInfo, network, KErrTimedOut );
       
   578             iRequestCurrentLoc = EFalse;
       
   579             iLocationCounter = 0;
       
   580             if ( !iTrailStarted )
       
   581                 {
       
   582                 iPositionInfo->Stop();
       
   583                 }
       
   584             }       
       
   585         }    
       
   586     }
       
   587 
       
   588 TInt CLocationRecord::UpdateNetworkInfo( TAny* aAny )
       
   589 	{
       
   590 	TPositionSatelliteInfo nullPositionInfo;
       
   591 	CLocationRecord* self = STATIC_CAST( CLocationRecord*, aAny );
       
   592 	self->StoreLocation( nullPositionInfo );
       
   593 	return KErrNone;
       
   594 	}
       
   595 
       
   596 
       
   597 EXPORT_C void CLocationRecord::CreateLocationObjectL( const TLocationData& aLocationData,
       
   598 		const TUint& aObjectId )
       
   599 	{
       
   600 	TItemId locationId = DoCreateLocationL( aLocationData );
       
   601 	CreateRelationL( aObjectId, locationId );
       
   602 	}
       
   603 
       
   604 
       
   605 EXPORT_C void CLocationRecord::LocationSnapshotL( const TUint& aObjectId )
       
   606 	{
       
   607 	LOG("CLocationRecord::LocationSnapshotL");
       
   608 	
       
   609 	TBool previousMatch = EFalse;
       
   610 	CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
       
   611 
       
   612 	// get locationdata from trail with object time
       
   613 	TTime timestamp = GetMdeObjectTimeL( aObjectId );
       
   614 	TLocationData locationData;
       
   615 	TLocTrailState state;
       
   616 	GetLocationByTimeL( timestamp, locationData, state );
       
   617 	
       
   618 	iObjectId = aObjectId;
       
   619 	iLocationData = locationData;
       
   620 
       
   621 	// capture only network data
       
   622 	if ( iTrailCaptureSetting == RLocationTrail::ECaptureNetworkInfo )
       
   623 		{
       
   624 		CTelephony::TNetworkInfoV1* net = &locationData.iNetworkInfo;
       
   625 		
       
   626 		if ( net->iCellId == 0 && 
       
   627 				net->iLocationAreaCode == 0 &&
       
   628 				net->iCountryCode.Length() == 0 &&
       
   629 				net->iNetworkId.Length() == 0 )
       
   630 			{
       
   631 			// nothing to do
       
   632 			LOG("CLocationRecord::LocationSnapshotL - no network info available");
       
   633 			}
       
   634 		else if ( iLastLocationId != 0 )
       
   635 			{
       
   636 			CTelephony::TNetworkInfoV1* lastnet = &iLastLocation.iNetworkInfo;
       
   637 			
       
   638 			// compare to previous network info
       
   639 			TItemId locationId = iLastLocationId;
       
   640 			if ( lastnet->iCellId != net->iCellId ||
       
   641 					lastnet->iLocationAreaCode != net->iLocationAreaCode ||
       
   642 					lastnet->iCountryCode != net->iCountryCode ||
       
   643 					lastnet->iNetworkId != net->iNetworkId )
       
   644 				{
       
   645 				LOG("CLocationRecord::LocationSnapshotL - network info changed");
       
   646 				locationId = DoCreateLocationL( locationData );
       
   647 				}
       
   648 			CreateRelationL( aObjectId, locationId );
       
   649 			}
       
   650 		else 
       
   651 			{
       
   652 			// new location
       
   653 			TItemId locationId = DoCreateLocationL( locationData );
       
   654 			CreateRelationL( aObjectId, locationId );
       
   655 			}
       
   656 		return;
       
   657 		}
       
   658 	
       
   659 	// coordinates empty (will be remapped)
       
   660 	if ( Math::IsNaN( locationData.iPosition.Latitude() ) && 
       
   661 			Math::IsNaN( locationData.iPosition.Longitude() ))
       
   662 		{
       
   663 		TRemapItem remapItem;
       
   664 		remapItem.iObjectId = aObjectId;
       
   665 		remapItem.iTime = timestamp;
       
   666 		
       
   667 		CTelephony::TNetworkInfoV1* net = &locationData.iNetworkInfo;
       
   668 
       
   669 		// no network info (offline mode + no GPS fix)
       
   670 		if ( net->iCellId == 0 && 
       
   671 				net->iLocationAreaCode == 0 &&
       
   672 				net->iCountryCode.Length() == 0 &&
       
   673 				net->iNetworkId.Length() == 0 )
       
   674 			{
       
   675 			LOG("CLocationRecord::LocationSnapshotL - empty remap item created");
       
   676 			}
       
   677 		// check match for last created locationobject
       
   678 		else if ( iLastLocationId != 0 )
       
   679 			{
       
   680 			TItemId locationId;
       
   681 			CTelephony::TNetworkInfoV1* lastnet = &iLastLocation.iNetworkInfo;
       
   682 
       
   683 			// networkinfo changed from last location
       
   684 			if ( lastnet->iCellId != net->iCellId ||
       
   685 					lastnet->iLocationAreaCode != net->iLocationAreaCode ||
       
   686 					lastnet->iCountryCode != net->iCountryCode ||
       
   687 					lastnet->iNetworkId != net->iNetworkId )
       
   688 				{
       
   689 				LOG("CLocationRecord::LocationSnapshotL - remap with new network info");
       
   690 				locationId = DoCreateLocationL( locationData );
       
   691 				}		
       
   692 			else
       
   693 				{
       
   694 				LOG("CLocationRecord::LocationSnapshotL - remap with previous network info");
       
   695 				locationId = iLastLocationId;
       
   696 				}
       
   697 			TItemId relationId = CreateRelationL( aObjectId, locationId );
       
   698 			remapItem.iLocationId = locationId;
       
   699 			remapItem.iRelationId = relationId;
       
   700 			}
       
   701 		else
       
   702 			{
       
   703 			// new location with only network data
       
   704 			TItemId locationId = DoCreateLocationL( locationData );
       
   705 			TItemId relationId = CreateRelationL( aObjectId, locationId );
       
   706 			remapItem.iLocationId = locationId;
       
   707 			remapItem.iRelationId = relationId;
       
   708 			}
       
   709 		iRemapper->Append( remapItem );
       
   710 		return;
       
   711 		}
       
   712 		
       
   713 	// valid coordinates found
       
   714 	if ( iLastLocationId != 0 )
       
   715 		{
       
   716 		CTelephony::TNetworkInfoV1* net = &locationData.iNetworkInfo;
       
   717 		CTelephony::TNetworkInfoV1* lastnet = &iLastLocation.iNetworkInfo;
       
   718 		
       
   719 		// first check if networkinfo matches last created location
       
   720 		if ( lastnet->iCellId == net->iCellId &&
       
   721 				lastnet->iLocationAreaCode == net->iLocationAreaCode &&
       
   722 				lastnet->iCountryCode == net->iCountryCode &&
       
   723 				lastnet->iNetworkId == net->iNetworkId )
       
   724 			{
       
   725 			LOG("CLocationRecord::LocationSnapshotL - network info matches");
       
   726 			
       
   727 			// if both locations have valid coordinates, calculate distance between points
       
   728 			if ( !Math::IsNaN( iLastLocation.iPosition.Latitude() ) && 
       
   729 					!Math::IsNaN( iLastLocation.iPosition.Longitude() ) && 
       
   730 					!Math::IsNaN( locationData.iPosition.Latitude() ) && 
       
   731 					!Math::IsNaN( locationData.iPosition.Longitude() ))
       
   732 				{
       
   733 				TReal32 distance;
       
   734 				TInt err = locationData.iPosition.Distance(iLastLocation.iPosition, distance);
       
   735 				
       
   736 				if ( distance < iLocationDelta )
       
   737 					{
       
   738 					LOG("CLocationRecord::LocationSnapshotL - location close to the previous one");
       
   739 					previousMatch = ETrue;
       
   740 					CreateRelationL( aObjectId, iLastLocationId );
       
   741 					LOG("CLocationRecord::CreateLocationObjectL - last location matched");
       
   742 					}
       
   743 				}
       
   744 			}
       
   745 		}
       
   746 	
       
   747 	// last location did not match, find existing one from DB
       
   748 	if( !previousMatch )
       
   749 		{
       
   750 		LOG("CLocationRecord::LocationSnapshotL - query location");
       
   751 		const TReal64 KMeterInDegrees = 0.000009;
       
   752 		const TReal64 KPi = 3.14159265358979;
       
   753 		const TReal32 K180Degrees = 180.0;
       
   754 	
       
   755 		TReal64 latitude = locationData.iPosition.Latitude();
       
   756 		TReal64 longitude = locationData.iPosition.Longitude();
       
   757 		// calculate distance in degrees
       
   758 		TReal64 cosine;
       
   759 		Math::Cos(cosine, locationData.iPosition.Latitude() * KPi / K180Degrees );
       
   760 		TReal64 latDelta = iLocationDelta * KMeterInDegrees;
       
   761 		TReal64 lonDelta = latDelta * cosine;
       
   762 		
       
   763 		CMdEObjectDef& locationObjectDef = namespaceDef.GetObjectDefL( Location::KLocationObject );
       
   764 		
       
   765 		CMdEPropertyDef& latitudeDef = locationObjectDef.GetPropertyDefL(
       
   766 				Location::KLatitudeProperty );
       
   767 		CMdEPropertyDef& longitudeDef = locationObjectDef.GetPropertyDefL(
       
   768 				Location::KLongitudeProperty );
       
   769 		CMdEPropertyDef& cellIdDef = locationObjectDef.GetPropertyDefL(
       
   770 				Location::KCellIdProperty );
       
   771 		CMdEPropertyDef& locationCodeDef = locationObjectDef.GetPropertyDefL( 
       
   772 				Location::KLocationAreaCodeProperty );
       
   773 		CMdEPropertyDef& countryCodeDef = locationObjectDef.GetPropertyDefL( 
       
   774 				Location::KCountryCodeProperty );
       
   775 		CMdEPropertyDef& networkCodeDef = locationObjectDef.GetPropertyDefL( 
       
   776 				Location::KNetworkCodeProperty );
       
   777 		
       
   778 		iLocationQuery = iMdeSession->NewObjectQueryL( namespaceDef, locationObjectDef, this );
       
   779 		CMdELogicCondition& cond = iLocationQuery->Conditions();
       
   780 		cond.SetOperator( ELogicConditionOperatorAnd );
       
   781 		
       
   782 		LOG1( "CLocationRecord::LocationSnapshotL latitude: %f", latitude);
       
   783 		LOG1( "CLocationRecord::LocationSnapshotL latdelta: %f", latDelta);
       
   784 		LOG1( "CLocationRecord::LocationSnapshotL longitude: %f", longitude);
       
   785 		LOG1( "CLocationRecord::LocationSnapshotL londelta: %f", lonDelta);
       
   786 		
       
   787 		cond.AddPropertyConditionL( latitudeDef, 
       
   788 				TMdERealBetween( latitude - latDelta, latitude + latDelta ));
       
   789 		cond.AddPropertyConditionL( longitudeDef, 
       
   790 				TMdERealBetween( longitude - lonDelta, longitude + lonDelta ));
       
   791 		cond.AddPropertyConditionL( cellIdDef, 
       
   792 				TMdEUintEqual( locationData.iNetworkInfo.iCellId) );
       
   793 		cond.AddPropertyConditionL( locationCodeDef, 
       
   794 				TMdEUintEqual( locationData.iNetworkInfo.iLocationAreaCode) );
       
   795 		cond.AddPropertyConditionL( countryCodeDef, ETextPropertyConditionCompareEquals,
       
   796 				locationData.iNetworkInfo.iCountryCode );
       
   797 		cond.AddPropertyConditionL( networkCodeDef, ETextPropertyConditionCompareEquals,
       
   798 				locationData.iNetworkInfo.iNetworkId );
       
   799 		
       
   800 		iLocationQuery->FindL();			
       
   801 		}
       
   802 	}
       
   803 
       
   804 	
       
   805 TItemId CLocationRecord::DoCreateLocationL( const TLocationData& aLocationData )
       
   806 	{
       
   807 	LOG("CLocationRecord::DoCreateLocationL - start");
       
   808 	TItemId locationObjectId;
       
   809 	
       
   810 	CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
       
   811 
       
   812 	CMdEObjectDef& locationObjectDef = namespaceDef.GetObjectDefL( Location::KLocationObject );
       
   813 	
       
   814 	// required object properties
       
   815 	CMdEPropertyDef& creationDef = locationObjectDef.GetPropertyDefL(
       
   816 			Object::KCreationDateProperty );
       
   817 	CMdEPropertyDef& modifiedDef = locationObjectDef.GetPropertyDefL(
       
   818 			Object::KLastModifiedDateProperty );
       
   819 	CMdEPropertyDef& sizeDef = locationObjectDef.GetPropertyDefL(
       
   820 			Object::KSizeProperty );
       
   821 	CMdEPropertyDef& itemTypeDef = locationObjectDef.GetPropertyDefL(
       
   822 			Object::KItemTypeProperty );
       
   823 	CMdEPropertyDef& offSetDef = locationObjectDef.GetPropertyDefL( 
       
   824 			Object::KTimeOffsetProperty );
       
   825 
       
   826 	// location related properties
       
   827 	CMdEPropertyDef& cellIdDef = locationObjectDef.GetPropertyDefL(
       
   828 			Location::KCellIdProperty );
       
   829 	CMdEPropertyDef& latitudeDef = locationObjectDef.GetPropertyDefL(
       
   830 			Location::KLatitudeProperty );
       
   831 	CMdEPropertyDef& longitudeDef = locationObjectDef.GetPropertyDefL(
       
   832 			Location::KLongitudeProperty );
       
   833 	CMdEPropertyDef& altitudeDef = locationObjectDef.GetPropertyDefL(
       
   834 			Location::KAltitudeProperty );
       
   835 
       
   836 	CMdEPropertyDef& directionDef = locationObjectDef.GetPropertyDefL(
       
   837 			Location::KDirectionProperty );
       
   838 	CMdEPropertyDef& speedDef = locationObjectDef.GetPropertyDefL( 
       
   839 			Location::KSpeedProperty );
       
   840 	CMdEPropertyDef& locationCodeDef = locationObjectDef.GetPropertyDefL( 
       
   841 			Location::KLocationAreaCodeProperty );
       
   842 	CMdEPropertyDef& countryCodeDef = locationObjectDef.GetPropertyDefL( 
       
   843 			Location::KCountryCodeProperty );
       
   844 	CMdEPropertyDef& networkCodeDef = locationObjectDef.GetPropertyDefL( 
       
   845 			Location::KNetworkCodeProperty );
       
   846 	CMdEPropertyDef& qualityDef = locationObjectDef.GetPropertyDefL( 
       
   847 			Location::KQualityProperty );
       
   848 
       
   849 	// location object
       
   850 	CMdEObject* locationObject = NULL;
       
   851 
       
   852 	locationObject = iMdeSession->NewObjectL( locationObjectDef, Object::KAutomaticUri );
       
   853 	CleanupStack::PushL( locationObject );
       
   854 
       
   855 	TTime timestamp( 0 );
       
   856 	timestamp.UniversalTime();
       
   857 
       
   858 	TTimeIntervalSeconds timeOffset = User::UTCOffset();
       
   859 	TTime localTime = timestamp + timeOffset;
       
   860 	
       
   861 	// required object properties
       
   862 	locationObject->AddTimePropertyL( creationDef, localTime );
       
   863 	locationObject->AddTimePropertyL( modifiedDef, timestamp );
       
   864 	locationObject->AddUint32PropertyL( sizeDef, 0 ); // always zero size for location objects
       
   865 	locationObject->AddTextPropertyL( itemTypeDef, Location::KLocationItemType );
       
   866 	locationObject->AddInt16PropertyL( offSetDef, timeOffset.Int() / 60 );
       
   867 	
       
   868 	LOG1( "CLocationRecord::DoCreateLocationL - location created with stamp: %Ld", timestamp.Int64() );
       
   869 	
       
   870 	// location related properties
       
   871 	if ( !Math::IsNaN( aLocationData.iPosition.Latitude() ) && 
       
   872 		 !Math::IsNaN( aLocationData.iPosition.Longitude() ))
       
   873 		{
       
   874 		locationObject->AddReal64PropertyL( latitudeDef, aLocationData.iPosition.Latitude() );
       
   875 		locationObject->AddReal64PropertyL( longitudeDef, aLocationData.iPosition.Longitude() );
       
   876 		}
       
   877 	if ( !Math::IsNaN( aLocationData.iPosition.Altitude() ) )
       
   878 		{
       
   879 		locationObject->AddReal64PropertyL( altitudeDef, aLocationData.iPosition.Altitude() );
       
   880 		}
       
   881 	if ( !Math::IsNaN( aLocationData.iCourse.Course() ) )
       
   882 		{
       
   883 		locationObject->AddReal32PropertyL( directionDef, aLocationData.iCourse.Course() );
       
   884 		}
       
   885 	if ( !Math::IsNaN( aLocationData.iCourse.Speed() ) )
       
   886 		{
       
   887 		locationObject->AddReal32PropertyL( speedDef, aLocationData.iCourse.Speed() );
       
   888 		}
       
   889 	if ( !Math::IsNaN( aLocationData.iQuality ) )
       
   890 		{
       
   891 		locationObject->AddReal32PropertyL( qualityDef, aLocationData.iQuality );
       
   892 		}
       
   893 
       
   894 	// network related properties
       
   895 	if ( aLocationData.iNetworkInfo.iAreaKnown )
       
   896 		{
       
   897 		if ( aLocationData.iNetworkInfo.iAccess != CTelephony::ENetworkAccessUnknown )
       
   898 			{
       
   899 			locationObject->AddUint32PropertyL( cellIdDef, aLocationData.iNetworkInfo.iCellId );
       
   900 			
       
   901 			}
       
   902 		if ( aLocationData.iNetworkInfo.iLocationAreaCode != 0 &&
       
   903 			aLocationData.iNetworkInfo.iAccess != CTelephony::ENetworkAccessUnknown )
       
   904 			{
       
   905 			locationObject->AddUint32PropertyL( locationCodeDef, 
       
   906 					aLocationData.iNetworkInfo.iLocationAreaCode );
       
   907 			
       
   908 			}
       
   909 		if ( aLocationData.iNetworkInfo.iCountryCode.Length() > 0 )
       
   910 			{
       
   911 			locationObject->AddTextPropertyL( countryCodeDef, 
       
   912 					aLocationData.iNetworkInfo.iCountryCode );
       
   913 			
       
   914 			}
       
   915 		if ( aLocationData.iNetworkInfo.iNetworkId.Length() > 0 )
       
   916 			{
       
   917 			locationObject->AddTextPropertyL(networkCodeDef, aLocationData.iNetworkInfo.iNetworkId);
       
   918 			
       
   919 			}
       
   920 		}
       
   921 
       
   922 	// Add the location object to the database.
       
   923 	locationObjectId = iMdeSession->AddObjectL( *locationObject );
       
   924 
       
   925 	iLastLocationId = locationObjectId;
       
   926 	iLastLocation = aLocationData;
       
   927 
       
   928 	CleanupStack::PopAndDestroy( locationObject );
       
   929 	
       
   930 	LOG("CLocationRecord::DoCreateLocationL - end");
       
   931 	
       
   932 	return locationObjectId;
       
   933 	}
       
   934 
       
   935 
       
   936 TItemId CLocationRecord::CreateRelationL( const TUint& aObjectId, const TUint& aLocationId )
       
   937 	{ 
       
   938 	LOG("CLocationRecord::CreateRelationL - start");
       
   939 	CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
       
   940 	
       
   941 	// "contains" relation definition
       
   942 	CMdERelationDef& containsRelDef = namespaceDef.GetRelationDefL( 
       
   943 			Relations::KContainsLocation );
       
   944 
       
   945 	CMdERelation* relationObject = iMdeSession->NewRelationLC( containsRelDef,
       
   946 			aObjectId, aLocationId, 0 );
       
   947 	if ( !relationObject )
       
   948 		{
       
   949 		User::Leave( KErrBadHandle );
       
   950 		}
       
   951 	TItemId relationId = iMdeSession->AddRelationL( *relationObject );
       
   952 
       
   953 	CleanupStack::PopAndDestroy( relationObject );
       
   954 	LOG("CLocationRecord::CreateRelationL - end");
       
   955 	
       
   956 	return relationId; 
       
   957 	}
       
   958 
       
   959 // --------------------------------------------------------------------------
       
   960 // CLocationManagerServer::ReadCenRepValueL
       
   961 // --------------------------------------------------------------------------
       
   962 //
       
   963 void CLocationRecord::ReadCenRepValueL(TInt aKey, TInt& aValue)
       
   964 	{
       
   965 	LOG( "CLocationRecord::::ReadCenRepValueL(), begin" );
       
   966 	CRepository* repository;
       
   967 	repository = CRepository::NewLC( KRepositoryUid );
       
   968 	User::LeaveIfError(repository->Get( aKey, aValue));
       
   969 	CleanupStack::PopAndDestroy(repository);
       
   970     LOG( "CLocationRecord::::ReadCenRepValueL(), end" );   
       
   971 	}
       
   972 
       
   973 void CLocationRecord::HandleQueryNewResults(CMdEQuery& /*aQuery*/, TInt /*aFirstNewItemIndex*/, 
       
   974 		TInt /*aNewItemCount*/)
       
   975 	{
       
   976 	}
       
   977 
       
   978 void CLocationRecord::HandleQueryCompleted(CMdEQuery& aQuery, TInt aError)
       
   979     {
       
   980     LOG("CLocationRecord::HandleQueryCompleted - start");
       
   981     const TInt count = aQuery.Count();
       
   982     LOG1("CLocationRecord::HandleQueryCompleted count: %d", count);
       
   983 
       
   984     CMdENamespaceDef* namespaceDef = NULL;
       
   985 
       
   986     TRAP_IGNORE( namespaceDef = &iMdeSession->GetDefaultNamespaceDefL() );
       
   987     if ( namespaceDef )
       
   988         {
       
   989         CMdEObjectDef* locationObjectDef = NULL;
       
   990 
       
   991         TRAP_IGNORE( locationObjectDef = &namespaceDef->GetObjectDefL( Location::KLocationObject ) );
       
   992         if ( locationObjectDef )
       
   993         	{
       
   994         	CMdEPropertyDef* latitudeDef = NULL;
       
   995         	CMdEPropertyDef* longitudeDef = NULL;
       
   996         	CMdEPropertyDef* altitudeDef = NULL;
       
   997         	
       
   998             TRAP_IGNORE( 
       
   999             		latitudeDef = &locationObjectDef->GetPropertyDefL(
       
  1000             				Location::KLatitudeProperty );
       
  1001             		longitudeDef = &locationObjectDef->GetPropertyDefL(	
       
  1002             				Location::KLongitudeProperty );
       
  1003             		altitudeDef = &locationObjectDef->GetPropertyDefL( 
       
  1004             				Location::KAltitudeProperty );
       
  1005             		);
       
  1006 
       
  1007             if( latitudeDef && longitudeDef && altitudeDef )
       
  1008             	{
       
  1009 	            TBool created = EFalse;
       
  1010 	            for ( TInt i = 0; i < count; i++ )
       
  1011 	                {
       
  1012 	                LOG1("CLocationRecord::HandleQueryCompleted check item: %d", i);
       
  1013 	                CMdEItem& item = aQuery.ResultItem(i);
       
  1014 	                CMdEObject& locationObject = static_cast<CMdEObject&>(item);
       
  1015 	
       
  1016 	                CMdEProperty* latProp = NULL;
       
  1017 	                CMdEProperty* lonProp = NULL; 
       
  1018 	                CMdEProperty* altProp = NULL;
       
  1019 	
       
  1020 	                locationObject.Property( *latitudeDef, latProp, 0 );
       
  1021 	                locationObject.Property( *longitudeDef, lonProp, 0 );
       
  1022 	                locationObject.Property( *altitudeDef, altProp, 0 );
       
  1023 	
       
  1024 	                if ( latProp && lonProp )
       
  1025 	                    {
       
  1026 	                    TReal32 distance;
       
  1027 	                    TCoordinate newCoords;
       
  1028 	                    if ( altProp )
       
  1029 	                        {
       
  1030 	                        TRAP_IGNORE( newCoords = TCoordinate( latProp->Real64ValueL(), lonProp->Real64ValueL(), (TReal32)altProp->Real64ValueL() ) );
       
  1031 	                        }
       
  1032 	                    else
       
  1033 	                        {
       
  1034 	                        TRAP_IGNORE( newCoords = TCoordinate( latProp->Real64ValueL(), lonProp->Real64ValueL() ) );
       
  1035 	                        }
       
  1036 	                    
       
  1037 	                    const TInt err = iLocationData.iPosition.Distance(newCoords, distance);
       
  1038 	                    
       
  1039 	                    if ( distance < iLocationDelta )
       
  1040 	                        {
       
  1041 	                        LOG("CLocationRecord::HandleQueryCompleted - match found in db");
       
  1042 	                        TRAPD( err, CreateRelationL( iObjectId, locationObject.Id() ) );
       
  1043 	                        if( err == KErrNone)
       
  1044 	                            {
       
  1045 	                            created = ETrue;
       
  1046 	                            i = count;
       
  1047 	                            }
       
  1048 	                        else
       
  1049 	                            {
       
  1050 	                            aError = err;
       
  1051 	                            }
       
  1052 	                        }
       
  1053 	                    }
       
  1054 	                }
       
  1055 
       
  1056 	            if ( !created && aError == KErrNone )
       
  1057 	                {
       
  1058 	                LOG("CLocationRecord::HandleQueryCompleted - no match found in db, create new");
       
  1059 	                TInt locationId( 0 );
       
  1060 	                TRAPD( err, locationId = DoCreateLocationL( iLocationData ) );
       
  1061 	                LOG1("CLocationRecord::HandleQueryCompleted - DoCreateLocationL err: %d", err);
       
  1062 	                if( err == KErrNone )
       
  1063 	                    {
       
  1064 	                    TRAP( err, CreateRelationL( iObjectId, locationId ));
       
  1065 	                    LOG1("CLocationRecord::HandleQueryCompleted - CreateRelationL err: %d", err);
       
  1066 	                    }
       
  1067 	                }
       
  1068             	}
       
  1069             }
       
  1070         }
       
  1071 
       
  1072     LOG("CLocationRecord::HandleQueryCompleted - end");
       
  1073     }
       
  1074 
       
  1075 EXPORT_C void CLocationRecord::SetMdeSession( CMdESession* aSession )
       
  1076 	{
       
  1077 	iMdeSession = aSession;
       
  1078 	TRAPD(err, iRemapper->InitialiseL( aSession ));
       
  1079 	if( err != KErrNone )
       
  1080 		{
       
  1081 		delete iRemapper;
       
  1082 		iRemapper = NULL;
       
  1083 		}
       
  1084 	}
       
  1085 
       
  1086 void CLocationRecord::StartTimerL()
       
  1087 	{
       
  1088 	LOG("CLocationRecord::StartTimerL");
       
  1089 	
       
  1090 	if( !iNetworkInfoTimer->IsActive() )
       
  1091 	    {
       
  1092 	    iNetworkInfoTimer->Start( iInterval, iInterval, TCallBack( UpdateNetworkInfo, this ) );
       
  1093 	    }
       
  1094 	}
       
  1095 
       
  1096 TTime CLocationRecord::GetMdeObjectTimeL( TItemId aObjectId ) 
       
  1097     {
       
  1098     CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
       
  1099 
       
  1100     CMdEObjectDef& objectDef = namespaceDef.GetObjectDefL( Object::KBaseObject );
       
  1101     CMdEPropertyDef& timeDef = objectDef.GetPropertyDefL( Object::KLastModifiedDateProperty );
       
  1102 
       
  1103     CMdEObject* object = NULL;
       
  1104     CMdEProperty* property = NULL;
       
  1105     
       
  1106     object = iMdeSession->GetObjectL( aObjectId );
       
  1107     object->Property( timeDef, property, 0 );
       
  1108     if ( !property )
       
  1109         {
       
  1110         User::Leave( KErrNotFound );
       
  1111         }
       
  1112     return property->TimeValueL();
       
  1113     }
       
  1114 
       
  1115 EXPORT_C TBool CLocationRecord::RemappingNeeded()
       
  1116 	{
       
  1117 	return iRemapper->ItemsInQueue();
       
  1118 	}
       
  1119 
       
  1120 // End of file