meetingrequest/mrgui/src/cesmrlocationpluginhandler.cpp
branchRCL_3
changeset 25 3533d4323edc
equal deleted inserted replaced
24:d189ee25cf9d 25:3533d4323edc
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  ESMR Location plugin handler implementation
       
    15 *
       
    16 */
       
    17 #include "emailtrace.h"
       
    18 #include "cesmrlocationpluginhandler.h"
       
    19 #include "esmrconfig.hrh"
       
    20 
       
    21 #include <calentry.h>
       
    22 #include <StringLoader.h>
       
    23 #include <AknQueryDialog.h>
       
    24 #include <esmrgui.rsg>
       
    25 //<cmail>
       
    26 #include "cesmrurlparserplugin.h"
       
    27 //</cmail>
       
    28 #include <CLmkLandmarkSelectorDlg.h>
       
    29 #include <TLmkItemIdDbCombiInfo.h>
       
    30 #include <EPos_CPosLandmark.h>
       
    31 #include <AknUtils.h>
       
    32 #include <ct/rcpointerarray.h>
       
    33 
       
    34 #ifdef RD_USE_MYLOCATIONUI
       
    35 #include <mylocationui.h>
       
    36 #endif //RD_USE_MYLOCATIONUI
       
    37 
       
    38 #include "cesmrlocationplugin.h"
       
    39 #include "esmrcommands.h"
       
    40 #include "mesmrcalentry.h"
       
    41 
       
    42 #include "cesmrdynamicitemselectionlist.h"
       
    43 #include "cesmrlocationhistorymanager.h"
       
    44 #include "mesmrlocationhistoryitem.h"
       
    45 #include "cesmrconfirmationquery.h"
       
    46 
       
    47 // Unnamed namespace for local definitions
       
    48 namespace { //codescanner::namespace
       
    49 
       
    50 _LIT(KSpace, " ");
       
    51 
       
    52 // Maximum length of the text in location field
       
    53 const TInt KMaxLocationTextLength = 255;
       
    54 
       
    55 }
       
    56 
       
    57 // ============================ MEMBER FUNCTIONS ===============================
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CESMRLocationPluginHandler::CESMRLocationPluginHandler
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 CESMRLocationPluginHandler::CESMRLocationPluginHandler( MObjectProvider& aParent )
       
    64 : iParent(&aParent)
       
    65     {
       
    66     FUNC_LOG;
       
    67 
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CESMRLocationPluginHandler::~CESMRLocationPluginHandler
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 CESMRLocationPluginHandler::~CESMRLocationPluginHandler()
       
    75     {
       
    76     FUNC_LOG;
       
    77     delete iWait;
       
    78     delete iLocationPlugin;
       
    79     delete iLandmark;
       
    80     delete iUrlParser;
       
    81     delete iLocationHistoryManager;
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CESMRLocationPluginHandler::NewL
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 CESMRLocationPluginHandler* CESMRLocationPluginHandler::NewL(
       
    89         MObjectProvider& aParent )
       
    90     {
       
    91     FUNC_LOG;
       
    92     CESMRLocationPluginHandler* self =
       
    93         new (ELeave) CESMRLocationPluginHandler( aParent );
       
    94     CleanupStack::PushL( self );
       
    95     self->ConstructL();
       
    96     CleanupStack::Pop( self );
       
    97     return self;
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CESMRLocationPluginHandler::ConstructL
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 void CESMRLocationPluginHandler::ConstructL()
       
   105     {
       
   106     FUNC_LOG;
       
   107     iWait = new( ELeave ) CActiveSchedulerWait;
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CESMRLocationPluginHandler::HandleCommandL
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 TBool CESMRLocationPluginHandler::HandleCommandL( TInt aCommand,
       
   115                                                  MESMRCalEntry& aEntry,
       
   116                                                  TBool aIgnoreQuery )
       
   117     {
       
   118     FUNC_LOG;
       
   119     iIgnoreQuery = aIgnoreQuery;
       
   120     iEntryUpdated = EFalse;
       
   121 
       
   122     switch ( aCommand )
       
   123         {
       
   124         case EESMRCmdAssignFromMap:
       
   125             {
       
   126             if( !(iWait->IsStarted()) )
       
   127                 {
       
   128                 CCalEntry& entry = aEntry.Entry();
       
   129                 const TDesC& location = entry.LocationL();
       
   130 
       
   131                 CCalGeoValue* geoVal = entry.GeoValueL();
       
   132                 
       
   133                 if ( geoVal )
       
   134                     {
       
   135                     // Launch Maps
       
   136                     LocationPluginL().SelectFromMapL( location, geoVal );
       
   137                     }
       
   138                 else
       
   139                     {
       
   140                     // Extract location URL from description
       
   141                     TPtrC locationUrl;
       
   142                     UrlParserL().FindLocationUrl( entry.DescriptionL(),
       
   143                                                   locationUrl );
       
   144     
       
   145                     // Launch Maps
       
   146                     LocationPluginL().SelectFromMapL( location, locationUrl );
       
   147                     }
       
   148                 iWait->Start(); // codescanner::callActiveObjectWithoutCheckingOrStopping
       
   149 
       
   150                 // iLandmark is set in async callback SelectFromMapCompleted
       
   151                 // from Maps application.
       
   152                 if ( iLandmark )
       
   153                     {
       
   154                     UpdateEntryFromLandmarkL( aEntry, *iLandmark );
       
   155                     delete iLandmark;
       
   156                     iLandmark = NULL;
       
   157                     }
       
   158                 }
       
   159             }
       
   160             break;
       
   161         case EESMRCmdUpdateFromMap:
       
   162             {
       
   163             if ( !(iWait->IsStarted()) )
       
   164                 {
       
   165                 // Launch Maps and ignore existing coordinates,
       
   166                 // because location has been updated
       
   167                 LocationPluginL().SelectFromMapL( aEntry.Entry().LocationL(),
       
   168                                                   KNullDesC );
       
   169                 iWait->Start(); // codescanner::callActiveObjectWithoutCheckingOrStopping
       
   170 
       
   171                 // iLandmark is set in async callback SelectFromMapCompleted
       
   172                 // from Maps application.
       
   173                 if ( iLandmark )
       
   174                     {
       
   175                     UpdateEntryFromLandmarkL( aEntry, *iLandmark );
       
   176                     delete iLandmark;
       
   177                     iLandmark = NULL;
       
   178                     }
       
   179                 }
       
   180             break;
       
   181             }
       
   182         case EESMRCmdSearchFromMap:
       
   183             {
       
   184             const TDesC& location = aEntry.Entry().LocationL();
       
   185             LocationPluginL().SearchFromMapL( location );
       
   186             }
       
   187             break;
       
   188         case EESMRCmdShowOnMap:
       
   189             {
       
   190             CCalGeoValue* geoVal = aEntry.Entry().GeoValueL();
       
   191             TReal lat, lon;
       
   192             if ( geoVal && geoVal->GetLatLong( lat, lon ) )
       
   193                 {
       
   194                 LocationPluginL().ShowOnMapL( *geoVal );
       
   195                 }
       
   196             else
       
   197                 {
       
   198                 const TDesC& description = aEntry.Entry().DescriptionL();
       
   199                 TPtrC locationUrl;
       
   200                 TInt position = UrlParserL().FindLocationUrl( description,
       
   201                                                               locationUrl );
       
   202                 if ( position >= 0 )
       
   203                     {
       
   204                     LocationPluginL().ShowOnMapL( locationUrl );
       
   205                     }
       
   206                 }
       
   207             break;
       
   208             }
       
   209         case EESMRCmdLandmarks:
       
   210             {
       
   211             SearchFromLandmarksL( iParent, aEntry );
       
   212             break;
       
   213             }
       
   214         case EESMRCmdPreviousLocations:
       
   215             {
       
   216             ShowSelectPreviousLocationQueryL( aEntry );
       
   217             break;
       
   218             }
       
   219         case EMRCommandMyLocations:
       
   220             {
       
   221 #ifdef RD_USE_MYLOCATIONUI
       
   222             CMyLocationSelectorUi* ui = CMyLocationSelectorUi::NewL();
       
   223             CleanupStack::PushL( ui );
       
   224             
       
   225             CPosLandmark* landmark = ui->LaunchMyLocationSelectorUiL();
       
   226             
       
   227             if ( landmark )
       
   228                 {
       
   229                 CleanupStack::PushL( landmark );
       
   230                 UpdateEntryFromLandmarkL( aEntry, *landmark );
       
   231                 CleanupStack::PopAndDestroy( landmark );
       
   232                 }
       
   233             
       
   234             CleanupStack::PopAndDestroy( ui );
       
   235 #else // RD_USE_MYLOCATIONUI
       
   236             ASSERT( EFalse );
       
   237 #endif //RD_USE_MYLOCATIONUI
       
   238             break;
       
   239             }
       
   240             
       
   241         default:
       
   242             User::Leave( KErrGeneral );
       
   243         }
       
   244 
       
   245     return iEntryUpdated;
       
   246     }
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 // CESMRLocationPluginHandler::IsCommandAvailableL
       
   250 // -----------------------------------------------------------------------------
       
   251 //
       
   252 TBool
       
   253 CESMRLocationPluginHandler::IsCommandAvailableL( TInt aCommand,
       
   254                                                  const MESMRCalEntry& aEntry )
       
   255     {
       
   256     FUNC_LOG;
       
   257     TBool res( EFalse );
       
   258 
       
   259     switch ( aCommand )
       
   260         {
       
   261         case EESMRCmdAssignFromMap: //fallthrough
       
   262         case EESMRCmdUpdateFromMap:
       
   263         case EESMRCmdSearchFromMap:
       
   264         case EESMRCmdLandmarks:
       
   265             {
       
   266             res = ETrue;
       
   267             break;
       
   268             }
       
   269         case EESMRCmdPreviousLocations:
       
   270             {
       
   271             if ( LocationHistoryManagerL().ItemCount() > 0 )
       
   272                 {
       
   273                 res = ETrue;
       
   274                 }
       
   275             break;
       
   276             }
       
   277         case EESMRCmdShowOnMap:
       
   278             {
       
   279             // Try to fetch vCal GEO value from entry
       
   280             CCalGeoValue* geoVal = aEntry.Entry().GeoValueL();
       
   281             TReal lat, lon;
       
   282             if ( geoVal && geoVal->GetLatLong( lat, lon ) )
       
   283                 {
       
   284                 res = ETrue;
       
   285                 }
       
   286             else
       
   287                 {
       
   288                 // Check if description field has location url
       
   289                 const TDesC& description = aEntry.Entry().DescriptionL();
       
   290                 TPtrC locationUrl;
       
   291                 TInt position = UrlParserL().FindLocationUrl( description,
       
   292                                                               locationUrl );
       
   293                 if ( position >= 0 )
       
   294                     {
       
   295                     res = ETrue;
       
   296                     }
       
   297                 }
       
   298 
       
   299             break;
       
   300             }
       
   301 #ifdef RD_USE_MYLOCATIONUI
       
   302         case EMRCommandMyLocations:
       
   303             {
       
   304             res = ETrue;
       
   305             break;
       
   306             }
       
   307 #endif
       
   308         default:
       
   309             {
       
   310             break;
       
   311             }
       
   312         }
       
   313 
       
   314     return res;
       
   315     }
       
   316 
       
   317 // -----------------------------------------------------------------------------
       
   318 // CESMRLocationPluginHandler::StoreLocationToHistoryL
       
   319 // -----------------------------------------------------------------------------
       
   320 //
       
   321 void CESMRLocationPluginHandler::StoreLocationToHistoryL(
       
   322         const MESMRCalEntry& aEntry  )
       
   323     {
       
   324     FUNC_LOG;
       
   325     const CCalEntry& entry = aEntry.Entry();
       
   326     const TDesC& entryLocation = entry.LocationL();
       
   327 
       
   328     // Store location only if location field contains text.
       
   329     if ( entryLocation.Length() > 0 )
       
   330         {
       
   331         const TDesC& entryDescription = entry.DescriptionL();
       
   332 
       
   333         // Search for location url from description.
       
   334         TPtrC locationUrl;
       
   335         TInt position = UrlParserL().FindLocationUrl( entryDescription,
       
   336                                                       locationUrl );
       
   337 
       
   338         // If url was not found.
       
   339         if ( position < 0 )
       
   340             {
       
   341             locationUrl.Set( KNullDesC );
       
   342             }
       
   343 
       
   344         // Check if history item with same location and url
       
   345         // is already stored.
       
   346         TInt existingHistoryItemIndex = KErrNotFound;
       
   347         TInt itemCount = LocationHistoryManagerL().ItemCount();
       
   348         for ( TInt i = 0; i < itemCount; ++i )
       
   349             {
       
   350             const MESMRLocationHistoryItem& item =
       
   351                 iLocationHistoryManager->LocationHistoryItemL( i );
       
   352 
       
   353             if ( item.Address().Compare( entryLocation ) == 0 &&
       
   354                  item.Url().Compare( locationUrl ) == 0 )
       
   355                 {
       
   356                 existingHistoryItemIndex = i;
       
   357                 break;
       
   358                 }
       
   359             }
       
   360 
       
   361         // If history item with same location and url existed
       
   362         // update it's position in history array only.
       
   363         if ( existingHistoryItemIndex >= 0 )
       
   364             {
       
   365             // Ownership is not transferred,
       
   366             // existing item is already owned by location manager.
       
   367             const MESMRLocationHistoryItem& item =
       
   368                 iLocationHistoryManager->LocationHistoryItemL( existingHistoryItemIndex );
       
   369 
       
   370             iLocationHistoryManager->UpdateLocationHistoryL( &item );
       
   371             }
       
   372         else
       
   373             {
       
   374             // Create new history item.
       
   375             MESMRLocationHistoryItem* historyItem =
       
   376                 iLocationHistoryManager->CreateLocationHistoryItemL(
       
   377                             entryLocation,
       
   378                             locationUrl );
       
   379 
       
   380             // Store new item to history.
       
   381             CleanupDeletePushL( historyItem );
       
   382             iLocationHistoryManager->UpdateLocationHistoryL( historyItem );
       
   383             CleanupStack::Pop( historyItem );
       
   384             }
       
   385         }
       
   386     }
       
   387 
       
   388 // -----------------------------------------------------------------------------
       
   389 // CESMRLocationPluginHandler::UpdateEntryLocationL
       
   390 // -----------------------------------------------------------------------------
       
   391 //
       
   392 TBool CESMRLocationPluginHandler::UpdateEntryLocationL( MESMRCalEntry& aEntry,
       
   393                                                         const TDesC& aLocation,
       
   394                                                         TBool aIgnoreQuery,
       
   395                                                         TBool& aLocationReplaced )
       
   396     {
       
   397     FUNC_LOG;
       
   398     TBool entryUpdated = EFalse;
       
   399 
       
   400     if ( aLocation.Length() > 0 )
       
   401         {
       
   402         CCalEntry& entry = aEntry.Entry();
       
   403         const TDesC& location = entry.LocationL();
       
   404 
       
   405         // Replace old location field content.
       
   406         TBool replace = ETrue;
       
   407 
       
   408         if ( !aIgnoreQuery
       
   409              && location.Length() > 0
       
   410              && location != aLocation )
       
   411             {
       
   412             // Query for replacing old location field content
       
   413             replace = CESMRConfirmationQuery::ExecuteL( location );
       
   414             }
       
   415 
       
   416         // If previous location was empty or user selects to
       
   417         // replace previous location with new one.
       
   418         if (  location.Length() == 0 || replace )
       
   419             {
       
   420             entry.SetLocationL( aLocation.Left( KMaxLocationTextLength ) );
       
   421             entryUpdated = ETrue;
       
   422             aLocationReplaced = ETrue;
       
   423             }
       
   424         else
       
   425             {
       
   426             // Concatenate new address to existing one
       
   427             // Create CPtrCArray for existing and new location strings
       
   428             CPtrCArray* strings = new( ELeave ) CPtrCArray( 2 );
       
   429             CleanupStack::PushL( strings );
       
   430             strings->AppendL( TPtrC( location ) );
       
   431             strings->AppendL( TPtrC( aLocation ) );
       
   432             HBufC* newLocation =
       
   433                 StringLoader::LoadLC( R_MEET_REQ_NEW_LOCATION_FORMAT,
       
   434                                       *strings );
       
   435             entry.SetLocationL( newLocation->Left( KMaxLocationTextLength ) );
       
   436             entryUpdated = ETrue;
       
   437             CleanupStack::PopAndDestroy( 2, strings );
       
   438             }
       
   439         }
       
   440 
       
   441     return entryUpdated;
       
   442     }
       
   443 
       
   444 // -----------------------------------------------------------------------------
       
   445 // From MESMRLocationPluginObserver
       
   446 // CESMRLocationPluginHandler::SelectFromMapCompleted
       
   447 // -----------------------------------------------------------------------------
       
   448 //
       
   449 void CESMRLocationPluginHandler::SelectFromMapCompleted( TInt aError,
       
   450                                                          CPosLandmark* aLandmark )
       
   451     {
       
   452     FUNC_LOG;
       
   453     // Remove previous landmark if one is set.
       
   454     if ( iLandmark )
       
   455         {
       
   456         delete iLandmark;
       
   457         iLandmark = NULL;
       
   458         }
       
   459 
       
   460     if ( aError == KErrNone )
       
   461         {
       
   462         // pointer to the result of landmark fetch
       
   463         iLandmark = aLandmark;
       
   464         }
       
   465 
       
   466     if ( iWait->IsStarted() )
       
   467         {
       
   468         iWait->AsyncStop();
       
   469         }
       
   470     }
       
   471 // -----------------------------------------------------------------------------
       
   472 // CESMRLocationPluginHandler::LocationPluginL
       
   473 // -----------------------------------------------------------------------------
       
   474 //
       
   475 CESMRLocationPlugin& CESMRLocationPluginHandler::LocationPluginL()
       
   476     {
       
   477     FUNC_LOG;
       
   478     if ( !iLocationPlugin )
       
   479         {
       
   480         // Lazy construction of Location ECom plugin
       
   481         iLocationPlugin = CESMRLocationPlugin::NewL();
       
   482         iLocationPlugin->SetObserver(this);
       
   483         }
       
   484 
       
   485     return *iLocationPlugin;
       
   486     }
       
   487 
       
   488 // ---------------------------------------------------------------------------
       
   489 // Creates adress descriptor from a  landmark object
       
   490 // ---------------------------------------------------------------------------
       
   491 //
       
   492 HBufC* CESMRLocationPluginHandler::CreateAddressFromLandmarkL(
       
   493                                                const CPosLandmark& aLandmark )
       
   494     {
       
   495     FUNC_LOG;
       
   496     CPtrCArray* addressStrings = new( ELeave ) CPtrCArray( 2 );
       
   497     CleanupStack::PushL( addressStrings );
       
   498 
       
   499     TBool streetAvailable = aLandmark.IsPositionFieldAvailable( EPositionFieldStreet );
       
   500     TBool cityAvailable = aLandmark.IsPositionFieldAvailable( EPositionFieldCity );
       
   501 
       
   502     if ( streetAvailable )
       
   503         {
       
   504         TPtrC streetPtr( KNullDesC );
       
   505         User::LeaveIfError( aLandmark.GetPositionField( EPositionFieldStreet,
       
   506                                                          streetPtr ) );
       
   507         if ( streetPtr.Length() > 0 )
       
   508             {
       
   509             addressStrings->AppendL( streetPtr );
       
   510             }
       
   511         }
       
   512 
       
   513     if ( cityAvailable )
       
   514         {
       
   515         TPtrC cityPtr( KNullDesC );
       
   516         User::LeaveIfError( aLandmark.GetPositionField( EPositionFieldCity,
       
   517                                                          cityPtr ) );
       
   518         if ( cityPtr.Length() > 0 )
       
   519             {
       
   520             addressStrings->AppendL( cityPtr );
       
   521             }
       
   522         }
       
   523 
       
   524     HBufC* address(NULL);
       
   525 
       
   526     if ( addressStrings->Count() == 2 )
       
   527         {
       
   528         //format street and city to buffer
       
   529         address = StringLoader::LoadL( R_MEET_REQ_ADDRESS_STREET_CITY,
       
   530                                        *addressStrings );
       
   531         }
       
   532     else if ( addressStrings->Count() == 1 )
       
   533         {
       
   534         // alloc street or city to buffer
       
   535         address = addressStrings->At( 0 ).AllocL();
       
   536         }
       
   537     else if ( addressStrings->Count() == 0 )
       
   538         {
       
   539         TPtrC namePtr( KNullDesC );
       
   540         User::LeaveIfError( aLandmark.GetLandmarkName( namePtr ));
       
   541         if ( namePtr.Length() > 0)
       
   542             {
       
   543             address = namePtr.AllocL();
       
   544             }
       
   545         }
       
   546 
       
   547     CleanupStack::PopAndDestroy( addressStrings );
       
   548 
       
   549     //Transfer ownership of address
       
   550     return address;
       
   551     }
       
   552 
       
   553 // -----------------------------------------------------------------------------
       
   554 // CESMRLocationPluginHandler::UpdateEntryFromLandmarkL
       
   555 // Updates location information into calendar entry from aLandmark.
       
   556 // -----------------------------------------------------------------------------
       
   557 //
       
   558 void CESMRLocationPluginHandler::UpdateEntryFromLandmarkL(
       
   559         MESMRCalEntry& aEntry,
       
   560         const CPosLandmark& aLandmark )
       
   561     {
       
   562     FUNC_LOG;
       
   563     //parse and add address to calendar entry
       
   564     HBufC* streetAddress = CreateAddressFromLandmarkL( aLandmark );
       
   565     if ( streetAddress )
       
   566         {
       
   567         CleanupDeletePushL( streetAddress );
       
   568         UpdateEntryLocationL( aEntry, *streetAddress );
       
   569         CleanupStack::PopAndDestroy( streetAddress );
       
   570         }
       
   571 
       
   572     CCalEntry& entry = aEntry.Entry();
       
   573     TLocality locality;
       
   574     if ( aLandmark.GetPosition( locality ) == KErrNone )
       
   575         {
       
   576         // Store GEO value to entry
       
   577         CCalGeoValue* geoVal = CCalGeoValue::NewL();
       
   578         CleanupStack::PushL( geoVal );
       
   579         geoVal->SetLatLongL( locality.Latitude(), locality.Longitude() );
       
   580         entry.SetGeoValueL( *geoVal );
       
   581         CleanupStack::PopAndDestroy( geoVal );
       
   582         
       
   583         //parse and add location url to calendar entry
       
   584         HBufC* locationUrl = UrlParserL().CreateUrlFromLandmarkL( aLandmark );
       
   585         CleanupDeletePushL( locationUrl );
       
   586         UpdateEntryDescriptionL( aEntry, *locationUrl );
       
   587         CleanupStack::PopAndDestroy( locationUrl );
       
   588         }
       
   589     else if ( iLocationReplaced )// Location field content from this landmark
       
   590         {
       
   591         // Clear GEO value and location URL
       
   592         entry.ClearGeoValueL();
       
   593         const TDesC& description = entry.DescriptionL();
       
   594         TPtrC url;
       
   595         TInt pos = UrlParserL().FindLocationUrl( description, url );
       
   596         if ( pos > KErrNotFound )
       
   597             {
       
   598             TPtrC desc = description.Mid( pos + url.Length() );
       
   599             entry.SetDescriptionL( desc );
       
   600             }
       
   601         }
       
   602     }
       
   603 
       
   604 // -----------------------------------------------------------------------------
       
   605 // CESMRLocationPluginHandler::SearchFromLandmarksL
       
   606 // -----------------------------------------------------------------------------
       
   607 //
       
   608 void CESMRLocationPluginHandler::SearchFromLandmarksL( MObjectProvider* aParent, MESMRCalEntry& aEntry )
       
   609     {
       
   610     FUNC_LOG;
       
   611     CLmkLandmarkSelectorDlg* landmarkDlg = CLmkLandmarkSelectorDlg::NewL();
       
   612     CleanupStack::PushL( landmarkDlg );
       
   613     landmarkDlg->SetMopParent( aParent );
       
   614     TLmkItemIdDbCombiInfo selectedItem;
       
   615 
       
   616     // Execute landmark selection dialog
       
   617     TInt isAccepted = landmarkDlg->ExecuteLD( selectedItem );
       
   618     CleanupStack::Pop( landmarkDlg );
       
   619     if ( isAccepted != 0 )
       
   620         {
       
   621         TPosLmItemId itemId = selectedItem.GetItemId();
       
   622         CPosLandmarkDatabase* lmDataBase = selectedItem.GetLmDb();
       
   623         CleanupStack::PushL( lmDataBase );
       
   624 
       
   625         // Read selected landmark from landmarks database
       
   626         CPosLandmark* landmark = lmDataBase->ReadLandmarkLC( itemId );
       
   627         UpdateEntryFromLandmarkL( aEntry, *landmark );
       
   628         CleanupStack::PopAndDestroy( landmark );
       
   629         CleanupStack::PopAndDestroy( lmDataBase );
       
   630         ReleaseLandmarkResources();
       
   631         }
       
   632     }
       
   633 
       
   634 // -----------------------------------------------------------------------------
       
   635 // CESMRLocationPluginHandler::ShowSelectPreviousLocationQueryL
       
   636 // Shows "Previous locations" query and updates aEntry
       
   637 // according to selection.
       
   638 // -----------------------------------------------------------------------------
       
   639 //
       
   640 void CESMRLocationPluginHandler::ShowSelectPreviousLocationQueryL( MESMRCalEntry& aEntry )
       
   641     {
       
   642     FUNC_LOG;
       
   643     // Array for listbox items.
       
   644     RCPointerArray<HBufC> addressArray;
       
   645     CleanupClosePushL( addressArray );
       
   646 
       
   647     // Populate address array.
       
   648     TInt itemCount = LocationHistoryManagerL().ItemCount();
       
   649     for ( TInt i = 0; i < itemCount; ++i )
       
   650         {
       
   651         const MESMRLocationHistoryItem& item =
       
   652             iLocationHistoryManager->LocationHistoryItemL( i );
       
   653         HBufC* address = item.Address().AllocLC();
       
   654         addressArray.AppendL( address );
       
   655         CleanupStack::Pop( address );
       
   656         }
       
   657 
       
   658     CESMRDynamicItemSelectionList* selectionQuery =
       
   659             CESMRDynamicItemSelectionList::NewL();
       
   660     CleanupStack::PushL( selectionQuery );
       
   661 
       
   662     // Execute selection query.
       
   663     TInt idx = selectionQuery->ExecuteL( addressArray,
       
   664             CESMRDynamicItemSelectionList::EESMRRecentLocationList );
       
   665 
       
   666     CleanupStack::PopAndDestroy( selectionQuery );
       
   667 
       
   668     if ( idx >= 0 && idx < addressArray.Count() )
       
   669         {
       
   670         const MESMRLocationHistoryItem& item =
       
   671             iLocationHistoryManager->LocationHistoryItemL( idx );
       
   672 
       
   673         // Update entry location.
       
   674         const TDesC& selectedLocation = item.Address();
       
   675         UpdateEntryLocationL( aEntry, selectedLocation );
       
   676 
       
   677         // Update entry description if url available.
       
   678         const TDesC& selectedLocationUrl = item.Url();
       
   679         if ( selectedLocationUrl.Length() > 0 )
       
   680             {
       
   681             UpdateEntryDescriptionL( aEntry, selectedLocationUrl );
       
   682             }
       
   683         }
       
   684 
       
   685     CleanupStack::PopAndDestroy( &addressArray );
       
   686     }
       
   687 
       
   688 // -----------------------------------------------------------------------------
       
   689 // CESMRLocationPluginHandler::UpdateEntryLocationL
       
   690 // Updates aEntry location with aLocation.
       
   691 // -----------------------------------------------------------------------------
       
   692 //
       
   693 void CESMRLocationPluginHandler::UpdateEntryLocationL(
       
   694         MESMRCalEntry& aEntry, const TDesC& aLocation )
       
   695     {
       
   696     FUNC_LOG;
       
   697     iEntryUpdated |= UpdateEntryLocationL( aEntry,
       
   698                                            aLocation,
       
   699                                            iIgnoreQuery,
       
   700                                            iLocationReplaced );
       
   701     }
       
   702 
       
   703 // -----------------------------------------------------------------------------
       
   704 // CESMRLocationPluginHandler::UpdateEntryDescriptionL
       
   705 // Updates aEntry description with aLocationUrl.
       
   706 // -----------------------------------------------------------------------------
       
   707 //
       
   708 void CESMRLocationPluginHandler::UpdateEntryDescriptionL(
       
   709         MESMRCalEntry& aEntry, const TDesC& aLocationUrl )
       
   710     {
       
   711     FUNC_LOG;
       
   712     if ( aLocationUrl.Length() > 0 )
       
   713         {
       
   714         CCalEntry& entry = aEntry.Entry();
       
   715         HBufC* description = HBufC::NewL( entry.DescriptionL().Length() +
       
   716                              aLocationUrl.Length() + 1);
       
   717         CleanupStack::PushL( description );
       
   718         TPtr descriptionPointer( description->Des() );
       
   719         TPtrC urlPointer;
       
   720         TInt position;
       
   721         position = UrlParserL().FindLocationUrl( entry.DescriptionL(), urlPointer);
       
   722 
       
   723         if ( position >= KErrNone )
       
   724             {
       
   725             descriptionPointer.Copy( entry.DescriptionL() );
       
   726             if ( ( position > 0 ) && ( entry.DescriptionL().Length() >
       
   727                                         (position + aLocationUrl.Length() ) ) )
       
   728                 {
       
   729                 descriptionPointer.Delete( position, ( urlPointer.Length() + 1 ) );
       
   730                 }
       
   731             else
       
   732                 {
       
   733                 descriptionPointer.Delete( position, urlPointer.Length() );
       
   734                 }
       
   735             descriptionPointer.Insert( 0, aLocationUrl );
       
   736             if ( position > 0 )
       
   737                 {
       
   738                 descriptionPointer.Insert( aLocationUrl.Length(), KSpace );
       
   739                 }
       
   740             entry.SetDescriptionL( *description );
       
   741             iEntryUpdated = ETrue;
       
   742             }
       
   743         else
       
   744             {
       
   745             descriptionPointer.Copy( entry.DescriptionL() );
       
   746             descriptionPointer.Insert( 0, aLocationUrl );
       
   747             if ( entry.DescriptionL().Length() != 0 )
       
   748                 {
       
   749                 descriptionPointer.Insert( aLocationUrl.Length(), KSpace );
       
   750                 }
       
   751             entry.SetDescriptionL( *description );
       
   752             iEntryUpdated = ETrue;
       
   753             }
       
   754         CleanupStack::PopAndDestroy( description );
       
   755         }
       
   756     }
       
   757 
       
   758 // -----------------------------------------------------------------------------
       
   759 // CESMRLocationPluginHandler::UrlParserL
       
   760 // Creates and returns location URL parser.
       
   761 // -----------------------------------------------------------------------------
       
   762 //
       
   763 CESMRUrlParserPlugin& CESMRLocationPluginHandler::UrlParserL()
       
   764     {
       
   765     FUNC_LOG;
       
   766     if ( !iUrlParser )
       
   767         {
       
   768         iUrlParser = CESMRUrlParserPlugin::NewL();
       
   769         }
       
   770 
       
   771     return *iUrlParser;
       
   772     }
       
   773 
       
   774 // -----------------------------------------------------------------------------
       
   775 // CESMRLocationPluginHandler::LocationHistoryManagerL
       
   776 // Creates and returns location history manager.
       
   777 // -----------------------------------------------------------------------------
       
   778 //
       
   779 CESMRLocationHistoryManager& CESMRLocationPluginHandler::LocationHistoryManagerL()
       
   780     {
       
   781     FUNC_LOG;
       
   782     if ( !iLocationHistoryManager )
       
   783         {
       
   784         iLocationHistoryManager = CESMRLocationHistoryManager::NewL();
       
   785         }
       
   786 
       
   787     return *iLocationHistoryManager;
       
   788     }
       
   789 
       
   790 // EOF
       
   791