contextframework/cfw/src/cfservices/cfcontextsourcecommandimpl.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2 * Copyright (c) 2008-2008 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:  CCFContextSourceCommandImpl class implementation.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // SYSTEM INCLUDES
       
    20 #include <s32strm.h>
       
    21 
       
    22 // USER INCLUDES
       
    23 #include "cfcontextsourcecommandimpl.h"
       
    24 #include "cftrace.h"
       
    25 
       
    26 // CONSTANTS
       
    27 _LIT( KUidAttribute, "uid" );
       
    28 
       
    29 // ======== MEMBER FUNCTIONS ========
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // C++ constructor.
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 CCFContextSourceCommandImpl::CCFContextSourceCommandImpl()
       
    36     {
       
    37     FUNC_LOG;
       
    38     }
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // Symbian 2nd phase constructor.
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 void CCFContextSourceCommandImpl::ConstructL()
       
    45     {
       
    46     FUNC_LOG;
       
    47     }
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // Symbian two phased constructor.
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 CCFContextSourceCommandImpl* CCFContextSourceCommandImpl::NewL()
       
    54     {
       
    55     FUNC_LOG;
       
    56 
       
    57     CCFContextSourceCommandImpl* self = CCFContextSourceCommandImpl::NewLC();
       
    58     CleanupStack::Pop( self );
       
    59     return self;
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // Symbian two phased constructor.
       
    64 // Leaves pointer in the cleanup stack.
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 CCFContextSourceCommandImpl* CCFContextSourceCommandImpl::NewLC()
       
    68     {
       
    69     FUNC_LOG;
       
    70 
       
    71     CCFContextSourceCommandImpl* self
       
    72             = new( ELeave ) CCFContextSourceCommandImpl;
       
    73     CleanupStack::PushL( self );
       
    74     self->ConstructL();
       
    75     return self;
       
    76     }
       
    77 
       
    78 // ---------------------------------------------------------------------------
       
    79 // C++ destructor.
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 CCFContextSourceCommandImpl::~CCFContextSourceCommandImpl()
       
    83     {
       
    84     FUNC_LOG;
       
    85 
       
    86     iParameters.ResetAndDestroy();
       
    87     iAttributes.ResetAndDestroy();
       
    88     delete iName;
       
    89     }
       
    90 
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // CCFContextSourceCommandImpl::Sender
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 const TCFSourceCommandSenderId& CCFContextSourceCommandImpl::Sender() const
       
    97     {
       
    98     FUNC_LOG;
       
    99 
       
   100     return iSender;
       
   101     }
       
   102 
       
   103 // ---------------------------------------------------------------------------
       
   104 // CCFContextSourceCommandImpl::SetSender
       
   105 // ---------------------------------------------------------------------------
       
   106 //
       
   107 void CCFContextSourceCommandImpl::SetSender(
       
   108     const TCFSourceCommandSenderId& aSender )
       
   109     {
       
   110     FUNC_LOG;
       
   111 
       
   112     iSender = aSender;
       
   113     }
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // CCFContextSourceCommandImpl::Name
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 const TDesC& CCFContextSourceCommandImpl::Name() const
       
   120     {
       
   121     FUNC_LOG;
       
   122 
       
   123     if ( iName )
       
   124         {
       
   125         return *iName;
       
   126         }
       
   127     else
       
   128         {
       
   129         return KNullDesC;
       
   130         }
       
   131     }
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // CCFContextSourceCommandImpl::SetNameL
       
   135 // ---------------------------------------------------------------------------
       
   136 //
       
   137 void CCFContextSourceCommandImpl::SetNameL( const TDesC& aName )
       
   138     {
       
   139     FUNC_LOG;
       
   140 
       
   141     delete iName;
       
   142     iName = NULL;
       
   143     iName = aName.AllocL();
       
   144     }
       
   145 
       
   146 // ---------------------------------------------------------------------------
       
   147 // CCFContextSourceCommandImpl::SourceUid
       
   148 // ---------------------------------------------------------------------------
       
   149 //
       
   150 TUid CCFContextSourceCommandImpl::SourceUid() const
       
   151     {
       
   152     FUNC_LOG;
       
   153 
       
   154     TUid sourceUid = KNullUid;
       
   155 
       
   156     for ( TInt i = 0; i < iAttributes.Count(); ++i )
       
   157         {
       
   158         CCFKeyValuePair* keyValue = iAttributes[ i ];
       
   159         if ( keyValue->Key().CompareF( KUidAttribute ) == 0 )
       
   160             {
       
   161             TPtrC uidString( keyValue->Value() );
       
   162             TLex lex( uidString );
       
   163 
       
   164             // Get rid of '0x'.
       
   165             const TInt skip = 2;
       
   166             lex.Inc( skip );
       
   167 
       
   168             TUint32 uid = 0;
       
   169             const TUint KLimit = 0xFFFFFFFF;
       
   170             TInt err = lex.BoundedVal( uid, EHex, KLimit );
       
   171             if( err == KErrNone )
       
   172                 {
       
   173                 sourceUid = TUid::Uid( uid );
       
   174                 }
       
   175             else
       
   176                 {
       
   177                 ERROR_1( err, "CCFContextSourceCommandImpl::SourceUid - Converting source uid from [%S] failed.", &uidString );
       
   178                 }
       
   179 
       
   180             break;
       
   181             }
       
   182         }
       
   183 
       
   184     return sourceUid;
       
   185     }
       
   186 
       
   187 // ---------------------------------------------------------------------------
       
   188 // CCFContextSourceCommandImpl::Attributes
       
   189 // ---------------------------------------------------------------------------
       
   190 //
       
   191 const RKeyValueArray& CCFContextSourceCommandImpl::Attributes() const
       
   192     {
       
   193     FUNC_LOG;
       
   194 
       
   195     return iAttributes;
       
   196     }
       
   197 
       
   198 // ---------------------------------------------------------------------------
       
   199 // CCFContextSourceCommandImpl::AddAttributeL
       
   200 // ---------------------------------------------------------------------------
       
   201 //
       
   202 void CCFContextSourceCommandImpl::AddAttributeL(
       
   203     CCFKeyValuePair* aAttribute )
       
   204     {
       
   205     FUNC_LOG;
       
   206 
       
   207     iAttributes.AppendL( aAttribute );
       
   208     }
       
   209 
       
   210 // ---------------------------------------------------------------------------
       
   211 // CCFContextSourceCommandImpl::AddAttributesL
       
   212 // ---------------------------------------------------------------------------
       
   213 //
       
   214 void CCFContextSourceCommandImpl::AddAttributesL( RKeyValueArray& aAttributes )
       
   215     {
       
   216     FUNC_LOG;
       
   217 
       
   218     TInt insertPos = iAttributes.Count();
       
   219     for ( TInt i = aAttributes.Count() - 1; i >= 0; --i )
       
   220         {
       
   221         CCFKeyValuePair* attribute = aAttributes[ i ];
       
   222         aAttributes.Remove( i );
       
   223         CleanupStack::PushL( attribute );       // CLEANUP<< attribute
       
   224         iAttributes.Insert( attribute, insertPos );
       
   225         CleanupStack::Pop( attribute );         // CLEANUP>> attribute
       
   226         }
       
   227     }
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 // CCFContextSourceCommandImpl::Parameters
       
   231 // ---------------------------------------------------------------------------
       
   232 //
       
   233 const RSourceCommandParameterArray&
       
   234 CCFContextSourceCommandImpl::Parameters() const
       
   235     {
       
   236     FUNC_LOG;
       
   237 
       
   238     return iParameters;
       
   239     }
       
   240 
       
   241 // ---------------------------------------------------------------------------
       
   242 // CCFContextSourceCommandImpl::AddParameterL
       
   243 // ---------------------------------------------------------------------------
       
   244 //
       
   245 void CCFContextSourceCommandImpl::AddParameterL(
       
   246     CCFContextSourceCommandParameter* aParameter )
       
   247     {
       
   248     FUNC_LOG;
       
   249 
       
   250     iParameters.AppendL( aParameter );
       
   251     }
       
   252 
       
   253 // ---------------------------------------------------------------------------
       
   254 // CCFContextSourceCommandImpl::Size
       
   255 // ---------------------------------------------------------------------------
       
   256 //
       
   257 TInt CCFContextSourceCommandImpl::Size() const
       
   258     {
       
   259     FUNC_LOG;
       
   260 
       
   261     TInt size = 0;
       
   262 
       
   263     // Sender identity
       
   264     size += sizeof( TCFSourceCommandSenderId );
       
   265 
       
   266     // Name: length and size of iName
       
   267     size += sizeof( TInt32 );
       
   268     size += iName->Size();
       
   269 
       
   270     // Attributes: array count & data
       
   271     size += sizeof( TInt32 );
       
   272     TInt count = iAttributes.Count();
       
   273     for ( TInt i = 0; i < count; ++i )
       
   274         {
       
   275         // Key length & data
       
   276         size += sizeof( TInt32 );
       
   277         size += iAttributes[ i ]->Key().Size();
       
   278 
       
   279         // Value length & data
       
   280         size += sizeof( TInt32 );
       
   281         size += iAttributes[ i ]->Value().Size();
       
   282         }
       
   283 
       
   284     // Parameters: array count & data
       
   285     size += sizeof( TInt32 );
       
   286     count = iParameters.Count();
       
   287     for ( TInt i = 0; i < count; ++i )
       
   288         {
       
   289         size += iParameters[ i ]->Size();
       
   290         }
       
   291 
       
   292     return size;
       
   293     }
       
   294 
       
   295 //-----------------------------------------------------------------------------
       
   296 // CCFContextSourceCommandImpl::ExternalizeL
       
   297 //-----------------------------------------------------------------------------
       
   298 //
       
   299 void CCFContextSourceCommandImpl::ExternalizeL( RWriteStream& aStream ) const
       
   300     {
       
   301     FUNC_LOG;
       
   302 
       
   303     // Sender identity
       
   304     const TUint8* senderId = reinterpret_cast< const TUint8* >( &iSender );
       
   305     aStream.WriteL( senderId, sizeof( TCFSourceCommandSenderId ) );
       
   306 
       
   307     // Name
       
   308     aStream.WriteInt32L( iName->Length() );
       
   309     aStream.WriteL( *iName );
       
   310 
       
   311     // Attributes
       
   312     TInt count = iAttributes.Count();
       
   313     aStream.WriteInt32L( count );
       
   314     for ( TInt i = 0; i < count; ++i )
       
   315         {
       
   316         TPtrC buf( KNullDesC );
       
   317 
       
   318         // Key length & data
       
   319         buf.Set( iAttributes[ i ]->Key() );
       
   320         aStream.WriteInt32L( buf.Length() );
       
   321         aStream.WriteL( buf, buf.Length() );
       
   322 
       
   323         // Value length & data
       
   324         buf.Set( iAttributes[ i ]->Value() );
       
   325         aStream.WriteInt32L( buf.Length() );
       
   326         aStream.WriteL( buf, buf.Length() );
       
   327         }
       
   328 
       
   329     // Parameters
       
   330     count = iParameters.Count();
       
   331     aStream.WriteInt32L( count );
       
   332     for ( TInt i = 0; i < count; ++i )
       
   333         {
       
   334         iParameters[ i ]->ExternalizeL( aStream );
       
   335         }
       
   336 
       
   337     // Commit stream
       
   338     aStream.CommitL();
       
   339     }
       
   340 
       
   341 //-----------------------------------------------------------------------------
       
   342 // CCFContextSourceCommandImpl::InternalizeL
       
   343 //-----------------------------------------------------------------------------
       
   344 //
       
   345 void CCFContextSourceCommandImpl::InternalizeL( RReadStream& aStream )
       
   346     {
       
   347     FUNC_LOG;
       
   348 
       
   349     // Clean up just in case
       
   350     delete iName;
       
   351     iName = NULL;
       
   352     iAttributes.ResetAndDestroy();
       
   353     iParameters.ResetAndDestroy();
       
   354 
       
   355     // Sender identity
       
   356     TUint8* senderId = reinterpret_cast< TUint8* >( &iSender );
       
   357     aStream.ReadL( senderId, sizeof( TCFSourceCommandSenderId ) );
       
   358 
       
   359     // Name
       
   360     TInt len = aStream.ReadInt32L();
       
   361     iName = HBufC::NewL( len );
       
   362     TPtr namePtr = iName->Des();
       
   363     aStream.ReadL( namePtr, len );
       
   364 
       
   365     // Attributes
       
   366     TInt count = aStream.ReadInt32L();
       
   367     for ( TInt i = 0; i < count; ++i )
       
   368         {
       
   369         // Key length & data
       
   370         len = aStream.ReadInt32L();
       
   371         HBufC* key = HBufC::NewLC( len );           // CLEANUP<< key
       
   372         TPtr keyPtr = key->Des();
       
   373         aStream.ReadL( keyPtr, len );
       
   374 
       
   375         // Value length & data
       
   376         len = aStream.ReadInt32L();
       
   377         HBufC* value = HBufC::NewLC( len );         // CLEANUP<< value
       
   378         TPtr valuePtr = value->Des();
       
   379         aStream.ReadL( valuePtr, len );
       
   380 
       
   381         // Create & append key value pair
       
   382         CCFKeyValuePair* keyValuePair = CCFKeyValuePair::NewL( key, value );
       
   383         CleanupStack::Pop( value );                 // CLEANUP>> value
       
   384         CleanupStack::Pop( key );                   // CLEANUP>> key
       
   385 
       
   386         CleanupStack::PushL( keyValuePair );        // CLEANUP<< keyValuePair
       
   387         iAttributes.AppendL( keyValuePair );
       
   388         CleanupStack::Pop( keyValuePair );          // CLEANUP>> keyValuePair
       
   389         }
       
   390 
       
   391     // Parameters
       
   392     count = aStream.ReadInt32L();
       
   393     for ( TInt i = 0; i < count; ++i )
       
   394         {
       
   395         // below                                    // CLEANUP<< param
       
   396         CCFContextSourceCommandParameter* param
       
   397             = CCFContextSourceCommandParameter::NewLC();
       
   398         param->InternalizeL( aStream );
       
   399         iParameters.AppendL( param );
       
   400         CleanupStack::Pop( param );                 // CLEANUP>> param
       
   401         }
       
   402     }
       
   403 
       
   404 // ---------------------------------------------------------------------------
       
   405 // CCFContextSourceCommandImpl::CloneL
       
   406 // ---------------------------------------------------------------------------
       
   407 //
       
   408 CCFContextSourceCommand* CCFContextSourceCommandImpl::CloneL() const
       
   409     {
       
   410     FUNC_LOG;
       
   411 
       
   412     // below                                        // CLEANUP<< cmd
       
   413     CCFContextSourceCommandImpl* cmd = CCFContextSourceCommandImpl::NewLC();
       
   414 
       
   415     // Copy command sender identity.
       
   416     cmd->iSender = iSender;
       
   417 
       
   418     // Copy name.
       
   419     if ( iName )
       
   420         {
       
   421         cmd->iName = iName->AllocL();
       
   422         }
       
   423 
       
   424     // Copy attributes.
       
   425     for ( TInt i = 0; i < iAttributes.Count(); ++i )
       
   426         {
       
   427         CCFKeyValuePair* keyValue = iAttributes[ i ];
       
   428         // below                                    // CLEANUP<< attribute
       
   429         CCFKeyValuePair* attribute = CCFKeyValuePair::NewLC( keyValue->Key(),
       
   430                 keyValue->Value() );
       
   431         cmd->iAttributes.AppendL( attribute );
       
   432         CleanupStack::Pop( attribute );             // CLEANUP>> attribute
       
   433         }
       
   434 
       
   435     // Copy parameters.
       
   436     for ( TInt i = 0; i < iParameters.Count(); ++i )
       
   437         {
       
   438         CCFContextSourceCommandParameter* cmdParam = iParameters[ i ];
       
   439         // below                                    // CLEANUP<< param
       
   440         CCFContextSourceCommandParameter* param = cmdParam->CloneLC();
       
   441         cmd->iParameters.AppendL( param );
       
   442         CleanupStack::Pop( param );                 // CLEANUP>> param
       
   443         }
       
   444 
       
   445     CleanupStack::Pop( cmd );                       // CLEANUP>> cmd
       
   446     return cmd;
       
   447     }
       
   448 
       
   449 // End of file