voipplugins/svtmatching/src/svturiparser.cpp
branchRCL_3
changeset 22 d38647835c2e
equal deleted inserted replaced
21:f742655b05bf 22:d38647835c2e
       
     1 /*
       
     2 * Copyright (c) 2008-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:  Settings handler class for svtmatching.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "svturiparser.h"
       
    20 
       
    21 // Valid characters for GSM call
       
    22 _LIT( KPhoneValidChars, "0123456789*#+pwPW" );
       
    23 // For parsing protocol prefix and domain part out of a VoIP call URI
       
    24 _LIT( KSvtColon, ":" );
       
    25 _LIT( KSvtAt, "@" );
       
    26 // Indicates the start of sip uri.
       
    27 const TUint KStartBracket = '<'; 
       
    28 // Indicates the end of sip uri.
       
    29 const TUint KEndBracket = '>'; 
       
    30 const TUint KSpaceMark = ' ';
       
    31 const TUint KQuotationMark = '"';
       
    32 
       
    33 
       
    34 // ======== MEMBER FUNCTIONS ========
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 CSvtUriParser::CSvtUriParser()
       
    40     {
       
    41     }
       
    42 
       
    43   
       
    44 // ---------------------------------------------------------------------------
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 CSvtUriParser* CSvtUriParser::NewL()
       
    48     {
       
    49     CSvtUriParser* self = new ( ELeave ) CSvtUriParser;
       
    50     return self;
       
    51     }
       
    52 
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 CSvtUriParser::~CSvtUriParser()
       
    58     {
       
    59 
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // Parses sip uri by ignore domain part setting
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 void CSvtUriParser::ParseAddressL( 
       
    67         TInt aIgnoreDomain, 
       
    68         const TDesC& aOriginal, 
       
    69         RBuf& aParsedAddress ) const
       
    70     {
       
    71     switch ( aIgnoreDomain )
       
    72         {
       
    73         case 1:
       
    74         case 2:
       
    75             {
       
    76             HandleUserNamePartL( aIgnoreDomain, aOriginal, aParsedAddress );
       
    77             break;
       
    78             }
       
    79         case 0:
       
    80         default:
       
    81             {
       
    82             aParsedAddress.Close();
       
    83             aParsedAddress.CreateL( aOriginal );
       
    84             break;
       
    85             }
       
    86         
       
    87         }
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // Resolves display name from sip uri
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 TInt CSvtUriParser::DisplayNameFromUri( 
       
    95         const TDesC& aData, 
       
    96         RBuf& aDisplayname ) const
       
    97     {
       
    98     TInt result = KErrNotFound;
       
    99 
       
   100     TRAPD( err, DisplayNameFromUriL( aData, aDisplayname, result ) );
       
   101     if ( err )
       
   102         {
       
   103         result = err;
       
   104         }
       
   105     return result;
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // The inner logic for resolving display name from sip uri
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 void CSvtUriParser::DisplayNameFromUriL(
       
   113         const TDesC& aData,
       
   114         RBuf& aDisplayname,
       
   115         TInt& aResult ) const
       
   116     {
       
   117     aResult = KErrNotFound;
       
   118     
       
   119     TPtrC preResultStr( aData );
       
   120     aDisplayname.Close();
       
   121     
       
   122     HBufC* tempBuffer = preResultStr.AllocLC(); // CS:1
       
   123     tempBuffer->Des().TrimAll();
       
   124     
       
   125     if ( tempBuffer->Length() )
       
   126         {
       
   127         TPtrC resultStr( tempBuffer->Des() );
       
   128 
       
   129         // resolves potential SIP Display info and removes it from uri
       
   130         // also possible "<" and ">" character are removed around the SIP uri
       
   131         TInt uriStartIndex = resultStr.LocateReverse( KStartBracket );      
       
   132         TInt uriEndIndex = resultStr.LocateReverse( KEndBracket );       
       
   133         
       
   134         if ( KErrNotFound != uriStartIndex && KErrNotFound != uriEndIndex )
       
   135             {
       
   136             if ( uriStartIndex < uriEndIndex )
       
   137                 {
       
   138                 // brackets found so modify descriptor and save the display info
       
   139                 
       
   140                 // check if there is anything before "<" if there is use
       
   141                 // it as displayname if match op fails.
       
   142                 if ( uriStartIndex > 1 )
       
   143                     {        
       
   144                     TPtrC tempStr( resultStr.Left( uriStartIndex ) );          
       
   145                     // remove possible quotation marks from displayname
       
   146                     TInt index = tempStr.Locate( KQuotationMark );
       
   147                     if ( KErrNotFound != index )
       
   148                         {
       
   149                         // marks have to be removed
       
   150                         tempStr.Set( tempStr.Mid( ++index ) );    
       
   151                         if ( tempStr[tempStr.Length() - 1] == KQuotationMark )
       
   152                             {
       
   153                             tempStr.Set( tempStr.Left( tempStr.Length() - 1 ) );
       
   154                             }
       
   155                         }
       
   156                     aResult = aDisplayname.Create( tempStr );
       
   157                     }                      
       
   158                 }
       
   159             else
       
   160                 {
       
   161                 // Start and end separators in wrong order: "xxxx>xxxx<xxx"
       
   162                 aResult = KErrArgument;
       
   163                 }
       
   164             }        
       
   165         else 
       
   166             {
       
   167             // it is also possible that displayname is included, in case there's
       
   168             // no brackets around the uri. So if there is something
       
   169             // inside quotationMarks it should be used as displayname
       
   170     
       
   171             // check if displayname is found
       
   172             TInt displayNameStart = resultStr.Locate( KQuotationMark );
       
   173             TInt displayNameEnd = resultStr.LocateReverse( KQuotationMark );
       
   174     
       
   175             if ( displayNameStart != KErrNotFound 
       
   176                 && displayNameEnd != KErrNotFound 
       
   177                 && displayNameStart < displayNameEnd )
       
   178                 {
       
   179                 // displayname is included
       
   180                 // +1, to remove quotationMark from the start
       
   181                 // -1, to remove quotationMark from the end
       
   182                 aResult = aDisplayname.Create( resultStr.Mid( displayNameStart + 1,
       
   183                                        displayNameEnd - displayNameStart - 1 ) );
       
   184                 }                                
       
   185             else
       
   186                 {
       
   187                 // check if there is spaces in the uri. If there is,
       
   188                 // everything before it belongs to display name                
       
   189                 TInt index = resultStr.LocateReverse( KSpaceMark );
       
   190                 
       
   191                 if ( KErrNotFound != index )            
       
   192                     {
       
   193                     // set displayname
       
   194                     aResult = aDisplayname.Create( resultStr.Left( index ) );
       
   195                     }            
       
   196                 }
       
   197              }
       
   198         }
       
   199     else
       
   200         {
       
   201         // Invalid data length
       
   202         aResult = KErrArgument;
       
   203         }
       
   204 
       
   205     CleanupStack::PopAndDestroy( tempBuffer ); // CS:0
       
   206     }
       
   207 
       
   208 // ---------------------------------------------------------------------------
       
   209 // Check original address for spaces. 
       
   210 // ---------------------------------------------------------------------------
       
   211 //
       
   212 void CSvtUriParser::CheckForSpacesL(
       
   213     const TDesC& aOriginal, 
       
   214     RBuf& aCheckedAddress ) const
       
   215     {
       
   216     aCheckedAddress.CreateL( aOriginal );
       
   217     
       
   218     TInt index( 0 );
       
   219     while ( index != KErrNotFound )
       
   220         {
       
   221         index = aCheckedAddress.Locate( KSpaceMark );
       
   222         
       
   223         // Remove space if itīs in begin or end of string
       
   224         if ( index == 0 || ( index == ( aCheckedAddress.Length() - 1 ) ) )
       
   225             {
       
   226             aCheckedAddress.Delete( index, 1 );
       
   227             }
       
   228         else
       
   229             {
       
   230             index = KErrNotFound;
       
   231             }
       
   232         }
       
   233     }
       
   234 
       
   235 // ---------------------------------------------------------------------------
       
   236 // Check is string valid for gsm call
       
   237 // ---------------------------------------------------------------------------
       
   238 //
       
   239 TBool CSvtUriParser::IsValidGsmNumber( const TDesC& aOriginal ) const
       
   240     {
       
   241     TLex input( aOriginal );
       
   242     TPtrC valid( KPhoneValidChars );
       
   243 
       
   244     while ( valid.Locate( input.Peek() ) != KErrNotFound )
       
   245         {
       
   246         input.Inc();
       
   247         }
       
   248   
       
   249     return !input.Remainder().Length();
       
   250     }
       
   251 
       
   252 // ---------------------------------------------------------------------------
       
   253 // Gets username part from sip uri
       
   254 // ---------------------------------------------------------------------------
       
   255 //
       
   256 TInt CSvtUriParser::GetUserNamePart( 
       
   257         const TDesC& aOriginal,
       
   258         TDes& aUserName ) const
       
   259     {
       
   260     if ( aOriginal.Length() > aUserName.MaxLength() )
       
   261         {
       
   262         return KErrArgument;
       
   263         }
       
   264     
       
   265     aUserName.Copy( aOriginal );
       
   266     
       
   267     // Parse protocol prefix and domain part out of a VoIP call URI
       
   268     TInt pos( aUserName.Find( KSvtColon ) );
       
   269     if ( pos > KErrNotFound )
       
   270         {
       
   271         aUserName.Delete( 0, pos+1 );
       
   272         }                
       
   273         
       
   274     pos = aUserName.Find( KSvtAt );
       
   275     if ( pos > KErrNotFound )
       
   276         {
       
   277         aUserName.Delete( pos, aUserName.Length() - pos );
       
   278         }
       
   279     
       
   280     if ( 0 == aUserName.Length() )
       
   281         {
       
   282         return KErrNotFound;
       
   283         }
       
   284     
       
   285     return KErrNone;
       
   286     }
       
   287 
       
   288 // ---------------------------------------------------------------------------
       
   289 // Handles sip uri's username part for ignore domain part values 1 and 2
       
   290 // ---------------------------------------------------------------------------
       
   291 //
       
   292 void CSvtUriParser::HandleUserNamePartL( 
       
   293         TInt aIgnoreDomain, 
       
   294         const TDesC& aOriginal, 
       
   295         RBuf& aParsedAddress ) const
       
   296     {
       
   297     if ( 0 == aOriginal.Length() )
       
   298         {
       
   299         User::Leave( KErrArgument );
       
   300         }
       
   301     
       
   302     HBufC* buf = HBufC::NewLC( aOriginal.Length() );
       
   303     TPtr des( buf->Des() );
       
   304     
       
   305     User::LeaveIfError( GetUserNamePart( aOriginal, des ) );
       
   306     
       
   307     aParsedAddress.Close();
       
   308     
       
   309     if ( ( 1 == aIgnoreDomain && IsValidGsmNumber( des ) ) ||
       
   310            2 == aIgnoreDomain )
       
   311         {
       
   312         // Set parsed address
       
   313         User::LeaveIfError( aParsedAddress.Create( des ) );
       
   314         }
       
   315     else
       
   316         {
       
   317         User::LeaveIfError( aParsedAddress.Create( aOriginal ) );
       
   318         }
       
   319         
       
   320     CleanupStack::PopAndDestroy( buf );
       
   321     }
       
   322 
       
   323