ncdengine/provider/server/src/ncdnodescreenshotimpl.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 CNcdNodeScreenshot class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "ncdnodescreenshotimpl.h"
       
    20 #include "catalogssession.h"
       
    21 #include "catalogsbasemessage.h"
       
    22 #include "ncdnodemanager.h"
       
    23 #include "ncdnodemetadataimpl.h"
       
    24 #include "ncdnodeidentifier.h"
       
    25 #include "ncdnodefunctionids.h"
       
    26 #include "ncdnodeclassids.h"
       
    27 #include "catalogsconstants.h"
       
    28 #include "ncd_pp_dataentity.h"
       
    29 #include "ncd_pp_downloadimpl.h"
       
    30 #include "ncd_cp_query.h"
       
    31 #include "catalogsutils.h"
       
    32 #include "catalogsdebug.h"
       
    33 #include "ncderrors.h"
       
    34 
       
    35 
       
    36 CNcdNodeScreenshot::CNcdNodeScreenshot(
       
    37     NcdNodeClassIds::TNcdNodeClassId aClassId,
       
    38     CNcdNodeManager& aNodeManager,
       
    39     const CNcdNodeMetaData& aParentMetaData )
       
    40 : CNcdCommunicable(),
       
    41   iClassId( aClassId ),
       
    42   iNodeManager( aNodeManager ),
       
    43   iParentMetaData( aParentMetaData )
       
    44     {
       
    45     }
       
    46 
       
    47 void CNcdNodeScreenshot::ConstructL()
       
    48     {
       
    49     }
       
    50 
       
    51 
       
    52 CNcdNodeScreenshot* CNcdNodeScreenshot::NewL(
       
    53     CNcdNodeManager& aNodeManager,
       
    54     const CNcdNodeMetaData& aParentMetaData )
       
    55     {
       
    56     CNcdNodeScreenshot* self =   
       
    57         CNcdNodeScreenshot::NewLC( aNodeManager, aParentMetaData );
       
    58     CleanupStack::Pop( self );
       
    59     return self;        
       
    60     }
       
    61 
       
    62 CNcdNodeScreenshot* CNcdNodeScreenshot::NewLC(
       
    63     CNcdNodeManager& aNodeManager,
       
    64     const CNcdNodeMetaData& aParentMetaData )
       
    65     {
       
    66     CNcdNodeScreenshot* self = 
       
    67         new( ELeave ) CNcdNodeScreenshot( 
       
    68             NcdNodeClassIds::ENcdNodeScreenshotClassId,
       
    69             aNodeManager,
       
    70             aParentMetaData );
       
    71     CleanupClosePushL( *self );
       
    72     self->ConstructL();
       
    73     return self;        
       
    74     }
       
    75 
       
    76 
       
    77 CNcdNodeScreenshot::~CNcdNodeScreenshot()
       
    78     {
       
    79     DLTRACEIN((""));
       
    80 
       
    81     // Delete member variables here
       
    82 
       
    83     iScreenshotDownload.ResetAndDestroy();
       
    84 
       
    85     DLTRACEOUT((""));
       
    86     }        
       
    87 
       
    88 NcdNodeClassIds::TNcdNodeClassId CNcdNodeScreenshot::ClassId() const
       
    89     {
       
    90     return iClassId;
       
    91     }
       
    92 
       
    93 TInt CNcdNodeScreenshot::ScreenshotDownloadCount() const
       
    94     {
       
    95     return iScreenshotDownload.Count();
       
    96     }
       
    97 
       
    98 const TDesC&
       
    99     CNcdNodeScreenshot::ScreenshotDownloadUri( TInt aIndex ) const
       
   100     {
       
   101     return iScreenshotDownload[aIndex]->Key();
       
   102     }
       
   103 
       
   104 // Internalization from the protocol
       
   105 
       
   106 void CNcdNodeScreenshot::InternalizeL( MNcdPreminetProtocolDataEntity& aData )
       
   107     {
       
   108     DLTRACEIN((""));
       
   109 
       
   110     iScreenshotDownload.ResetAndDestroy();
       
   111     
       
   112     for ( TInt i = 0; i < aData.ScreenshotCount(); i++ )
       
   113         {
       
   114         CNcdKeyValuePair* download = CNcdKeyValuePair::NewLC(
       
   115             aData.ScreenshotL( i ).Uri(),
       
   116             aData.ScreenshotL( i ).Mime() );
       
   117         
       
   118         DLINFO(( _L("Screenshot URI: %S, mime: %S"), 
       
   119             &download->Key(),
       
   120             &download->Value() ));
       
   121             
       
   122         iScreenshotDownload.AppendL( download );
       
   123         CleanupStack::Pop( download );
       
   124         }
       
   125 
       
   126     DLTRACEOUT((""));
       
   127     }
       
   128 
       
   129 
       
   130 // Internalization from and externalization to the database
       
   131     
       
   132 void CNcdNodeScreenshot::ExternalizeL( RWriteStream& aStream )
       
   133     {
       
   134     DLTRACEIN((""));
       
   135 
       
   136     // Set all the membervariable values to the stream. So,
       
   137     // that the stream may be used later to create a new
       
   138     // object.
       
   139 
       
   140     // First insert data that the creator of this class object will use to
       
   141     // create this class object. Class id informs what class object
       
   142     // will be created.
       
   143     
       
   144     aStream.WriteInt32L( ClassId() );
       
   145     
       
   146     
       
   147     // Write the data that will be used when internalize function
       
   148     // is called. catalogsutils.h contains good internalize and
       
   149     // externalize functions.
       
   150     // Make sure that this matches to the order that is used 
       
   151     // when this stream is internalized.
       
   152 
       
   153     TInt count = iScreenshotDownload.Count();
       
   154     aStream.WriteInt32L( count );
       
   155 
       
   156     DLTRACE(("Externalizing %d screenshot URIs and mimes", count ));
       
   157     for ( TInt i = 0; i < count; ++i )
       
   158         {
       
   159         iScreenshotDownload[i]->ExternalizeL( aStream );
       
   160         }
       
   161 
       
   162     DLTRACEOUT((""));
       
   163     }
       
   164 
       
   165 
       
   166 void CNcdNodeScreenshot::InternalizeL( RReadStream& aStream )
       
   167     {
       
   168     DLTRACEIN((""));
       
   169 
       
   170     // Read the class id first because it is set to the stream in internalize
       
   171     // function and it is not read from the stream anywhere else.
       
   172     TInt classId( aStream.ReadInt32L() );
       
   173     if ( classId != ClassId() )
       
   174         {
       
   175         DLERROR(("Wrong class id"));
       
   176         DASSERT( EFalse );
       
   177         // Leave because the stream does not match this class object
       
   178         User::Leave( KErrCorrupt );
       
   179         }    
       
   180     
       
   181     TInt count = aStream.ReadInt32L();
       
   182     DLTRACE(("Internalizing %d screenshot URIs and mimes", count ));
       
   183     
       
   184     iScreenshotDownload.ResetAndDestroy();
       
   185     iScreenshotDownload.ReserveL( count );
       
   186     
       
   187     while ( count-- )
       
   188         {
       
   189         CNcdKeyValuePair* screenshot = CNcdKeyValuePair::NewLC( aStream );
       
   190         iScreenshotDownload.Append( screenshot );
       
   191         CleanupStack::Pop( screenshot );
       
   192         }
       
   193     
       
   194     DLTRACEOUT((""));
       
   195     }
       
   196 
       
   197 
       
   198 
       
   199 void CNcdNodeScreenshot::ReceiveMessage( MCatalogsBaseMessage* aMessage,
       
   200                                   TInt aFunctionNumber )
       
   201     {
       
   202     DLTRACEIN((""));    
       
   203 
       
   204     DASSERT( aMessage );
       
   205     
       
   206     // Now, we can be sure that rest of the time iMessage exists.
       
   207     // This member variable is set for the CounterPartLost function.
       
   208     iMessage = aMessage;
       
   209     
       
   210     TInt trapError( KErrNone );
       
   211     
       
   212     // Check which function is called by the proxy side object.
       
   213     // Function number are located in ncdnodefunctinoids.h file.
       
   214     switch( aFunctionNumber )
       
   215         {
       
   216         case NcdNodeFunctionIds::ENcdInternalize:
       
   217             // Internalize the proxy side according to the data
       
   218             // of this object.
       
   219             TRAP( trapError, InternalizeRequestL( *aMessage ) );
       
   220             break;
       
   221 
       
   222         case NcdNodeFunctionIds::ENcdScreenshotData:
       
   223             // Proxy side requested screenshot data.
       
   224             TRAP( trapError, ScreenshotDataRequestL( *aMessage ) );
       
   225             break;
       
   226 
       
   227         case NcdNodeFunctionIds::ENcdRelease:
       
   228             // The proxy does not want to use this object anymore.
       
   229             // So, release the handle from the session.
       
   230             ReleaseRequest( *aMessage );
       
   231             break;
       
   232                     
       
   233         default:
       
   234             DLERROR(("Unidentified function request"));
       
   235             DASSERT( EFalse );
       
   236             break;
       
   237         }
       
   238 
       
   239     if ( trapError != KErrNone )
       
   240         {
       
   241         // Because something went wrong, the complete has not been
       
   242         // yet called for the message.
       
   243         // So, inform the client about the error if the
       
   244         // message is still available.
       
   245         aMessage->CompleteAndRelease( trapError );
       
   246         }
       
   247 
       
   248     // Because the message should not be used after this, set it NULL.
       
   249     // So, CounterPartLost function will know that no messages are
       
   250     // waiting the response at the moment.
       
   251     iMessage = NULL;        
       
   252     
       
   253     DLTRACEOUT((""));
       
   254     }
       
   255 
       
   256 void CNcdNodeScreenshot::CounterPartLost( const MCatalogsSession& aSession )
       
   257     {
       
   258     // This function may be called whenever -- when the message is waiting
       
   259     // response or when the message does not exist.
       
   260     // iMessage may be NULL here, because in the end of the
       
   261     // ReceiveMessage it is set to NULL. The life time of the message
       
   262     // ends shortly after CompleteAndRelease is called.
       
   263     if ( iMessage != NULL )
       
   264         {
       
   265         iMessage->CounterPartLost( aSession );
       
   266         }
       
   267     }
       
   268                 
       
   269 
       
   270 void CNcdNodeScreenshot::InternalizeRequestL( MCatalogsBaseMessage& aMessage )
       
   271     {
       
   272     DLTRACEIN((""));
       
   273     
       
   274     CBufBase* buf = CBufFlat::NewL( KBufExpandSize );
       
   275     CleanupStack::PushL( buf );
       
   276     
       
   277     RBufWriteStream stream( *buf );
       
   278     CleanupClosePushL( stream );
       
   279 
       
   280 
       
   281     // Include all the necessary node data to the stream
       
   282     ExternalizeDataForRequestL( stream );     
       
   283     
       
   284     
       
   285     // Commits data to the stream when closing.
       
   286     CleanupStack::PopAndDestroy( &stream );
       
   287 
       
   288 
       
   289     // If this leaves, ReceiveMessage will complete the message.
       
   290     // NOTE: that here we expect that the buffer contains at least
       
   291     // some data. So, make sure that ExternalizeDataForRequestL inserts
       
   292     // something to the buffer.
       
   293     aMessage.CompleteAndReleaseL( buf->Ptr( 0 ), KErrNone );        
       
   294         
       
   295     
       
   296     DLTRACE(("Deleting the buf"));
       
   297     CleanupStack::PopAndDestroy( buf );
       
   298         
       
   299     DLTRACEOUT((""));
       
   300     }
       
   301     
       
   302 
       
   303 void CNcdNodeScreenshot::ExternalizeDataForRequestL( RWriteStream& aStream )
       
   304     {
       
   305     DLTRACEIN((""));
       
   306 
       
   307     if ( IsObsolete() )
       
   308         {
       
   309         DLINFO(("Set as obsolete. This means that server has removed the object."));
       
   310         User::Leave( KNcdErrorObsolete );
       
   311         }
       
   312 
       
   313     // Make sure that the id is internalized correctly in the proxy side.
       
   314     aStream.WriteInt32L( ClassId() );
       
   315 
       
   316     // Add additional content to the stream.
       
   317     // Make sure that this matches to the order that is used in the proxy
       
   318     // side when this stream is internalized.
       
   319     // NOTE: Be careful with the 8- and 16-bit descriptors. Remember to check
       
   320     // if the proxy wants the data in 16 or 8 bits?    
       
   321 
       
   322     TInt count = iScreenshotDownload.Count();
       
   323     
       
   324     aStream.WriteInt32L( count );
       
   325     
       
   326     for ( TInt i = 0; i < count; i++ )
       
   327         {
       
   328         ExternalizeDesL( iScreenshotDownload[i]->Value(), aStream );
       
   329         }
       
   330         
       
   331     DLTRACEOUT((""));
       
   332     }
       
   333 
       
   334 
       
   335 void CNcdNodeScreenshot::ScreenshotDataRequestL( MCatalogsBaseMessage& aMessage )
       
   336     {
       
   337     DLTRACEIN((""));
       
   338 
       
   339     // If this leaves, ReceiveMessage will complete the message.    
       
   340 
       
   341     TInt inputLength = aMessage.InputLength();
       
   342     User::LeaveIfError( inputLength );
       
   343     
       
   344     HBufC8* data = HBufC8::NewLC( inputLength );
       
   345     TPtr8 ptr = data->Des();
       
   346     User::LeaveIfError( aMessage.ReadInput( ptr ) );
       
   347 
       
   348     TInt index = Des8ToInt( *data );
       
   349 
       
   350     CleanupStack::PopAndDestroy( data );
       
   351     
       
   352     CNcdNodeIdentifier* shotIdentifier = 
       
   353         CNcdNodeIdentifier::NewLC( iParentMetaData.Identifier().NodeNameSpace(),
       
   354                                    iScreenshotDownload[index]->Key(),
       
   355                                    iScreenshotDownload[index]->Key(),
       
   356                                    iParentMetaData.Identifier().ClientUid() );
       
   357     HBufC8* screenshot = iNodeManager.DbScreenshotDataLC( *shotIdentifier );
       
   358         
       
   359     aMessage.CompleteAndReleaseL( *screenshot, KErrNone );
       
   360 
       
   361     CleanupStack::PopAndDestroy( screenshot );
       
   362     CleanupStack::PopAndDestroy( shotIdentifier );    
       
   363     
       
   364     DLTRACEOUT((""));
       
   365     }
       
   366     
       
   367 
       
   368 void CNcdNodeScreenshot::ReleaseRequest( MCatalogsBaseMessage& aMessage ) const
       
   369     {
       
   370     DLTRACEIN((""));
       
   371 
       
   372     // Decrease the reference count for this object.
       
   373     // When the reference count reaches zero, this object will be destroyed
       
   374     // and removed from the session.
       
   375     MCatalogsSession& requestSession( aMessage.Session() );
       
   376     TInt handle( aMessage.Handle() );
       
   377 
       
   378     // Send complete information back to proxy.
       
   379     aMessage.CompleteAndRelease( KErrNone );
       
   380         
       
   381     // Remove this object from the session.
       
   382     requestSession.RemoveObject( handle );
       
   383         
       
   384     DLTRACEOUT((""));
       
   385     }
       
   386 
       
   387