meetingrequest/mrurlparserplugin/src/cmrurlparserextension.cpp
branchRCL_3
changeset 12 4ce476e64c59
equal deleted inserted replaced
11:0396474f30f5 12:4ce476e64c59
       
     1 /*
       
     2 * Copyright (c) 2008-2008 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:  Parser class extension for parsing landmarks to url and vice versa
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "cmrurlparserextension.h"
       
    21 
       
    22 #include <EPos_CPosLandmark.h>
       
    23 #include <lbsposition.h>
       
    24 #include <utf.h>
       
    25 #include <finditemengine.h>
       
    26 #include <calentry.h>
       
    27 
       
    28 #include "emailtrace.h"
       
    29 
       
    30 namespace
       
    31 {
       
    32 // Old location base URL format
       
    33 _LIT( KLocationUrl, "http://www.ovi.com/maps/" );
       
    34 
       
    35 // Latitude parameter
       
    36 _LIT( KUrlParamLatitude, "lat=" );
       
    37 
       
    38 // Parameter separator
       
    39 _LIT( KUrlParamSeparator, "&" );
       
    40 
       
    41 // Longitude parameter
       
    42 _LIT( KUrlParamLongitude, "lon=" );
       
    43 
       
    44 // Location sieve URL
       
    45 _LIT( KLocationSieveUrl, "http://www.ovi.com/maps/lat=*&lon=*" );
       
    46 
       
    47 const TUint KDecimalSeparator = '.';
       
    48 const TUint KMinusSign = '-';
       
    49 
       
    50 // ======== LOCAL FUNCTIONS ========
       
    51 // ----------------------------------------------------------------------------
       
    52 // CheckCoordinateParamL
       
    53 //
       
    54 // Checks if coordinate parameter (lon or lat) is in correct format
       
    55 // Leaves if not
       
    56 // Correct form is:
       
    57 // -minus sign allowed only in first position
       
    58 // -only one decimalseparator sign allowed
       
    59 // -only digits allowed
       
    60 // -aParam length not allowed to be zero
       
    61 // ----------------------------------------------------------------------------
       
    62 //
       
    63 void CheckCoordinateParamL( const TDesC& aParam )
       
    64     {
       
    65     FUNC_LOG;
       
    66     if( aParam.Length() == 0 )
       
    67         {
       
    68         User::Leave( KErrArgument );
       
    69         }
       
    70 
       
    71     //check that aParam contains only digits and only one decimalseparator
       
    72     //and minus sign is first, if it exists
       
    73     TChar character;
       
    74     TBool decimalSeparatorFound = EFalse;
       
    75     TLex lex;
       
    76     lex.Assign( aParam );
       
    77 
       
    78     for( TInt i = 0; i < aParam.Length(); i++)
       
    79         {
       
    80         character = lex.Get();
       
    81         //checks if first character is minus sign and continues if it is
       
    82         if( i == 0 && (TUint)character == KMinusSign)
       
    83             {
       
    84             continue;
       
    85             }
       
    86         
       
    87         //check that only one decimal separator exists
       
    88         if ( (TUint)character == KDecimalSeparator )
       
    89             {
       
    90             if ( decimalSeparatorFound )
       
    91                 {
       
    92                 User::Leave( KErrArgument );
       
    93                 }
       
    94             else
       
    95                 {
       
    96                 decimalSeparatorFound = ETrue;
       
    97                 }
       
    98             }
       
    99         //check that character is either digit or decimalseparator
       
   100         if( !( character.IsDigit() ) && (TUint)character != KDecimalSeparator )
       
   101             {
       
   102             User::Leave( KErrArgument );
       
   103             }
       
   104         }
       
   105     }
       
   106 
       
   107 // ----------------------------------------------------------------------------
       
   108 // GetCoordinateParamValuesL
       
   109 // Returns longitude and latitude if found correctly
       
   110 // ----------------------------------------------------------------------------
       
   111 //
       
   112 void GetCoordinateParamValuesL( const TDesC& aUrl,
       
   113                                 TPtrC& aLatitude,
       
   114                                 TPtrC& aLongitude )
       
   115     {
       
   116     FUNC_LOG;
       
   117     
       
   118     //Find out if lat and lon params and separator exists in aUrl
       
   119     TInt latPos = aUrl.Find( KUrlParamLatitude );
       
   120     TInt lonPos = aUrl.Find( KUrlParamLongitude );
       
   121     TInt separatorPos = aUrl.Find( KUrlParamSeparator );
       
   122     
       
   123     if( latPos == KErrNotFound || lonPos == KErrNotFound
       
   124          || separatorPos == KErrNotFound )
       
   125         {
       
   126         User::Leave( KErrNotFound );
       
   127         }
       
   128 
       
   129     //takes from aUrl parts with actual coordinate data
       
   130     aLatitude.Set( aUrl.Mid(
       
   131             latPos + KUrlParamLatitude().Length(),
       
   132             separatorPos - latPos - KUrlParamLatitude().Length() ) );
       
   133     
       
   134     aLongitude.Set( aUrl.Right(
       
   135             aUrl.Length() - separatorPos - 1 - KUrlParamLongitude().Length() ) );
       
   136     }
       
   137 }
       
   138 
       
   139 // ======== MEMBER FUNCTIONS ========
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // CMRUrlParserExtension::CMRUrlParserExtension
       
   143 // ---------------------------------------------------------------------------
       
   144 //
       
   145 CMRUrlParserExtension::CMRUrlParserExtension()
       
   146     {
       
   147     FUNC_LOG;
       
   148     }
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // CMRUrlParserExtension::NewL
       
   152 // ---------------------------------------------------------------------------
       
   153 //
       
   154 CMRUrlParserExtension* CMRUrlParserExtension::NewL()
       
   155     {
       
   156     FUNC_LOG;
       
   157     
       
   158     CMRUrlParserExtension* self = CMRUrlParserExtension::NewLC();
       
   159     CleanupStack::Pop( self );
       
   160     return self;
       
   161     }
       
   162 
       
   163 
       
   164 // ---------------------------------------------------------------------------
       
   165 // CMRUrlParserExtension::NewLC
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168 CMRUrlParserExtension* CMRUrlParserExtension::NewLC()
       
   169     {
       
   170     FUNC_LOG;
       
   171     
       
   172     CMRUrlParserExtension* self = new( ELeave ) CMRUrlParserExtension;
       
   173     CleanupStack::PushL( self );
       
   174     return self;
       
   175     }
       
   176 
       
   177 
       
   178 // ---------------------------------------------------------------------------
       
   179 // CMRUrlParserExtension::~CMRUrlParserExtension
       
   180 // ---------------------------------------------------------------------------
       
   181 //
       
   182 CMRUrlParserExtension::~CMRUrlParserExtension()
       
   183     {
       
   184     FUNC_LOG;
       
   185     }
       
   186 
       
   187 // ---------------------------------------------------------------------------
       
   188 // CMRUrlParserExtension::FindLocationUrl
       
   189 // Finds location URL from given text input
       
   190 // ---------------------------------------------------------------------------
       
   191 //
       
   192 void CMRUrlParserExtension::FindLocationUrlL(
       
   193         const TDesC& aText,
       
   194         TPtrC& aUrl,
       
   195         TInt& aPos)
       
   196 
       
   197     {
       
   198     FUNC_LOG;
       
   199     
       
   200     //Seach if base string is found from aText
       
   201     TInt urlLocation = aText.FindF( KLocationUrl );
       
   202     if ( urlLocation < KErrNone )
       
   203         {
       
   204         User::Leave(KErrNotFound);
       
   205         }
       
   206 
       
   207     //Take out unnecessary part before URL and search if endmark (whitespace)
       
   208     //is found. If endmark is not found,
       
   209     //then all the rest of descriptor is part of URL
       
   210     TPtrC urlAndAfter = aText.Mid( urlLocation );
       
   211     TInt urlAndAfterLength = urlAndAfter.Length();
       
   212     TInt urlEndmarkLocation = KErrNotFound;
       
   213 
       
   214     for( TInt i = 0; i < urlAndAfterLength; i++)
       
   215         {
       
   216         if( TChar( urlAndAfter[i] ).IsSpace() )
       
   217             {
       
   218             urlEndmarkLocation = i;
       
   219             break;
       
   220             }
       
   221         }
       
   222 
       
   223     if( urlEndmarkLocation == KErrNotFound )
       
   224         {
       
   225         urlEndmarkLocation = urlAndAfterLength;
       
   226         }
       
   227 
       
   228     //Take out part from beginning of URL to endmark
       
   229     TPtrC urlToEndMark = urlAndAfter.Left( urlEndmarkLocation );
       
   230 
       
   231     //Now we should have only URL left, check with "sieve" that it is about in
       
   232     //right format
       
   233     TInt sievedStartPoint = urlToEndMark.MatchF( KLocationSieveUrl );
       
   234 
       
   235     if( sievedStartPoint == KErrNotFound )
       
   236         {
       
   237         User::Leave(KErrNotFound);
       
   238         }
       
   239 
       
   240     //Check that parameters are in right format
       
   241     TPtrC latValue;
       
   242     TPtrC lonValue;
       
   243 
       
   244     GetCoordinateParamValuesL(
       
   245             urlToEndMark,
       
   246             latValue,
       
   247             lonValue );
       
   248     CheckCoordinateParamL( latValue );
       
   249     CheckCoordinateParamL( lonValue );
       
   250 
       
   251     //Set aURL to correspond URL part of aText and aPos with url position
       
   252     aUrl.Set( urlToEndMark );
       
   253     aPos = urlLocation;
       
   254     }
       
   255 
       
   256 // ---------------------------------------------------------------------------
       
   257 // CMRUrlParserExtension::CreateLandmarkFromUrlL
       
   258 // Creates landmark object from location URL
       
   259 // ---------------------------------------------------------------------------
       
   260 //
       
   261 CPosLandmark* CMRUrlParserExtension::CreateLandmarkFromUrlL(
       
   262         const TDesC& aUrl )
       
   263     {
       
   264     FUNC_LOG;
       
   265     
       
   266     TInt matchPos = User::LeaveIfError( aUrl.MatchF( KLocationSieveUrl ) );
       
   267     
       
   268     if( matchPos != 0 )
       
   269         {
       
   270         //URL was found but is not int the beginning of desc
       
   271         User::Leave( KErrArgument );
       
   272         }
       
   273 
       
   274     //Parse actual coordinate values out of url
       
   275     TPtrC latValue;
       
   276     TPtrC lonValue;
       
   277     GetCoordinateParamValuesL( aUrl, latValue, lonValue );
       
   278 
       
   279     //Check that parameters are in right format
       
   280     CheckCoordinateParamL( latValue );
       
   281     CheckCoordinateParamL( lonValue );
       
   282 
       
   283     //Convert parameters to TReal values
       
   284     TLex lexConverter( latValue );
       
   285     TReal64 realLatitude;
       
   286     lexConverter.Val( realLatitude );
       
   287 
       
   288     lexConverter.Assign( lonValue );
       
   289     TReal64 realLongitude;
       
   290     lexConverter.Val( realLongitude );
       
   291 
       
   292     //Create landmark with coordinatevalues
       
   293     CPosLandmark* landmark = CPosLandmark::NewLC();
       
   294     TLocality position;
       
   295     position.SetCoordinate( realLatitude, realLongitude );
       
   296     landmark->SetPositionL( position );
       
   297     CleanupStack::Pop( landmark );
       
   298     
       
   299     //transfer ownership
       
   300     return landmark;
       
   301     }
       
   302 
       
   303 //EOF
       
   304