voipplugins/ipapputils/src/csipclientresolverutils.cpp
branchRCL_3
changeset 21 f742655b05bf
parent 20 65a3ef1d5bd0
child 22 d38647835c2e
equal deleted inserted replaced
20:65a3ef1d5bd0 21:f742655b05bf
     1 /*
       
     2 * Copyright (c) 2007-2010 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:  SIP client resolver utility class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <centralrepository.h>
       
    20 #include <sipclientresolverconfigcrkeys.h>
       
    21 #include <spsettings.h>
       
    22 #include <spentry.h>
       
    23 #include <spproperty.h>
       
    24 
       
    25 #include "csipclientresolverutils.h"
       
    26 
       
    27 
       
    28 // ======== LOCAL FUNCTIONS ========
       
    29 // ======== MEMBER FUNCTIONS ========
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // 
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 CSipClientResolverUtils::CSipClientResolverUtils()
       
    36     {
       
    37     }
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // 
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43 void CSipClientResolverUtils::ConstructL()
       
    44     {
       
    45     iRepository = CRepository::NewL( KCRUidSIPClientResolverConfig );
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // 
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 EXPORT_C CSipClientResolverUtils* CSipClientResolverUtils::NewL()
       
    53     {
       
    54     CSipClientResolverUtils* self = CSipClientResolverUtils::NewLC();
       
    55     CleanupStack::Pop( self );
       
    56     return self;
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // 
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 EXPORT_C CSipClientResolverUtils* CSipClientResolverUtils::NewLC()
       
    64     {
       
    65     CSipClientResolverUtils* self = new( ELeave ) CSipClientResolverUtils;
       
    66     CleanupStack::PushL( self );
       
    67     self->ConstructL();
       
    68     return self;
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // 
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 EXPORT_C CSipClientResolverUtils::~CSipClientResolverUtils()
       
    76     {
       
    77     delete iRepository;
       
    78     }
       
    79 
       
    80 // ---------------------------------------------------------------------------
       
    81 // Add line to client resolver cenrep table
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 EXPORT_C void CSipClientResolverUtils::RegisterClientWithUserL(
       
    85     const TUid& aImplementation,
       
    86     const TDesC8& aProfileContactHeaderUser, 
       
    87     const TUid& aResolver ) const
       
    88     {
       
    89     __ASSERT_ALWAYS( aImplementation.iUid > 0, User::Leave( KErrArgument ) );
       
    90     __ASSERT_ALWAYS( aProfileContactHeaderUser.Length() > 0,
       
    91             User::Leave( KErrArgument ) );
       
    92     __ASSERT_ALWAYS( aResolver.iUid > 0, User::Leave( KErrArgument ));
       
    93 
       
    94     // Remove all earlier instances of client&user combination
       
    95     UnRegisterClientWithUserL( aImplementation, aProfileContactHeaderUser );
       
    96 
       
    97     User::LeaveIfError( 
       
    98         iRepository->StartTransaction( CRepository::EConcurrentReadWriteTransaction ) );
       
    99     TUint32 newKey = 0;
       
   100     CreateNewKeyL( newKey );
       
   101     // Add the new row
       
   102     TInt uid = aImplementation.iUid;
       
   103     User::LeaveIfError( 
       
   104         iRepository->Create(newKey|KSIPClientResolverClientUIDMask, uid ) );    
       
   105     User::LeaveIfError( 
       
   106         iRepository->Create(newKey|KSIPClientResolverUserNameMask, aProfileContactHeaderUser ) );
       
   107     uid = aResolver.iUid;    
       
   108     User::LeaveIfError( 
       
   109         iRepository->Create(newKey|KSIPClientResolverPluginUIDMask, uid ) ); 
       
   110 
       
   111     // Commit the transaction
       
   112     User::LeaveIfError( iRepository->CommitTransaction( newKey ) );
       
   113     }
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // Remove line from client resolver cenrep table
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 EXPORT_C void CSipClientResolverUtils::UnRegisterClientWithUserL(
       
   120     const TUid& aImplementation, 
       
   121     const TDesC8& aProfileContactHeaderUser ) const
       
   122     {
       
   123     __ASSERT_ALWAYS( aImplementation.iUid > 0, User::Leave( KErrArgument ) );
       
   124     __ASSERT_ALWAYS( aProfileContactHeaderUser.Length() > 0,
       
   125             User::Leave( KErrArgument ) );  
       
   126 
       
   127     RArray<TUint32> keys;
       
   128     CleanupClosePushL( keys );
       
   129     GetClientWithUserL( aProfileContactHeaderUser, keys );
       
   130     for ( TInt i = 0; i < keys.Count(); i++ )
       
   131         {        
       
   132         if ( CheckImplementationUidL( keys[ i ], aImplementation ) )
       
   133             {
       
   134             TUint32 errorKey;
       
   135             User::LeaveIfError( 
       
   136                 iRepository->StartTransaction( 
       
   137                     CRepository::EConcurrentReadWriteTransaction ) );
       
   138             iRepository->Delete( keys[i], KSIPClientResolverKeyMask, errorKey );
       
   139             // Commit the transaction
       
   140             User::LeaveIfError( iRepository->CommitTransaction( errorKey ) );            
       
   141             }
       
   142         }
       
   143     CleanupStack::PopAndDestroy( &keys );
       
   144     }
       
   145 
       
   146 // ---------------------------------------------------------------------------
       
   147 // Resolve correct implementation UID for contact header
       
   148 // ---------------------------------------------------------------------------
       
   149 //    
       
   150 EXPORT_C void CSipClientResolverUtils::GetImplementationUidWithUserL( 
       
   151     const TDesC8& aProfileContactHeaderUser,
       
   152     TUid& aImplementation ) const
       
   153     {
       
   154     RArray<TUint32> keys;    
       
   155     CleanupClosePushL( keys );
       
   156     // Find all rows where KSIPClientResolverUserNameMask is aProfileContactHeaderUser
       
   157     iRepository->FindEqL( KSIPClientResolverUserNameMask, 
       
   158                     KSIPClientResolverFieldTypeMask,
       
   159                     aProfileContactHeaderUser, keys );
       
   160     
       
   161     TInt implementationValue = KErrNotFound;
       
   162     TInt count( keys.Count() );
       
   163     if ( count > 1 )
       
   164         {
       
   165         // Call provider UIDs should be read from spsettings table and if some
       
   166         // UID found from spsettings match one uid in client resolver cenrep,
       
   167         // that should be returned to caller.
       
   168         RArray<TUint> uidArray;
       
   169         CleanupClosePushL( uidArray );
       
   170         CallProviderUidsL( uidArray );
       
   171         __ASSERT_ALWAYS( uidArray.Count() > 0, User::Leave( KErrNotFound ) );
       
   172         TInt i( 0 );
       
   173         for( ; i < count; i++ )
       
   174             {
       
   175             // Get implementation uid of found user name
       
   176             iRepository->Get( ( keys[i]^KSIPClientResolverUserNameMask ) |
       
   177                     KSIPClientResolverClientUIDMask, implementationValue );
       
   178             
       
   179             User::LeaveIfError( implementationValue );
       
   180             if ( KErrNotFound != uidArray.Find( implementationValue ) )
       
   181                 {
       
   182                 aImplementation.iUid = implementationValue;
       
   183                 break;
       
   184                 }
       
   185             }
       
   186         CleanupStack::PopAndDestroy( &uidArray );
       
   187         if ( i == count && aImplementation.iUid != implementationValue )
       
   188             {
       
   189             // No matching Uid found
       
   190             User::Leave( KErrNotFound );            
       
   191             }
       
   192         }
       
   193         
       
   194     else if ( count == 1 )
       
   195         {        
       
   196         // Get implementation uid of found user name
       
   197         iRepository->Get( ( keys[0]^KSIPClientResolverUserNameMask ) |
       
   198         KSIPClientResolverClientUIDMask, implementationValue );
       
   199 
       
   200         User::LeaveIfError( implementationValue );
       
   201         aImplementation.iUid = implementationValue;
       
   202         }
       
   203     else
       
   204         {
       
   205         User::Leave( KErrNotFound );
       
   206         }
       
   207 
       
   208     CleanupStack::PopAndDestroy( &keys );    
       
   209     }
       
   210 
       
   211 // ---------------------------------------------------------------------------
       
   212 // Return all implementation Uids bases on contact header
       
   213 // ---------------------------------------------------------------------------
       
   214 //    
       
   215 EXPORT_C void CSipClientResolverUtils::GetAllImplementationUidsWithUserL( 
       
   216     const TDesC8& aProfileContactHeaderUser,
       
   217     RArray<TUid>& aImplementationUids ) const
       
   218     {
       
   219     RArray<TUint32> keys;    
       
   220     CleanupClosePushL( keys );
       
   221     
       
   222     // Find all rows where KSIPClientResolverUserNameMask
       
   223     // is aProfileContactHeaderUser
       
   224     User::LeaveIfError( iRepository->FindEqL( KSIPClientResolverUserNameMask, 
       
   225         KSIPClientResolverFieldTypeMask,
       
   226         aProfileContactHeaderUser, keys ) );
       
   227     
       
   228     for( TInt i( 0 ) ; i < keys.Count() ; i++ )
       
   229         {
       
   230         TInt implementationValue = KErrNotFound;
       
   231         
       
   232         // Get implementation uid of found user name
       
   233         User::LeaveIfError( iRepository->Get( 
       
   234             ( keys[i]^KSIPClientResolverUserNameMask ) |
       
   235             KSIPClientResolverClientUIDMask, implementationValue ) );
       
   236         
       
   237         if ( implementationValue )
       
   238             { 
       
   239             TUid implementationUid;
       
   240             implementationUid.iUid = implementationValue;
       
   241             aImplementationUids.AppendL( implementationUid );
       
   242             }
       
   243         }
       
   244     CleanupStack::PopAndDestroy( &keys );
       
   245     }
       
   246 
       
   247 // ---------------------------------------------------------------------------
       
   248 // Find clients from resolver cenrep table
       
   249 // ---------------------------------------------------------------------------
       
   250 //
       
   251 void CSipClientResolverUtils::GetClientWithUserL(
       
   252     const TDesC8& aProfileContactHeaderUser, 
       
   253     RArray<TUint32>& aFoundKeys ) const
       
   254     {
       
   255     RArray<TUint32> keys;
       
   256     
       
   257     CleanupClosePushL( keys );
       
   258     // Find all rows where KSIPClientResolverUserNameMask is aProfileUserName
       
   259     iRepository->FindEqL( KSIPClientResolverUserNameMask, 
       
   260                             KSIPClientResolverFieldTypeMask,
       
   261                             aProfileContactHeaderUser, keys );
       
   262     for ( TInt i = 0; i < keys.Count(); i++ )
       
   263         {
       
   264         // Identification is now done only by aProfileContactHeaderUser
       
   265         aFoundKeys.AppendL( keys[i]^KSIPClientResolverUserNameMask );
       
   266         }
       
   267     CleanupStack::PopAndDestroy( &keys );
       
   268     }
       
   269 
       
   270 // ---------------------------------------------------------------------------
       
   271 // Create a new key for the new row
       
   272 // ---------------------------------------------------------------------------
       
   273 //
       
   274 void CSipClientResolverUtils::CreateNewKeyL(
       
   275     TUint32& aNewKey ) const
       
   276     {
       
   277     RArray<TUint32> keys;
       
   278     CleanupClosePushL( keys );
       
   279     TInt err = KErrNone;
       
   280     
       
   281     TRAPD( leaveCode, err = iRepository->FindL( KSIPClientResolverUserNameMask, 
       
   282                                   KSIPClientResolverFieldTypeMask, 
       
   283                                   keys ) );
       
   284     if ( KErrNotFound == leaveCode )
       
   285         {
       
   286         err = KErrNotFound;
       
   287         leaveCode = KErrNone;
       
   288         }
       
   289     User::LeaveIfError( leaveCode );
       
   290     if ( ( KErrNotFound == err ) || ( keys.Count() == 0 ) )
       
   291         {
       
   292         aNewKey = 1;
       
   293         }
       
   294     else
       
   295         {
       
   296         User::LeaveIfError( err );
       
   297         // Find the biggest key and increment it by one
       
   298         keys.SortUnsigned();
       
   299         aNewKey = KSIPClientResolverUserNameMask^keys[ keys.Count() - 1 ] + 1;       
       
   300         }
       
   301     CleanupStack::PopAndDestroy( &keys );
       
   302     }
       
   303 
       
   304 
       
   305 // -----------------------------------------------------------------------------
       
   306 // CSipClientResolverUtils::CallProviderUidsL
       
   307 // -----------------------------------------------------------------------------
       
   308 //    
       
   309  void CSipClientResolverUtils::CallProviderUidsL( RArray<TUint>& aUidArray ) const
       
   310     {
       
   311     CSPSettings* spSettings = CSPSettings::NewLC();
       
   312     CSPProperty* property = CSPProperty::NewLC();
       
   313     
       
   314     RArray<TUint> idArray;
       
   315     CleanupClosePushL( idArray );
       
   316     User::LeaveIfError( spSettings->FindServiceIdsL( idArray ) );
       
   317     TInt count = idArray.Count();
       
   318     TInt result( KErrNotFound );
       
   319     
       
   320     for( TInt i( 0 ); i < count; i++ )
       
   321         {
       
   322         result =  spSettings->FindPropertyL(
       
   323                 idArray[i], EPropertyCallProviderPluginId, *property );
       
   324         
       
   325         if ( result == KErrNone && property )
       
   326             {
       
   327             TInt value( 0 );
       
   328             property->GetValue( value );
       
   329             aUidArray.Append( value );
       
   330             }
       
   331         }
       
   332     
       
   333     CleanupStack::PopAndDestroy( &idArray );
       
   334     CleanupStack::PopAndDestroy( property );
       
   335     CleanupStack::PopAndDestroy( spSettings );
       
   336     }
       
   337  
       
   338  // -----------------------------------------------------------------------------
       
   339  // CSipClientResolverUtils::CheckImplementationUidL
       
   340  // -----------------------------------------------------------------------------
       
   341  // 
       
   342  TBool CSipClientResolverUtils::CheckImplementationUidL( 
       
   343      const TUint32& aKey, const TUid& aImplementation ) const
       
   344      {     
       
   345      TBool match( EFalse );
       
   346      TInt implementationValue( KErrNotFound );
       
   347      
       
   348      iRepository->Get( ( aKey^KSIPClientResolverUserNameMask )  |
       
   349                          KSIPClientResolverClientUIDMask,
       
   350                          implementationValue );
       
   351       
       
   352      if ( implementationValue == aImplementation.iUid )
       
   353          { 
       
   354          match = ETrue;
       
   355          }
       
   356      
       
   357      return match;
       
   358      }