locationdataharvester/mylocationsengine/src/mylocationsengine.cpp
changeset 26 f3533f6eae3f
parent 20 cd10d5b85554
child 30 96df3ab41000
equal deleted inserted replaced
24:ccec19943943 26:f3533f6eae3f
    30 #include "mylocationsdefines.h"
    30 #include "mylocationsdefines.h"
    31 #include "geocodeupdate.h" //header for GeocodeUpdate class
    31 #include "geocodeupdate.h" //header for GeocodeUpdate class
    32 //handle for CMyLocationsHistoryDbObserver class
    32 //handle for CMyLocationsHistoryDbObserver class
    33 #include "mylocationlogger.h"
    33 #include "mylocationlogger.h"
    34 #if ( defined __WINSCW__ ) || ( defined __WINS__ )
    34 #if ( defined __WINSCW__ ) || ( defined __WINS__ )
    35 _LIT ( KImageStorageDrive, "C:\\Maptile\\");
    35 _LIT ( KImageStorageDrive, "C:\\MapTile\\");
    36 #endif
    36 #endif
    37 _LIT(KFolderName,":\\MapTile\\");
    37 _LIT(KFolderName,":\\MapTile\\");
    38 const TInt KImagePathSize=36;
    38 const TInt KImagePathSize=36;
    39 const TInt KDefaultFilePathSize = 20;
    39 const TInt KDefaultFilePathSize = 20;
    40 
       
    41 // separator
       
    42 _LIT( KSeparator, ",");
       
    43 _LIT(KPNGType, ".png");
       
    44 _LIT(KSingleSpace, " ");
       
    45 
    40 
    46 // ============================ MEMBER FUNCTIONS ===============================
    41 // ============================ MEMBER FUNCTIONS ===============================
    47 
    42 
    48 CMyLocationsEngine* CMyLocationsEngine::NewL()
    43 CMyLocationsEngine* CMyLocationsEngine::NewL()
    49 {
    44 {
    86     iMaptileDatabase = CLookupMapTileDatabase::NewL(KMapTileLookupDatabaseName);
    81     iMaptileDatabase = CLookupMapTileDatabase::NewL(KMapTileLookupDatabaseName);
    87 
    82 
    88     MYLOCLOGSTRING("Maptile Db instance created ");
    83     MYLOCLOGSTRING("Maptile Db instance created ");
    89     
    84     
    90     iAddressCompare = CAddressComparision::NewL();
    85     iAddressCompare = CAddressComparision::NewL();
       
    86 	
       
    87     iMyLocationThreeAMTimer = CLocationGeoTagTimerAO::NewL(*this);
       
    88   
       
    89     iMyLocationThreeAMTimer->StartTimer();
       
    90     
       
    91     MYLOCLOGSTRING(" iMyLocationThreeAMTimer = CLocationGeoTagTimerAO::NewL(this)");
       
    92   
       
    93     //Create instance of contact manager 
       
    94     iContactManager = new QContactManager();
    91 
    95 
    92     MYLOCLOGSTRING(" start contact db observation ");
    96     MYLOCLOGSTRING(" start contact db observation ");
    93     StartContactsChangeNotifierL();
    97     StartContactsChangeNotifierL();
    94 
    98 
    95     //set the folder path to store maptile
    99     //set the folder path to store maptile
    96     imageFilePath.Zero();
   100     imageFilePath.Zero();
    97     SetFolderPathL();
   101     SetFolderPathL();
    98 
   102 
    99     TInt status;
   103     TInt status = KErrNone;
   100     iCalSession = CCalSession::NewL();
   104     iCalSession = CCalSession::NewL();
   101     NotifyChangeL(status);
   105     NotifyChangeL(status);
   102 
   106 
   103     // Start listening to landmarks db changes
   107     // Start listening to landmarks db changes
   104     StartLandmarksChangeNotifier();
   108     StartLandmarksChangeNotifier();   
   105 
   109 
   106 }
   110 }
   107 // -----------------------------------------------------------------------------
   111 // -----------------------------------------------------------------------------
   108 // CMyLocationsEngine::SetFolderPath()
   112 // CMyLocationsEngine::SetFolderPath()
   109 // set folder path structure
   113 // set folder path structure
   112 void CMyLocationsEngine::SetFolderPathL()
   116 void CMyLocationsEngine::SetFolderPathL()
   113 {
   117 {
   114     __TRACE_CALLSTACK;
   118     __TRACE_CALLSTACK;
   115     RFs session;
   119     RFs session;
   116     User::LeaveIfError(session.Connect());
   120     User::LeaveIfError(session.Connect());
       
   121    
   117     if ( imageFilePath.Length() > 0 && BaflUtils::FolderExists(session, imageFilePath))
   122     if ( imageFilePath.Length() > 0 && BaflUtils::FolderExists(session, imageFilePath))
   118     {
   123     {
   119         session.Close();
   124         session.Close();
   120         MYLOCLOGSTRING("Image folder path exists");
   125         MYLOCLOGSTRING("Image folder path exists");
   121         return;
   126         return;
   207             iCalSession(NULL), iCalView(NULL), 
   212             iCalSession(NULL), iCalView(NULL), 
   208 	        iContactsDb(NULL), iContactChangeNotifier(NULL), 
   213 	        iContactsDb(NULL), iContactChangeNotifier(NULL), 
   209 			iLandmarkDb(NULL),
   214 			iLandmarkDb(NULL),
   210             iMapTileInterface(NULL), iMyLocationsDatabaseManager(NULL),
   215             iMapTileInterface(NULL), iMyLocationsDatabaseManager(NULL),
   211             iMaptileDatabase(NULL), iAddressCompare(NULL),
   216             iMaptileDatabase(NULL), iAddressCompare(NULL),
   212             iMaptileGeocoderPluginAvailable(EFalse),iCalenderNotification(NULL)
   217             iMyLocationThreeAMTimer(NULL),
       
   218             iMaptileGeocoderPluginAvailable(EFalse),iCalenderNotification(NULL),
       
   219             iContactManager(NULL)
   213 {
   220 {
   214 
   221 
   215 }
   222 }
   216 
   223 
   217 // -----------------------------------------------------------------------------
   224 // -----------------------------------------------------------------------------
   240     delete iMaptileDatabase;
   247     delete iMaptileDatabase;
   241 
   248 
   242     delete iAddressCompare;
   249     delete iAddressCompare;
   243     
   250     
   244     delete iCalenderNotification;
   251     delete iCalenderNotification;
       
   252     
       
   253 	delete iMyLocationThreeAMTimer;
       
   254 		
       
   255     delete iContactManager;
   245     
   256     
   246 }
   257 }
   247 
   258 
   248 // -----------------------------------------------------------------------------
   259 // -----------------------------------------------------------------------------
   249 // CMyLocationsEngine::NotifyChangeL()
   260 // CMyLocationsEngine::NotifyChangeL()
   385                 }
   396                 }
   386             }
   397             }
   387             TLookupItem lookupItem;
   398             TLookupItem lookupItem;
   388             lookupItem.iSource = ESourceCalendar;
   399             lookupItem.iSource = ESourceCalendar;
   389             lookupItem.iUid = calChangeEntry.iEntryId;
   400             lookupItem.iUid = calChangeEntry.iEntryId;
       
   401             TRAP_IGNORE(ManipulateMapTileDataBaseL(lookupItem));
   390             //TODO: comapare address and then delete 
   402             //TODO: comapare address and then delete 
   391             TRAP_IGNORE( iMaptileDatabase->DeleteEntryL(lookupItem) );
       
   392             TRAP_IGNORE( CalenderEntryAddedL(calChangeEntry) );
   403             TRAP_IGNORE( CalenderEntryAddedL(calChangeEntry) );
   393             break;
   404             break;
   394         }
   405         }
   395         case EChangeDelete:
   406         case EChangeDelete:
   396         {
   407         {
   397             TLookupItem lookupItem;
   408             TLookupItem lookupItem;
   398             lookupItem.iSource = ESourceCalendar;
   409             lookupItem.iSource = ESourceCalendar;
   399             lookupItem.iUid = calChangeEntry.iEntryId;
   410             lookupItem.iUid = calChangeEntry.iEntryId;
   400            TRAP_IGNORE( iMaptileDatabase->DeleteEntryL(lookupItem));
   411             TRAP_IGNORE(ManipulateMapTileDataBaseL(lookupItem));           
   401            
   412             TRAP_IGNORE( UpdateDatabaseL( NULL, 
   402            TRAP_IGNORE( UpdateDatabaseL( NULL, 
       
   403                               calChangeEntry.iEntryId, ESourceCalendar, EEntryDeleted ) );
   413                               calChangeEntry.iEntryId, ESourceCalendar, EEntryDeleted ) );
   404             break;
   414             break;
   405         }
   415         }
   406         };
   416         };
   407 
   417 
   418     CCalEntry* calEntry = NULL;
   428     CCalEntry* calEntry = NULL;
   419     calEntry = iCalView->FetchL(aCalChangeEntry.iEntryId);
   429     calEntry = iCalView->FetchL(aCalChangeEntry.iEntryId);
   420     TPtrC address(calEntry->LocationL());
   430     TPtrC address(calEntry->LocationL());
   421     if(address.Length()>0)
   431     if(address.Length()>0)
   422     {        
   432     {        
       
   433     //create entry in the data base and maintain a fetching state.
       
   434         TLookupItem lookupItem;
       
   435         lookupItem.iUid = aCalChangeEntry.iEntryId;
       
   436         lookupItem.iSource = ESourceCalendar;
       
   437         lookupItem.iFilePath.Zero();
       
   438         lookupItem.iFetchingStatus = EMapTileFetchingInProgress;
       
   439         iMaptileDatabase->CreateEntryL(lookupItem);  
       
   440         
   423         RequestMapTileImageL(address,ESourceCalendar, aCalChangeEntry.iEntryId);
   441         RequestMapTileImageL(address,ESourceCalendar, aCalChangeEntry.iEntryId);
   424     }
   442     }
   425     delete calEntry;
   443     delete calEntry;
   426 }
   444 }
   427 
   445 
   454 {
   472 {
   455     __TRACE_CALLSTACK;// If contact is modified or added, update the mylocations db
   473     __TRACE_CALLSTACK;// If contact is modified or added, update the mylocations db
   456     if (aEvent.iType == EContactDbObserverEventContactChanged || aEvent.iType
   474     if (aEvent.iType == EContactDbObserverEventContactChanged || aEvent.iType
   457             == EContactDbObserverEventContactAdded)
   475             == EContactDbObserverEventContactAdded)
   458     {
   476     {
   459         //Get the contact item
   477        
   460         CContactItem* contactItem =
   478         CPosLandmark *preferedAddressLm = NULL;
   461                 iContactsDb->ReadContactL(aEvent.iContactId);
   479         CPosLandmark *workAddressLm = NULL;
   462 
   480         CPosLandmark *homeAddressLm = NULL;
   463         if (contactItem)
   481         
   464         {
   482         QContact contactInfo = iContactManager->contact( aEvent.iContactId );
   465             CleanupStack::PushL(contactItem);
   483          
   466 
   484         //Get the contact name details
   467             // Get the default/prefered address details
   485         QContactName name = contactInfo.detail( QContactName::DefinitionName );
   468             CPosLandmark *preferedAddressLm = GetContactLocationDetailsLC(
   486         QString firstName = name.firstName();
   469                     contactItem, EAddressPref);
   487         QString lastName = name.lastName();
   470 
   488         TPtrC16 tempPtr1(reinterpret_cast<const TUint16*>(firstName.utf16()));
   471             // Get the work address details
   489         TPtrC16 tempPtr2( reinterpret_cast <const TUint16*>(lastName.utf16()));
   472             CPosLandmark *workAddressLm = GetContactLocationDetailsLC(
   490         
   473                     contactItem, EAddressWork);
   491         TBuf<KBufSize> landmarkName;
   474 
   492         landmarkName.Append( tempPtr1 );
   475             // Get the home address details
   493         landmarkName.Append( tempPtr2 );
   476             CPosLandmark *homeAddressLm = GetContactLocationDetailsLC(
   494         
   477                     contactItem, EAddressHome);
   495         TInt itemCount = 0;
   478 
   496         
   479             // if the contact item has no validated address (preferef, home or work)
   497   
   480             if (!preferedAddressLm && !workAddressLm && !homeAddressLm)
   498         double longitude, latitude;
   481             {
   499         
   482                 if (aEvent.iType == EContactDbObserverEventContactChanged)
   500         foreach ( QContactAddress address, contactInfo.details<QContactAddress>() )
   483                 {
   501         {
   484                     // If the contact is a modified one and it might already exist in
   502           QStringList context = address.contexts();
   485                     // mylocations db, so delete it
   503           if ( context.isEmpty() ) // no context
   486                     UpdateDatabaseL(NULL,
   504           {
   487                             aEvent.iContactId, ESourceContactsPref,
   505               //check whether geocordinate available
   488                             EEntryDeleted);
   506               TBool geoField = IsGeoCoordinateAvailable( 
   489                     UpdateDatabaseL(NULL,
   507                        contactInfo, NULL , latitude, longitude );
   490                             aEvent.iContactId, ESourceContactsWork,
   508               if( geoField )
   491                             EEntryDeleted);
   509               {
   492                     UpdateDatabaseL(NULL,
   510                   // Get the default/prefered address details
   493                             aEvent.iContactId, ESourceContactsHome,
   511                   preferedAddressLm = GetContactAddressDetailsLC( address );
   494                             EEntryDeleted);
   512                   TLocality loc( TCoordinate( latitude, longitude ), 0 );
   495                 }
   513                   // Fill the location details into the landmark object
   496             }
   514                   preferedAddressLm->SetPositionL(loc);
   497             else
   515                   preferedAddressLm->SetLandmarkNameL( landmarkName );
   498             {
   516                   itemCount++;
   499                 // There is atleast one valid address present.       
   517 
   500 
   518               }
   501                 // Get the TEntryChangeType for contacts.
   519           }
   502                 TEntryChangeType changeType = MapChangeType(
   520           else if (context.first() == QContactAddress::ContextHome  )
   503                         ESourceContactsPref, aEvent.iType);
   521           {
   504 
   522               //check whether geocordinate available
   505                 // if home address available, update Mylocations.  
   523               TBool geoField = IsGeoCoordinateAvailable( 
   506                 if (homeAddressLm)
   524                                contactInfo, QContactAddress::ContextHome, latitude, longitude );
   507                 {
   525               if( geoField )
   508                     UpdateDatabaseL(homeAddressLm,
   526               {
   509                             aEvent.iContactId, ESourceContactsHome, changeType);
   527                   // Get the home address details
   510                     CleanupStack::PopAndDestroy(homeAddressLm);
   528                   homeAddressLm = GetContactAddressDetailsLC( address );
   511                 }
   529                   TLocality loc(TCoordinate( latitude,longitude ), 0);
   512 
   530                   homeAddressLm->SetPositionL(loc);
   513                 // if work address available, update Mylocations.  
   531                   homeAddressLm->SetLandmarkNameL( landmarkName );
   514                 if (workAddressLm)
   532                   itemCount++;
   515                 {
   533               }
   516                     UpdateDatabaseL(workAddressLm,
   534           }
   517                             aEvent.iContactId, ESourceContactsWork, changeType);
   535           else if (context.first() == QContactAddress::ContextWork )
   518                     CleanupStack::PopAndDestroy(workAddressLm);
   536           {
   519                 }
   537               //check whether geocordinate available
   520 
   538               TBool geoField = IsGeoCoordinateAvailable( 
   521                 // if prefered address available, update Mylocations.  
   539                         contactInfo, QContactAddress::ContextWork, latitude, longitude );
   522                 if (preferedAddressLm)
   540               if( geoField )
   523                 {
   541               {
   524                     UpdateDatabaseL(
   542                   // Get the work address details
   525                             preferedAddressLm, aEvent.iContactId,
   543                   workAddressLm = GetContactAddressDetailsLC( address );
   526                             ESourceContactsPref, changeType);
   544                   TLocality loc(TCoordinate( latitude, longitude ), 0);
   527                     CleanupStack::PopAndDestroy(preferedAddressLm);
   545                   // Fill the location details into the landmark object
   528                 }
   546                   workAddressLm->SetPositionL(loc);
   529 
   547                   workAddressLm->SetLandmarkNameL( landmarkName );
   530             }
   548                   itemCount++;
   531 
   549               }
   532             // Pop and destroy the contactItem
   550         
   533             CleanupStack::PopAndDestroy(contactItem);
   551           }
   534         }
   552         }
       
   553 
       
   554         // if the contact item has no validated address (preferef, home or work)
       
   555         if (!preferedAddressLm && !workAddressLm && !homeAddressLm)
       
   556         {
       
   557             if (aEvent.iType == EContactDbObserverEventContactChanged)
       
   558             {
       
   559                 // If the contact is a modified one and it might already exist in
       
   560                 // mylocations db, so delete it
       
   561                 UpdateDatabaseL(NULL,
       
   562                         aEvent.iContactId, ESourceContactsPref,
       
   563                         EEntryDeleted);
       
   564                 UpdateDatabaseL(NULL,
       
   565                         aEvent.iContactId, ESourceContactsWork,
       
   566                         EEntryDeleted);
       
   567                 UpdateDatabaseL(NULL,
       
   568                         aEvent.iContactId, ESourceContactsHome,
       
   569                         EEntryDeleted);
       
   570             }
       
   571         }
       
   572         else
       
   573         {
       
   574             // There is atleast one valid address present.       
       
   575 
       
   576             // Get the TEntryChangeType for contacts.
       
   577             TEntryChangeType changeType = MapChangeType(
       
   578                     ESourceContactsPref, aEvent.iType);
       
   579 
       
   580             // if home address available, update Mylocations.  
       
   581             if (homeAddressLm)
       
   582             {
       
   583                 UpdateDatabaseL(homeAddressLm,
       
   584                         aEvent.iContactId, ESourceContactsHome, changeType);
       
   585            }
       
   586 
       
   587             // if work address available, update Mylocations.  
       
   588             if (workAddressLm)
       
   589             {
       
   590                 UpdateDatabaseL(workAddressLm,
       
   591                         aEvent.iContactId, ESourceContactsWork, changeType);
       
   592             }
       
   593 
       
   594             // if prefered address available, update Mylocations.  
       
   595             if (preferedAddressLm)
       
   596             {
       
   597                 UpdateDatabaseL(
       
   598                         preferedAddressLm, aEvent.iContactId,
       
   599                         ESourceContactsPref, changeType);
       
   600            }
       
   601             CleanupStack::PopAndDestroy( itemCount );
       
   602         }  
       
   603 
   535     }
   604     }
   536     else if (aEvent.iType == EContactDbObserverEventContactDeleted)
   605     else if (aEvent.iType == EContactDbObserverEventContactDeleted)
   537     {
   606     {
   538         // the contact is deleted, so delete the corresponding entries from database.
   607         // the contact is deleted, so delete the corresponding entries from database.
   539 
   608 
   551     }
   620     }
   552 }
   621 }
   553 
   622 
   554 // -----------------------------------------------------------------------------
   623 // -----------------------------------------------------------------------------
   555 // CMyLocationsEngine::TriggerMaptileRequestL()
   624 // CMyLocationsEngine::TriggerMaptileRequestL()
   556 // Callback that provides information about the contact database change event.
   625 // Process the contact address information for fetching maptile.
   557 // -----------------------------------------------------------------------------
   626 // -----------------------------------------------------------------------------
   558 //
   627 //
   559 
       
   560 void CMyLocationsEngine::TriggerMaptileRequestL(TContactDbObserverEvent& aEvent)
   628 void CMyLocationsEngine::TriggerMaptileRequestL(TContactDbObserverEvent& aEvent)
   561 {
   629 {
   562     __TRACE_CALLSTACK;
   630     __TRACE_CALLSTACK;
   563     TLookupItem lookupItem;
   631     TLookupItem lookupItem;      
   564     lookupItem.iUid = aEvent.iContactId;
   632     lookupItem.iUid = aEvent.iContactId;
   565     // If contact is deleted delete from mylocations db
   633     // If contact is deleted delete from mylocations db
   566         if (aEvent.iType == EContactDbObserverEventContactDeleted)
   634     if (aEvent.iType == EContactDbObserverEventContactDeleted)
   567      {
   635     {        
   568         lookupItem.iSource = ESourceContactsPref;
   636         lookupItem.iSource = ESourceContactsPref;
   569         iMaptileDatabase->DeleteEntryL(lookupItem);
   637         TRAP_IGNORE( ManipulateMapTileDataBaseL(lookupItem));
   570 
   638 
   571         lookupItem.iSource = ESourceContactsWork;
   639         lookupItem.iSource = ESourceContactsWork;
   572         iMaptileDatabase->DeleteEntryL(lookupItem);
   640         TRAP_IGNORE( ManipulateMapTileDataBaseL(lookupItem));
   573 
   641         
   574         lookupItem.iSource = ESourceContactsHome;
   642         lookupItem.iSource = ESourceContactsHome;
   575         iMaptileDatabase->DeleteEntryL(lookupItem);
   643         TRAP_IGNORE( ManipulateMapTileDataBaseL(lookupItem));
   576 
   644               
   577         MYLOCLOGSTRING("EContactDbObserverEventContactDeleted ");
   645         MYLOCLOGSTRING("EContactDbObserverEventContactDeleted ");
   578         return;
   646         return;
   579      }
   647     }
   580 
   648 
   581     //Get the contact item
   649     //Get the contact item
   582     iEventType = aEvent.iType;
   650     iEventType = aEvent.iType;
   583     CContactItem* contactItem = iContactsDb->ReadContactL(aEvent.iContactId);
   651     QContact contactInfo = iContactManager->contact( aEvent.iContactId );
   584     CleanupStack::PushL(contactItem);
   652     
   585 
   653   
   586     CPosLandmark *preferedAddressLm = NULL;
   654     CPosLandmark *preferedAddressLm = NULL;
   587     CPosLandmark *workAddressLm = NULL;
   655     CPosLandmark *workAddressLm = NULL;
   588     CPosLandmark *homeAddressLm = NULL;
   656     CPosLandmark *homeAddressLm = NULL;
   589 
   657     
   590     // If contact is modified or added, update the mylocations db
   658     TInt itemCount = 0;
   591     /* if (contactItem)
   659     
   592      {
   660     foreach ( QContactAddress address, contactInfo.details<QContactAddress>() )
   593      CleanupStack::PushL(contactItem);
   661     {
   594      */
   662         QStringList context = address.contexts();
   595     // Get the home address details
   663         if ( context.isEmpty() ) // no context
   596     homeAddressLm = GetContactAddressDetailsLC(contactItem, EAddressHome);
   664         {
   597 
   665             preferedAddressLm = GetContactAddressDetailsLC( address );
   598     // Get the work address details
   666             itemCount++;
   599     workAddressLm = GetContactAddressDetailsLC(contactItem, EAddressWork);
   667         }
   600 
   668         else if ( context.first() == QContactAddress::ContextHome  )
   601     // Get the default/prefered address details
   669         {
   602     preferedAddressLm = GetContactAddressDetailsLC(contactItem, EAddressPref);
   670             homeAddressLm = GetContactAddressDetailsLC( address );
   603 
   671             itemCount++;
   604     // iContactUid = aEvent.iContactId;
   672         }
       
   673         else if ( context.first() == QContactAddress::ContextWork )
       
   674         {
       
   675             workAddressLm = GetContactAddressDetailsLC( address );
       
   676             itemCount++;
       
   677         }
       
   678     }
       
   679    
   605     switch (aEvent.iType)
   680     switch (aEvent.iType)
   606     {
   681     {
   607     case EContactDbObserverEventContactChanged:
   682         case EContactDbObserverEventContactChanged:
   608     {
   683         {
   609         MYLOCLOGSTRING("EContactDbObserverEventContactChanged" );MYLOCLOGSTRING1("iMapTileRequestQueue.Count()-%d",iMapTileRequestQueue.Count() );
   684             MYLOCLOGSTRING("EContactDbObserverEventContactChanged" );
   610 
   685             MYLOCLOGSTRING1("iMapTileRequestQueue.Count()-%d",iMapTileRequestQueue.Count() );
   611         if (iMapTileRequestQueue.Count() > 0)
   686 
   612         {
   687             if (iMapTileRequestQueue.Count() > 0)
   613             if (iMapTileRequestQueue[0]->iUId == aEvent.iContactId)
   688             {
   614             {
   689                 if (iMapTileRequestQueue[0]->iUId == aEvent.iContactId)
   615                 if (preferedAddressLm)
       
   616                 {
   690                 {
   617                     CleanupStack::PopAndDestroy(preferedAddressLm);
   691                     CleanupStack::PopAndDestroy( itemCount );
       
   692                     MYLOCLOGSTRING("retrun from geolocation callback" );
       
   693                     return;
   618                 }
   694                 }
   619                 if (workAddressLm)
   695             }
       
   696        
       
   697             // if default address available, update Mylocations. 
       
   698             lookupItem.iSource = ESourceContactsPref;
       
   699             if (preferedAddressLm)
       
   700             {
       
   701                 MYLOCLOGSTRING("preferedAddressLm address changed" );
       
   702 
       
   703                 if (iAddressCompare->IsAddressChangedL(*preferedAddressLm,
       
   704                     aEvent.iContactId, ESourceContactsPref))
       
   705 
   620                 {
   706                 {
   621                     CleanupStack::PopAndDestroy(workAddressLm);
   707                     //remove entry from database
       
   708                     TRAP_IGNORE( ManipulateMapTileDataBaseL(lookupItem));
       
   709                     RequestMapTileImageL(*preferedAddressLm, ESourceContactsPref,
       
   710                             aEvent.iContactId);
   622                 }
   711                 }
   623                 if (homeAddressLm)
   712             }
       
   713             else
       
   714             {
       
   715                 TRAP_IGNORE( ManipulateMapTileDataBaseL(lookupItem));
       
   716                 
       
   717             }  
       
   718 
       
   719             // if work address available, update Mylocations.
       
   720             lookupItem.iSource = ESourceContactsWork;
       
   721             if (workAddressLm)
       
   722             {
       
   723                 MYLOCLOGSTRING("workAddressLm address changed" );
       
   724                 if (iAddressCompare->IsAddressChangedL(*workAddressLm,
       
   725                         aEvent.iContactId, ESourceContactsWork))
       
   726                 //remove entry from database
   624                 {
   727                 {
   625                     CleanupStack::PopAndDestroy(homeAddressLm);
   728                     
       
   729                     TRAP_IGNORE( ManipulateMapTileDataBaseL(lookupItem));
       
   730                     RequestMapTileImageL(*workAddressLm, ESourceContactsWork,
       
   731                             aEvent.iContactId);
   626                 }
   732                 }
   627 
   733             }
   628                 CleanupStack::PopAndDestroy(contactItem);
   734             else
   629                 MYLOCLOGSTRING("retrun from geolocation callback" );
   735             {
   630                 return;
   736                 TRAP_IGNORE( ManipulateMapTileDataBaseL(lookupItem));
   631             }
   737                
   632         }
   738             }
   633         /*
   739     
   634          // if the contact item has no validated address (preferef, home or work)
   740             // if home address available, update Mylocations.
   635          if (!preferedAddressLm && !workAddressLm && !homeAddressLm)
   741             lookupItem.iSource = ESourceContactsHome;
   636          {
   742             if (homeAddressLm)
   637          MYLOCLOGSTRING("Contact changed no  address" );
   743             {
   638          //Delete the entries from maptile lookup table
   744                 MYLOCLOGSTRING("homeAddressLm address changed" );
   639          lookupItem.iSource = ESourceContactsPref;
   745     
   640          iMaptileDatabase->DeleteEntryL(lookupItem);
   746                 if (iAddressCompare->IsAddressChangedL(*homeAddressLm,
   641 
   747                         aEvent.iContactId, ESourceContactsHome))
   642          lookupItem.iSource = ESourceContactsWork;
   748     
   643          iMaptileDatabase->DeleteEntryL(lookupItem);
   749                 {
   644 
   750                     //remove entry from databse                    
   645          lookupItem.iSource = ESourceContactsHome;
   751                     TRAP_IGNORE( ManipulateMapTileDataBaseL(lookupItem));
   646          iMaptileDatabase->DeleteEntryL(lookupItem);
   752                     RequestMapTileImageL(*homeAddressLm, ESourceContactsHome,
   647 
   753                             aEvent.iContactId);
   648          }*/
   754                 }
   649         /* else
   755  
   650          {*/
   756             }
   651         MYLOCLOGSTRING("Contact address changed" );
   757             else
   652 
   758             {
   653         // if default address available, update Mylocations. 
   759                 TRAP_IGNORE( ManipulateMapTileDataBaseL(lookupItem));
   654         lookupItem.iSource = ESourceContactsPref;
   760                 
   655         if (preferedAddressLm)
   761             }
   656         {
   762             // }
   657             MYLOCLOGSTRING("preferedAddressLm address changed" );
   763             break;
   658 
   764         }    
   659             if (iAddressCompare->IsAddressChangedL(*preferedAddressLm,
   765         case EContactDbObserverEventContactAdded:
   660                     aEvent.iContactId, ESourceContactsPref))
   766         {
   661 
   767             MYLOCLOGSTRING("EContactDbObserverEventContactAdded" );
   662             {
   768             if (preferedAddressLm)
   663                 //remove entry from database
   769             {
   664                 iMaptileDatabase->DeleteEntryL(lookupItem);
       
   665                 RequestMapTileImageL(*preferedAddressLm, ESourceContactsPref,
   770                 RequestMapTileImageL(*preferedAddressLm, ESourceContactsPref,
   666                         aEvent.iContactId);
   771                         aEvent.iContactId);
   667             }
   772             }
   668 
   773             if (workAddressLm)
   669             CleanupStack::PopAndDestroy(preferedAddressLm);
   774             {
   670 
       
   671         }
       
   672         else
       
   673         {
       
   674             iMaptileDatabase->DeleteEntryL(lookupItem);
       
   675         }
       
   676 
       
   677         // if work address available, update Mylocations.
       
   678         lookupItem.iSource = ESourceContactsWork;
       
   679         if (workAddressLm)
       
   680         {
       
   681             MYLOCLOGSTRING("workAddressLm address changed" );
       
   682             if (iAddressCompare->IsAddressChangedL(*workAddressLm,
       
   683                     aEvent.iContactId, ESourceContactsWork))
       
   684             //remove entry from database
       
   685             {
       
   686                 iMaptileDatabase->DeleteEntryL(lookupItem);
       
   687                 RequestMapTileImageL(*workAddressLm, ESourceContactsWork,
   775                 RequestMapTileImageL(*workAddressLm, ESourceContactsWork,
   688                         aEvent.iContactId);
   776                         aEvent.iContactId);
   689             }
   777             }
   690 
   778             if (homeAddressLm)
   691             CleanupStack::PopAndDestroy(workAddressLm);
   779             {
   692 
       
   693         }
       
   694         else
       
   695         {
       
   696             iMaptileDatabase->DeleteEntryL(lookupItem);
       
   697         }
       
   698 
       
   699         // if home address available, update Mylocations.
       
   700         lookupItem.iSource = ESourceContactsHome;
       
   701         if (homeAddressLm)
       
   702         {
       
   703             MYLOCLOGSTRING("homeAddressLm address changed" );
       
   704 
       
   705             if (iAddressCompare->IsAddressChangedL(*homeAddressLm,
       
   706                     aEvent.iContactId, ESourceContactsHome))
       
   707 
       
   708             {
       
   709                 //remove entry from databse
       
   710                 iMaptileDatabase->DeleteEntryL(lookupItem);
       
   711                 RequestMapTileImageL(*homeAddressLm, ESourceContactsHome,
   780                 RequestMapTileImageL(*homeAddressLm, ESourceContactsHome,
   712                         aEvent.iContactId);
   781                         aEvent.iContactId);
   713             }
   782             }
   714             CleanupStack::PopAndDestroy(homeAddressLm);
   783             break;
   715         }
   784         }
   716         else
   785 
   717         {
   786     }
   718             iMaptileDatabase->DeleteEntryL(lookupItem);
   787  
   719         }
   788     CleanupStack::PopAndDestroy( itemCount );
   720         // }
   789 
   721         break;
   790 }
   722     }    
   791 
   723     case EContactDbObserverEventContactAdded:
   792 
   724     {
   793 // -----------------------------------------------------------------------------
   725         MYLOCLOGSTRING("EContactDbObserverEventContactAdded" );
   794 // CMyLocationsEngine::RequestMapTileImageL()
   726         if (preferedAddressLm)
   795 // Request to get maptiel
   727         {
   796 // -----------------------------------------------------------------------------
   728             RequestMapTileImageL(*preferedAddressLm, ESourceContactsPref,
   797 //
   729                     aEvent.iContactId);
       
   730             CleanupStack::PopAndDestroy(preferedAddressLm);
       
   731         }
       
   732         if (workAddressLm)
       
   733         {
       
   734             RequestMapTileImageL(*workAddressLm, ESourceContactsWork,
       
   735                     aEvent.iContactId);
       
   736             CleanupStack::PopAndDestroy(workAddressLm);
       
   737         }
       
   738         if (homeAddressLm)
       
   739         {
       
   740             RequestMapTileImageL(*homeAddressLm, ESourceContactsHome,
       
   741                     aEvent.iContactId);
       
   742             CleanupStack::PopAndDestroy(homeAddressLm);
       
   743         }
       
   744         break;
       
   745     }
       
   746 
       
   747     };
       
   748     CleanupStack::PopAndDestroy(contactItem);
       
   749     //}
       
   750 
       
   751 }
       
   752 
       
   753 void CMyLocationsEngine::RequestMapTileImageL(const TDesC& aAddressDetails,
   798 void CMyLocationsEngine::RequestMapTileImageL(const TDesC& aAddressDetails,
   754         const TUidSourceType aAddressType, const TInt32 aUId)
   799         const TUidSourceType aAddressType, const TInt32 aUId)
   755 {
   800 {
   756     __TRACE_CALLSTACK;
   801     __TRACE_CALLSTACK;
   757     SetFolderPathL();
   802     SetFolderPathL();
   758     TBuf<KImagePathSize> mImagePath;
   803     TBuf<KImagePathSize> mImagePath;
   759 
       
   760     //mImagePath.Append(KImageStorageDrive);
       
   761     mImagePath.Copy(imageFilePath);
   804     mImagePath.Copy(imageFilePath);
   762     mImagePath.AppendNum(aUId);
   805  
   763     mImagePath.AppendNum(aAddressType);
       
   764     mImagePath.Append(KPNGType);
       
   765 
   806 
   766     CMapTileRequest* mapTileRequest = new (ELeave) CMapTileRequest;
   807     CMapTileRequest* mapTileRequest = new (ELeave) CMapTileRequest;
   767 
   808 
   768     mapTileRequest->iAddressDetails = aAddressDetails.AllocL();
   809     mapTileRequest->iAddressDetails = aAddressDetails.AllocL();
   769     mapTileRequest->iUId = aUId;
   810     mapTileRequest->iUId = aUId;
   798 {
   839 {
   799     __TRACE_CALLSTACK;
   840     __TRACE_CALLSTACK;
   800 
   841 
   801     MYLOCLOGSTRING("check folder path existance!");
   842     MYLOCLOGSTRING("check folder path existance!");
   802     SetFolderPathL();
   843     SetFolderPathL();
   803     TBuf<KImagePathSize> mImagePath;
   844  
   804 
       
   805     //mImagePath.Append(KImageStorageDrive);
       
   806     mImagePath.Copy(imageFilePath);
       
   807     mImagePath.AppendNum(aUId);
       
   808     mImagePath.AppendNum(aAddressType);
       
   809     mImagePath.Append(KPNGType);
       
   810 
   845 
   811     CMapTileRequest* mapTileRequest = new (ELeave) CMapTileRequest;
   846     CMapTileRequest* mapTileRequest = new (ELeave) CMapTileRequest;
   812 
   847 
   813     mapTileRequest->iLandmarkInfo = CPosLandmark::NewL(aLandmark);
   848     mapTileRequest->iLandmarkInfo = CPosLandmark::NewL(aLandmark);
   814     mapTileRequest->iUId = aUId;
   849     mapTileRequest->iUId = aUId;
   815     mapTileRequest->iAddressType = aAddressType;
   850     mapTileRequest->iAddressType = aAddressType;
   816     mapTileRequest->iEventType = iEventType;
   851     mapTileRequest->iEventType = iEventType;
   817     mapTileRequest->iImagePath.Zero();
   852     mapTileRequest->iImagePath.Zero();
   818     mapTileRequest->iImagePath.Copy(mImagePath);
   853     mapTileRequest->iImagePath.Copy(imageFilePath);
   819     MYLOCLOGSTRING1("RequestMapTileImageL() Queue count -%d",iMapTileRequestQueue.Count());
   854     MYLOCLOGSTRING1("RequestMapTileImageL() Queue count -%d",iMapTileRequestQueue.Count());
   820 
   855 
   821     if (iMapTileRequestQueue.Count() <= 0)
   856     if (iMapTileRequestQueue.Count() <= 0)
   822        {
   857        {
   823           // iMapTileRequestQueue.Append(mapTileRequest);
   858           
   824            if( KErrNone == RequestExecute(mapTileRequest) )
   859            if( KErrNone == RequestExecute(mapTileRequest) )
   825            {
   860            {
   826                iMapTileRequestQueue.Append(mapTileRequest);
   861                iMapTileRequestQueue.Append(mapTileRequest);
   827            }
   862            }
   828            else
   863            else
   836            iMapTileRequestQueue.Append(mapTileRequest);
   871            iMapTileRequestQueue.Append(mapTileRequest);
   837        }
   872        }
   838     
   873     
   839    
   874    
   840 }
   875 }
       
   876 
       
   877 // -----------------------------------------------------------------------------
       
   878 // CMyLocationsEngine::RequestExecute()
       
   879 // Executes the maptile image processing request
       
   880 // -----------------------------------------------------------------------------
       
   881 //
   841 TInt CMyLocationsEngine::RequestExecute( CMapTileRequest* aMapTileRequest)
   882 TInt CMyLocationsEngine::RequestExecute( CMapTileRequest* aMapTileRequest)
   842 {
   883 {
   843     __TRACE_CALLSTACK;
   884     __TRACE_CALLSTACK;
   844     TInt errorCode = KErrNone;
   885     TInt errorCode = KErrNone;
       
   886     MYLOCLOGSTRING1("Request address type - %d ",aMapTileRequest->iAddressType);
   845     switch (aMapTileRequest->iAddressType)
   887     switch (aMapTileRequest->iAddressType)
   846     {
   888     {
   847         case ESourceCalendar:
   889         case ESourceCalendar:
   848         {
   890         {
   849             TRAP(errorCode,iMapTileInterface->GetMapTileImageL(aMapTileRequest->iAddressDetails->Des(),
   891             TRAP(errorCode,iMapTileInterface->GetGeoCodeFromAddressL(*(aMapTileRequest->iAddressDetails),
   850                             aMapTileRequest->iImagePath, this ));        
   892                             aMapTileRequest->iImagePath, this ));        
   851             break;
   893             break;
   852         }
   894         }
   853         case ESourceContactsPref:
   895         case ESourceContactsPref:
   854         case ESourceContactsWork:
   896         case ESourceContactsWork:
   855         case ESourceContactsHome:
   897         case ESourceContactsHome:
   856         {
   898         {
   857             TRAP(errorCode, iMapTileInterface->GetMapTileImageL(aMapTileRequest->iLandmarkInfo,
   899             TRAP(errorCode, iMapTileInterface->GetGeoCodeFromAddressL(aMapTileRequest->iLandmarkInfo,
   858                             aMapTileRequest->iImagePath, this));            
   900                             aMapTileRequest->iImagePath, this));            
   859             break;
   901             break;
   860         }
   902         }
   861         default:
   903         default:
   862             break;
   904             break;
   868 // -----------------------------------------------------------------------------
   910 // -----------------------------------------------------------------------------
   869 // CMyLocationsEngine::GetLocationDetailsLC()
   911 // CMyLocationsEngine::GetLocationDetailsLC()
   870 // get locatin details
   912 // get locatin details
   871 // -----------------------------------------------------------------------------
   913 // -----------------------------------------------------------------------------
   872 //
   914 //
   873 CPosLandmark* CMyLocationsEngine::GetContactAddressDetailsLC(
   915 CPosLandmark* CMyLocationsEngine::GetContactAddressDetailsLC( 
   874         const CContactItem *aContactItem, TContactAddressType aAddressType)
   916                                    QContactAddress& aContactAddress )
   875 {
   917 {
   876     __TRACE_CALLSTACK;
   918     __TRACE_CALLSTACK;
   877     CPosLandmark *landmark = NULL;
   919     CPosLandmark *landmark = NULL;
   878 
   920     
   879     // Set the street
   921     QString country = aContactAddress.country();
   880     TInt adrId = FindContactsField(aContactItem, aAddressType,
   922     QString locality = aContactAddress.locality();
   881             KUidContactFieldVCardMapADR);
   923     QString street = aContactAddress.street();
   882 
   924     QString region = aContactAddress.region();
   883     if (adrId != KErrNotFound)
   925     QString postalcode = aContactAddress.postcode();
   884     {
   926     
   885         TPtrC tempText =
   927     landmark = CPosLandmark::NewL();
   886                 aContactItem->CardFields()[adrId].TextStorage()->Text();
   928     CleanupStack::PushL(landmark);
   887         if (tempText.Length() > 0)
   929     
   888         {
   930     if ( !country.isEmpty() )
   889             if (!landmark)
   931     {
   890             {
   932         TPtrC16 tempText(reinterpret_cast<const TUint16*>(country.utf16()));
   891                 landmark = CPosLandmark::NewL();
   933         landmark->SetPositionFieldL( EPositionFieldCountry, tempText );
   892                 CleanupStack::PushL(landmark);
   934     }
   893             }
   935     if ( !locality.isEmpty() )
   894 
   936     {
   895             landmark->SetPositionFieldL(EPositionFieldStreet, tempText);
   937         TPtrC16 tempText(reinterpret_cast<const TUint16*>(locality.utf16())); 
   896         }
   938         landmark->SetPositionFieldL(EPositionFieldCity, tempText);
   897     }
   939     }
   898 
   940     if ( !street.isEmpty() )
   899     // Set the City
   941     { 
   900     adrId = FindContactsField(aContactItem, aAddressType,
   942         TPtrC16 tempText(reinterpret_cast<const TUint16*>(street.utf16()));
   901             KUidContactFieldVCardMapLOCALITY);
   943         landmark->SetPositionFieldL(EPositionFieldStreet, tempText);
   902     if (adrId != KErrNotFound)
   944     }
   903     {
   945     if ( !postalcode.isEmpty() )
   904         TPtrC tempText =
   946     {
   905                 aContactItem->CardFields()[adrId].TextStorage()->Text();
   947         TPtrC16 tempText(reinterpret_cast<const TUint16*>(postalcode.utf16()));
   906         if (tempText.Length() > 0)
   948         landmark->SetPositionFieldL(EPositionFieldPostalCode, tempText);
   907         {
   949     }
   908             if (!landmark)
   950 
   909             {
   951      return landmark;
   910                 landmark = CPosLandmark::NewL();
       
   911                 CleanupStack::PushL(landmark);
       
   912             }
       
   913             landmark->SetPositionFieldL(EPositionFieldCity, tempText);
       
   914         }
       
   915     }
       
   916 
       
   917     // Set the state/region
       
   918     adrId = FindContactsField(aContactItem, aAddressType,
       
   919             KUidContactFieldVCardMapREGION);
       
   920     if (adrId != KErrNotFound)
       
   921     {
       
   922         TPtrC tempText =
       
   923                 aContactItem->CardFields()[adrId].TextStorage()->Text();
       
   924         if (tempText.Length() > 0)
       
   925         {
       
   926             if (!landmark)
       
   927             {
       
   928                 landmark = CPosLandmark::NewL();
       
   929                 CleanupStack::PushL(landmark);
       
   930             }
       
   931             landmark->SetPositionFieldL(EPositionFieldState, tempText);
       
   932         }
       
   933     }
       
   934 
       
   935     // Set the Postal code
       
   936     adrId = FindContactsField(aContactItem, aAddressType,
       
   937             KUidContactFieldVCardMapPOSTCODE);
       
   938     if (adrId != KErrNotFound)
       
   939     {
       
   940         TPtrC tempText =
       
   941                 aContactItem->CardFields()[adrId].TextStorage()->Text();
       
   942         if (tempText.Length() > 0)
       
   943         {
       
   944             if (!landmark)
       
   945             {
       
   946                 landmark = CPosLandmark::NewL();
       
   947                 CleanupStack::PushL(landmark);
       
   948             }
       
   949             landmark->SetPositionFieldL(EPositionFieldPostalCode, tempText);
       
   950         }
       
   951     }
       
   952 
       
   953     // Set the country
       
   954     adrId = FindContactsField(aContactItem, aAddressType,
       
   955             KUidContactFieldVCardMapCOUNTRY);
       
   956     if (adrId != KErrNotFound)
       
   957     {
       
   958         TPtrC tempText =
       
   959                 aContactItem->CardFields()[adrId].TextStorage()->Text();
       
   960         if (tempText.Length() > 0)
       
   961         {
       
   962             if (!landmark)
       
   963             {
       
   964                 landmark = CPosLandmark::NewL();
       
   965                 CleanupStack::PushL(landmark);
       
   966             }
       
   967             landmark->SetPositionFieldL(EPositionFieldCountry, tempText);
       
   968         }
       
   969     }
       
   970        
       
   971     return landmark;
       
   972 
       
   973 }
       
   974 
       
   975 // -----------------------------------------------------------------------------
       
   976 // CMyLocationsEngine::GetContactLocationDetailsLC()
       
   977 // Finds the contact's location details
       
   978 // -----------------------------------------------------------------------------
       
   979 //
       
   980 CPosLandmark* CMyLocationsEngine::GetContactLocationDetailsLC(
       
   981         const CContactItem *aContactItem, TContactAddressType aAddressType)
       
   982 {
       
   983     __TRACE_CALLSTACK;//return value
       
   984     CPosLandmark *landmark = NULL;
       
   985     // Get the geo field
       
   986     TInt addrInd = FindContactsField(aContactItem, aAddressType,
       
   987             KUidContactFieldVCardMapGEO);
       
   988     if (addrInd != KErrNotFound)
       
   989     {
       
   990         // Geo field present. 
       
   991 
       
   992         TPtrC addrText =
       
   993                 aContactItem->CardFields()[addrInd].TextStorage()->Text();
       
   994         if (addrText.Length() > 0)
       
   995         {
       
   996             //Parse the addresstext to get the latitude and longitude
       
   997             TInt separator = addrText.Find(KSeparator);
       
   998             if (separator != KErrNotFound)
       
   999             {
       
  1000                 TReal64 latitude = 0;
       
  1001                 TReal64 longitude = 0;
       
  1002                 TLex lexLatitude(addrText.Left(addrText.Length() - separator));
       
  1003                 TLex lexLongitude(addrText.Right(addrText.Length() - separator
       
  1004                         - 1));
       
  1005                 if (lexLatitude.Val(latitude) == KErrNone && lexLongitude.Val(
       
  1006                         longitude) == KErrNone)
       
  1007                 {
       
  1008                     TLocality loc(TCoordinate(latitude, longitude), 0);
       
  1009 
       
  1010                     landmark = CPosLandmark::NewL();
       
  1011                     CleanupStack::PushL(landmark);
       
  1012 
       
  1013                     // Fill the location details into the landmark object
       
  1014                     landmark->SetPositionL(loc);
       
  1015 
       
  1016                     // Set the landmark name as contact name
       
  1017                     TBuf<KBufSize> sName1;
       
  1018                     TBool nameEmpty = ETrue;
       
  1019                     //get the second name and
       
  1020                     TInt sNameInd = aContactItem->CardFields().Find(
       
  1021                             KUidContactFieldGivenName);
       
  1022                     if (sNameInd != KErrNotFound)
       
  1023                     {
       
  1024                         TPtrC      sName =
       
  1025                                         aContactItem->CardFields()[sNameInd].TextStorage()->Text();
       
  1026                         sName1.Copy(sName);
       
  1027                         nameEmpty = EFalse;
       
  1028                     }
       
  1029 
       
  1030                     sNameInd = aContactItem->CardFields().Find(
       
  1031                             KUidContactFieldFamilyName);
       
  1032                     if (sNameInd != KErrNotFound)
       
  1033                     {
       
  1034                         if (!nameEmpty)
       
  1035                         {
       
  1036                             sName1.Append(KSingleSpace);
       
  1037                         }
       
  1038                         TPtrC
       
  1039                                 sName =
       
  1040                                         aContactItem->CardFields()[sNameInd].TextStorage()->Text();
       
  1041                         sName1.Append(sName);
       
  1042                     }
       
  1043 
       
  1044                     TRAP_IGNORE( landmark->SetLandmarkNameL( sName1 ) );
       
  1045 
       
  1046                     // Set the street
       
  1047                     TInt adrId = FindContactsField(aContactItem, aAddressType,
       
  1048                             KUidContactFieldVCardMapADR);
       
  1049                     if (adrId != KErrNotFound)
       
  1050                     {
       
  1051                         TPtrC
       
  1052                                 tempText =
       
  1053                                         aContactItem->CardFields()[adrId].TextStorage()->Text();
       
  1054                         if (tempText.Length() > 0)
       
  1055                         {
       
  1056                             landmark->SetPositionFieldL(EPositionFieldStreet,
       
  1057                                     tempText);
       
  1058                         }
       
  1059                     }
       
  1060 
       
  1061                     // Set the City
       
  1062                     adrId = FindContactsField(aContactItem, aAddressType,
       
  1063                             KUidContactFieldVCardMapLOCALITY);
       
  1064                     if (adrId != KErrNotFound)
       
  1065                     {
       
  1066                         TPtrC
       
  1067                                 tempText =
       
  1068                                         aContactItem->CardFields()[adrId].TextStorage()->Text();
       
  1069                         if (tempText.Length() > 0)
       
  1070                         {
       
  1071                             landmark->SetPositionFieldL(EPositionFieldCity,
       
  1072                                     tempText);
       
  1073                         }
       
  1074                     }
       
  1075 
       
  1076                     // Set the state/region
       
  1077                     adrId = FindContactsField(aContactItem, aAddressType,
       
  1078                             KUidContactFieldVCardMapREGION);
       
  1079                     if (adrId != KErrNotFound)
       
  1080                     {
       
  1081                         TPtrC
       
  1082                                 tempText =
       
  1083                                         aContactItem->CardFields()[adrId].TextStorage()->Text();
       
  1084                         if (tempText.Length() > 0)
       
  1085                         {
       
  1086                             landmark->SetPositionFieldL(EPositionFieldState,
       
  1087                                     tempText);
       
  1088                         }
       
  1089                     }
       
  1090 
       
  1091                     // Set the Postal code
       
  1092                     adrId = FindContactsField(aContactItem, aAddressType,
       
  1093                             KUidContactFieldVCardMapPOSTCODE);
       
  1094                     if (adrId != KErrNotFound)
       
  1095                     {
       
  1096                         TPtrC
       
  1097                                 tempText =
       
  1098                                         aContactItem->CardFields()[adrId].TextStorage()->Text();
       
  1099                         if (tempText.Length() > 0)
       
  1100                         {
       
  1101                             landmark->SetPositionFieldL(
       
  1102                                     EPositionFieldPostalCode, tempText);
       
  1103                         }
       
  1104                     }
       
  1105 
       
  1106                     // Set the country
       
  1107                     adrId = FindContactsField(aContactItem, aAddressType,
       
  1108                             KUidContactFieldVCardMapCOUNTRY);
       
  1109                     if (adrId != KErrNotFound)
       
  1110                     {
       
  1111                         TPtrC
       
  1112                                 tempText =
       
  1113                                         aContactItem->CardFields()[adrId].TextStorage()->Text();
       
  1114                         if (tempText.Length() > 0)
       
  1115                         {
       
  1116                             landmark->SetPositionFieldL(EPositionFieldCountry,
       
  1117                                     tempText);
       
  1118                         }
       
  1119                     }
       
  1120                 }
       
  1121             }
       
  1122         }
       
  1123     }
       
  1124 
       
  1125     return landmark;
       
  1126 }
       
  1127 
       
  1128 // -----------------------------------------------------------------------------
       
  1129 // CMyLocationsEngine::FindContactsField()
       
  1130 // Finds the contact's field type id
       
  1131 // -----------------------------------------------------------------------------
       
  1132 //
       
  1133 
       
  1134 TInt CMyLocationsEngine::FindContactsField(const CContactItem *aContactItem,
       
  1135         TContactAddressType aAddressType, TUid aField)
       
  1136 {
       
  1137     __TRACE_CALLSTACK;
       
  1138     if (aAddressType == EAddressPref) // default/prefered address
       
  1139     {
       
  1140         return aContactItem->CardFields().Find(aField);
       
  1141     }
       
  1142     else if (aAddressType == EAddressWork) // work address
       
  1143     {
       
  1144         return aContactItem->CardFields().Find(KUidContactFieldVCardMapWORK,
       
  1145                 aField);
       
  1146     }
       
  1147     else // home address
       
  1148     {
       
  1149         return aContactItem->CardFields().Find(KUidContactFieldVCardMapHOME,
       
  1150                 aField);
       
  1151     }
       
  1152 }
   952 }
  1153 
   953 
  1154 // -----------------------------------------------------------------------------
   954 // -----------------------------------------------------------------------------
  1155 // CMyLocationsEngine::MapsChangeType()
   955 // CMyLocationsEngine::MapsChangeType()
  1156 // Maps the source's change type to Mylocations entry change type
   956 // Maps the source's change type to Mylocations entry change type
  1230         break;
  1030         break;
  1231     }
  1031     }
  1232     return retVal;
  1032     return retVal;
  1233 }
  1033 }
  1234 
  1034 
       
  1035 
       
  1036 // -----------------------------------------------------------------------------
       
  1037 // CMyLocationsEngine::IsGeoCoordinateAvailable()
       
  1038 // Checks whether geocoordinate available in the contact detail.
       
  1039 // -----------------------------------------------------------------------------
       
  1040 //
       
  1041 TBool CMyLocationsEngine::IsGeoCoordinateAvailable( QContact& aContact, 
       
  1042         QString aAddressType, double& aLatitude , double& aLongitude )
       
  1043 
       
  1044 {
       
  1045       TBool geoFieldAvailable = EFalse;
       
  1046       QContactGeoLocation geoLocation;
       
  1047       
       
  1048       foreach( geoLocation, aContact.details<QContactGeoLocation>() )
       
  1049       {
       
  1050           if( !geoLocation.isEmpty())
       
  1051           {
       
  1052               QStringList context = geoLocation.contexts();
       
  1053               if ( context.isEmpty() )
       
  1054               {
       
  1055                   if ( aAddressType.isEmpty() )
       
  1056                   {
       
  1057                       geoFieldAvailable = ETrue;
       
  1058                       break;
       
  1059                   }
       
  1060               }
       
  1061               else if( context.first() == aAddressType )
       
  1062               {
       
  1063                   geoFieldAvailable = ETrue;
       
  1064                   break;
       
  1065               }
       
  1066               
       
  1067           }
       
  1068           
       
  1069       }
       
  1070       if( geoFieldAvailable )
       
  1071       {
       
  1072           aLatitude = geoLocation.latitude();
       
  1073           aLongitude = geoLocation.longitude();
       
  1074       }
       
  1075       return geoFieldAvailable;
       
  1076 }
       
  1077 
       
  1078 
       
  1079 
  1235 // -----------------------------------------------------------------------------
  1080 // -----------------------------------------------------------------------------
  1236 // CMyLocationsEngine::RunL()
  1081 // CMyLocationsEngine::RunL()
  1237 // Handles active object's request completion event.
  1082 // Handles active object's request completion event.
  1238 // -----------------------------------------------------------------------------
  1083 // -----------------------------------------------------------------------------
  1239 //
  1084 //
  1283     __TRACE_CALLSTACK;
  1128     __TRACE_CALLSTACK;
  1284     iLandmarkDb->CancelNotifyDatabaseEvent();
  1129     iLandmarkDb->CancelNotifyDatabaseEvent();
  1285 }
  1130 }
  1286 
  1131 
  1287 // -----------------------------------------------------------------------------
  1132 // -----------------------------------------------------------------------------
       
  1133 // CMyLocationsEngine::GeoCodefetchingCompleted()
       
  1134 // Handles the maptile fetching completion event and updates the maptile lookup db.
       
  1135 // -----------------------------------------------------------------------------
       
  1136 //
       
  1137 void CMyLocationsEngine::GeoCodefetchingCompleted( TInt aErrCode, const TReal& aLatitude,
       
  1138             const TReal& aLongitude, const TDesC& aMapTilePath )
       
  1139 {
       
  1140     __TRACE_CALLSTACK;
       
  1141     MYLOCLOGSTRING1("MapTilefetchingCompleted aErrCode - %d ",aErrCode);
       
  1142     MYLOCLOGSTRING1("iMapTileRequestQueue.Count - %d",iMapTileRequestQueue.Count());
       
  1143 
       
  1144     if (iMapTileRequestQueue.Count() > 0)
       
  1145     {
       
  1146         MYLOCLOGSTRING1("No.of RequestQueue - %d",iMapTileRequestQueue.Count());
       
  1147        
       
  1148 	    TLookupItem lookupItem;
       
  1149         lookupItem.iSource = iMapTileRequestQueue[0]->iAddressType;
       
  1150         lookupItem.iUid = iMapTileRequestQueue[0]->iUId;
       
  1151 
       
  1152         if (aErrCode == KErrNone)
       
  1153         {
       
  1154             UpdateGeoCodeToAppDataBase( aLatitude, aLongitude );
       
  1155             
       
  1156             TBool flag = EFalse;
       
  1157             TRAP_IGNORE( flag = iMaptileDatabase->FindEntryByFilePathL(aMapTilePath) );
       
  1158             if ( flag )
       
  1159             {  
       
  1160                 MYLOCLOGSTRING1("%S - found in the DB",&aMapTilePath);
       
  1161             
       
  1162              				
       
  1163                 lookupItem.iFilePath.Copy(aMapTilePath);
       
  1164                 lookupItem.iFetchingStatus = EMapTileFectchingCompleted;
       
  1165                 TRAP_IGNORE( UpdateMaptileDatabaseL(iMapTileRequestQueue[0]->iEventType, lookupItem ) );              
       
  1166 
       
  1167                 MYLOCLOGSTRING("UpdateMaptileDatabaseL handled");
       
  1168 
       
  1169                 //Process the pending maptile requests
       
  1170                 ProcessNextMaptileRequest();
       
  1171                 
       
  1172             }
       
  1173             else
       
  1174             {
       
  1175                 TRAPD( error, iMapTileInterface->GetMapTileL( aLatitude, aLongitude ) );
       
  1176                 if ( error != KErrNone )
       
  1177                 {
       
  1178                     //Log error message
       
  1179                      MYLOCLOGSTRING1("iMapTileInterface->GetMapTileL() status-%d",error);
       
  1180                      MapTilefetchingCompleted(error, KNullDesC);
       
  1181                 }            
       
  1182             }            
       
  1183         }
       
  1184         else
       
  1185         {
       
  1186 		   if ( aErrCode == KErrCouldNotConnect )
       
  1187 		   {
       
  1188 		       lookupItem.iFetchingStatus = EMapTileFetchingNetworkError;
       
  1189                iMyLocationThreeAMTimer->StartTimer();
       
  1190               
       
  1191 		   }
       
  1192 		   else
       
  1193 		   {
       
  1194 		       lookupItem.iFetchingStatus = EMapTileFetchingUnknownError;
       
  1195 		   }
       
  1196            TRAP_IGNORE( UpdateMaptileDatabaseL(iMapTileRequestQueue[0]->iEventType,lookupItem ) );
       
  1197            
       
  1198 		   ProcessNextMaptileRequest();
       
  1199         }
       
  1200     }    
       
  1201 }
       
  1202 
       
  1203 void CMyLocationsEngine::MyLocationThreeAMTimerExpiredL()
       
  1204 {
       
  1205     //Forward the event for maptile fetching only if maptile plugin available
       
  1206     if( iMaptileGeocoderPluginAvailable )
       
  1207     {
       
  1208         RArray<TLookupItem> iLookupItems;
       
  1209         iMaptileDatabase->FindEntriesByMapTileFetchingStateL((TUint32)EMapTileFetchingNetworkError,
       
  1210                                             iLookupItems);
       
  1211         for( TUint32 i = 0; i < iLookupItems.Count(); i++ )
       
  1212         {
       
  1213             TLookupItem iItem = iLookupItems[i];
       
  1214             switch( iItem.iSource )
       
  1215             {
       
  1216                 // Get the home address details
       
  1217                 case ESourceContactsHome:
       
  1218                 case ESourceContactsWork:
       
  1219                 case ESourceContactsPref:
       
  1220                      { 
       
  1221                         QContact contactInfo = iContactManager->contact( iItem.iUid );
       
  1222                         CPosLandmark *addressLm = NULL;
       
  1223     
       
  1224                         foreach ( QContactAddress address, contactInfo.details<QContactAddress>() )
       
  1225                         {
       
  1226                             QStringList context = address.contexts();
       
  1227                             if ( ( context.isEmpty() && iItem.iSource == ESourceContactsPref ) ||
       
  1228                                    ( context.first() == QContactAddress::ContextHome  && iItem.iSource == ESourceContactsHome ) ||
       
  1229                                    ( context.first() == QContactAddress::ContextWork  && iItem.iSource == ESourceContactsWork ) ) 
       
  1230                             {
       
  1231                                 // Get the default/prefered address details
       
  1232                                 addressLm = GetContactAddressDetailsLC( address );
       
  1233                                 if( addressLm ) 
       
  1234                                 {
       
  1235                                     RequestMapTileImageL( *addressLm,
       
  1236                                            ( TUidSourceType )iItem.iSource, iItem.iUid );
       
  1237                                     CleanupStack::PopAndDestroy( addressLm );
       
  1238                                     break;
       
  1239                                 }
       
  1240                             }
       
  1241                         }                    
       
  1242                      }
       
  1243                      break;
       
  1244                 
       
  1245                 case ESourceCalendar:
       
  1246                      {
       
  1247                         CCalEntry* calEntry = NULL;
       
  1248                         calEntry = iCalView->FetchL(iItem.iUid);
       
  1249                         if( calEntry )
       
  1250                         {
       
  1251                             CleanupStack::PushL(calEntry);
       
  1252                             TPtrC address(calEntry->LocationL());
       
  1253                             if(address.Length()>0)
       
  1254                             {        
       
  1255                                 RequestMapTileImageL( address, ESourceCalendar, iItem.iUid);
       
  1256                             }
       
  1257                             CleanupStack::PopAndDestroy(calEntry);
       
  1258                         }
       
  1259                         else
       
  1260                         {
       
  1261                             iMaptileDatabase->DeleteEntryL( iItem );
       
  1262                         }
       
  1263                      }
       
  1264                      break;
       
  1265                  
       
  1266                  default:
       
  1267                      break;
       
  1268               }
       
  1269          }// end for
       
  1270      }
       
  1271 }
       
  1272 
       
  1273 
       
  1274 // -----------------------------------------------------------------------------
  1288 // CMyLocationsEngine::MapTilefetchingCompleted()
  1275 // CMyLocationsEngine::MapTilefetchingCompleted()
  1289 // Handles the maptile fetching completion event and updates the maptile lookup db.
  1276 // Handles the maptile fetching completion event and updates the maptile lookup db.
  1290 // -----------------------------------------------------------------------------
  1277 // -----------------------------------------------------------------------------
  1291 //
  1278 //
  1292 void CMyLocationsEngine::MapTilefetchingCompleted(TInt aErrCode,
  1279 void CMyLocationsEngine::MapTilefetchingCompleted(TInt aErrCode,
  1294 {
  1281 {
  1295     __TRACE_CALLSTACK;
  1282     __TRACE_CALLSTACK;
  1296     MYLOCLOGSTRING1("MapTilefetchingCompleted aErrCode - %d ",aErrCode);
  1283     MYLOCLOGSTRING1("MapTilefetchingCompleted aErrCode - %d ",aErrCode);
  1297     MYLOCLOGSTRING1("iMapTileRequestQueue.Count - %d",iMapTileRequestQueue.Count());
  1284     MYLOCLOGSTRING1("iMapTileRequestQueue.Count - %d",iMapTileRequestQueue.Count());
  1298 
  1285 
  1299     if (iMapTileRequestQueue.Count() > 0)
  1286     if ( iMapTileRequestQueue.Count() > 0 )
  1300     {
  1287     {
  1301 
  1288 
  1302         MYLOCLOGSTRING1("No.of RequestQueue - %d",iMapTileRequestQueue.Count());
  1289         MYLOCLOGSTRING1("No.of RequestQueue - %d",iMapTileRequestQueue.Count());
  1303 
  1290 
  1304         if (aErrCode == KErrNone )
  1291         TLookupItem lookupItem;
       
  1292         lookupItem.iSource = iMapTileRequestQueue[0]->iAddressType;
       
  1293         lookupItem.iUid = iMapTileRequestQueue[0]->iUId;
       
  1294 
       
  1295         if ( aErrCode == KErrNone )
  1305         {           
  1296         {           
  1306             TLookupItem lookupItem;
       
  1307             lookupItem.iSource = iMapTileRequestQueue[0]->iAddressType;
       
  1308             lookupItem.iUid = iMapTileRequestQueue[0]->iUId;
       
  1309             lookupItem.iFilePath.Copy(aMapTilePath);
  1297             lookupItem.iFilePath.Copy(aMapTilePath);
  1310             TRAP_IGNORE( HandleMaptileDatabaseL(iMapTileRequestQueue[0]->iEventType,lookupItem ) );
  1298    		    lookupItem.iFetchingStatus = EMapTileFectchingCompleted;
  1311         }
  1299  
  1312 
  1300         }
  1313             delete iMapTileRequestQueue[0];
  1301         else if ( aErrCode == KErrCouldNotConnect )
  1314             iMapTileRequestQueue.Remove(0);
  1302 		{
  1315             iMapTileRequestQueue.Compress();
  1303 		    lookupItem.iFetchingStatus = EMapTileFetchingNetworkError;
  1316     }
  1304             iMyLocationThreeAMTimer->StartTimer();
  1317     
  1305               
  1318     
  1306 		}
  1319     //Process the next request
  1307 		else
  1320     if (iMapTileRequestQueue.Count() > 0)
  1308 		{
  1321     {
  1309 		    lookupItem.iFetchingStatus = EMapTileFetchingUnknownError;
       
  1310 		}
       
  1311 		
       
  1312         TRAP_IGNORE( UpdateMaptileDatabaseL(iMapTileRequestQueue[0]->iEventType,lookupItem ) );
       
  1313     }
       
  1314     
       
  1315     ProcessNextMaptileRequest();
       
  1316 }
       
  1317 
       
  1318 
       
  1319 // -----------------------------------------------------------------------------
       
  1320 // CMyLocationsEngine::ProcessNextMaptileRequest()
       
  1321 // Process the next maptile request if any pending.
       
  1322 // -----------------------------------------------------------------------------
       
  1323 //
       
  1324 void CMyLocationsEngine::ProcessNextMaptileRequest()
       
  1325 {
       
  1326     //Process the next request if it is pending
       
  1327     if ( iMapTileRequestQueue.Count() > 0)
       
  1328     {
       
  1329         //Remove the current top request 
       
  1330         delete iMapTileRequestQueue[0];
       
  1331         iMapTileRequestQueue.Remove(0);
       
  1332         iMapTileRequestQueue.Compress();
       
  1333     
       
  1334         //Process the next request in queue
  1322         MYLOCLOGSTRING1("MapTile fetch completed request-%d",iMapTileRequestQueue.Count());
  1335         MYLOCLOGSTRING1("MapTile fetch completed request-%d",iMapTileRequestQueue.Count());
  1323         for (TInt cnt = 0; cnt < iMapTileRequestQueue.Count(); cnt++)
  1336         for (TInt cnt = 0; cnt < iMapTileRequestQueue.Count(); cnt++)
  1324         {          
  1337         {          
  1325             if ( KErrNone == RequestExecute(iMapTileRequestQueue[0]) )
  1338             if ( KErrNone == RequestExecute( iMapTileRequestQueue[0]) )
  1326             {
  1339             {
  1327                 break;
  1340                    break;
  1328             }
  1341             }
  1329             else
  1342             else
  1330             {
  1343             {
  1331                 delete iMapTileRequestQueue[0];
  1344                //Process the next request in queue
  1332                 iMapTileRequestQueue.Remove(0);
  1345                ProcessNextMaptileRequest();
  1333                 iMapTileRequestQueue.Compress();
  1346 
  1334             }
  1347             }
  1335         }
  1348         }
  1336     }
  1349     }
  1337     else
  1350     else
  1338     {
  1351     {
  1339         MYLOCLOGSTRING("MapTile fetch completed no request in queue");
  1352        MYLOCLOGSTRING("MapTile fetch completed no request in queue");
  1340         iMapTileRequestQueue.Reset();
  1353            iMapTileRequestQueue.Reset();
  1341     }
  1354     }    
  1342 
  1355 }
  1343 }
  1356 
  1344 // -----------------------------------------------------------------------------
  1357 // -----------------------------------------------------------------------------
  1345 // CMyLocationsEngine::HandleMaptileDatabaseL()
  1358 // CMyLocationsEngine::HandleMaptileDatabaseL()
  1346 // Handle maptile database(find/create/update/delete).
  1359 // Handle maptile database(find/create/update/delete).
  1347 // -----------------------------------------------------------------------------
  1360 // -----------------------------------------------------------------------------
  1348 //
  1361 //
  1349 void CMyLocationsEngine::HandleMaptileDatabaseL(
  1362 void CMyLocationsEngine::UpdateMaptileDatabaseL(
  1350         TInt aEventType, TLookupItem& aLookupItem)
  1363         TInt aEventType, TLookupItem& aLookupItem)
  1351 {
  1364 {
  1352     __TRACE_CALLSTACK;
  1365     __TRACE_CALLSTACK;
  1353     if (aEventType == EContactDbObserverEventContactChanged || aEventType
  1366     if (aEventType == EContactDbObserverEventContactChanged || aEventType
  1354             == EChangeModify)
  1367             == EChangeModify || aEventType == EContactDbObserverEventContactAdded ||
       
  1368             aEventType == EChangeAdd )
  1355     {
  1369     {
  1356         if (iMaptileDatabase->FindEntryL(aLookupItem))
  1370         if (iMaptileDatabase->FindEntryL(aLookupItem))
  1357         {
  1371         {
  1358             iMaptileDatabase->UpdateEntryL(aLookupItem);
  1372             iMaptileDatabase->UpdateEntryL(aLookupItem);
  1359         }
  1373         }
  1360         else
  1374         else
  1361         {
  1375         {
  1362             iMaptileDatabase->CreateEntryL(aLookupItem);
  1376             iMaptileDatabase->CreateEntryL(aLookupItem);
  1363         }
  1377         }
  1364     }
       
  1365     else if (aEventType == EContactDbObserverEventContactAdded || aEventType
       
  1366             == EChangeAdd)
       
  1367     {
       
  1368         iMaptileDatabase->CreateEntryL(aLookupItem);
       
  1369 
       
  1370     }
  1378     }
  1371     iMyLocationsDatabaseManager->UpdateMapTilePath( aLookupItem.iUid, aLookupItem.iSource, 
  1379     iMyLocationsDatabaseManager->UpdateMapTilePath( aLookupItem.iUid, aLookupItem.iSource, 
  1372                                             aLookupItem.iFilePath );    
  1380                                             aLookupItem.iFilePath );    
  1373 }
  1381 }
  1374 
  1382 
  1376 // CMyLocationsEngine::RestGeoCodeCompleted()
  1384 // CMyLocationsEngine::RestGeoCodeCompleted()
  1377 // observed when rest geo codeing completed.
  1385 // observed when rest geo codeing completed.
  1378 // started lat and lon field updation into contact db.
  1386 // started lat and lon field updation into contact db.
  1379 // -----------------------------------------------------------------------------
  1387 // -----------------------------------------------------------------------------
  1380 //
  1388 //
  1381 void CMyLocationsEngine::RestGeoCodeCompleted(TReal aLatitude, TReal aLongitude)
  1389 void CMyLocationsEngine::UpdateGeoCodeToAppDataBase( TReal aLatitude, TReal aLongitude )
  1382 {
  1390 {
  1383     __TRACE_CALLSTACK;
  1391     __TRACE_CALLSTACK;
  1384 
  1392 
  1385     MYLOCLOGSTRING1("No. of iMapTileRequestQueue - %d",iMapTileRequestQueue.Count());
  1393     MYLOCLOGSTRING1("No. of iMapTileRequestQueue - %d",iMapTileRequestQueue.Count());
  1386     if (iMapTileRequestQueue.Count() > 0)
  1394     if (iMapTileRequestQueue.Count() > 0)
  1433         StartLandmarksChangeNotifier();
  1441         StartLandmarksChangeNotifier();
  1434     }
  1442     }
  1435     
  1443     
  1436 }
  1444 }
  1437 
  1445 
       
  1446 // -----------------------------------------------------------------------------
       
  1447 // CMyLocationsEngine::ManipulateMapTileDataBaseL()
       
  1448 // -----------------------------------------------------------------------------
       
  1449 //
       
  1450 void CMyLocationsEngine::ManipulateMapTileDataBaseL(TLookupItem aLookupItem)
       
  1451 {
       
  1452     __TRACE_CALLSTACK;
       
  1453     TBool entryAvailable=EFalse;
       
  1454     entryAvailable = iMaptileDatabase->FindEntryL(aLookupItem);
       
  1455     iMaptileDatabase->DeleteEntryL(aLookupItem);
       
  1456     if (entryAvailable)
       
  1457     {
       
  1458         iMaptileDatabase->DeleteMapTileL(aLookupItem);
       
  1459     }
       
  1460             
       
  1461 }
  1438 //End of file
  1462 //End of file
  1439 
  1463