locationdataharvester/mylocationsengine/src/mylocationsengine.cpp
branchRCL_3
changeset 36 1fc85118c3ae
equal deleted inserted replaced
34:8173571d354e 36:1fc85118c3ae
       
     1 /*
       
     2 * Copyright (c) 2010 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: Mylocation engine source implementation for location picker
       
    15 *              and maptile service.
       
    16 *
       
    17 */
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <QFile>
       
    21 #include <f32file.h>
       
    22 #include <calchangecallback.h> 
       
    23 #include <cntitem.h>
       
    24 #include <cntfldst.h>
       
    25 #include <EPos_CPosLmDatabaseManager.h>
       
    26 #include <lbsposition.h>
       
    27 #include <bautils.h>
       
    28 #include <f32file.h>
       
    29 #include <locationservicedefines.h>
       
    30 #include <e32property.h>
       
    31 #include "contactsubscriber.h"
       
    32 #include "calendarsubscriber.h"
       
    33 #include "mylocationsengine.h"
       
    34 #include "geocodeupdate.h" //header for GeocodeUpdate class
       
    35 //handle for CMyLocationsHistoryDbObserver class
       
    36 #include "mylocationlogger.h"
       
    37 #if ( defined __WINSCW__ ) || ( defined __WINS__ )
       
    38 _LIT ( KImageStorageDrive, "C:\\MapTile\\");
       
    39 #endif
       
    40 _LIT(KFolderName,":\\MapTile\\");
       
    41 const TInt KImagePathSize=36;
       
    42 const TInt KDefaultFilePathSize = 20;
       
    43 const TUid KMaptileStatusPublish={0x2002680A};
       
    44 enum TMaptileStatusKeys {EMaptileStatusInteger=0x1};
       
    45 
       
    46 //Protocol : [appid-addresstype-maptilestatus]
       
    47 _LIT8( KMaptileStatusFormat, "%d-%d-%d" );
       
    48 const TInt KProtocolBufferSize = 16;
       
    49 
       
    50 const QString KSpace(" ");
       
    51 
       
    52 // ============================ MEMBER FUNCTIONS ===============================
       
    53 
       
    54 CMyLocationsEngine* CMyLocationsEngine::NewL()
       
    55 {
       
    56     CMyLocationsEngine* self = new (ELeave) CMyLocationsEngine();
       
    57     CleanupStack::PushL(self);
       
    58     self->ConstructL();
       
    59     CleanupStack::Pop(self);
       
    60     return self;
       
    61 }
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // CMyLocationsEngine::ConstructL()
       
    65 // Symbian 2nd phase constructor can leave.
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 void CMyLocationsEngine::ConstructL()
       
    69 {
       
    70     __TRACE_CALLSTACK;
       
    71     CActiveScheduler::Add(this);
       
    72 
       
    73     //Connection to Landmark DB
       
    74     iLandmarkDb = CPosLandmarkDatabase::OpenL();
       
    75     ExecuteAndDeleteLD(iLandmarkDb->InitializeL());
       
    76 
       
    77     MYLOCLOGSTRING("Connection to Landmark DB.");
       
    78     // create CMyLocationsDatabaseManager instance
       
    79     iMyLocationsDatabaseManager = new (ELeave) CMyLocationsDatabaseManager;
       
    80     iMyLocationsDatabaseManager->ConstructL();
       
    81     
       
    82     MYLOCLOGSTRING("create CMyLocationsDatabaseManager instance");
       
    83 
       
    84     TRAPD( error , iMapTileInterface = CMapTileInterface::NewL());
       
    85     if( error == KErrNone )
       
    86     {
       
    87         iMaptileGeocoderPluginAvailable = ETrue;	
       
    88     }
       
    89     
       
    90     MYLOCLOGSTRING("iMapTileInterface = CMapTileInterface::NewL()");
       
    91     //Maptile Db instance
       
    92     iMaptileDatabase = CLookupMapTileDatabase::NewL(KMapTileLookupDatabaseName);
       
    93 
       
    94     MYLOCLOGSTRING("Maptile Db instance created ");
       
    95 	
       
    96     iGeocodeUpdate = new GeocodeUpdate();
       
    97     iMyLocationThreeAMTimer = CLocationGeoTagTimerAO::NewL(*this);
       
    98   
       
    99     iMyLocationThreeAMTimer->StartTimer();
       
   100     
       
   101     MYLOCLOGSTRING(" iMyLocationThreeAMTimer = CLocationGeoTagTimerAO::NewL(this)");
       
   102   
       
   103     //Create instance of contact manager 
       
   104     iContactManager = new QContactManager();
       
   105 
       
   106     MYLOCLOGSTRING(" start contact db observation ");
       
   107     StartContactsChangeNotifierL();
       
   108 
       
   109     //set the folder path to store maptile
       
   110     imageFilePath.Zero();
       
   111     SetFolderPathL();
       
   112 
       
   113     TInt status = KErrNone;
       
   114     iCalSession = CCalSession::NewL();
       
   115     NotifyChangeL(status);
       
   116 
       
   117     // Start listening to landmarks db changes
       
   118     StartLandmarksChangeNotifier();   
       
   119     iContactSubscriber = CContactSubscriber::NewL(this);
       
   120     iCalendarSubscriber = CCalendarSubscriber::NewL(this);
       
   121     TInt ret = RProperty::Define( KMaptileStatusPublish, EMaptileStatusInteger, RProperty::EByteArray  );
       
   122 
       
   123 }
       
   124 // -----------------------------------------------------------------------------
       
   125 // CMyLocationsEngine::SetFolderPath()
       
   126 // set folder path structure
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 void CMyLocationsEngine::SetFolderPathL()
       
   130 {
       
   131     __TRACE_CALLSTACK;
       
   132     RFs session;
       
   133     User::LeaveIfError(session.Connect());
       
   134    
       
   135     if ( imageFilePath.Length() > 0 && BaflUtils::FolderExists(session, imageFilePath))
       
   136     {
       
   137         session.Close();
       
   138         MYLOCLOGSTRING("Image folder path exists");
       
   139         return;
       
   140     }
       
   141     const TUint EMediaMemoryCard = 0x000014;
       
   142     imageFilePath.Zero();
       
   143 #if ( defined __WINSCW__ ) || ( defined __WINS__ )
       
   144     imageFilePath.Copy(KImageStorageDrive);
       
   145     MYLOCLOGSTRING("using in  debug mode");
       
   146 #else        
       
   147     TVolumeInfo vol;
       
   148     TInt driveNumber = EDriveE;
       
   149     TChar driveLetter;
       
   150     TDriveInfo driveInfo;
       
   151     TInt error( KErrNone );
       
   152     error=session.Volume(vol, driveNumber);
       
   153     MYLOCLOGSTRING1("E drive volume info status -%d",error);
       
   154     if (error == KErrNone)
       
   155     {
       
   156         error = session.Drive(driveInfo, driveNumber);
       
   157         MYLOCLOGSTRING1("E driveinfo status -%d",error);
       
   158         switch(driveInfo.iType)
       
   159         {
       
   160             case EMediaFlash:
       
   161             case EMediaHardDisk:
       
   162             case EMediaNANDFlash:
       
   163             case EMediaMemoryCard:
       
   164             case EMediaCdRom:
       
   165             {
       
   166                 MYLOCLOGSTRING(" E drive match case");
       
   167                 session.DriveToChar( driveNumber , driveLetter );
       
   168                 imageFilePath.Append(driveLetter);
       
   169                 imageFilePath.Append(KFolderName);
       
   170                 break;
       
   171             }
       
   172             default:
       
   173             break;
       
   174 
       
   175         };
       
   176     }
       
   177 
       
   178     if (!imageFilePath.Length() > 0)
       
   179     {
       
   180         driveNumber = EDriveF;
       
   181         error = session.Volume(vol, driveNumber);
       
   182         MYLOCLOGSTRING1("F drive volume info status -%d",error);
       
   183 
       
   184         if (error == KErrNone)
       
   185         {
       
   186             error = session.Drive(driveInfo, driveNumber);
       
   187             MYLOCLOGSTRING1("F drive info status-%d", error);
       
   188 
       
   189             switch (driveInfo.iMediaAtt)
       
   190             {
       
   191                 /** Solid-state media. */
       
   192                 case EMediaFlash:
       
   193                 case EMediaHardDisk:
       
   194                 case EMediaNANDFlash:
       
   195                 case EMediaMemoryCard:
       
   196                 {
       
   197                     MYLOCLOGSTRING("F drive exist as this type");
       
   198                     session.DriveToChar(driveNumber, driveLetter);
       
   199                     imageFilePath.Append(driveLetter);
       
   200                     imageFilePath.Append(KFolderName);
       
   201                     break;
       
   202                 }
       
   203 
       
   204                 default:
       
   205                 break;
       
   206 
       
   207             };
       
   208         }
       
   209     }
       
   210 #endif
       
   211     if (imageFilePath.Length()>0 && !BaflUtils::FolderExists(session, imageFilePath))
       
   212     {
       
   213         session.MkDirAll(imageFilePath);
       
   214         MYLOCLOGSTRING("folder path does not exist , created ");
       
   215     }
       
   216     session.Close();
       
   217 }
       
   218 // -----------------------------------------------------------------------------
       
   219 // CMyLocationsEngine::CMyLocationsEngine()
       
   220 // C++ default constructor can NOT contain any code, that might leave.
       
   221 // -----------------------------------------------------------------------------
       
   222 //
       
   223 CMyLocationsEngine::CMyLocationsEngine() :
       
   224             CActive(EPriorityStandard), 
       
   225             iCalSession(NULL),
       
   226             iCalView(NULL), 
       
   227 	        iContactsDb(NULL), 
       
   228 	        iContactChangeNotifier(NULL), 
       
   229 			iLandmarkDb(NULL),
       
   230             iMapTileInterface(NULL),
       
   231             iMyLocationsDatabaseManager(NULL),
       
   232             iMaptileDatabase(NULL),
       
   233             iMyLocationThreeAMTimer(NULL),
       
   234             iMaptileGeocoderPluginAvailable(EFalse),
       
   235             iCalenderNotification(NULL),
       
   236             iContactManager(NULL),
       
   237             iContactSubscriber(NULL),
       
   238             iCalendarSubscriber(NULL),
       
   239             iGeocodeUpdate(NULL),
       
   240             iLastContactId( -1 ) ,
       
   241             iLastCalendarId( 0 )
       
   242 {
       
   243 
       
   244 }
       
   245 
       
   246 // -----------------------------------------------------------------------------
       
   247 // CMyLocationsEngine::~CMyLocationsEngine()
       
   248 // Destructor.
       
   249 // -----------------------------------------------------------------------------
       
   250 //
       
   251 CMyLocationsEngine::~CMyLocationsEngine()
       
   252 {
       
   253     __TRACE_CALLSTACK;// delete the member variables.
       
   254    
       
   255     Cancel();
       
   256     
       
   257     delete iContactChangeNotifier;
       
   258 
       
   259     delete iMyLocationsDatabaseManager;
       
   260 
       
   261     delete iLandmarkDb;
       
   262 
       
   263     delete iContactsDb;
       
   264    
       
   265     delete iCalView;
       
   266 	
       
   267     delete iCalSession;
       
   268 	 
       
   269 	delete iMapTileInterface;
       
   270 
       
   271     delete iMaptileDatabase;
       
   272        
       
   273     delete iCalenderNotification;
       
   274     
       
   275 	delete iMyLocationThreeAMTimer;
       
   276 		
       
   277     delete iContactManager;
       
   278     
       
   279     delete iContactSubscriber;
       
   280 
       
   281     delete iCalendarSubscriber;
       
   282     
       
   283     delete iGeocodeUpdate;
       
   284     
       
   285     for( TInt index = 0; index < iAddressInfo.Count(); index++ )
       
   286     {
       
   287         delete iAddressInfo[index];
       
   288         iAddressInfo.Remove(index);
       
   289         iAddressInfo.Compress();
       
   290     }
       
   291     
       
   292     iAddressInfo.Reset();
       
   293     
       
   294 }
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // CMyLocationsEngine::NotifyChangeL()
       
   298 // Starts calender db create notification to get callbacks. 
       
   299 // -----------------------------------------------------------------------------
       
   300 //
       
   301 void CMyLocationsEngine::NotifyChangeL(TInt &aStatus)
       
   302 {
       
   303     __TRACE_CALLSTACK;
       
   304     
       
   305     TBufC<KDefaultFilePathSize> defaultFile = iCalSession->DefaultFileNameL();
       
   306     TChar drive = defaultFile[0];    
       
   307     TRAP(aStatus, iCalSession->OpenL( defaultFile ));
       
   308     MYLOCLOGSTRING1("iCalSession->OpenL() status-%d",aStatus);
       
   309     if ( KErrNone == aStatus )
       
   310     {
       
   311         // create a calendar entry view with the opened session
       
   312         iCalView = CCalEntryView::NewL(*iCalSession);
       
   313         StartCalenderChangeNotifierL();
       
   314     }
       
   315     else
       
   316     {
       
   317         if( !iCalenderNotification )
       
   318         {
       
   319             iCalenderNotification = CCalenderNotification::NewL( this );
       
   320         }
       
   321         
       
   322         
       
   323         iCalenderNotification->CheckCalenderDbFileStructure( drive );
       
   324     }
       
   325 }
       
   326 // -----------------------------------------------------------------------------
       
   327 // CMyLocationsEngine::GetChangeNoficationL()
       
   328 // To get callbacks through publisher from contact context
       
   329 // -----------------------------------------------------------------------------
       
   330 //
       
   331 void CMyLocationsEngine::GetChangeNotificationL( TInt &aId, TInt& addressType, TInt& addressCount  )
       
   332 {
       
   333     __TRACE_CALLSTACK;
       
   334    
       
   335     iLastContactId = aId;
       
   336     
       
   337     //If the requested id is already in queue, just return
       
   338     for( TInt index = 0 ; index < iMapTileRequestQueue.Count(); index++ )
       
   339     {
       
   340         if( iLastContactId == iMapTileRequestQueue[index]->iUId )
       
   341         {           
       
   342             MYLOCLOGSTRING("contact id is in queue");
       
   343             return;
       
   344         }
       
   345     }
       
   346     
       
   347     TAppAddressInfo* addressInfo = new (ELeave) TAppAddressInfo;
       
   348     addressInfo->iUid = aId;
       
   349     addressInfo->iAddressType =  addressType;
       
   350     //Memory will be freed when the queue is deleted
       
   351     if( iAddressInfo.Append(addressInfo) != KErrNone )
       
   352     {
       
   353         delete addressInfo;
       
   354     }
       
   355     
       
   356     //Get all 3 adress
       
   357     if( addressCount > 1 && iAddressInfo.Count() < addressCount )
       
   358         return;  
       
   359     
       
   360     
       
   361     for( TInt index = 0; index < iAddressInfo.Count(); index++ )
       
   362     {
       
   363         TUidSourceType type = static_cast<TUidSourceType>(iAddressInfo[index]->iAddressType );
       
   364     
       
   365         //if type is contact
       
   366         if( type == ESourceContactsPref || type == ESourceContactsWork || 
       
   367                       type == ESourceContactsHome )
       
   368         {
       
   369             QContact contactInfo = iContactManager->contact( iAddressInfo[index]->iUid );
       
   370             
       
   371             CPosLandmark *contactAddressLm = NULL;
       
   372           
       
   373             TInt itemCount = 0;
       
   374             
       
   375             foreach ( QContactAddress address, contactInfo.details<QContactAddress>() )
       
   376             {
       
   377                 QStringList context = address.contexts();
       
   378                 if ( context.isEmpty()  && type ==  ESourceContactsPref ) // no context
       
   379                 {
       
   380                     contactAddressLm = GetContactAddressDetailsLC( address );
       
   381                     itemCount++;
       
   382                     break;
       
   383                 }
       
   384                 else if ( !context.isEmpty() && context.first() == QContactAddress::ContextHome &&
       
   385                                     type == ESourceContactsHome )
       
   386                 {
       
   387                     contactAddressLm = GetContactAddressDetailsLC( address );
       
   388                     itemCount++;
       
   389                     break;
       
   390                 }
       
   391                 else if ( !context.isEmpty() && context.first() == QContactAddress::ContextWork  && 
       
   392                                 type == ESourceContactsWork )
       
   393                 {
       
   394                     contactAddressLm = GetContactAddressDetailsLC( address );
       
   395                     itemCount++;
       
   396                     break;
       
   397                 }
       
   398             }
       
   399 
       
   400             //Update the entry with inprogress status
       
   401             TLookupItem lookupItem;
       
   402             lookupItem.iUid = iAddressInfo[index]->iUid;
       
   403             lookupItem.iSource = type;
       
   404             lookupItem.iFilePath.Zero();
       
   405             lookupItem.iFetchingStatus = EMapTileFetchingInProgress;
       
   406             iMaptileDatabase->UpdateEntryL( lookupItem );
       
   407             
       
   408             //Request for maptile fetching
       
   409             if( contactAddressLm )
       
   410             {
       
   411                 RequestMapTileImageL( *contactAddressLm, type, 
       
   412                          iAddressInfo[index]->iUid, EContactDbObserverEventContactChanged );
       
   413             }
       
   414             
       
   415             CleanupStack::PopAndDestroy( itemCount );
       
   416         }
       
   417     }
       
   418     for( TInt index = 0; index < iAddressInfo.Count(); index++ )
       
   419     {
       
   420 	    //free the allocated memory
       
   421         delete iAddressInfo[index];
       
   422 		iAddressInfo.Remove(index);
       
   423         iAddressInfo.Compress();
       
   424 	}
       
   425     
       
   426     iAddressInfo.Reset();
       
   427 }
       
   428 
       
   429 // -----------------------------------------------------------------------------
       
   430 // CMyLocationsEngine::SubscribeFromCalendarL()
       
   431 // To get callbacks through publisher from calendar context
       
   432 // -----------------------------------------------------------------------------
       
   433 //
       
   434 void CMyLocationsEngine::SubscribeFromCalendarL(TInt aId)
       
   435 {
       
   436     __TRACE_CALLSTACK;
       
   437     iLastCalendarId = aId;
       
   438     for ( int index =0;iMapTileRequestQueue.Count()>index ;index++)
       
   439     {
       
   440         if( iLastCalendarId == iMapTileRequestQueue[index]->iUId )
       
   441         {            
       
   442             return;
       
   443         }
       
   444     }
       
   445     
       
   446     CCalEntry* calEntry = NULL;
       
   447     calEntry = iCalView->FetchL(aId);
       
   448     CleanupStack::PushL(calEntry);
       
   449     TPtrC address(calEntry->LocationL());
       
   450     if( address.Length()>0 )
       
   451     {
       
   452         TLookupItem lookupItem;
       
   453         lookupItem.iUid = aId;
       
   454         lookupItem.iSource = ESourceCalendar;
       
   455         lookupItem.iFilePath.Zero();
       
   456         lookupItem.iFetchingStatus = EMapTileFetchingInProgress;
       
   457         iMaptileDatabase->UpdateEntryL( lookupItem );
       
   458         RequestMapTileImageL( address, ESourceCalendar, aId , EChangeModify );
       
   459     }
       
   460     CleanupStack::PopAndDestroy(calEntry);
       
   461 }
       
   462 // -----------------------------------------------------------------------------
       
   463 // CMyLocationsEngine::StartCalenderChangeNotifierL()
       
   464 // Starts calender change notification method to get callbacks when entries are 
       
   465 // added/modified/deleted in contacts
       
   466 // -----------------------------------------------------------------------------
       
   467 //
       
   468 void CMyLocationsEngine::StartCalenderChangeNotifierL()
       
   469 {
       
   470     __TRACE_CALLSTACK;// Get the current time and set Calendar filter to consider entries whose start time
       
   471     // fall under the timeframe one year past and one year ahead of the current time.
       
   472     if (iCalSession)
       
   473     {
       
   474         MYLOCLOGSTRING("iCalSession is not null");
       
   475         TTime currentTime;
       
   476         currentTime.HomeTime();
       
   477 
       
   478         TTime startTime = currentTime - TTimeIntervalYears(1);
       
   479         TTime endTime = currentTime + TTimeIntervalYears(1);
       
   480 
       
   481         TCalTime calStartTime;
       
   482         calStartTime.SetTimeLocalL(startTime);
       
   483         
       
   484         MYLOCLOGSTRING("SetTimeLocalL(startTime)");
       
   485 
       
   486         TCalTime calEndTime;
       
   487         calEndTime.SetTimeLocalL(endTime);
       
   488         
       
   489         MYLOCLOGSTRING("SetTimeLocalL(endTime)");
       
   490 
       
   491         // set the filter for modification tracking
       
   492         CCalChangeNotificationFilter *filter =
       
   493                 CCalChangeNotificationFilter::NewL(
       
   494                         MCalChangeCallBack2::EChangeEntryAll, ETrue,
       
   495                         CalCommon::TCalTimeRange(calStartTime, calEndTime));
       
   496         MYLOCLOGSTRING(" CCalChangeNotificationFilter::NewL()");
       
   497         // 'this' object implements MCalChangeCallBack
       
   498         iCalSession->StartChangeNotification(*this, *filter);
       
   499     }
       
   500 }
       
   501 
       
   502 // -----------------------------------------------------------------------------
       
   503 // CMyLocationsEngine::StartContactsChangeNotifierL()
       
   504 // Starts contacts change notification method to get callbacks when entries are 
       
   505 // added/modified/deleted in contacts
       
   506 // -----------------------------------------------------------------------------
       
   507 //
       
   508 void CMyLocationsEngine::StartContactsChangeNotifierL()
       
   509 {
       
   510     __TRACE_CALLSTACK;
       
   511    
       
   512     iGeocodeUpdate->createContactdb();
       
   513     iContactsDb = CContactDatabase::OpenL();
       
   514     // Create CContactChangeNotifier object with 'this' object. 
       
   515     iContactChangeNotifier = CContactChangeNotifier::NewL(*iContactsDb,this);
       
   516 }
       
   517 
       
   518 // -----------------------------------------------------------------------------
       
   519 // CMyLocationsEngine::StartLandmarksChangeNotifier()
       
   520 // Starts landmarks change notification method to get callbacks when entries are added/modified/deleted in landmarks
       
   521 // -----------------------------------------------------------------------------
       
   522 //
       
   523 void CMyLocationsEngine::StartLandmarksChangeNotifier()
       
   524 {
       
   525     __TRACE_CALLSTACK;// Resume event listening
       
   526     iLandmarkDb->NotifyDatabaseEvent(iLmEvent, iStatus);
       
   527     SetActive();
       
   528 }
       
   529 
       
   530 // -----------------------------------------------------------------------------
       
   531 // CMyLocationsEngine::CalChangeNotification()
       
   532 // Callback when there is a change in the calendar database.
       
   533 // -----------------------------------------------------------------------------
       
   534 //
       
   535 
       
   536 void CMyLocationsEngine::CalChangeNotification(
       
   537         RArray<TCalChangeEntry>& aChangeItems)
       
   538 {
       
   539     __TRACE_CALLSTACK;
       
   540        
       
   541     if(iCalenderNotification)
       
   542     {
       
   543         delete iCalenderNotification;
       
   544         iCalenderNotification = NULL;
       
   545     }
       
   546     
       
   547     // get entries associated with this UID
       
   548     for (int i = 0; i < aChangeItems.Count(); i++)
       
   549     {
       
   550 
       
   551         // Check if this is some undefined change in calendar db. 
       
   552         if( aChangeItems[0].iChangeType == EChangeUndefined && aChangeItems[0].iEntryType == EChangeEntryAll )
       
   553         {
       
   554             // Refresh the calendar related entries in the location and maptiledb.
       
   555             RefreshCalendarEntryListL();
       
   556             break;
       
   557         }
       
   558         TCalChangeEntry calChangeEntry = aChangeItems[i];
       
   559         iEventType = calChangeEntry.iChangeType;
       
   560         switch (calChangeEntry.iChangeType)
       
   561         {
       
   562         case EChangeAdd:
       
   563         {
       
   564             
       
   565             TRAP_IGNORE(CalenderEntryAddedL(calChangeEntry));
       
   566             break;
       
   567         }
       
   568 
       
   569         case EChangeModify:
       
   570         {
       
   571             if (iMapTileRequestQueue.Count() > 0)
       
   572             {
       
   573                 if (calChangeEntry.iEntryId == iMapTileRequestQueue[0]->iUId)
       
   574                 {
       
   575                     return;
       
   576                 }
       
   577             }
       
   578             TRAP_IGNORE( CalenderEntryModifyL(calChangeEntry) ) ;
       
   579             break;
       
   580         }
       
   581         case EChangeDelete:
       
   582         {
       
   583             TLookupItem lookupItem;
       
   584             lookupItem.iSource = ESourceCalendar;
       
   585             lookupItem.iUid = calChangeEntry.iEntryId;
       
   586             TRAP_IGNORE(ManipulateMapTileDataBaseL(lookupItem));           
       
   587             TRAP_IGNORE( UpdateDatabaseL( NULL, 
       
   588                               calChangeEntry.iEntryId, ESourceCalendar, EEntryDeleted ) );
       
   589             break;
       
   590         }
       
   591         };
       
   592 
       
   593     }
       
   594 }
       
   595 
       
   596 // -----------------------------------------------------------------------------
       
   597 // CMyLocationsEngine::RefreshCalendarEntryListL()
       
   598 // -----------------------------------------------------------------------------
       
   599 //
       
   600 void CMyLocationsEngine::RefreshCalendarEntryListL()
       
   601 {
       
   602     //Get all the calendar ids and check its validity. 
       
   603     //Delete if they are no more valid.
       
   604     RArray<TUint32> ids;
       
   605     iMaptileDatabase->GetAllCalendarIdsL( ids );
       
   606     for( TInt i = 0; i < ids.Count(); i++ )
       
   607     {
       
   608         if( !IsCalendarEntryValidL( ids[i] ) )
       
   609         {
       
   610             TLookupItem lookupItem;
       
   611             lookupItem.iSource = ESourceCalendar;
       
   612             lookupItem.iUid = ids[i];
       
   613             TRAP_IGNORE( ManipulateMapTileDataBaseL( lookupItem ) );           
       
   614             TRAP_IGNORE( UpdateDatabaseL( NULL, 
       
   615                     ids[i], ESourceCalendar, EEntryDeleted ) );
       
   616         
       
   617         }        
       
   618     }
       
   619 }
       
   620 
       
   621 // -----------------------------------------------------------------------------
       
   622 // CMyLocationsEngine::IsCalendarEntryValidL()
       
   623 // -----------------------------------------------------------------------------
       
   624 //
       
   625 TBool CMyLocationsEngine::IsCalendarEntryValidL( TUint32 aId )
       
   626 {
       
   627     return ( ( iCalView->FetchL( aId ) == NULL ) ? EFalse:ETrue );
       
   628 }
       
   629 // -----------------------------------------------------------------------------
       
   630 // CMyLocationsEngine::CalenderEntryAddedL()
       
   631 // -----------------------------------------------------------------------------
       
   632 //
       
   633 void CMyLocationsEngine::CalenderEntryAddedL(TCalChangeEntry aCalChangeEntry)
       
   634 {
       
   635     __TRACE_CALLSTACK;
       
   636     
       
   637     TUint32 entryId=0;
       
   638     entryId=aCalChangeEntry.iEntryId;
       
   639     //create entry in the data base and maintain a fetching state.
       
   640     TLookupItem lookupItem;
       
   641     lookupItem.iUid = entryId ;
       
   642     lookupItem.iSource = ESourceCalendar;
       
   643     lookupItem.iFilePath.Zero();
       
   644     lookupItem.iFetchingStatus = EMapTileFetchingInProgress;     
       
   645     TRAP_IGNORE( iMaptileDatabase->CreateEntryL(lookupItem) );
       
   646     CCalEntry* calEntry = NULL;
       
   647     calEntry = iCalView->FetchL( entryId );
       
   648     CleanupStack::PushL(calEntry);
       
   649     TPtrC address(calEntry->LocationL());     
       
   650     if (address.Length() > 0)
       
   651     {
       
   652         RequestMapTileImageL( address, ESourceCalendar, entryId , EChangeAdd );
       
   653     }  
       
   654     CleanupStack::PopAndDestroy(calEntry);
       
   655 }
       
   656 
       
   657 // -----------------------------------------------------------------------------
       
   658 // CMyLocationsEngine::CalenderEntryModifyL()
       
   659 // -----------------------------------------------------------------------------
       
   660 //
       
   661 void CMyLocationsEngine::CalenderEntryModifyL(TCalChangeEntry aCalChangeEntry)
       
   662 {
       
   663     __TRACE_CALLSTACK;
       
   664     TUint32 entryId = 0;
       
   665     entryId = aCalChangeEntry.iEntryId;
       
   666     TLookupItem lookupItem;
       
   667     lookupItem.iSource = ESourceCalendar;
       
   668     lookupItem.iUid = entryId;
       
   669     CCalEntry* calEntry = NULL;
       
   670     calEntry = iCalView->FetchL(entryId);
       
   671     CleanupStack::PushL(calEntry);
       
   672     if(iGeocodeUpdate->isGeocodeNotAvailable(entryId))
       
   673     {        
       
   674         TPtrC address(calEntry->LocationL());
       
   675         if (iMyLocationsDatabaseManager->CheckIfAddressChanged(address, entryId,
       
   676                 ESourceCalendar))
       
   677         {
       
   678             lookupItem.iFilePath.Zero();
       
   679             lookupItem.iFetchingStatus = EMapTileFetchingInProgress;
       
   680             TRAP_IGNORE( iMaptileDatabase->ReSetEntryL(lookupItem) );
       
   681             if (address.Length() > 0)
       
   682             {
       
   683                 RequestMapTileImageL(address, ESourceCalendar, entryId , EChangeModify);
       
   684             }
       
   685             else
       
   686             {
       
   687                 UpdateDatabaseL(NULL, entryId, ESourceCalendar, EEntryDeleted);
       
   688             }
       
   689             if ( lookupItem.iFilePath.Length() > 0 )
       
   690             {
       
   691                 iMaptileDatabase->DeleteMapTileL(lookupItem);
       
   692             }
       
   693     
       
   694         }        
       
   695     }   
       
   696     CleanupStack::PopAndDestroy(calEntry);
       
   697 }
       
   698 
       
   699 // -----------------------------------------------------------------------------
       
   700 // CMyLocationsEngine::HandleDatabaseEventL()
       
   701 // Callback that provides information about the contact database change event.
       
   702 // -----------------------------------------------------------------------------
       
   703 //
       
   704 void CMyLocationsEngine::HandleDatabaseEventL(TContactDbObserverEvent aEvent)
       
   705 {
       
   706     __TRACE_CALLSTACK;
       
   707     
       
   708     //Forward the event for maptile fetching only if maptile plugin available
       
   709     if( iMaptileGeocoderPluginAvailable )
       
   710     {
       
   711         TriggerMaptileRequestL(aEvent);
       
   712     }
       
   713 }
       
   714 
       
   715 
       
   716 // -----------------------------------------------------------------------------
       
   717 // CMyLocationsEngine::TriggerMaptileRequestL()
       
   718 // Process the contact address information for fetching maptile.
       
   719 // -----------------------------------------------------------------------------
       
   720 //
       
   721 void CMyLocationsEngine::TriggerMaptileRequestL(TContactDbObserverEvent& aEvent)
       
   722 {
       
   723     __TRACE_CALLSTACK;
       
   724     TLookupItem lookupItem;      
       
   725     lookupItem.iUid = aEvent.iContactId;
       
   726     // If contact is deleted delete from mylocations db
       
   727     if ( aEvent.iType == EContactDbObserverEventContactDeleted || aEvent.iType == EContactDbObserverEventOwnCardDeleted )
       
   728     {        
       
   729         //Delete the entries from maptile database
       
   730         lookupItem.iSource = ESourceContactsPref;
       
   731         TRAP_IGNORE( ManipulateMapTileDataBaseL(lookupItem));
       
   732 
       
   733         lookupItem.iSource = ESourceContactsWork;
       
   734         TRAP_IGNORE( ManipulateMapTileDataBaseL(lookupItem));
       
   735         
       
   736         lookupItem.iSource = ESourceContactsHome;
       
   737         TRAP_IGNORE( ManipulateMapTileDataBaseL(lookupItem));
       
   738               
       
   739         // Delete entries from  mylocation database
       
   740         TRAP_IGNORE( UpdateDatabaseL( NULL, aEvent.iContactId,
       
   741                      ESourceContactsPref, EEntryDeleted ));
       
   742        
       
   743         TRAP_IGNORE( UpdateDatabaseL( NULL, aEvent.iContactId,
       
   744                ESourceContactsHome, EEntryDeleted ));
       
   745 
       
   746         TRAP_IGNORE( UpdateDatabaseL( NULL, aEvent.iContactId,
       
   747                                ESourceContactsWork, EEntryDeleted ) );  
       
   748         
       
   749         MYLOCLOGSTRING("EContactDbObserverEventContactDeleted ");
       
   750         return;
       
   751     }
       
   752 
       
   753     //Get the contact item
       
   754     iEventType = aEvent.iType;
       
   755     QContact contactInfo = iContactManager->contact( aEvent.iContactId );            
       
   756     CPosLandmark *preferedAddressLm = NULL;
       
   757     CPosLandmark *workAddressLm = NULL;
       
   758     CPosLandmark *homeAddressLm = NULL;
       
   759     
       
   760     TInt itemCount = 0;
       
   761     
       
   762     foreach ( QContactAddress address, contactInfo.details<QContactAddress>() )
       
   763     {
       
   764         QStringList context = address.contexts();
       
   765         if ( context.isEmpty() ) // no context
       
   766         {
       
   767             preferedAddressLm = GetContactAddressDetailsLC( address );
       
   768             itemCount++;
       
   769         }
       
   770         else if ( context.first() == QContactAddress::ContextHome  )
       
   771         {
       
   772             homeAddressLm = GetContactAddressDetailsLC( address );
       
   773             itemCount++;
       
   774         }
       
   775         else if ( context.first() == QContactAddress::ContextWork )
       
   776         {
       
   777             workAddressLm = GetContactAddressDetailsLC( address );
       
   778             itemCount++;
       
   779         }
       
   780     }
       
   781    
       
   782     switch (aEvent.iType)
       
   783     {
       
   784         case EContactDbObserverEventContactChanged:
       
   785         case EContactDbObserverEventOwnCardChanged:
       
   786         {
       
   787             MYLOCLOGSTRING("EContactDbObserverEventContactChanged" );
       
   788             MYLOCLOGSTRING1("iMapTileRequestQueue.Count()-%d",iMapTileRequestQueue.Count() );
       
   789 
       
   790             if (iMapTileRequestQueue.Count() > 0)
       
   791             {
       
   792                 if (iMapTileRequestQueue[0]->iUId == aEvent.iContactId)
       
   793                 {
       
   794                     CleanupStack::PopAndDestroy( itemCount );
       
   795                     MYLOCLOGSTRING("retrun from geolocation callback" );
       
   796                     return;
       
   797                 }
       
   798             }
       
   799 
       
   800             TBuf<KBufSize> landmarkName;
       
   801             GetContactName(aEvent.iContactId,landmarkName);
       
   802 
       
   803             // if default address available, update Mylocations. 
       
   804             lookupItem.iSource = ESourceContactsPref;
       
   805             if (preferedAddressLm)
       
   806             {
       
   807                 iMyLocationsDatabaseManager->UpdateEntryName( aEvent.iContactId, ESourceContactsPref, 
       
   808                                      landmarkName );
       
   809                 MYLOCLOGSTRING("preferedAddressLm address changed" );
       
   810 
       
   811                 if ( iMyLocationsDatabaseManager->CheckIfAddressChanged(*preferedAddressLm,
       
   812                     aEvent.iContactId, ESourceContactsPref) )
       
   813 
       
   814                 {
       
   815                     lookupItem.iFilePath.Zero();
       
   816                     lookupItem.iFetchingStatus = EMapTileFetchingInProgress;
       
   817                     TRAP_IGNORE( iMaptileDatabase->ReSetEntryL(lookupItem) );
       
   818                     
       
   819                     RequestMapTileImageL(*preferedAddressLm, ESourceContactsPref,
       
   820                             aEvent.iContactId, iEventType );
       
   821 
       
   822                     if ( lookupItem.iFilePath.Length() > 0 )
       
   823                     {
       
   824                         iMaptileDatabase->DeleteMapTileL(lookupItem);
       
   825                     }
       
   826                     
       
   827                 }
       
   828             }
       
   829             else
       
   830             {
       
   831             	TRAP_IGNORE( UpdateDatabaseL( NULL, aEvent.iContactId,
       
   832                      ESourceContactsPref, EEntryDeleted ));
       
   833                 TRAP_IGNORE( ManipulateMapTileDataBaseL(lookupItem));
       
   834                 
       
   835             }
       
   836             
       
   837             // if home address available, update Mylocations.
       
   838             lookupItem.iSource = ESourceContactsHome;
       
   839             if (homeAddressLm)
       
   840             {
       
   841                 iMyLocationsDatabaseManager->UpdateEntryName( aEvent.iContactId, ESourceContactsHome, 
       
   842                                      landmarkName );
       
   843                 MYLOCLOGSTRING("homeAddressLm address changed" );
       
   844                 if ( iMyLocationsDatabaseManager->CheckIfAddressChanged(*homeAddressLm,
       
   845                         aEvent.iContactId, ESourceContactsHome) )
       
   846                 {
       
   847                     lookupItem.iFilePath.Zero();
       
   848                     lookupItem.iFetchingStatus = EMapTileFetchingInProgress;
       
   849                     TRAP_IGNORE( iMaptileDatabase->ReSetEntryL(lookupItem) )
       
   850                     //remove entry from databse                    
       
   851                     RequestMapTileImageL(*homeAddressLm, ESourceContactsHome, aEvent.iContactId,
       
   852                         iEventType);
       
   853                     if (lookupItem.iFilePath.Length() > 0) 
       
   854                     {
       
   855                         iMaptileDatabase->DeleteMapTileL(lookupItem);
       
   856                     }                 
       
   857                 }
       
   858             }
       
   859             else
       
   860             {
       
   861                 TRAP_IGNORE( UpdateDatabaseL( NULL, aEvent.iContactId,
       
   862                      ESourceContactsHome, EEntryDeleted ));            
       
   863                 TRAP_IGNORE( ManipulateMapTileDataBaseL(lookupItem));
       
   864                 
       
   865             }
       
   866  
       
   867 
       
   868             // if work address available, update Mylocations.
       
   869             lookupItem.iSource = ESourceContactsWork;
       
   870             if (workAddressLm)
       
   871             {
       
   872             	  iMyLocationsDatabaseManager->UpdateEntryName( aEvent.iContactId, ESourceContactsWork, 
       
   873                                      landmarkName );
       
   874 
       
   875                 MYLOCLOGSTRING("workAddressLm address changed" );
       
   876                 if ( iMyLocationsDatabaseManager->CheckIfAddressChanged(*workAddressLm,
       
   877                         aEvent.iContactId, ESourceContactsWork) )
       
   878                 {
       
   879                     lookupItem.iFilePath.Zero();
       
   880                     lookupItem.iFetchingStatus = EMapTileFetchingInProgress;
       
   881                     TRAP_IGNORE( iMaptileDatabase->ReSetEntryL(lookupItem) )
       
   882 
       
   883                     RequestMapTileImageL(*workAddressLm, ESourceContactsWork,
       
   884                             aEvent.iContactId, iEventType);
       
   885                     if (lookupItem.iFilePath.Length() > 0) 
       
   886                     {
       
   887                         iMaptileDatabase->DeleteMapTileL(lookupItem);
       
   888                     }
       
   889                 } 
       
   890             }
       
   891             else
       
   892             {
       
   893                 TRAP_IGNORE( UpdateDatabaseL( NULL, aEvent.iContactId,
       
   894                      ESourceContactsWork, EEntryDeleted ));            
       
   895                 TRAP_IGNORE( ManipulateMapTileDataBaseL(lookupItem));
       
   896                
       
   897             }
       
   898             break;
       
   899         }    
       
   900         case EContactDbObserverEventContactAdded:
       
   901         {
       
   902             TLookupItem lookupItem;
       
   903             lookupItem.iUid = aEvent.iContactId;
       
   904             lookupItem.iFilePath.Zero();
       
   905             lookupItem.iFetchingStatus = EMapTileFetchingInProgress;
       
   906             
       
   907             MYLOCLOGSTRING("EContactDbObserverEventContactAdded" );
       
   908             if (preferedAddressLm)
       
   909             {
       
   910                 //create entry in the data base and maintain a fetching state.
       
   911                 lookupItem.iSource = ESourceContactsPref;
       
   912                 iMaptileDatabase->CreateEntryL(lookupItem);
       
   913                 RequestMapTileImageL(*preferedAddressLm, ESourceContactsPref,
       
   914                         aEvent.iContactId, iEventType);
       
   915             }
       
   916             if (homeAddressLm)
       
   917             {
       
   918                 lookupItem.iSource = ESourceContactsHome;
       
   919                 iMaptileDatabase->CreateEntryL(lookupItem);
       
   920                 RequestMapTileImageL(*homeAddressLm, ESourceContactsHome,
       
   921                         aEvent.iContactId, iEventType);
       
   922             }
       
   923             if (workAddressLm)
       
   924             {
       
   925                 lookupItem.iSource = ESourceContactsWork;
       
   926                 iMaptileDatabase->CreateEntryL(lookupItem);
       
   927                 RequestMapTileImageL(*workAddressLm, ESourceContactsWork,
       
   928                         aEvent.iContactId, iEventType);
       
   929             }
       
   930             break;
       
   931         }
       
   932 
       
   933     }
       
   934  
       
   935     CleanupStack::PopAndDestroy( itemCount );
       
   936 
       
   937 }
       
   938 
       
   939 
       
   940 // -----------------------------------------------------------------------------
       
   941 // CMyLocationsEngine::RequestMapTileImageL()
       
   942 // Request to get maptiel
       
   943 // -----------------------------------------------------------------------------
       
   944 //
       
   945 void CMyLocationsEngine::RequestMapTileImageL(const TDesC& aAddressDetails,
       
   946         const TUidSourceType aAddressType, const TInt32 aUId ,const TInt aEventType)
       
   947 {
       
   948     __TRACE_CALLSTACK;
       
   949     SetFolderPathL();
       
   950     TBuf<KImagePathSize> mImagePath;
       
   951     mImagePath.Copy(imageFilePath);
       
   952  
       
   953 
       
   954     CMapTileRequest* mapTileRequest = new (ELeave) CMapTileRequest;
       
   955 
       
   956     mapTileRequest->iAddressDetails = aAddressDetails.AllocL();
       
   957     mapTileRequest->iUId = aUId;
       
   958     mapTileRequest->iAddressType = aAddressType;
       
   959     mapTileRequest->iEventType = aEventType;
       
   960     mapTileRequest->iImagePath.Zero();
       
   961     mapTileRequest->iImagePath.Copy(mImagePath);
       
   962     
       
   963     TInt error = KErrNone;
       
   964     
       
   965     if (iMapTileRequestQueue.Count() <= 0)
       
   966     {
       
   967         error = iMapTileRequestQueue.Append(mapTileRequest);
       
   968         if ( KErrNone == error )
       
   969         {
       
   970             error = RequestExecute(mapTileRequest);    
       
   971         }
       
   972     }
       
   973     else
       
   974     {
       
   975         MYLOCLOGSTRING("Added one more request to request queue" );
       
   976         error = iMapTileRequestQueue.Append(mapTileRequest);
       
   977     }
       
   978     
       
   979     //If any error , free the allocated memory
       
   980     if( error != KErrNone )
       
   981     {
       
   982         delete mapTileRequest;
       
   983     }
       
   984     
       
   985 }
       
   986 // -----------------------------------------------------------------------------
       
   987 // CMyLocationsEngine::RequestMapTileImageL()
       
   988 // Request to get maptiel
       
   989 // -----------------------------------------------------------------------------
       
   990 //
       
   991 void CMyLocationsEngine::RequestMapTileImageL(CPosLandmark& aLandmark,
       
   992                     const TUidSourceType aAddressType, const TInt32 aUId,
       
   993                     const TInt aEventType )
       
   994 {
       
   995     __TRACE_CALLSTACK;
       
   996 
       
   997     MYLOCLOGSTRING("check folder path existance!");
       
   998     SetFolderPathL();
       
   999  
       
  1000 
       
  1001     CMapTileRequest* mapTileRequest = new (ELeave) CMapTileRequest;
       
  1002 
       
  1003     mapTileRequest->iLandmarkInfo = CPosLandmark::NewL(aLandmark);
       
  1004     mapTileRequest->iUId = aUId;
       
  1005     mapTileRequest->iAddressType = aAddressType;
       
  1006     mapTileRequest->iEventType = aEventType;
       
  1007     mapTileRequest->iImagePath.Zero();
       
  1008     mapTileRequest->iImagePath.Copy(imageFilePath);
       
  1009     MYLOCLOGSTRING1("RequestMapTileImageL() Queue count -%d",iMapTileRequestQueue.Count());
       
  1010 
       
  1011     TInt error = KErrNone;
       
  1012     
       
  1013     if (iMapTileRequestQueue.Count() <= 0)
       
  1014        {
       
  1015            error = iMapTileRequestQueue.Append(mapTileRequest);
       
  1016            //error = RequestExecute(mapTileRequest);
       
  1017            if( KErrNone == error  )
       
  1018            {
       
  1019                //error = iMapTileRequestQueue.Append(mapTileRequest);
       
  1020                error = RequestExecute(mapTileRequest);
       
  1021            }
       
  1022   
       
  1023        }
       
  1024        else
       
  1025        {
       
  1026            MYLOCLOGSTRING("Added one more request to request queue" );
       
  1027            error = iMapTileRequestQueue.Append(mapTileRequest);
       
  1028        }
       
  1029     
       
  1030        //If any error, free the memory allocated
       
  1031        if( error != KErrNone )
       
  1032        {
       
  1033            delete mapTileRequest;
       
  1034        }
       
  1035 }
       
  1036 
       
  1037 // -----------------------------------------------------------------------------
       
  1038 // CMyLocationsEngine::RequestExecute()
       
  1039 // Executes the maptile image processing request
       
  1040 // -----------------------------------------------------------------------------
       
  1041 //
       
  1042 TInt CMyLocationsEngine::RequestExecute( CMapTileRequest* aMapTileRequest)
       
  1043 {
       
  1044     __TRACE_CALLSTACK;
       
  1045     TInt errorCode = KErrNone;
       
  1046     MYLOCLOGSTRING1("Request address type - %d ",aMapTileRequest->iAddressType);
       
  1047     switch (aMapTileRequest->iAddressType)
       
  1048     {
       
  1049         case ESourceCalendar:
       
  1050         {
       
  1051             TRAP(errorCode,iMapTileInterface->GetGeoCodeFromAddressL(*(aMapTileRequest->iAddressDetails),
       
  1052                             aMapTileRequest->iImagePath, this ));        
       
  1053             break;
       
  1054         }
       
  1055         case ESourceContactsPref:
       
  1056         case ESourceContactsWork:
       
  1057         case ESourceContactsHome:
       
  1058         {
       
  1059             TRAP(errorCode, iMapTileInterface->GetGeoCodeFromAddressL(aMapTileRequest->iLandmarkInfo,
       
  1060                             aMapTileRequest->iImagePath, this));            
       
  1061             break;
       
  1062         }
       
  1063         default:
       
  1064             break;
       
  1065     };  
       
  1066 
       
  1067     return errorCode;
       
  1068 }
       
  1069  
       
  1070 // -----------------------------------------------------------------------------
       
  1071 // CMyLocationsEngine::GetContactAddressDetailsLC()
       
  1072 // get locatin details
       
  1073 // -----------------------------------------------------------------------------
       
  1074 //
       
  1075 CPosLandmark* CMyLocationsEngine::GetContactAddressDetailsLC( 
       
  1076                                    QContactAddress& aContactAddress )
       
  1077 {
       
  1078     __TRACE_CALLSTACK;
       
  1079     CPosLandmark *landmark = NULL;
       
  1080     
       
  1081     QString country = aContactAddress.country();
       
  1082     QString locality = aContactAddress.locality();
       
  1083     QString street = aContactAddress.street();
       
  1084     QString region = aContactAddress.region();
       
  1085     QString postalcode = aContactAddress.postcode();
       
  1086     
       
  1087     landmark = CPosLandmark::NewL();
       
  1088     CleanupStack::PushL(landmark);
       
  1089     
       
  1090     if ( !country.isEmpty() )
       
  1091     {
       
  1092         TPtrC16 tempText(reinterpret_cast<const TUint16*>(country.utf16()));
       
  1093         landmark->SetPositionFieldL( EPositionFieldCountry, tempText );
       
  1094     }
       
  1095     if ( !locality.isEmpty() )
       
  1096     {
       
  1097         TPtrC16 tempText(reinterpret_cast<const TUint16*>(locality.utf16())); 
       
  1098         landmark->SetPositionFieldL(EPositionFieldCity, tempText);
       
  1099     }
       
  1100     if ( !street.isEmpty() )
       
  1101     { 
       
  1102         TPtrC16 tempText(reinterpret_cast<const TUint16*>(street.utf16()));
       
  1103         landmark->SetPositionFieldL(EPositionFieldStreet, tempText);
       
  1104     }
       
  1105     if ( !postalcode.isEmpty() )
       
  1106     {
       
  1107         TPtrC16 tempText(reinterpret_cast<const TUint16*>(postalcode.utf16()));
       
  1108         landmark->SetPositionFieldL(EPositionFieldPostalCode, tempText);
       
  1109     }
       
  1110 
       
  1111      return landmark;
       
  1112 }
       
  1113 
       
  1114 // -----------------------------------------------------------------------------
       
  1115 // CMyLocationsEngine::MapsChangeType()
       
  1116 // Maps the source's change type to Mylocations entry change type
       
  1117 // -----------------------------------------------------------------------------
       
  1118 //
       
  1119 TEntryChangeType CMyLocationsEngine::MapChangeType(TUidSourceType aSrcType,
       
  1120         TUint32 aChangeType)
       
  1121 {
       
  1122     __TRACE_CALLSTACK;// return value
       
  1123     TEntryChangeType retVal = EEntryUnknown;
       
  1124 
       
  1125     switch (aSrcType)
       
  1126     {
       
  1127     // if source type is calendar
       
  1128     case ESourceCalendar:
       
  1129     {
       
  1130         switch( aChangeType )
       
  1131             {
       
  1132             case EChangeAdd:
       
  1133                 retVal = EEntryAdded;
       
  1134                 break;
       
  1135             case EChangeDelete:
       
  1136                 retVal =  EEntryDeleted;
       
  1137                 break;
       
  1138             case EChangeModify:
       
  1139                 retVal =  EEntryModified;
       
  1140                 break;
       
  1141             }
       
  1142         break;
       
  1143     }
       
  1144     // if source type is contacts
       
  1145     case ESourceContactsPref:
       
  1146     case ESourceContactsWork:
       
  1147     case ESourceContactsHome:
       
  1148     {
       
  1149         switch (aChangeType)
       
  1150         {
       
  1151         case EContactDbObserverEventContactAdded:
       
  1152             retVal = EEntryAdded;
       
  1153             break;
       
  1154         case EContactDbObserverEventContactDeleted:
       
  1155             retVal = EEntryDeleted;
       
  1156             break;
       
  1157         case EContactDbObserverEventContactChanged:
       
  1158             retVal = EEntryModified;
       
  1159             break;
       
  1160         }
       
  1161         break;
       
  1162     }
       
  1163 
       
  1164         // if source type is landmarks or maps history
       
  1165     case ESourceLandmarks:
       
  1166     {
       
  1167         switch (aChangeType)
       
  1168         {
       
  1169         case EPosLmEventLandmarkCreated:
       
  1170             retVal = EEntryAdded;
       
  1171             break;
       
  1172         case EPosLmEventLandmarkDeleted:
       
  1173             retVal = EEntryDeleted;
       
  1174             break;
       
  1175         case EPosLmEventLandmarkUpdated:
       
  1176             retVal = EEntryModified;
       
  1177             break;
       
  1178         }
       
  1179         break;
       
  1180     }
       
  1181     case ESourceLandmarksCategory:
       
  1182     {
       
  1183         switch (aChangeType)
       
  1184         {
       
  1185         case EPosLmEventCategoryCreated:
       
  1186             retVal = EEntryAdded;
       
  1187             break;
       
  1188         }
       
  1189     }
       
  1190         break;
       
  1191     }
       
  1192     return retVal;
       
  1193 }
       
  1194 
       
  1195 
       
  1196 // -----------------------------------------------------------------------------
       
  1197 // CMyLocationsEngine::IsGeoCoordinateAvailable()
       
  1198 // Checks whether geocoordinate available in the contact detail.
       
  1199 // -----------------------------------------------------------------------------
       
  1200 //
       
  1201 TBool CMyLocationsEngine::IsGeoCoordinateAvailable( QContact& aContact, 
       
  1202         QString aAddressType, double& aLatitude , double& aLongitude )
       
  1203 
       
  1204 {
       
  1205       TBool geoFieldAvailable = EFalse;
       
  1206       QContactGeoLocation geoLocation;
       
  1207       
       
  1208       foreach( geoLocation, aContact.details<QContactGeoLocation>() )
       
  1209       {
       
  1210           if( !geoLocation.isEmpty())
       
  1211           {
       
  1212               QStringList context = geoLocation.contexts();
       
  1213               if ( context.isEmpty() )
       
  1214               {
       
  1215                   if ( aAddressType.isEmpty() )
       
  1216                   {
       
  1217                       geoFieldAvailable = ETrue;
       
  1218                       break;
       
  1219                   }
       
  1220               }
       
  1221               else if( context.first() == aAddressType )
       
  1222               {
       
  1223                   geoFieldAvailable = ETrue;
       
  1224                   break;
       
  1225               }
       
  1226               
       
  1227           }
       
  1228           
       
  1229       }
       
  1230       if( geoFieldAvailable )
       
  1231       {
       
  1232           aLatitude = geoLocation.latitude();
       
  1233           aLongitude = geoLocation.longitude();
       
  1234       }
       
  1235       return geoFieldAvailable;
       
  1236 }
       
  1237 
       
  1238 
       
  1239 
       
  1240 // -----------------------------------------------------------------------------
       
  1241 // CMyLocationsEngine::RunL()
       
  1242 // Handles active object's request completion event.
       
  1243 // -----------------------------------------------------------------------------
       
  1244 //
       
  1245 void CMyLocationsEngine::RunL()
       
  1246 {
       
  1247     __TRACE_CALLSTACK;
       
  1248     switch (iLmEvent.iEventType)
       
  1249     {
       
  1250         case EPosLmEventLandmarkCreated:
       
  1251         case EPosLmEventLandmarkUpdated:
       
  1252         {
       
  1253             CPosLandmark* readLandmark = iLandmarkDb->ReadLandmarkLC(
       
  1254                     iLmEvent.iLandmarkItemId);
       
  1255     
       
  1256                 // update the entry in database.
       
  1257                 UpdateDatabaseL( readLandmark,
       
  1258                         iLmEvent.iLandmarkItemId, ESourceLandmarks, MapChangeType(
       
  1259                                 ESourceLandmarks, iLmEvent.iEventType ) );
       
  1260     
       
  1261                 CleanupStack::PopAndDestroy(readLandmark);
       
  1262         }
       
  1263         break;
       
  1264         case EPosLmEventLandmarkDeleted:
       
  1265         {
       
  1266             // delete the corresponding entries in mylocations database.
       
  1267             UpdateDatabaseL(NULL,
       
  1268                     iLmEvent.iLandmarkItemId, ESourceLandmarks, EEntryDeleted);
       
  1269         }
       
  1270         break;
       
  1271 
       
  1272     }
       
  1273 
       
  1274     // start the change notifier again;
       
  1275     StartLandmarksChangeNotifier();
       
  1276 
       
  1277 }
       
  1278 // -----------------------------------------------------------------------------
       
  1279 // CMyLocationsEngine::DoCancel()
       
  1280 // Implements cancellation of an outstanding request.
       
  1281 // -----------------------------------------------------------------------------
       
  1282 //
       
  1283 void CMyLocationsEngine::DoCancel()
       
  1284 {
       
  1285     __TRACE_CALLSTACK;
       
  1286     iLandmarkDb->CancelNotifyDatabaseEvent();
       
  1287 }
       
  1288 
       
  1289 // -----------------------------------------------------------------------------
       
  1290 // CMyLocationsEngine::GeoCodefetchingCompleted()
       
  1291 // Handles the maptile fetching completion event and updates the maptile lookup db.
       
  1292 // -----------------------------------------------------------------------------
       
  1293 //
       
  1294 void CMyLocationsEngine::GeoCodefetchingCompleted( TInt aErrCode, const TReal& aLatitude,
       
  1295             const TReal& aLongitude, const TDesC& aMapTilePath )
       
  1296 {
       
  1297     __TRACE_CALLSTACK;
       
  1298     MYLOCLOGSTRING1("GeoCodefetchingCompleted aErrCode - %d ",aErrCode);
       
  1299     MYLOCLOGSTRING1("iMapTileRequestQueue.Count - %d",iMapTileRequestQueue.Count());
       
  1300 
       
  1301     TBuf8<KProtocolBufferSize> buffer;
       
  1302     
       
  1303     if (iMapTileRequestQueue.Count() > 0)
       
  1304     {
       
  1305         MYLOCLOGSTRING1("No.of RequestQueue - %d",iMapTileRequestQueue.Count());
       
  1306        
       
  1307 	    TLookupItem lookupItem;
       
  1308         lookupItem.iSource = iMapTileRequestQueue[0]->iAddressType;
       
  1309         lookupItem.iUid = iMapTileRequestQueue[0]->iUId;
       
  1310 
       
  1311         if (aErrCode == KErrNone)
       
  1312         {
       
  1313             UpdateGeoCodeToAppDataBase( aLatitude, aLongitude );
       
  1314             
       
  1315             TBool flag = EFalse;
       
  1316             TRAP_IGNORE( flag = iMaptileDatabase->FindEntryByFilePathL(aMapTilePath) );
       
  1317             if ( flag )
       
  1318             {  
       
  1319                 MYLOCLOGSTRING1("%S - found in the DB",&aMapTilePath);
       
  1320             
       
  1321              				
       
  1322                 lookupItem.iFilePath.Copy(aMapTilePath);
       
  1323                 lookupItem.iFetchingStatus = EMapTileFectchingCompleted;
       
  1324                 TRAP_IGNORE( UpdateMaptileDatabaseL(iMapTileRequestQueue[0]->iEventType, lookupItem ) );              
       
  1325                 //Publish the maptile status
       
  1326                 if( iLastContactId == iMapTileRequestQueue[0]->iUId )
       
  1327                 {
       
  1328                     buffer.Zero();
       
  1329                     buffer.Format( KMaptileStatusFormat, iLastContactId, lookupItem.iSource, lookupItem.iFetchingStatus );
       
  1330                     RProperty::Set( KMaptileStatusPublish, EMaptileStatusInteger, buffer );
       
  1331                 }
       
  1332                 //Publish the maptile status ,if it was from calendar
       
  1333                 if( iLastCalendarId == iMapTileRequestQueue[0]->iUId )
       
  1334                 {
       
  1335                     buffer.Zero();
       
  1336                     buffer.Format( KMaptileStatusFormat, iLastCalendarId, lookupItem.iSource, lookupItem.iFetchingStatus );
       
  1337                     RProperty::Set( KMaptileStatusPublish, EMaptileStatusInteger, buffer );
       
  1338                 }
       
  1339                 MYLOCLOGSTRING("UpdateMaptileDatabaseL handled");
       
  1340 
       
  1341                 //Process the pending maptile requests
       
  1342                 ProcessNextMaptileRequest();
       
  1343                 
       
  1344             }
       
  1345             else
       
  1346             {
       
  1347                 TRAPD( error, iMapTileInterface->GetMapTileL( aLatitude, aLongitude ) );
       
  1348                 if ( error != KErrNone )
       
  1349                 {
       
  1350                     //Log error message
       
  1351                      MYLOCLOGSTRING1("iMapTileInterface->GetMapTileL() status-%d",error);
       
  1352                      MapTilefetchingCompleted(error, KNullDesC);
       
  1353                 }            
       
  1354             }            
       
  1355         }
       
  1356         else
       
  1357         {
       
  1358 		   if ( aErrCode == KErrCouldNotConnect )
       
  1359 		   {
       
  1360 		       lookupItem.iFetchingStatus = EMapTileFetchingNetworkError;
       
  1361                iMyLocationThreeAMTimer->StartTimer();
       
  1362               
       
  1363 		   }
       
  1364 		   else
       
  1365 		   {
       
  1366 		       lookupItem.iFetchingStatus = EMapTileFetchingUnknownError;
       
  1367 		   }
       
  1368            TRAP_IGNORE( UpdateMaptileDatabaseL(iMapTileRequestQueue[0]->iEventType,lookupItem ) );
       
  1369            
       
  1370            //Publish the maptile status
       
  1371 		   if( iLastContactId == iMapTileRequestQueue[0]->iUId )
       
  1372 		   {
       
  1373 		      buffer.Zero();
       
  1374 		      buffer.Format( KMaptileStatusFormat, iLastContactId, lookupItem.iSource, lookupItem.iFetchingStatus );
       
  1375 		      RProperty::Set( KMaptileStatusPublish, EMaptileStatusInteger, buffer );
       
  1376 		   }
       
  1377             //Publish the maptile status ,if it was from calendar
       
  1378 		   if( iLastCalendarId == iMapTileRequestQueue[0]->iUId )
       
  1379             {
       
  1380 		      buffer.Zero();
       
  1381               buffer.Format( KMaptileStatusFormat, iLastCalendarId, lookupItem.iSource, lookupItem.iFetchingStatus );
       
  1382               RProperty::Set( KMaptileStatusPublish, EMaptileStatusInteger, buffer );
       
  1383             }
       
  1384 		   
       
  1385 		   ProcessNextMaptileRequest();
       
  1386         }
       
  1387     }    
       
  1388 }
       
  1389 
       
  1390 
       
  1391 
       
  1392 
       
  1393 // -----------------------------------------------------------------------------
       
  1394 // CMyLocationsEngine::MyLocationThreeAMTimerExpiredL()
       
  1395 // Triggers the maptile fetching at 3.00 for the network failure cases.
       
  1396 // -----------------------------------------------------------------------------
       
  1397 //
       
  1398 void CMyLocationsEngine::MyLocationThreeAMTimerExpiredL()
       
  1399 {
       
  1400     //Forward the event for maptile fetching only if maptile plugin available
       
  1401     if( iMaptileGeocoderPluginAvailable )
       
  1402     {
       
  1403         RArray<TLookupItem> iLookupItems;
       
  1404         iMaptileDatabase->FindEntriesByMapTileFetchingStateL((TUint32)EMapTileFetchingNetworkError,
       
  1405                                             iLookupItems);
       
  1406         for( TUint32 i = 0; i < iLookupItems.Count(); i++ )
       
  1407         {
       
  1408             TLookupItem iItem = iLookupItems[i];
       
  1409             switch( iItem.iSource )
       
  1410             {
       
  1411                 // Get the home address details
       
  1412                 case ESourceContactsHome:
       
  1413                 case ESourceContactsWork:
       
  1414                 case ESourceContactsPref:
       
  1415                      { 
       
  1416                         QContact contactInfo = iContactManager->contact( iItem.iUid );
       
  1417                         
       
  1418                         CPosLandmark *addressLm = NULL;
       
  1419     
       
  1420                         foreach ( QContactAddress address, contactInfo.details<QContactAddress>() )
       
  1421                         {
       
  1422                             QStringList context = address.contexts();
       
  1423                             if ( ( context.isEmpty() && iItem.iSource == ESourceContactsPref  ) 
       
  1424                                  ||
       
  1425                                  ( !context.isEmpty() && 
       
  1426                                     ( ( context.first() == QContactAddress::ContextHome  && iItem.iSource == ESourceContactsHome ) ||
       
  1427                                       ( context.first() == QContactAddress::ContextWork  && iItem.iSource == ESourceContactsWork ) ) ) )
       
  1428                             {
       
  1429                                 // Get the default/prefered address details
       
  1430                                 addressLm = GetContactAddressDetailsLC( address );
       
  1431                                 if( addressLm ) 
       
  1432                                 {
       
  1433                                     RequestMapTileImageL( *addressLm,
       
  1434                                            ( TUidSourceType )iItem.iSource, iItem.iUid, EContactDbObserverEventContactChanged );
       
  1435                                     CleanupStack::PopAndDestroy( addressLm );
       
  1436                                     break;
       
  1437                                 }
       
  1438                             }
       
  1439                         }                    
       
  1440                      }
       
  1441                      break;
       
  1442                 
       
  1443                 case ESourceCalendar:
       
  1444                      {
       
  1445                         CCalEntry* calEntry = NULL;
       
  1446                         calEntry = iCalView->FetchL(iItem.iUid);
       
  1447                         if( calEntry )
       
  1448                         {
       
  1449                             CleanupStack::PushL(calEntry);
       
  1450                             TPtrC address(calEntry->LocationL());
       
  1451                             if(address.Length()>0)
       
  1452                             {        
       
  1453                                 RequestMapTileImageL( address, ESourceCalendar, iItem.iUid , EChangeModify);
       
  1454                             }
       
  1455                             CleanupStack::PopAndDestroy(calEntry);
       
  1456                         }
       
  1457                         else
       
  1458                         {
       
  1459                             iMaptileDatabase->DeleteEntryL( iItem );
       
  1460                         }
       
  1461                      }
       
  1462                      break;
       
  1463                  
       
  1464                  default:
       
  1465                      break;
       
  1466               }
       
  1467          }// end for
       
  1468      }
       
  1469 }
       
  1470 
       
  1471 
       
  1472 // -----------------------------------------------------------------------------
       
  1473 // CMyLocationsEngine::MapTilefetchingCompleted()
       
  1474 // Handles the maptile fetching completion event and updates the maptile lookup db.
       
  1475 // -----------------------------------------------------------------------------
       
  1476 //
       
  1477 void CMyLocationsEngine::MapTilefetchingCompleted(TInt aErrCode,
       
  1478         const TDesC& aMapTilePath)
       
  1479 {
       
  1480     __TRACE_CALLSTACK;
       
  1481     MYLOCLOGSTRING1("MapTilefetchingCompleted aErrCode - %d ",aErrCode);
       
  1482     MYLOCLOGSTRING1("iMapTileRequestQueue.Count - %d",iMapTileRequestQueue.Count());
       
  1483 
       
  1484     if ( iMapTileRequestQueue.Count() > 0 )
       
  1485     {
       
  1486 
       
  1487         MYLOCLOGSTRING1("No.of RequestQueue - %d",iMapTileRequestQueue.Count());
       
  1488 
       
  1489         TLookupItem lookupItem;
       
  1490         lookupItem.iSource = iMapTileRequestQueue[0]->iAddressType;
       
  1491         lookupItem.iUid = iMapTileRequestQueue[0]->iUId;
       
  1492        
       
  1493         if ( aErrCode == KErrNone )
       
  1494         {           
       
  1495             CreateMultipleMaptiles( aMapTilePath );
       
  1496             lookupItem.iFilePath.Copy(aMapTilePath);
       
  1497             lookupItem.iFetchingStatus = EMapTileFectchingCompleted;
       
  1498  
       
  1499         }
       
  1500         else if ( aErrCode == KErrCouldNotConnect )
       
  1501 		{
       
  1502 		    lookupItem.iFetchingStatus = EMapTileFetchingNetworkError;
       
  1503             iMyLocationThreeAMTimer->StartTimer();
       
  1504               
       
  1505 		}
       
  1506 		else
       
  1507 		{
       
  1508 		    lookupItem.iFetchingStatus = EMapTileFetchingUnknownError;
       
  1509 		}
       
  1510 		
       
  1511         TRAP_IGNORE( UpdateMaptileDatabaseL( iMapTileRequestQueue[0]->iEventType,lookupItem ) );
       
  1512 
       
  1513         // if the source type is contacts, update the contact name into the locationdatalookupdb. 
       
  1514         // This has to be done, because there is a possibility  that the user might change the 
       
  1515         // contact name between geocodefetchingcomplete and maptilefetchingcomplete.
       
  1516         if( iMapTileRequestQueue[0]->iAddressType == ESourceContactsPref ||
       
  1517             iMapTileRequestQueue[0]->iAddressType == ESourceContactsHome ||
       
  1518             iMapTileRequestQueue[0]->iAddressType == ESourceContactsWork )
       
  1519         {
       
  1520             TBuf<KBufSize> landmarkName;
       
  1521             GetContactName( iMapTileRequestQueue[0]->iUId, landmarkName );
       
  1522             
       
  1523             iMyLocationsDatabaseManager->UpdateEntryName( 
       
  1524                     iMapTileRequestQueue[0]->iUId, ESourceContactsPref, landmarkName );
       
  1525             iMyLocationsDatabaseManager->UpdateEntryName( 
       
  1526                     iMapTileRequestQueue[0]->iUId, ESourceContactsHome, landmarkName );
       
  1527             iMyLocationsDatabaseManager->UpdateEntryName( 
       
  1528                     iMapTileRequestQueue[0]->iUId, ESourceContactsWork, landmarkName );
       
  1529         }
       
  1530 
       
  1531         //Publish the maptile status , if it was from contact
       
  1532         if( iLastContactId == iMapTileRequestQueue[0]->iUId )
       
  1533         {
       
  1534              TBuf8<KProtocolBufferSize> buffer;
       
  1535              buffer.Format( KMaptileStatusFormat, iLastContactId, lookupItem.iSource, lookupItem.iFetchingStatus );
       
  1536              RProperty::Set( KMaptileStatusPublish, EMaptileStatusInteger, buffer );
       
  1537         }
       
  1538         //Publish the maptile status ,if it was from calendar
       
  1539         if( iLastCalendarId == iMapTileRequestQueue[0]->iUId )
       
  1540         {
       
  1541             TBuf8<KProtocolBufferSize> buffer;
       
  1542             buffer.Format( KMaptileStatusFormat, iLastCalendarId, lookupItem.iSource, lookupItem.iFetchingStatus );
       
  1543             RProperty::Set( KMaptileStatusPublish, EMaptileStatusInteger, buffer );
       
  1544         }
       
  1545     }
       
  1546     
       
  1547     ProcessNextMaptileRequest();
       
  1548 }
       
  1549 
       
  1550 
       
  1551 // -----------------------------------------------------------------------------
       
  1552 // CMyLocationsEngine::ProcessNextMaptileRequest()
       
  1553 // Process the next maptile request if any pending.
       
  1554 // -----------------------------------------------------------------------------
       
  1555 //
       
  1556 void CMyLocationsEngine::ProcessNextMaptileRequest()
       
  1557 {
       
  1558     //Process the next request if it is pending
       
  1559     if ( iMapTileRequestQueue.Count() > 0)
       
  1560     {
       
  1561         //Remove the current top request 
       
  1562         delete iMapTileRequestQueue[0];
       
  1563         iMapTileRequestQueue.Remove(0);
       
  1564         iMapTileRequestQueue.Compress();
       
  1565     
       
  1566         //Process the next request in queue
       
  1567         MYLOCLOGSTRING1("MapTile fetch completed request-%d",iMapTileRequestQueue.Count());
       
  1568         for (TInt cnt = 0; cnt < iMapTileRequestQueue.Count(); cnt++)
       
  1569         {          
       
  1570             if ( KErrNone == RequestExecute( iMapTileRequestQueue[0]) )
       
  1571             {
       
  1572                    break;
       
  1573             }
       
  1574             else
       
  1575             {
       
  1576                //Process the next request in queue
       
  1577                ProcessNextMaptileRequest();
       
  1578 
       
  1579             }
       
  1580         }
       
  1581     }
       
  1582     else
       
  1583     {
       
  1584        MYLOCLOGSTRING("MapTile fetch completed no request in queue");
       
  1585            iMapTileRequestQueue.Reset();
       
  1586     }    
       
  1587 }
       
  1588 
       
  1589 // -----------------------------------------------------------------------------
       
  1590 // CMyLocationsEngine::HandleMaptileDatabaseL()
       
  1591 // Handle maptile database(find/create/update/delete).
       
  1592 // -----------------------------------------------------------------------------
       
  1593 //
       
  1594 void CMyLocationsEngine::UpdateMaptileDatabaseL(
       
  1595         TInt aEventType, TLookupItem& aLookupItem)
       
  1596 {
       
  1597     __TRACE_CALLSTACK;
       
  1598     if (aEventType == EContactDbObserverEventContactChanged || aEventType
       
  1599             == EChangeModify || aEventType == EContactDbObserverEventContactAdded ||
       
  1600             aEventType == EChangeAdd )
       
  1601     {
       
  1602         
       
  1603         if (iMaptileDatabase->FindEntryL(aLookupItem))
       
  1604         {
       
  1605             iMaptileDatabase->UpdateEntryL(aLookupItem);
       
  1606         }
       
  1607         else
       
  1608         {
       
  1609             iMaptileDatabase->CreateEntryL(aLookupItem);
       
  1610         }
       
  1611     }
       
  1612     if (aLookupItem.iFetchingStatus == EMapTileFetchingUnknownError
       
  1613             || aLookupItem.iFetchingStatus == EMapTileFetchingNetworkError)
       
  1614     {
       
  1615         TRAP_IGNORE( UpdateDatabaseL( NULL,
       
  1616                         aLookupItem.iUid, aLookupItem.iSource, EEntryDeleted ) );
       
  1617 
       
  1618     }
       
  1619     else
       
  1620     {      
       
  1621         TPtrC8 ptr(reinterpret_cast<const TUint8*>(MAPTILE_IMAGE_HURRIGANES));   
       
  1622         HBufC* buffer = NULL;
       
  1623         buffer=HBufC::NewLC(ptr.Length());
       
  1624         buffer->Des().Copy(ptr);
       
  1625         if(buffer)
       
  1626         {
       
  1627             aLookupItem.iFilePath.Append(*buffer);
       
  1628         }
       
  1629         CleanupStack::PopAndDestroy(buffer);
       
  1630         iMyLocationsDatabaseManager->UpdateMapTilePath(aLookupItem.iUid,
       
  1631                 aLookupItem.iSource, aLookupItem.iFilePath);
       
  1632     }
       
  1633 }
       
  1634 
       
  1635 // -----------------------------------------------------------------------------
       
  1636 // CMyLocationsEngine::RestGeoCodeCompleted()
       
  1637 // observed when rest geo codeing completed.
       
  1638 // started lat and lon field updation into contact db.
       
  1639 // -----------------------------------------------------------------------------
       
  1640 //
       
  1641 void CMyLocationsEngine::UpdateGeoCodeToAppDataBase( TReal aLatitude, TReal aLongitude )
       
  1642 {
       
  1643     __TRACE_CALLSTACK;
       
  1644 
       
  1645     MYLOCLOGSTRING1("No. of iMapTileRequestQueue - %d",iMapTileRequestQueue.Count());
       
  1646     if (iMapTileRequestQueue.Count() > 0)
       
  1647     {
       
  1648         switch (iMapTileRequestQueue[0]->iAddressType)
       
  1649         {
       
  1650         //TODO:
       
  1651         case ESourceCalendar:
       
  1652         {           
       
  1653             iGeocodeUpdate->updateGeocodeToCalenderDB(iMapTileRequestQueue[0]->iUId,
       
  1654                     aLatitude,aLongitude);                    
       
  1655             CPosLandmark *landmark = NULL;
       
  1656             landmark = iMapTileInterface->GetLandMarkDetails();
       
  1657             if (NULL != landmark)
       
  1658             {
       
  1659                 TRAP_IGNORE( landmark->SetPositionFieldL(EPositionFieldComment,
       
  1660                         iMapTileRequestQueue[0]->iAddressDetails->Des() ) );
       
  1661                 TRAP_IGNORE( UpdateDatabaseL( landmark, iMapTileRequestQueue[0]->iUId,
       
  1662                                 ESourceCalendar,
       
  1663                                 MapChangeType( ESourceCalendar, iMapTileRequestQueue[0]->iEventType ) ) );
       
  1664             }
       
  1665             MYLOCLOGSTRING("Geo-codinate updated to calender db");
       
  1666             break;
       
  1667         }
       
  1668         case ESourceContactsPref:
       
  1669         case ESourceContactsWork:
       
  1670         case ESourceContactsHome:
       
  1671         {
       
  1672             iGeocodeUpdate->updateGeocodeToContactDB(
       
  1673                     iMapTileRequestQueue[0]->iUId,
       
  1674                     iMapTileRequestQueue[0]->iAddressType, aLatitude,
       
  1675                     aLongitude);
       
  1676             TBuf<KBufSize> landmarkName;
       
  1677             GetContactName(iMapTileRequestQueue[0]->iUId,landmarkName);
       
  1678             //Update mylocation database 
       
  1679             iMapTileRequestQueue[0]->iLandmarkInfo->SetLandmarkNameL( landmarkName );
       
  1680             TRAP_IGNORE( UpdateDatabaseL( 
       
  1681                     iMapTileRequestQueue[0]->iLandmarkInfo, 
       
  1682                     iMapTileRequestQueue[0]->iUId,
       
  1683                     iMapTileRequestQueue[0]->iAddressType,
       
  1684                     MapChangeType( 
       
  1685                         static_cast<TUidSourceType>( iMapTileRequestQueue[0]->iAddressType ), 
       
  1686                                                   iMapTileRequestQueue[0]->iEventType )));
       
  1687             
       
  1688             MYLOCLOGSTRING("Geo-codinate updated to contact db");
       
  1689             break;
       
  1690         }
       
  1691         };
       
  1692     }
       
  1693 
       
  1694 }
       
  1695 
       
  1696 
       
  1697 // -----------------------------------------------------------------------------
       
  1698 // CMyLocationsEngine::GetContactName()
       
  1699 // -----------------------------------------------------------------------------
       
  1700 //
       
  1701 void CMyLocationsEngine::GetContactName( TInt32 aUId,TDes& aName)
       
  1702 {
       
  1703 	QContact contactInfo = iContactManager->contact( aUId );
       
  1704 	QContactName name = contactInfo.detail( QContactName::DefinitionName );
       
  1705 	QString firstName = name.firstName();
       
  1706 	QString lastName = name.lastName();
       
  1707 	QString fullName("");
       
  1708 	
       
  1709 	if( lastName.isEmpty() )
       
  1710     {
       
  1711 	    fullName = firstName;
       
  1712     }
       
  1713 	else
       
  1714     {
       
  1715         if( !firstName.isEmpty() )
       
  1716         {
       
  1717             fullName = firstName + KSpace;
       
  1718         }
       
  1719         fullName = fullName + lastName; 
       
  1720     }
       
  1721 	
       
  1722 	aName.Copy( reinterpret_cast<const TUint16*>(fullName.utf16()) );
       
  1723 }
       
  1724 
       
  1725 // -----------------------------------------------------------------------------
       
  1726 // CMyLocationsEngine::UpdateDatabaseL()
       
  1727 // -----------------------------------------------------------------------------
       
  1728 //
       
  1729 void CMyLocationsEngine::UpdateDatabaseL( CPosLandmark* aLandmark, const TUint32 aUid, 
       
  1730         const TUint32 aSourceType, const TEntryChangeType aChangeType )
       
  1731 {
       
  1732     __TRACE_CALLSTACK;
       
  1733     if(IsActive())
       
  1734     {
       
  1735         Cancel();
       
  1736     }
       
  1737     iMyLocationsDatabaseManager->UpdateDatabaseL( aLandmark, aUid, 
       
  1738         aSourceType, aChangeType );
       
  1739     if( aSourceType != ESourceLandmarks )
       
  1740     {
       
  1741         StartLandmarksChangeNotifier();
       
  1742     }
       
  1743     
       
  1744 }
       
  1745 
       
  1746 // -----------------------------------------------------------------------------
       
  1747 // CMyLocationsEngine::ManipulateMapTileDataBaseL()
       
  1748 // -----------------------------------------------------------------------------
       
  1749 //
       
  1750 void CMyLocationsEngine::ManipulateMapTileDataBaseL(TLookupItem& aLookupItem)
       
  1751 {
       
  1752     __TRACE_CALLSTACK;
       
  1753     TBool entryAvailable=EFalse;
       
  1754     entryAvailable = iMaptileDatabase->FindEntryL(aLookupItem);
       
  1755     iMaptileDatabase->DeleteEntryL(aLookupItem);
       
  1756     if (entryAvailable)
       
  1757     {
       
  1758         iMaptileDatabase->DeleteMapTileL(aLookupItem);
       
  1759     }            
       
  1760 }
       
  1761 
       
  1762 
       
  1763 // -----------------------------------------------------------------------------
       
  1764 // CMyLocationsEngine::CreateMultipleMaptiles()
       
  1765 // -----------------------------------------------------------------------------
       
  1766 //
       
  1767 void CMyLocationsEngine::CreateMultipleMaptiles( const TDesC& aMaptilePath )
       
  1768 {
       
  1769     __TRACE_CALLSTACK;
       
  1770     QString filePath =  QString::fromUtf16( aMaptilePath.Ptr(), aMaptilePath.Length() );
       
  1771     
       
  1772     //Portrait image , common for contacts/calendar/context menu
       
  1773     CropAndSaveImage( filePath, 
       
  1774                       MaptilePortraitWidth, 
       
  1775                       MaptilePortraitHeight, 
       
  1776                       QString(),
       
  1777                       QString( MAPTILE_IMAGE_PORTRAIT ) );
       
  1778     
       
  1779     //Landscape image for contacts
       
  1780     CropAndSaveImage( filePath, 
       
  1781                       MaptileContactLandscapeWidth, 
       
  1782                       MaptileContactLandscapeHeight,
       
  1783                       QString( MAPTILE_IMAGE_CONTACT ),
       
  1784                       QString( MAPTILE_IMAGE_LANDSCAPE ) );
       
  1785     
       
  1786     //Ladscape image for calendar
       
  1787     CropAndSaveImage( filePath, 
       
  1788                       MaptileCalendarLandscapeWidth, 
       
  1789                       MaptileCalendarLandscapeHeight,
       
  1790                       QString( MAPTILE_IMAGE_CALENDAR ),
       
  1791                       QString( MAPTILE_IMAGE_LANDSCAPE ) );
       
  1792     
       
  1793     //Image for hurriganes
       
  1794     CropAndSaveImage( filePath, 
       
  1795                       MaptileHurriganesWidth, 
       
  1796                       MaptileHurriganesHeight,
       
  1797                       QString( MAPTILE_IMAGE_HURRIGANES ),
       
  1798                       QString() );
       
  1799     QFile file(filePath);
       
  1800     file.remove();   
       
  1801 }
       
  1802 
       
  1803 
       
  1804 // -----------------------------------------------------------------------------
       
  1805 // CMyLocationsEngine::CropAndSaveImage()
       
  1806 // -----------------------------------------------------------------------------
       
  1807 //
       
  1808 void CMyLocationsEngine::CropAndSaveImage( QString filePath, int width, 
       
  1809                                    int height, QString appType, QString orientationType )
       
  1810 {    
       
  1811      __TRACE_CALLSTACK;
       
  1812      QImage SourcePNG( filePath );
       
  1813      QImage sourcePixmap( SourcePNG.convertToFormat(QImage::Format_Indexed8));     
       
  1814      QImage targetPixmap( sourcePixmap.copy( 
       
  1815                                ( MapTileWidth - width )/2, 
       
  1816                                ( MapTileHeight - height )/2 , 
       
  1817                                width, 
       
  1818                                height ) );
       
  1819          
       
  1820      QString targetImage;
       
  1821      targetImage.append( filePath );
       
  1822      targetImage.append( appType );
       
  1823      targetImage.append( orientationType );
       
  1824      targetPixmap.save( targetImage, MAPTILE_IMAGE_TYPE  );
       
  1825              
       
  1826 }
       
  1827 
       
  1828 //End of file
       
  1829