ncdengine/provider/server/src/ncdnodeuserdataimpl.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2006 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:   Implements CNcdNodeUserData class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "ncdnodeuserdataimpl.h"
       
    20 #include "ncdnodemanager.h"
       
    21 #include "ncdnodeidentifier.h"
       
    22 #include "catalogssession.h"
       
    23 #include "catalogsbasemessage.h"
       
    24 #include "ncdnodefunctionids.h"
       
    25 #include "ncdnodeclassids.h"
       
    26 #include "catalogsconstants.h"
       
    27 #include "ncd_pp_dataentity.h"
       
    28 #include "ncd_cp_query.h"
       
    29 #include "catalogsutils.h"
       
    30 #include "catalogsdebug.h"
       
    31 
       
    32 
       
    33 CNcdNodeUserData::CNcdNodeUserData( NcdNodeClassIds::TNcdNodeClassId aClassId,
       
    34                                     CNcdNodeManager& aManager )
       
    35 : CCatalogsCommunicable(),
       
    36   iClassId( aClassId ),
       
    37   iManager( aManager )
       
    38     {
       
    39     }
       
    40 
       
    41 void CNcdNodeUserData::ConstructL( const CNcdNodeIdentifier& aIdentifier )
       
    42     {
       
    43     DLTRACEIN((""));
       
    44 
       
    45     // These values have to be set.
       
    46 
       
    47     iIdentifier = 
       
    48         CNcdNodeIdentifier::NewL( aIdentifier );
       
    49 
       
    50 
       
    51     // Notice that the user data is left here to be NULL.
       
    52     // The value is set from db during the first query for the user data.
       
    53     // This is checked by comparing the user data value to NULL.
       
    54     // Also, notice that the Internalization can not be done here because
       
    55     // this class object may be created from the node when it is been internalized.
       
    56     // Multiple internalizations from the same db at the same time are not allowed.
       
    57 
       
    58     DLTRACEOUT((""));
       
    59     }
       
    60 
       
    61 
       
    62 CNcdNodeUserData* CNcdNodeUserData::NewL( const CNcdNodeIdentifier& aIdentifier,
       
    63                                           CNcdNodeManager& aManager )
       
    64     {
       
    65     CNcdNodeUserData* self =   
       
    66         CNcdNodeUserData::NewLC( aIdentifier, aManager );
       
    67     CleanupStack::Pop( self );
       
    68     return self;        
       
    69     }
       
    70 
       
    71 CNcdNodeUserData* CNcdNodeUserData::NewLC( const CNcdNodeIdentifier& aIdentifier,
       
    72                                            CNcdNodeManager& aManager )
       
    73     {
       
    74     CNcdNodeUserData* self = 
       
    75         new( ELeave ) CNcdNodeUserData( 
       
    76             NcdNodeClassIds::ENcdNodeUserDataClassId, aManager );
       
    77     CleanupClosePushL( *self );
       
    78     self->ConstructL( aIdentifier );
       
    79     return self;        
       
    80     }
       
    81 
       
    82 
       
    83 CNcdNodeUserData::~CNcdNodeUserData()
       
    84     {
       
    85     DLTRACEIN((""));
       
    86 
       
    87     // Delete member variables here
       
    88     delete iIdentifier;
       
    89     iIdentifier = NULL;
       
    90     
       
    91     delete iUserData;
       
    92     iUserData = NULL;
       
    93 
       
    94     // Do not delete manager because this class object does not own it.
       
    95     
       
    96     DLTRACEOUT((""));
       
    97     }        
       
    98 
       
    99 NcdNodeClassIds::TNcdNodeClassId CNcdNodeUserData::ClassId() const
       
   100     {
       
   101     return iClassId;
       
   102     }
       
   103 
       
   104 
       
   105 void CNcdNodeUserData::ReceiveMessage( MCatalogsBaseMessage* aMessage,
       
   106                                        TInt aFunctionNumber )
       
   107     {
       
   108     DLTRACEIN((""));    
       
   109 
       
   110     DASSERT( aMessage );
       
   111     
       
   112     // Now, we can be sure that rest of the time iMessage exists.
       
   113     // This member variable is set for the CounterPartLost function.
       
   114     iMessage = aMessage;
       
   115     
       
   116     TInt trapError( KErrNone );
       
   117     
       
   118     // Check which function is called by the proxy side object.
       
   119     // Function number are located in ncdnodefunctinoids.h file.
       
   120     switch( aFunctionNumber )
       
   121         {
       
   122         case NcdNodeFunctionIds::ENcdUserData:
       
   123             TRAP( trapError, UserDataRequestL( *aMessage ) );
       
   124             break;
       
   125 
       
   126         case NcdNodeFunctionIds::ENcdSetUserData:
       
   127             TRAP( trapError, SetUserDataRequestL( *aMessage ) );
       
   128             break;
       
   129 
       
   130         case NcdNodeFunctionIds::ENcdClearUserData:
       
   131             TRAP( trapError, ClearUserDataRequestL( *aMessage ) );
       
   132             break;
       
   133 
       
   134         case NcdNodeFunctionIds::ENcdRelease:
       
   135             // The proxy does not want to use this object anymore.
       
   136             // So, release the handle from the session.
       
   137             ReleaseRequest( *aMessage );
       
   138             break;
       
   139                     
       
   140         default:
       
   141             DLERROR(("Unidentified function request"));
       
   142             DASSERT( EFalse );
       
   143             break;
       
   144         }
       
   145 
       
   146     if ( trapError != KErrNone )
       
   147         {
       
   148         // Because something went wrong, the complete has not been
       
   149         // yet called for the message.
       
   150         // So, inform the client about the error if the
       
   151         // message is still available.
       
   152         aMessage->CompleteAndRelease( trapError );
       
   153         }
       
   154 
       
   155     // Because the message should not be used after this, set it NULL.
       
   156     // So, CounterPartLost function will know that no messages are
       
   157     // waiting the response at the moment.
       
   158     iMessage = NULL;        
       
   159     
       
   160     DLTRACEOUT((""));
       
   161     }
       
   162 
       
   163 void CNcdNodeUserData::CounterPartLost( const MCatalogsSession& aSession )
       
   164     {
       
   165     DLTRACEIN((""));
       
   166 
       
   167     // This function may be called whenever -- when the message is waiting
       
   168     // response or when the message does not exist.
       
   169     // iMessage may be NULL here, because in the end of the
       
   170     // ReceiveMessage it is set to NULL. The life time of the message
       
   171     // ends shortly after CompleteAndRelease is called.
       
   172     if ( iMessage != NULL )
       
   173         {
       
   174         iMessage->CounterPartLost( aSession );
       
   175         }
       
   176 
       
   177     DLTRACEOUT((""));    
       
   178     }
       
   179 
       
   180 
       
   181 void CNcdNodeUserData::ExternalizeL( RWriteStream& aStream )
       
   182     {
       
   183     DLTRACEIN(("Data: %S", iUserData));
       
   184 
       
   185     if ( iUserData != NULL )
       
   186         {
       
   187         // Write the class id to the stream in case it should be checked sometime.
       
   188         aStream.WriteInt32L( iClassId );
       
   189 
       
   190         // Just insert the user data into the stream
       
   191         ExternalizeDesL( *iUserData, aStream );        
       
   192         }
       
   193 
       
   194     DLTRACEOUT((""));    
       
   195     }
       
   196 
       
   197 
       
   198 void CNcdNodeUserData::InternalizeL( RReadStream& aStream )
       
   199     {
       
   200     DLTRACEIN((""));
       
   201 
       
   202     // Read the class id first because it is set to the stream in internalize
       
   203     // function and it is not read from the stream anywhere else.
       
   204     TInt classId( aStream.ReadInt32L() );
       
   205     if ( classId != ClassId() )
       
   206         {
       
   207         DLTRACE(("Wrong class id"));
       
   208         DASSERT( EFalse );
       
   209         // Leave because the stream does not match this class object
       
   210         User::Leave( KErrCorrupt );
       
   211         }
       
   212     
       
   213     // Only the user data is part of the stream
       
   214     // If the length was set to zero, then this function will set the value
       
   215     // to KNullDesC8. So, after internalization the iUserData will not be NULL.
       
   216     InternalizeDesL( iUserData, aStream );        
       
   217 
       
   218     DLTRACEOUT(("Data: %S", iUserData));
       
   219     
       
   220     }
       
   221 
       
   222 
       
   223 void CNcdNodeUserData::UserDataRequestL( MCatalogsBaseMessage& aMessage )
       
   224     {
       
   225     DLTRACEIN((""));
       
   226 
       
   227     if ( iUserData == NULL )
       
   228         {
       
   229         TInt trapError( KErrNone );
       
   230         
       
   231         // This function will read data from the database and call
       
   232         // InternalizeL which sets the data to the user data variable.    
       
   233         TRAP( trapError,
       
   234               iManager.DbLoadUserDataL( *iIdentifier,
       
   235                                         *this ) );
       
   236 
       
   237         if ( trapError == KErrNotFound )
       
   238             {
       
   239             // User data was not found from the db.
       
   240             // So, set it to KNullDesC8
       
   241             iUserData = KNullDesC8().AllocL();
       
   242             }
       
   243         else if ( trapError != KErrNone )
       
   244             {        
       
   245             // Only KErrNotFound is allowed error.
       
   246             // Otherwise leave if error occurred.
       
   247             User::Leave( trapError );
       
   248             }
       
   249         }
       
   250                                           
       
   251     // iUserData is always up to date because the data is read from the db
       
   252     // when this object is created. Also, when the data is updated the latest
       
   253     // info will be set into the iUserData same time when it is updated to the
       
   254     // db.
       
   255 
       
   256     // Send complete information back to proxy.
       
   257     aMessage.CompleteAndReleaseL( *iUserData, KErrNone );
       
   258         
       
   259     DLTRACEOUT((""));
       
   260     }
       
   261 
       
   262 void CNcdNodeUserData::SetUserDataRequestL( MCatalogsBaseMessage& aMessage )
       
   263     {
       
   264     DLTRACEIN((""));
       
   265 
       
   266     // Get the session that will contain the user data
       
   267     MCatalogsSession& requestSession( aMessage.Session() );
       
   268 
       
   269     // Get the user data
       
   270     HBufC8* data = HBufC8::NewLC( aMessage.InputLength() );
       
   271     
       
   272     // Read data that proxy sent into the created buffer.
       
   273     TPtr8 dataPtr( data->Des() );
       
   274     TInt err = aMessage.ReadInput( dataPtr );
       
   275 
       
   276     User::LeaveIfError( err );
       
   277 
       
   278     DLTRACE(("Replace old information"));
       
   279     
       
   280     // Replace old information by the new.
       
   281     delete iUserData;
       
   282     iUserData = data;
       
   283     CleanupStack::Pop( data );
       
   284         
       
   285     // ExternalizeL of this class object will be called 
       
   286     // and the iUserData is saved to the stream there.
       
   287     iManager.DbSaveUserDataL( *iIdentifier, *this );        
       
   288 
       
   289     DLTRACE(("Complete message"));
       
   290     
       
   291     // Send complete information back to proxy.
       
   292     aMessage.CompleteAndRelease( KErrNone );
       
   293     
       
   294     DLTRACEOUT((""));
       
   295     }
       
   296 
       
   297 void CNcdNodeUserData::ClearUserDataRequestL( MCatalogsBaseMessage& aMessage )
       
   298     {
       
   299     DLTRACEIN((""));
       
   300 
       
   301     // Clear the db data using the manager.
       
   302     iManager.DbRemoveUserDataL( *iIdentifier ); 
       
   303 
       
   304     // Also, delete the old info from here and the user data empty.
       
   305     HBufC8* tmpUserData = KNullDesC8().AllocL();
       
   306     delete iUserData;
       
   307     iUserData = tmpUserData;
       
   308 
       
   309     // Send complete information back to proxy.
       
   310     aMessage.CompleteAndRelease( KErrNone );
       
   311             
       
   312     DLTRACEOUT((""));
       
   313     }
       
   314                 
       
   315 void CNcdNodeUserData::ReleaseRequest( MCatalogsBaseMessage& aMessage ) const
       
   316     {
       
   317     DLTRACEIN((""));
       
   318 
       
   319     // Decrease the reference count for this object.
       
   320     // When the reference count reaches zero, this object will be destroyed
       
   321     // and removed from the session.
       
   322     MCatalogsSession& requestSession( aMessage.Session() );
       
   323     TInt handle( aMessage.Handle() );
       
   324 
       
   325     // Send complete information back to proxy.
       
   326     aMessage.CompleteAndRelease( KErrNone );
       
   327         
       
   328     // Remove this object from the session.
       
   329     requestSession.RemoveObject( handle );
       
   330         
       
   331     DLTRACEOUT((""));
       
   332     }