messagingapp/msgutils/unidatautils/unidatamodel/src/UniSmilList.cpp
changeset 25 84d9eb65b26f
equal deleted inserted replaced
23:238255e8b033 25:84d9eb65b26f
       
     1 /*
       
     2 * Copyright (c) 2005 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: 
       
    15 *       CUniSmilList, List of attachments.
       
    16 *
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // ========== INCLUDE FILES ================================
       
    22 
       
    23 #include <e32std.h>
       
    24 #include <e32def.h>     // for basic types
       
    25 #include <eikenv.h>     // for CBase
       
    26 #include <mtclbase.h>   // for CBaseMtm
       
    27 #include <msvstd.h>     // for TMsvId
       
    28 #include <msvids.h>     // for KMsvTempIndexEntryId
       
    29 #include <msvstore.h>
       
    30 #include <mmsvattachmentmanager.h>
       
    31 #include <mmsvattachmentmanagersync.h>
       
    32 #include <cmsvattachment.h>
       
    33 #include <cmsvmimeheaders.h>
       
    34 
       
    35 #include "msgtextutils.h"
       
    36 #include "UniSmilList.h"
       
    37 #include "MsgMimeTypes.h"
       
    38 #include "MuiuOperationWait.h"
       
    39 
       
    40 // ========== EXTERNAL DATA STRUCTURES =====================
       
    41 
       
    42 // ========== EXTERNAL FUNCTION PROTOTYPES =================
       
    43 
       
    44 // ========== CONSTANTS ====================================
       
    45 
       
    46 // ========== MACROS =======================================
       
    47 
       
    48 // ========== LOCAL CONSTANTS AND MACROS ===================
       
    49 
       
    50 _LIT16( KPresentationFileName, "pres.smil" );
       
    51 
       
    52 // ========== MODULE DATA STRUCTURES =======================
       
    53 
       
    54 // ========== LOCAL FUNCTION PROTOTYPES ====================
       
    55 
       
    56 // ========== LOCAL FUNCTIONS ==============================
       
    57 
       
    58 // ========== MEMBER FUNCTIONS =============================
       
    59 
       
    60 // ---------------------------------------------------------
       
    61 // CUniSmilList::NewLC
       
    62 //
       
    63 // Factory method.
       
    64 // ---------------------------------------------------------
       
    65 //
       
    66 EXPORT_C CUniSmilList* CUniSmilList::NewL( CBaseMtm& aMtm )
       
    67     {
       
    68     CUniSmilList* self = new(ELeave) CUniSmilList( aMtm );
       
    69     CleanupStack::PushL( self );
       
    70     self->ConstructL();
       
    71     CleanupStack::Pop( self );
       
    72     return self;
       
    73     }
       
    74 
       
    75 
       
    76 // ---------------------------------------------------------
       
    77 // CUniSmilList::CUniSmilList
       
    78 //
       
    79 // Constructor.
       
    80 // ---------------------------------------------------------
       
    81 //
       
    82 CUniSmilList::CUniSmilList( CBaseMtm& aMtm ) :
       
    83     CActive( EPriorityStandard ),
       
    84     iMtm( aMtm ),
       
    85     iSmilAttachmentArray( NULL )
       
    86     {
       
    87     CActiveScheduler::Add( this );
       
    88     }
       
    89 
       
    90 
       
    91 // ---------------------------------------------------------
       
    92 // CUniSmilList::CUniSmilList
       
    93 //
       
    94 // Destructor.
       
    95 // ---------------------------------------------------------
       
    96 //
       
    97 CUniSmilList::~CUniSmilList()
       
    98     {
       
    99     Cancel();
       
   100 
       
   101     delete iSmilAttachmentArray;
       
   102     delete iComposer;
       
   103 
       
   104     if ( iEditFile )
       
   105         {
       
   106         iEditFile->Close();
       
   107         delete iEditFile;
       
   108         }
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------
       
   112 // CUniSmilList::AddSmilAttachmentL
       
   113 //
       
   114 // Add SMIL attachment
       
   115 // ---------------------------------------------------------
       
   116 //
       
   117 EXPORT_C void CUniSmilList::AddSmilAttachmentL(
       
   118     MMsvAttachmentManager& aManager,
       
   119     TMsvAttachmentId aAttachmentId )
       
   120     {
       
   121     RFile file = aManager.GetAttachmentFileL( aAttachmentId );
       
   122     CleanupClosePushL( file );
       
   123     TInt size( 0 );
       
   124     User::LeaveIfError( file.Size( size ) );
       
   125     CleanupStack::PopAndDestroy( &file );
       
   126     iSmilAttachmentArray->AppendL( aAttachmentId );
       
   127     iSmilSize += size;
       
   128     }
       
   129 
       
   130 // ---------------------------------------------------------
       
   131 // CUniSmilList::CreateSmilAttachmentL
       
   132 //
       
   133 // Create SMIL from DOM
       
   134 // ---------------------------------------------------------
       
   135 //
       
   136 EXPORT_C void CUniSmilList::CreateSmilAttachmentL(
       
   137         MUniSmilComposeObserver& aObserver,
       
   138         CMsvStore& aEditStore,
       
   139         CMDXMLDocument* aDom )
       
   140     {
       
   141     iDom = aDom;
       
   142     iObserver = &aObserver;
       
   143     //iSmilAttaId = KMsvNullIndexEntryId;
       
   144     //iSmilAttaFullpath.Zero();
       
   145 
       
   146     iManager = &( aEditStore.AttachmentManagerL() );
       
   147     CMsvAttachment* attachment = CMsvAttachment::NewL( CMsvAttachment::EMsvFile );
       
   148     CleanupStack::PushL( attachment );
       
   149 
       
   150     //TODO: Set size(?) & complete flag(?)
       
   151     //attachment->SetSize()
       
   152     //attachment->SetComplete( EFalse );
       
   153 
       
   154     iEditFile = new ( ELeave ) RFile;
       
   155     CMuiuOperationWait* wait = CMuiuOperationWait::NewLC();
       
   156     iManager->CreateAttachmentL( KPresentationFileName, *iEditFile, attachment, wait->iStatus );
       
   157     wait->Start();
       
   158     //manager.AddAttachmentL( aPath, attachment, iStatus );
       
   159     CleanupStack::PopAndDestroy( wait );	
       
   160 
       
   161     CleanupStack::Pop( attachment );
       
   162     iAttachment = attachment;
       
   163 
       
   164     //Do self complete to trigger runL 
       
   165     RunL();
       
   166     }
       
   167 
       
   168 // ---------------------------------------------------------
       
   169 // CUniSmilList::GetSmilAttachmentByIndex
       
   170 //
       
   171 // GetSmilAttachmentByIndex.
       
   172 // ---------------------------------------------------------
       
   173 //
       
   174 EXPORT_C TMsvAttachmentId CUniSmilList::GetSmilAttachmentByIndex( TInt aIndex )
       
   175     {
       
   176     if ( aIndex < 0 || aIndex >= iSmilAttachmentArray->Count() )
       
   177         {
       
   178         return KMsvNullIndexEntryId;
       
   179         }
       
   180     return iSmilAttachmentArray->At( aIndex );
       
   181     }
       
   182 
       
   183 // ---------------------------------------------------------
       
   184 // CUniSmilList::GetSmilFullPathByIndex
       
   185 //
       
   186 // GetSmilFullPathByIndex.
       
   187 // ---------------------------------------------------------
       
   188 //
       
   189 EXPORT_C RFile CUniSmilList::GetSmilFileByIndexL(
       
   190     TInt aIndex )
       
   191     {
       
   192     if ( aIndex < 0 || aIndex >= iSmilAttachmentArray->Count() )
       
   193         {
       
   194         User::Leave( KErrArgument );
       
   195         }
       
   196     CMsvStore* store = iMtm.Entry().ReadStoreL();
       
   197     CleanupStack::PushL( store );
       
   198     MMsvAttachmentManager& manager = store->AttachmentManagerL();
       
   199     RFile file = manager.GetAttachmentFileL( iSmilAttachmentArray->At( aIndex ) );
       
   200     CleanupStack::PopAndDestroy( store );
       
   201     return file;
       
   202     }
       
   203 
       
   204 // ---------------------------------------------------------
       
   205 // CUniSmilList::ConstructL
       
   206 //
       
   207 // 2nd phase constructor.
       
   208 // ---------------------------------------------------------
       
   209 //
       
   210 void CUniSmilList::ConstructL()
       
   211     {
       
   212     iSmilAttachmentArray = new ( ELeave ) CArrayFixFlat<TMsvAttachmentId>( 2 );
       
   213     }
       
   214 
       
   215 // ---------------------------------------------------------
       
   216 // CUniSmilList::ComposeFileCompleteL
       
   217 // ---------------------------------------------------------
       
   218 //
       
   219 void CUniSmilList::ComposeFileCompleteL()
       
   220     {
       
   221     CMsvMimeHeaders* msvMime = CMsvMimeHeaders::NewLC();
       
   222 
       
   223     TInt slash = KMsgMimeSmil().Locate( '/' );
       
   224     msvMime->SetSuggestedFilenameL( KPresentationFileName );
       
   225     msvMime->SetContentTypeL( KMsgMimeSmil().Left( slash ) );
       
   226     msvMime->SetContentSubTypeL( KMsgMimeSmil().Mid( slash + 1 ) );
       
   227     msvMime->SetMimeCharset( KCharacterSetMIBEnumUtf8 );
       
   228     msvMime->StoreL( *iAttachment );
       
   229     CleanupStack::PopAndDestroy( msvMime );
       
   230 
       
   231     iAttachment->SetMimeTypeL( KMsgMimeSmil );
       
   232     // TODO: iEditFile is already closed? How to get the file size?
       
   233     //iAttachment->SetSize( 0 );
       
   234     TMsvAttachmentId smilId = iAttachment->Id();
       
   235     AddSmilAttachmentL( *iManager, smilId );
       
   236 
       
   237     // TODO: Cleanup attachment in some cases?
       
   238     //       -> Just do use Revert() instead of CommitL()?
       
   239     iManager = NULL;
       
   240     iAttachment = NULL;
       
   241 
       
   242     iObserver->SmilComposeEvent( KErrNone );
       
   243     
       
   244     if ( iObjWait.IsStarted() )
       
   245     {
       
   246     iObjWait.AsyncStop();
       
   247     }    
       
   248 
       
   249     }
       
   250 
       
   251 // ---------------------------------------------------------
       
   252 // CUniSmilList::DoCancel
       
   253 //
       
   254 // ---------------------------------------------------------
       
   255 //
       
   256 void CUniSmilList::DoCancel()
       
   257     {
       
   258     if ( iManager )
       
   259         {
       
   260         iManager->CancelRequest();
       
   261         }
       
   262     }
       
   263 
       
   264 // ---------------------------------------------------------
       
   265 // CUniSmilList::RunL
       
   266 //
       
   267 // ---------------------------------------------------------
       
   268 //
       
   269 void CUniSmilList::RunL()
       
   270     {
       
   271     if ( !iComposer )
       
   272         {
       
   273         iComposer = CMDXMLComposer::NewL( this, EFalse );
       
   274         }
       
   275     iComposer->ComposeFile( *iEditFile, iDom, EAscii );
       
   276     if( !iObjWait.IsStarted() )
       
   277     {
       
   278     iObjWait.Start();
       
   279     }
       
   280     delete iEditFile;
       
   281     iEditFile = NULL; // Ownership transferred.
       
   282     }
       
   283 
       
   284 // ---------------------------------------------------------
       
   285 // CUniSmilList::RunError
       
   286 //
       
   287 // ---------------------------------------------------------
       
   288 //
       
   289 TInt CUniSmilList::RunError( TInt /*aError*/ )
       
   290     {
       
   291     return KErrNone;
       
   292     }
       
   293 
       
   294 
       
   295