locationmanager/locationtrail/src/clocationrecord.cpp
changeset 54 a3cc46f37772
parent 36 aa5a574040a4
equal deleted inserted replaced
36:aa5a574040a4 54:a3cc46f37772
    19 #include <lbserrors.h>
    19 #include <lbserrors.h>
    20 #include <lbssatellite.h>
    20 #include <lbssatellite.h>
    21 #include <ecom.h>
    21 #include <ecom.h>
    22 #include <centralrepository.h>
    22 #include <centralrepository.h>
    23 #include <hwrmpowerstatesdkpskeys.h>
    23 #include <hwrmpowerstatesdkpskeys.h>
       
    24 #ifdef LOC_GEOTAGGING_CELLID
       
    25 #include <lbslocationinfo.h>
       
    26 #endif //LOC_GEOTAGGING_CELLID
    24 
    27 
    25 #include "rlocationtrail.h"
    28 #include "rlocationtrail.h"
    26 #include "clocationrecord.h"
    29 #include "clocationrecord.h"
    27 #include "cnetworkinfo.h"
    30 #include "cnetworkinfo.h"
    28 #include "locationmanagerdebug.h"
    31 #include "locationmanagerdebug.h"
    29 #include "locationtraildefs.h"
    32 #include "locationtraildefs.h"
    30 #include "locationtrailpskeys.h"
    33 #include "locationtrailpskeys.h"
    31 #include "mdeconstants.h"
    34 #include "mdeconstants.h"
    32 #ifdef LOC_REVERSEGEOCODE
    35 
    33 #include "reversegeocoderplugin.h"
       
    34 const TUid KReverseGeoCodeUid = {0x2002DD12}; 
       
    35 #endif	
       
    36 
    36 
    37 
    37 
    38 using namespace MdeConstants;
    38 using namespace MdeConstants;
    39 
    39 
    40 const TUint KNetQueryBit = 1; // 1 bit
    40 const TUint KNetQueryBit = 1; // 1 bit
    99 #ifdef LOC_REVERSEGEOCODE
    99 #ifdef LOC_REVERSEGEOCODE
   100     ,iConnectionOption(ESilent)
   100     ,iConnectionOption(ESilent)
   101 	,iImageQuery(NULL)
   101 	,iImageQuery(NULL)
   102 	,iTagQuery(NULL)
   102 	,iTagQuery(NULL)
   103     ,iTagCreator( NULL )
   103     ,iTagCreator( NULL )
       
   104     ,iLastReverseGeocodeFails(EFalse)
   104 	,iRevGeocoderPlugin( NULL )
   105 	,iRevGeocoderPlugin( NULL )
   105 #endif
   106 #endif
   106     {
   107     {
   107     iMaxTrailSize = KMaxTrailLength / KUpdateInterval;
   108     iMaxTrailSize = KMaxTrailLength / KUpdateInterval;
   108     }
   109     }
   130     iNetworkInfoTimer = CPeriodic::NewL( CActive::EPriorityStandard );
   131     iNetworkInfoTimer = CPeriodic::NewL( CActive::EPriorityStandard );
   131 
   132 
   132 #ifdef LOC_REVERSEGEOCODE
   133 #ifdef LOC_REVERSEGEOCODE
   133     iTagCreator = CTagCreator::NewL();
   134     iTagCreator = CTagCreator::NewL();
   134 
   135 
   135 
   136 	  TInt pluginerr = KErrNone;
   136     if (!iRevGeocoderPlugin)
   137     TRAP(pluginerr,iRevGeocoderPlugin = CReverseGeoCoderPlugin::NewL());
   137         {
   138     		
   138         iRevGeocoderPlugin = reinterpret_cast<CReverseGeoCoderPlugin*>(
   139    if(pluginerr == KErrNone)
   139               REComSession::CreateImplementationL(KReverseGeoCodeUid,iDtorKey));
   140    	{
   140         if(iRevGeocoderPlugin)
   141 		iRevGeocoderPlugin->AddObserverL(*this);
   141             {
   142 	 	}
   142             iRevGeocoderPlugin->AddObserverL(*this);
       
   143             }
       
   144          }
       
   145  
       
   146  #endif
   143  #endif
   147 
   144 
   148 	
   145 	
   149     TInt interval( 0 );
   146     TInt interval( 0 );
   150     TRAP(err, ReadCenRepValueL(KIntervalKey, interval));
   147     TRAP(err, ReadCenRepValueL(KIntervalKey, interval));
   199 #ifdef LOC_REVERSEGEOCODE
   196 #ifdef LOC_REVERSEGEOCODE
   200     iLocationItems.ResetAndDestroy();
   197     iLocationItems.ResetAndDestroy();
   201     iLocationItems.Close();
   198     iLocationItems.Close();
   202 #endif    
   199 #endif    
   203     delete iNetworkInfoChangeListener;
   200     delete iNetworkInfoChangeListener;
       
   201     iNetworkInfoChangeListener = NULL;
   204     delete iPositionInfo;
   202     delete iPositionInfo;
       
   203     iPositionInfo = NULL;
   205     delete iNetworkInfoTimer;
   204     delete iNetworkInfoTimer;
       
   205     iNetworkInfoTimer = NULL;
   206 #ifdef LOC_GEOTAGGING_CELLID
   206 #ifdef LOC_GEOTAGGING_CELLID
   207     if(iGeoConverter)
   207     if(iGeoConverter)
   208         {
   208         {
   209         delete iGeoConverter;
   209         delete iGeoConverter;
   210         iGeoConverter = NULL;
   210         iGeoConverter = NULL;
   212 #endif    
   212 #endif    
   213     if (iRemapper)
   213     if (iRemapper)
   214 		{
   214 		{
   215 		iRemapper->StopRemapping();
   215 		iRemapper->StopRemapping();
   216 		delete iRemapper;
   216 		delete iRemapper;
       
   217 		iRemapper = NULL;
   217 		}
   218 		}
   218     if(iLocationQuery)
   219     if(iLocationQuery)
   219 		{
   220 		{
   220 		iLocationQuery->RemoveObserver(*this);
   221 		iLocationQuery->RemoveObserver(*this);
   221 		iLocationQuery->Cancel();
   222 		iLocationQuery->Cancel();
   249 		}	
   250 		}	
   250 	delete iTagCreator;
   251 	delete iTagCreator;
   251 	// set the pointer to NULL, ECOM will destroy object.
   252 	// set the pointer to NULL, ECOM will destroy object.
   252     delete iRevGeocoderPlugin;
   253     delete iRevGeocoderPlugin;
   253     iRevGeocoderPlugin = NULL;
   254     iRevGeocoderPlugin = NULL;
   254     REComSession::DestroyedImplementation(iDtorKey);
       
   255 #endif
   255 #endif
   256     LOG( "CLocationRecord::~CLocationRecord(), end" );	
   256     LOG( "CLocationRecord::~CLocationRecord(), end" );	
   257     }
   257     }
   258 
   258 
   259 // --------------------------------------------------------------------------
   259 // --------------------------------------------------------------------------
   353     _LIT( KDateTimeFormat, "%Y/%M/%D %H:%T:%S" );
   353     _LIT( KDateTimeFormat, "%Y/%M/%D %H:%T:%S" );
   354     const TInt DateTimeStrMaxLength = 20;
   354     const TInt DateTimeStrMaxLength = 20;
   355     LOG1( "CLocationRecord::GetLocationByTimeL - aTime: %Ld", aTime.Int64() );
   355     LOG1( "CLocationRecord::GetLocationByTimeL - aTime: %Ld", aTime.Int64() );
   356     TBuf<DateTimeStrMaxLength> str1;
   356     TBuf<DateTimeStrMaxLength> str1;
   357     aTime.FormatL( str1, KDateTimeFormat );
   357     aTime.FormatL( str1, KDateTimeFormat );
   358    // LOG1( "CLocationRecord::GetLocationByTimeL - aTime: %S", &str1 );
   358     LOG1( "CLocationRecord::GetLocationByTimeL - aTime: %S", &str1 );
   359 #endif
   359 #endif
   360 
   360 
   361     TTimeIntervalSeconds interval;
   361     TTimeIntervalSeconds interval;
   362     TTimeIntervalSeconds nextInterval;
   362     TTimeIntervalSeconds nextInterval;
   363     for ( TInt i(iTrail.Count()-1) ; i >= 0 && !posFound ; i-- )
   363     for ( TInt i(iTrail.Count()-1) ; i >= 0 && !posFound ; i-- )
   368 
   368 
   369 #ifdef _DEBUG
   369 #ifdef _DEBUG
   370         LOG1( "CLocationRecord::GetLocationByTimeL - Trail timestamp: %Ld", iTrail[i]->iTimeStamp.Int64() );
   370         LOG1( "CLocationRecord::GetLocationByTimeL - Trail timestamp: %Ld", iTrail[i]->iTimeStamp.Int64() );
   371         TBuf<DateTimeStrMaxLength> str;
   371         TBuf<DateTimeStrMaxLength> str;
   372         iTrail[i]->iTimeStamp.FormatL( str, KDateTimeFormat );
   372         iTrail[i]->iTimeStamp.FormatL( str, KDateTimeFormat );
   373         //LOG1( "CLocationRecord::GetLocationByTimeL - Trail timestamp: %S", &str );
   373         LOG1( "CLocationRecord::GetLocationByTimeL - Trail timestamp: %S", &str );
   374         LOG1( "CLocationRecord::GetLocationByTimeL - timeDiff: %d", timeDiff );
   374         LOG1( "CLocationRecord::GetLocationByTimeL - timeDiff: %d", timeDiff );
   375 #endif
   375 #endif
   376 
   376 
   377         if ( err == KErrNone && timeDiff <= KMaximumIntervalSeconds )
   377         if ( err == KErrNone && timeDiff <= KMaximumIntervalSeconds )
   378             {
   378             {
   501     
   501     
   502     iGpsDataAvailableFlag = EFalse;
   502     iGpsDataAvailableFlag = EFalse;
   503     
   503     
   504     if ( !iTrailStarted || iState == RLocationTrail::ETrailStopped)
   504     if ( !iTrailStarted || iState == RLocationTrail::ETrailStopped)
   505         {
   505         {
   506     	LOG("trail not started/stopped");
   506     	LOG("CLocationRecord::Position(), trail not started/stopped");
   507         iPositionInfo->Stop();
   507         iPositionInfo->Stop();
   508         return;
   508         return;
   509         }
   509         }
   510     // all cases store the location..
   510     // all cases store the location..
   511     StoreLocation( positionSatelliteInfo );
   511     StoreLocation( positionSatelliteInfo );
   525         {
   525         {
   526         case KPositionPartialUpdate: // fall through
   526         case KPositionPartialUpdate: // fall through
   527         case KPositionQualityLoss: 
   527         case KPositionQualityLoss: 
   528             {
   528             {
   529             // Location is stored, even if it may not be valid.
   529             // Location is stored, even if it may not be valid.
   530             LOG("Partial update");
   530             LOG("CLocationRecord::Position(), Partial update");
   531             if ( iState != RLocationTrail::EWaitingGPSData && 
   531             if ( iState != RLocationTrail::EWaitingGPSData && 
   532             	 iState != RLocationTrail::ETrailStopping ) 
   532             	 iState != RLocationTrail::ETrailStopping ) 
   533                 {
   533                 {
   534                 SetCurrentState( RLocationTrail::EWaitingGPSData );
   534                 SetCurrentState( RLocationTrail::EWaitingGPSData );
   535             	LOG("Trail waiting for gps");
   535             	LOG("CLocationRecord::Position(), Trail waiting for gps");
   536                 }
   536                 }
   537             break;
   537             break;
   538             }
   538             }
   539         case KErrNone:
   539         case KErrNone:
   540             {
   540             {
   541             LOG("Good GPS coordinates");
   541             LOG("CLocationRecord::Position(), Good GPS coordinates");
   542             iGpsDataAvailableFlag = ETrue;
   542             iGpsDataAvailableFlag = ETrue;
   543             if ( iState != RLocationTrail::ETrailStarted ) 
   543             if ( iState != RLocationTrail::ETrailStarted ) 
   544                 {
   544                 {
   545                 if ( iRemapper )
   545                 if ( iRemapper )
   546                 	{
   546                 	{
   547                 	LOG("Start remapping");
   547                 	LOG("CLocationRecord::Position(), Start remapping");
   548                		iRemapper->StartRemappingObjects( iNewItem.iLocationData );
   548                		iRemapper->StartRemappingObjects( iNewItem.iLocationData );
   549 
   549 
   550                     if( iObserver->WaitForPositioningStopTimeout() && !RemappingNeeded() )                                
   550                     if( iObserver->WaitForPositioningStopTimeout() && !RemappingNeeded() )                                
   551                		    {                                                
   551                		    {                                                
   552                         iObserver->RemapedCompleted();
   552                         iObserver->RemapedCompleted();
   563                 }
   563                 }
   564             break;
   564             break;
   565             }
   565             }
   566         default:
   566         default:
   567             {
   567             {
   568             LOG1("Searching GPS, aError %d", aError );
   568             LOG1("CLocationRecord::Position(), Searching GPS, aError %d", aError );
   569             if ( iState != RLocationTrail::ESearchingGPS &&
   569             if ( iState != RLocationTrail::ESearchingGPS &&
   570                	 iState != RLocationTrail::ETrailStopping ) 
   570                	 iState != RLocationTrail::ETrailStopping ) 
   571                 {
   571                 {
   572                 SetCurrentState( RLocationTrail::ESearchingGPS );
   572                 SetCurrentState( RLocationTrail::ESearchingGPS );
   573             	LOG("Trail searching gps");
   573             	LOG("Trail searching gps");
   574                 }
   574                 }
   575             break;
   575             break;
   576             }      
   576             }      
   577         }
   577         }
   578     TBool fixState = CheckGPSFix( positionSatelliteInfo );
   578     TBool fixState = CheckGPSFix( positionSatelliteInfo );
   579     LOG1( "fixState %d", fixState );
   579     LOG1( "CLocationRecord::Position(), fixState %d", fixState );
   580     LOG1( "iLastGPSFixState %d", iLastGPSFixState );
   580     LOG1( "CLocationRecord::Position(), iLastGPSFixState %d", iLastGPSFixState );
   581     
   581     
   582     if ( iObserver && iLastGPSFixState != fixState )
   582     if ( iObserver && iLastGPSFixState != fixState )
   583     	{
   583     	{
   584     	LOG("Quality changed");
   584     	LOG("CLocationRecord::Position(), Quality changed");
   585     	iObserver->GPSSignalQualityChanged( positionSatelliteInfo );
   585     	iObserver->GPSSignalQualityChanged( positionSatelliteInfo );
   586     	}
   586     	}
   587     
   587     
   588    	iLastGPSFixState = fixState;
   588    	iLastGPSFixState = fixState;
   589     
   589     
   705         iNetwork = aNetworkInfo;
   705         iNetwork = aNetworkInfo;
   706         if (iNetwork.iAccess == CTelephony::ENetworkAccessUtran)
   706         if (iNetwork.iAccess == CTelephony::ENetworkAccessUtran)
   707         	{
   707         	{
   708         	iNetwork.iLocationAreaCode = 0;
   708         	iNetwork.iLocationAreaCode = 0;
   709         	}
   709         	}
       
   710 #ifdef LOC_GEOTAGGING_CELLID
   710         if ( iState == RLocationTrail::ETrailStarting && iTrailStarted )
   711         if ( iState == RLocationTrail::ETrailStarting && iTrailStarted )
   711         	{
   712         	{
   712         	SetCurrentState( RLocationTrail::ETrailStarted );
   713         	SetCurrentState( RLocationTrail::ETrailStarted );
   713         	}
   714         	}
       
   715 #endif        
   714         }
   716         }
   715     else
   717     else
   716         {
   718         {
   717         LOG1("CLocationRecord::NetworkInfo - %d", aError );
   719         LOG1("CLocationRecord::NetworkInfo - %d", aError );
   718         iNetwork = CTelephony::TNetworkInfoV1();
   720         iNetwork = CTelephony::TNetworkInfoV1();
   795     LOG1( "CLocationRecord::SetCurrentState(), begin, state:%d", aState );
   797     LOG1( "CLocationRecord::SetCurrentState(), begin, state:%d", aState );
   796     iState = aState;
   798     iState = aState;
   797     if( iTrailStarted )
   799     if( iTrailStarted )
   798         {
   800         {
   799         // Set the property only when trail is started to avoid icon flickering and wrong icon update in UI
   801         // Set the property only when trail is started to avoid icon flickering and wrong icon update in UI
   800         if( iGpsDataAvailableFlag  || 
   802         if( iGpsDataAvailableFlag  
   801             iNetwork.iCellId > 0 && 
   803 #ifdef LOC_GEOTAGGING_CELLID
       
   804             || ( iNetwork.iCellId > 0 && 
   802             ((iNetwork.iLocationAreaCode == 0 && iNetwork.iAccess == CTelephony::ENetworkAccessUtran) || // 3G
   805             ((iNetwork.iLocationAreaCode == 0 && iNetwork.iAccess == CTelephony::ENetworkAccessUtran) || // 3G
   803             (iNetwork.iLocationAreaCode > 0 && (iNetwork.iAccess == CTelephony::ENetworkAccessGsm ||  // 2G
   806             (iNetwork.iLocationAreaCode > 0 && (iNetwork.iAccess == CTelephony::ENetworkAccessGsm ||  // 2G
   804             						iNetwork.iAccess == CTelephony::ENetworkAccessGsmCompact)))  &&
   807             						iNetwork.iAccess == CTelephony::ENetworkAccessGsmCompact)))  &&
   805             iNetwork.iCountryCode.Length() > 0 &&
   808             iNetwork.iCountryCode.Length() > 0 &&
   806             iNetwork.iNetworkId.Length() > 0 )
   809             iNetwork.iNetworkId.Length() > 0 )
       
   810 #endif // LOC_GEOTAGGING_CELLID            
       
   811             )
   807             {
   812             {
   808             // set the value 3 to have Geo tag available icon else not available.
   813             // set the value 3 to have Geo tag available icon else not available.
   809             iProperty.Set( KPSUidLocationTrail, KLocationTrailState, 
   814             iProperty.Set( KPSUidLocationTrail, KLocationTrailState, 
   810                 (TInt) RLocationTrail::ETrailStarted );
   815                 (TInt) RLocationTrail::ETrailStarted );
   811             }
   816             }
   955              net->iCountryCode.Length() == 0 &&
   960              net->iCountryCode.Length() == 0 &&
   956              net->iNetworkId.Length() == 0 )
   961              net->iNetworkId.Length() == 0 )
   957             {
   962             {
   958             // no n/w info... put it into remap.
   963             // no n/w info... put it into remap.
   959             // remove the last appended element.
   964             // remove the last appended element.
   960             LOG("No network info (offline mode + no GPS fix), keep for remapping");
   965             LOG("CLocationRecord::LocationSnapshotL(), No network info (offline mode + no GPS fix), keep for remapping");
   961 			TRemapItem remapItem;
   966 			TRemapItem remapItem;
   962 			remapItem.iObjectId = aObjectId;
   967 			remapItem.iObjectId = aObjectId;
   963 			remapItem.iTime = timestamp;
   968 			remapItem.iTime = timestamp;
   964 			iRemapper->Append( remapItem );
   969 			iRemapper->Append( remapItem );
   965 		
   970 		
   966 			TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
   971 			TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
   967 			iMediaItems.Remove(iMediaItems.Count() - 1);
   972 			iMediaItems.Remove(iMediaItems.Count() - 1);
   968 			iMediaItems.Compress();
   973 			iMediaItems.Compress();
   969 			delete firstPtr;
   974             if( firstPtr == newItem)
       
   975                 {                
       
   976                 newItem = NULL;
       
   977                 }           
       
   978             delete firstPtr;            
       
   979             firstPtr = NULL;
   970             }   
   980             }   
   971         else
   981         else
   972             {
   982             {
   973             // n/w info available
   983             // n/w info available
   974             NetworkInfoSnapshotL();
   984             NetworkInfoSnapshotL();
   975             }
   985             }
   976 		}
   986 		}
   977 	else if ( Math::IsNaN( locationData.iPosition.Latitude() ) && 
   987 	else if ( Math::IsNaN( locationData.iPosition.Latitude() ) && 
   978 			Math::IsNaN( locationData.iPosition.Longitude() ))
   988 			Math::IsNaN( locationData.iPosition.Longitude() ))
   979 		{
   989 		{
       
   990         LOG("CLocationRecord::LocationSnapshotL(), coordinates empty with or without cellular info")
   980 	    // coordinates empty, with or without cellular info
   991 	    // coordinates empty, with or without cellular info
   981 
   992 
   982 		if ( net->iCellId == 0 && 
   993 		if ( net->iCellId == 0 && 
   983 				net->iLocationAreaCode == 0 &&
   994 				net->iLocationAreaCode == 0 &&
   984 				net->iCountryCode.Length() == 0 &&
   995 				net->iCountryCode.Length() == 0 &&
   985 				net->iNetworkId.Length() == 0 )
   996 				net->iNetworkId.Length() == 0 )
   986 			{
   997 			{
   987             LOG("No network info (offline mode + no GPS fix), keep for remapping");
   998             LOG("CLocationRecord::LocationSnapshotL(), No network info (offline mode + no GPS fix), keep for remapping");
   988 			TRemapItem remapItem;
   999 			TRemapItem remapItem;
   989 			remapItem.iObjectId = aObjectId;
  1000 			remapItem.iObjectId = aObjectId;
   990 			remapItem.iTime = timestamp;
  1001 			remapItem.iTime = timestamp;
   991 			iRemapper->Append( remapItem );
  1002 			iRemapper->Append( remapItem );
   992 		
  1003 		
   993 			TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
  1004 			TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
   994 			iMediaItems.Remove(iMediaItems.Count() - 1);
  1005 			iMediaItems.Remove(iMediaItems.Count() - 1);
   995 			iMediaItems.Compress();
  1006 			iMediaItems.Compress();
   996 			delete firstPtr;
  1007             if( firstPtr == newItem)
       
  1008                 {                
       
  1009                 newItem = NULL;
       
  1010                 }           
       
  1011             delete firstPtr;            
       
  1012             firstPtr = NULL;
   997 			}
  1013 			}
   998 		// check match for last created locationobject
  1014 		// check match for last created locationobject
   999 #ifdef LOC_REVERSEGEOCODE
  1015 #ifdef LOC_REVERSEGEOCODE
  1000 		else if ( (iLastMediaItem.iFlag & KSnapMediaFile) > 0 &&
  1016 		else if ( (iLastMediaItem.iFlag & KSnapMediaFile) > 0 &&
  1001             iLastMediaItem.iCountryTagId > 0)
  1017             iLastMediaItem.iCountryTagId > 0)
  1002 #else
  1018 #else
  1003         else if ( (iLastMediaItem.iFlag & KSnapMediaFile) > 0)
  1019         else if ( (iLastMediaItem.iFlag & KSnapMediaFile) > 0)
  1004 #endif //LOC_REVERSEGEOCODE
  1020 #endif //LOC_REVERSEGEOCODE
  1005 			{
  1021 			{
       
  1022             LOG("CLocationRecord::LocationSnapshotL(), last created locationobject match" );
  1006 			TLocationData lastLocationData = iLastMediaItem.iLocationData;
  1023 			TLocationData lastLocationData = iLastMediaItem.iLocationData;
  1007 			CTelephony::TNetworkInfoV1* lastnet = &lastLocationData.iNetworkInfo;
  1024 			CTelephony::TNetworkInfoV1* lastnet = &lastLocationData.iNetworkInfo;
  1008 
  1025 
  1009 			// networkinfo changed from last location
  1026 			// networkinfo changed from last location
  1010 			if ( lastnet->iCellId == net->iCellId &&
  1027 			if ( lastnet->iCellId == net->iCellId &&
  1025 #endif //LOC_REVERSEGEOCODE
  1042 #endif //LOC_REVERSEGEOCODE
  1026                 // remove the current item.
  1043                 // remove the current item.
  1027 				TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
  1044 				TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
  1028 				iMediaItems.Remove(iMediaItems.Count() - 1);
  1045 				iMediaItems.Remove(iMediaItems.Count() - 1);
  1029 				iMediaItems.Compress();
  1046 				iMediaItems.Compress();
  1030 				delete firstPtr;
  1047 	            if( firstPtr == newItem)
       
  1048 	                {                
       
  1049 	                newItem = NULL;
       
  1050 	                }           
       
  1051 	            delete firstPtr;            
       
  1052 	            firstPtr = NULL;
  1031 				}
  1053 				}
  1032 			}
  1054 			}
  1033 		
  1055 		
  1034 		if ( !previousMatch )
  1056         if (  newItem && !previousMatch )
  1035 		    {
  1057 		    {
  1036 			// go for n/w based
  1058 			// go for n/w based
  1037             newItem->iFlag |= KNetQueryBit;
  1059             newItem->iFlag |= KNetQueryBit;
  1038 		    }
  1060 		    }
  1039 		}
  1061 		}
  1040        
  1062        
  1041 	// valid coordinates found
  1063 	// valid coordinates found
  1042 	else if ( lastLocationId != 0 && 
  1064 	else if ( lastLocationId != 0 && 
  1043         ((iLastMediaItem.iFlag & KSnapMediaFile) > 0))
  1065         ((iLastMediaItem.iFlag & KSnapMediaFile) > 0))
  1044 		{
  1066 		{
       
  1067         LOG("CLocationRecord::LocationSnapshotL(), valid coordinates found");
  1045         TLocationData lastLocationData = iLastMediaItem.iLocationData;
  1068         TLocationData lastLocationData = iLastMediaItem.iLocationData;
  1046 		CTelephony::TNetworkInfoV1* lastnet = &lastLocationData.iNetworkInfo;
  1069 		CTelephony::TNetworkInfoV1* lastnet = &lastLocationData.iNetworkInfo;
  1047 		
  1070 		
  1048 		// first check if networkinfo matches last created location
  1071 		// first check if networkinfo matches last created location
  1049 		if ( lastnet->iCellId == net->iCellId &&
  1072 		if ( lastnet->iCellId == net->iCellId &&
  1051 				lastnet->iCountryCode == net->iCountryCode &&
  1074 				lastnet->iCountryCode == net->iCountryCode &&
  1052 				lastnet->iNetworkId == net->iNetworkId )
  1075 				lastnet->iNetworkId == net->iNetworkId )
  1053 			{
  1076 			{
  1054 			
  1077 			
  1055 			// if both locations have valid coordinates, calculate distance between points
  1078 			// if both locations have valid coordinates, calculate distance between points
  1056 			if ( !Math::IsNaN( lastLocationData.iPosition.Latitude() ) && 
  1079 			if (
       
  1080 #ifdef LOC_REVERSEGEOCODE
       
  1081                 !iLastReverseGeocodeFails &&
       
  1082 #endif //LOC_REVERSEGEOCODE
       
  1083                 !Math::IsNaN( lastLocationData.iPosition.Latitude() ) && 
  1057 					!Math::IsNaN( lastLocationData.iPosition.Longitude() ) && 
  1084 					!Math::IsNaN( lastLocationData.iPosition.Longitude() ) && 
  1058 					!Math::IsNaN( locationData.iPosition.Latitude() ) && 
  1085 					!Math::IsNaN( locationData.iPosition.Latitude() ) && 
  1059 					!Math::IsNaN( locationData.iPosition.Longitude() ))
  1086 					!Math::IsNaN( locationData.iPosition.Longitude() ))
  1060 				{
  1087 				{
  1061 				TReal32 distance;
  1088 				TReal32 distance;
  1062 				TInt err = locationData.iPosition.Distance(lastLocationData.iPosition, distance);
  1089 				TInt err = locationData.iPosition.Distance(lastLocationData.iPosition, distance);
  1063 				
  1090 				
  1064 				if ( distance < iLocationDelta )
  1091 				if ( distance < iLocationDelta )
  1065 					{
  1092 					{
  1066 					LOG("location close to the previous one");
  1093 					LOG("CLocationRecord::LocationSnapshotL(), location close to the previous one");
  1067 					previousMatch = ETrue;
  1094 					previousMatch = ETrue;
  1068 					CreateRelationL( aObjectId, lastLocationId );
  1095 					CreateRelationL( aObjectId, lastLocationId );
  1069 					
  1096 					
  1070 #ifdef LOC_REVERSEGEOCODE
  1097 #ifdef LOC_REVERSEGEOCODE
  1071 					// attach same tags associated to last location
  1098 					// attach same tags associated to last location
  1074 					    iTagCreator->AttachTagsL( 
  1101 					    iTagCreator->AttachTagsL( 
  1075 					          aObjectId, iLastMediaItem.iCountryTagId, iLastMediaItem.iCityTagId );
  1102 					          aObjectId, iLastMediaItem.iCountryTagId, iLastMediaItem.iCityTagId );
  1076                         TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
  1103                         TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
  1077                         iMediaItems.Remove(iMediaItems.Count() - 1);
  1104                         iMediaItems.Remove(iMediaItems.Count() - 1);
  1078                         iMediaItems.Compress();
  1105                         iMediaItems.Compress();
  1079                         delete firstPtr;
  1106                         if( firstPtr == newItem)
       
  1107                             {                
       
  1108                             newItem = NULL;
       
  1109                             }           
       
  1110                         delete firstPtr;            
       
  1111                         firstPtr = NULL;
  1080 					    }
  1112 					    }
  1081 					else 
  1113 					//check newItem for validity
       
  1114 					else if (newItem)
  1082 					    {
  1115 					    {
  1083 					    // country tag not found.. go for reverse geocoding..
  1116 					    // country tag not found.. go for reverse geocoding..
  1084 					    newItem->iLocationId = lastLocationId;
  1117 					    newItem->iLocationId = lastLocationId;
  1085 					    iLocationItems.Append( newItem );
  1118 					    iLocationItems.Append( newItem );
  1086 						iMediaItems.Remove(iMediaItems.Count() - 1);
  1119 						iMediaItems.Remove(iMediaItems.Count() - 1);
  1100 #else
  1133 #else
  1101                     // remove from the queue
  1134                     // remove from the queue
  1102                     TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
  1135                     TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
  1103                     iMediaItems.Remove(iMediaItems.Count() - 1);
  1136                     iMediaItems.Remove(iMediaItems.Count() - 1);
  1104                     iMediaItems.Compress();
  1137                     iMediaItems.Compress();
  1105                     delete firstPtr;
  1138                     if( firstPtr == newItem)
       
  1139                         {                
       
  1140                         newItem = NULL;
       
  1141                         }           
       
  1142                     delete firstPtr;            
       
  1143                     firstPtr = NULL;
  1106 
  1144 
  1107 #endif //LOC_REVERSEGEOCODE
  1145 #endif //LOC_REVERSEGEOCODE
  1108 					}
  1146 					}
  1109 				}
  1147 				}
  1110 			}
  1148 			}
  1176     LOG( "CLocationRecord::FindLocationFromDBL(), begin" );    
  1214     LOG( "CLocationRecord::FindLocationFromDBL(), begin" );    
  1177     if(((iMediaHandlingFlag & KLocationQueryInProgress)> 0)
  1215     if(((iMediaHandlingFlag & KLocationQueryInProgress)> 0)
  1178 	     || (iMediaItems.Count() <= 0))
  1216 	     || (iMediaItems.Count() <= 0))
  1179         {
  1217         {
  1180 		// query is in progress or queue is empty
  1218 		// query is in progress or queue is empty
  1181 		LOG1( "query is in progress or queue is empty. Count - %d", iMediaItems.Count() );
  1219 		LOG1( "CLocationRecord::FindLocationFromDBL(), Count - %d", iMediaItems.Count() );
  1182         return;
  1220         return;
  1183         }
  1221         }
  1184      if ( (iMediaItems[0]->iFlag & KNetQueryBit) > 0 )
  1222      if ( (iMediaItems[0]->iFlag & KNetQueryBit) > 0 )
  1185          {
  1223          {
  1186 		 // n/w based.
  1224 		 // n/w based.
  1189          }
  1227          }
  1190         
  1228         
  1191      const TReal64 KMeterInDegrees = 0.000009;
  1229      const TReal64 KMeterInDegrees = 0.000009;
  1192      const TReal64 KPi = 3.14159265358979;
  1230      const TReal64 KPi = 3.14159265358979;
  1193      const TReal32 K180Degrees = 180.0;
  1231      const TReal32 K180Degrees = 180.0;
  1194      
       
  1195      //TLocationData locationData = iMediaItems[0].iLocationData;
       
  1196             
  1232             
  1197      TReal64 latitude = iMediaItems[0]->iLocationData.iPosition.Latitude();
  1233      TReal64 latitude = iMediaItems[0]->iLocationData.iPosition.Latitude();
  1198      TReal64 longitude = iMediaItems[0]->iLocationData.iPosition.Longitude();
  1234      TReal64 longitude = iMediaItems[0]->iLocationData.iPosition.Longitude();
  1199      // calculate distance in degrees
  1235      // calculate distance in degrees
  1200      TReal64 cosine;
  1236      TReal64 cosine;
  1221      iLocationQuery = iMdeSession->NewObjectQueryL( *iNamespaceDef, *iLocationObjectDef, this );
  1257      iLocationQuery = iMdeSession->NewObjectQueryL( *iNamespaceDef, *iLocationObjectDef, this );
  1222                  
  1258                  
  1223      CMdELogicCondition& cond = iLocationQuery->Conditions();
  1259      CMdELogicCondition& cond = iLocationQuery->Conditions();
  1224      cond.SetOperator( ELogicConditionOperatorAnd );
  1260      cond.SetOperator( ELogicConditionOperatorAnd );
  1225             
  1261             
  1226      LOG1( "latitude: %f", latitude);
  1262      LOG1( "CLocationRecord::FindLocationFromDBL(), latitude: %f", latitude);
  1227      LOG1( "latdelta: %f", latDelta);
  1263      LOG1( "CLocationRecord::FindLocationFromDBL(), latdelta: %f", latDelta);
  1228      LOG1( "longitude: %f", longitude);
  1264      LOG1( "CLocationRecord::FindLocationFromDBL(), longitude: %f", longitude);
  1229      LOG1( "londelta: %f", lonDelta);
  1265      LOG1( "CLocationRecord::FindLocationFromDBL(), londelta: %f", lonDelta);
  1230             
  1266             
  1231      cond.AddPropertyConditionL( *iLatitudeDef, 
  1267      cond.AddPropertyConditionL( *iLatitudeDef, 
  1232                     TMdERealBetween( latitude - latDelta, latitude + latDelta ));
  1268                     TMdERealBetween( latitude - latDelta, latitude + latDelta ));
  1233      cond.AddPropertyConditionL( *iLongitudeDef, 
  1269      cond.AddPropertyConditionL( *iLongitudeDef, 
  1234                     TMdERealBetween( longitude - lonDelta, longitude + lonDelta ));
  1270                     TMdERealBetween( longitude - lonDelta, longitude + lonDelta ));
  1314 	locationObject->AddTimePropertyL( modifiedDef, timestamp );
  1350 	locationObject->AddTimePropertyL( modifiedDef, timestamp );
  1315 	locationObject->AddUint32PropertyL( sizeDef, 0 ); // always zero size for location objects
  1351 	locationObject->AddUint32PropertyL( sizeDef, 0 ); // always zero size for location objects
  1316 	locationObject->AddTextPropertyL( itemTypeDef, Location::KLocationItemType );
  1352 	locationObject->AddTextPropertyL( itemTypeDef, Location::KLocationItemType );
  1317 	locationObject->AddInt16PropertyL( offSetDef, timeOffset.Int() / 60 );
  1353 	locationObject->AddInt16PropertyL( offSetDef, timeOffset.Int() / 60 );
  1318 	
  1354 	
  1319 	LOG1( "Location created with stamp: %Ld", timestamp.Int64() );
  1355 	LOG1( "CLocationRecord::DoCreateLocationL(), Location created with stamp: %Ld", timestamp.Int64() );
  1320 	
  1356 	
  1321 	// location related properties
  1357 	// location related properties
  1322 	if ( !Math::IsNaN( aLocationData.iPosition.Latitude() ) && 
  1358 	if ( !Math::IsNaN( aLocationData.iPosition.Latitude() ) && 
  1323 		 !Math::IsNaN( aLocationData.iPosition.Longitude() ))
  1359 		 !Math::IsNaN( aLocationData.iPosition.Longitude() ))
  1324 		{
  1360 		{
       
  1361         LOG1("CLocationRecord::DoCreateLocationL(), Lan - %f", aLocationData.iPosition.Latitude());
       
  1362         LOG1("CLocationRecord::DoCreateLocationL(), Lon - %f", aLocationData.iPosition.Longitude());
  1325 		locationObject->AddReal64PropertyL( *iLatitudeDef, aLocationData.iPosition.Latitude() );
  1363 		locationObject->AddReal64PropertyL( *iLatitudeDef, aLocationData.iPosition.Latitude() );
  1326 		locationObject->AddReal64PropertyL( *iLongitudeDef, aLocationData.iPosition.Longitude() );
  1364 		locationObject->AddReal64PropertyL( *iLongitudeDef, aLocationData.iPosition.Longitude() );
  1327 
  1365 
  1328 		}
  1366 		}
  1329 	if ( !Math::IsNaN( aLocationData.iPosition.Altitude() ) )
  1367 	if ( !Math::IsNaN( aLocationData.iPosition.Altitude() ) )
  1342 		{
  1380 		{
  1343 		locationObject->AddReal32PropertyL( qualityDef, aLocationData.iQuality );
  1381 		locationObject->AddReal32PropertyL( qualityDef, aLocationData.iQuality );
  1344 		}
  1382 		}
  1345 
  1383 
  1346 	// network related properties
  1384 	// network related properties
  1347 	if ( aLocationData.iNetworkInfo.iAreaKnown )
  1385 	if ( aLocationData.iNetworkInfo.iAccess != CTelephony::ENetworkAccessUnknown )
  1348 		{
  1386 		{
  1349 		if ( aLocationData.iNetworkInfo.iAccess != CTelephony::ENetworkAccessUnknown )
  1387 		LOG1("CLocationRecord::DoCreateLocationL(), Cell id - %d", aLocationData.iNetworkInfo.iCellId);
  1350 			{
  1388 		locationObject->AddUint32PropertyL( cellIdDef, aLocationData.iNetworkInfo.iCellId );
  1351 			locationObject->AddUint32PropertyL( cellIdDef, aLocationData.iNetworkInfo.iCellId );
  1389 		}
  1352 			
  1390 	if ( aLocationData.iNetworkInfo.iAreaKnown && 
  1353 			}
  1391         aLocationData.iNetworkInfo.iLocationAreaCode != 0 &&
  1354 		if ( aLocationData.iNetworkInfo.iLocationAreaCode != 0 &&
  1392 		aLocationData.iNetworkInfo.iAccess != CTelephony::ENetworkAccessUnknown )
  1355 			aLocationData.iNetworkInfo.iAccess != CTelephony::ENetworkAccessUnknown )
  1393 		{
  1356 			{
  1394 		LOG1("CLocationRecord::DoCreateLocationL(), Areacode - %d", aLocationData.iNetworkInfo.iLocationAreaCode);
  1357 			locationObject->AddUint32PropertyL( locationCodeDef, 
  1395 		locationObject->AddUint32PropertyL( locationCodeDef, 
  1358 					aLocationData.iNetworkInfo.iLocationAreaCode );
  1396 				aLocationData.iNetworkInfo.iLocationAreaCode );
  1359 			
  1397 		}
  1360 			}
  1398 #ifdef _DEBUG
  1361 		if ( aLocationData.iNetworkInfo.iCountryCode.Length() > 0 )
  1399 	
  1362 			{
  1400     if ( aLocationData.iNetworkInfo.iCountryCode.Length() > 0 )
  1363 			locationObject->AddTextPropertyL( countryCodeDef, 
  1401         {
  1364 					aLocationData.iNetworkInfo.iCountryCode );
  1402         TLex lexer( aLocationData.iNetworkInfo.iCountryCode );
  1365 			
  1403         TUint countryCode = 0;    
  1366 			}
  1404         TRAP_IGNORE(lexer.Val( countryCode, EDecimal));        
  1367 		if ( aLocationData.iNetworkInfo.iNetworkId.Length() > 0 )
  1405         LOG1("CLocationRecord::DoCreateLocationL(), Country code - %d", countryCode);
  1368 			{
  1406         }
  1369 			locationObject->AddTextPropertyL(networkCodeDef, aLocationData.iNetworkInfo.iNetworkId);
  1407     else
  1370 			
  1408         {
  1371 			}
  1409         LOG("CLocationRecord::DoCreateLocationL(), No Country code");
       
  1410         }
       
  1411     
       
  1412     //Set mobile network code
       
  1413     if ( aLocationData.iNetworkInfo.iNetworkId.Length() > 0 )
       
  1414         {
       
  1415         TLex lexer = aLocationData.iNetworkInfo.iNetworkId;
       
  1416         TUint networkCode = 0;
       
  1417         TRAP_IGNORE(lexer.Val( networkCode, EDecimal));
       
  1418         LOG1("CLocationRecord::DoCreateLocationL(), Network id - %d", networkCode); 
       
  1419         }
       
  1420     else
       
  1421         {
       
  1422         LOG("CLocationRecord::DoCreateLocationL(), No network code");
       
  1423         }
       
  1424 
       
  1425 #endif
       
  1426 	if ( aLocationData.iNetworkInfo.iCountryCode.Length() > 0 )
       
  1427 		{
       
  1428 		locationObject->AddTextPropertyL( countryCodeDef, 
       
  1429 				aLocationData.iNetworkInfo.iCountryCode );
       
  1430 		}
       
  1431 
       
  1432 	if ( aLocationData.iNetworkInfo.iNetworkId.Length() > 0 )
       
  1433 		{
       
  1434 		locationObject->AddTextPropertyL(networkCodeDef, aLocationData.iNetworkInfo.iNetworkId);
  1372 		}
  1435 		}
  1373 	        
  1436 	        
  1374 	// Add the location object to the database.
  1437 	// Add the location object to the database.
  1375 	locationObjectId = iMdeSession->AddObjectL( *locationObject );
  1438 	locationObjectId = iMdeSession->AddObjectL( *locationObject );
  1376 
  1439     LOG1("CLocationRecord::DoCreateLocationL(), Location id - %d", locationObjectId);
  1377 	CleanupStack::PopAndDestroy( locationObject );
  1440 	CleanupStack::PopAndDestroy( locationObject );
  1378     LOG( "CLocationRecord::DoCreateLocationL(), end" );
  1441     LOG( "CLocationRecord::DoCreateLocationL(), end" );
  1379 
  1442 
  1380 	return locationObjectId;
  1443 	return locationObjectId;
  1381 	}
  1444 	}
  1551 void CLocationRecord::HandleFindLocationFromDB()
  1614 void CLocationRecord::HandleFindLocationFromDB()
  1552     {
  1615     {
  1553     LOG( "CLocationRecord::HandleFindLocationFromDB(), begin" );    
  1616     LOG( "CLocationRecord::HandleFindLocationFromDB(), begin" );    
  1554 	if ( iMediaItems.Count() > 0 )
  1617 	if ( iMediaItems.Count() > 0 )
  1555 	   {
  1618 	   {
  1556 		//TODO: by module owner
       
  1557 		TInt trapErr = KErrNone;
  1619 		TInt trapErr = KErrNone;
  1558 		TRAP(trapErr,FindLocationFromDBL());
  1620 		TRAP(trapErr,FindLocationFromDBL());
  1559 		// no memory, don't proceed further
  1621 		// no memory, don't proceed further
  1560 		// other error sceenario, we can move the node to the end and process the next
  1622 		// other error sceenario, we can move the node to the end and process the next
  1561 		if(trapErr != KErrNoMemory && trapErr != KErrNone)
  1623 		if(trapErr != KErrNoMemory && trapErr != KErrNone)
  1751         {
  1813         {
  1752         CMdEItem& item = aQuery.ResultItem(0);
  1814         CMdEItem& item = aQuery.ResultItem(0);
  1753         CMdEObject& locationObject = static_cast<CMdEObject&>(item);
  1815         CMdEObject& locationObject = static_cast<CMdEObject&>(item);
  1754         locationId = locationObject.Id();
  1816         locationId = locationObject.Id();
  1755         
  1817         
       
  1818         iMediaItems[0]->iLocationId = locationId;
  1756         TRAP( error, CreateRelationL( iMediaItems[0]->iObjectId, locationId ) );
  1819         TRAP( error, CreateRelationL( iMediaItems[0]->iObjectId, locationId ) );
  1757 #ifdef LOC_REVERSEGEOCODE
  1820 #ifdef LOC_REVERSEGEOCODE
  1758         //check if found location object has lat, long
  1821         //check if found location object has lat, long
  1759         CMdEProperty* latProp = NULL;
  1822         CMdEProperty* latProp = NULL;
  1760         CMdEProperty* lonProp = NULL; 
  1823         CMdEProperty* lonProp = NULL; 
  1822             iGeoConverter->ConvertL(iMediaItems[0]->iLocationData.iNetworkInfo);
  1885             iGeoConverter->ConvertL(iMediaItems[0]->iLocationData.iNetworkInfo);
  1823             iMediaHandlingFlag |= KSnapGeoConvertInProgress;
  1886             iMediaHandlingFlag |= KSnapGeoConvertInProgress;
  1824             iMediaHandlingFlag &= ~KSnapGeoConvertInPendingState;
  1887             iMediaHandlingFlag &= ~KSnapGeoConvertInPendingState;
  1825             }
  1888             }
  1826 #else
  1889 #else
       
  1890         // cell id based geo tagging is not supported.. go for remapping.
  1827         locationId = DoCreateLocationL( iMediaItems[0]->iLocationData );
  1891         locationId = DoCreateLocationL( iMediaItems[0]->iLocationData );
  1828         iMediaItems[0]->iLocationId = locationId;
  1892         iMediaItems[0]->iLocationId = locationId;
  1829         CreateRelationL( iMediaItems[0]->iObjectId, locationId );
  1893         TItemId relationId = CreateRelationL( iMediaItems[0]->iObjectId, locationId );
  1830         TLocationSnapshotItem* firstPtr = iMediaItems[0];
  1894         TLocationSnapshotItem* firstPtr = iMediaItems[0];
       
  1895 
       
  1896         // Go for remapping.. get locationdata from trail with object time
       
  1897         TTime timestamp = GetMdeObjectTimeL( iMediaItems[0]->iObjectId );
       
  1898         TRemapItem remapItem;
       
  1899         remapItem.iObjectId = iMediaItems[0]->iObjectId;
       
  1900         remapItem.iTime = timestamp;
       
  1901         remapItem.iLocationId = locationId;
       
  1902         remapItem.iRelationId = relationId;
       
  1903         iRemapper->Append( remapItem );
       
  1904         
  1831         iMediaItems.Remove(0);
  1905         iMediaItems.Remove(0);
  1832         delete firstPtr;
  1906         delete firstPtr;
  1833         iMediaItems.Compress();
  1907         iMediaItems.Compress();
  1834         iMediaHandlingFlag &= ~KLocationQueryInProgress;
  1908         iMediaHandlingFlag &= ~KLocationQueryInProgress;
  1835         if ( iMediaItems.Count() > 0 )
  1909         if ( iMediaItems.Count() > 0 )
  1879             iNewItem.iLocationData.iPosition.SetCoordinate
  1953             iNewItem.iLocationData.iPosition.SetCoordinate
  1880                 ( aPosition.Latitude(), aPosition.Longitude(), aPosition.Altitude());
  1954                 ( aPosition.Latitude(), aPosition.Longitude(), aPosition.Altitude());
  1881             iNewItem.iLocationData.iQuality = aPosition.HorizontalAccuracy();
  1955             iNewItem.iLocationData.iQuality = aPosition.HorizontalAccuracy();
  1882             TItemId locationId = DoCreateLocationL( iNewItem.iLocationData );
  1956             TItemId locationId = DoCreateLocationL( iNewItem.iLocationData );
  1883             iRemapper->UpdateRelationsL( locationId ); 
  1957             iRemapper->UpdateRelationsL( locationId ); 
  1884             // TODO: remap.
       
  1885 #ifdef LOC_REVERSEGEOCODE
  1958 #ifdef LOC_REVERSEGEOCODE
  1886             if(!(iMediaHandlingFlag & KReverseGeoCodingInProgress))
  1959             if(!(iMediaHandlingFlag & KReverseGeoCodingInProgress))
  1887                 {
  1960                 {
  1888                 iRemapState = ERemapRevGeoCodeInProgress;
  1961                 iRemapState = ERemapRevGeoCodeInProgress;
  1889                 //Find the address by coordinate, results a call to ReverseGeocodeComplete()
  1962                 //Find the address by coordinate, results a call to ReverseGeocodeComplete()
  1909                 ( aPosition.Latitude(), aPosition.Longitude(), aPosition.Altitude());
  1982                 ( aPosition.Latitude(), aPosition.Longitude(), aPosition.Altitude());
  1910            iMediaItems[0]->iLocationData.iQuality = aPosition.HorizontalAccuracy();
  1983            iMediaItems[0]->iLocationData.iQuality = aPosition.HorizontalAccuracy();
  1911            TLocationSnapshotItem* item = iMediaItems[0];
  1984            TLocationSnapshotItem* item = iMediaItems[0];
  1912            TItemId locationId = DoCreateLocationL( iMediaItems[0]->iLocationData );
  1985            TItemId locationId = DoCreateLocationL( iMediaItems[0]->iLocationData );
  1913            iMediaItems[0]->iLocationId = locationId;
  1986            iMediaItems[0]->iLocationId = locationId;
  1914            CreateRelationL( iMediaItems[0]->iObjectId, locationId );
  1987            TItemId relationId = CreateRelationL( iMediaItems[0]->iObjectId, locationId );
       
  1988 
       
  1989            // Go for remapping.. get locationdata from trail with object time
       
  1990            TTime timestamp = GetMdeObjectTimeL( iMediaItems[0]->iObjectId );
       
  1991            TRemapItem remapItem;
       
  1992            remapItem.iObjectId = iMediaItems[0]->iObjectId;
       
  1993            remapItem.iTime = timestamp;
       
  1994            remapItem.iLocationId = locationId;
       
  1995            remapItem.iRelationId = relationId;
       
  1996            iRemapper->Append( remapItem );
       
  1997 
  1915            if((iMediaItems[0]->iFlag & KSnapMediaFile) > 0)
  1998            if((iMediaItems[0]->iFlag & KSnapMediaFile) > 0)
  1916                {
  1999                {
  1917                iLastMediaItem = *(iMediaItems[0]);
  2000                iLastMediaItem = *(iMediaItems[0]);
  1918                }
  2001                }
  1919            iMediaItems.Remove(0);
  2002            iMediaItems.Remove(0);
  1950                 iRemapState = ERemapProgressNone;
  2033                 iRemapState = ERemapProgressNone;
  1951                 }
  2034                 }
  1952             else if ( iMediaItems.Count() > 0 )
  2035             else if ( iMediaItems.Count() > 0 )
  1953                 {
  2036                 {
  1954                 TLocationSnapshotItem* item = iMediaItems[0];
  2037                 TLocationSnapshotItem* item = iMediaItems[0];
       
  2038                 // Fails may be becuase of n/w reason..create location + relation so that we can handle at 3:00 AM.
       
  2039                 TItemId locationId = DoCreateLocationL( iMediaItems[0]->iLocationData );
       
  2040                 iMediaItems[0]->iLocationId = locationId;
       
  2041                 TItemId relationId = CreateRelationL( iMediaItems[0]->iObjectId, locationId );
       
  2042 
       
  2043                 // Go for remapping.. get locationdata from trail with object time
       
  2044                 TTime timestamp = GetMdeObjectTimeL( iMediaItems[0]->iObjectId );
       
  2045                 TRemapItem remapItem;
       
  2046                 remapItem.iObjectId = iMediaItems[0]->iObjectId;
       
  2047                 remapItem.iTime = timestamp;
       
  2048                 remapItem.iLocationId = locationId;
       
  2049                 remapItem.iRelationId = relationId;
       
  2050                 iRemapper->Append( remapItem );
       
  2051 
  1955                 iMediaItems.Remove(0);
  2052                 iMediaItems.Remove(0);
  1956                 iMediaItems.Compress();
  2053                 iMediaItems.Compress();
  1957                 iMediaHandlingFlag &= ~KLocationQueryInProgress;
  2054                 iMediaHandlingFlag &= ~KLocationQueryInProgress;
  1958                 delete item;
  2055                 delete item;
  1959                 }
  2056                 }
  2137 
  2234 
  2138     CMdEObject* object = NULL;
  2235     CMdEObject* object = NULL;
  2139     CMdEProperty* property = NULL;
  2236     CMdEProperty* property = NULL;
  2140     
  2237     
  2141     object = iMdeSession->GetObjectL( aObjectId );
  2238     object = iMdeSession->GetObjectL( aObjectId );
       
  2239     if( !object )
       
  2240         {
       
  2241         User::Leave( KErrNotFound );
       
  2242         }
       
  2243     
  2142     CleanupStack::PushL( object );
  2244     CleanupStack::PushL( object );
  2143     object->Property( timeDef, property, 0 );
  2245     object->Property( timeDef, property, 0 );
  2144     if ( !property )
  2246     if ( !property )
  2145         {
  2247         {
  2146         User::Leave( KErrNotFound );
  2248         User::Leave( KErrNotFound );
  2147         }
  2249         }
  2148     
  2250     
  2149     const TTime timeValue( property->TimeValueL() );
  2251     const TTime timeValue( property->TimeValueL() );
  2150     CleanupStack::PopAndDestroy( object );
  2252     CleanupStack::PopAndDestroy( object );
       
  2253     
       
  2254     LOG( "CLocationRecord::GetMdeObjectTimeL(), end" );
  2151     return timeValue;
  2255     return timeValue;
       
  2256         
  2152     }
  2257     }
  2153 
  2258 
  2154 
  2259 
  2155 
  2260 
  2156 // --------------------------------------------------------------------------
  2261 // --------------------------------------------------------------------------
  2207         cond.AddPropertyConditionL( networkCodeDef, ETextPropertyConditionCompareEquals,
  2312         cond.AddPropertyConditionL( networkCodeDef, ETextPropertyConditionCompareEquals,
  2208                 iMediaItems[0]->iLocationData.iNetworkInfo.iNetworkId );
  2313                 iMediaItems[0]->iLocationData.iNetworkInfo.iNetworkId );
  2209             
  2314             
  2210         iNetLocationQuery->FindL(1, 1); 
  2315         iNetLocationQuery->FindL(1, 1); 
  2211         iMediaHandlingFlag |= KLocationQueryInProgress;
  2316         iMediaHandlingFlag |= KLocationQueryInProgress;
  2212     //    iMediaHandlingFlag |= KNetQueryInProgress;
       
  2213         }
  2317         }
  2214     else
  2318     else
  2215         {
  2319         {
  2216         TLocationSnapshotItem* firstPtr = iMediaItems[0];
  2320         TLocationSnapshotItem* firstPtr = iMediaItems[0];
  2217         iMediaItems.Remove(0);
  2321         iMediaItems.Remove(0);
  2459     TItemId countryTagId(0);
  2563     TItemId countryTagId(0);
  2460     TItemId cityTagId(0);
  2564     TItemId cityTagId(0);
  2461     
  2565     
  2462     iMediaHandlingFlag &= (~KReverseGeoCodingInProgress);
  2566     iMediaHandlingFlag &= (~KReverseGeoCodingInProgress);
  2463 	TLocationSnapshotItem* snapshotItem = NULL;
  2567 	TLocationSnapshotItem* snapshotItem = NULL;
       
  2568     LOG1("Error - %d", aErrorcode);
  2464     if( aErrorcode == KErrNone )
  2569     if( aErrorcode == KErrNone )
  2465         {
  2570         {
       
  2571         iLastReverseGeocodeFails = EFalse;
  2466         TPtrC countryPtr( aAddressInfo.GetCountryName() );
  2572         TPtrC countryPtr( aAddressInfo.GetCountryName() );
  2467         TPtrC cityPtr( aAddressInfo.GetCity() );
  2573         TPtrC cityPtr( aAddressInfo.GetCity() );
  2468         TRAP_IGNORE( iTagCreator->CreateLocationTagsL( countryPtr, countryTagId, 
  2574         TRAP_IGNORE( iTagCreator->CreateLocationTagsL( countryPtr, countryTagId, 
  2469                                                       cityPtr, cityTagId ) );
  2575                                                       cityPtr, cityTagId ) );
  2470         if ( iRemapState == ERemapRevGeoCodeInProgress)
  2576         if ( iRemapState == ERemapRevGeoCodeInProgress)
  2480                 iLocationItems[0]->iCityTagId = cityTagId;
  2586                 iLocationItems[0]->iCityTagId = cityTagId;
  2481                 iLastLocationItem = (*iLocationItems[0]);
  2587                 iLastLocationItem = (*iLocationItems[0]);
  2482                 
  2588                 
  2483                 TRAP_IGNORE( iTagCreator->AttachTagsL( 
  2589                 TRAP_IGNORE( iTagCreator->AttachTagsL( 
  2484                                 iLocationItems[0]->iObjectId, countryTagId, cityTagId  ) );
  2590                                 iLocationItems[0]->iObjectId, countryTagId, cityTagId  ) );
       
  2591                 if ( (iLastMediaItem.iFlag & KSnapMediaFile) > 0 
       
  2592                     && iLastMediaItem.iLocationId == iLastLocationItem.iLocationId )
       
  2593                     {
       
  2594                     LOG("Updating country/city\n");
       
  2595                     iLastMediaItem.iCountryTagId = countryTagId;
       
  2596                     iLastMediaItem.iCityTagId = cityTagId;
       
  2597                     }
       
  2598                 
  2485                 }
  2599                 }
  2486             }
  2600             }
  2487         if(iLastMediaItem.iFlag == 0)
       
  2488             {
       
  2489             LOG("Last media item is null\n");
       
  2490             }
       
  2491         if ( iLastMediaItem.iFlag > 0 && iLastMediaItem.iLocationId == iLastLocationItem.iLocationId )
       
  2492             {
       
  2493             LOG("Updating country/city\n");
       
  2494             iLastMediaItem.iCountryTagId = countryTagId;
       
  2495             iLastMediaItem.iCityTagId = cityTagId;
       
  2496             }
       
  2497         
       
  2498         //check other items in the array has same location 
  2601         //check other items in the array has same location 
  2499         for ( TInt index = iLocationItems.Count() - 1; index > 0; index--)
  2602         for ( TInt index = iLocationItems.Count() - 1; index > 0; index--)
  2500             {
  2603             {
  2501             if ( iLocationItems[index]->iLocationId == iLastLocationItem.iLocationId )
  2604             if ( iLocationItems[index]->iLocationId == iLastLocationItem.iLocationId )
  2502                 {
  2605                 {
  2510             }
  2613             }
  2511         }
  2614         }
  2512     else
  2615     else
  2513         {
  2616         {
  2514         //handle error
  2617         //handle error
       
  2618         LOG("Reverse geo coding fails");
       
  2619         iLastReverseGeocodeFails = ETrue;
  2515         }
  2620         }
  2516     
  2621     
  2517     //irrespective of error or not, remove current(first) item to proceed further
  2622     //irrespective of error or not, remove current(first) item to proceed further
  2518     if ( iLocationItems.Count() > 0 )
  2623     if ( iLocationItems.Count() > 0 )
  2519         {
  2624         {