locationmanager/geotagger/src/cinternalgeotagger.cpp
branchRCL_3
changeset 19 b73252188534
equal deleted inserted replaced
18:63c982fb92f2 19:b73252188534
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  A class responsible for geotagging of media files in background.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32cmn.h> 
       
    19 #include <lbserrors.h>
       
    20 #include <lbssatellite.h>
       
    21 #include <ecom.h>
       
    22 
       
    23 #include "locationmanagerdebug.h"
       
    24 #include "mdeconstants.h"
       
    25 #include "mdesession.h"
       
    26 #include "cinternalgeotagger.h"
       
    27 #ifdef LOC_REVERSEGEOCODE
       
    28 #include "reversegeocoderplugin.h"
       
    29 const TUid KReverseGeoCodeUid = {0x2002DD12};
       
    30 #endif
       
    31 
       
    32 using namespace MdeConstants;
       
    33 
       
    34 _LIT ( KCountry, "country:");   // country:india 
       
    35 
       
    36 const TReal64 KZeroLatLon (0.000000 );
       
    37 
       
    38 // --------------------------------------------------------------------------
       
    39 // CInternalGeoTagger::NewL
       
    40 // --------------------------------------------------------------------------
       
    41 //
       
    42 EXPORT_C CInternalGeoTagger* CInternalGeoTagger::NewL( CMdESession* aMdeSession,
       
    43             MGeoTaggerObserver* aObserver )
       
    44     {
       
    45     CInternalGeoTagger* self = 
       
    46             new (ELeave) CInternalGeoTagger( aMdeSession, aObserver );
       
    47     CleanupStack::PushL( self );
       
    48     self->ConstructL();
       
    49     CleanupStack::Pop( self );
       
    50     return self;
       
    51     }
       
    52         
       
    53 // --------------------------------------------------------------------------
       
    54 // CInternalGeoTagger::CInternalGeoTagger
       
    55 // --------------------------------------------------------------------------
       
    56 //  
       
    57 CInternalGeoTagger::CInternalGeoTagger( CMdESession* aMdeSession,
       
    58         MGeoTaggerObserver* aObserver )
       
    59                         : iMdeSession(aMdeSession),
       
    60                         iLocationQuery(NULL),
       
    61                         iRelationQuery(NULL),
       
    62                         iTagQuery(NULL),
       
    63                         iIndex(0),
       
    64                         iSessionReady( EFalse ),
       
    65                         iObserver( aObserver ),
       
    66                         iASW(NULL),
       
    67                         iTagPendingHandlerFlag(EFalse)
       
    68 #ifdef LOC_GEOTAGGING_CELLID
       
    69 						,iGeoConverter(NULL)
       
    70 #endif						
       
    71 #ifdef LOC_REVERSEGEOCODE
       
    72                         ,iTagCreator(NULL)
       
    73                         ,iRevGeocoderPlugin(NULL)
       
    74 #endif						
       
    75     {
       
    76     iMdeSessionOwnFlag = (iMdeSession == NULL);
       
    77     }
       
    78 
       
    79 // --------------------------------------------------------------------------
       
    80 // CInternalGeoTagger::ConstructL
       
    81 // --------------------------------------------------------------------------
       
    82 //    
       
    83 void CInternalGeoTagger::ConstructL()
       
    84     {
       
    85     LOG("CInternalGeoTagger::ConstructL ,begin");
       
    86     if(iMdeSession != NULL)
       
    87         {
       
    88 		LOG("Not owning mds session");
       
    89         iMdeSessionOwnFlag = EFalse;
       
    90         //object is not owning iMdeSession
       
    91         iSessionReady = ETrue;
       
    92 #ifdef LOC_REVERSEGEOCODE
       
    93         iTagCreator = CTagCreator::NewL();
       
    94         
       
    95         iTagCreator->SetSession( iMdeSession );
       
    96 #endif //LOC_REVERSEGEOCODE
       
    97         }
       
    98     else
       
    99         {
       
   100         iMdeSessionOwnFlag = ETrue;
       
   101         iASW = new (ELeave) CActiveSchedulerWait();
       
   102         iMdeSession = CMdESession::NewL( *this );
       
   103         //for creating tags
       
   104 #ifdef LOC_REVERSEGEOCODE
       
   105         iTagCreator = CTagCreator::NewL();
       
   106 #endif
       
   107         
       
   108         iASW->Start();
       
   109         delete iASW;
       
   110         iASW = NULL;
       
   111         }
       
   112     
       
   113     //for reverse geocoding (geo-tagging)
       
   114     
       
   115 #ifdef LOC_REVERSEGEOCODE
       
   116       
       
   117 	   TRAP_IGNORE(
       
   118         iRevGeocoderPlugin = reinterpret_cast<CReverseGeoCoderPlugin*>(
       
   119            REComSession::CreateImplementationL(KReverseGeoCodeUid, iDtorKey));)
       
   120            	
       
   121         if( iRevGeocoderPlugin )
       
   122 			{
       
   123             iRevGeocoderPlugin->AddObserverL(*this); 
       
   124 			}
       
   125 
       
   126 #endif //LOC_REVERSEGEOCODE
       
   127 
       
   128     LOG("CInternalGeoTagger::ConstructL ,end");
       
   129     }
       
   130     
       
   131 // --------------------------------------------------------------------------
       
   132 // CInternalGeoTagger::~CInternalGeoTagger
       
   133 // --------------------------------------------------------------------------
       
   134 //    
       
   135 CInternalGeoTagger::~CInternalGeoTagger()
       
   136     {  
       
   137     LOG("CInternalGeoTagger::~CInternalGeoTagger ,begin");
       
   138 #ifdef LOC_GEOTAGGING_CELLID
       
   139     if(iGeoConverter)
       
   140         {
       
   141         delete iGeoConverter;
       
   142         iGeoConverter = NULL;
       
   143         }
       
   144 #endif    
       
   145 	if(iRelationQuery)
       
   146 		{
       
   147 		iRelationQuery->RemoveObserver(*this);
       
   148 		iRelationQuery->Cancel();
       
   149 		delete iRelationQuery;
       
   150 		iRelationQuery = NULL;
       
   151 		}
       
   152 	if(iLocationQuery)
       
   153 		{
       
   154 		iLocationQuery->RemoveObserver(*this);
       
   155 		iLocationQuery->Cancel();
       
   156 		delete iLocationQuery;
       
   157 		iLocationQuery = NULL;
       
   158 		}
       
   159 	if(iTagQuery)
       
   160 		{
       
   161 		iTagQuery->RemoveObserver(*this);
       
   162 		iTagQuery->Cancel();
       
   163 		delete iTagQuery;
       
   164 		iTagQuery = NULL;
       
   165 		}	
       
   166 #ifdef LOC_REVERSEGEOCODE
       
   167     delete iTagCreator;
       
   168     iTagCreator = NULL;
       
   169     delete iRevGeocoderPlugin;
       
   170     iRevGeocoderPlugin = NULL;
       
   171     REComSession::DestroyedImplementation(iDtorKey);	
       
   172 #endif //LOC_REVERSEGEOCODE
       
   173     if(iMdeSessionOwnFlag)
       
   174         {
       
   175     	delete iMdeSession;
       
   176     	iMdeSession = NULL; 
       
   177         delete iASW;
       
   178         iASW = NULL;
       
   179         }
       
   180     LOG("CInternalGeoTagger::~CInternalGeoTagger ,end");
       
   181     }
       
   182 
       
   183 // --------------------------------------------------------------------------
       
   184 // CInternalGeoTagger::CreateGeoTags
       
   185 // --------------------------------------------------------------------------
       
   186 //
       
   187 void CInternalGeoTagger::CreateGeoTagsL( const TConnectionOption aOption )
       
   188     {  
       
   189     LOG("CInternalGeoTagger::CreateGeoTagsL ,begin");
       
   190     if ( !IsSessionReady() )
       
   191         {
       
   192         User::Leave( KErrNotReady );
       
   193         }
       
   194     
       
   195     iConnectionOption = aOption;
       
   196     iIndex = 0;
       
   197     GetAllLocationsL();
       
   198     
       
   199     LOG("CInternalGeoTagger::CreateGeoTagsL ,end");
       
   200     }
       
   201 
       
   202 // --------------------------------------------------------------------------
       
   203 // CInternalGeoTagger::HandleSessionOpened
       
   204 // --------------------------------------------------------------------------
       
   205 //
       
   206 void CInternalGeoTagger::HandleSessionOpened(CMdESession& /*aSession*/, TInt aError)
       
   207     {
       
   208     LOG("CInternalGeoTagger::HandleSessionOpened ,begin");
       
   209     if ( iASW && iASW->IsStarted() )
       
   210         {
       
   211         iASW->AsyncStop();
       
   212         }
       
   213     
       
   214     if ( KErrNone == aError )
       
   215         {
       
   216         iSessionReady = ETrue;
       
   217         
       
   218 #ifdef LOC_REVERSEGEOCODE
       
   219         iTagCreator->SetSession( iMdeSession );
       
   220 #endif
       
   221         }
       
   222     else 
       
   223         {
       
   224         delete iMdeSession;
       
   225         iMdeSession = NULL;
       
   226         }
       
   227     LOG("CInternalGeoTagger::HandleSessionOpened ,end");
       
   228     }
       
   229 
       
   230 // --------------------------------------------------------------------------
       
   231 // CInternalGeoTagger::HandleSessionError
       
   232 // --------------------------------------------------------------------------
       
   233 //
       
   234 void CInternalGeoTagger::HandleSessionError(CMdESession& /*aSession*/, TInt /*aError*/)
       
   235     {
       
   236     LOG("CInternalGeoTagger::HandleSessionError ,begin");
       
   237     if (iASW && iASW->IsStarted() )
       
   238         {
       
   239         iASW->AsyncStop();
       
   240         }
       
   241     iSessionReady = EFalse;
       
   242 
       
   243 	delete iMdeSession;
       
   244 	iMdeSession = NULL;
       
   245 
       
   246     LOG("CInternalGeoTagger::HandleSessionError ,end");
       
   247     }
       
   248 
       
   249 // --------------------------------------------------------------------------
       
   250 // CInternalGeoTagger::IsSessionReady
       
   251 // --------------------------------------------------------------------------
       
   252 //
       
   253 TBool CInternalGeoTagger::IsSessionReady()
       
   254     {
       
   255     return iSessionReady;
       
   256     }
       
   257 
       
   258 
       
   259 // --------------------------------------------------------------------------
       
   260 // CInternalGeoTagger::GetRelationObjectsL()
       
   261 // --------------------------------------------------------------------------
       
   262 //
       
   263 void CInternalGeoTagger::GetRelationObjectsL(TItemId aLocID)
       
   264 	{
       
   265     LOG("CInternalGeoTagger::GetRelationObjectsL ,begin");
       
   266 	CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
       
   267 	CMdEObjectDef& imageObjDef = namespaceDef.GetObjectDefL( Image::KImageObject );
       
   268 	
       
   269 	if(iRelationQuery)
       
   270 		{
       
   271 		iRelationQuery->RemoveObserver(*this);
       
   272 		iRelationQuery->Cancel();
       
   273 		delete iRelationQuery;
       
   274     	iRelationQuery = NULL;
       
   275 		}
       
   276     
       
   277     iRelationQuery = iMdeSession->NewRelationQueryL( namespaceDef, this );
       
   278     User::LeaveIfNull( iRelationQuery );
       
   279 
       
   280     iRelationQuery->SetResultMode( EQueryResultModeItem );
       
   281     
       
   282     // both left and right condition must match
       
   283     CMdERelationCondition& filterCond = iRelationQuery->Conditions().
       
   284     		AddRelationConditionL( ERelationConditionSideRight );
       
   285 
       
   286     // left one must be any image object.
       
   287     filterCond.LeftL().AddObjectConditionL( imageObjDef );
       
   288 
       
   289     // right one must be this location object 
       
   290     filterCond.RightL().AddObjectConditionL( aLocID );
       
   291 
       
   292     iRelationQuery->FindL(); // results to a call to HandleQueryCompleted()
       
   293     LOG("CInternalGeoTagger::GetRelationObjectsL ,end");
       
   294 	}
       
   295 
       
   296 // --------------------------------------------------------------------------
       
   297 // CInternalGeoTagger::HandleQueryNewResults()
       
   298 // --------------------------------------------------------------------------
       
   299 //
       
   300 void CInternalGeoTagger::HandleQueryNewResults(CMdEQuery& /*aQuery*/, TInt /*aFirstNewItemIndex*/, 
       
   301 		TInt /*aNewItemCount*/)
       
   302 	{
       
   303 	LOG("CInternalGeoTagger::HandleQueryNewResults ,begin");
       
   304 	}
       
   305 
       
   306 // --------------------------------------------------------------------------
       
   307 // CInternalGeoTagger::HandleQueryCompleted()
       
   308 // --------------------------------------------------------------------------
       
   309 //
       
   310 void CInternalGeoTagger::HandleQueryCompleted(CMdEQuery& aQuery, TInt aError)
       
   311     {
       
   312     LOG("CInternalGeoTagger::HandleQueryCompleted ,begin");
       
   313 
       
   314     TInt count = aQuery.Count();
       
   315     LOG1("CInternalGeoTagger::HandleQueryCompleted count: %d", count);
       
   316         
       
   317     if ( &aQuery == iLocationQuery )
       
   318         {
       
   319         if ( count )
       
   320             {
       
   321             iLocationId = iLocationQuery->ResultId( iIndex++ );
       
   322             TRAP_IGNORE( GetRelationObjectsL( iLocationId ) );
       
   323             }
       
   324         else 
       
   325             {
       
   326             iIndex = 0;
       
   327             iObserver->GeoTaggingCompleted( aError );
       
   328             if(iTagPendingHandlerFlag)
       
   329                 {
       
   330                 iTagPendingHandlerFlag = EFalse;
       
   331                 iObserver->PendingGeoTagReqComplete(KErrNone);
       
   332                 }
       
   333             }  
       
   334         }
       
   335     else if ( &aQuery == iRelationQuery )
       
   336         {
       
   337         //if it is a relation query, then update gps info of location object.
       
   338      
       
   339         if ( iRelationQuery->Count() )
       
   340             {
       
   341             TInt err = KErrNone;
       
   342             TRAP(err, StartGeoTaggingL( ) );              
       
   343             if(err != KErrNone)
       
   344                 {
       
   345                 IterateNextLocation();
       
   346                 }
       
   347             }
       
   348         else
       
   349             {
       
   350             IterateNextLocation();
       
   351             }
       
   352         
       
   353         }
       
   354 		
       
   355     else if ( &aQuery == iTagQuery )
       
   356         {
       
   357                        
       
   358         if ( LocationTagExists() )  //nothing to do for this location
       
   359             {
       
   360             IterateNextLocation();  //Go through next location
       
   361             }
       
   362         else   //do reverse geocoding
       
   363             {          
       
   364             if(iTagPendingHandlerFlag)
       
   365                 {
       
   366                 iIndex = 0;
       
   367                 iTagPendingHandlerFlag = EFalse;
       
   368                 iObserver->PendingGeoTagReqComplete(KErrNotFound);
       
   369                 return;
       
   370                 }
       
   371 
       
   372             //Find the address for this geo-coordinate
       
   373             if(GPSInfoExists())
       
   374                 {
       
   375                 // GPS info exist go for reverse geo coding.
       
   376 #ifdef LOC_REVERSEGEOCODE
       
   377                 if(iRevGeocoderPlugin)
       
   378     			    {
       
   379                     TRAP_IGNORE( 
       
   380     				iRevGeocoderPlugin->GetAddressByCoordinateL
       
   381     				   ( iLocationData.iPosition, iConnectionOption ) );
       
   382     				}
       
   383     			else
       
   384 #endif //LOC_REVERSEGEOCODE
       
   385     			    {
       
   386                     IterateNextLocation();  //Go through next location
       
   387     			    }
       
   388     			}
       
   389     		else
       
   390     		    {
       
   391         		if ( iLocationData.iNetworkInfo.iCellId > 0 && 
       
   392         				iLocationData.iNetworkInfo.iCountryCode.Length() > 0 &&
       
   393         				iLocationData.iNetworkInfo.iNetworkId.Length() > 0 )
       
   394         			{
       
   395         			// valid n/w information.. go for cell id based geo coding.
       
   396 #ifdef LOC_GEOTAGGING_CELLID
       
   397                     TInt err = KErrNone;
       
   398                     if(iGeoConverter == NULL)
       
   399                        {
       
   400                        TRAP(err, iGeoConverter = CGeoConverter::NewL(*this));
       
   401                        }
       
   402                    
       
   403                     if(err == KErrNone && iGeoConverter != NULL)
       
   404                        {
       
   405                        // there is no field to store type of network. 
       
   406                        // but it's safe to take decission based on area code.
       
   407                        if(iLocationData.iNetworkInfo.iLocationAreaCode > 0)
       
   408                            {
       
   409                            LOG("Valid areacode. Treat as GSM n/w");
       
   410                            iLocationData.iNetworkInfo.iAccess = CTelephony::ENetworkAccessGsm;
       
   411                            }
       
   412                        else
       
   413                            {
       
   414                            LOG("Areacode is 0. Treat as 3G n/w");
       
   415                            iLocationData.iNetworkInfo.iAccess = CTelephony::ENetworkAccessUtran;
       
   416                            }
       
   417                        TRAP(err, iGeoConverter->ConvertL(iLocationData.iNetworkInfo);)
       
   418                        }
       
   419                     LOG1("Error - %d", err);
       
   420                    if(err != KErrNone)
       
   421                        {
       
   422                        HandleConversionError(err);	
       
   423                        }
       
   424 #else
       
   425                     IterateNextLocation();  //Go through next location
       
   426 #endif                    
       
   427         			}
       
   428         		else
       
   429         		    {
       
   430                     IterateNextLocation();  //Go through next location
       
   431         		    }
       
   432     		    }
       
   433             }
       
   434         
       
   435         }
       
   436 
       
   437     LOG("CInternalGeoTagger::HandleQueryCompleted ,end");
       
   438     }
       
   439 
       
   440 
       
   441 // --------------------------------------------------------------------------
       
   442 // CInternalGeoTagger::GPSInfoExists()
       
   443 // --------------------------------------------------------------------------
       
   444 //
       
   445 TBool CInternalGeoTagger::GPSInfoExists()
       
   446     {
       
   447     LOG("CInternalGeoTagger::GPSInfoExists");
       
   448     TBool gpsInfoExists = EFalse;
       
   449     if ( !Math::IsNaN( iLocationData.iPosition.Latitude() ) && 
       
   450                 !Math::IsNaN( iLocationData.iPosition.Longitude() ) &&
       
   451                 KZeroLatLon != iLocationData.iPosition.Latitude() && 
       
   452                 KZeroLatLon != iLocationData.iPosition.Longitude() )  //lat, long is there
       
   453         {
       
   454         
       
   455 		LOG("Valid lat/lon\n");
       
   456         gpsInfoExists = ETrue;
       
   457         }
       
   458     return gpsInfoExists;
       
   459     }
       
   460 
       
   461 
       
   462 // --------------------------------------------------------------------------
       
   463 // CInternalGeoTagger::StartGeoTagging()
       
   464 // --------------------------------------------------------------------------
       
   465 //
       
   466 void CInternalGeoTagger::StartGeoTaggingL( )
       
   467     {       
       
   468     LOG("CInternalGeoTagger::StartGeoTaggingL ,start");
       
   469     GetLocationInfoL();
       
   470     
       
   471     CMdERelation& relation = iRelationQuery->Result( 0 );
       
   472     
       
   473     GetTagsL( relation.LeftObjectId() );
       
   474 
       
   475 	LOG("CInternalGeoTagger::StartGeoTaggingL ,end");
       
   476     }
       
   477 // --------------------------------------------------------------------------
       
   478 // CInternalGeoTagger::GetLocationInfoL()
       
   479 // --------------------------------------------------------------------------
       
   480 //
       
   481 void CInternalGeoTagger::GetLocationInfoL()
       
   482     {
       
   483     LOG("CInternalGeoTagger::GetLocationInfoL, begin");
       
   484 
       
   485     CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
       
   486 
       
   487     CMdEObjectDef& locationObjectDef = namespaceDef.GetObjectDefL( Location::KLocationObject );
       
   488     
       
   489     CMdEPropertyDef& latitudeDef = locationObjectDef.GetPropertyDefL( 
       
   490                                         Location::KLatitudeProperty );
       
   491     CMdEPropertyDef& longitudeDef = locationObjectDef.GetPropertyDefL( 
       
   492                                         Location::KLongitudeProperty );
       
   493     CMdEPropertyDef& cellIdDef = locationObjectDef.GetPropertyDefL( 
       
   494                                         Location::KCellIdProperty );
       
   495     CMdEPropertyDef& lacCodeDef = locationObjectDef.GetPropertyDefL( 
       
   496                                         Location::KLocationAreaCodeProperty );
       
   497     CMdEPropertyDef& countryCodeDef = locationObjectDef.GetPropertyDefL( 
       
   498                                         Location::KCountryCodeProperty );
       
   499     CMdEPropertyDef& networkCodeDef = locationObjectDef.GetPropertyDefL( 
       
   500                                         Location::KNetworkCodeProperty );
       
   501     
       
   502     CMdEObject* locationObject = NULL;
       
   503     CMdEProperty* latProp = NULL;
       
   504     CMdEProperty* lonProp = NULL;
       
   505     CMdEProperty* cellProp = NULL;
       
   506     CMdEProperty* lacProp = NULL;
       
   507     CMdEProperty* countryProp = NULL;
       
   508     CMdEProperty* networkProp = NULL;
       
   509        
       
   510     locationObject = iMdeSession->GetObjectL( iLocationId, locationObjectDef );
       
   511     
       
   512     locationObject->Property( latitudeDef, latProp, 0 );
       
   513     locationObject->Property( longitudeDef, lonProp, 0 );
       
   514     if ( latProp && lonProp )
       
   515         {
       
   516         iLocationData.iPosition.SetCoordinate( latProp->Real64ValueL(), 
       
   517                                                lonProp->Real64ValueL() );
       
   518         }
       
   519     else
       
   520         {
       
   521         iLocationData.iPosition.SetCoordinate( KZeroLatLon, 
       
   522                                                KZeroLatLon );
       
   523         }
       
   524     
       
   525     locationObject->Property( cellIdDef, cellProp, 0 );
       
   526     locationObject->Property( lacCodeDef, lacProp, 0 );
       
   527     locationObject->Property( countryCodeDef, countryProp, 0 );
       
   528     locationObject->Property( networkCodeDef, networkProp, 0 );
       
   529     if ( cellProp && countryProp && networkProp)
       
   530         {
       
   531         iLocationData.iNetworkInfo.iCellId = cellProp->Uint32ValueL();
       
   532         iLocationData.iNetworkInfo.iCountryCode = countryProp->TextValueL();
       
   533         iLocationData.iNetworkInfo.iNetworkId = networkProp->TextValueL();
       
   534         }
       
   535     else
       
   536         {
       
   537         // set to invalid values.
       
   538         iLocationData.iNetworkInfo.iCellId = 0;
       
   539         iLocationData.iNetworkInfo.iAccess = CTelephony::ENetworkAccessUnknown;
       
   540         iLocationData.iNetworkInfo.iLocationAreaCode = 0;
       
   541         iLocationData.iNetworkInfo.iAreaKnown = EFalse;
       
   542         iLocationData.iNetworkInfo.iCountryCode.Zero();
       
   543         iLocationData.iNetworkInfo.iNetworkId.Zero();
       
   544         }
       
   545 
       
   546     iLocationData.iNetworkInfo.iAreaKnown = EFalse;
       
   547     if(lacProp)
       
   548         {
       
   549         iLocationData.iNetworkInfo.iLocationAreaCode = lacProp->Uint32ValueL();
       
   550         if(iLocationData.iNetworkInfo.iLocationAreaCode > 0)
       
   551             {
       
   552             iLocationData.iNetworkInfo.iAreaKnown = ETrue;
       
   553             }
       
   554         }
       
   555     delete locationObject;
       
   556 	LOG("CInternalGeoTagger::GetLocationInfoL ,end");
       
   557     }
       
   558 
       
   559 // --------------------------------------------------------------------------
       
   560 // CInternalGeoTagger::LocationTagExists()
       
   561 // --------------------------------------------------------------------------
       
   562 //
       
   563 TBool CInternalGeoTagger::LocationTagExists()
       
   564     {
       
   565     LOG("CInternalGeoTagger::LocationTagExists ,begin");
       
   566                            
       
   567     TBool tagFound = EFalse;
       
   568     TInt error = KErrNone;
       
   569     const TInt count = iTagQuery->Count();
       
   570                 
       
   571     for ( TInt i = 0; i < count; i++ )
       
   572         {
       
   573         CMdERelation& relation =  static_cast<CMdERelation&>( iTagQuery->ResultItem( i ) );
       
   574                     
       
   575         TItemId tagId = relation.RightObjectId(); 
       
   576         CMdEObject* object = NULL;
       
   577         TRAP_IGNORE( object = iMdeSession->GetObjectL( tagId ) );
       
   578         if(object)
       
   579             {
       
   580             error = object->Uri().Find( KCountry );
       
   581             delete object;
       
   582             if ( error == KErrNone )
       
   583                 {
       
   584                 i = count;
       
   585                 tagFound = ETrue;
       
   586                 break;
       
   587                 }
       
   588             }
       
   589         }
       
   590                 
       
   591            
       
   592 	LOG("CInternalGeoTagger::LocationTagExists ,end");
       
   593     return tagFound;
       
   594 
       
   595     }
       
   596 
       
   597 
       
   598 // --------------------------------------------------------------------------
       
   599 // CInternalGeoTagger::IterateNextLocation()
       
   600 // --------------------------------------------------------------------------
       
   601 //
       
   602 void CInternalGeoTagger::IterateNextLocation()
       
   603     {
       
   604     LOG("CInternalGeoTagger::IterateNextLocation ,begin");
       
   605                                    
       
   606     if (iIndex < iLocationQuery->Count())
       
   607         {
       
   608         iLocationId = iLocationQuery->ResultId( iIndex++ );
       
   609         TRAP_IGNORE( GetRelationObjectsL( iLocationId ) );
       
   610         }
       
   611     else
       
   612         {
       
   613         if ( iObserver )
       
   614             {
       
   615             iIndex = 0;
       
   616             TInt error = KErrNone;
       
   617             iObserver->GeoTaggingCompleted( error );
       
   618             if(iTagPendingHandlerFlag)
       
   619                 {
       
   620                 iTagPendingHandlerFlag = EFalse;
       
   621                 iObserver->PendingGeoTagReqComplete(KErrNone);
       
   622                 }
       
   623             }
       
   624         }
       
   625 	LOG("CInternalGeoTagger::IterateNextLocation ,end");
       
   626     }
       
   627 
       
   628 // --------------------------------------------------------------------------
       
   629 // CInternalGeoTagger::GetAllLocationsL()
       
   630 // --------------------------------------------------------------------------
       
   631 //
       
   632 void CInternalGeoTagger::GetAllLocationsL()
       
   633     {
       
   634     LOG("CInternalGeoTagger::GetAllLocationsL ,begin");
       
   635 
       
   636     CMdENamespaceDef& defaultNamespaceDef = iMdeSession->GetDefaultNamespaceDefL();
       
   637     CMdEObjectDef& locObjDef = defaultNamespaceDef.GetObjectDefL( 
       
   638                                MdeConstants::Location::KLocationObject );
       
   639     CMdEPropertyDef& cellIdPropDef = locObjDef.GetPropertyDefL( 
       
   640                                MdeConstants::Location::KCellIdProperty );
       
   641 
       
   642 	if(iLocationQuery)
       
   643 		{
       
   644 		iLocationQuery->RemoveObserver(*this);
       
   645 		iLocationQuery->Cancel();
       
   646 		delete iLocationQuery;
       
   647 		iLocationQuery = NULL;
       
   648 		}
       
   649     // query objects with object definition "Image"
       
   650     iLocationQuery = iMdeSession->NewObjectQueryL( defaultNamespaceDef, locObjDef, this );
       
   651     
       
   652     //Define the result order
       
   653     iLocationQuery->AppendOrderRuleL(TMdEOrderRule(cellIdPropDef, ESortAscending));
       
   654      
       
   655     iLocationQuery->SetResultMode( EQueryResultModeId );
       
   656     iLocationQuery->FindL();
       
   657 
       
   658   LOG("CInternalGeoTagger::GetAllLocationsL ,end");
       
   659     }
       
   660 
       
   661 
       
   662 // --------------------------------------------------------------------------
       
   663 // CInternalGeoTagger::GetTagsL()
       
   664 // Get tags related to this image/video object
       
   665 // --------------------------------------------------------------------------
       
   666 //
       
   667 void CInternalGeoTagger::GetTagsL( TItemId aImageID )
       
   668     {
       
   669     LOG("CInternalGeoTagger::GetTagsL ,begin");
       
   670     CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
       
   671     CMdEObjectDef& tagObjectDef = namespaceDef.GetObjectDefL( Tag::KTagObject );
       
   672         
       
   673 	if(iTagQuery)
       
   674 		{
       
   675 		iTagQuery->RemoveObserver(*this);
       
   676 		iTagQuery->Cancel();
       
   677 		delete iTagQuery;
       
   678         iTagQuery = NULL;
       
   679 		}	
       
   680         
       
   681     iTagQuery = iMdeSession->NewRelationQueryL( namespaceDef, this );
       
   682     User::LeaveIfNull( iTagQuery );
       
   683 
       
   684     iTagQuery->SetResultMode( EQueryResultModeItem );
       
   685         
       
   686     // both left and right condition must match
       
   687     CMdERelationCondition& filterCond = iTagQuery->Conditions().
       
   688     AddRelationConditionL( ERelationConditionSideRight );
       
   689 
       
   690     // left one must be this image object.
       
   691     filterCond.LeftL().AddObjectConditionL( aImageID );
       
   692 
       
   693     // right one must be tag object 
       
   694     filterCond.RightL().AddObjectConditionL( tagObjectDef );
       
   695 
       
   696     iTagQuery->FindL(); // results to a call to HandleQueryCompleted()
       
   697     LOG("CInternalGeoTagger::GetTagsL ,end");
       
   698     }
       
   699 
       
   700 #ifdef LOC_GEOTAGGING_CELLID	
       
   701 
       
   702 // --------------------------------------------------------------------------
       
   703 // CInternalGeoTagger::UpdateGPSInfoL()
       
   704 // --------------------------------------------------------------------------
       
   705 //
       
   706 void CInternalGeoTagger::UpdateGPSInfoL(const TLocality& aPosition)
       
   707 	{
       
   708 	
       
   709     LOG("CInternalGeoTagger::UpdateGPSInfoL ,start");
       
   710     if(Math::IsNaN( aPosition.Latitude()) ||
       
   711         Math::IsNaN( aPosition.Longitude()) )
       
   712         {
       
   713         LOG("Not a valid location info.");
       
   714         LOG("CInternalGeoTagger::UpdateGPSInfoL ,end");
       
   715         return;
       
   716         }
       
   717     
       
   718 	CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
       
   719 	CMdEObjectDef& locObjDef = namespaceDef.GetObjectDefL( Location::KLocationObject );
       
   720 	CMdEObject* location = iMdeSession->OpenObjectL(iLocationId, locObjDef);
       
   721 	CleanupStack::PushL( location );
       
   722 	
       
   723 	CMdEPropertyDef& propLatDef = locObjDef.GetPropertyDefL( Location::KLatitudeProperty );
       
   724 	CMdEPropertyDef& propLongDef = locObjDef.GetPropertyDefL( Location::KLongitudeProperty );
       
   725 	CMdEPropertyDef& propAltDef = locObjDef.GetPropertyDefL( Location::KAltitudeProperty );
       
   726 	CMdEPropertyDef& qualityDef = locObjDef.GetPropertyDefL( Location::KQualityProperty );
       
   727 
       
   728 	if (location->PropertyCount(propLatDef) == 0)
       
   729 		{
       
   730 		location->AddReal64PropertyL(propLatDef, aPosition.Latitude() ); //iLatitude
       
   731 		}
       
   732 	if (location->PropertyCount(propLongDef) == 0)
       
   733 		{
       
   734 		location->AddReal64PropertyL(propLongDef, aPosition.Longitude() ); //iLongitude
       
   735 		}
       
   736 	if (location->PropertyCount(propAltDef) == 0 &&
       
   737         !Math::IsNaN( aPosition.Altitude()) )
       
   738 		{
       
   739 		location->AddReal64PropertyL(propAltDef, aPosition.Altitude());
       
   740 		}
       
   741     LOG1( "Updating quality - %d", aPosition.HorizontalAccuracy());
       
   742 	if (location->PropertyCount(qualityDef) == 0 )
       
   743 		{
       
   744 		location->AddReal32PropertyL(qualityDef, aPosition.HorizontalAccuracy());
       
   745 		}
       
   746 	
       
   747 	CMdEProperty* modProp = NULL;
       
   748 	CMdEObjectDef& objImageDef = namespaceDef.GetObjectDefL( Image::KImageObject );
       
   749 	CMdEPropertyDef& propModifiedDef = objImageDef.GetPropertyDefL( Object::KLastModifiedDateProperty );
       
   750 	location->Property( propModifiedDef, modProp, 0 );
       
   751 	if ( modProp )
       
   752 		{
       
   753 		TTime timestamp( 0 );
       
   754 		timestamp.UniversalTime();
       
   755 		modProp->SetTimeValueL( timestamp );
       
   756 		}
       
   757 	// commit to DB
       
   758 	iMdeSession->CommitObjectL(*location);
       
   759 	CleanupStack::PopAndDestroy( location );
       
   760 
       
   761     // update the relation
       
   762     const TInt count = iRelationQuery->Count();
       
   763     LOG1("Relation count - %d", count);
       
   764     for ( TInt i = 0; i < count; i++ )
       
   765         {
       
   766         CMdERelation& relation = iRelationQuery->Result( i );
       
   767         // update relation timestamp, composer will then update exif data   
       
   768         
       
   769         TTime timestamp( 0 );
       
   770         timestamp.UniversalTime();
       
   771         relation.SetLastModifiedDate( timestamp );     
       
   772         iMdeSession->UpdateRelationL( relation );
       
   773         }
       
   774     LOG("CInternalGeoTagger::UpdateGPSInfoL ,end");
       
   775 	}
       
   776 
       
   777 // --------------------------------------------------------------------------
       
   778 // CInternalGeoTagger::ConversionCompletedL()
       
   779 // --------------------------------------------------------------------------
       
   780 //
       
   781 
       
   782 void CInternalGeoTagger::ConversionCompletedL( const TInt aError, 
       
   783                     TLocality& aPosition )
       
   784     {
       
   785     LOG1("CInternalGeoTagger::ConversionCompletedL, begin. Error - %d", aError);
       
   786     if(aError == KErrNone)
       
   787         {
       
   788         iLocationData.iPosition.SetCoordinate
       
   789                 ( aPosition.Latitude(), aPosition.Longitude(), aPosition.Altitude());
       
   790         iLocationData.iQuality = aPosition.HorizontalAccuracy();
       
   791         UpdateGPSInfoL(aPosition);
       
   792 #ifdef LOC_REVERSEGEOCODE
       
   793         if(iRevGeocoderPlugin)
       
   794 		    {
       
   795            	iRevGeocoderPlugin->GetAddressByCoordinateL
       
   796 			   ( iLocationData.iPosition, iConnectionOption ) ;
       
   797 			}
       
   798 		else
       
   799 #endif //LOC_REVERSEGEOCODE
       
   800 		    {
       
   801             IterateNextLocation();  //Go through next location
       
   802 		    }
       
   803        }
       
   804 	else
       
   805 	    {
       
   806         IterateNextLocation();  //Go through next location
       
   807 	    }
       
   808     LOG("CInternalGeoTagger::ConversionCompletedL, end");
       
   809     }
       
   810 
       
   811 
       
   812 //-----------------------------------------------------------------
       
   813 // CInternalGeoTagger::HandleConversionError
       
   814 //------------------------------------------------------------------
       
   815 //
       
   816 void CInternalGeoTagger::HandleConversionError(TInt aError)
       
   817 	{
       
   818     LOG("CInternalGeoTagger::HandleConversionError, begin");
       
   819     ARG_USED(aError);
       
   820     IterateNextLocation();  //Go through next location
       
   821     LOG("CInternalGeoTagger::HandleConversionError, end");
       
   822 	}
       
   823 
       
   824 #endif //LOC_GEOTAGGING_CELLID
       
   825 
       
   826 // --------------------------------------------------------------------------
       
   827 // CInternalGeoTagger::PendingGeoTagsL()
       
   828 // Is there any untagged photos with location info?.
       
   829 // --------------------------------------------------------------------------
       
   830 //
       
   831 void CInternalGeoTagger::PendingGeoTagsL( TBool aTagInProgress )
       
   832     {
       
   833     LOG("CInternalGeoTagger::PendingGeoTagsL ,begin");
       
   834     iIndex = 0;
       
   835     if ( aTagInProgress )
       
   836         {
       
   837         //Tagging is going through location trail, so photo does not need to do tagging now
       
   838         LOG("Geo tagging in progress.\n");
       
   839         iObserver->PendingGeoTagReqComplete( KErrInUse );
       
   840         }
       
   841     else
       
   842         {
       
   843         if(!iTagPendingHandlerFlag && iIndex <= 0)
       
   844             {
       
   845             LOG("Processing the request.\n");
       
   846             // tag pending request is not going on
       
   847             iTagPendingHandlerFlag = ETrue;
       
   848             CreateGeoTagsL();
       
   849             }
       
   850         else
       
   851             {
       
   852             // req osn progress, don't do anything.
       
   853             LOG("Request in progress.\n");
       
   854             iObserver->PendingGeoTagReqComplete( KErrInUse );
       
   855             }
       
   856         }
       
   857     LOG("CInternalGeoTagger::PendingGeoTagsL ,end");
       
   858     }
       
   859 
       
   860 
       
   861 #ifdef LOC_REVERSEGEOCODE
       
   862 // --------------------------------------------------------------------------
       
   863 // CInternalGeoTagger::AddressInfo()
       
   864 // --------------------------------------------------------------------------
       
   865 //
       
   866 void CInternalGeoTagger::AddressInfoL( const TItemId aCountryTagId, const TItemId aCityTagId )
       
   867     {
       
   868     LOG("CInternalGeoTagger::AddressInfoL ,begin");
       
   869                                
       
   870     const TInt count = iRelationQuery->Count();
       
   871     
       
   872     for ( TInt i = 0; i < count; i++ )
       
   873         {
       
   874         CMdERelation& relation = iRelationQuery->Result( i );
       
   875         TItemId imageId = relation.LeftObjectId(); 
       
   876         
       
   877         if ( !GPSInfoExists() )   //to update EXIF
       
   878             {
       
   879             // update relation timestamp, composer will then update exif data   
       
   880             TTime timestamp( 0 );
       
   881             timestamp.UniversalTime();
       
   882             relation.SetLastModifiedDate( timestamp );     
       
   883             iMdeSession->UpdateRelationL( relation );
       
   884             }
       
   885         
       
   886         iTagCreator->AttachTagsL( imageId, aCountryTagId, aCityTagId  );
       
   887         }
       
   888 
       
   889    LOG("CInternalGeoTagger::AddressInfoL ,end");
       
   890     }
       
   891 
       
   892 
       
   893 // --------------------------------------------------------------------------
       
   894 // CInternalGeoTagger::ReverseGeocodeComplete()
       
   895 //  Get address details like street, city, state, etc.
       
   896 // --------------------------------------------------------------------------
       
   897 //
       
   898 void CInternalGeoTagger::ReverseGeocodeComplete( TInt& aErrorcode, MAddressInfo& aAddressInfo )
       
   899     {
       
   900     LOG("CInternalGeoTagger::ReverseGeocodeComplete ,begin");
       
   901     TItemId countryTagId(0);
       
   902     TItemId cityTagId(0);
       
   903     
       
   904     // create country and city tags
       
   905     if( aErrorcode == KErrNone )
       
   906         {
       
   907         TPtrC countryPtr( aAddressInfo.GetCountryName() ); 
       
   908         TPtrC cityPtr( aAddressInfo.GetCity() );
       
   909         
       
   910         TRAP_IGNORE( iTagCreator->CreateLocationTagsL( countryPtr, countryTagId, cityPtr, cityTagId ) );
       
   911 		
       
   912         TRAP_IGNORE(AddressInfoL( countryTagId, cityTagId ));
       
   913         
       
   914         IterateNextLocation();
       
   915         }
       
   916     else
       
   917         {
       
   918         LOG1("Reverse geocode err - %d", aErrorcode);
       
   919 
       
   920         IterateNextLocation();
       
   921         }
       
   922    
       
   923     
       
   924     LOG("CInternalGeoTagger::ReverseGeocodeComplete ,end");
       
   925     }
       
   926 
       
   927 // ----------------------------------------------------------------------------
       
   928 // CInternalGeoTagger::GetCurrentRegisterNw()
       
   929 // ---------------------------------------------------------------------------- 
       
   930 RMobilePhone::TMobilePhoneNetworkInfoV2& CInternalGeoTagger::GetCurrentRegisterNw()
       
   931     {
       
   932     LOG( "CInternalGeoTagger::GetCurrentRegisterNw ,begin" );
       
   933     return iObserver->GetCurrentRegisterNw();
       
   934     }
       
   935 
       
   936 // ----------------------------------------------------------------------------
       
   937 // CInternalGeoTagger::IsRegisteredAtHomeNetwork()
       
   938 // ---------------------------------------------------------------------------- 
       
   939 TBool CInternalGeoTagger::IsRegisteredAtHomeNetwork()
       
   940     {
       
   941     LOG( "CInternalGeoTagger::IsRegisteredAtHomeNetwork" );
       
   942     return iObserver->IsRegisteredAtHomeNetwork();
       
   943     }
       
   944 
       
   945 // ----------------------------------------------------------------------------
       
   946 // CInternalGeoTagger::GetHomeNetworkInfo()
       
   947 // ----------------------------------------------------------------------------
       
   948 const RMobilePhone::TMobilePhoneNetworkInfoV1& 
       
   949         CInternalGeoTagger::GetHomeNetworkInfo(TBool& aHomeNwInfoAvailableFlag)
       
   950     {
       
   951     LOG( "CInternalReverseGeocode::GetHomeNetworkInfo" );
       
   952     return iObserver->GetHomeNetworkInfo(aHomeNwInfoAvailableFlag);
       
   953     }
       
   954 
       
   955 #endif //LOC_REVERSEGEOCODE
       
   956 
       
   957 // End of file
       
   958