mmsharing/mmshavailability/src/musavaclientresolverutil.cpp
changeset 0 f0cf47e981f9
equal deleted inserted replaced
-1:000000000000 0:f0cf47e981f9
       
     1 /*
       
     2 * Copyright (c) 2007 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:   Utility class to handle SIP client resolver cenrep table.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "musavaclientresolverutil.h"
       
    22 #include "musuid.hrh"
       
    23 #include "muslogger.h"
       
    24 #include <centralrepository.h>
       
    25 #include <sipprofile.h>
       
    26 
       
    27 // SipMXResolverImplUid
       
    28 const TUid KSipMXResolverImplUid = { 0x10283140 };
       
    29 
       
    30 
       
    31 // <copied values>
       
    32 
       
    33 // The following constant is defined in sipprofile.h in Sawfish.
       
    34 // Since the constant is not defined in all releases, it must be copied here.
       
    35 
       
    36 /** 
       
    37 * A unique Contact-header's user-part 
       
    38 * generated automatically by the SIP implementation.
       
    39 * The value is of type TDesC8.
       
    40 * This parameter is read-only and 
       
    41 * trying to set it will fail with KErrAccessDenied.
       
    42 */
       
    43 const TUint32 KCopyOfKSIPContactHeaderUser = 15;
       
    44 
       
    45 // The following Uids and keys are defined in file 
       
    46 // sipclientresolverconfigcrkeys.h in Sawfish. Since the header is not present
       
    47 // in all releases, the values must be defined here.
       
    48 
       
    49 /// Defines configuration parameters used by SIP Client Resolver
       
    50 const TUid KCRUidSIPClientResolverConfig = { 0x10282EE7 };
       
    51 
       
    52 /// Mask for the less significant bits in setting keys.
       
    53 /// These bits are reserved for field type information.
       
    54 const TUint32 KSIPClientResolverFieldTypeMask = 0xf0000000;
       
    55 
       
    56 /// Mask for the most significant bits in setting keys.
       
    57 /// These bits are reserved for the actual key part.
       
    58 const TUint32 KSIPClientResolverKeyMask = 0x0fffffff;
       
    59 
       
    60 /** 
       
    61 * Mask for username field.
       
    62 * Field type is TDesC8.
       
    63 */
       
    64 const TUint32 KSIPClientResolverUserNameMask = 0x10000000;
       
    65 
       
    66 /** 
       
    67 * Mask for plug-in implementation UID field.
       
    68 * Field type is TInt32.
       
    69 */
       
    70 const TUint32 KSIPClientResolverPluginUIDMask = 0x20000000;
       
    71 
       
    72 /** 
       
    73 * Mask for client UID field.
       
    74 * Field type is TInt32.
       
    75 */
       
    76 const TUint32 KSIPClientResolverClientUIDMask = 0x30000000;
       
    77 
       
    78 // </copied values>
       
    79 
       
    80 
       
    81 // ======== MEMBER FUNCTIONS ========
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // 
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 CMusAvaClientResolverUtil::CMusAvaClientResolverUtil()
       
    88     {
       
    89     }
       
    90 
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // 
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 void CMusAvaClientResolverUtil::ConstructL()
       
    97     {
       
    98     iRepository = CRepository::NewL( KCRUidSIPClientResolverConfig );
       
    99     }
       
   100 
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // 
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 CMusAvaClientResolverUtil* CMusAvaClientResolverUtil::NewL()
       
   107     {
       
   108     MUS_LOG( "mus: [MUSAVA]  -> CMusAvaClientResolverUtil::NewL()" )
       
   109     CMusAvaClientResolverUtil* self = CMusAvaClientResolverUtil::NewLC();
       
   110     CleanupStack::Pop( self );
       
   111     MUS_LOG( "mus: [MUSAVA]  <- CMusAvaClientResolverUtil::NewL()" )
       
   112     return self;
       
   113     }
       
   114 
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // 
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 CMusAvaClientResolverUtil* CMusAvaClientResolverUtil::NewLC()
       
   121     {
       
   122     MUS_LOG( "mus: [MUSAVA]  -> CMusAvaClientResolverUtil::NewLC()" )
       
   123     CMusAvaClientResolverUtil* self = new( ELeave ) CMusAvaClientResolverUtil;
       
   124     CleanupStack::PushL( self );
       
   125     self->ConstructL();
       
   126     MUS_LOG( "mus: [MUSAVA]  <- CMusAvaClientResolverUtil::NewLC()" )
       
   127     return self;
       
   128     }
       
   129 
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // 
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 CMusAvaClientResolverUtil::~CMusAvaClientResolverUtil()
       
   136     {
       
   137     MUS_LOG( "mus: [MUSAVA]  -> ~CMusAvaClientResolverUtil()" )
       
   138     delete iRepository;
       
   139     MUS_LOG( "mus: [MUSAVA]  <- ~CMusAvaClientResolverUtil()" )
       
   140     }
       
   141 
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // 
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 void CMusAvaClientResolverUtil::RegisterClientWithUserL( 
       
   148                                                 CSIPProfile& aProfile ) const
       
   149     {
       
   150     MUS_LOG( "mus: [MUSAVA]  -> CMusAvaClientResolverUtil::RegisterClientWithUserL( profile )" )
       
   151     RBuf8 contactHeaderUser;
       
   152     contactHeaderUser.CleanupClosePushL();
       
   153     User::LeaveIfError( GetContactHeaderUser( aProfile, contactHeaderUser ) );
       
   154     
       
   155     RegisterClientWithUserL( TUid::Uid( KMusUiUid ), 
       
   156                              contactHeaderUser, 
       
   157                              KSipMXResolverImplUid );
       
   158     
       
   159     CleanupStack::PopAndDestroy( &contactHeaderUser );
       
   160     MUS_LOG( "mus: [MUSAVA]  <- CMusAvaClientResolverUtil::RegisterClientWithUserL( profile )" )
       
   161     }
       
   162     
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // Add line to client resolver cenrep table
       
   166 // -----------------------------------------------------------------------------
       
   167 //
       
   168 void CMusAvaClientResolverUtil::RegisterClientWithUserL(
       
   169     const TUid& aImplementation,
       
   170     const TDesC8& aProfileContactHeaderUser, 
       
   171     const TUid& aResolver ) const
       
   172     {
       
   173     MUS_LOG( "mus: [MUSAVA]  -> CMusAvaClientResolverUtil::RegisterClientWithUserL()" )
       
   174     
       
   175     if( aImplementation.iUid <= 0 || 
       
   176         aProfileContactHeaderUser.Length() <= 0 ||
       
   177         aResolver.iUid <= 0 )
       
   178         {
       
   179         User::Leave( KErrArgument );
       
   180         }
       
   181 
       
   182     // Remove all earlier instances of client&user combination
       
   183     UnRegisterClientWithUserL( aImplementation, aProfileContactHeaderUser );
       
   184 
       
   185     User::LeaveIfError( 
       
   186         iRepository->StartTransaction( CRepository::EConcurrentReadWriteTransaction ) );
       
   187     TUint32 newKey = 0;
       
   188     CreateNewKeyL( newKey );
       
   189     // Add the new row
       
   190     TInt uid = aImplementation.iUid;
       
   191     User::LeaveIfError( 
       
   192         iRepository->Create(newKey|KSIPClientResolverClientUIDMask, uid ) );    
       
   193     User::LeaveIfError( 
       
   194         iRepository->Create(newKey|KSIPClientResolverUserNameMask, aProfileContactHeaderUser ) );
       
   195     uid = aResolver.iUid;    
       
   196     User::LeaveIfError( 
       
   197         iRepository->Create(newKey|KSIPClientResolverPluginUIDMask, uid ) ); 
       
   198 
       
   199     // Commit the transaction
       
   200     User::LeaveIfError( iRepository->CommitTransaction( newKey ) );
       
   201 
       
   202     MUS_LOG( "mus: [MUSAVA]  <- CMusAvaClientResolverUtil::RegisterClientWithUserL()" )
       
   203     }
       
   204 
       
   205 
       
   206 // -----------------------------------------------------------------------------
       
   207 // Remove line from client resolver cenrep table
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 void CMusAvaClientResolverUtil::UnRegisterClientWithUserL( 
       
   211                                                 CSIPProfile& aProfile ) const
       
   212     {
       
   213     MUS_LOG( "mus: [MUSAVA]  -> CMusAvaClientResolverUtil::UnRegisterClientWithUserL( profile )" )
       
   214 
       
   215     RBuf8 contactHeaderUser;
       
   216     contactHeaderUser.CleanupClosePushL();
       
   217     User::LeaveIfError( GetContactHeaderUser( aProfile, contactHeaderUser ) );
       
   218     
       
   219     UnRegisterClientWithUserL( TUid::Uid( KMusUiUid ), contactHeaderUser );
       
   220     
       
   221     CleanupStack::PopAndDestroy( &contactHeaderUser );
       
   222 
       
   223     MUS_LOG( "mus: [MUSAVA]  <- CMusAvaClientResolverUtil::UnRegisterClientWithUserL( profile )" )
       
   224     }
       
   225     
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // Remove line from client resolver cenrep table
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 void CMusAvaClientResolverUtil::UnRegisterClientWithUserL(
       
   232     const TUid& aImplementation, 
       
   233     const TDesC8& aProfileContactHeaderUser ) const
       
   234     {
       
   235     MUS_LOG( "mus: [MUSAVA]  -> CMusAvaClientResolverUtil::UnRegisterClientWithUserL()" )
       
   236 
       
   237     if( aImplementation.iUid <= 0 || 
       
   238         aProfileContactHeaderUser.Length() <= 0 )
       
   239         {
       
   240         User::Leave( KErrArgument );
       
   241         }
       
   242 
       
   243     RArray<TUint32> keys;
       
   244     CleanupClosePushL( keys );
       
   245     GetClientWithUserL( aImplementation, aProfileContactHeaderUser, keys );
       
   246     for ( TInt i = 0; i < keys.Count(); i++ )
       
   247         {
       
   248         TUint32 errorKey;
       
   249         User::LeaveIfError( 
       
   250             iRepository->StartTransaction( 
       
   251                 CRepository::EConcurrentReadWriteTransaction ) );
       
   252         iRepository->Delete( keys[i], KSIPClientResolverKeyMask, errorKey );
       
   253         // Commit the transaction
       
   254         User::LeaveIfError( iRepository->CommitTransaction( errorKey ) );
       
   255         }
       
   256     CleanupStack::PopAndDestroy( &keys );
       
   257     
       
   258     MUS_LOG( "mus: [MUSAVA]  <- CMusAvaClientResolverUtil::UnRegisterClientWithUserL()" )
       
   259     }
       
   260 
       
   261 
       
   262 // -----------------------------------------------------------------------------
       
   263 // Resolve correct implementation UID for contact header
       
   264 // -----------------------------------------------------------------------------
       
   265 //    
       
   266 void CMusAvaClientResolverUtil::GetImplementationUidWithUserL( 
       
   267 	const TDesC8& aProfileContactHeaderUser,
       
   268     TUid& aImplementation ) const
       
   269     {
       
   270     MUS_LOG( "mus: [MUSAVA]  -> CMusAvaClientResolverUtil::GetImplementationUidWithUserL()" )
       
   271 
       
   272     RArray<TUint32> keys;    
       
   273     CleanupClosePushL( keys );
       
   274     // Find all rows where KSIPClientResolverUserNameMask is aProfileContactHeaderUser
       
   275     iRepository->FindEqL( KSIPClientResolverUserNameMask, 
       
   276                           KSIPClientResolverFieldTypeMask,
       
   277                           aProfileContactHeaderUser, 
       
   278                           keys );
       
   279     if (keys.Count() > 1)
       
   280         {
       
   281         //resolve correct Key
       
   282         }
       
   283     else if(keys.Count() == 1)
       
   284         {
       
   285         TInt implementationValue = KErrNotFound;
       
   286         // Get implmentation uid of found user name
       
   287         iRepository->Get( ( keys[0]^KSIPClientResolverUserNameMask ) |
       
   288         KSIPClientResolverClientUIDMask, implementationValue );
       
   289 
       
   290         User::LeaveIfError(implementationValue);
       
   291         aImplementation.iUid = implementationValue;
       
   292         }
       
   293     else
       
   294         {
       
   295         User::Leave(KErrNotFound);
       
   296         }
       
   297 
       
   298     CleanupStack::PopAndDestroy( &keys );	
       
   299     
       
   300     MUS_LOG( "mus: [MUSAVA]  <- CMusAvaClientResolverUtil::GetImplementationUidWithUserL()" )
       
   301     }
       
   302     
       
   303     
       
   304 // -----------------------------------------------------------------------------
       
   305 // Find clients from resolver cenrep table
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 void CMusAvaClientResolverUtil::GetClientWithUserL(
       
   309     const TUid& aImplementation, 
       
   310     const TDesC8& aProfileContactHeaderUser, 
       
   311     RArray<TUint32>& aFoundKeys ) const
       
   312     {
       
   313     MUS_LOG( "mus: [MUSAVA]  -> CMusAvaClientResolverUtil::GetClientWithUserL()" )
       
   314 
       
   315     RArray<TUint32> keys;
       
   316     
       
   317     CleanupClosePushL( keys );
       
   318     // Find all rows where KSIPClientResolverUserNameMask is aProfileUserName
       
   319     iRepository->FindEqL( KSIPClientResolverUserNameMask, 
       
   320                             KSIPClientResolverFieldTypeMask,
       
   321                             aProfileContactHeaderUser, keys );
       
   322     for ( TInt i = 0; i < keys.Count(); i++ )
       
   323         {
       
   324         TInt implementationValue = KErrNotFound;
       
   325         // Get implmentation uid of found user name
       
   326         iRepository->Get( ( keys[i]^KSIPClientResolverUserNameMask ) |
       
   327             KSIPClientResolverClientUIDMask, implementationValue );
       
   328         // If implementation uid matches given uid, add row to found keys
       
   329         if ( implementationValue == aImplementation.iUid )
       
   330             {
       
   331             aFoundKeys.Append( keys[i]^KSIPClientResolverUserNameMask );
       
   332             }
       
   333 
       
   334         }
       
   335     CleanupStack::PopAndDestroy( &keys );
       
   336     
       
   337     MUS_LOG( "mus: [MUSAVA]  <- CMusAvaClientResolverUtil::GetClientWithUserL()" )
       
   338     }
       
   339     
       
   340     
       
   341 // -----------------------------------------------------------------------------
       
   342 // Create a new key for the new row
       
   343 // -----------------------------------------------------------------------------
       
   344 //
       
   345 void CMusAvaClientResolverUtil::CreateNewKeyL(
       
   346     TUint32& aNewKey ) const
       
   347     {
       
   348     MUS_LOG( "mus: [MUSAVA]  -> CMusAvaClientResolverUtil::CreateNewKeyL()" )
       
   349 
       
   350     RArray<TUint32> keys;
       
   351     CleanupClosePushL( keys );
       
   352     TInt err = KErrNone;
       
   353     
       
   354     TRAPD( leaveCode, err = iRepository->FindL( KSIPClientResolverUserNameMask, 
       
   355                                   KSIPClientResolverFieldTypeMask, 
       
   356                                   keys ) );
       
   357     if ( KErrNotFound == leaveCode )
       
   358         {
       
   359         err = KErrNotFound;
       
   360         leaveCode = KErrNone;
       
   361         }
       
   362     User::LeaveIfError( leaveCode );
       
   363     if ( ( KErrNotFound == err ) || ( keys.Count() == 0 ) )
       
   364         {
       
   365         aNewKey = 1;
       
   366         }
       
   367     else
       
   368         {
       
   369         User::LeaveIfError( err );
       
   370         // Find the biggest key and increment it by one
       
   371         keys.SortUnsigned();
       
   372         aNewKey = KSIPClientResolverUserNameMask^keys[ keys.Count() - 1 ] + 1;       
       
   373         }
       
   374     CleanupStack::PopAndDestroy( &keys );
       
   375     
       
   376     MUS_LOG( "mus: [MUSAVA]  <- CMusAvaClientResolverUtil::CreateNewKeyL()" )
       
   377     }
       
   378 
       
   379 
       
   380 // -----------------------------------------------------------------------------
       
   381 //
       
   382 // -----------------------------------------------------------------------------
       
   383 //
       
   384 TInt CMusAvaClientResolverUtil::GetContactHeaderUser(
       
   385             CSIPProfile& aProfile,
       
   386             RBuf8& aContactHeaderUser ) const
       
   387     {
       
   388     MUS_LOG( "mus: [MUSAVA]  -> CMusAvaClientResolverUtil::GetContactHeaderUser()" )
       
   389 
       
   390     TInt result( KErrNotFound );
       
   391     const TDesC8* contactHeaderUser( NULL );
       
   392 
       
   393     // Get SIP's Username    
       
   394     result = aProfile.GetParameter( KCopyOfKSIPContactHeaderUser, contactHeaderUser );
       
   395     
       
   396     if ( result == KErrNone && contactHeaderUser )
       
   397         {
       
   398         result = aContactHeaderUser.ReAlloc( contactHeaderUser->Length() );
       
   399         if ( KErrNone == result )
       
   400             {
       
   401             aContactHeaderUser.Copy( *contactHeaderUser );
       
   402             }
       
   403         }
       
   404 
       
   405     MUS_LOG( "mus: [MUSAVA]  <- CMusAvaClientResolverUtil::GetContactHeaderUser()" )
       
   406 
       
   407     return result;                
       
   408     }
       
   409 
       
   410