sipplugins/sippsipsettingsui/src/sipserversettingvalidator.cpp
changeset 0 307788aac0a8
child 1 dd3853b8dc3f
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 /*
       
     2 * Copyright (c) 2005 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:  Validation methods for checking username etc.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include    <StringLoader.h>
       
    21 #include    <escapeutils.h>
       
    22 #include    <aknutils.h>
       
    23 #include    <sipstrings.h>
       
    24 #include    <sipaddress.h>
       
    25 #include    <gssipsettingspluginrsc.rsg> //GUI Resource
       
    26 #include    "sipserversettingvalidator.h"
       
    27 #include    "tsipsettingsdata.h"
       
    28 #include    "gssipprofileutil.h"
       
    29 #include    "gssippluginlogger.h"
       
    30 
       
    31 // ============================ MEMBER FUNCTIONS ===============================
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // SIPServerSettingValidator::SIPServerSettingValidator
       
    35 // C++ default constructor can NOT contain any code, that
       
    36 // might leave.
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 SIPServerSettingValidator::SIPServerSettingValidator()
       
    40     {
       
    41     __GSLOGSTRING("SIPServerSettingValidator::SIPServerSettingValidator" )
       
    42     }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // SIPServerSettingValidator::ValidateServerUsernameL
       
    46 // Validates proxy/registrar server username
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 TBool SIPServerSettingValidator::ValidateServerUsernameL( 
       
    50     TAny* aDes )
       
    51     {  
       
    52     __GSLOGSTRING("SIPServerSettingValidator::ValidateServerUsernameL" )
       
    53     __ASSERT_ALWAYS( aDes != NULL, User::Leave( KErrArgument ) );
       
    54     TBool result( EFalse );
       
    55     TDes* desPtr = static_cast<TDes*>( aDes );
       
    56     TDes& des = *desPtr;
       
    57     
       
    58 	if ( des.Length() == 0 )    
       
    59 		{
       
    60 		return ETrue;
       
    61 		}
       
    62 		
       
    63     SIPStrings::OpenL();    // Needed for CSIPAddress.
       
    64      
       
    65     AknTextUtils::StripCharacters( des, KAknStripListControlChars );
       
    66     
       
    67     // Check that scheme is found from setting data.
       
    68     TBool protocolFound( EFalse );
       
    69     TBuf<KSIPProtocolHeaderLength + 1> sipHeader( KSIPProtocolHeader );
       
    70     sipHeader.Append( KSIPColonSeparator );
       
    71     TBuf<KSIPSProtocolHeaderLength + 1> sipsHeader( KSIPSProtocolHeader );
       
    72     sipsHeader.Append( KSIPColonSeparator );
       
    73     
       
    74     if ( des.Length() > KSIPProtocolHeaderLength )
       
    75         {
       
    76         if ( !sipHeader.Compare( 
       
    77             des.Left( KSIPProtocolHeaderLength + 1 ) ) )
       
    78             {
       
    79             protocolFound = ETrue;
       
    80             }
       
    81         }
       
    82     
       
    83     if ( des.Length() > KSIPSProtocolHeaderLength )
       
    84         {
       
    85         if ( !sipsHeader.Compare( 
       
    86             des.Left( KSIPSProtocolHeaderLength + 1 ) ) )
       
    87             {
       
    88             protocolFound = ETrue;
       
    89             }
       
    90         }   
       
    91     
       
    92     // Create address to be validated.
       
    93     HBufC8* tmp = EscapeUtils::ConvertFromUnicodeToUtf8L( des );
       
    94     CleanupStack::PushL( tmp );
       
    95     HBufC8* conv = NULL; 
       
    96     
       
    97     if ( !protocolFound )
       
    98         {
       
    99         conv = HBufC8::NewLC( sipHeader.Length() + tmp->Length() );
       
   100         conv->Des().Copy( sipHeader );    
       
   101         conv->Des().Append( tmp->Des() );
       
   102         }
       
   103     else
       
   104         {
       
   105         conv = EscapeUtils::ConvertFromUnicodeToUtf8L( des );
       
   106         CleanupStack::PushL( conv );
       
   107         }    
       
   108         
       
   109     EscapeUtils::TEscapeMode mode = EscapeUtils::EEscapeNormal;
       
   110     HBufC8* enc = EscapeUtils::EscapeEncodeL( conv->Des(), mode );
       
   111     CleanupStack::PushL( enc );
       
   112     
       
   113     CSIPAddress* addr = CSIPAddress::DecodeL( enc->Des() );
       
   114     CleanupStack::PushL( addr );
       
   115    
       
   116     // Finally, validate entered host.
       
   117     if ( addr->Uri8().Uri().Validate() == KErrNone )
       
   118         {
       
   119         result = ETrue;
       
   120         }
       
   121    
       
   122     // PopAndDestroy all items in CleanupStack.
       
   123     CleanupStack::PopAndDestroy( addr ); 
       
   124     CleanupStack::PopAndDestroy( enc );
       
   125     CleanupStack::PopAndDestroy( conv );
       
   126     CleanupStack::PopAndDestroy( tmp );
       
   127     
       
   128     SIPStrings::Close();
       
   129     return result;
       
   130     }
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // SIPServerSettingValidator::ValidateServerAddressL
       
   134 // Validates proxy/registrar server address
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 TBool SIPServerSettingValidator::ValidateServerAddressL( TAny* aDes )
       
   138     {   
       
   139     __GSLOGSTRING("SIPServerSettingValidator::ValidateServerAddressL" )
       
   140     __ASSERT_ALWAYS( aDes != NULL, User::Leave( KErrArgument ) );
       
   141     TBool result( ETrue );
       
   142     TBool onlySpaces( ETrue );
       
   143     
       
   144     SIPStrings::OpenL();    // Needed for CSIPAddress.
       
   145     
       
   146     TDes* desPtr = static_cast<TDes*>( aDes );
       
   147     TDes& des = *desPtr;
       
   148 
       
   149     AknTextUtils::StripCharacters( des, KAknStripListControlChars );
       
   150 
       
   151     // Check that is there other characters than spaces.
       
   152     for ( TInt i = 0; i < des.Length() && onlySpaces; i++ )
       
   153         {
       
   154         const TChar KSpaceCharacter = ' ';
       
   155         if ( des[ i ] != KSpaceCharacter )
       
   156             {
       
   157             onlySpaces = EFalse;
       
   158             }
       
   159         }
       
   160     
       
   161     // Allow user set spaces, spaces are removed elsewhere.
       
   162     if ( !onlySpaces )
       
   163         {
       
   164         HBufC8* finalAddress = HBufC8::NewLC( KMaxProxyLength * 2 );
       
   165         
       
   166         // Convert address to 8-bit descriptor format.
       
   167         HBufC8* conv = EscapeUtils::ConvertFromUnicodeToUtf8L( des );
       
   168         CleanupStack::PushL( conv );
       
   169         
       
   170         // Parse address.
       
   171         TUriParser8 parser;
       
   172         User::LeaveIfError( parser.Parse( conv->Des() ) );
       
   173         
       
   174         // Check if scheme included and also if brackets in host correctly.
       
   175         TInt sipSchemeIncluded = 
       
   176         	GSSIPProfileUtil::SchemeIncluded( conv->Des(), ETrue );
       
   177         if ( sipSchemeIncluded == KErrNotFound )
       
   178         	{ 
       
   179         	// Brackets in host incorrectly.
       
   180         	result = EFalse;
       
   181         	}
       
   182         else if ( !sipSchemeIncluded && parser.IsPresent( EUriScheme ) )
       
   183 			{
       
   184 			// Scheme exists but it's not "sip" or "sips" scheme.
       
   185 			result = EFalse;
       
   186 			}
       
   187 		else if ( !sipSchemeIncluded )
       
   188 		    {
       
   189 		    // Create address for validation. Scheme is added.
       
   190 		    finalAddress->Des().Append( KSIPProtocolHeader );
       
   191 		    finalAddress->Des().Append( KSIPColonSeparator );
       
   192 		    finalAddress->Des().Append( conv->Des() );
       
   193 		    if ( GSSIPProfileUtil::URIValid( *finalAddress ) )
       
   194 		        {
       
   195 		        result = EFalse;
       
   196 		        }
       
   197 		    }
       
   198 		else
       
   199 			{
       
   200 			// Create address for validation.
       
   201 			finalAddress->Des().Append( conv->Des() );
       
   202 			if ( GSSIPProfileUtil::URIValid( *finalAddress ) )
       
   203 				{
       
   204 				result = EFalse;
       
   205 				}
       
   206 			}
       
   207 		CleanupStack::PopAndDestroy( conv );
       
   208 		
       
   209 		// Validate entered host.
       
   210     	if ( result )    	
       
   211     		{
       
   212     		User::LeaveIfError( parser.Parse( *finalAddress ) );
       
   213             if ( parser.Validate() != KErrNone )
       
   214                 {
       
   215                 result = EFalse;
       
   216                 }
       
   217     		}
       
   218         
       
   219         CleanupStack::PopAndDestroy( finalAddress );
       
   220         }
       
   221     
       
   222     SIPStrings::Close();
       
   223     return result;
       
   224     }
       
   225 
       
   226 //  End of File