PECengine/JanitorPlugin2/Src/PEngContactIdTools.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     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:  WV ID handling tools
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 
       
    20 #include <e32std.h>
       
    21 #include "PEngContactIdTools.h"
       
    22 
       
    23 // CONSTANTS
       
    24 _LIT( KPengWVIDPrefix, "wv:" );
       
    25 const TInt KPengWVIDPrefixLength( 3 );
       
    26 _LIT( KCharAdd, "@" );
       
    27 
       
    28 // -----------------------------------------------------------------------------
       
    29 // FindContactIdInArray()
       
    30 // Find Contact Id in the aray
       
    31 // (other items were commented in a header).
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 GLREF_C  TInt FindContactIdInArray(
       
    35     const MDesCArray& aArray,
       
    36     const TDesC& aId )
       
    37     {
       
    38     TInt count ( aArray.MdcaCount() );
       
    39     for ( TInt x( 0 ) ; x < count ; x++ )
       
    40         {
       
    41         TInt err ( CompareContactIds( aArray.MdcaPoint( x ), aId ) );
       
    42         if ( err == KErrNone )
       
    43             {
       
    44             return x;
       
    45             }
       
    46         if ( err > 0 ) // no sense to look further in the array
       
    47             {
       
    48             return KErrNotFound;
       
    49             }
       
    50         }
       
    51     return KErrNotFound;
       
    52     }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // AddContactIToArrayL()
       
    56 // Add contact Id to the array of the contact Ids
       
    57 // (other items were commented in a header).
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 GLREF_C TInt AddContactIdToArrayL(
       
    61     CDesCArray& aArray,
       
    62     const TDesC& aId )
       
    63     {
       
    64     // insert the id in sequence
       
    65     TInt count ( aArray.MdcaCount() );
       
    66     for ( TInt x( 0 ) ; x < count ; x++ )
       
    67         {
       
    68         TInt err ( CompareContactIds( aArray.MdcaPoint( x ), aId ) );
       
    69         if ( err > 0 )
       
    70             {
       
    71             aArray.InsertL( x, aId  );
       
    72             return x;
       
    73             }
       
    74         if ( err == 0 )
       
    75             {
       
    76             return KErrAlreadyExists;
       
    77             }
       
    78         // go for another loop
       
    79         }
       
    80     // was not inserted, then add it to the end of the array
       
    81     aArray.AppendL( aId );
       
    82     return count;
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CompareContactIs()
       
    87 // Compare two contact Ids
       
    88 // (other items were commented in a header).
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 GLREF_C TInt CompareContactIds(
       
    92     const TDesC& aId1,
       
    93     const TDesC& aId2 )
       
    94     {
       
    95     // get name without wv: part on the beginning
       
    96     TPtrC id1( aId1 );
       
    97     TPtrC id2( aId2 );
       
    98 
       
    99     if ( aId1.Left( KPengWVIDPrefixLength ).CompareF( KPengWVIDPrefix ) == KErrNone )
       
   100         {
       
   101         id1.Set( aId1.Mid( KPengWVIDPrefixLength ) );
       
   102         }
       
   103 
       
   104     if ( aId2.Left( KPengWVIDPrefixLength ).CompareF( KPengWVIDPrefix ) == KErrNone )
       
   105         {
       
   106         id2.Set( aId2.Mid( KPengWVIDPrefixLength ) );
       
   107         }
       
   108 
       
   109     // fix 21.04.2005 OK:
       
   110     // check how it goes with the domains, if they are different
       
   111     // no match, if one missing and base id is same, then same ids
       
   112     TInt err ( 	id1.CompareF( id2 ) );
       
   113 
       
   114     if ( err == KErrNone )
       
   115         {
       
   116         return err;
       
   117         }
       
   118 
       
   119     // try to compare them without domain
       
   120     // if home domain info is missing
       
   121     // or wv id domain is same as home one, remove it from the wv id
       
   122     TInt offset1( id1.Find( KCharAdd ) );
       
   123     TInt offset2( id2.Find( KCharAdd ) );
       
   124 
       
   125     // if both are with domain, then they are not same
       
   126     if ( ( offset1 != KErrNotFound ) && ( offset2 != KErrNotFound ) )
       
   127         {
       
   128         return err;
       
   129         }
       
   130     // remove domains from wv id1
       
   131     TInt domainCount( 0 );
       
   132     if ( KErrNotFound != offset1 )
       
   133         {
       
   134         id1.Set( id1.Left( offset1 ) ); // cause we need length
       
   135         domainCount++;
       
   136         }
       
   137     // remove domains from wv id2
       
   138     if ( KErrNotFound != offset2 )
       
   139         {
       
   140         id2.Set( id2.Left( offset2 ) ); // cause we need length
       
   141         domainCount++;
       
   142         }
       
   143 
       
   144     // if none of then or both have domain, no need to compare
       
   145     if ( ( !domainCount ) || ( domainCount == 2 ) )
       
   146         {
       
   147         return err;
       
   148         }
       
   149     return id1.CompareF( id2 );
       
   150     }
       
   151 
       
   152 //  End of File