ncdengine/provider/server/src/ncdnodepreviewimpl.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 CNcdNodePreview class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "ncdnodepreviewimpl.h"
       
    20 #include "ncdnodemetadataimpl.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 #include "ncd_pp_download.h"
       
    32 #include "ncdnodemanager.h"
       
    33 #include "ncdproviderdefines.h"
       
    34 #include "ncdpreviewmanager.h"
       
    35 #include "ncderrors.h"
       
    36 
       
    37 
       
    38 CNcdNodePreview::CNcdNodePreview( const NcdNodeClassIds::TNcdNodeClassId aClassId,
       
    39                                   CNcdNodeMetaData& aParentMetaData,
       
    40                                   CNcdNodeManager& aNodeManager )
       
    41 : CNcdCommunicable(),
       
    42   iClassId( aClassId ),
       
    43   iParentMetaData( aParentMetaData ),
       
    44   iNodeManager( aNodeManager )
       
    45     {
       
    46     }
       
    47 
       
    48 void CNcdNodePreview::ConstructL()
       
    49     {
       
    50     }
       
    51 
       
    52 
       
    53 CNcdNodePreview* CNcdNodePreview::NewL( CNcdNodeMetaData& aParentMetaData,
       
    54                                         CNcdNodeManager& aNodeManager )
       
    55     {
       
    56     CNcdNodePreview* self =   
       
    57         CNcdNodePreview::NewLC( aParentMetaData, aNodeManager );
       
    58     CleanupStack::Pop( self );
       
    59     return self;        
       
    60     }
       
    61 
       
    62 CNcdNodePreview* CNcdNodePreview::NewLC( CNcdNodeMetaData& aParentMetaData,
       
    63                                          CNcdNodeManager& aNodeManager )
       
    64     {
       
    65     CNcdNodePreview* self = 
       
    66         new( ELeave ) CNcdNodePreview( 
       
    67             NcdNodeClassIds::ENcdNodePreviewClassId, 
       
    68             aParentMetaData,
       
    69             aNodeManager );
       
    70     CleanupClosePushL( *self );
       
    71     self->ConstructL();
       
    72     return self;        
       
    73     }
       
    74 
       
    75 
       
    76 CNcdNodePreview::~CNcdNodePreview()
       
    77     {
       
    78     DLTRACEIN((""));
       
    79 
       
    80     iPreviewMimeTypes.ResetAndDestroy();
       
    81     iUris.ResetAndDestroy();
       
    82 
       
    83     DLTRACEOUT((""));
       
    84     }        
       
    85 
       
    86 NcdNodeClassIds::TNcdNodeClassId CNcdNodePreview::ClassId() const
       
    87     {
       
    88     return iClassId;
       
    89     }
       
    90 
       
    91 
       
    92 // Internalization from the protocol
       
    93 
       
    94 void CNcdNodePreview::InternalizeL( MNcdPreminetProtocolDataEntity& aData )
       
    95     {
       
    96     DLTRACEIN((""));
       
    97 
       
    98     iFileCount = aData.PreviewCount();
       
    99     
       
   100     HBufC* mimetype;
       
   101     HBufC* uri;
       
   102     iPreviewMimeTypes.ResetAndDestroy();
       
   103     iUris.ResetAndDestroy();
       
   104 
       
   105     for( int i = 0; i < iFileCount; i++ )
       
   106         {
       
   107         mimetype = aData.PreviewL( i ).Mime().AllocLC();
       
   108         iPreviewMimeTypes.AppendL( mimetype );
       
   109         CleanupStack::Pop( mimetype );
       
   110         mimetype = 0;
       
   111 
       
   112         uri = aData.PreviewL( i ).Uri().AllocLC();
       
   113         iUris.AppendL( uri );
       
   114         CleanupStack::Pop( uri );
       
   115         uri = 0;        
       
   116         }
       
   117 
       
   118     // Try to update mime types from already downloaded previews
       
   119     // if the server didn't send any for this
       
   120     UpdateMimesFromPreviewManagerL();
       
   121     DLTRACEOUT((""));
       
   122     }
       
   123 
       
   124 
       
   125 // Internalization from and externalization to the database
       
   126     
       
   127 void CNcdNodePreview::ExternalizeL( RWriteStream& aStream )
       
   128     {
       
   129     DLTRACEIN((""));
       
   130 
       
   131     // Set all the membervariable values to the stream. So,
       
   132     // that the stream may be used later to create a new
       
   133     // object.
       
   134 
       
   135     // First insert data that the creator of this class object will use to
       
   136     // create this class object. Class id informs what class object
       
   137     // will be created.
       
   138     
       
   139     aStream.WriteInt32L( ClassId() );
       
   140     
       
   141     aStream.WriteInt16L( iFileCount );
       
   142     
       
   143     for( int i = 0; i < iFileCount; i++ )
       
   144         {
       
   145         ExternalizeDesL( *iPreviewMimeTypes[i], aStream );
       
   146         }
       
   147     
       
   148     for( int i = 0; i < iFileCount; i++ )
       
   149         {
       
   150         ExternalizeDesL( *iUris[i], aStream );
       
   151         }
       
   152         
       
   153     DLTRACEOUT((""));
       
   154     }
       
   155 
       
   156 
       
   157 void CNcdNodePreview::InternalizeL( RReadStream& aStream )
       
   158     {
       
   159     DLTRACEIN((""));
       
   160 
       
   161     // Read the class id first because it is set to the stream in internalize
       
   162     // function and it is not read from the stream anywhere else.
       
   163     TInt classId( aStream.ReadInt32L() );
       
   164     if ( classId != ClassId() )
       
   165         {
       
   166         DLERROR(("Wrong class id"));
       
   167         DASSERT( EFalse );
       
   168         // Leave because the stream does not match this class object
       
   169         User::Leave( KErrCorrupt );
       
   170         }
       
   171     
       
   172     iFileCount = aStream.ReadInt16L();
       
   173 
       
   174     HBufC* previewMimeType = 0;
       
   175     iPreviewMimeTypes.ResetAndDestroy();
       
   176     for( int i = 0; i < iFileCount; i++ )
       
   177         {
       
   178         InternalizeDesL( previewMimeType, aStream );
       
   179         CleanupStack::PushL( previewMimeType );
       
   180         iPreviewMimeTypes.AppendL( previewMimeType );
       
   181         CleanupStack::Pop( previewMimeType );
       
   182         }
       
   183         
       
   184     HBufC* uri = 0;
       
   185     iUris.ResetAndDestroy();
       
   186     for( int i = 0; i < iFileCount; i++ )
       
   187         {
       
   188         InternalizeDesL( uri, aStream );
       
   189         CleanupStack::PushL( uri );
       
   190         iUris.AppendL( uri );
       
   191         CleanupStack::Pop( uri );
       
   192         }
       
   193     
       
   194     // Try to update MIME from downloaded previews
       
   195     UpdateMimesFromPreviewManagerL();
       
   196     DLTRACEOUT((""));
       
   197     }
       
   198 
       
   199 
       
   200 void CNcdNodePreview::ReceiveMessage( MCatalogsBaseMessage* aMessage,
       
   201                                   TInt aFunctionNumber )
       
   202     {
       
   203     DLTRACEIN((""));    
       
   204 
       
   205     DASSERT( aMessage );
       
   206     
       
   207     // Now, we can be sure that rest of the time iMessage exists.
       
   208     // This member variable is set for the CounterPartLost function.
       
   209     iMessage = aMessage;
       
   210     
       
   211     TInt trapError( KErrNone );
       
   212     
       
   213     // Check which function is called by the proxy side object.
       
   214     // Function number are located in ncdnodefunctinoids.h file.
       
   215     switch( aFunctionNumber )
       
   216         {
       
   217         case NcdNodeFunctionIds::ENcdInternalize:
       
   218             // Internalize the proxy side according to the data
       
   219             // of this object.
       
   220             TRAP( trapError, InternalizeRequestL( *aMessage ) );
       
   221             break;
       
   222 
       
   223         case NcdNodeFunctionIds::ENcdPreviewOpenFile:
       
   224             TRAP( trapError, OpenPreviewFileL( *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         case NcdNodeFunctionIds::ENcdIsPreviewLoaded:
       
   234             TRAP( trapError, IsPreviewLoadedL( *aMessage ));
       
   235             break;
       
   236         
       
   237         default:
       
   238             DLERROR(("Unidentified function request"));
       
   239             DASSERT( EFalse );
       
   240             break;
       
   241         }
       
   242 
       
   243     if ( trapError != KErrNone )
       
   244         {
       
   245         // Because something went wrong, the complete has not been
       
   246         // yet called for the message.
       
   247         // So, inform the client about the error if the
       
   248         // message is still available.
       
   249         aMessage->CompleteAndRelease( trapError );
       
   250         }
       
   251 
       
   252     // Because the message should not be used after this, set it NULL.
       
   253     // So, CounterPartLost function will know that no messages are
       
   254     // waiting the response at the moment.
       
   255     iMessage = NULL;        
       
   256     
       
   257     DLTRACEOUT((""));
       
   258     }
       
   259 
       
   260 void CNcdNodePreview::CounterPartLost( const MCatalogsSession& aSession )
       
   261     {
       
   262     // This function may be called whenever -- when the message is waiting
       
   263     // response or when the message does not exist.
       
   264     // iMessage may be NULL here, because in the end of the
       
   265     // ReceiveMessage it is set to NULL. The life time of the message
       
   266     // ends shortly after CompleteAndRelease is called.
       
   267     if ( iMessage != NULL )
       
   268         {
       
   269         iMessage->CounterPartLost( aSession );
       
   270         }
       
   271     }
       
   272                 
       
   273 
       
   274 void CNcdNodePreview::InternalizeRequestL( MCatalogsBaseMessage& aMessage )
       
   275     {
       
   276     DLTRACEIN((""));
       
   277     
       
   278     CBufBase* buf = CBufFlat::NewL( KBufExpandSize );
       
   279     CleanupStack::PushL( buf );
       
   280     
       
   281     RBufWriteStream stream( *buf );
       
   282     CleanupClosePushL( stream );
       
   283 
       
   284 
       
   285     // Include all the necessary node data to the stream
       
   286     ExternalizeDataForRequestL( stream );     
       
   287     
       
   288     
       
   289     // Commits data to the stream when closing.
       
   290     CleanupStack::PopAndDestroy( &stream );
       
   291 
       
   292 
       
   293     // If this leaves, ReceiveMessage will complete the message.
       
   294     // NOTE: that here we expect that the buffer contains at least
       
   295     // some data. So, make sure that ExternalizeDataForRequestL inserts
       
   296     // something to the buffer.
       
   297     aMessage.CompleteAndReleaseL( buf->Ptr( 0 ), KErrNone );        
       
   298         
       
   299     
       
   300     DLINFO(("Deleting the buf"));
       
   301     CleanupStack::PopAndDestroy( buf );
       
   302         
       
   303     DLTRACEOUT((""));
       
   304     }
       
   305     
       
   306 
       
   307 void CNcdNodePreview::ExternalizeDataForRequestL( RWriteStream& aStream )
       
   308     {
       
   309     DLTRACEIN((""));
       
   310 
       
   311     if ( IsObsolete() )
       
   312         {
       
   313         DLINFO(("Set as obsolete. This means that server has removed the object."));
       
   314         User::Leave( KNcdErrorObsolete );
       
   315         }
       
   316 
       
   317     aStream.WriteInt32L( ClassId() );
       
   318     
       
   319     aStream.WriteInt16L( iFileCount );
       
   320 
       
   321     for( int i = 0; i < iFileCount; i++ )
       
   322         {
       
   323         ExternalizeDesL( *iPreviewMimeTypes[i], aStream );
       
   324         }
       
   325     
       
   326     DLTRACEOUT((""));
       
   327     }
       
   328 
       
   329 void CNcdNodePreview::ReleaseRequest( MCatalogsBaseMessage& aMessage ) const
       
   330     {
       
   331     DLTRACEIN((""));
       
   332 
       
   333     // Decrease the reference count for this object.
       
   334     // When the reference count reaches zero, this object will be destroyed
       
   335     // and removed from the session.
       
   336     MCatalogsSession& requestSession( aMessage.Session() );
       
   337     TInt handle( aMessage.Handle() );
       
   338 
       
   339     // Send complete information back to proxy.
       
   340     aMessage.CompleteAndRelease( KErrNone );
       
   341         
       
   342     // Remove this object from the session.
       
   343     requestSession.RemoveObject( handle );
       
   344         
       
   345     DLTRACEOUT((""));
       
   346     }
       
   347 
       
   348 void CNcdNodePreview::OpenPreviewFileL( MCatalogsBaseMessage& aMessage )
       
   349     {
       
   350     DLTRACEIN((""));
       
   351     RBuf8 buf;
       
   352     buf.CreateL( aMessage.InputLength() );
       
   353     CleanupClosePushL( buf );
       
   354     User::LeaveIfError( aMessage.ReadInput( buf ) );
       
   355     
       
   356     // Read the requested file index    
       
   357     TInt fileIndex = Des8ToInt( buf );
       
   358 
       
   359     // Open a new file server session because using the same session
       
   360     // for shared and non-shared files is a security risk     
       
   361     RFs fs;
       
   362     CleanupClosePushL( fs );
       
   363     User::LeaveIfError( fs.Connect() );
       
   364 
       
   365     // Share the file server session
       
   366     User::LeaveIfError( fs.ShareProtected() );
       
   367 
       
   368     DLTRACE(("Open preview file"));
       
   369     RFile openFile = iNodeManager.PreviewManager().PreviewL( 
       
   370             fs,
       
   371             iParentMetaData.Identifier(), 
       
   372             *iUris[fileIndex] );
       
   373             
       
   374     CleanupClosePushL( openFile );    
       
   375     
       
   376     DLINFO(("File open, transferring to client"));
       
   377     aMessage.CompleteAndReleaseL( fs, 
       
   378                                   openFile );
       
   379 
       
   380     // a shared file must be closed on the server side too
       
   381     // also the shared file session must be closed 
       
   382     CleanupStack::PopAndDestroy( 2, &fs ); // openFile, fs
       
   383     
       
   384     CleanupStack::PopAndDestroy( &buf );                    
       
   385     
       
   386     DLTRACEOUT((""));
       
   387     }
       
   388 
       
   389 
       
   390 TInt CNcdNodePreview::UriCount() const
       
   391     {
       
   392     return iUris.Count();
       
   393     }
       
   394 
       
   395 
       
   396 const TDesC& CNcdNodePreview::Uri( TInt aIndex ) const
       
   397     {
       
   398     return *(iUris[aIndex]);
       
   399     }
       
   400 
       
   401 
       
   402 void CNcdNodePreview::UpdateMimesFromPreviewManagerL()
       
   403     {
       
   404     DLTRACEIN((""));
       
   405     CNcdPreviewManager& previewManager( iNodeManager.PreviewManager() );
       
   406     const CNcdNodeIdentifier& metaId( iParentMetaData.Identifier() );
       
   407     
       
   408     TInt count = iFileCount; // use iFileCount since it's the "real" count
       
   409     while ( count-- ) 
       
   410         {
       
   411         if ( !iPreviewMimeTypes[ count ]->Length() ) 
       
   412             {            
       
   413             const TDesC& mime( previewManager.PreviewMimeType( 
       
   414                 metaId,
       
   415                 *iUris[ count ] ) );
       
   416                 
       
   417             // assign only if there's something to assign
       
   418             if ( mime.Length() ) 
       
   419                 {                
       
   420                 AssignDesL( iPreviewMimeTypes[ count ], mime );
       
   421                 }
       
   422             }        
       
   423         }
       
   424     }
       
   425 
       
   426 
       
   427 void CNcdNodePreview::IsPreviewLoadedL( MCatalogsBaseMessage& aMessage )
       
   428     {
       
   429     RBuf8 buf;
       
   430     buf.CreateL( aMessage.InputLength() );
       
   431     CleanupClosePushL( buf );
       
   432     User::LeaveIfError( aMessage.ReadInput( buf ) );
       
   433 
       
   434     TInt fileIndex = Des8ToInt( buf );
       
   435 
       
   436     TInt isLoaded = iNodeManager.PreviewManager().PreviewExists( 
       
   437         iParentMetaData.Identifier(), 
       
   438         *iUris[fileIndex] );
       
   439     
       
   440     CleanupStack::PopAndDestroy( &buf );                    
       
   441     
       
   442     aMessage.CompleteAndReleaseL( isLoaded, KErrNone );                                  
       
   443     
       
   444     }