locationmanager/locationtrail/src/clocationrecord.cpp
branchRCL_3
changeset 19 b73252188534
parent 12 9f21bab39f42
child 20 f23c07ec56e2
equal deleted inserted replaced
18:63c982fb92f2 19:b73252188534
    16 */
    16 */
    17 
    17 
    18 #include <e32cmn.h> 
    18 #include <e32cmn.h> 
    19 #include <lbserrors.h>
    19 #include <lbserrors.h>
    20 #include <lbssatellite.h>
    20 #include <lbssatellite.h>
       
    21 #include <ecom.h>
       
    22 #include <centralrepository.h>
       
    23 #include <hwrmpowerstatesdkpskeys.h>
       
    24 #ifdef LOC_GEOTAGGING_CELLID
       
    25 #include <lbslocationinfo.h>
       
    26 #endif //LOC_GEOTAGGING_CELLID
    21 
    27 
    22 #include "rlocationtrail.h"
    28 #include "rlocationtrail.h"
    23 #include "clocationrecord.h"
    29 #include "clocationrecord.h"
    24 #include "cnetworkinfo.h"
    30 #include "cnetworkinfo.h"
    25 #include "locationmanagerdebug.h"
    31 #include "locationmanagerdebug.h"
    26 #include "locationtraildefs.h"
    32 #include "locationtraildefs.h"
    27 #include "locationtrailpskeys.h"
    33 #include "locationtrailpskeys.h"
    28 #include "mdeconstants.h"
    34 #include "mdeconstants.h"
    29 #include <centralrepository.h>
    35 #ifdef LOC_REVERSEGEOCODE
    30 #include <hwrmpowerstatesdkpskeys.h>
    36 #include "reversegeocoderplugin.h"
       
    37 const TUid KReverseGeoCodeUid = {0x2002DD12}; 
       
    38 #endif	
    31 
    39 
    32 
    40 
    33 using namespace MdeConstants;
    41 using namespace MdeConstants;
    34 
    42 
       
    43 const TUint KNetQueryBit = 1; // 1 bit
       
    44 const TUint KDownloadMediaFile = 2; // 2  bit
       
    45 const TUint KSnapMediaFile = 4; // 3 bit
       
    46 const TUint KLocationQueryInProgress = 1; // 1  bit
       
    47 #ifdef LOC_REVERSEGEOCODE
       
    48 const TUint KReverseGeoCodingInProgress = 2; // 2 bit
       
    49 _LIT ( KCountry, "country:");   // country:india 
       
    50 _LIT ( KCity, "city:");         // city:bangalore|country:india
       
    51 #endif
       
    52 const TUint KSnapGeoConvertInProgress = 4; // 3 bit
       
    53 #ifdef LOC_GEOTAGGING_CELLID
       
    54 const TUint KSnapGeoConvertInPendingState = 8; // 4 bit
       
    55 #endif
       
    56 
       
    57 
       
    58 
    35 // --------------------------------------------------------------------------
    59 // --------------------------------------------------------------------------
    36 // CLocationRecord::NewL
    60 // CLocationRecord::NewL
    37 // --------------------------------------------------------------------------
    61 // --------------------------------------------------------------------------
    38 //
    62 //
    39 EXPORT_C CLocationRecord* CLocationRecord::NewL()
    63 EXPORT_C CLocationRecord* CLocationRecord::NewL(MGeoTaggerObserver& aGeoTaggerObserver,
    40     {
    64                 RMobilePhone& aPhone)
    41     CLocationRecord* self = new (ELeave) CLocationRecord();
    65     {
       
    66     CLocationRecord* self = new (ELeave) CLocationRecord(aGeoTaggerObserver, aPhone);
    42     CleanupStack::PushL( self );
    67     CleanupStack::PushL( self );
    43     self->ConstructL();
    68     self->ConstructL();
    44     CleanupStack::Pop( self );
    69     CleanupStack::Pop( self );
    45     return self;
    70     return self;
    46     }
    71     }
    47         
    72 
    48 // --------------------------------------------------------------------------
    73 // --------------------------------------------------------------------------
    49 // CLocationRecord::CLocationRecord
    74 // CLocationRecord::CLocationRecord
    50 // --------------------------------------------------------------------------
    75 // --------------------------------------------------------------------------
    51 //  
    76 //  
    52 CLocationRecord::CLocationRecord()
    77 CLocationRecord::CLocationRecord(MGeoTaggerObserver& aGeoTaggerObserver,
       
    78                 RMobilePhone& aPhone)
    53     : iNetworkInfoTimer( NULL ),
    79     : iNetworkInfoTimer( NULL ),
    54     iState( RLocationTrail::ETrailStopped ),
    80     iState( RLocationTrail::ETrailStopped ),
    55     iTrailCaptureSetting( RLocationTrail::ECaptureAll ),
    81     iTrailCaptureSetting( RLocationTrail::ECaptureAll ),
    56     iLocationCounter( 0 ),
    82     iLocationCounter( 0 ),
    57     iRequestCurrentLoc( EFalse ),
    83     iRequestCurrentLoc( EFalse ),
    58     iTrailStarted( EFalse ),
    84     iTrailStarted( EFalse ),
    59     iLastGPSFixState( EFalse ),
    85     iLastGPSFixState( EFalse ),
    60     iLastLocationId( 0 )
    86 	iLocationQuery(NULL),
       
    87 	iNetLocationQuery(NULL),
       
    88     iRemapState( ERemapProgressNone),
       
    89 	iNamespaceDef(NULL),
       
    90 	iLocationObjectDef(NULL),
       
    91 	iLatitudeDef(NULL),
       
    92 	iLongitudeDef(NULL),
       
    93 	iAltitudeDef(NULL),
       
    94     iMediaHandlingFlag(0),
       
    95     iPhone(aPhone),
       
    96     iGpsDataAvailableFlag(EFalse),
       
    97     iGeoTaggerObserver(aGeoTaggerObserver)
       
    98 #ifdef LOC_GEOTAGGING_CELLID	
       
    99     ,iGeoConverter(NULL)
       
   100     , iConvertRetry(ETrue)
       
   101 #endif
       
   102 #ifdef LOC_REVERSEGEOCODE
       
   103     ,iConnectionOption(ESilent)
       
   104 	,iImageQuery(NULL)
       
   105 	,iTagQuery(NULL)
       
   106     ,iTagCreator( NULL )
       
   107     ,iLastReverseGeocodeFails(EFalse)
       
   108 	,iRevGeocoderPlugin( NULL )
       
   109 #endif
    61     {
   110     {
    62     iMaxTrailSize = KMaxTrailLength / KUpdateInterval;
   111     iMaxTrailSize = KMaxTrailLength / KUpdateInterval;
    63     }
   112     }
    64 
   113 
    65 // --------------------------------------------------------------------------
   114 // --------------------------------------------------------------------------
    66 // CLocationRecord::ConstructL
   115 // CLocationRecord::ConstructL
    67 // --------------------------------------------------------------------------
   116 // --------------------------------------------------------------------------
    68 //    
   117 //    
    69 void CLocationRecord::ConstructL()
   118 void CLocationRecord::ConstructL()
    70     {
   119     {
       
   120     LOG( "CLocationRecord::ConstructL(), begin" );
    71     const TInt KMillion = 1000000;
   121     const TInt KMillion = 1000000;
    72     TInt err = iProperty.Define( KPSUidLocationTrail, KLocationTrailState, RProperty::EInt );
   122     TInt err = iProperty.Define( KPSUidLocationTrail, KLocationTrailState, RProperty::EInt );
    73     if ( err != KErrNone && err != KErrAlreadyExists )
   123     if ( err != KErrNone && err != KErrAlreadyExists )
    74         {
   124         {
    75         User::Leave( err );
   125         User::Leave( err );
    76         }
   126         }
    77     User::LeaveIfError( iProperty.Set( KPSUidLocationTrail,
   127     User::LeaveIfError( iProperty.Set( KPSUidLocationTrail,
    78         KLocationTrailState, (TInt) RLocationTrail::ETrailStopped ) ); 
   128         KLocationTrailState, (TInt) RLocationTrail::ETrailStopped ) ); 
    79 
   129 
    80     iNetworkInfo = CNetworkInfo::NewL( this );
   130     
       
   131     iNetworkInfoChangeListener = CNetworkInfo::NewL( this );
    81     iPositionInfo = CPositionInfo::NewL( this );
   132     iPositionInfo = CPositionInfo::NewL( this );
    82 	iRemapper = CLocationRemappingAO::NewL();
   133 	iRemapper = CLocationRemappingAO::NewL();
    83     iNetworkInfoTimer = CPeriodic::NewL( CActive::EPriorityStandard );
   134     iNetworkInfoTimer = CPeriodic::NewL( CActive::EPriorityStandard );
    84     
   135 
       
   136 #ifdef LOC_REVERSEGEOCODE
       
   137     iTagCreator = CTagCreator::NewL();
       
   138 
       
   139     iRevGeocoderPlugin = reinterpret_cast<CReverseGeoCoderPlugin*>(
       
   140           REComSession::CreateImplementationL(KReverseGeoCodeUid,iDtorKey));
       
   141      if( iRevGeocoderPlugin )
       
   142 	 	{
       
   143      	iRevGeocoderPlugin->AddObserverL(*this);
       
   144 	 	}
       
   145 
       
   146 
       
   147  
       
   148  #endif
       
   149 
       
   150 	
    85     TInt interval( 0 );
   151     TInt interval( 0 );
    86     TRAP(err, ReadCenRepValueL(KIntervalKey, interval));
   152     TRAP(err, ReadCenRepValueL(KIntervalKey, interval));
    87     LOG1("CLocationManagerServer::ConstructL, cenrep interval value:%d", interval);
   153     LOG1("Cenrep interval value:%d", interval);
    88     
   154     
    89     if (interval == 0 || err != KErrNone )
   155     if (interval == 0 || err != KErrNone )
    90     	{
   156     	{
    91         LOG1("CLocationManagerServer::ConstructL, cenrep interval err:%d", err);
   157         LOG1("Cenrep interval err:%d", err);
    92     	iInterval = KUpdateInterval;
   158     	iInterval = KUpdateInterval;
    93     	}
   159     	}
    94     else 
   160     else 
    95     	{
   161     	{
    96     	iInterval = interval * KMillion;
   162     	iInterval = interval * KMillion;
    97     	}
   163     	}
    98 
   164 
    99     TRAP(err, ReadCenRepValueL(KLocationDeltaKey, iLocationDelta));
   165     TRAP(err, ReadCenRepValueL(KLocationDeltaKey, iLocationDelta));
   100     LOG1("CLocationManagerServer::ConstructL, location delta value:%d", iLocationDelta);
   166     LOG1("Location delta value:%d", iLocationDelta);
   101     
   167     
   102     if (iLocationDelta == 0)
   168     if (iLocationDelta == 0)
   103     	{
   169     	{
   104         LOG1("CLocationManagerServer::ConstructL, location delta err:%d", err);
   170         LOG1("Location delta err:%d", err);
   105         iLocationDelta = KLocationDelta;
   171         iLocationDelta = KLocationDelta;
   106     	}
   172     	}
   107 
   173     iLastMediaItem.iFlag = 0;
       
   174     iLastMediaItem.iLocationId = 0;
       
   175 #ifdef LOC_REVERSEGEOCODE
       
   176     iLastMediaItem.iCityTagId= 0;
       
   177     iLastMediaItem.iCountryTagId = 0;
       
   178 
       
   179     iLastLocationItem.iFlag = 0;
       
   180     iLastLocationItem.iCityTagId= 0;
       
   181     iLastLocationItem.iCountryTagId = 0;
       
   182     iLastLocationItem.iLocationId = 0;
       
   183 #endif
       
   184     LOG( "CLocationRecord::ConstructL(), end" );    
   108     }
   185     }
   109     
   186     
   110 // --------------------------------------------------------------------------
   187 // --------------------------------------------------------------------------
   111 // CLocationRecord::~CLocationRecord
   188 // CLocationRecord::~CLocationRecord
   112 // --------------------------------------------------------------------------
   189 // --------------------------------------------------------------------------
   113 //    
   190 //    
   114 EXPORT_C CLocationRecord::~CLocationRecord()
   191 EXPORT_C CLocationRecord::~CLocationRecord()
   115     {
   192     {
       
   193     LOG( "CLocationRecord::~CLocationRecord(), begin" );    
   116     Stop();
   194     Stop();
   117     iProperty.Delete( KPSUidLocationTrail, KLocationTrailState );
   195     iProperty.Delete( KPSUidLocationTrail, KLocationTrailState );
   118     iProperty.Close();
   196     iProperty.Close();
       
   197     iTrail.ResetAndDestroy();
   119     iTrail.Close();
   198     iTrail.Close();
   120     
   199     iMediaItems.ResetAndDestroy();
   121     delete iNetworkInfo;
   200     iMediaItems.Close();
       
   201 #ifdef LOC_REVERSEGEOCODE
       
   202     iLocationItems.ResetAndDestroy();
       
   203     iLocationItems.Close();
       
   204 #endif    
       
   205     delete iNetworkInfoChangeListener;
       
   206     iNetworkInfoChangeListener = NULL;
   122     delete iPositionInfo;
   207     delete iPositionInfo;
       
   208     iPositionInfo = NULL;
   123     delete iNetworkInfoTimer;
   209     delete iNetworkInfoTimer;
   124 	if (iRemapper)
   210     iNetworkInfoTimer = NULL;
       
   211 #ifdef LOC_GEOTAGGING_CELLID
       
   212     if(iGeoConverter)
       
   213         {
       
   214         delete iGeoConverter;
       
   215         iGeoConverter = NULL;
       
   216         }
       
   217 #endif    
       
   218     if (iRemapper)
   125 		{
   219 		{
   126 		iRemapper->StopRemapping();
   220 		iRemapper->StopRemapping();
   127 		delete iRemapper;
   221 		delete iRemapper;
       
   222 		iRemapper = NULL;
   128 		}
   223 		}
       
   224     if(iLocationQuery)
       
   225 		{
       
   226 		iLocationQuery->RemoveObserver(*this);
       
   227 		iLocationQuery->Cancel();
       
   228 		delete iLocationQuery;
       
   229 		iLocationQuery = NULL;
       
   230 		}
       
   231 
       
   232 if(iNetLocationQuery)
       
   233     {
       
   234     iNetLocationQuery->RemoveObserver(*this);
       
   235     iNetLocationQuery->Cancel();
       
   236     delete iNetLocationQuery;
       
   237     iNetLocationQuery = NULL;
       
   238     }
       
   239 
       
   240 
       
   241 #ifdef LOC_REVERSEGEOCODE
       
   242     if(iImageQuery)
       
   243 		{
       
   244 		iImageQuery->RemoveObserver(*this);
       
   245 		iImageQuery->Cancel();
       
   246 		delete iImageQuery;
       
   247         iImageQuery = NULL;
       
   248 		}
       
   249     if(iTagQuery)
       
   250 		{
       
   251 	    iTagQuery->RemoveObserver(*this);
       
   252 		iTagQuery->Cancel();
       
   253 		delete iTagQuery;
       
   254         iTagQuery = NULL;
       
   255 		}	
       
   256 	delete iTagCreator;
       
   257 	// set the pointer to NULL, ECOM will destroy object.
       
   258     delete iRevGeocoderPlugin;
       
   259     iRevGeocoderPlugin = NULL;
       
   260     REComSession::DestroyedImplementation(iDtorKey);
       
   261 #endif
       
   262     LOG( "CLocationRecord::~CLocationRecord(), end" );	
   129     }
   263     }
   130 
   264 
   131 // --------------------------------------------------------------------------
   265 // --------------------------------------------------------------------------
   132 // CLocationRecord::CurrentState
   266 // CLocationRecord::CurrentState
   133 // --------------------------------------------------------------------------
   267 // --------------------------------------------------------------------------
   143 //
   277 //
   144 EXPORT_C void CLocationRecord::StartL( RLocationTrail::TTrailCaptureSetting aCaptureSetting )
   278 EXPORT_C void CLocationRecord::StartL( RLocationTrail::TTrailCaptureSetting aCaptureSetting )
   145     {
   279     {
   146     LOG( "CLocationRecord::StartL(), begin" );
   280     LOG( "CLocationRecord::StartL(), begin" );
   147     iTrailCaptureSetting = aCaptureSetting;
   281     iTrailCaptureSetting = aCaptureSetting;
       
   282     iGpsDataAvailableFlag = EFalse;
   148     if ( aCaptureSetting == RLocationTrail::ECaptureAll && !iPositionInfo->IsActive() )
   283     if ( aCaptureSetting == RLocationTrail::ECaptureAll && !iPositionInfo->IsActive() )
   149         {
   284         {
   150         if( iState == RLocationTrail::ETrailStopped  )
   285         if( iState == RLocationTrail::ETrailStopped  )
   151             {
   286             {
   152             iTrail.Reset();
   287             iTrail.ResetAndDestroy();
   153             }
   288             }
       
   289     	// Capture at least one location with n/w info else we wouldn't be able to tag the immediate taken photograph
       
   290     	// incase LBS didn't provide any valid position data. Without this there may be a leave from GetLocationByTimeL 
       
   291     	// method while calling this from LocationSnapshotL method
       
   292     	UpdateNetworkInfo( this );
   154         iPositionInfo->StartL( aCaptureSetting, iInterval );
   293         iPositionInfo->StartL( aCaptureSetting, iInterval );
   155         }
   294         }
   156     else if ( aCaptureSetting == RLocationTrail::ECaptureNetworkInfo )
   295     else if ( aCaptureSetting == RLocationTrail::ECaptureNetworkInfo )
   157     	{
   296     	{
   158     	// Update and store network info in location trail immediately.
   297     	// Update and store network info in location trail immediately.
   165         	}
   304         	}
   166         	
   305         	
   167         StartTimerL();
   306         StartTimerL();
   168     	}
   307     	}
   169     
   308     
   170     iLastLocationId = 0;
   309     iTrailStarted = ETrue;
   171     
       
   172     SetCurrentState( RLocationTrail::ETrailStarting );
   310     SetCurrentState( RLocationTrail::ETrailStarting );
   173     
   311     
   174     iTrailStarted = ETrue;
       
   175     LOG( "CLocationRecord::StartL(), end" );
   312     LOG( "CLocationRecord::StartL(), end" );
   176     }
   313     }
   177 
   314 
   178 // --------------------------------------------------------------------------
   315 // --------------------------------------------------------------------------
   179 // CLocationRecord::Stop
   316 // CLocationRecord::Stop
   181 //
   318 //
   182 EXPORT_C void CLocationRecord::Stop()
   319 EXPORT_C void CLocationRecord::Stop()
   183     {
   320     {
   184     LOG( "CLocationRecord::StopL(), begin" );
   321     LOG( "CLocationRecord::StopL(), begin" );
   185     iPositionInfo->Stop();
   322     iPositionInfo->Stop();
       
   323     iGpsDataAvailableFlag = EFalse;
   186     iTrailStarted = EFalse;
   324     iTrailStarted = EFalse;
   187     
       
   188     if ( iNetworkInfoTimer && iNetworkInfoTimer->IsActive() )
   325     if ( iNetworkInfoTimer && iNetworkInfoTimer->IsActive() )
   189     	{
   326     	{
   190     	iNetworkInfoTimer->Cancel();
   327     	iNetworkInfoTimer->Cancel();
   191     	}
   328     	}
   192 
   329 
   229 
   366 
   230     TTimeIntervalSeconds interval;
   367     TTimeIntervalSeconds interval;
   231     TTimeIntervalSeconds nextInterval;
   368     TTimeIntervalSeconds nextInterval;
   232     for ( TInt i(iTrail.Count()-1) ; i >= 0 && !posFound ; i-- )
   369     for ( TInt i(iTrail.Count()-1) ; i >= 0 && !posFound ; i-- )
   233         {
   370         {
   234         TInt err = iTrail[i].iTimeStamp.SecondsFrom( aTime, interval );
   371         TInt err = iTrail[i]->iTimeStamp.SecondsFrom( aTime, interval );
   235         
   372         
   236         TInt timeDiff = Abs( interval.Int() );
   373         TInt timeDiff = Abs( interval.Int() );
   237 
   374 
   238 #ifdef _DEBUG
   375 #ifdef _DEBUG
   239         LOG1( "CLocationRecord::GetLocationByTimeL - Trail timestamp: %Ld", iTrail[i].iTimeStamp.Int64() );
   376         LOG1( "CLocationRecord::GetLocationByTimeL - Trail timestamp: %Ld", iTrail[i]->iTimeStamp.Int64() );
   240         TBuf<DateTimeStrMaxLength> str;
   377         TBuf<DateTimeStrMaxLength> str;
   241         iTrail[i].iTimeStamp.FormatL( str, KDateTimeFormat );
   378         iTrail[i]->iTimeStamp.FormatL( str, KDateTimeFormat );
   242         LOG1( "CLocationRecord::GetLocationByTimeL - Trail timestamp: %S", &str );
   379         LOG1( "CLocationRecord::GetLocationByTimeL - Trail timestamp: %S", &str );
   243         LOG1( "CLocationRecord::GetLocationByTimeL - timeDiff: %d", timeDiff );
   380         LOG1( "CLocationRecord::GetLocationByTimeL - timeDiff: %d", timeDiff );
   244 #endif
   381 #endif
   245 
   382 
   246         if ( err == KErrNone && timeDiff <= KMaximumIntervalSeconds )
   383         if ( err == KErrNone && timeDiff <= KMaximumIntervalSeconds )
   247             {
   384             {
   248             // The nearest time is in iTrail[i] or in iTrail[i-1].
   385             // The nearest time is in iTrail[i] or in iTrail[i-1].
   249             if ( i > 0 )
   386             if ( i > 0 )
   250                 {
   387                 {
   251                 iTrail[i-1].iTimeStamp.SecondsFrom( aTime, nextInterval );
   388                 iTrail[i-1]->iTimeStamp.SecondsFrom( aTime, nextInterval );
   252                 
   389                 
   253                 TInt nextDiff = Abs( nextInterval.Int() );
   390                 TInt nextDiff = Abs( nextInterval.Int() );
   254                     
   391                     
   255                 if ( nextDiff < timeDiff )
   392                 if ( nextDiff < timeDiff )
   256                     {
   393                     {
   257                     aLocationData = iTrail[i-1].iLocationData;
   394                     aLocationData = iTrail[i-1]->iLocationData;
   258                     aState = iTrail[i-1].iTrailState;
   395                     aState = iTrail[i-1]->iTrailState;
   259                     }
   396                     }
   260                 else
   397                 else
   261                     {
   398                     {
   262                     aLocationData = iTrail[i].iLocationData;
   399                     aLocationData = iTrail[i]->iLocationData;
   263                     aState = iTrail[i].iTrailState;
   400                     aState = iTrail[i]->iTrailState;
   264                     }                    
   401                     }                    
   265                 }            
   402                 }            
   266             else
   403             else
   267                 {
   404                 {
   268                 aLocationData = iTrail[i].iLocationData;
   405                 aLocationData = iTrail[i]->iLocationData;
   269                 aState = iTrail[i].iTrailState;
   406                 aState = iTrail[i]->iTrailState;
   270                 }
   407                 }
   271             posFound = ETrue;
   408             posFound = ETrue;
   272             }
   409             }
   273         }
   410         }
   274     if ( !posFound )
   411     if ( !posFound )
   282 // CLocationRecord::RequestLocationL
   419 // CLocationRecord::RequestLocationL
   283 // --------------------------------------------------------------------------
   420 // --------------------------------------------------------------------------
   284 //
   421 //
   285 EXPORT_C void CLocationRecord::RequestLocationL()
   422 EXPORT_C void CLocationRecord::RequestLocationL()
   286     {
   423     {
       
   424     LOG( "CLocationRecord::RequestLocationL(), begin" );    
   287     iRequestCurrentLoc = ETrue;
   425     iRequestCurrentLoc = ETrue;
   288     if ( iTrailCaptureSetting != RLocationTrail::ECaptureNetworkInfo &&
   426     if ( iTrailCaptureSetting != RLocationTrail::ECaptureNetworkInfo &&
   289     	!iPositionInfo->IsActive() )
   427     	!iPositionInfo->IsActive() )
   290         {
   428         {
   291         iPositionInfo->StartL( iTrailCaptureSetting, iInterval );
   429         iPositionInfo->StartL( iTrailCaptureSetting, iInterval );
       
   430         SetCurrentState( RLocationTrail::ETrailStarting );
       
   431         
       
   432         iTrailStarted = ETrue;
   292         }
   433         }
   293     else if ( iTrailCaptureSetting == RLocationTrail::ECaptureNetworkInfo )
   434     else if ( iTrailCaptureSetting == RLocationTrail::ECaptureNetworkInfo )
   294     	{
   435     	{
   295     	TPositionSatelliteInfo posInfo;
   436     	TPositionSatelliteInfo posInfo;
   296     	CTelephony::TNetworkInfoV1 network = CTelephony::TNetworkInfoV1();
   437     	CTelephony::TNetworkInfoV1 network = CTelephony::TNetworkInfoV1();
   297     	GetNetworkInfo( network );
   438     	GetNetworkInfo( network );
   298        	iObserver->CurrentLocation( posInfo, network, KErrNone );
   439        	iObserver->CurrentLocation( posInfo, network, KErrNone );
   299         iRequestCurrentLoc = EFalse;
   440         iRequestCurrentLoc = EFalse;
   300     	}
   441     	}
       
   442     LOG( "CLocationRecord::RequestLocationL(), end" );	
   301     }
   443     }
   302 
   444 
   303 // --------------------------------------------------------------------------
   445 // --------------------------------------------------------------------------
   304 // CLocationRecord::CancelLocationRequest
   446 // CLocationRecord::CancelLocationRequest
   305 // --------------------------------------------------------------------------
   447 // --------------------------------------------------------------------------
   306 //    
   448 //    
   307 EXPORT_C void CLocationRecord::CancelLocationRequest()
   449 EXPORT_C void CLocationRecord::CancelLocationRequest()
   308     {
   450     {
       
   451     LOG( "CLocationRecord::CancelLocationRequest(), begin" );    
   309     iRequestCurrentLoc = EFalse;
   452     iRequestCurrentLoc = EFalse;
   310     if ( !iTrailStarted )
   453     if ( !iTrailStarted )
   311         {
   454         {
       
   455         iGpsDataAvailableFlag = EFalse;
   312         iPositionInfo->Stop();
   456         iPositionInfo->Stop();
   313         }
   457         }
       
   458     LOG( "CLocationRecord::CancelLocationRequest(), end" );	
   314     }
   459     }
   315         
   460         
   316         
   461         
   317 // --------------------------------------------------------------------------
   462 // --------------------------------------------------------------------------
   318 // CLocationRecord::GetNetworkInfo
   463 // CLocationRecord::GetNetworkInfo
   349 // --------------------------------------------------------------------------
   494 // --------------------------------------------------------------------------
   350 //    
   495 //    
   351 void CLocationRecord::Position( const TPositionInfo& aPositionInfo,
   496 void CLocationRecord::Position( const TPositionInfo& aPositionInfo,
   352                                 const TInt aError  )
   497                                 const TInt aError  )
   353     {    
   498     {    
       
   499     LOG( "CLocationRecord::Position(), begin" );    
   354     const TPositionSatelliteInfo& positionSatelliteInfo = 
   500     const TPositionSatelliteInfo& positionSatelliteInfo = 
   355     	static_cast<const TPositionSatelliteInfo&>(aPositionInfo);
   501     	static_cast<const TPositionSatelliteInfo&>(aPositionInfo);
   356 
   502 
   357     if ( iRequestCurrentLoc )
   503     if ( iRequestCurrentLoc )
   358         {
   504         {
   359         HandleLocationRequest( positionSatelliteInfo, aError );
   505         HandleLocationRequest( positionSatelliteInfo, aError );
   360         }
   506         }
   361     if( iState == RLocationTrail::ETrailStopped )
   507     
   362     	{
   508     iGpsDataAvailableFlag = EFalse;
   363     	LOG("CLocationRecord::Position - trail stopped");
   509     
   364     	return;
   510     if ( !iTrailStarted || iState == RLocationTrail::ETrailStopped)
   365     	}
   511         {
   366     
   512     	LOG("trail not started/stopped");
   367     if ( !iTrailStarted )
   513         iPositionInfo->Stop();
   368         {
       
   369         iPositionInfo->NextPosition();
       
   370         return;
   514         return;
       
   515         }
       
   516     // all cases store the location..
       
   517     StoreLocation( positionSatelliteInfo );
       
   518     
       
   519     if ( RemappingNeeded() )
       
   520         {
       
   521         //either network or gps signal is available
       
   522         if ( ( iNetwork.iCellId != 0 && 
       
   523                 iNetwork.iCountryCode.Length() != 0 &&
       
   524                 iNetwork.iNetworkId.Length() != 0 ) || ( aError == KErrNone ) )
       
   525             {
       
   526             //no error means gps info available
       
   527             TRAP_IGNORE( RemapObjectsL( aError == KErrNone ) );
       
   528             }
   371         }
   529         }
   372     switch ( aError )
   530     switch ( aError )
   373         {
   531         {
   374         case KPositionPartialUpdate: // fall through
   532         case KPositionPartialUpdate: // fall through
   375         case KPositionQualityLoss: 
   533         case KPositionQualityLoss: 
   376             {
   534             {
   377             // Location is stored, even if it may not be valid.
   535             // Location is stored, even if it may not be valid.
   378             StoreLocation( positionSatelliteInfo ); 
   536             LOG("Partial update");
   379             LOG("CLocationRecord::Position - partial update");
       
   380             if ( iState != RLocationTrail::EWaitingGPSData && 
   537             if ( iState != RLocationTrail::EWaitingGPSData && 
   381             	 iState != RLocationTrail::ETrailStopping ) 
   538             	 iState != RLocationTrail::ETrailStopping ) 
   382                 {
   539                 {
   383                 SetCurrentState( RLocationTrail::EWaitingGPSData );
   540                 SetCurrentState( RLocationTrail::EWaitingGPSData );
   384             	LOG("CLocationRecord::Position trail waiting for gps");
   541             	LOG("Trail waiting for gps");
   385                 }
   542                 }
   386             break;
   543             break;
   387             }
   544             }
   388         case KErrNone:
   545         case KErrNone:
   389             {
   546             {
   390             StoreLocation( positionSatelliteInfo );
   547             LOG("Good GPS coordinates");
   391             LOG("CLocationRecord::Position - good GPS coordinates");
   548             iGpsDataAvailableFlag = ETrue;
   392             if ( iState != RLocationTrail::ETrailStarted ) 
   549             if ( iState != RLocationTrail::ETrailStarted ) 
   393                 {
   550                 {
   394                 if ( iRemapper )
   551                 if ( iRemapper )
   395                 	{
   552                 	{
   396                 	LOG("CLocationRecord::Position start remapping");
   553                 	LOG("Start remapping");
   397                 	iLastLocationId = 0;
       
   398                 	TBool createLocation = iRemapper->CheckQueue();
       
   399                 	if( createLocation )
       
   400                 		{
       
   401                 		TRAP_IGNORE(	
       
   402                 		TItemId locationId = DoCreateLocationL( iNewItem.iLocationData );
       
   403                 		iRemapper->UpdateRelationsL( locationId );
       
   404                 		)
       
   405                 		}
       
   406                		iRemapper->StartRemappingObjects( iNewItem.iLocationData );
   554                		iRemapper->StartRemappingObjects( iNewItem.iLocationData );
   407 
   555 
   408                     if( iObserver->WaitForPositioningStopTimeout() && !RemappingNeeded() )                                
   556                     if( iObserver->WaitForPositioningStopTimeout() && !RemappingNeeded() )                                
   409                		    {                                                
   557                		    {                                                
   410                         iObserver->RemapedCompleted();
   558                         iObserver->RemapedCompleted();
       
   559                         iPositionInfo->HandleRemapComplete();
   411                         return;
   560                         return;
   412                		    }
   561                		    }
   413                		
   562                		
   414                 	}
   563                 	}
   415                 if ( iState != RLocationTrail::ETrailStopping )
   564                 if ( iState != RLocationTrail::ETrailStopping )
   416                 	{
   565                 	{
   417                     SetCurrentState( RLocationTrail::ETrailStarted );
   566                     SetCurrentState( RLocationTrail::ETrailStarted );
   418                 	LOG("CLocationRecord::Position trail started");
   567                 	LOG("Trail started");
   419                 	}
   568                 	}
   420                 }
   569                 }
   421             break;
   570             break;
   422             }
   571             }
   423         default:
   572         default:
   424             {
   573             {
   425             StoreLocation( positionSatelliteInfo );
   574             LOG1("Searching GPS, aError %d", aError );
   426             LOG1("CLocationRecord::Position - searching GPS, aError %d", aError );
       
   427             if ( iState != RLocationTrail::ESearchingGPS &&
   575             if ( iState != RLocationTrail::ESearchingGPS &&
   428                	 iState != RLocationTrail::ETrailStopping ) 
   576                	 iState != RLocationTrail::ETrailStopping ) 
   429                 {
   577                 {
   430                 SetCurrentState( RLocationTrail::ESearchingGPS );
   578                 SetCurrentState( RLocationTrail::ESearchingGPS );
   431             	LOG("CLocationRecord::Position trail searching gps");
   579             	LOG("Trail searching gps");
   432                 }
   580                 }
   433             break;
   581             break;
   434             }      
   582             }      
   435         }
   583         }
   436     TBool fixState = CheckGPSFix( positionSatelliteInfo );
   584     TBool fixState = CheckGPSFix( positionSatelliteInfo );
   437     LOG1( "CLocationRecord::Position fixState %d", fixState );
   585     LOG1( "fixState %d", fixState );
   438     LOG1( "CLocationRecord::Position iLastGPSFixState %d", iLastGPSFixState );
   586     LOG1( "iLastGPSFixState %d", iLastGPSFixState );
   439     
   587     
   440     if ( iObserver && iLastGPSFixState != fixState )
   588     if ( iObserver && iLastGPSFixState != fixState )
   441     	{
   589     	{
   442     	LOG("CLocationRecord::Position quality changed");
   590     	LOG("Quality changed");
   443     	iObserver->GPSSignalQualityChanged( positionSatelliteInfo );
   591     	iObserver->GPSSignalQualityChanged( positionSatelliteInfo );
   444     	}
   592     	}
   445     
   593     
   446    	iLastGPSFixState = fixState;
   594    	iLastGPSFixState = fixState;
   447     
   595     
   448     iPositionInfo->NextPosition();
   596     LOG( "CLocationRecord::Position(), end" );	
       
   597     }
       
   598 
       
   599 
       
   600 // --------------------------------------------------------------------------
       
   601 // CLocationRecord::RemapObjectsL
       
   602 // Remaps the location objects when GPS is available
       
   603 // --------------------------------------------------------------------------
       
   604 //
       
   605 void CLocationRecord::RemapObjectsL( TBool aGPSInfoAvailable )
       
   606     {
       
   607     LOG( "CLocationRecord::RemapObjectsL(), begin" );    
       
   608     TBool createLocation = EFalse;
       
   609 
       
   610     if (iRemapper)
       
   611         {
       
   612         createLocation = iRemapper->CheckQueue();
       
   613         }
       
   614 
       
   615     if ( !createLocation || iRemapper == NULL)
       
   616         {
       
   617         return;
       
   618         }
       
   619 
       
   620     
       
   621     if ( aGPSInfoAvailable )
       
   622         {       
       
   623         TItemId locationId = DoCreateLocationL( iNewItem.iLocationData );
       
   624         iRemapper->UpdateRelationsL( locationId ); 
       
   625 #ifdef LOC_REVERSEGEOCODE
       
   626         if(!(iMediaHandlingFlag & KReverseGeoCodingInProgress))
       
   627             {
       
   628             iRemapState = ERemapRevGeoCodeInProgress;
       
   629             //Find the address by coordinate, results a call to ReverseGeocodeComplete()
       
   630             iMediaHandlingFlag |= KReverseGeoCodingInProgress;
       
   631 
       
   632             if(iRevGeocoderPlugin)
       
   633             	{
       
   634             	iRevGeocoderPlugin->GetAddressByCoordinateL( iNewItem.iLocationData.iPosition, iConnectionOption ); 
       
   635             	}
       
   636             }
       
   637         else
       
   638             {
       
   639             // make this as pending state so that on rev geo code complete
       
   640             // it will take the priority over the next item within the location 
       
   641             // item in location queue.
       
   642 			iRemapState = ERemapRevGeoCodePending;
       
   643             }
       
   644 #endif //LOC_REVERSEGEOCODE
       
   645         }
       
   646     else
       
   647         {
       
   648         //call location m/w API to convert cell ID to lat, long
       
   649 #ifdef LOC_GEOTAGGING_CELLID
       
   650         if((iMediaHandlingFlag & KSnapGeoConvertInProgress) > 0)
       
   651             {
       
   652             iRemapState = ERemapNwGeoConverterPending;
       
   653             }
       
   654         else
       
   655             {
       
   656             // go for cell id based geo coding.
       
   657             iRemapState = ERemapNwGeoConverterInProgress;
       
   658             if(iGeoConverter == NULL)
       
   659                {
       
   660                iGeoConverter = CGeoConverter::NewL(*this);
       
   661                }
       
   662             iGeoConverter->ConvertL(iNewItem.iLocationData.iNetworkInfo);
       
   663             iMediaHandlingFlag |= KSnapGeoConvertInProgress;
       
   664             }
       
   665 #endif
       
   666         }    
       
   667 #ifdef LOC_REVERSEGEOCODE
       
   668     if((iMediaHandlingFlag & KSnapGeoConvertInProgress) > 0 || 
       
   669         (iMediaHandlingFlag & KReverseGeoCodingInProgress) > 0)
       
   670 #else
       
   671     if((iMediaHandlingFlag & KSnapGeoConvertInProgress) > 0 )
       
   672 #endif //LOC_REVERSEGEOCODE
       
   673         {
       
   674         // stop n/w info change listener, since device may connect to n/w 
       
   675         // and local trail will receive so many call backs on current n/w info change.
       
   676         iNetworkInfoChangeListener->StopNwInfoChangeNotifier();
       
   677         }
       
   678     else
       
   679         {
       
   680         iNetworkInfoChangeListener->StartNwInfoChangeNotifier();
       
   681         }
       
   682     LOG( "CLocationRecord::RemapObjectsL(), end" );
       
   683 
   449     }
   684     }
   450 
   685 
   451 TBool CLocationRecord::CheckGPSFix( const TPositionSatelliteInfo& aSatelliteInfo )
   686 TBool CLocationRecord::CheckGPSFix( const TPositionSatelliteInfo& aSatelliteInfo )
   452 	{
   687 	{
       
   688 	LOG("CLocationRecord::CheckGPSFix, begin");
   453 	TPosition position;
   689 	TPosition position;
   454 	aSatelliteInfo.GetPosition( position );
   690 	aSatelliteInfo.GetPosition( position );
   455 	LOG1( "CLocationRecord::CheckGPSFix latitude %f", position.Latitude() );
   691 	LOG1( "latitude %f", position.Latitude() );
   456 	LOG1( "CLocationRecord::CheckGPSFix longitude %f", position.Longitude() );
   692 	LOG1( "longitude %f", position.Longitude() );
   457 	TBool ret = ( Math::IsNaN(position.Latitude()) || Math::IsNaN(position.Longitude()) ) 
   693 	TBool ret = ( Math::IsNaN(position.Latitude()) || Math::IsNaN(position.Longitude()) ) 
   458 		? EFalse : ETrue;
   694 		? EFalse : ETrue;
       
   695     LOG1("CLocationRecord::CheckGPSFix, end. Ret - %d", ret);
   459    	return ret;
   696    	return ret;
   460 	}
   697 	}
   461     
   698     
   462 // --------------------------------------------------------------------------
   699 // --------------------------------------------------------------------------
   463 // From MPositionerObserver.
   700 // From MPositionerObserver.
   465 // --------------------------------------------------------------------------
   702 // --------------------------------------------------------------------------
   466 //    
   703 //    
   467 void CLocationRecord::NetworkInfo( const CTelephony::TNetworkInfoV1 &aNetworkInfo, 
   704 void CLocationRecord::NetworkInfo( const CTelephony::TNetworkInfoV1 &aNetworkInfo, 
   468 		const TInt aError )
   705 		const TInt aError )
   469     {
   706     {
   470     LOG("CLocationRecord::NetworkInfo");
   707     LOG("CLocationRecord::NetworkInfo, begin");
   471     if ( aError == KErrNone )
   708     if ( aError == KErrNone )
   472         {
   709         {
   473         LOG("CLocationRecord::NetworkInfo - KErrNone");
   710         LOG("CLocationRecord::NetworkInfo - KErrNone");
   474         iNetwork = aNetworkInfo;
   711         iNetwork = aNetworkInfo;
   475         if (iNetwork.iAccess == CTelephony::ENetworkAccessUtran)
   712         if (iNetwork.iAccess == CTelephony::ENetworkAccessUtran)
   476         	{
   713         	{
   477         	iNetwork.iLocationAreaCode = 0;
   714         	iNetwork.iLocationAreaCode = 0;
   478         	}
   715         	}
       
   716 #ifdef LOC_GEOTAGGING_CELLID
   479         if ( iState == RLocationTrail::ETrailStarting && iTrailStarted )
   717         if ( iState == RLocationTrail::ETrailStarting && iTrailStarted )
   480         	{
   718         	{
   481         	SetCurrentState( RLocationTrail::ETrailStarted );
   719         	SetCurrentState( RLocationTrail::ETrailStarted );
   482         	}
   720         	}
       
   721 #endif        
   483         }
   722         }
   484     else
   723     else
   485         {
   724         {
   486         LOG1("CLocationRecord::NetworkInfo - %d", aError );
   725         LOG1("CLocationRecord::NetworkInfo - %d", aError );
   487         iNetwork = CTelephony::TNetworkInfoV1();
   726         iNetwork = CTelephony::TNetworkInfoV1();
   490         iNetwork.iCellId = 0;
   729         iNetwork.iCellId = 0;
   491         iNetwork.iLocationAreaCode = 0;
   730         iNetwork.iLocationAreaCode = 0;
   492         iNetwork.iCountryCode.Zero();
   731         iNetwork.iCountryCode.Zero();
   493         iNetwork.iNetworkId.Zero();
   732         iNetwork.iNetworkId.Zero();
   494         }
   733         }
       
   734     LOG( "CLocationRecord::NetworkInfo(), end" );	
       
   735     }
       
   736 
       
   737 // --------------------------------------------------------------------------
       
   738 // CLocationRecord::GetMobilePhone4NwInfo
       
   739 // --------------------------------------------------------------------------
       
   740 //    
       
   741 RMobilePhone& CLocationRecord::GetMobilePhone4NwInfo()
       
   742     {
       
   743     LOG( "CLocationRecord::GetMobilePhone4NwInfo()" );    
       
   744     return iPhone;
   495     }
   745     }
   496 
   746 
   497 // --------------------------------------------------------------------------
   747 // --------------------------------------------------------------------------
   498 // CLocationRecord::StoreLocationL
   748 // CLocationRecord::StoreLocationL
   499 // --------------------------------------------------------------------------
   749 // --------------------------------------------------------------------------
   500 //    
   750 //    
   501 void CLocationRecord::StoreLocation( const TPositionSatelliteInfo& aSatelliteInfo )
   751 void CLocationRecord::StoreLocation( const TPositionSatelliteInfo& aSatelliteInfo )
   502     {
   752     {
       
   753     LOG( "CLocationRecord::StoreLocation(), begin" );    
   503     aSatelliteInfo.GetPosition( iNewItem.iLocationData.iPosition );
   754     aSatelliteInfo.GetPosition( iNewItem.iLocationData.iPosition );
   504     aSatelliteInfo.GetCourse( iNewItem.iLocationData.iCourse );
   755     aSatelliteInfo.GetCourse( iNewItem.iLocationData.iCourse );
   505     iNewItem.iLocationData.iSatellites = aSatelliteInfo.NumSatellitesUsed();
   756     iNewItem.iLocationData.iSatellites = aSatelliteInfo.NumSatellitesUsed();
   506     iNewItem.iLocationData.iQuality = aSatelliteInfo.HorizontalDoP();
   757     iNewItem.iLocationData.iQuality = aSatelliteInfo.HorizontalDoP();
   507     
   758     
   509     GetNetworkInfo( iNewItem.iLocationData.iNetworkInfo );
   760     GetNetworkInfo( iNewItem.iLocationData.iNetworkInfo );
   510     // Get Universal time
   761     // Get Universal time
   511     iNewItem.iTimeStamp.UniversalTime();
   762     iNewItem.iTimeStamp.UniversalTime();
   512     iNewItem.iTrailState = iState;
   763     iNewItem.iTrailState = iState;
   513     
   764     
   514     TInt error = iTrail.Append( iNewItem );
   765     TLocationTrailItem *item = new TLocationTrailItem();
       
   766     TInt error = KErrNone;
       
   767     if(item != NULL)
       
   768         {
       
   769         *item = iNewItem;
       
   770         error = iTrail.Append( item );
       
   771         }
   515     
   772     
   516     // If appending an item to the trail fails because of OOM, remove oldest trail items
   773     // If appending an item to the trail fails because of OOM, remove oldest trail items
   517     // until the new item fits or there's only one item left in the trail.
   774     // until the new item fits or there's only one item left in the trail.
   518     while ( error == KErrNoMemory && iTrail.Count() > 1 )
   775     while ( error == KErrNoMemory && iTrail.Count() > 1 )
   519 		{
   776 		{
   520 		LOG("CLocationRecord::StoreLocation - Out of memory! Shortening trail!");
   777 		LOG("CLocationRecord::StoreLocation - Out of memory! Shortening trail!");
   521 		iTrail.Remove( 0 );
   778 		iTrail.Remove( 0 );
   522 		error = iTrail.Append( iNewItem );
   779 		error = iTrail.Append( item ); 
   523 		}
   780 		}
   524     
   781     
   525     if ( iTrail.Count() > iMaxTrailSize )
   782     if ( iTrail.Count() > iMaxTrailSize )
   526         {
   783         {
       
   784         item = iTrail[0];
   527         iTrail.Remove( 0 );
   785         iTrail.Remove( 0 );
       
   786         iTrail.Compress();
       
   787         delete item;
   528         }
   788         }
   529     
   789     
   530     if( iAddObserver )
   790     if( iAddObserver )
   531     	{
   791     	{
   532     	iAddObserver->LocationAdded( iNewItem, aSatelliteInfo );
   792     	iAddObserver->LocationAdded( iNewItem, aSatelliteInfo );
   533     	}
   793     	}
       
   794     LOG( "CLocationRecord::StoreLocation(), end" );	
   534     }
   795     }
   535     
   796     
   536 // --------------------------------------------------------------------------
   797 // --------------------------------------------------------------------------
   537 // CLocationRecord::SetCurrentState
   798 // CLocationRecord::SetCurrentState
   538 // --------------------------------------------------------------------------
   799 // --------------------------------------------------------------------------
   539 //        
   800 //        
   540 void CLocationRecord::SetCurrentState( TLocTrailState aState )    
   801 void CLocationRecord::SetCurrentState( TLocTrailState aState )    
   541     {
   802     {
   542     LOG1( "CLocationRecord::SetCurrentState(), begin, state:%d", aState );
   803     LOG1( "CLocationRecord::SetCurrentState(), begin, state:%d", aState );
   543     iState = aState;
   804     iState = aState;
   544     iProperty.Set( KPSUidLocationTrail, KLocationTrailState, (TInt) aState );
   805     if( iTrailStarted )
       
   806         {
       
   807         // Set the property only when trail is started to avoid icon flickering and wrong icon update in UI
       
   808         if( iGpsDataAvailableFlag  
       
   809 #ifdef LOC_GEOTAGGING_CELLID
       
   810             || ( iNetwork.iCellId > 0 && 
       
   811             ((iNetwork.iLocationAreaCode == 0 && iNetwork.iAccess == CTelephony::ENetworkAccessUtran) || // 3G
       
   812             (iNetwork.iLocationAreaCode > 0 && (iNetwork.iAccess == CTelephony::ENetworkAccessGsm ||  // 2G
       
   813             						iNetwork.iAccess == CTelephony::ENetworkAccessGsmCompact)))  &&
       
   814             iNetwork.iCountryCode.Length() > 0 &&
       
   815             iNetwork.iNetworkId.Length() > 0 )
       
   816 #endif // LOC_GEOTAGGING_CELLID            
       
   817             )
       
   818             {
       
   819             // set the value 3 to have Geo tag available icon else not available.
       
   820             iProperty.Set( KPSUidLocationTrail, KLocationTrailState, 
       
   821                 (TInt) RLocationTrail::ETrailStarted );
       
   822             }
       
   823         else
       
   824             {
       
   825             iProperty.Set( KPSUidLocationTrail, KLocationTrailState, 
       
   826                 (TInt) RLocationTrail::ETrailStopped );
       
   827             }
       
   828         }
       
   829     else
       
   830     	{
       
   831     		iProperty.Set( KPSUidLocationTrail, KLocationTrailState, 
       
   832              (TInt) RLocationTrail::ETrailStopped );
       
   833     	}
       
   834     		
   545     if ( iObserver )
   835     if ( iObserver )
   546         {
   836         {
   547         iObserver->LocationTrailStateChange();
   837         iObserver->LocationTrailStateChange();
   548         }
   838         }
   549     LOG( "CLocationRecord::SetCurrentState(), end" );
   839     LOG( "CLocationRecord::SetCurrentState(), end" );
   554 // --------------------------------------------------------------------------
   844 // --------------------------------------------------------------------------
   555 //
   845 //
   556 void CLocationRecord::HandleLocationRequest( const TPositionSatelliteInfo& aSatelliteInfo, 
   846 void CLocationRecord::HandleLocationRequest( const TPositionSatelliteInfo& aSatelliteInfo, 
   557                                              const TInt aError )    
   847                                              const TInt aError )    
   558     {
   848     {
       
   849     LOG( "CLocationRecord::HandleLocationRequest(), begin" );    
   559 	CTelephony::TNetworkInfoV1 network = CTelephony::TNetworkInfoV1();
   850 	CTelephony::TNetworkInfoV1 network = CTelephony::TNetworkInfoV1();
   560     if ( aError == KErrNone )
   851     if ( aError == KErrNone )
   561         {
   852         {
   562        	GetNetworkInfo( network );
   853        	GetNetworkInfo( network );
   563         iObserver->CurrentLocation( aSatelliteInfo, network, aError );
   854         iObserver->CurrentLocation( aSatelliteInfo, network, aError );
   564         iRequestCurrentLoc = EFalse;
   855         iRequestCurrentLoc = EFalse;
   565         if ( !iTrailStarted )
   856         if ( !iTrailStarted )
   566             {
   857             {
       
   858             iGpsDataAvailableFlag = EFalse;
   567             iPositionInfo->Stop();
   859             iPositionInfo->Stop();
   568             }
   860             }
   569         }
   861         }
   570     else
   862     else
   571         {
   863         {
   575             iObserver->CurrentLocation( aSatelliteInfo, network, KErrTimedOut );
   867             iObserver->CurrentLocation( aSatelliteInfo, network, KErrTimedOut );
   576             iRequestCurrentLoc = EFalse;
   868             iRequestCurrentLoc = EFalse;
   577             iLocationCounter = 0;
   869             iLocationCounter = 0;
   578             if ( !iTrailStarted )
   870             if ( !iTrailStarted )
   579                 {
   871                 {
       
   872                 iGpsDataAvailableFlag = EFalse;
   580                 iPositionInfo->Stop();
   873                 iPositionInfo->Stop();
   581                 }
   874                 }
   582             }       
   875             }       
   583         }    
   876         }  
   584     }
   877     LOG( "CLocationRecord::HandleLocationRequest(), end" );	
   585 
   878     }
       
   879 
       
   880 
       
   881 // --------------------------------------------------------------------------
       
   882 // CLocationRecord::UpdateNetworkInfo
       
   883 // --------------------------------------------------------------------------
       
   884 //
   586 TInt CLocationRecord::UpdateNetworkInfo( TAny* aAny )
   885 TInt CLocationRecord::UpdateNetworkInfo( TAny* aAny )
   587 	{
   886 	{
   588 	TPositionSatelliteInfo nullPositionInfo;
   887 	TPositionSatelliteInfo nullPositionInfo;
   589 	CLocationRecord* self = STATIC_CAST( CLocationRecord*, aAny );
   888 	CLocationRecord* self = STATIC_CAST( CLocationRecord*, aAny );
   590 	self->StoreLocation( nullPositionInfo );
   889 	self->StoreLocation( nullPositionInfo );
   593 
   892 
   594 
   893 
   595 EXPORT_C void CLocationRecord::CreateLocationObjectL( const TLocationData& aLocationData,
   894 EXPORT_C void CLocationRecord::CreateLocationObjectL( const TLocationData& aLocationData,
   596 		const TUint& aObjectId )
   895 		const TUint& aObjectId )
   597 	{
   896 	{
   598 	TItemId locationId = DoCreateLocationL( aLocationData );
   897     LOG( "CLocationRecord::CreateLocationObjectL(), begin" );	
   599 	CreateRelationL( aObjectId, locationId );
   898 	TLocationSnapshotItem* newItem = new (ELeave) TLocationSnapshotItem;
       
   899 	newItem->iObjectId = aObjectId;
       
   900 	newItem->iLocationData = aLocationData;
       
   901 	
       
   902 	// for downloaded files, network informations are not valid.
       
   903 	// Do location handling only based on lat/lon
       
   904 	// all all n/w information to invalid value.
       
   905     newItem->iLocationData.iNetworkInfo.iCellId = 0;
       
   906     newItem->iLocationData.iNetworkInfo.iAccess = CTelephony::ENetworkAccessUnknown;
       
   907     newItem->iLocationData.iNetworkInfo.iLocationAreaCode = 0;
       
   908     newItem->iLocationData.iNetworkInfo.iCountryCode.Zero();
       
   909     newItem->iLocationData.iNetworkInfo.iNetworkId.Zero();
       
   910 	newItem->iFlag = KDownloadMediaFile;
       
   911 	
       
   912 	iMediaItems.Append( newItem );
       
   913     FindLocationFromDBL();
       
   914     GeoTaggingCompleted();
       
   915     LOG( "CLocationRecord::CreateLocationObjectL(), end" );	
       
   916 
   600 	}
   917 	}
   601 
   918 
   602 
   919 // --------------------------------------------------------------------------
       
   920 // CLocationRecord::LocationSnapshotL
       
   921 // --------------------------------------------------------------------------
       
   922 //
   603 EXPORT_C void CLocationRecord::LocationSnapshotL( const TUint& aObjectId )
   923 EXPORT_C void CLocationRecord::LocationSnapshotL( const TUint& aObjectId )
   604 	{
   924 	{
   605 	LOG("CLocationRecord::LocationSnapshotL");
   925     LOG( "CLocationRecord::LocationSnapshotL(), begin" );	
   606 	
       
   607 	TBool previousMatch = EFalse;
   926 	TBool previousMatch = EFalse;
   608 	CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
       
   609 
   927 
   610 	// get locationdata from trail with object time
   928 	// get locationdata from trail with object time
   611 	TTime timestamp = GetMdeObjectTimeL( aObjectId );
   929 	TTime timestamp = GetMdeObjectTimeL( aObjectId );
   612 	TLocationData locationData;
   930 	TLocationData locationData;
   613 	TLocTrailState state;
   931 	TLocTrailState state;
   614 	GetLocationByTimeL( timestamp, locationData, state );
   932 	TRAPD(err, GetLocationByTimeL( timestamp, locationData, state ));
   615 	
   933 	if(err != KErrNone && iTrailStarted)
   616 	iObjectId = aObjectId;
   934         {
   617 	iLocationData = locationData;
   935         // Execution shouldn't come over here.
   618 
   936         // Handling error case in worst sceenario..
       
   937         StartL(iTrailCaptureSetting);
       
   938         // this case may apprear, when cache data is not within the delta limit because someone call stop trail.
       
   939         // sceenario, take photograph, then stop taking for sometime, again take photograph.
       
   940         // during this time, if there is no change in position (because someone stop trail) and n/w, then cache has old value.
       
   941         // go with the n/w based.
       
   942         LOG( "Old trail cache. go for n/w based." );
       
   943         UpdateNetworkInfo( this );
       
   944         // again read the location.
       
   945         GetLocationByTimeL( timestamp, locationData, state );
       
   946         }
       
   947 	TLocationSnapshotItem* newItem = new (ELeave) TLocationSnapshotItem;
       
   948 	newItem->iObjectId = aObjectId;
       
   949 	newItem->iLocationData = locationData;
       
   950 	newItem->iFlag = KSnapMediaFile;
       
   951 	                
       
   952 	iMediaItems.Append( newItem );
       
   953 
       
   954 	TItemId lastLocationId = 0;
       
   955 	if ( (iLastMediaItem.iFlag & KSnapMediaFile) > 0)
       
   956 	    {
       
   957         lastLocationId = iLastMediaItem.iLocationId;
       
   958 	    }
       
   959 
       
   960 	CTelephony::TNetworkInfoV1* net = &locationData.iNetworkInfo;
   619 	// capture only network data
   961 	// capture only network data
   620 	if ( iTrailCaptureSetting == RLocationTrail::ECaptureNetworkInfo )
   962 	if ( iTrailCaptureSetting == RLocationTrail::ECaptureNetworkInfo )
   621 		{
   963 		{
   622 		CTelephony::TNetworkInfoV1* net = &locationData.iNetworkInfo;
   964         if ( net->iCellId == 0 && 
       
   965              net->iLocationAreaCode == 0 &&
       
   966              net->iCountryCode.Length() == 0 &&
       
   967              net->iNetworkId.Length() == 0 )
       
   968             {
       
   969             // no n/w info... put it into remap.
       
   970             // remove the last appended element.
       
   971             LOG("No network info (offline mode + no GPS fix), keep for remapping");
       
   972 			TRemapItem remapItem;
       
   973 			remapItem.iObjectId = aObjectId;
       
   974 			remapItem.iTime = timestamp;
       
   975 			iRemapper->Append( remapItem );
   623 		
   976 		
       
   977 			TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
       
   978 			iMediaItems.Remove(iMediaItems.Count() - 1);
       
   979 			iMediaItems.Compress();
       
   980 			delete firstPtr;
       
   981             }   
       
   982         else
       
   983             {
       
   984             // n/w info available
       
   985             NetworkInfoSnapshotL();
       
   986             }
       
   987 		}
       
   988 	else if ( Math::IsNaN( locationData.iPosition.Latitude() ) && 
       
   989 			Math::IsNaN( locationData.iPosition.Longitude() ))
       
   990 		{
       
   991 	    // coordinates empty, with or without cellular info
       
   992 
   624 		if ( net->iCellId == 0 && 
   993 		if ( net->iCellId == 0 && 
   625 				net->iLocationAreaCode == 0 &&
   994 				net->iLocationAreaCode == 0 &&
   626 				net->iCountryCode.Length() == 0 &&
   995 				net->iCountryCode.Length() == 0 &&
   627 				net->iNetworkId.Length() == 0 )
   996 				net->iNetworkId.Length() == 0 )
   628 			{
   997 			{
   629 			// nothing to do
   998             LOG("No network info (offline mode + no GPS fix), keep for remapping");
   630 			LOG("CLocationRecord::LocationSnapshotL - no network info available");
   999 			TRemapItem remapItem;
   631 			}
  1000 			remapItem.iObjectId = aObjectId;
   632 		else if ( iLastLocationId != 0 )
  1001 			remapItem.iTime = timestamp;
   633 			{
  1002 			iRemapper->Append( remapItem );
   634 			CTelephony::TNetworkInfoV1* lastnet = &iLastLocation.iNetworkInfo;
       
   635 			
       
   636 			// compare to previous network info
       
   637 			TItemId locationId = iLastLocationId;
       
   638 			if ( lastnet->iCellId != net->iCellId ||
       
   639 					lastnet->iLocationAreaCode != net->iLocationAreaCode ||
       
   640 					lastnet->iCountryCode != net->iCountryCode ||
       
   641 					lastnet->iNetworkId != net->iNetworkId )
       
   642 				{
       
   643 				LOG("CLocationRecord::LocationSnapshotL - network info changed");
       
   644 				locationId = DoCreateLocationL( locationData );
       
   645 				}
       
   646 			CreateRelationL( aObjectId, locationId );
       
   647 			}
       
   648 		else 
       
   649 			{
       
   650 			// new location
       
   651 			TItemId locationId = DoCreateLocationL( locationData );
       
   652 			CreateRelationL( aObjectId, locationId );
       
   653 			}
       
   654 		return;
       
   655 		}
       
   656 	
       
   657 	// coordinates empty (will be remapped)
       
   658 	if ( Math::IsNaN( locationData.iPosition.Latitude() ) && 
       
   659 			Math::IsNaN( locationData.iPosition.Longitude() ))
       
   660 		{
       
   661 		TRemapItem remapItem;
       
   662 		remapItem.iObjectId = aObjectId;
       
   663 		remapItem.iTime = timestamp;
       
   664 		
  1003 		
   665 		CTelephony::TNetworkInfoV1* net = &locationData.iNetworkInfo;
  1004 			TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
   666 
  1005 			iMediaItems.Remove(iMediaItems.Count() - 1);
   667 		// no network info (offline mode + no GPS fix)
  1006 			iMediaItems.Compress();
   668 		if ( net->iCellId == 0 && 
  1007 			delete firstPtr;
   669 				net->iLocationAreaCode == 0 &&
       
   670 				net->iCountryCode.Length() == 0 &&
       
   671 				net->iNetworkId.Length() == 0 )
       
   672 			{
       
   673 			LOG("CLocationRecord::LocationSnapshotL - empty remap item created");
       
   674 			}
  1008 			}
   675 		// check match for last created locationobject
  1009 		// check match for last created locationobject
   676 		else if ( iLastLocationId != 0 )
  1010 #ifdef LOC_REVERSEGEOCODE
       
  1011 		else if ( (iLastMediaItem.iFlag & KSnapMediaFile) > 0 &&
       
  1012             iLastMediaItem.iCountryTagId > 0)
       
  1013 #else
       
  1014         else if ( (iLastMediaItem.iFlag & KSnapMediaFile) > 0)
       
  1015 #endif //LOC_REVERSEGEOCODE
   677 			{
  1016 			{
   678 			TItemId locationId;
  1017 			TLocationData lastLocationData = iLastMediaItem.iLocationData;
   679 			CTelephony::TNetworkInfoV1* lastnet = &iLastLocation.iNetworkInfo;
  1018 			CTelephony::TNetworkInfoV1* lastnet = &lastLocationData.iNetworkInfo;
   680 
  1019 
   681 			// networkinfo changed from last location
  1020 			// networkinfo changed from last location
   682 			if ( lastnet->iCellId != net->iCellId ||
  1021 			if ( lastnet->iCellId == net->iCellId &&
   683 					lastnet->iLocationAreaCode != net->iLocationAreaCode ||
  1022 					lastnet->iLocationAreaCode == net->iLocationAreaCode &&
   684 					lastnet->iCountryCode != net->iCountryCode ||
  1023 					lastnet->iCountryCode == net->iCountryCode &&
   685 					lastnet->iNetworkId != net->iNetworkId )
  1024 					lastnet->iNetworkId == net->iNetworkId )
   686 				{
  1025 				{
   687 				LOG("CLocationRecord::LocationSnapshotL - remap with new network info");
  1026 				// same network.
   688 				locationId = DoCreateLocationL( locationData );
  1027 				previousMatch = ETrue;
   689 				}		
  1028 				CreateRelationL( aObjectId, lastLocationId );
   690 			else
  1029 				// attach same tags associated to last location
   691 				{
  1030 #ifdef LOC_REVERSEGEOCODE
   692 				LOG("CLocationRecord::LocationSnapshotL - remap with previous network info");
  1031 				if ( iLastMediaItem.iCountryTagId )   //found from DB last time
   693 				locationId = iLastLocationId;
  1032 				    {
       
  1033 				    iTagCreator->AttachTagsL( aObjectId, 
       
  1034                                 iLastMediaItem.iCountryTagId, iLastMediaItem.iCityTagId );
       
  1035 				    }
       
  1036 #endif //LOC_REVERSEGEOCODE
       
  1037                 // remove the current item.
       
  1038 				TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
       
  1039 				iMediaItems.Remove(iMediaItems.Count() - 1);
       
  1040 				iMediaItems.Compress();
       
  1041 				delete firstPtr;
   694 				}
  1042 				}
   695 			TItemId relationId = CreateRelationL( aObjectId, locationId );
       
   696 			remapItem.iLocationId = locationId;
       
   697 			remapItem.iRelationId = relationId;
       
   698 			}
  1043 			}
   699 		else
  1044 		
   700 			{
  1045 		if ( !previousMatch )
   701 			// new location with only network data
  1046 		    {
   702 			TItemId locationId = DoCreateLocationL( locationData );
  1047 			// go for n/w based
   703 			TItemId relationId = CreateRelationL( aObjectId, locationId );
  1048             newItem->iFlag |= KNetQueryBit;
   704 			remapItem.iLocationId = locationId;
  1049 		    }
   705 			remapItem.iRelationId = relationId;
       
   706 			}
       
   707 		iRemapper->Append( remapItem );
       
   708 		return;
       
   709 		}
  1050 		}
   710 		
  1051        
   711 	// valid coordinates found
  1052 	// valid coordinates found
   712 	if ( iLastLocationId != 0 )
  1053 	else if ( lastLocationId != 0 && 
       
  1054         ((iLastMediaItem.iFlag & KSnapMediaFile) > 0))
   713 		{
  1055 		{
   714 		CTelephony::TNetworkInfoV1* net = &locationData.iNetworkInfo;
  1056         TLocationData lastLocationData = iLastMediaItem.iLocationData;
   715 		CTelephony::TNetworkInfoV1* lastnet = &iLastLocation.iNetworkInfo;
  1057 		CTelephony::TNetworkInfoV1* lastnet = &lastLocationData.iNetworkInfo;
   716 		
  1058 		
   717 		// first check if networkinfo matches last created location
  1059 		// first check if networkinfo matches last created location
   718 		if ( lastnet->iCellId == net->iCellId &&
  1060 		if ( lastnet->iCellId == net->iCellId &&
   719 				lastnet->iLocationAreaCode == net->iLocationAreaCode &&
  1061 				lastnet->iLocationAreaCode == net->iLocationAreaCode &&
   720 				lastnet->iCountryCode == net->iCountryCode &&
  1062 				lastnet->iCountryCode == net->iCountryCode &&
   721 				lastnet->iNetworkId == net->iNetworkId )
  1063 				lastnet->iNetworkId == net->iNetworkId )
   722 			{
  1064 			{
   723 			LOG("CLocationRecord::LocationSnapshotL - network info matches");
       
   724 			
  1065 			
   725 			// if both locations have valid coordinates, calculate distance between points
  1066 			// if both locations have valid coordinates, calculate distance between points
   726 			if ( !Math::IsNaN( iLastLocation.iPosition.Latitude() ) && 
  1067 			if (
   727 					!Math::IsNaN( iLastLocation.iPosition.Longitude() ) && 
  1068 #ifdef LOC_REVERSEGEOCODE
       
  1069                 !iLastReverseGeocodeFails &&
       
  1070 #endif //LOC_REVERSEGEOCODE
       
  1071                 !Math::IsNaN( lastLocationData.iPosition.Latitude() ) && 
       
  1072 					!Math::IsNaN( lastLocationData.iPosition.Longitude() ) && 
   728 					!Math::IsNaN( locationData.iPosition.Latitude() ) && 
  1073 					!Math::IsNaN( locationData.iPosition.Latitude() ) && 
   729 					!Math::IsNaN( locationData.iPosition.Longitude() ))
  1074 					!Math::IsNaN( locationData.iPosition.Longitude() ))
   730 				{
  1075 				{
   731 				TReal32 distance;
  1076 				TReal32 distance;
   732 				TInt err = locationData.iPosition.Distance(iLastLocation.iPosition, distance);
  1077 				TInt err = locationData.iPosition.Distance(lastLocationData.iPosition, distance);
   733 				
  1078 				
   734 				if ( distance < iLocationDelta )
  1079 				if ( distance < iLocationDelta )
   735 					{
  1080 					{
   736 					LOG("CLocationRecord::LocationSnapshotL - location close to the previous one");
  1081 					LOG("location close to the previous one");
   737 					previousMatch = ETrue;
  1082 					previousMatch = ETrue;
   738 					CreateRelationL( aObjectId, iLastLocationId );
  1083 					CreateRelationL( aObjectId, lastLocationId );
   739 					LOG("CLocationRecord::CreateLocationObjectL - last location matched");
  1084 					
       
  1085 #ifdef LOC_REVERSEGEOCODE
       
  1086 					// attach same tags associated to last location
       
  1087 					if ( iLastMediaItem.iCountryTagId )
       
  1088 					    {
       
  1089 					    iTagCreator->AttachTagsL( 
       
  1090 					          aObjectId, iLastMediaItem.iCountryTagId, iLastMediaItem.iCityTagId );
       
  1091                         TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
       
  1092                         iMediaItems.Remove(iMediaItems.Count() - 1);
       
  1093                         iMediaItems.Compress();
       
  1094                         delete firstPtr;
       
  1095 					    }
       
  1096 					else 
       
  1097 					    {
       
  1098 					    // country tag not found.. go for reverse geocoding..
       
  1099 					    newItem->iLocationId = lastLocationId;
       
  1100 					    iLocationItems.Append( newItem );
       
  1101 						iMediaItems.Remove(iMediaItems.Count() - 1);
       
  1102 						iMediaItems.Compress();
       
  1103 					                           
       
  1104                         if(!(iMediaHandlingFlag & KReverseGeoCodingInProgress))
       
  1105                            {
       
  1106                            iMediaHandlingFlag |= KReverseGeoCodingInProgress;
       
  1107                            if(iRevGeocoderPlugin)
       
  1108 						       {
       
  1109                                iRevGeocoderPlugin->GetAddressByCoordinateL
       
  1110 							        ( iLocationItems[0]->iLocationData.iPosition,
       
  1111                                       iConnectionOption);
       
  1112 							   }
       
  1113                            }
       
  1114 					    }
       
  1115 #else
       
  1116                     // remove from the queue
       
  1117                     TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
       
  1118                     iMediaItems.Remove(iMediaItems.Count() - 1);
       
  1119                     iMediaItems.Compress();
       
  1120                     delete firstPtr;
       
  1121 
       
  1122 #endif //LOC_REVERSEGEOCODE
   740 					}
  1123 					}
   741 				}
  1124 				}
   742 			}
  1125 			}
   743 		}
  1126 		}
   744 	
  1127 	
   745 	// last location did not match, find existing one from DB
  1128 	// last location did not match, find existing one from DB
   746 	if( !previousMatch )
  1129 	if( !previousMatch )
   747 		{
  1130 		{
   748 		LOG("CLocationRecord::LocationSnapshotL - query location");
  1131         FindLocationFromDBL();
   749 		const TReal64 KMeterInDegrees = 0.000009;
       
   750 		const TReal64 KPi = 3.14159265358979;
       
   751 		const TReal32 K180Degrees = 180.0;
       
   752 	
       
   753 		TReal64 latitude = locationData.iPosition.Latitude();
       
   754 		TReal64 longitude = locationData.iPosition.Longitude();
       
   755 		// calculate distance in degrees
       
   756 		TReal64 cosine;
       
   757 		Math::Cos(cosine, locationData.iPosition.Latitude() * KPi / K180Degrees );
       
   758 		TReal64 latDelta = iLocationDelta * KMeterInDegrees;
       
   759 		TReal64 lonDelta = latDelta * cosine;
       
   760 		
       
   761 		CMdEObjectDef& locationObjectDef = namespaceDef.GetObjectDefL( Location::KLocationObject );
       
   762 		
       
   763 		CMdEPropertyDef& latitudeDef = locationObjectDef.GetPropertyDefL(
       
   764 				Location::KLatitudeProperty );
       
   765 		CMdEPropertyDef& longitudeDef = locationObjectDef.GetPropertyDefL(
       
   766 				Location::KLongitudeProperty );
       
   767 		CMdEPropertyDef& cellIdDef = locationObjectDef.GetPropertyDefL(
       
   768 				Location::KCellIdProperty );
       
   769 		CMdEPropertyDef& locationCodeDef = locationObjectDef.GetPropertyDefL( 
       
   770 				Location::KLocationAreaCodeProperty );
       
   771 		CMdEPropertyDef& countryCodeDef = locationObjectDef.GetPropertyDefL( 
       
   772 				Location::KCountryCodeProperty );
       
   773 		CMdEPropertyDef& networkCodeDef = locationObjectDef.GetPropertyDefL( 
       
   774 				Location::KNetworkCodeProperty );
       
   775 		
       
   776 		iLocationQuery = iMdeSession->NewObjectQueryL( namespaceDef, locationObjectDef, this );
       
   777 		CMdELogicCondition& cond = iLocationQuery->Conditions();
       
   778 		cond.SetOperator( ELogicConditionOperatorAnd );
       
   779 		
       
   780 		LOG1( "CLocationRecord::LocationSnapshotL latitude: %f", latitude);
       
   781 		LOG1( "CLocationRecord::LocationSnapshotL latdelta: %f", latDelta);
       
   782 		LOG1( "CLocationRecord::LocationSnapshotL longitude: %f", longitude);
       
   783 		LOG1( "CLocationRecord::LocationSnapshotL londelta: %f", lonDelta);
       
   784 		
       
   785 		cond.AddPropertyConditionL( latitudeDef, 
       
   786 				TMdERealBetween( latitude - latDelta, latitude + latDelta ));
       
   787 		cond.AddPropertyConditionL( longitudeDef, 
       
   788 				TMdERealBetween( longitude - lonDelta, longitude + lonDelta ));
       
   789 		cond.AddPropertyConditionL( cellIdDef, 
       
   790 				TMdEUintEqual( locationData.iNetworkInfo.iCellId) );
       
   791 		cond.AddPropertyConditionL( locationCodeDef, 
       
   792 				TMdEUintEqual( locationData.iNetworkInfo.iLocationAreaCode) );
       
   793 		cond.AddPropertyConditionL( countryCodeDef, ETextPropertyConditionCompareEquals,
       
   794 				locationData.iNetworkInfo.iCountryCode );
       
   795 		cond.AddPropertyConditionL( networkCodeDef, ETextPropertyConditionCompareEquals,
       
   796 				locationData.iNetworkInfo.iNetworkId );
       
   797 		
       
   798 		iLocationQuery->FindL();			
       
   799 		}
  1132 		}
       
  1133     LOG( "CLocationRecord::LocationSnapshotL(), end" );	
   800 	}
  1134 	}
   801 
  1135 
   802 	
  1136 
   803 TItemId CLocationRecord::DoCreateLocationL( const TLocationData& aLocationData ) 
  1137 // --------------------------------------------------------------------------
       
  1138 // CLocationRecord::NetworkInfoSnapshotL
       
  1139 // --------------------------------------------------------------------------
       
  1140 //
       
  1141 void CLocationRecord::NetworkInfoSnapshotL()
       
  1142     {
       
  1143     LOG( "CLocationRecord::NetworkInfoSnapshotL(), begin" );    
       
  1144     // n/w info available.. always act on last element within the queue
       
  1145     TInt lastItemIndex = iMediaItems.Count() - 1;
       
  1146     if ( lastItemIndex >= 0 &&
       
  1147         (iMediaItems[lastItemIndex]->iFlag & KSnapMediaFile) > 0 )
       
  1148         {
       
  1149         CTelephony::TNetworkInfoV1* net = &iMediaItems[lastItemIndex]->iLocationData.iNetworkInfo;
       
  1150         //only for snap item.
       
  1151         if ( (iLastMediaItem.iFlag & KSnapMediaFile) > 0 )
       
  1152             {
       
  1153              CTelephony::TNetworkInfoV1* lastnet = &iLastMediaItem.iLocationData.iNetworkInfo;
       
  1154             
       
  1155             // compare to previous network info
       
  1156             TItemId locationId = iLastMediaItem.iLocationId;
       
  1157             if ( lastnet->iCellId != net->iCellId ||
       
  1158                     lastnet->iLocationAreaCode != net->iLocationAreaCode ||
       
  1159                     lastnet->iCountryCode != net->iCountryCode ||
       
  1160                     lastnet->iNetworkId != net->iNetworkId )
       
  1161                 {
       
  1162                 // last one is not matching.
       
  1163                 // let's check the database of any existing etry.
       
  1164                 iMediaItems[lastItemIndex]->iFlag |= KNetQueryBit;
       
  1165                 }
       
  1166             else
       
  1167                 {
       
  1168                 // matching with the last entry. Just create a relation.
       
  1169                 CreateRelationL( iMediaItems[lastItemIndex]->iObjectId, locationId );
       
  1170                 TLocationSnapshotItem* firstPtr = iMediaItems[lastItemIndex];
       
  1171                 iMediaItems.Remove(lastItemIndex);
       
  1172                 delete firstPtr;
       
  1173                 }
       
  1174             }
       
  1175         else 
       
  1176             {
       
  1177             // let's check the database of any existing etry.
       
  1178             iMediaItems[lastItemIndex]->iFlag |= KNetQueryBit;
       
  1179             }
       
  1180         }    
       
  1181     LOG( "CLocationRecord::NetworkInfoSnapshotL(), end" );   
       
  1182     }
       
  1183 
       
  1184 
       
  1185 // --------------------------------------------------------------------------
       
  1186 // CLocationRecord::FindLocationFromDBL
       
  1187 // --------------------------------------------------------------------------
       
  1188 //
       
  1189 void CLocationRecord::FindLocationFromDBL()
       
  1190     {
       
  1191     LOG( "CLocationRecord::FindLocationFromDBL(), begin" );    
       
  1192     if(((iMediaHandlingFlag & KLocationQueryInProgress)> 0)
       
  1193 	     || (iMediaItems.Count() <= 0))
       
  1194         {
       
  1195 		// query is in progress or queue is empty
       
  1196 		LOG1( "query is in progress or queue is empty. Count - %d", iMediaItems.Count() );
       
  1197         return;
       
  1198         }
       
  1199      if ( (iMediaItems[0]->iFlag & KNetQueryBit) > 0 )
       
  1200          {
       
  1201 		 // n/w based.
       
  1202          FindLocationWithSameNetInfoL();
       
  1203          return;
       
  1204          }
       
  1205         
       
  1206      const TReal64 KMeterInDegrees = 0.000009;
       
  1207      const TReal64 KPi = 3.14159265358979;
       
  1208      const TReal32 K180Degrees = 180.0;
       
  1209             
       
  1210      TReal64 latitude = iMediaItems[0]->iLocationData.iPosition.Latitude();
       
  1211      TReal64 longitude = iMediaItems[0]->iLocationData.iPosition.Longitude();
       
  1212      // calculate distance in degrees
       
  1213      TReal64 cosine;
       
  1214      Math::Cos(cosine, latitude * KPi / K180Degrees );
       
  1215      TReal64 latDelta = iLocationDelta * KMeterInDegrees;
       
  1216      TReal64 lonDelta = latDelta * cosine;
       
  1217      
       
  1218      //get network related defs
       
  1219      CMdEPropertyDef& cellIdDef = iLocationObjectDef->GetPropertyDefL(
       
  1220                     Location::KCellIdProperty );
       
  1221      CMdEPropertyDef& locationCodeDef = iLocationObjectDef->GetPropertyDefL( 
       
  1222                     Location::KLocationAreaCodeProperty );
       
  1223      CMdEPropertyDef& countryCodeDef = iLocationObjectDef->GetPropertyDefL( 
       
  1224                     Location::KCountryCodeProperty );
       
  1225      CMdEPropertyDef& networkCodeDef = iLocationObjectDef->GetPropertyDefL( 
       
  1226                     Location::KNetworkCodeProperty );
       
  1227      if(iLocationQuery)
       
  1228          {
       
  1229          iLocationQuery->RemoveObserver(*this);
       
  1230          iLocationQuery->Cancel();
       
  1231          delete iLocationQuery;
       
  1232          iLocationQuery = NULL;
       
  1233          }
       
  1234      iLocationQuery = iMdeSession->NewObjectQueryL( *iNamespaceDef, *iLocationObjectDef, this );
       
  1235                  
       
  1236      CMdELogicCondition& cond = iLocationQuery->Conditions();
       
  1237      cond.SetOperator( ELogicConditionOperatorAnd );
       
  1238             
       
  1239      LOG1( "latitude: %f", latitude);
       
  1240      LOG1( "latdelta: %f", latDelta);
       
  1241      LOG1( "longitude: %f", longitude);
       
  1242      LOG1( "londelta: %f", lonDelta);
       
  1243             
       
  1244      cond.AddPropertyConditionL( *iLatitudeDef, 
       
  1245                     TMdERealBetween( latitude - latDelta, latitude + latDelta ));
       
  1246      cond.AddPropertyConditionL( *iLongitudeDef, 
       
  1247                     TMdERealBetween( longitude - lonDelta, longitude + lonDelta ));
       
  1248      
       
  1249      if ( iMediaItems[0]->iLocationData.iNetworkInfo.iCellId > 0 )
       
  1250          {
       
  1251          cond.AddPropertyConditionL( cellIdDef, 
       
  1252                     TMdEUintEqual( iMediaItems[0]->iLocationData.iNetworkInfo.iCellId) );
       
  1253          }
       
  1254      if ( iMediaItems[0]->iLocationData.iNetworkInfo.iLocationAreaCode > 0 )
       
  1255          {
       
  1256          cond.AddPropertyConditionL( locationCodeDef, 
       
  1257                     TMdEUintEqual( iMediaItems[0]->iLocationData.iNetworkInfo.iLocationAreaCode) );
       
  1258          }
       
  1259      if ( iMediaItems[0]->iLocationData.iNetworkInfo.iCountryCode.Length() > 0 )
       
  1260          {
       
  1261          cond.AddPropertyConditionL( countryCodeDef, ETextPropertyConditionCompareEquals,
       
  1262                  iMediaItems[0]->iLocationData.iNetworkInfo.iCountryCode );
       
  1263          }
       
  1264      if ( iMediaItems[0]->iLocationData.iNetworkInfo.iNetworkId.Length() > 0 )
       
  1265          {
       
  1266          cond.AddPropertyConditionL( networkCodeDef, ETextPropertyConditionCompareEquals,
       
  1267                  iMediaItems[0]->iLocationData.iNetworkInfo.iNetworkId );
       
  1268          }
       
  1269      iMediaHandlingFlag |= KLocationQueryInProgress;
       
  1270      iLocationQuery->FindL();  
       
  1271 
       
  1272     LOG( "CLocationRecord::FindLocationFromDBL(), end" );     
       
  1273     }
       
  1274 
       
  1275 
       
  1276 // --------------------------------------------------------------------------
       
  1277 // CLocationRecord::DoCreateLocationL
       
  1278 // --------------------------------------------------------------------------
       
  1279 //	
       
  1280 TItemId CLocationRecord::DoCreateLocationL( const TLocationData& aLocationData )
   804 	{
  1281 	{
   805 	LOG("CLocationRecord::DoCreateLocationL - start");
  1282     LOG( "CLocationRecord::DoCreateLocationL(), begin" );    
   806 	TItemId locationObjectId;
  1283 	TItemId locationObjectId;
   807 	
  1284 	
   808 	CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
       
   809 
       
   810 	CMdEObjectDef& locationObjectDef = namespaceDef.GetObjectDefL( Location::KLocationObject );
       
   811 	
       
   812 	// required object properties
  1285 	// required object properties
   813 	CMdEPropertyDef& creationDef = locationObjectDef.GetPropertyDefL(
  1286 	CMdEPropertyDef& creationDef = iLocationObjectDef->GetPropertyDefL(
   814 			Object::KCreationDateProperty );
  1287 			Object::KCreationDateProperty );
   815 	CMdEPropertyDef& modifiedDef = locationObjectDef.GetPropertyDefL(
  1288 	CMdEPropertyDef& modifiedDef = iLocationObjectDef->GetPropertyDefL(
   816 			Object::KLastModifiedDateProperty );
  1289 			Object::KLastModifiedDateProperty );
   817 	CMdEPropertyDef& sizeDef = locationObjectDef.GetPropertyDefL(
  1290 	CMdEPropertyDef& sizeDef = iLocationObjectDef->GetPropertyDefL(
   818 			Object::KSizeProperty );
  1291 			Object::KSizeProperty );
   819 	CMdEPropertyDef& itemTypeDef = locationObjectDef.GetPropertyDefL(
  1292 	CMdEPropertyDef& itemTypeDef = iLocationObjectDef->GetPropertyDefL(
   820 			Object::KItemTypeProperty );
  1293 			Object::KItemTypeProperty );
   821 	CMdEPropertyDef& offSetDef = locationObjectDef.GetPropertyDefL( 
  1294 	CMdEPropertyDef& offSetDef = iLocationObjectDef->GetPropertyDefL( 
   822 			Object::KTimeOffsetProperty );
  1295 			Object::KTimeOffsetProperty );
   823 
  1296 
   824 	// location related properties
  1297 	// location related properties
   825 	CMdEPropertyDef& cellIdDef = locationObjectDef.GetPropertyDefL(
  1298 	CMdEPropertyDef& cellIdDef = iLocationObjectDef->GetPropertyDefL(
   826 			Location::KCellIdProperty );
  1299 			Location::KCellIdProperty );
   827 	CMdEPropertyDef& latitudeDef = locationObjectDef.GetPropertyDefL(
  1300 	CMdEPropertyDef& directionDef = iLocationObjectDef->GetPropertyDefL(
   828 			Location::KLatitudeProperty );
       
   829 	CMdEPropertyDef& longitudeDef = locationObjectDef.GetPropertyDefL(
       
   830 			Location::KLongitudeProperty );
       
   831 	CMdEPropertyDef& altitudeDef = locationObjectDef.GetPropertyDefL(
       
   832 			Location::KAltitudeProperty );
       
   833 
       
   834 	CMdEPropertyDef& directionDef = locationObjectDef.GetPropertyDefL(
       
   835 			Location::KDirectionProperty );
  1301 			Location::KDirectionProperty );
   836 	CMdEPropertyDef& speedDef = locationObjectDef.GetPropertyDefL( 
  1302 	CMdEPropertyDef& speedDef = iLocationObjectDef->GetPropertyDefL( 
   837 			Location::KSpeedProperty );
  1303 			Location::KSpeedProperty );
   838 	CMdEPropertyDef& locationCodeDef = locationObjectDef.GetPropertyDefL( 
  1304 	CMdEPropertyDef& locationCodeDef = iLocationObjectDef->GetPropertyDefL( 
   839 			Location::KLocationAreaCodeProperty );
  1305 			Location::KLocationAreaCodeProperty );
   840 	CMdEPropertyDef& countryCodeDef = locationObjectDef.GetPropertyDefL( 
  1306 	CMdEPropertyDef& countryCodeDef = iLocationObjectDef->GetPropertyDefL( 
   841 			Location::KCountryCodeProperty );
  1307 			Location::KCountryCodeProperty );
   842 	CMdEPropertyDef& networkCodeDef = locationObjectDef.GetPropertyDefL( 
  1308 	CMdEPropertyDef& networkCodeDef = iLocationObjectDef->GetPropertyDefL( 
   843 			Location::KNetworkCodeProperty );
  1309 			Location::KNetworkCodeProperty );
   844 	CMdEPropertyDef& qualityDef = locationObjectDef.GetPropertyDefL( 
  1310 	CMdEPropertyDef& qualityDef = iLocationObjectDef->GetPropertyDefL( 
   845 			Location::KQualityProperty );
  1311 			Location::KQualityProperty );
   846 
  1312 
   847 	// location object
  1313 	// location object
   848 	CMdEObject* locationObject = NULL;
  1314 	CMdEObject* locationObject = NULL;
   849 
  1315 
   850 	locationObject = iMdeSession->NewObjectL( locationObjectDef, Object::KAutomaticUri );
  1316 	locationObject = iMdeSession->NewObjectL( *iLocationObjectDef, Object::KAutomaticUri );
   851 	CleanupStack::PushL( locationObject );
  1317 	CleanupStack::PushL( locationObject );
   852 
  1318 
   853 	TTime timestamp( 0 );
  1319 	TTime timestamp( 0 );
   854 	timestamp.UniversalTime();
  1320 	timestamp.UniversalTime();
   855 
  1321 
   861 	locationObject->AddTimePropertyL( modifiedDef, timestamp );
  1327 	locationObject->AddTimePropertyL( modifiedDef, timestamp );
   862 	locationObject->AddUint32PropertyL( sizeDef, 0 ); // always zero size for location objects
  1328 	locationObject->AddUint32PropertyL( sizeDef, 0 ); // always zero size for location objects
   863 	locationObject->AddTextPropertyL( itemTypeDef, Location::KLocationItemType );
  1329 	locationObject->AddTextPropertyL( itemTypeDef, Location::KLocationItemType );
   864 	locationObject->AddInt16PropertyL( offSetDef, timeOffset.Int() / 60 );
  1330 	locationObject->AddInt16PropertyL( offSetDef, timeOffset.Int() / 60 );
   865 	
  1331 	
   866 	LOG1( "CLocationRecord::DoCreateLocationL - location created with stamp: %Ld", timestamp.Int64() );
  1332 	LOG1( "Location created with stamp: %Ld", timestamp.Int64() );
   867 	
  1333 	
   868 	// location related properties
  1334 	// location related properties
   869 	if ( !Math::IsNaN( aLocationData.iPosition.Latitude() ) && 
  1335 	if ( !Math::IsNaN( aLocationData.iPosition.Latitude() ) && 
   870 		 !Math::IsNaN( aLocationData.iPosition.Longitude() ))
  1336 		 !Math::IsNaN( aLocationData.iPosition.Longitude() ))
   871 		{
  1337 		{
   872 		locationObject->AddReal64PropertyL( latitudeDef, aLocationData.iPosition.Latitude() );
  1338         LOG1("Lan - %f", aLocationData.iPosition.Latitude());
   873 		locationObject->AddReal64PropertyL( longitudeDef, aLocationData.iPosition.Longitude() );
  1339         LOG1("Lon - %f", aLocationData.iPosition.Longitude());
       
  1340 		locationObject->AddReal64PropertyL( *iLatitudeDef, aLocationData.iPosition.Latitude() );
       
  1341 		locationObject->AddReal64PropertyL( *iLongitudeDef, aLocationData.iPosition.Longitude() );
       
  1342 
   874 		}
  1343 		}
   875 	if ( !Math::IsNaN( aLocationData.iPosition.Altitude() ) )
  1344 	if ( !Math::IsNaN( aLocationData.iPosition.Altitude() ) )
   876 		{
  1345 		{
   877 		locationObject->AddReal64PropertyL( altitudeDef, aLocationData.iPosition.Altitude() );
  1346 		locationObject->AddReal64PropertyL( *iAltitudeDef, aLocationData.iPosition.Altitude() );
   878 		}
  1347 		}
   879 	if ( !Math::IsNaN( aLocationData.iCourse.Course() ) )
  1348 	if ( !Math::IsNaN( aLocationData.iCourse.Course() ) )
   880 		{
  1349 		{
   881 		locationObject->AddReal32PropertyL( directionDef, aLocationData.iCourse.Course() );
  1350 		locationObject->AddReal32PropertyL( directionDef, aLocationData.iCourse.Course() );
   882 		}
  1351 		}
   888 		{
  1357 		{
   889 		locationObject->AddReal32PropertyL( qualityDef, aLocationData.iQuality );
  1358 		locationObject->AddReal32PropertyL( qualityDef, aLocationData.iQuality );
   890 		}
  1359 		}
   891 
  1360 
   892 	// network related properties
  1361 	// network related properties
   893 	if ( aLocationData.iNetworkInfo.iAreaKnown )
  1362 	if ( aLocationData.iNetworkInfo.iAccess != CTelephony::ENetworkAccessUnknown )
   894 		{
  1363 		{
   895 		if ( aLocationData.iNetworkInfo.iAccess != CTelephony::ENetworkAccessUnknown )
  1364 		LOG1("Cell id - %d", aLocationData.iNetworkInfo.iCellId);
   896 			{
  1365 		locationObject->AddUint32PropertyL( cellIdDef, aLocationData.iNetworkInfo.iCellId );
   897 			locationObject->AddUint32PropertyL( cellIdDef, aLocationData.iNetworkInfo.iCellId );
       
   898 			
       
   899 			}
       
   900 		if ( aLocationData.iNetworkInfo.iLocationAreaCode != 0 &&
       
   901 			aLocationData.iNetworkInfo.iAccess != CTelephony::ENetworkAccessUnknown )
       
   902 			{
       
   903 			locationObject->AddUint32PropertyL( locationCodeDef, 
       
   904 					aLocationData.iNetworkInfo.iLocationAreaCode );
       
   905 			
       
   906 			}
       
   907 		if ( aLocationData.iNetworkInfo.iCountryCode.Length() > 0 )
       
   908 			{
       
   909 			locationObject->AddTextPropertyL( countryCodeDef, 
       
   910 					aLocationData.iNetworkInfo.iCountryCode );
       
   911 			
       
   912 			}
       
   913 		if ( aLocationData.iNetworkInfo.iNetworkId.Length() > 0 )
       
   914 			{
       
   915 			locationObject->AddTextPropertyL(networkCodeDef, aLocationData.iNetworkInfo.iNetworkId);
       
   916 			
       
   917 			}
       
   918 		}
  1366 		}
   919 
  1367 	if ( aLocationData.iNetworkInfo.iAreaKnown && 
       
  1368         aLocationData.iNetworkInfo.iLocationAreaCode != 0 &&
       
  1369 		aLocationData.iNetworkInfo.iAccess != CTelephony::ENetworkAccessUnknown )
       
  1370 		{
       
  1371 		LOG1("Areacode - %d", aLocationData.iNetworkInfo.iLocationAreaCode);
       
  1372 		locationObject->AddUint32PropertyL( locationCodeDef, 
       
  1373 				aLocationData.iNetworkInfo.iLocationAreaCode );
       
  1374 		}
       
  1375 #ifdef _DEBUG
       
  1376     TLex lexer( aLocationData.iNetworkInfo.iCountryCode );
       
  1377     TUint countryCode = 0;
       
  1378     
       
  1379     User::LeaveIfError( lexer.Val( countryCode, EDecimal) );
       
  1380     LOG1("Country code - %d", countryCode);
       
  1381     
       
  1382     //Set mobile network code
       
  1383     lexer = aLocationData.iNetworkInfo.iNetworkId;
       
  1384     TUint networkCode = 0;
       
  1385     User::LeaveIfError( lexer.Val( networkCode, EDecimal) );
       
  1386     LOG1("Network id - %d", networkCode);
       
  1387 #endif
       
  1388 	if ( aLocationData.iNetworkInfo.iCountryCode.Length() > 0 )
       
  1389 		{
       
  1390 		locationObject->AddTextPropertyL( countryCodeDef, 
       
  1391 				aLocationData.iNetworkInfo.iCountryCode );
       
  1392 		}
       
  1393 
       
  1394 	if ( aLocationData.iNetworkInfo.iNetworkId.Length() > 0 )
       
  1395 		{
       
  1396 		locationObject->AddTextPropertyL(networkCodeDef, aLocationData.iNetworkInfo.iNetworkId);
       
  1397 		}
       
  1398 	        
   920 	// Add the location object to the database.
  1399 	// Add the location object to the database.
   921 	locationObjectId = iMdeSession->AddObjectL( *locationObject );
  1400 	locationObjectId = iMdeSession->AddObjectL( *locationObject );
   922 
  1401     LOG1("Location id - %d", locationObjectId);
   923 	iLastLocationId = locationObjectId;
       
   924 	iLastLocation = aLocationData;
       
   925 
       
   926 	CleanupStack::PopAndDestroy( locationObject );
  1402 	CleanupStack::PopAndDestroy( locationObject );
   927 	
  1403     LOG( "CLocationRecord::DoCreateLocationL(), end" );
   928 	LOG("CLocationRecord::DoCreateLocationL - end");
  1404 
   929 	
       
   930 	return locationObjectId;
  1405 	return locationObjectId;
   931 	}
  1406 	}
   932 
  1407 
   933 
  1408 // --------------------------------------------------------------------------
       
  1409 // CLocationRecord::CreateRelationL
       
  1410 // --------------------------------------------------------------------------
       
  1411 //
   934 TItemId CLocationRecord::CreateRelationL( const TUint& aObjectId, const TUint& aLocationId )
  1412 TItemId CLocationRecord::CreateRelationL( const TUint& aObjectId, const TUint& aLocationId )
   935 	{ 
  1413 	{ 
   936 	LOG("CLocationRecord::CreateRelationL - start");
  1414 	LOG("CLocationRecord::CreateRelationL - start");
   937 	CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
  1415 	CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
   938 	
  1416 	
   966 	User::LeaveIfError(repository->Get( aKey, aValue));
  1444 	User::LeaveIfError(repository->Get( aKey, aValue));
   967 	CleanupStack::PopAndDestroy(repository);
  1445 	CleanupStack::PopAndDestroy(repository);
   968     LOG( "CLocationRecord::::ReadCenRepValueL(), end" );   
  1446     LOG( "CLocationRecord::::ReadCenRepValueL(), end" );   
   969 	}
  1447 	}
   970 
  1448 
       
  1449 
       
  1450 // --------------------------------------------------------------------------
       
  1451 // CLocationRecord::HandleQueryNewResults
       
  1452 // --------------------------------------------------------------------------
       
  1453 //
   971 void CLocationRecord::HandleQueryNewResults(CMdEQuery& /*aQuery*/, TInt /*aFirstNewItemIndex*/, 
  1454 void CLocationRecord::HandleQueryNewResults(CMdEQuery& /*aQuery*/, TInt /*aFirstNewItemIndex*/, 
   972 		TInt /*aNewItemCount*/)
  1455 		TInt /*aNewItemCount*/)
   973 	{
  1456 	{
   974 	}
  1457 	}
   975 
  1458 
       
  1459 
       
  1460 // --------------------------------------------------------------------------
       
  1461 // CLocationRecord::HandleQueryCompleted
       
  1462 // --------------------------------------------------------------------------
       
  1463 //
   976 void CLocationRecord::HandleQueryCompleted(CMdEQuery& aQuery, TInt aError)
  1464 void CLocationRecord::HandleQueryCompleted(CMdEQuery& aQuery, TInt aError)
   977     {
  1465     {
   978     LOG("CLocationRecord::HandleQueryCompleted - start");
  1466     LOG("CLocationRecord::HandleQueryCompleted - start");
   979     const TInt count = aQuery.Count();
  1467     const TInt count = aQuery.Count();
   980     LOG1("CLocationRecord::HandleQueryCompleted count: %d", count);
  1468     LOG1("CLocationRecord::HandleQueryCompleted count: %d", count);
   981 
  1469      
   982     CMdENamespaceDef* namespaceDef = NULL;
  1470     if ( aError != KErrNone )
   983 
  1471         {
   984     TRAP_IGNORE( namespaceDef = &iMdeSession->GetDefaultNamespaceDefL() );
  1472 		HandleQueryFailure();
   985     if ( namespaceDef )
  1473         }
   986         {
  1474     
   987         CMdEObjectDef* locationObjectDef = NULL;
  1475     else if ( &aQuery == iLocationQuery )
   988 
  1476         {
   989         TRAP_IGNORE( locationObjectDef = &namespaceDef->GetObjectDefL( Location::KLocationObject ) );
  1477      
   990         if ( locationObjectDef )
  1478       	TRAPD(err,HandleLocationQueryL( aQuery ));
   991         	{
  1479 		if(err != KErrNone)
   992         	CMdEPropertyDef* latitudeDef = NULL;
  1480 			{
   993         	CMdEPropertyDef* longitudeDef = NULL;
  1481 			// unable to process the first node..
   994         	CMdEPropertyDef* altitudeDef = NULL;
  1482 			// remove this and process the next.
   995         	
  1483 
   996             TRAP_IGNORE( 
  1484 			// reset the flag
   997             		latitudeDef = &locationObjectDef->GetPropertyDefL(
  1485 			HandleQueryFailure();
   998             				Location::KLatitudeProperty );
  1486 			}
   999             		longitudeDef = &locationObjectDef->GetPropertyDefL(	
  1487         
  1000             				Location::KLongitudeProperty );
  1488         }
  1001             		altitudeDef = &locationObjectDef->GetPropertyDefL( 
  1489     else if ( &aQuery == iNetLocationQuery )
  1002             				Location::KAltitudeProperty );
  1490         {
  1003             		);
  1491       	TRAPD(err,HandleNetLocationQueryL( aQuery ));
  1004 
  1492 		 if(err != KErrNone)
  1005             if( latitudeDef && longitudeDef && altitudeDef )
  1493 			 {
  1006             	{
  1494 			 // unable to process the first node..
  1007 	            TBool created = EFalse;
  1495 			 // remove this and process the next.
  1008 	            for ( TInt i = 0; i < count; i++ )
  1496 			 HandleQueryFailure();
  1009 	                {
  1497 			 }
  1010 	                LOG1("CLocationRecord::HandleQueryCompleted check item: %d", i);
  1498         
  1011 	                CMdEItem& item = aQuery.ResultItem(i);
  1499         }
  1012 	                CMdEObject& locationObject = static_cast<CMdEObject&>(item);
  1500 
  1013 	
  1501 #ifdef LOC_REVERSEGEOCODE
  1014 	                CMdEProperty* latProp = NULL;
  1502     else if ( &aQuery == iImageQuery )
  1015 	                CMdEProperty* lonProp = NULL; 
  1503         {
  1016 	                CMdEProperty* altProp = NULL;
  1504         if(aQuery.Count() > 0)
  1017 	
  1505             {
  1018 	                locationObject.Property( *latitudeDef, latProp, 0 );
  1506            
  1019 	                locationObject.Property( *longitudeDef, lonProp, 0 );
  1507             CMdERelation& relation = static_cast<CMdERelation&>( aQuery.ResultItem( 0 ) );
  1020 	                locationObject.Property( *altitudeDef, altProp, 0 );
  1508                                 
  1021 	
  1509             TItemId imageId = relation.LeftObjectId(); 
  1022 	                if ( latProp && lonProp )
  1510                 
  1023 	                    {
  1511             TRAPD(err, GetTagsL( imageId ) );
  1024 	                    TReal32 distance;
  1512 			if(err != KErrNone)
  1025 	                    TCoordinate newCoords;
  1513 				{
  1026 	                    if ( altProp )
  1514 				// unable to process the first node..
  1027 	                        {
  1515 				// remove this and process the next.
  1028 	                        TRAP_IGNORE( newCoords = TCoordinate( latProp->Real64ValueL(), lonProp->Real64ValueL(), (TReal32)altProp->Real64ValueL() ) );
  1516 			
  1029 	                        }
  1517 				// reset the flag
  1030 	                    else
  1518 				HandleQueryFailure();
  1031 	                        {
  1519 				}
  1032 	                        TRAP_IGNORE( newCoords = TCoordinate( latProp->Real64ValueL(), lonProp->Real64ValueL() ) );
  1520             }
  1033 	                        }
  1521         else
  1034 	                    
  1522             {
  1035 	                    const TInt err = iLocationData.iPosition.Distance(newCoords, distance);
  1523 			HandleQueryFailure();
  1036 	                    
  1524             }
  1037 	                    if ( distance < iLocationDelta )
  1525         }
  1038 	                        {
  1526     
  1039 	                        LOG("CLocationRecord::HandleQueryCompleted - match found in db");
  1527     else if ( &aQuery == iTagQuery )
  1040 	                        TRAPD( err, CreateRelationL( iObjectId, locationObject.Id() ) );
  1528         {
  1041 	                        if( err == KErrNone)
  1529         TRAPD(err, HandleTagQueryL( aQuery ) );
  1042 	                            {
  1530         
  1043 	                            created = ETrue;
  1531         GeoTaggingCompleted();    
  1044 	                            i = count;
  1532 		if(err != KErrNone)
  1045 	                            }
  1533 			{
  1046 	                        else
  1534 			// unable to process the first node..
  1047 	                            {
  1535 			// remove this and process the next.
  1048 	                            aError = err;
  1536 
  1049 	                            }
  1537 			// reset the flag
  1050 	                        }
  1538 			HandleQueryFailure();
  1051 	                    }
  1539 			}
  1052 	                }
  1540         }
  1053 
  1541 #endif //LOC_REVERSEGEOCODE
  1054 	            if ( !created && aError == KErrNone )
  1542 	else
  1055 	                {
  1543 		{
  1056 	                LOG("CLocationRecord::HandleQueryCompleted - no match found in db, create new");
  1544 		// execution should not come over here...still take recovery action.
  1057 	                TInt locationId( 0 );
  1545 		HandleQueryFailure();
  1058 	                TRAPD( err, locationId = DoCreateLocationL( iLocationData ) );
  1546 		}
  1059 	                LOG1("CLocationRecord::HandleQueryCompleted - DoCreateLocationL err: %d", err);
  1547     GeoTaggingCompleted();
  1060 	                if( err == KErrNone )
  1548     LOG( "CLocationRecord::HandleQueryCompleted(), end" );	
  1061 	                    {
  1549     }
  1062 	                    TRAP( err, CreateRelationL( iObjectId, locationId ));
  1550 
  1063 	                    LOG1("CLocationRecord::HandleQueryCompleted - CreateRelationL err: %d", err);
  1551 // --------------------------------------------------------------------------
  1064 	                    }
  1552 // CLocationRecord::HandleQueryFailure()
  1065 	                }
  1553 // handle MDS query sceenario
  1066             	}
  1554 // --------------------------------------------------------------------------
  1067             }
  1555 //
  1068         }
  1556 void CLocationRecord::HandleQueryFailure()
  1069 
  1557     {
  1070     LOG("CLocationRecord::HandleQueryCompleted - end");
  1558     LOG( "CLocationRecord::HandleQueryFailure(), begin" );    
  1071     }
  1559 	iMediaHandlingFlag &= ~KLocationQueryInProgress;
  1072 
  1560 	if ( iMediaItems.Count() > 0 )
       
  1561 		{
       
  1562 		TLocationSnapshotItem* firstPtr = iMediaItems[0];
       
  1563 		iMediaItems.Remove(0);
       
  1564 		delete firstPtr;
       
  1565 		iMediaItems.Compress();
       
  1566 		HandleFindLocationFromDB();
       
  1567 		}
       
  1568     LOG( "CLocationRecord::HandleQueryFailure(), end" );	
       
  1569 	}
       
  1570 
       
  1571 // --------------------------------------------------------------------------
       
  1572 // CLocationRecord::HandleFindLocationFromDB()
       
  1573 // handle find location from DB within the non leaving method
       
  1574 // --------------------------------------------------------------------------
       
  1575 //
       
  1576 void CLocationRecord::HandleFindLocationFromDB()
       
  1577     {
       
  1578     LOG( "CLocationRecord::HandleFindLocationFromDB(), begin" );    
       
  1579 	if ( iMediaItems.Count() > 0 )
       
  1580 	   {
       
  1581 		TInt trapErr = KErrNone;
       
  1582 		TRAP(trapErr,FindLocationFromDBL());
       
  1583 		// no memory, don't proceed further
       
  1584 		// other error sceenario, we can move the node to the end and process the next
       
  1585 		if(trapErr != KErrNoMemory && trapErr != KErrNone)
       
  1586 			{
       
  1587 			// other than no memory
       
  1588 			TInt numberOfNodes = iMediaItems.Count();
       
  1589 			LOG1("media count - %d\n", numberOfNodes);
       
  1590 			while(--numberOfNodes >= 0 &&
       
  1591 				trapErr != KErrNoMemory && 
       
  1592 				trapErr != KErrNone)
       
  1593 				{
       
  1594 				// first remove the node
       
  1595 				TLocationSnapshotItem* firstPtr = iMediaItems[0];
       
  1596 				iMediaItems.Remove(0);
       
  1597 				// move this to last
       
  1598 				iMediaItems.Append(firstPtr);
       
  1599 				iMediaItems.Compress();
       
  1600 				trapErr = KErrNone;
       
  1601 				// process for the next till we reached the last node.
       
  1602 				TRAP(trapErr,FindLocationFromDBL());
       
  1603 				}
       
  1604 			}
       
  1605 	   }
       
  1606     LOG( "CLocationRecord::HandleFindLocationFromDB(), end" );	
       
  1607 	}        
       
  1608         
       
  1609         
       
  1610 // --------------------------------------------------------------------------
       
  1611 // CLocationRecord::HandleLocationQuery()
       
  1612 // handle if only gps info available
       
  1613 // --------------------------------------------------------------------------
       
  1614 //
       
  1615 void CLocationRecord::HandleLocationQueryL( CMdEQuery& aQuery )
       
  1616     {
       
  1617     LOG( "CLocationRecord::HandleLocationQueryL(), begin" );    
       
  1618     TInt error = KErrNone;
       
  1619     TUint locationId( 0 );
       
  1620     if ( iMediaItems.Count() <= 0 )
       
  1621         {
       
  1622         LOG("CLocationRecord::HandleLocationQueryL. No media items to process");
       
  1623         return;
       
  1624         }
       
  1625     
       
  1626     TLocationData locationData = iMediaItems[0]->iLocationData;
       
  1627     
       
  1628     const TInt count = aQuery.Count();
       
  1629     //find any location matches
       
  1630     for ( TInt i = 0; i < count; i++ )
       
  1631         {
       
  1632         LOG1("CLocationRecord::HandleLocationQueryL check item: %d", i);
       
  1633         CMdEItem& item = aQuery.ResultItem(i);
       
  1634         CMdEObject& locationObject = static_cast<CMdEObject&>(item);
       
  1635         
       
  1636         CMdEProperty* latProp = NULL;
       
  1637         CMdEProperty* lonProp = NULL; 
       
  1638         CMdEProperty* altProp = NULL;
       
  1639         
       
  1640         locationObject.Property( *iLatitudeDef, latProp, 0 );
       
  1641         locationObject.Property( *iLongitudeDef, lonProp, 0 );
       
  1642         locationObject.Property( *iAltitudeDef, altProp, 0 );
       
  1643         
       
  1644         if ( latProp && lonProp )
       
  1645             {
       
  1646             TReal32 distance;
       
  1647             TCoordinate newCoords;
       
  1648                     
       
  1649             TReal64 lat = latProp->Real64ValueL();
       
  1650             TReal64 lon = lonProp->Real64ValueL();
       
  1651             if ( altProp )
       
  1652                 {
       
  1653                 TReal32 alt = (TReal32)altProp->Real64ValueL();                       
       
  1654                 newCoords = TCoordinate( lat, lon, alt );
       
  1655                 }
       
  1656             else
       
  1657                 {
       
  1658                 newCoords = TCoordinate( lat, lon );
       
  1659                 }
       
  1660                             
       
  1661             locationData.iPosition.Distance(newCoords, distance);
       
  1662                             
       
  1663             if ( distance < iLocationDelta )  
       
  1664                 {
       
  1665                  i = count;
       
  1666                  locationId = locationObject.Id();
       
  1667                  iMediaItems[0]->iLocationId = locationId;
       
  1668                  break;
       
  1669                 }
       
  1670             }
       
  1671         }
       
  1672            
       
  1673     
       
  1674     if ( locationId )
       
  1675         {
       
  1676         TRAP( error, CreateRelationL( iMediaItems[0]->iObjectId, locationId ) );
       
  1677         
       
  1678 #ifdef LOC_REVERSEGEOCODE
       
  1679         if( error == KErrNone)
       
  1680            {             
       
  1681            //find out if image for this location is tagged already
       
  1682            GetRelatedImageL( locationId ); 
       
  1683            }
       
  1684 #else        
       
  1685        TLocationSnapshotItem* firstPtr = iMediaItems[0];
       
  1686        iMediaItems.Remove(0);
       
  1687        delete firstPtr;
       
  1688        iMediaItems.Compress();
       
  1689        iMediaHandlingFlag &= ~KLocationQueryInProgress;
       
  1690        if ( iMediaItems.Count() > 0 )
       
  1691           {
       
  1692           FindLocationFromDBL();
       
  1693           }
       
  1694 #endif //LOC_REVERSEGEOCODE
       
  1695         }
       
  1696     else
       
  1697         {
       
  1698         
       
  1699         if((iMediaItems[0]->iFlag & KDownloadMediaFile) > 0)
       
  1700             {
       
  1701             locationData.iNetworkInfo.iCellId = 0;
       
  1702             locationData.iNetworkInfo.iAccess = CTelephony::ENetworkAccessUnknown;
       
  1703             locationData.iNetworkInfo.iLocationAreaCode = 0;
       
  1704             locationData.iNetworkInfo.iCountryCode.Zero();
       
  1705             locationData.iNetworkInfo.iNetworkId.Zero();
       
  1706             }
       
  1707         TRAP( error, locationId = DoCreateLocationL( locationData ) );
       
  1708 
       
  1709         if ( error == KErrNone )
       
  1710            {
       
  1711            iMediaItems[0]->iLocationId = locationId;
       
  1712            TRAP( error, CreateRelationL( iMediaItems[0]->iObjectId, locationId ));
       
  1713            }
       
  1714         
       
  1715         TLocationSnapshotItem* item = iMediaItems[0];
       
  1716         if((iMediaItems[0]->iFlag & KSnapMediaFile) > 0)
       
  1717             {
       
  1718             iLastMediaItem = *(iMediaItems[0]);
       
  1719             }
       
  1720         iMediaItems.Remove(0);
       
  1721         iMediaItems.Compress();
       
  1722         iMediaHandlingFlag &= ~KLocationQueryInProgress;
       
  1723         if ( error == KErrNone )
       
  1724            {
       
  1725            //Find the address by coordinate, results a call to ReverseGeocodeComplete()
       
  1726 #ifdef LOC_REVERSEGEOCODE
       
  1727            iLocationItems.Append( item );
       
  1728            if(!(iMediaHandlingFlag & KReverseGeoCodingInProgress))
       
  1729                {
       
  1730                iMediaHandlingFlag |= KReverseGeoCodingInProgress;
       
  1731 
       
  1732                if(iRevGeocoderPlugin)
       
  1733 			       {
       
  1734                    iRevGeocoderPlugin->GetAddressByCoordinateL
       
  1735 				        ( iLocationItems[0]->iLocationData.iPosition,
       
  1736                            iConnectionOption);
       
  1737 				   }
       
  1738                }
       
  1739 #else
       
  1740             // free resources
       
  1741             delete item;
       
  1742 #endif //LOC_REVERSEGEOCODE
       
  1743             }
       
  1744         else
       
  1745             {
       
  1746             // free resources
       
  1747             delete item;
       
  1748             }
       
  1749         
       
  1750         if ( iMediaItems.Count() > 0 )
       
  1751            {
       
  1752            FindLocationFromDBL();
       
  1753            }
       
  1754         }
       
  1755     LOG( "CLocationRecord::HandleLocationQueryL(), end" );
       
  1756 
       
  1757     }
       
  1758 
       
  1759 // --------------------------------------------------------------------------
       
  1760 // CLocationRecord::HandleNetLocationQuery()
       
  1761 // handle if only network info available
       
  1762 // --------------------------------------------------------------------------
       
  1763 //
       
  1764 void CLocationRecord::HandleNetLocationQueryL( CMdEQuery& aQuery )
       
  1765     {
       
  1766     LOG( "CLocationRecord::HandleNetLocationQueryL(), begin" );    
       
  1767     TInt error = KErrNone;
       
  1768     TUint locationId( 0 );
       
  1769     if ( iMediaItems.Count() <= 0 )
       
  1770         {
       
  1771         LOG("CLocationRecord::HandleNetLocationQueryL(), End. No media items to process\n");
       
  1772         return;
       
  1773         }
       
  1774     if( aQuery.Count() )
       
  1775         {
       
  1776         CMdEItem& item = aQuery.ResultItem(0);
       
  1777         CMdEObject& locationObject = static_cast<CMdEObject&>(item);
       
  1778         locationId = locationObject.Id();
       
  1779         
       
  1780         TRAP( error, CreateRelationL( iMediaItems[0]->iObjectId, locationId ) );
       
  1781 #ifdef LOC_REVERSEGEOCODE
       
  1782         //check if found location object has lat, long
       
  1783         CMdEProperty* latProp = NULL;
       
  1784         CMdEProperty* lonProp = NULL; 
       
  1785         CMdEProperty* cellIdProp = NULL;
       
  1786         CMdEProperty* areadCodeProp = NULL; 
       
  1787         CMdEProperty* countryProp = NULL;
       
  1788         CMdEProperty* networkCodeProp = NULL; 
       
  1789         if ( error == KErrNone )
       
  1790             {
       
  1791 
       
  1792             CMdEPropertyDef& cellIdDef = iLocationObjectDef->GetPropertyDefL(
       
  1793                            Location::KCellIdProperty );
       
  1794             CMdEPropertyDef& locationAreadCodeDef = iLocationObjectDef->GetPropertyDefL( 
       
  1795                            Location::KLocationAreaCodeProperty );
       
  1796             CMdEPropertyDef& countryCodeDef = iLocationObjectDef->GetPropertyDefL( 
       
  1797                            Location::KCountryCodeProperty );
       
  1798             CMdEPropertyDef& networkCodeDef = iLocationObjectDef->GetPropertyDefL( 
       
  1799                            Location::KNetworkCodeProperty );
       
  1800 
       
  1801             locationObject.Property( *iLatitudeDef, latProp, 0 );
       
  1802             locationObject.Property( *iLongitudeDef, lonProp, 0 );      
       
  1803 
       
  1804             locationObject.Property( cellIdDef, cellIdProp, 0 );
       
  1805             locationObject.Property( locationAreadCodeDef, areadCodeProp, 0 );      
       
  1806             locationObject.Property( countryCodeDef, countryProp, 0 );
       
  1807             locationObject.Property( networkCodeDef, networkCodeProp, 0 );      
       
  1808             } 
       
  1809         
       
  1810         if( (latProp && lonProp)
       
  1811             || (cellIdProp && areadCodeProp && countryProp && networkCodeProp) )
       
  1812             {
       
  1813             //find out if image for this location is tagged already
       
  1814             GetRelatedImageL( locationId );           
       
  1815             }
       
  1816         else
       
  1817 #endif //LOC_REVERSEGEOCODE
       
  1818             {
       
  1819             // no geo info.. remove the item and proceed for the next.
       
  1820             TLocationSnapshotItem* firstPtr = iMediaItems[0];
       
  1821             iMediaItems.Remove(0);
       
  1822             delete firstPtr;
       
  1823             iMediaItems.Compress();
       
  1824             iMediaHandlingFlag &= ~KLocationQueryInProgress;
       
  1825             if ( iMediaItems.Count() > 0 )
       
  1826                {
       
  1827                FindLocationFromDBL();
       
  1828                }
       
  1829             }
       
  1830         }
       
  1831     else
       
  1832         {
       
  1833 #ifdef LOC_GEOTAGGING_CELLID
       
  1834         LOG1("Media handling flag = %d", iMediaHandlingFlag);
       
  1835         if((iMediaHandlingFlag & KSnapGeoConvertInProgress) > 0)
       
  1836             {
       
  1837             iMediaHandlingFlag |= KSnapGeoConvertInPendingState;
       
  1838             }
       
  1839         else
       
  1840             {
       
  1841             // go for cell id based geo coding.
       
  1842             if(iGeoConverter == NULL)
       
  1843                {
       
  1844                iGeoConverter = CGeoConverter::NewL(*this);
       
  1845                }
       
  1846             iGeoConverter->ConvertL(iMediaItems[0]->iLocationData.iNetworkInfo);
       
  1847             iMediaHandlingFlag |= KSnapGeoConvertInProgress;
       
  1848             iMediaHandlingFlag &= ~KSnapGeoConvertInPendingState;
       
  1849             }
       
  1850 #else
       
  1851         // cell id based geo tagging is not supported.. go for remapping.
       
  1852         locationId = DoCreateLocationL( iMediaItems[0]->iLocationData );
       
  1853         iMediaItems[0]->iLocationId = locationId;
       
  1854         TItemId relationId = CreateRelationL( iMediaItems[0]->iObjectId, locationId );
       
  1855         TLocationSnapshotItem* firstPtr = iMediaItems[0];
       
  1856 
       
  1857         // Go for remapping.. get locationdata from trail with object time
       
  1858         TTime timestamp = GetMdeObjectTimeL( iMediaItems[0]->iObjectId );
       
  1859         TRemapItem remapItem;
       
  1860         remapItem.iObjectId = iMediaItems[0]->iObjectId;
       
  1861         remapItem.iTime = timestamp;
       
  1862         remapItem.iLocationId = locationId;
       
  1863         remapItem.iRelationId = relationId;
       
  1864         iRemapper->Append( remapItem );
       
  1865         
       
  1866         iMediaItems.Remove(0);
       
  1867         delete firstPtr;
       
  1868         iMediaItems.Compress();
       
  1869         iMediaHandlingFlag &= ~KLocationQueryInProgress;
       
  1870         if ( iMediaItems.Count() > 0 )
       
  1871             {
       
  1872             FindLocationFromDBL();
       
  1873             }
       
  1874 #endif
       
  1875         }
       
  1876 #ifdef LOC_REVERSEGEOCODE
       
  1877     if((iMediaHandlingFlag & KSnapGeoConvertInProgress) > 0 || 
       
  1878         (iMediaHandlingFlag & KReverseGeoCodingInProgress) > 0)
       
  1879 #else        
       
  1880     if((iMediaHandlingFlag & KSnapGeoConvertInProgress) > 0)
       
  1881 #endif //LOC_REVERSEGEOCODE
       
  1882         {
       
  1883         // stop n/w info change listener, since device may connect to n/w 
       
  1884         // and local trail will receive so many call backs on current n/w info change.
       
  1885         iNetworkInfoChangeListener->StopNwInfoChangeNotifier();
       
  1886         }
       
  1887     else
       
  1888         {
       
  1889         iNetworkInfoChangeListener->StartNwInfoChangeNotifier();
       
  1890         }
       
  1891     LOG( "CLocationRecord::HandleNetLocationQueryL(), end" );
       
  1892 
       
  1893     }     
       
  1894 
       
  1895 
       
  1896 #ifdef LOC_GEOTAGGING_CELLID	
       
  1897 // --------------------------------------------------------------------------
       
  1898 // CLocationRecord::ConversionCompletedL()
       
  1899 // --------------------------------------------------------------------------
       
  1900 //
       
  1901 void CLocationRecord::ConversionCompletedL( const TInt aError, TLocality& aPosition )
       
  1902     {
       
  1903 
       
  1904     LOG1("CLocationRecord::ConversionCompletedL, begin. Error - %d", aError);
       
  1905     // reset the flag first
       
  1906     iMediaHandlingFlag &= ~KSnapGeoConvertInProgress;
       
  1907     LOG1("iRemapState - %d", iRemapState);
       
  1908     if(aError == KErrNone)
       
  1909         {
       
  1910         iConvertRetry = ETrue;
       
  1911         LOG("Conversion completed successfully");
       
  1912         if(iRemapState == ERemapNwGeoConverterInProgress)
       
  1913             {
       
  1914             iNewItem.iLocationData.iPosition.SetCoordinate
       
  1915                 ( aPosition.Latitude(), aPosition.Longitude(), aPosition.Altitude());
       
  1916             iNewItem.iLocationData.iQuality = aPosition.HorizontalAccuracy();
       
  1917             TItemId locationId = DoCreateLocationL( iNewItem.iLocationData );
       
  1918             iRemapper->UpdateRelationsL( locationId ); 
       
  1919 #ifdef LOC_REVERSEGEOCODE
       
  1920             if(!(iMediaHandlingFlag & KReverseGeoCodingInProgress))
       
  1921                 {
       
  1922                 iRemapState = ERemapRevGeoCodeInProgress;
       
  1923                 //Find the address by coordinate, results a call to ReverseGeocodeComplete()
       
  1924                 iMediaHandlingFlag |= KReverseGeoCodingInProgress;
       
  1925 
       
  1926                 if(iRevGeocoderPlugin)
       
  1927                 	{
       
  1928                 	iRevGeocoderPlugin->GetAddressByCoordinateL
       
  1929                 	    ( iNewItem.iLocationData.iPosition, iConnectionOption ); 
       
  1930                 	}
       
  1931                 }
       
  1932             else
       
  1933                 {
       
  1934                 // remap in reverse geocoding pending state.
       
  1935                 iRemapState = ERemapRevGeoCodePending;
       
  1936                 }
       
  1937 #endif //LOC_REVERSEGEOCODE
       
  1938             }
       
  1939         else if ( iMediaItems.Count() > 0 )
       
  1940            {
       
  1941             
       
  1942            iMediaItems[0]->iLocationData.iPosition.SetCoordinate
       
  1943                 ( aPosition.Latitude(), aPosition.Longitude(), aPosition.Altitude());
       
  1944            iMediaItems[0]->iLocationData.iQuality = aPosition.HorizontalAccuracy();
       
  1945            TLocationSnapshotItem* item = iMediaItems[0];
       
  1946            TItemId locationId = DoCreateLocationL( iMediaItems[0]->iLocationData );
       
  1947            iMediaItems[0]->iLocationId = locationId;
       
  1948            TItemId relationId = CreateRelationL( iMediaItems[0]->iObjectId, locationId );
       
  1949 
       
  1950            // Go for remapping.. get locationdata from trail with object time
       
  1951            TTime timestamp = GetMdeObjectTimeL( iMediaItems[0]->iObjectId );
       
  1952            TRemapItem remapItem;
       
  1953            remapItem.iObjectId = iMediaItems[0]->iObjectId;
       
  1954            remapItem.iTime = timestamp;
       
  1955            remapItem.iLocationId = locationId;
       
  1956            remapItem.iRelationId = relationId;
       
  1957            iRemapper->Append( remapItem );
       
  1958 
       
  1959            if((iMediaItems[0]->iFlag & KSnapMediaFile) > 0)
       
  1960                {
       
  1961                iLastMediaItem = *(iMediaItems[0]);
       
  1962                }
       
  1963            iMediaItems.Remove(0);
       
  1964            iMediaItems.Compress();
       
  1965            iMediaHandlingFlag &= ~KLocationQueryInProgress;
       
  1966 #ifdef LOC_REVERSEGEOCODE
       
  1967            iLocationItems.Append( item );
       
  1968 #else
       
  1969            // free resource
       
  1970            delete item;
       
  1971 #endif
       
  1972            }
       
  1973        }
       
  1974     else
       
  1975         {
       
  1976         LOG1("Conversion error - %d", aError);
       
  1977         if(iConvertRetry)
       
  1978             {
       
  1979             LOG("Retry once");
       
  1980             iConvertRetry = EFalse;
       
  1981             if(iRemapState == ERemapNwGeoConverterInProgress)
       
  1982                 {
       
  1983                 iRemapState = ERemapNwGeoConverterPending;
       
  1984                 }
       
  1985             else if ( iMediaItems.Count() > 0 )
       
  1986                 {
       
  1987                 iMediaHandlingFlag |= KSnapGeoConvertInPendingState;
       
  1988                 }
       
  1989             }
       
  1990         else
       
  1991             {
       
  1992             if(iRemapState == ERemapNwGeoConverterInProgress)
       
  1993                 {
       
  1994                 iRemapState = ERemapProgressNone;
       
  1995                 }
       
  1996             else if ( iMediaItems.Count() > 0 )
       
  1997                 {
       
  1998                 TLocationSnapshotItem* item = iMediaItems[0];
       
  1999                 // Fails may be becuase of n/w reason..create location + relation so that we can handle at 3:00 AM.
       
  2000                 TItemId locationId = DoCreateLocationL( iMediaItems[0]->iLocationData );
       
  2001                 iMediaItems[0]->iLocationId = locationId;
       
  2002                 TItemId relationId = CreateRelationL( iMediaItems[0]->iObjectId, locationId );
       
  2003 
       
  2004                 // Go for remapping.. get locationdata from trail with object time
       
  2005                 TTime timestamp = GetMdeObjectTimeL( iMediaItems[0]->iObjectId );
       
  2006                 TRemapItem remapItem;
       
  2007                 remapItem.iObjectId = iMediaItems[0]->iObjectId;
       
  2008                 remapItem.iTime = timestamp;
       
  2009                 remapItem.iLocationId = locationId;
       
  2010                 remapItem.iRelationId = relationId;
       
  2011                 iRemapper->Append( remapItem );
       
  2012 
       
  2013                 iMediaItems.Remove(0);
       
  2014                 iMediaItems.Compress();
       
  2015                 iMediaHandlingFlag &= ~KLocationQueryInProgress;
       
  2016                 delete item;
       
  2017                 }
       
  2018             }
       
  2019 
       
  2020         }
       
  2021 
       
  2022     // check the remap item first.
       
  2023     if(iRemapState == ERemapNwGeoConverterPending)
       
  2024         {
       
  2025         // go for cell id based geo coding.
       
  2026         iRemapState = ERemapNwGeoConverterInProgress;
       
  2027         iGeoConverter->ConvertL(iNewItem.iLocationData.iNetworkInfo);
       
  2028         iMediaHandlingFlag |= KSnapGeoConvertInProgress;
       
  2029         }
       
  2030     //check for media queue pending request.
       
  2031     else if((iMediaHandlingFlag & KSnapGeoConvertInPendingState) > 0)
       
  2032          {
       
  2033          // previous media queue geo convert is in pending state.
       
  2034          // go for cell id based geo coding.
       
  2035          iGeoConverter->ConvertL(iMediaItems[0]->iLocationData.iNetworkInfo);
       
  2036          iMediaHandlingFlag |= KSnapGeoConvertInProgress;
       
  2037          iMediaHandlingFlag &= ~KSnapGeoConvertInPendingState;
       
  2038          }
       
  2039     // let's not use multiple access point.
       
  2040 #ifdef LOC_REVERSEGEOCODE
       
  2041     else if(!(iMediaHandlingFlag & KReverseGeoCodingInProgress) &&
       
  2042         iLocationItems.Count() > 0)
       
  2043        {
       
  2044        iMediaHandlingFlag |= KReverseGeoCodingInProgress;
       
  2045 
       
  2046        if(iRevGeocoderPlugin)
       
  2047            {
       
  2048            iRevGeocoderPlugin->GetAddressByCoordinateL
       
  2049                   ( iLocationItems[0]->iLocationData.iPosition,
       
  2050                     iConnectionOption);
       
  2051            }
       
  2052        } 
       
  2053     if((iMediaHandlingFlag & KSnapGeoConvertInProgress) > 0 || 
       
  2054         (iMediaHandlingFlag & KReverseGeoCodingInProgress) > 0)
       
  2055 #else
       
  2056     if((iMediaHandlingFlag & KSnapGeoConvertInProgress) > 0)
       
  2057 #endif //LOC_REVERSEGEOCODE
       
  2058         {
       
  2059         // stop n/w info change listener, since device may connect to n/w 
       
  2060         // and local trail will receive so many call backs on current n/w info change.
       
  2061         iNetworkInfoChangeListener->StopNwInfoChangeNotifier();
       
  2062         }
       
  2063     else
       
  2064         {
       
  2065         iNetworkInfoChangeListener->StartNwInfoChangeNotifier();
       
  2066         }
       
  2067     FindLocationFromDBL();
       
  2068     GeoTaggingCompleted();
       
  2069     LOG("CLocationRecord::ConversionCompletedL, end");
       
  2070     }
       
  2071 
       
  2072 
       
  2073 //------------------------------------------------------------------------
       
  2074 // CLocationRecord::HandleConversionError
       
  2075 //------------------------------------------------------------------------
       
  2076 //
       
  2077 void CLocationRecord::HandleConversionError(TInt aError)
       
  2078 	{
       
  2079     LOG( "CLocationRecord::HandleConversionError(), begin" );	
       
  2080     ARG_USED(aError);
       
  2081     if(iRemapState == ERemapNwGeoConverterInProgress)
       
  2082         {
       
  2083         iRemapState = ERemapProgressNone;
       
  2084         }
       
  2085     else if ( iMediaItems.Count() > 0 )
       
  2086         {
       
  2087         TLocationSnapshotItem* item = iMediaItems[0];
       
  2088         iMediaItems.Remove(0);
       
  2089         iMediaItems.Compress();
       
  2090         iMediaHandlingFlag &= ~KLocationQueryInProgress;
       
  2091         delete item;
       
  2092         }
       
  2093         // let's not use multiple access point.
       
  2094 #ifdef LOC_REVERSEGEOCODE
       
  2095     if(!(iMediaHandlingFlag & KReverseGeoCodingInProgress) &&
       
  2096         iLocationItems.Count() > 0)
       
  2097        {
       
  2098        iMediaHandlingFlag |= KReverseGeoCodingInProgress;
       
  2099 
       
  2100        if(iRevGeocoderPlugin)
       
  2101            {
       
  2102            TRAP_IGNORE(iRevGeocoderPlugin->GetAddressByCoordinateL
       
  2103                   ( iLocationItems[0]->iLocationData.iPosition,
       
  2104                     iConnectionOption));
       
  2105            }
       
  2106        } 
       
  2107     if((iMediaHandlingFlag & KSnapGeoConvertInProgress) > 0 || 
       
  2108         (iMediaHandlingFlag & KReverseGeoCodingInProgress) > 0)
       
  2109 #else
       
  2110     if((iMediaHandlingFlag & KSnapGeoConvertInProgress) > 0)
       
  2111 #endif //LOC_REVERSEGEOCODE
       
  2112         {
       
  2113         // stop n/w info change listener, since device may connect to n/w 
       
  2114         // and local trail will receive so many call backs on current n/w info change.
       
  2115         iNetworkInfoChangeListener->StopNwInfoChangeNotifier();
       
  2116         }
       
  2117     else
       
  2118         {
       
  2119         iNetworkInfoChangeListener->StartNwInfoChangeNotifier();
       
  2120         }
       
  2121     TRAP_IGNORE(FindLocationFromDBL());
       
  2122     GeoTaggingCompleted();
       
  2123     LOG( "CLocationRecord::HandleConversionError(), end" );	
       
  2124 	}
       
  2125 
       
  2126 #endif // LOC_GEOTAGGING_CELLID
       
  2127 
       
  2128 // --------------------------------------------------------------------------
       
  2129 // CLocationRecord::SetMdeSession
       
  2130 // --------------------------------------------------------------------------
       
  2131 //
  1073 EXPORT_C void CLocationRecord::SetMdeSession( CMdESession* aSession )
  2132 EXPORT_C void CLocationRecord::SetMdeSession( CMdESession* aSession )
  1074 	{
  2133 	{
       
  2134     LOG( "CLocationRecord::SetMdeSession(), begin" );	
  1075 	iMdeSession = aSession;
  2135 	iMdeSession = aSession;
       
  2136 #ifdef LOC_REVERSEGEOCODE
       
  2137 	iTagCreator->SetSession( aSession );
       
  2138 #endif
  1076 	TRAPD(err, iRemapper->InitialiseL( aSession ));
  2139 	TRAPD(err, iRemapper->InitialiseL( aSession ));
  1077 	if( err != KErrNone )
  2140 	if( err != KErrNone )
  1078 		{
  2141 		{
  1079 		delete iRemapper;
  2142 		delete iRemapper;
  1080 		iRemapper = NULL;
  2143 		iRemapper = NULL;
  1081 		}
  2144 		}
       
  2145 	TRAP(err, InitialiseL() );
       
  2146     LOG( "CLocationRecord::SetMdeSession(), end" );	
  1082 	}
  2147 	}
  1083 
  2148 
       
  2149 // --------------------------------------------------------------------------
       
  2150 // CLocationRecord::InitialiseL
       
  2151 // --------------------------------------------------------------------------
       
  2152 //
       
  2153 void CLocationRecord::InitialiseL()
       
  2154     {
       
  2155     LOG( "CLocationRecord::InitialiseL(), begin" );    
       
  2156     // namespace defaults
       
  2157     iNamespaceDef = &iMdeSession->GetDefaultNamespaceDefL();
       
  2158     
       
  2159     // location object definitions
       
  2160     iLocationObjectDef = &iNamespaceDef->GetObjectDefL( Location::KLocationObject );
       
  2161     iLatitudeDef = &iLocationObjectDef->GetPropertyDefL( Location::KLatitudeProperty );
       
  2162     iLongitudeDef = &iLocationObjectDef->GetPropertyDefL( Location::KLongitudeProperty );
       
  2163     iAltitudeDef = &iLocationObjectDef->GetPropertyDefL( Location::KAltitudeProperty );
       
  2164     LOG( "CLocationRecord::InitialiseL(), end" );	
       
  2165     }
       
  2166       
       
  2167 
       
  2168 // --------------------------------------------------------------------------
       
  2169 // CLocationRecord::StartTimerL
       
  2170 // --------------------------------------------------------------------------
       
  2171 //  
  1084 void CLocationRecord::StartTimerL()
  2172 void CLocationRecord::StartTimerL()
  1085 	{
  2173 	{
  1086 	LOG("CLocationRecord::StartTimerL");
  2174 	LOG("CLocationRecord::StartTimerL,begin");
  1087 	
  2175 	
  1088 	if( !iNetworkInfoTimer->IsActive() )
  2176 	if( !iNetworkInfoTimer->IsActive() )
  1089 	    {
  2177 	    {
  1090 	    iNetworkInfoTimer->Start( iInterval, iInterval, TCallBack( UpdateNetworkInfo, this ) );
  2178 	    iNetworkInfoTimer->Start( iInterval, iInterval, TCallBack( UpdateNetworkInfo, this ) );
  1091 	    }
  2179 	    }
       
  2180     LOG( "CLocationRecord::StartL(), end" );	
  1092 	}
  2181 	}
  1093 
  2182 
       
  2183 
       
  2184 // --------------------------------------------------------------------------
       
  2185 // CLocationRecord::GetMdeObjectTimeL
       
  2186 // --------------------------------------------------------------------------
       
  2187 //
  1094 TTime CLocationRecord::GetMdeObjectTimeL( TItemId aObjectId ) 
  2188 TTime CLocationRecord::GetMdeObjectTimeL( TItemId aObjectId ) 
  1095     {
  2189     {
       
  2190     LOG( "CLocationRecord::GetMdeObjectTimeL(), begin" );    
  1096     CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
  2191     CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
  1097 
  2192 
  1098     CMdEObjectDef& objectDef = namespaceDef.GetObjectDefL( Object::KBaseObject );
  2193     CMdEObjectDef& objectDef = namespaceDef.GetObjectDefL( Object::KBaseObject );
  1099     CMdEPropertyDef& timeDef = objectDef.GetPropertyDefL( Object::KLastModifiedDateProperty );
  2194     CMdEPropertyDef& timeDef = objectDef.GetPropertyDefL( Object::KLastModifiedDateProperty );
  1100 
  2195 
  1112     const TTime timeValue( property->TimeValueL() );
  2207     const TTime timeValue( property->TimeValueL() );
  1113     CleanupStack::PopAndDestroy( object );
  2208     CleanupStack::PopAndDestroy( object );
  1114     return timeValue;
  2209     return timeValue;
  1115     }
  2210     }
  1116 
  2211 
       
  2212 
       
  2213 
       
  2214 // --------------------------------------------------------------------------
       
  2215 // CLocationRecord::RemappingNeeded
       
  2216 // --------------------------------------------------------------------------
       
  2217 //
  1117 EXPORT_C TBool CLocationRecord::RemappingNeeded()
  2218 EXPORT_C TBool CLocationRecord::RemappingNeeded()
  1118 	{
  2219 	{
  1119 	return iRemapper->ItemsInQueue();
  2220 	return ( iRemapper->ItemsInQueue() 
       
  2221 	    && (iRemapState == ERemapProgressNone));
  1120 	}
  2222 	}
       
  2223 
       
  2224 
       
  2225 
       
  2226 
       
  2227 // --------------------------------------------------------------------------
       
  2228 // CLocationRecord::FindLocationWithSameNetInfoL()
       
  2229 // check any location object already exists with same network info
       
  2230 // --------------------------------------------------------------------------
       
  2231 //
       
  2232 void CLocationRecord::FindLocationWithSameNetInfoL()
       
  2233     {
       
  2234     LOG( "CLocationRecord::FindLocationWithSameNetInfoL(), begin" );   
       
  2235     if(iMediaItems[0]->iLocationData.iNetworkInfo.iCellId > 0 &&
       
  2236         iMediaItems[0]->iLocationData.iNetworkInfo.iCountryCode.Length() > 0 &&
       
  2237         iMediaItems[0]->iLocationData.iNetworkInfo.iNetworkId.Length() > 0)
       
  2238         {
       
  2239         CMdEPropertyDef& cellIdDef = iLocationObjectDef->GetPropertyDefL(
       
  2240                     Location::KCellIdProperty );
       
  2241         CMdEPropertyDef& locationCodeDef = iLocationObjectDef->GetPropertyDefL( 
       
  2242                     Location::KLocationAreaCodeProperty );
       
  2243         CMdEPropertyDef& countryCodeDef = iLocationObjectDef->GetPropertyDefL( 
       
  2244                     Location::KCountryCodeProperty );
       
  2245         CMdEPropertyDef& networkCodeDef = iLocationObjectDef->GetPropertyDefL( 
       
  2246                     Location::KNetworkCodeProperty );
       
  2247 
       
  2248         if(iNetLocationQuery)
       
  2249             {
       
  2250             iNetLocationQuery->RemoveObserver(*this);
       
  2251             iNetLocationQuery->Cancel();
       
  2252             delete iNetLocationQuery;
       
  2253             iNetLocationQuery = NULL;
       
  2254             }
       
  2255         iNetLocationQuery = iMdeSession->NewObjectQueryL( *iNamespaceDef, *iLocationObjectDef, this );
       
  2256         CMdELogicCondition& cond = iNetLocationQuery->Conditions();
       
  2257         cond.SetOperator( ELogicConditionOperatorAnd );
       
  2258             
       
  2259         cond.AddPropertyConditionL( cellIdDef, 
       
  2260                     TMdEUintEqual( iMediaItems[0]->iLocationData.iNetworkInfo.iCellId) );
       
  2261         cond.AddPropertyConditionL( locationCodeDef, 
       
  2262                     TMdEUintEqual( iMediaItems[0]->iLocationData.iNetworkInfo.iLocationAreaCode) );
       
  2263         cond.AddPropertyConditionL( countryCodeDef, ETextPropertyConditionCompareEquals,
       
  2264                 iMediaItems[0]->iLocationData.iNetworkInfo.iCountryCode );
       
  2265         cond.AddPropertyConditionL( networkCodeDef, ETextPropertyConditionCompareEquals,
       
  2266                 iMediaItems[0]->iLocationData.iNetworkInfo.iNetworkId );
       
  2267             
       
  2268         iNetLocationQuery->FindL(1, 1); 
       
  2269         iMediaHandlingFlag |= KLocationQueryInProgress;
       
  2270         }
       
  2271     else
       
  2272         {
       
  2273         TLocationSnapshotItem* firstPtr = iMediaItems[0];
       
  2274         iMediaItems.Remove(0);
       
  2275         delete firstPtr;
       
  2276         iMediaItems.Compress();
       
  2277         iMediaHandlingFlag &= ~KLocationQueryInProgress;
       
  2278         FindLocationFromDBL();
       
  2279         }
       
  2280     LOG( "CLocationRecord::FindLocationWithSameNetInfoL(), end" );
       
  2281 
       
  2282    }
  1121 
  2283 
  1122 EXPORT_C TBool CLocationRecord::IsLowBattery()
  2284 EXPORT_C TBool CLocationRecord::IsLowBattery()
  1123     {
  2285     {
  1124     LOG("CLocationRecord::IsLowBattery()");
  2286     LOG("CLocationRecord::IsLowBattery()");
  1125     RProperty batteryProperty;
  2287     RProperty batteryProperty;
  1133         }
  2295         }
  1134     else
  2296     else
  1135         {
  2297         {
  1136         return ETrue;
  2298         return ETrue;
  1137         }
  2299         }
  1138     
  2300     }
  1139     }
  2301 
       
  2302 // --------------------------------------------------------------------------
       
  2303 // CLocationRecord::GeoTaggingCompleted
       
  2304 // --------------------------------------------------------------------------
       
  2305 //	
       
  2306 void CLocationRecord::GeoTaggingCompleted()
       
  2307     {
       
  2308     LOG( "CLocationRecord::GeoTaggingCompleted(), begin" );    
       
  2309 	if((iMediaItems.Count() ==  0) 
       
  2310 #ifdef LOC_REVERSEGEOCODE
       
  2311         && (iLocationItems.Count() == 0) 
       
  2312 #endif //LOC_REVERSEGEOCODE
       
  2313         )
       
  2314 	    {
       
  2315         LOG("Geo tagging completed");
       
  2316 	    iGeoTaggerObserver.GeoTaggingCompleted(KErrNone);
       
  2317 		// fallback to silent
       
  2318 #ifdef LOC_REVERSEGEOCODE
       
  2319 		iConnectionOption = ESilent;
       
  2320 #endif //LOC_REVERSEGEOCODE
       
  2321 	    }
       
  2322     LOG( "CLocationRecord::GeoTaggingCompleted(), end" );	
       
  2323     }
       
  2324 
       
  2325 
       
  2326 
       
  2327 
       
  2328 // --------------------------------------------------------------------------
       
  2329 // CLocationRecord::TaggingInProgress
       
  2330 // --------------------------------------------------------------------------
       
  2331 //
       
  2332 EXPORT_C TBool CLocationRecord::TaggingInProgress()
       
  2333 	{
       
  2334     LOG( "CLocationRecord::TaggingInProgress(), begin" );	
       
  2335 	TBool retVal = EFalse;
       
  2336 #ifdef LOC_REVERSEGEOCODE
       
  2337     if( ((iMediaItems.Count() >  0) || (iLocationItems.Count() > 0) )
       
  2338         && (iRevGeocoderPlugin && iRevGeocoderPlugin->SilentConnectionAllowed()))
       
  2339 #else        
       
  2340     if( iMediaItems.Count() >  0 )
       
  2341 
       
  2342 #endif //LOC_REVERSEGEOCODE
       
  2343         {
       
  2344 #ifdef LOC_REVERSEGEOCODE
       
  2345         if(!(iMediaHandlingFlag & KReverseGeoCodingInProgress))
       
  2346             {
       
  2347             // start geocoding
       
  2348            
       
  2349             if (iLocationItems.Count() > 0 )
       
  2350                 {
       
  2351                 iMediaHandlingFlag |= KReverseGeoCodingInProgress;
       
  2352 
       
  2353             	TRAP_IGNORE( iRevGeocoderPlugin->GetAddressByCoordinateL( 
       
  2354                             iLocationItems[0]->iLocationData.iPosition,
       
  2355                             iConnectionOption) );
       
  2356 				
       
  2357                 retVal = ETrue;
       
  2358                 }
       
  2359             }
       
  2360         else
       
  2361             {
       
  2362             retVal = ETrue;
       
  2363             }
       
  2364 #endif //LOC_REVERSEGEOCODE
       
  2365         if(!(iMediaHandlingFlag & KLocationQueryInProgress))
       
  2366             {
       
  2367             if(iMediaItems.Count() > 0)
       
  2368                 {
       
  2369 				HandleFindLocationFromDB();
       
  2370                 retVal = ETrue;
       
  2371                 }
       
  2372             }
       
  2373         else
       
  2374             {
       
  2375             retVal = ETrue;
       
  2376             }
       
  2377         }
       
  2378     else
       
  2379         {
       
  2380         // Flash the array to avoid double tagging by photos & localrail.
       
  2381         }
       
  2382     LOG( "CLocationRecord::TaggingInProgress(), end" );	
       
  2383     return retVal;
       
  2384 	}
       
  2385 
       
  2386 
       
  2387 // ----------------------------------------------------------------------------
       
  2388 // CLocationRecord::GetCurrentRegisteredNw()
       
  2389 // ---------------------------------------------------------------------------- 
       
  2390 EXPORT_C RMobilePhone::TMobilePhoneNetworkInfoV2& CLocationRecord::GetCurrentRegisteredNw()
       
  2391     {
       
  2392     LOG( "CLocationRecord::GetCurrentRegisteredNw ,begin" ); 
       
  2393     return iNetworkInfoChangeListener->GetCurrentRegisterNw();
       
  2394     }
       
  2395 
       
  2396 // --------------------------------------------------------------------------
       
  2397 // CLocationRecord::StartGeoTagging
       
  2398 // --------------------------------------------------------------------------
       
  2399 //
       
  2400 EXPORT_C TBool CLocationRecord::StartGeoTagging(const TConnectionOption aConnectionOption)
       
  2401 	{
       
  2402     LOG( "CLocationRecord::StartGeoTagging(), begin" );	
       
  2403 	TBool retVal = EFalse;
       
  2404     ARG_USED(aConnectionOption);
       
  2405  	if((iMediaItems.Count() >  0) 
       
  2406 #ifdef LOC_REVERSEGEOCODE
       
  2407         || (iLocationItems.Count() > 0) 
       
  2408 #endif //LOC_REVERSEGEOCODE
       
  2409         )
       
  2410         {
       
  2411 #ifdef LOC_REVERSEGEOCODE
       
  2412         iConnectionOption = aConnectionOption;
       
  2413         if(!(iMediaHandlingFlag & KReverseGeoCodingInProgress))
       
  2414             {
       
  2415             // start geocoding
       
  2416             if (iLocationItems.Count() > 0 )
       
  2417                 {
       
  2418                 iMediaHandlingFlag |= KReverseGeoCodingInProgress;
       
  2419                 if(iRevGeocoderPlugin)
       
  2420                 	{
       
  2421                		 TRAP_IGNORE( iRevGeocoderPlugin->GetAddressByCoordinateL( 
       
  2422                                     iLocationItems[0]->iLocationData.iPosition,
       
  2423                                     iConnectionOption) );
       
  2424                 	}
       
  2425                 retVal = ETrue;
       
  2426                 }
       
  2427             }
       
  2428         else
       
  2429             {
       
  2430             retVal = ETrue;
       
  2431             }
       
  2432 #endif //LOC_REVERSEGEOCODE
       
  2433         if(!(iMediaHandlingFlag & KLocationQueryInProgress))
       
  2434             {
       
  2435             if(iMediaItems.Count() > 0)
       
  2436                 {
       
  2437 				HandleFindLocationFromDB();
       
  2438                 retVal = ETrue;
       
  2439                 }
       
  2440             }
       
  2441         else
       
  2442             {
       
  2443             retVal = ETrue;
       
  2444             }
       
  2445         }
       
  2446     LOG( "CLocationRecord::StartGeoTagging(), end" );	
       
  2447     return retVal;
       
  2448 	}
       
  2449 
       
  2450 
       
  2451 // --------------------------------------------------------------------------
       
  2452 // CLocationRecord::CancelGeoTagging
       
  2453 // --------------------------------------------------------------------------
       
  2454 //
       
  2455 EXPORT_C void CLocationRecord::CancelGeoTagging()
       
  2456 	{
       
  2457 #ifdef LOC_REVERSEGEOCODE
       
  2458     LOG1( "CLocationRecord::CancelGeoTagging(), Connetion opt - %d", 
       
  2459 	             iConnectionOption );	
       
  2460 	// set this to default connection.
       
  2461 	iConnectionOption = ESilent;
       
  2462 #endif //LOC_REVERSEGEOCODE
       
  2463     }
       
  2464 
       
  2465 
       
  2466 #ifdef LOC_REVERSEGEOCODE
       
  2467 
       
  2468 // --------------------------------------------------------------------------
       
  2469 // CLocationManagerServer::GetRelatedImages()
       
  2470 // Find any image, already related to this location object
       
  2471 // --------------------------------------------------------------------------
       
  2472 //
       
  2473 void CLocationRecord::GetRelatedImageL(TItemId aLocID)
       
  2474     {
       
  2475     LOG( "CLocationRecord::GetRelatedImageL(), begin" );    
       
  2476     CMdEObjectDef& imageObjDef = iNamespaceDef->GetObjectDefL( Image::KImageObject );
       
  2477     
       
  2478 	if(iImageQuery)
       
  2479 		{
       
  2480 		iImageQuery->RemoveObserver(*this);
       
  2481 		iImageQuery->Cancel();
       
  2482 		delete iImageQuery;
       
  2483         iImageQuery = NULL;
       
  2484 		}
       
  2485     
       
  2486     iImageQuery = iMdeSession->NewRelationQueryL( *iNamespaceDef, this );
       
  2487     User::LeaveIfNull( iImageQuery );
       
  2488 
       
  2489     iImageQuery->SetResultMode( EQueryResultModeItem );
       
  2490     
       
  2491     // both left and right condition must match
       
  2492     CMdERelationCondition& filterCond = iImageQuery->Conditions().
       
  2493             AddRelationConditionL( ERelationConditionSideRight );
       
  2494 
       
  2495     // left one must be any image object.
       
  2496     filterCond.LeftL().AddObjectConditionL( imageObjDef );
       
  2497 
       
  2498     // right one must be this location object 
       
  2499     filterCond.RightL().AddObjectConditionL( aLocID );
       
  2500 
       
  2501     iImageQuery->FindL(1, 1); // results to a call to HandleQueryCompleted()
       
  2502     LOG( "CLocationRecord::GetRelatedImageL(), end" );    
       
  2503     }
       
  2504 
       
  2505 
       
  2506 
       
  2507 // --------------------------------------------------------------------------
       
  2508 // CLocationRecord::ReverseGeocodeComplete()
       
  2509 // Get address details like country, city..
       
  2510 // Create country and city tags and attach to the current image/video object
       
  2511 // --------------------------------------------------------------------------
       
  2512 //
       
  2513 void CLocationRecord::ReverseGeocodeComplete( TInt& aErrorcode, MAddressInfo& aAddressInfo )
       
  2514     {
       
  2515     LOG( "CLocationRecord::ReverseGeocodeComplete(), begin" );    
       
  2516     TItemId countryTagId(0);
       
  2517     TItemId cityTagId(0);
       
  2518     
       
  2519     iMediaHandlingFlag &= (~KReverseGeoCodingInProgress);
       
  2520 	TLocationSnapshotItem* snapshotItem = NULL;
       
  2521     LOG1("Error - %d", aErrorcode);
       
  2522     if( aErrorcode == KErrNone )
       
  2523         {
       
  2524         iLastReverseGeocodeFails = EFalse;
       
  2525         TPtrC countryPtr( aAddressInfo.GetCountryName() );
       
  2526         TPtrC cityPtr( aAddressInfo.GetCity() );
       
  2527         TRAP_IGNORE( iTagCreator->CreateLocationTagsL( countryPtr, countryTagId, 
       
  2528                                                       cityPtr, cityTagId ) );
       
  2529         if ( iRemapState == ERemapRevGeoCodeInProgress)
       
  2530             {
       
  2531             TRAP_IGNORE( iRemapper->AttachGeoTagsL( iTagCreator, countryTagId, cityTagId ) );
       
  2532             iRemapState = ERemapProgressNone;
       
  2533             }
       
  2534         else
       
  2535             {
       
  2536             if(iLocationItems.Count() > 0)
       
  2537                 {
       
  2538                 iLocationItems[0]->iCountryTagId = countryTagId;
       
  2539                 iLocationItems[0]->iCityTagId = cityTagId;
       
  2540                 iLastLocationItem = (*iLocationItems[0]);
       
  2541                 
       
  2542                 TRAP_IGNORE( iTagCreator->AttachTagsL( 
       
  2543                                 iLocationItems[0]->iObjectId, countryTagId, cityTagId  ) );
       
  2544                 if ( (iLastMediaItem.iFlag & KSnapMediaFile) > 0 
       
  2545                     && iLastMediaItem.iLocationId == iLastLocationItem.iLocationId )
       
  2546                     {
       
  2547                     LOG("Updating country/city\n");
       
  2548                     iLastMediaItem.iCountryTagId = countryTagId;
       
  2549                     iLastMediaItem.iCityTagId = cityTagId;
       
  2550                     }
       
  2551                 
       
  2552                 }
       
  2553             }
       
  2554         //check other items in the array has same location 
       
  2555         for ( TInt index = iLocationItems.Count() - 1; index > 0; index--)
       
  2556             {
       
  2557             if ( iLocationItems[index]->iLocationId == iLastLocationItem.iLocationId )
       
  2558                 {
       
  2559                 LOG1("Attached tags in for - %d\n", index);
       
  2560                 TRAP_IGNORE( iTagCreator->AttachTagsL( 
       
  2561                                 iLocationItems[index]->iObjectId, countryTagId, cityTagId  ) );
       
  2562 				snapshotItem = iLocationItems[index];
       
  2563                 iLocationItems.Remove(index);
       
  2564 				delete snapshotItem;
       
  2565                 }
       
  2566             }
       
  2567         }
       
  2568     else
       
  2569         {
       
  2570         //handle error
       
  2571         LOG("Reverse geo coding fails");
       
  2572         iLastReverseGeocodeFails = ETrue;
       
  2573         }
       
  2574     
       
  2575     //irrespective of error or not, remove current(first) item to proceed further
       
  2576     if ( iLocationItems.Count() > 0 )
       
  2577         {
       
  2578     	snapshotItem = iLocationItems[0];
       
  2579         iLocationItems.Remove(0); 
       
  2580     	delete snapshotItem;
       
  2581     	iLocationItems.Compress();
       
  2582         }
       
  2583     if (aErrorcode == KErrNone)
       
  2584         {
       
  2585         if(iRemapState == ERemapRevGeoCodePending)
       
  2586             {
       
  2587             if(iRevGeocoderPlugin)
       
  2588             	{
       
  2589             	TRAPD(err, iRevGeocoderPlugin->GetAddressByCoordinateL( iNewItem.iLocationData.iPosition, iConnectionOption );) 
       
  2590             	if(err == KErrNone)
       
  2591             		{
       
  2592             		// Remap geo coding in pending state
       
  2593             		iRemapState = ERemapRevGeoCodeInProgress;
       
  2594             		//Find the address by coordinate, results a call to ReverseGeocodeComplete()
       
  2595             		iMediaHandlingFlag |= KReverseGeoCodingInProgress;
       
  2596             		}
       
  2597             	}
       
  2598             }
       
  2599         else if(iLocationItems.Count() > 0)
       
  2600             {
       
  2601             if(iRevGeocoderPlugin)
       
  2602             	{
       
  2603            		TRAPD(err, iRevGeocoderPlugin->GetAddressByCoordinateL( 
       
  2604                                 iLocationItems[0]->iLocationData.iPosition,
       
  2605                                 iConnectionOption) );
       
  2606             	if(err == KErrNone)
       
  2607             		{
       
  2608             		// queue is not empty process the next.
       
  2609             		iMediaHandlingFlag |= KReverseGeoCodingInProgress;
       
  2610             		}
       
  2611             	}
       
  2612             }
       
  2613         }
       
  2614 
       
  2615     GeoTaggingCompleted();
       
  2616     if((iMediaHandlingFlag & KSnapGeoConvertInProgress) > 0 || 
       
  2617         (iMediaHandlingFlag & KReverseGeoCodingInProgress) > 0)
       
  2618         {
       
  2619         // stop n/w info change listener, since device may connect to n/w 
       
  2620         // and local trail will receive so many call backs on current n/w info change.
       
  2621         iNetworkInfoChangeListener->StopNwInfoChangeNotifier();
       
  2622         }
       
  2623     else
       
  2624         {
       
  2625         iNetworkInfoChangeListener->StartNwInfoChangeNotifier();
       
  2626         }
       
  2627     LOG( "CLocationRecord::ReverseGeocodeComplete(), end" );	
       
  2628     }
       
  2629 
       
  2630 
       
  2631 // ----------------------------------------------------------------------------
       
  2632 // CLocationRecord::IsRegisteredAtHomeNetwork()
       
  2633 // ---------------------------------------------------------------------------- 
       
  2634 TBool CLocationRecord::IsRegisteredAtHomeNetwork()
       
  2635     {
       
  2636     LOG( "CLocationRecord::IsRegisteredAtHomeNetwork" );
       
  2637     return iGeoTaggerObserver.IsRegisteredAtHomeNetwork();
       
  2638     }
       
  2639 
       
  2640 // ----------------------------------------------------------------------------
       
  2641 // CLocationRecord::GetHomeNetworkInfo()
       
  2642 // ----------------------------------------------------------------------------
       
  2643 const RMobilePhone::TMobilePhoneNetworkInfoV1& 
       
  2644         CLocationRecord::GetHomeNetworkInfo(TBool& aHomeNwInfoAvailableFlag)
       
  2645     {
       
  2646     LOG( "CLocationRecord::GetHomeNetworkInfo" );
       
  2647     return iGeoTaggerObserver.GetHomeNetworkInfo(aHomeNwInfoAvailableFlag);
       
  2648     }
       
  2649 
       
  2650 // ----------------------------------------------------------------------------
       
  2651 // CLocationRecord::GetCurrentRegisterNw()
       
  2652 // ---------------------------------------------------------------------------- 
       
  2653 RMobilePhone::TMobilePhoneNetworkInfoV2& CLocationRecord::GetCurrentRegisterNw()
       
  2654     {
       
  2655     LOG( "CLocationRecord::GetCurrentRegisterNw ,begin" ); 
       
  2656     return GetCurrentRegisteredNw();
       
  2657     }
       
  2658 
       
  2659 
       
  2660 // --------------------------------------------------------------------------
       
  2661 // CLocationRecord::HandleTagQuery()
       
  2662 // handle if only gps info available
       
  2663 // --------------------------------------------------------------------------
       
  2664 //
       
  2665 void CLocationRecord::HandleTagQueryL( CMdEQuery& aQuery )
       
  2666     {
       
  2667     LOG( "CLocationRecord::HandleTagQueryL(), begin" );    
       
  2668     TItemId countryTagId = 0;
       
  2669     TItemId cityTagId = 0;
       
  2670     TRAPD( error, FindCountryAndCityTagL( aQuery, countryTagId, cityTagId ) );
       
  2671     if ( error == KErrNone )
       
  2672         {
       
  2673         LOG1("Media count - %d\n", iMediaItems.Count());
       
  2674         if ( !countryTagId && !cityTagId )
       
  2675             {
       
  2676              if ( iMediaItems.Count() > 0 )
       
  2677                 {
       
  2678                 iLocationItems.Append( iMediaItems[0] );
       
  2679                 if((iMediaItems[0]->iFlag & KSnapMediaFile) > 0)
       
  2680                     {
       
  2681                     iLastMediaItem = *(iMediaItems[0]);
       
  2682                     }
       
  2683                 iMediaItems.Remove(0);
       
  2684                 iMediaItems.Compress();
       
  2685                 iMediaHandlingFlag &= ~KLocationQueryInProgress;
       
  2686                 }
       
  2687 
       
  2688 #ifdef LOC_REVERSEGEOCODE
       
  2689             if(!(iMediaHandlingFlag & KReverseGeoCodingInProgress) &&
       
  2690                 iLocationItems.Count() > 0)
       
  2691                {
       
  2692                iMediaHandlingFlag |= KReverseGeoCodingInProgress;
       
  2693                if(iRevGeocoderPlugin)
       
  2694 			       {
       
  2695                    iRevGeocoderPlugin->GetAddressByCoordinateL
       
  2696 				          ( iLocationItems[0]->iLocationData.iPosition,
       
  2697                             iConnectionOption);
       
  2698 				   }
       
  2699                } 
       
  2700 #endif //LOC_REVERSEGEOCODE
       
  2701             }
       
  2702         else 
       
  2703             {
       
  2704 
       
  2705             iTagCreator->AttachTagsL( iMediaItems[0]->iObjectId, countryTagId, cityTagId );
       
  2706             if ( iMediaItems.Count() > 0 )
       
  2707                 {
       
  2708                 iMediaItems[0]->iCountryTagId = countryTagId;
       
  2709                 iMediaItems[0]->iCityTagId = cityTagId;
       
  2710                 TLocationSnapshotItem* item = iMediaItems[0];
       
  2711                 if((iMediaItems[0]->iFlag & KSnapMediaFile) > 0)
       
  2712                     {
       
  2713     				iLastMediaItem = *item;
       
  2714                     }
       
  2715                 iMediaItems.Remove(0);
       
  2716                 iMediaItems.Compress();
       
  2717                 iMediaHandlingFlag &= ~KLocationQueryInProgress;
       
  2718                 delete item;
       
  2719                 }
       
  2720             }
       
  2721         
       
  2722         }
       
  2723     if ( iMediaItems.Count() > 0 )
       
  2724         {
       
  2725         FindLocationFromDBL();
       
  2726         }
       
  2727     LOG( "CLocationRecord::HandleTagQueryL(), end" );	
       
  2728     }
       
  2729 
       
  2730 
       
  2731 // --------------------------------------------------------------------------
       
  2732 // CLocationRecord::FindCountryAndCityTagL()
       
  2733 // Go through all attached tags to get location tags only
       
  2734 // --------------------------------------------------------------------------
       
  2735 //
       
  2736 void CLocationRecord::FindCountryAndCityTagL( CMdEQuery& aQuery, 
       
  2737                                              TItemId& aCountryTagId, TItemId& aCityTagId )
       
  2738     {
       
  2739     LOG( "CLocationRecord::FindCountryAndCityTagL(), begin" );    
       
  2740     TItemId tagId = 0;
       
  2741     CMdEObject* object = NULL;
       
  2742     TInt error = KErrNone;
       
  2743     
       
  2744     const TInt count = aQuery.Count();
       
  2745                 
       
  2746     for ( TInt i = 0; i < count; i++ )
       
  2747         {
       
  2748         CMdERelation& relation =  static_cast<CMdERelation&>( aQuery.ResultItem( i ) );
       
  2749                     
       
  2750         tagId = relation.RightObjectId(); 
       
  2751         TRAP_IGNORE( object = iMdeSession->GetObjectL( tagId ) );
       
  2752         
       
  2753         if ( !aCountryTagId )
       
  2754             {
       
  2755             error = object->Uri().Find( KCountry );
       
  2756             //just make sure 'country' appears first in the tag uri
       
  2757             if ( error == KErrNone )
       
  2758                 {
       
  2759                 aCountryTagId = tagId;
       
  2760                 continue;
       
  2761                 }
       
  2762             }
       
  2763                     
       
  2764         if ( !aCityTagId )
       
  2765             {
       
  2766             error = object->Uri().Find( KCity );
       
  2767             if ( error == KErrNone )
       
  2768                 {
       
  2769                 aCityTagId = tagId;
       
  2770                 }
       
  2771             }
       
  2772         
       
  2773         if ( aCountryTagId && aCityTagId )
       
  2774             {
       
  2775             i = count;
       
  2776             }
       
  2777         }
       
  2778     LOG( "CLocationRecord::FindCountryAndCityTagL(), end" );	
       
  2779                 
       
  2780     }
       
  2781 
       
  2782 // --------------------------------------------------------------------------
       
  2783 // CLocationRecord::GetTagsL()
       
  2784 // --------------------------------------------------------------------------
       
  2785 //
       
  2786 void CLocationRecord::GetTagsL( TItemId aImageID )
       
  2787     {
       
  2788     LOG( "CLocationRecord::GetTagsL(), begin" );    
       
  2789     CMdEObjectDef& tagObjectDef = iNamespaceDef->GetObjectDefL( Tag::KTagObject );
       
  2790         
       
  2791     if(iTagQuery)
       
  2792         {
       
  2793         iTagQuery->RemoveObserver(*this);
       
  2794         iTagQuery->Cancel();
       
  2795         delete iTagQuery;
       
  2796         iTagQuery = NULL;
       
  2797         }   
       
  2798         
       
  2799     iTagQuery = iMdeSession->NewRelationQueryL( *iNamespaceDef, this );
       
  2800     User::LeaveIfNull( iTagQuery );
       
  2801 
       
  2802     iTagQuery->SetResultMode( EQueryResultModeItem );
       
  2803         
       
  2804     // both left and right condition must match
       
  2805     CMdERelationCondition& filterCond = iTagQuery->Conditions().
       
  2806     AddRelationConditionL( ERelationConditionSideRight );
       
  2807 
       
  2808     // left one must be this image object.
       
  2809     filterCond.LeftL().AddObjectConditionL( aImageID );
       
  2810 
       
  2811     // right one must be tag object 
       
  2812     filterCond.RightL().AddObjectConditionL( tagObjectDef );
       
  2813 
       
  2814     iTagQuery->FindL(); // results to a call to HandleQueryCompleted()
       
  2815     LOG( "CLocationRecord::GetTagsL(), end" );    
       
  2816     }
       
  2817 
       
  2818 
       
  2819 #endif //LOC_REVERSEGEOCODE
  1140 
  2820 
  1141 // End of file
  2821 // End of file
       
  2822 
       
  2823