PECengine/AttributeLibrary2/SrcTransactions/PEngAddressUtils.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2004 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:  Tools for matching & handling presence addresses & user id's.
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <E32Std.h>
       
    20 #include "PEngAddressUtils.h"
       
    21 #include "PresenceDebugPrint.h"
       
    22 
       
    23 
       
    24 // CONSTANTS
       
    25 /**
       
    26  * WV schema prefix & length.
       
    27  */
       
    28 _LIT( KWVSchemaPrefix, "wv:" );
       
    29 const TInt KWVSchemaPrefixLength = 3;
       
    30 
       
    31 
       
    32 /**
       
    33  * Resource separator "/" & length.
       
    34  */
       
    35 _LIT( KWVResourceSeparator, "/" );
       
    36 const TInt KWVResourceSeparatorLength = 1;
       
    37 
       
    38 
       
    39 /**
       
    40  * Domain separator "@" & length.
       
    41  */
       
    42 _LIT( KWVDomainSeparator, "@" );
       
    43 const TInt KWVDomainSeparatorLength = 1;
       
    44 
       
    45 
       
    46 
       
    47 
       
    48 // ============================ MEMBER FUNCTIONS ===============================
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // PEngAddressUtils::WVUserId()
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 TPtrC PEngAddressUtils::WVUserId( const TDesC& aWVAddress )
       
    55     {
       
    56     TPtrC userId( aWVAddress );
       
    57     if ( aWVAddress.Left( KWVSchemaPrefixLength ).CompareF( KWVSchemaPrefix ) == 0 )
       
    58         {
       
    59         userId.Set( aWVAddress.Mid( KWVSchemaPrefixLength ) );
       
    60         }
       
    61 
       
    62 
       
    63     TInt resourceStartPos = userId.Find( KWVResourceSeparator );
       
    64     if ( resourceStartPos != KErrNotFound )
       
    65         {
       
    66         userId.Set( userId.Left( resourceStartPos ) );
       
    67         }
       
    68 
       
    69     else
       
    70         {
       
    71         TInt domainStartPos = userId.Find( KWVDomainSeparator );
       
    72         if ( domainStartPos != KErrNotFound )
       
    73             {
       
    74             userId.Set( userId.Left( domainStartPos ) );
       
    75             }
       
    76         }
       
    77 
       
    78 
       
    79     return userId;
       
    80     }
       
    81 
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // PEngAddressUtils::WVResourceId()
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 TPtrC PEngAddressUtils::WVResourceId( const TDesC& aWVAddress )
       
    88     {
       
    89     TPtrC resourceId( NULL, 0 );
       
    90 
       
    91     TInt resourceStartPos = aWVAddress.Find( KWVResourceSeparator );
       
    92     if ( resourceStartPos != KErrNotFound )
       
    93         {
       
    94         resourceId.Set( aWVAddress.Mid( resourceStartPos + KWVResourceSeparatorLength ) );
       
    95 
       
    96 
       
    97         TInt domainStartPos = resourceId.Find( KWVDomainSeparator );
       
    98         if ( domainStartPos != KErrNotFound )
       
    99             {
       
   100             resourceId.Set( resourceId.Left( domainStartPos ) );
       
   101             }
       
   102         }
       
   103 
       
   104 
       
   105     return resourceId;
       
   106     }
       
   107 
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // PEngAddressUtils::WVDomain()
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 TPtrC PEngAddressUtils::WVDomain( const TDesC& aWVAddress )
       
   114     {
       
   115     TInt domainStartPos = aWVAddress.Find( KWVDomainSeparator );
       
   116     if ( domainStartPos == KErrNotFound )
       
   117         {
       
   118         return KNullDesC();
       
   119         }
       
   120 
       
   121     else
       
   122         {
       
   123         return aWVAddress.Mid( domainStartPos + KWVDomainSeparatorLength );
       
   124         }
       
   125     }
       
   126 
       
   127 
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // PEngAddressUtils::ReducedWVAddress()
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 
       
   134 TPtrC PEngAddressUtils::ReducedWVAddress( const TDesC& aWVAddress )
       
   135     {
       
   136     TPtrC prefix( aWVAddress.Left( KWVSchemaPrefixLength ) );
       
   137     if ( prefix.CompareF( KWVSchemaPrefix ) == 0 )
       
   138         {
       
   139         //has prefix
       
   140         return aWVAddress.Mid( KWVSchemaPrefixLength );
       
   141         }
       
   142     else
       
   143         {
       
   144         return aWVAddress;
       
   145         }
       
   146     }
       
   147 
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // PEngAddressUtils::CanonicWVAddressLC()
       
   151 // -----------------------------------------------------------------------------
       
   152 //
       
   153 HBufC* PEngAddressUtils::CanonicWVAddressLC( const TDesC& aUserId,
       
   154                                              const TDesC& aResourceId,
       
   155                                              const TDesC& aDomain )
       
   156     {
       
   157     const TInt staticAddressLength = KWVSchemaPrefixLength +
       
   158                                      KWVResourceSeparatorLength +
       
   159                                      KWVDomainSeparatorLength;
       
   160 
       
   161     const TInt dynIdPartLength = aUserId.Length() +
       
   162                                  aResourceId.Length();
       
   163 
       
   164     __ASSERT_ALWAYS( dynIdPartLength > 0, User::Leave( KErrArgument ) );
       
   165 
       
   166 
       
   167 
       
   168     HBufC* addressBuf = HBufC::NewLC( staticAddressLength + dynIdPartLength + aDomain.Length() );
       
   169     TPtr address = addressBuf->Des();
       
   170 
       
   171     address.Append( KWVSchemaPrefix );
       
   172     address.Append( aUserId );      //empty user id can be always added into start
       
   173 
       
   174     if ( aResourceId.Length() > 0 )
       
   175         {
       
   176         address.Append( KWVResourceSeparator );
       
   177         address.Append( aResourceId );
       
   178         }
       
   179 
       
   180     if ( aDomain.Length() > 0 )
       
   181         {
       
   182         address.Append( KWVDomainSeparator );
       
   183         address.Append( aDomain );
       
   184         }
       
   185 
       
   186     return addressBuf;
       
   187     }
       
   188 
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // PEngAddressUtils::MatchWVAddressesF()
       
   192 // -----------------------------------------------------------------------------
       
   193 //
       
   194 TBool PEngAddressUtils::MatchWVAddressesF( const TDesC& aWVAddress1,
       
   195                                            const TDesC& aWVAddress2,
       
   196                                            const TDesC& aMatchDomain )
       
   197     {
       
   198     //User ID's must match exactly
       
   199     TPtrC pureUserId1 = WVUserId( aWVAddress1 );
       
   200     TPtrC pureUserId2 = WVUserId( aWVAddress2 );
       
   201     if ( pureUserId1.CompareF( pureUserId2 ) != 0 )
       
   202         {
       
   203         return EFalse;
       
   204         }
       
   205 
       
   206 
       
   207     //Resource ID's must match exactly
       
   208     TPtrC pureResourceId1 = WVResourceId( aWVAddress1 );
       
   209     TPtrC pureResourceId2 = WVResourceId( aWVAddress2 );
       
   210     if ( pureResourceId1.CompareF( pureResourceId2 ) != 0 )
       
   211         {
       
   212         return EFalse;
       
   213         }
       
   214 
       
   215 
       
   216     //Domains _may_ match exactly
       
   217     TPtrC pureDomain1 = WVDomain( aWVAddress1 );
       
   218     TPtrC pureDomain2 = WVDomain( aWVAddress2 );
       
   219     if ( pureDomain1.CompareF( pureDomain2 ) == 0 )
       
   220         {
       
   221         return ETrue;
       
   222         }
       
   223 
       
   224 
       
   225     //Or they might match after domain extended / reduced match
       
   226     if ( aMatchDomain.Length() > 0 )
       
   227         {
       
   228             {
       
   229             //If first id is local address (from aMatchDomain)
       
   230             //And if second id is from aMatchDomain ==> match.
       
   231             TBool id1LocalAddress = ( pureDomain1.Length() == 0 );
       
   232             TBool id2FromMatchDomain = ( pureDomain2.CompareF( aMatchDomain ) == 0 );
       
   233             if ( id1LocalAddress && id2FromMatchDomain )
       
   234                 {
       
   235                 return ETrue;
       
   236                 }
       
   237             }
       
   238 
       
   239             {
       
   240             //If second id is local address (from aMatchDomain)
       
   241             //And if first id is from aMatchDomain ==> match.
       
   242             TBool id2LocalAddress = ( pureDomain2.Length() == 0 );
       
   243             TBool id1FromMatchDomain = ( pureDomain1.CompareF( aMatchDomain ) == 0 );
       
   244             if ( id2LocalAddress && id1FromMatchDomain )
       
   245                 {
       
   246                 return ETrue;
       
   247                 }
       
   248             }
       
   249         }
       
   250 
       
   251     //no match so far
       
   252     return EFalse;
       
   253     }
       
   254 
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // PEngAddressUtils::FindWVAddressF()
       
   258 // -----------------------------------------------------------------------------
       
   259 //
       
   260 TInt PEngAddressUtils::FindWVAddressF( const TDesC& aWVAddress,
       
   261                                        const MDesCArray& aWVAddresses,
       
   262                                        const TDesC& aMatchDomain,
       
   263                                        TPEngAddressMatchdMode aMode )
       
   264     {
       
   265     PENG_DP( D_PENG_LIT( "PEngAddressUtils::FindWVAddressF( [%S] from [%d] in domain [%S]" ),
       
   266              &aWVAddress, aWVAddresses.MdcaCount(), &aMatchDomain );
       
   267 
       
   268 
       
   269     //Try first direct match
       
   270     const TInt arrayCount( aWVAddresses.MdcaCount() );
       
   271     for ( TInt index( 0 ); index < arrayCount; index++ )
       
   272         {
       
   273         const TPtrC candidate( aWVAddresses.MdcaPoint( index ) );
       
   274         if ( MatchWVAddressesF( aWVAddress, candidate, aMatchDomain ) )
       
   275             {
       
   276 #ifdef PENG_ENABLE_DEBUG_PRINT
       
   277             TPtrC matchedAddress = aWVAddresses.MdcaPoint( index );
       
   278             PENG_DP( D_PENG_LIT( "PEngAddressUtils::FindWVAddressF( normal match to %d: [%S]" ),
       
   279                      index, &matchedAddress );
       
   280 #endif //PENG_ENABLE_DEBUG_PRINT
       
   281 
       
   282 
       
   283 
       
   284             return index;
       
   285             }
       
   286         }
       
   287 
       
   288     //conventional find didn't succeed
       
   289     TInt uniqueMatchIndex = KErrNotFound;
       
   290     if ( aMode == EMatchToUniqueId )
       
   291         {
       
   292         // Do the extended matching if possible:
       
   293         // - no domain in searched id
       
   294         // - no match domain available
       
   295         // - searched id / resource match to unique id / resource in array ==> match
       
   296 
       
   297         if ( ( WVDomain( aWVAddress ).Length() == 0 ) && ( aMatchDomain.Length() == 0 ) )
       
   298             {
       
   299             const TPtrC pureSearchUserId = WVUserId( aWVAddress );
       
   300             const TPtrC pureSearchResourceId = WVResourceId( aWVAddress );
       
   301 
       
   302 
       
   303             const TInt arrayCount( aWVAddresses.MdcaCount() );
       
   304             for ( TInt index( 0 ); index < arrayCount; index++ )
       
   305                 {
       
   306                 const TPtrC candidateUserId = WVUserId( aWVAddresses.MdcaPoint( index ) );
       
   307                 const TPtrC candidateResourceId = WVResourceId( aWVAddresses.MdcaPoint( index ) );
       
   308 
       
   309                 TBool userIdsMatch = ( pureSearchUserId.CompareF( candidateUserId ) == 0 );
       
   310                 TBool resourceIdsMatch = ( pureSearchResourceId.CompareF( candidateResourceId ) == 0 );
       
   311 
       
   312                 if ( userIdsMatch && resourceIdsMatch )
       
   313                     {
       
   314                     if ( uniqueMatchIndex == KErrNotFound )
       
   315                         {
       
   316                         //first match, remember the index
       
   317                         uniqueMatchIndex = index;
       
   318                         }
       
   319                     else
       
   320                         {
       
   321                         //subsequent match, previous match isn't unique anymore
       
   322                         return KErrNotFound;
       
   323                         }
       
   324                     }
       
   325                 }
       
   326 
       
   327 #ifdef PENG_ENABLE_DEBUG_PRINT
       
   328             if ( uniqueMatchIndex != KErrNotFound )
       
   329                 {
       
   330                 TPtrC matchedAddress = aWVAddresses.MdcaPoint( uniqueMatchIndex );
       
   331                 PENG_DP( D_PENG_LIT( "PEngAddressUtils::FindWVAddressF( Unique match to %d: [%S]" ),
       
   332                          uniqueMatchIndex, &matchedAddress );
       
   333                 }
       
   334 
       
   335 #endif //PENG_ENABLE_DEBUG_PRINT
       
   336             }
       
   337         }
       
   338 
       
   339 
       
   340     //uniqueMatchIndex is either the index to unique match or KErrNotFound
       
   341     //if no match was found / or whole matching wasn't performed
       
   342     return uniqueMatchIndex;
       
   343     }
       
   344 
       
   345 
       
   346 //  End of File
       
   347