PECengine/ListLibrary2/ToolsSrc/PEngContactIdsTools.cpp
branchRCL_3
changeset 13 a941bc465d9f
parent 0 094583676ce7
equal deleted inserted replaced
12:6ca72c0fe49a 13:a941bc465d9f
       
     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:  Contact Id handling tools
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 
       
    20 #include <e32std.h>
       
    21 #include "PEngContactIdsTools.h"
       
    22 
       
    23 
       
    24 // ================ LOCAL FUNCTIONS ============================================
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // AssertDomain()
       
    28 // Checks domain of the contact id
       
    29 // If domain is same as passed, it is removed from the id.
       
    30 //
       
    31 //
       
    32 // @param aContactId The contact id from where to check the domain.
       
    33 // @param aUserDomain The user domain.
       
    34 // @param aDomain Return paramater for extracted domain.
       
    35 // @return 1 if the domain was updated. Else 0.
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 TInt DoAssertDomain( TPtrC& aContactId,
       
    39                      const TDesC& aUserDomain,
       
    40                      TPtrC& aDomain )
       
    41     {
       
    42     TChar domainSeparator( '@' );
       
    43 
       
    44     TInt offset( aContactId.Locate( domainSeparator ) );
       
    45     if ( ( KErrNotFound != offset ) &&
       
    46          ( !aUserDomain.Length()    ||
       
    47            ( KErrNone == aContactId.Mid( offset ).CompareF( aUserDomain ) ) ) )
       
    48         {
       
    49         aDomain.Set( aContactId.Mid( offset + 1 ) ); // storing the domain too
       
    50         aContactId.Set( aContactId.Left( offset ) ); // cause we need length
       
    51         return 1; // as described in description
       
    52         }
       
    53 
       
    54     return 0; // nothing was updated
       
    55     }
       
    56 
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CleanContactListName()
       
    60 // Removes slash from the beginning of the contact list id if it is there
       
    61 //
       
    62 // @param aCntListId Contact list id to clean.
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 void DoCleanContactListName( TPtrC& aCntListId )
       
    66     {
       
    67     // look for the slash as first character->1
       
    68     if ( aCntListId.Left( 1 ).Compare( KPEngWVResourceSeparator ) == KErrNone )
       
    69         {
       
    70         // ignore slash on the beginning, 1 character
       
    71         aCntListId.Set( aCntListId.Mid( 1 ) );
       
    72         }
       
    73     }
       
    74 
       
    75 
       
    76 
       
    77 // -----------------------------------------------------------------------------
       
    78 // DoCompareContactIdsCommon()
       
    79 // Compare two Contact Ids
       
    80 //
       
    81 // @param aContactId1 Contact Id 1. Possible "WV:" prefix is stripped away.
       
    82 // @param aContactId2 Contact Id 2. Possible "WV:" prefix is stripped away.
       
    83 // @param aUserDomain user domain
       
    84 // @return KErrNone if they contact IDs are same, other non zero number if not
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 TInt DoCompareContactIdsCommon( TPtrC& aContactId1,
       
    88                                 TPtrC& aContactId2 )
       
    89     {
       
    90     // get name without wv: part on the beginning
       
    91     if ( KErrNone
       
    92          ==
       
    93          aContactId1.Left( KPEngWVIDPrefixLength ).CompareF( KPEngWVIDPrefix ) )
       
    94         {
       
    95         aContactId1.Set( aContactId1.Mid( KPEngWVIDPrefixLength ) );
       
    96         }
       
    97 
       
    98     if ( KErrNone
       
    99          ==
       
   100          aContactId2.Left( KPEngWVIDPrefixLength ).CompareF( KPEngWVIDPrefix ) )
       
   101         {
       
   102         aContactId2.Set( aContactId2.Mid( KPEngWVIDPrefixLength ) );
       
   103         }
       
   104 
       
   105     return aContactId1.CompareF( aContactId2 );
       
   106     }
       
   107 
       
   108 
       
   109 
       
   110 // ============================ MEMBER FUNCTIONS ===============================
       
   111 
       
   112 // -----------------------------------------------------------------------------
       
   113 // NContactIdsTools::FindContactIdInArray()
       
   114 // -----------------------------------------------------------------------------
       
   115 //
       
   116 TInt NContactIdsTools::FindContactIdInArray( const MDesCArray& aArray,
       
   117                                              const TDesC& aContactId,
       
   118                                              TInt& aIndex,
       
   119                                              const TDesC& aUserDomain )
       
   120     {
       
   121     TInt l( 0 );
       
   122     TInt r( aArray.MdcaCount() );
       
   123     TInt ret( KErrNotFound );
       
   124     while ( r > l )
       
   125         {
       
   126         TInt inx = ( l + r ) >> 1;
       
   127         TInt k ( CompareContactIds( aContactId,
       
   128                                     aArray.MdcaPoint( inx ),
       
   129                                     aUserDomain ) );
       
   130         if ( k == 0 )
       
   131             {
       
   132             aIndex = inx;
       
   133             return KErrNone;
       
   134             }
       
   135         else if ( k > 0 )
       
   136             l = inx + 1;
       
   137         else
       
   138             r = inx;
       
   139         }
       
   140     aIndex = r;
       
   141     return ret;
       
   142     }
       
   143 
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // NContactIdsTools::FindContactIdInArray()
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 TInt NContactIdsTools::FindContactIdInArray( const MDesCArray& aArray,
       
   150                                              const TDesC& aContactId,
       
   151                                              const TDesC& aUserDomain )
       
   152     {
       
   153     TInt index( 0 );
       
   154     if ( KErrNone == NContactIdsTools::FindContactIdInArray( aArray,
       
   155                                                              aContactId,
       
   156                                                              index,
       
   157                                                              aUserDomain ) )
       
   158         {
       
   159         return index;
       
   160         }
       
   161 
       
   162     return KErrNotFound;
       
   163     }
       
   164 
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // NContactIdsTools::AddContactIdToArrayL()
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 TInt NContactIdsTools::AddContactIdToArrayL( CDesCArray& aArray,
       
   171                                              const TDesC& aContactId,
       
   172                                              const TDesC& aUserDomain )
       
   173     {
       
   174     TInt index( 0 );
       
   175     if ( KErrNone == NContactIdsTools::FindContactIdInArray( aArray,
       
   176                                                              aContactId,
       
   177                                                              index,
       
   178                                                              aUserDomain ) )
       
   179         {
       
   180         return KErrAlreadyExists;
       
   181         }
       
   182 
       
   183     aArray.InsertL( index , aContactId );
       
   184     return index;
       
   185     }
       
   186 
       
   187 
       
   188 // -----------------------------------------------------------------------------
       
   189 // NContactIdsTools::RemoveContactIdFromArray()
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 TInt NContactIdsTools::RemoveContactIdFromArray( CDesCArray& aArray,
       
   193                                                  const TDesC& aContactId,
       
   194                                                  const TDesC& aUserDomain )
       
   195     {
       
   196     TInt posToDelete( NContactIdsTools::FindContactIdInArray( aArray,
       
   197                                                               aContactId,
       
   198                                                               aUserDomain ) );
       
   199     if ( posToDelete != KErrNotFound )
       
   200         {
       
   201         aArray.Delete( posToDelete );
       
   202         return KErrNone;
       
   203         }
       
   204 
       
   205     return KErrNotFound;
       
   206     }
       
   207 
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // NContactIdsTools::CompareWVIDs()
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 TInt NContactIdsTools::CompareContactIds( const TDesC& aContactId1,
       
   214                                           const TDesC& aContactId2,
       
   215                                           const TDesC& aUserDomain )
       
   216     {
       
   217     // get contact ids without wv: prefix
       
   218     TPtrC contactId1( aContactId1 );
       
   219     TPtrC contactId2( aContactId2 );
       
   220 
       
   221     TInt err = DoCompareContactIdsCommon( contactId1, contactId2 );
       
   222     if ( err == KErrNone )
       
   223         {
       
   224         return KErrNone;
       
   225         }
       
   226 
       
   227     // try to compare them without domain if domain is missing or is same as user one
       
   228     // remove domain from contactId1
       
   229     TPtrC domain1;
       
   230     TPtrC domain2;
       
   231     TInt domainCount = DoAssertDomain( contactId1, aUserDomain, domain1 );
       
   232     domainCount += DoAssertDomain( contactId2, aUserDomain, domain2 );
       
   233 
       
   234     // if none of then or both have domain, no need to compare
       
   235     if ( ( domainCount == 0 ) ||
       
   236          ( domainCount == 2 ) )
       
   237         {
       
   238         // compare domains, if match continue, else return the error
       
   239         if ( domain1.CompareF( domain2 ) != KErrNone )
       
   240             {
       
   241             return err;
       
   242             }
       
   243         }
       
   244 
       
   245     return contactId1.CompareF( contactId2 );
       
   246     }
       
   247 
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // NContactListIdsTools::ExtractPureContactListName()
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 void NContactListIdsTools::ExtractPureContactListName( TPtrC& aContactListId,
       
   254                                                        const TDesC& aPlainContactId )
       
   255     {
       
   256     TChar domainSeparator( '@' );
       
   257     TInt offset( aContactListId.Locate( domainSeparator ) );
       
   258 
       
   259     // remove domain
       
   260     if ( KErrNotFound != offset )
       
   261         {
       
   262         aContactListId.Set( aContactListId.Left( offset ) );
       
   263         }
       
   264 
       
   265     // remove wv prefix
       
   266     if ( KErrNone
       
   267          ==
       
   268          aContactListId.Left( KPEngWVIDPrefixLength ).CompareF( KPEngWVIDPrefix ) )
       
   269         {
       
   270         aContactListId.Set( aContactListId.Mid( KPEngWVIDPrefixLength ) );
       
   271         }
       
   272 
       
   273 
       
   274     // remove user part
       
   275     if ( KErrNone
       
   276          ==
       
   277          aContactListId.Left( aPlainContactId.Length() ).CompareF( aPlainContactId ) )
       
   278         {
       
   279         aContactListId.Set( aContactListId.Mid( aPlainContactId.Length() ) );
       
   280         }
       
   281     }
       
   282 
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // NContactListIdsTools::CompareContactListIds()
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 TInt NContactListIdsTools::CompareContactListIds( const TDesC& aContactListId1,
       
   289                                                   const TDesC& aContactListId2 )
       
   290     {
       
   291     // extract pure list name, free from domain and possibly user part
       
   292     TPtrC id1( aContactListId1 );
       
   293     TPtrC id2( aContactListId2 );
       
   294     DoCleanContactListName( id1 );
       
   295     DoCleanContactListName( id2 );
       
   296 
       
   297     // do the compare
       
   298     return id1.CompareF( id2 );
       
   299     }
       
   300 
       
   301 //  End of File