mobilemessaging/unieditor/model/src/UniDataModel.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     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 *       Provides CUniDataModel class methods.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 // ========== INCLUDE FILES ================================
       
    23 
       
    24 #include <eikenv.h>
       
    25 #include <eikdoc.h>
       
    26 
       
    27 #include <msvstd.h>                     // TMsvEntry
       
    28 #include <msvapi.h>
       
    29 #include <msvuids.h>                    // Msgs Uids
       
    30 #include <mtclbase.h>                   // CBaseMtm
       
    31 #include <mmsvattachmentmanager.h>
       
    32 #include <cmsvattachment.h>
       
    33 #include <cmsvmimeheaders.h>
       
    34 
       
    35 #include <bautils.h>
       
    36 
       
    37 #include <MuiuMsvUiServiceUtilities.h>  //Disk space check
       
    38 #include <MsgAttachmentInfo.h>
       
    39 
       
    40 #include <gmxmldocument.h>
       
    41 #include <gmxmlnode.h>
       
    42 #include <gmxmlelement.h>
       
    43 
       
    44 // Features
       
    45 #include <featmgr.h>	
       
    46 #include <bldvariant.hrh>
       
    47 #include <messagingvariant.hrh>
       
    48 
       
    49 // MsgMedia
       
    50 #include <MsgMimeTypes.h>
       
    51 #include <MsgMediaResolver.h>
       
    52 #include <MsgMediaInfo.h>
       
    53 #include <MsgTextInfo.h>
       
    54 
       
    55 #include <MmsConformance.h>
       
    56 
       
    57 #include "UniDataModel.h"
       
    58 #include "UniSmilModel.h"
       
    59 
       
    60 #include "UniDataUtils.h"
       
    61 #include "UniModelConst.h"
       
    62 #include "UniObjectList.h"
       
    63 #include "UniSmilList.h"
       
    64 #include "UniObject.h"
       
    65 #include "UniTextObject.h"
       
    66 #include "UniMimeInfo.h"
       
    67 
       
    68 #include "UniSmilUtils.h"
       
    69 #include "UniEditorLogging.h"
       
    70 
       
    71 // ========== CONSTANTS ====================================
       
    72 
       
    73 // ---------------------------------------------------------
       
    74 // CUniDataModel::Constructor
       
    75 // ---------------------------------------------------------
       
    76 //
       
    77 CUniDataModel::CUniDataModel( RFs& aFs, CBaseMtm& aMtm ) :
       
    78     iFs( aFs ),
       
    79     iMtm( aMtm ),
       
    80     iSmilModel( NULL ),
       
    81     iDom( NULL )
       
    82     {
       
    83     }
       
    84 
       
    85 // ---------------------------------------------------------
       
    86 // CUniDataModel::NewL
       
    87 // ---------------------------------------------------------
       
    88 //
       
    89 EXPORT_C CUniDataModel* CUniDataModel::NewL( RFs& aFs, CBaseMtm& aMtm )
       
    90     {
       
    91     CUniDataModel* self =  new ( ELeave ) CUniDataModel( aFs, aMtm );
       
    92     CleanupStack::PushL( self );
       
    93     self->ConstructL();
       
    94     CleanupStack::Pop( self );
       
    95     return self;
       
    96     }
       
    97 
       
    98 // ---------------------------------------------------------
       
    99 // CUniDataModel::ConstructL
       
   100 // ---------------------------------------------------------
       
   101 //
       
   102 EXPORT_C void CUniDataModel::ConstructL()
       
   103     {
       
   104     iDataUtils = CUniDataUtils::NewL( iFs );
       
   105     iObjectList = CUniObjectList::NewL( iMtm, *iDataUtils );
       
   106     iAttachmentList = CUniObjectList::NewL( iMtm, *iDataUtils );
       
   107     iSmilList = CUniSmilList::NewL( iMtm );
       
   108     iSmilModel = CUniSmilModel::NewL( iFs, iMtm, *iDataUtils, *iObjectList );
       
   109 
       
   110     iMediaResolver = CMsgMediaResolver::NewL();
       
   111     iMediaResolver->SetCharacterSetRecognition( EFalse );
       
   112     
       
   113     iMmsConformance = CMmsConformance::NewL();
       
   114     iMmsConformance->CheckCharacterSet( EFalse );
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------
       
   118 // CUniDataModel::~CUniDataModel
       
   119 //
       
   120 // Destructor.
       
   121 // ---------------------------------------------------------
       
   122 //
       
   123 EXPORT_C CUniDataModel::~CUniDataModel()
       
   124     {
       
   125     delete iMediaResolver;
       
   126     delete iMmsConformance;
       
   127     delete iSmilModel;
       
   128     delete iObjectList;
       
   129     delete iAttachmentList;
       
   130     delete iSmilList;
       
   131     delete iDataUtils;
       
   132     delete iParser;
       
   133     delete iDom;
       
   134     }
       
   135     
       
   136 // ---------------------------------------------------------
       
   137 // CUniDataModel::RestoreL
       
   138 // 
       
   139 // Initialise the model. 
       
   140 // ---------------------------------------------------------
       
   141 //
       
   142 EXPORT_C void CUniDataModel::RestoreL(
       
   143         MUniDataModelObserver& aObserver,
       
   144         TBool aRemoveCorrupted /*= ETrue*/,
       
   145         TBool aAllowSmil /*= ETrue*/ )
       
   146     {
       
   147     iObserver = &aObserver;
       
   148     iSmilType = ENoSmil;
       
   149     
       
   150     if ( iMtm.Entry().HasStoreL() )
       
   151     	{
       
   152         FindAndHandleCorruptL( aRemoveCorrupted );
       
   153 
       
   154         TBool readOnly = iMtm.Entry().Entry().ReadOnly();
       
   155         CMsvStore* store = readOnly
       
   156         	? iMtm.Entry().ReadStoreL()
       
   157         	: iMtm.Entry().EditStoreL();
       
   158         CleanupStack::PushL( store );
       
   159         MMsvAttachmentManager& manager = store->AttachmentManagerL();
       
   160         CMsvMimeHeaders* msvMime = CMsvMimeHeaders::NewLC();
       
   161 
       
   162         iParseResult = 0;
       
   163 
       
   164         iOrigAttachmentCount = manager.AttachmentCount();
       
   165 
       
   166         for ( TInt i = 0; i < iOrigAttachmentCount ; i++ )
       
   167             {
       
   168             CMsvAttachment* attachment = manager.GetAttachmentInfoL( i );
       
   169             CleanupStack::PushL( attachment );
       
   170             msvMime->RestoreL( *attachment );
       
   171 
       
   172             TBuf8<KMaxDataTypeLength> contentType;
       
   173             contentType.Copy( msvMime->ContentType() );
       
   174             contentType.Append( _L8("/") );
       
   175             contentType.Append( msvMime->ContentSubType() );
       
   176 
       
   177             // Put smil attas to own array, except if SendUi
       
   178             if ( aAllowSmil &&
       
   179                 contentType.CompareF( KMsgMimeSmil ) == 0 )
       
   180                 {
       
   181                 iSmilList->AddSmilAttachmentL( manager, attachment->Id() );
       
   182                 CleanupStack::PopAndDestroy( attachment );
       
   183                 continue;
       
   184                 }
       
   185             
       
   186             RFile readFile = manager.GetAttachmentFileL( i );
       
   187             CleanupClosePushL( readFile );
       
   188             CUniObject* newObj = CreateObjectFromAttachmentLC(
       
   189                 contentType,
       
   190                 readFile,
       
   191                 manager,
       
   192                 *attachment );
       
   193             
       
   194             if ( newObj->Attachment() )
       
   195                 {
       
   196                 iAttachmentList->AddObjectL( newObj );
       
   197                 }
       
   198             else
       
   199                 {
       
   200                 iObjectList->AddObjectL( newObj );
       
   201                 }
       
   202                 
       
   203             CleanupStack::Pop( newObj );
       
   204             CleanupStack::PopAndDestroy( &readFile ); 
       
   205             CleanupStack::PopAndDestroy( attachment );
       
   206             } // for
       
   207 
       
   208         CleanupStack::PopAndDestroy( msvMime );
       
   209         
       
   210         
       
   211         
       
   212         UNILOGGER_WRITE_TIMESTAMP("CUniDataModel::RestoreL commit start");
       
   213         
       
   214         if ( !readOnly && iOrigAttachmentCount > 0 )
       
   215     	    {
       
   216     	    store->CommitL();	
       
   217     	    }
       
   218         
       
   219         UNILOGGER_WRITE_TIMESTAMP("CUniDataModel::RestoreL commit end");
       
   220         CleanupStack::PopAndDestroy( store );
       
   221 
       
   222         if ( iSmilList->Count() == 1 )
       
   223             {
       
   224             RFile smilFile = iDataUtils->GetAttachmentFileL( iMtm, iSmilList->GetSmilAttachmentByIndex( 0 ) );
       
   225             iParser = CMDXMLParser::NewL( this );
       
   226             iParser->ParseFile( smilFile );
       
   227             }
       
   228         else
       
   229             {
       
   230             if ( iSmilList->Count() > 1 )
       
   231                 {
       
   232                 iSmilType = EMultipleSmil;
       
   233                 }
       
   234             iParseResult = iSmilModel->ParseL( NULL ); // Try to create something anyway
       
   235             FinalizeRestoreL( KErrNone );
       
   236             }
       
   237     	}
       
   238     else
       
   239     	{
       
   240         FinalizeRestoreL( KErrNone );
       
   241     	}
       
   242     }
       
   243 
       
   244 // ---------------------------------------------------------
       
   245 // CUniDataModel::AddAttachmentL
       
   246 // ---------------------------------------------------------
       
   247 //
       
   248 EXPORT_C CUniObject* CUniDataModel::AddAttachmentL(
       
   249         MUniObjectSaveObserver& aObserver,
       
   250         CMsgMediaInfo* aMedia,
       
   251         CMsvAttachment::TMsvAttachmentType aSaveType /*= CMsvAttachment::EMsvLinkedFile*/ )
       
   252     {
       
   253     CUniObject* obj = CUniObject::NewLC( iFs, iMtm, *iDataUtils, aMedia );
       
   254     
       
   255     // Update attachment list
       
   256     iAttachmentList->AddObjectL( obj );
       
   257     obj->SetAttachment( ETrue );
       
   258     
       
   259     CleanupStack::Pop( obj );
       
   260     obj->Save( aObserver, aSaveType );
       
   261     
       
   262     return obj;
       
   263     }
       
   264 
       
   265 // ---------------------------------------------------------
       
   266 // CUniDataModel::AddStoredAttachmentL
       
   267 // ---------------------------------------------------------
       
   268 //
       
   269 EXPORT_C CUniObject* CUniDataModel::AddStoredAttachmentL( TMsvAttachmentId aAttachmentId,
       
   270                                                           CMsgMediaInfo* aMedia )
       
   271     {
       
   272     CMsvStore* store = iMtm.Entry().EditStoreL();
       
   273     CleanupStack::PushL( store );
       
   274     
       
   275     MMsvAttachmentManager& manager = store->AttachmentManagerL();
       
   276     MMsvAttachmentManagerSync& managerSync = store->AttachmentManagerExtensionsL();
       
   277     CMsvAttachment* attachment = manager.GetAttachmentInfoL( aAttachmentId );
       
   278     CleanupStack::PushL( attachment );
       
   279     
       
   280     CUniObject* obj = CUniObject::NewLC( iFs, iMtm, *iDataUtils, aMedia, manager, *attachment );
       
   281     
       
   282     managerSync.ModifyAttachmentInfoL( attachment );
       
   283     CleanupStack::Pop( 2, attachment ); //obj, attachment
       
   284     CleanupStack::PushL( obj );
       
   285     store->CommitL();
       
   286 
       
   287     // Update attachment list
       
   288     iAttachmentList->AddObjectL( obj );
       
   289     obj->SetAttachment( ETrue );
       
   290     
       
   291     CleanupStack::Pop( obj );
       
   292     CleanupStack::PopAndDestroy( store );
       
   293     return obj;
       
   294     }
       
   295 
       
   296 // ---------------------------------------------------------
       
   297 // CUniDataModel::FinalizeMediaParse
       
   298 // ---------------------------------------------------------
       
   299 //
       
   300 EXPORT_C void CUniDataModel::FinalizeMediaParse()
       
   301     {
       
   302     while ( iObjectToParse )
       
   303         {
       
   304         iObjWait.Start();
       
   305         }
       
   306     }
       
   307 
       
   308 // ---------------------------------------------------------
       
   309 // CUniDataModel::MediaInfoParsed
       
   310 // ---------------------------------------------------------
       
   311 //
       
   312 void CUniDataModel::MediaInfoParsed()
       
   313     {
       
   314     if ( iObjectToParse->MediaInfo()->Corrupt() )
       
   315         {
       
   316         iObjectToParse->SetCorrupted( ETrue );
       
   317         }
       
   318     ParseNextObject();
       
   319     if ( iObjWait.IsStarted() )
       
   320         {
       
   321         iObjWait.AsyncStop();
       
   322         }
       
   323     }
       
   324 
       
   325 // ---------------------------------------------------------
       
   326 // CUniDataModel::ParseNextObject
       
   327 // ---------------------------------------------------------
       
   328 //
       
   329 void CUniDataModel::ParseNextObject()
       
   330     {
       
   331     CUniObjectList* list = iObjectList;
       
   332 
       
   333     while ( list )
       
   334         {
       
   335         for ( TInt i = 0; i < list->Count(); i++ )
       
   336             {
       
   337             iObjectToParse = list->GetByIndex( i );
       
   338             if ( !iObjectToParse->MediaInfo()->Parsed() &&
       
   339                  !iObjectToParse->MediaInfo()->ParseError() )
       
   340                 {
       
   341                 RFile file;
       
   342                 TRAPD( error, file = iDataUtils->GetAttachmentFileL( iMtm, iObjectToParse->AttachmentId() ) );
       
   343                 
       
   344                 if ( error == KErrNone )
       
   345                     {
       
   346                     iObjectToParse->MediaInfo()->ParseInfoDetails( file, *( iMediaResolver->DRMHelper() ), *this );
       
   347                     file.Close();
       
   348                     return;
       
   349                     }
       
   350                 else
       
   351                     {
       
   352                     iObjectToParse->SetCorrupted( ETrue );
       
   353                     file.Close();
       
   354                     }
       
   355                 }
       
   356             }
       
   357         if ( list == iObjectList )
       
   358             {
       
   359             list = iAttachmentList;
       
   360             }
       
   361         else
       
   362             {
       
   363             list = NULL;
       
   364             }
       
   365         }
       
   366    iObjectToParse = NULL;
       
   367    }
       
   368 
       
   369 // ---------------------------------------------------------
       
   370 // CUniDataModel::ParseFileCompleteL
       
   371 // ---------------------------------------------------------
       
   372 //
       
   373 void CUniDataModel::ParseFileCompleteL()
       
   374     {
       
   375     TRAPD( err, DoParseFileCompleteL() );
       
   376     FinalizeRestoreL( err );
       
   377     }
       
   378     
       
   379 // ---------------------------------------------------------
       
   380 // CUniDataModel::DoParseFileCompleteL
       
   381 // ---------------------------------------------------------
       
   382 //
       
   383 void CUniDataModel::DoParseFileCompleteL()
       
   384     {
       
   385     iDom = iParser->DetachXMLDoc();
       
   386     iSmilType = UniSmilUtils::GetSmilTypeL( iDom );
       
   387     if ( iSmilType == ENoSmil )
       
   388         {
       
   389         iParseResult = iSmilModel->ParseL( NULL );
       
   390         }
       
   391     else if ( iSmilType == EMmsSmil )
       
   392         {
       
   393         TRAPD( err, ( iParseResult = iSmilModel->ParseL( iDom ) ) );
       
   394         if ( err )
       
   395             {
       
   396             iSmilType = E3GPPSmil;
       
   397             iParseResult = 0;
       
   398             FindDomObjectRefCountsL();
       
   399             }
       
   400         }
       
   401     else 
       
   402         {
       
   403         FindDomObjectRefCountsL();
       
   404         }
       
   405     }
       
   406 
       
   407 
       
   408 // ---------------------------------------------------------
       
   409 // CUniDataModel::FinalizeRestoreL
       
   410 // ---------------------------------------------------------
       
   411 //
       
   412 void CUniDataModel::FinalizeRestoreL( TInt aError )
       
   413     {
       
   414     if ( !aError )
       
   415         {
       
   416         // Won't leave.
       
   417         FindAttachmentsL();
       
   418         }
       
   419     iObserver->RestoreReady( iParseResult, aError );
       
   420     ParseNextObject();
       
   421     }
       
   422 
       
   423 // ---------------------------------------------------------
       
   424 // CUniSmilModel::FindAttachmentsL
       
   425 //
       
   426 //
       
   427 // ---------------------------------------------------------
       
   428 //
       
   429 void CUniDataModel::FindAttachmentsL()
       
   430     {
       
   431     for ( TInt i = iObjectList->Count(); --i >= 0; )
       
   432         {
       
   433         CUniObject* obj = iObjectList->GetByIndex( i );
       
   434         if ( !obj->ReferenceCount() )
       
   435             {
       
   436             //don't remove from store -> cannot leave
       
   437             iObjectList->RemoveObjectL( obj, EFalse );
       
   438             iAttachmentList->AddObjectL( obj );
       
   439             obj->SetAttachment( ETrue );
       
   440             }
       
   441         }
       
   442     }
       
   443 
       
   444 // ---------------------------------------------------------
       
   445 // FindAndHandleCorruptL
       
   446 // 
       
   447 // ---------------------------------------------------------
       
   448 void CUniDataModel::FindAndHandleCorruptL( TBool aRemoveCorrupted )
       
   449     {
       
   450     CMsvEntrySelection* corSel = FindCorruptLC();
       
   451     iCorruptedCount = corSel->Count();
       
   452 
       
   453     if ( iCorruptedCount > 0 )
       
   454         {
       
   455         if ( aRemoveCorrupted )
       
   456             {
       
   457             CMsvStore* store = iMtm.Entry().EditStoreL();
       
   458             CleanupStack::PushL( store );
       
   459             MMsvAttachmentManager& manager = store->AttachmentManagerL();
       
   460             MMsvAttachmentManagerSync& managerSync = store->AttachmentManagerExtensionsL();    
       
   461 
       
   462             for (TInt i = iCorruptedCount; --i >= 0 ;)
       
   463                 {
       
   464                 // Editor tries to recover by deleting corrupted ones
       
   465                 // Note: This means that SMIL parsing will fail with ESmil_Missing_Objects. 
       
   466                 //       and object order is not maintained. But better to do it this
       
   467                 //       way so that message can be opened.
       
   468                 TRAP_IGNORE ( managerSync.RemoveAttachmentL( 
       
   469                     CUniDataUtils::IndexPositionOfAttachmentL( manager, corSel->At( i ) ) ) );
       
   470                 }
       
   471             store->CommitL();
       
   472             CleanupStack::PopAndDestroy( store );
       
   473             }
       
   474         else
       
   475             {
       
   476             // In viewer case consider message corrupted and leave.
       
   477             // Model not made viewer should exit.
       
   478             User::Leave( KErrCorrupt );
       
   479             }
       
   480         }
       
   481 
       
   482     CleanupStack::PopAndDestroy( corSel );
       
   483     }
       
   484  
       
   485 // ---------------------------------------------------------
       
   486 // FindCorruptLC
       
   487 // 
       
   488 // ---------------------------------------------------------
       
   489 CMsvEntrySelection* CUniDataModel::FindCorruptLC()
       
   490     {
       
   491     CMsvEntrySelection* corrupts = new(ELeave) CMsvEntrySelection();
       
   492     CleanupStack::PushL( corrupts );
       
   493     
       
   494     if ( iMtm.Entry().HasStoreL() )
       
   495     	{
       
   496         CMsvStore* store = iMtm.Entry().ReadStoreL();
       
   497         CleanupStack::PushL( store );
       
   498         MMsvAttachmentManager& manager = store->AttachmentManagerL();
       
   499         
       
   500         TInt count = manager.AttachmentCount();
       
   501         for( TInt i = count; --i >= 0; )
       
   502             {
       
   503             CMsvAttachment* atta = manager.GetAttachmentInfoL( i );
       
   504             TMsvAttachmentId attaId = atta->Id();
       
   505             delete atta;
       
   506             
       
   507             RFile attaFile;
       
   508             TRAPD( err, attaFile = manager.GetAttachmentFileL( attaId ) );
       
   509             // if error, hardly no need to close
       
   510             attaFile.Close();
       
   511             if ( err )
       
   512                 {
       
   513                 corrupts->AppendL( attaId );
       
   514                 }
       
   515             }
       
   516 
       
   517         CleanupStack::PopAndDestroy( store ); 
       
   518     	}
       
   519     return corrupts;
       
   520     }
       
   521 
       
   522 // ---------------------------------------------------------
       
   523 // CreateObjectFromAttachmentLC
       
   524 // 
       
   525 // ---------------------------------------------------------
       
   526 CUniObject* CUniDataModel::CreateObjectFromAttachmentLC( 
       
   527                             const TPtrC8&               aMimeType,
       
   528                             RFile&                      aFile,
       
   529                             MMsvAttachmentManager&      aManager,
       
   530                             CMsvAttachment&             aAttachment )
       
   531     {
       
   532     // Linked attachments are handles similarly as ordinary ones.
       
   533     CUniObject* object = NULL;
       
   534     CMsgMediaInfo* info = NULL;
       
   535     if ( aMimeType.CompareF( KMsgMimeTextPlain ) == 0 )
       
   536         {
       
   537         TDataType mimeType( aMimeType );
       
   538         info = iMediaResolver->CreateMediaInfoL( aFile, mimeType );
       
   539         }
       
   540     else
       
   541         {
       
   542         info = iMediaResolver->CreateMediaInfoL( aFile );
       
   543         }
       
   544  
       
   545     if ( info->MediaType() == EMsgMediaText ||
       
   546          info->MediaType() == EMsgMediaXhtml )
       
   547         {
       
   548         object = CUniTextObject::NewLC(
       
   549             iFs,
       
   550             iMtm,
       
   551             *iDataUtils,
       
   552             static_cast< CMsgTextInfo* >( info ),
       
   553             aManager,
       
   554             aAttachment );
       
   555         }
       
   556     else
       
   557         {
       
   558         object = CUniObject::NewLC(
       
   559             iFs,
       
   560             iMtm,
       
   561             *iDataUtils,
       
   562             info,
       
   563             aManager,
       
   564             aAttachment );
       
   565         }
       
   566     // Media info is to be parsed asynchronously!
       
   567     
       
   568     return object;        
       
   569     }
       
   570 
       
   571 // ---------------------------------------------------------
       
   572 // GetDomObjectRefCounts
       
   573 // 
       
   574 // ---------------------------------------------------------
       
   575 void CUniDataModel::FindDomObjectRefCountsL()
       
   576     {
       
   577     CMDXMLNode* node = iDom->DocumentElement()->FirstChild();
       
   578     CUniObject* obj = NULL;
       
   579 
       
   580     while ( node )
       
   581         {
       
   582         if ( UniSmilUtils::IsMediaElem( node ) ) // Filtering of structural nodes.
       
   583             {
       
   584             obj = iObjectList->GetByNodeL( node );
       
   585             if ( obj )
       
   586                 {
       
   587                 obj->SetReferenceCount( obj->ReferenceCount() + 1 );
       
   588                 }                       
       
   589 
       
   590             }
       
   591         node = UniSmilUtils::NextNode( node );
       
   592         }
       
   593 
       
   594     }
       
   595 
       
   596 //  End of File