mobilemessaging/unieditor/application/src/UniEditorProcessTextOperation.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2006,2007 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:   Provides CUniEditorProcessImageOperation methods.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // ========== INCLUDE FILES ================================
       
    21 
       
    22 #include <apmstd.h>
       
    23 
       
    24 #include <icl/imagedata.h>
       
    25 
       
    26 #include <txtrich.h>
       
    27 
       
    28 #include <centralrepository.h>
       
    29 
       
    30 #include <messagingvariant.hrh>
       
    31 #include <messaginginternalcrkeys.h> // for Central Repository keys
       
    32 
       
    33 #include <MmsConformance.h>
       
    34 #include <MsgMimeTypes.h>
       
    35 #include <MsgTextInfo.h>
       
    36 #include <unimsventry.h>
       
    37 
       
    38 #include <mmsvattachmentmanager.h>
       
    39 #include <mmsvattachmentmanagersync.h>
       
    40 #include <cmsvattachment.h>
       
    41 
       
    42 #include <msgtextutils.h>
       
    43 
       
    44 #include <mmssettingsdefs.h>
       
    45 
       
    46 #include "UniClientMtm.h"
       
    47 #include "UniEditorEnum.h"
       
    48 #include "UniEditorDocument.h"
       
    49 #include "unimimeinfo.h"
       
    50 #include "UniEditorProcessTextOperation.h"
       
    51 
       
    52 // ========== CONSTANTS ====================================
       
    53 
       
    54 _LIT( KUniExtPlainText,  ".txt" );
       
    55 
       
    56 // ========== MEMBER FUNCTIONS =============================
       
    57 
       
    58 // ---------------------------------------------------------
       
    59 // CUniEditorProcessTextOperation::NewL
       
    60 //
       
    61 // Factory method.
       
    62 // ---------------------------------------------------------
       
    63 //
       
    64 CUniEditorProcessTextOperation* CUniEditorProcessTextOperation::NewL( 
       
    65         MUniEditorOperationObserver& aObserver,
       
    66         CUniEditorDocument& aDocument,
       
    67         RFs& aFs )
       
    68     {
       
    69     CUniEditorProcessTextOperation* self = new ( ELeave ) CUniEditorProcessTextOperation(
       
    70             aObserver,
       
    71             aDocument,
       
    72             aFs );
       
    73             
       
    74     CleanupStack::PushL( self );
       
    75     self->ConstructL();
       
    76     CleanupStack::Pop( self );
       
    77     
       
    78     return self;
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------
       
    82 // CUniEditorProcessTextOperation::CUniEditorProcessImageOperation.
       
    83 // ---------------------------------------------------------
       
    84 //
       
    85 CUniEditorProcessTextOperation::CUniEditorProcessTextOperation(
       
    86         MUniEditorOperationObserver& aObserver,
       
    87         CUniEditorDocument& aDocument,
       
    88         RFs& aFs ) :
       
    89     CUniEditorOperation( aObserver, aDocument, aFs, EUniEditorOperationProcessText )
       
    90     {
       
    91     }
       
    92 
       
    93 // ---------------------------------------------------------
       
    94 // CUniEditorProcessTextOperation::ConstructL
       
    95 //
       
    96 // 2nd phase constructor.
       
    97 // ---------------------------------------------------------
       
    98 //
       
    99 void CUniEditorProcessTextOperation::ConstructL()
       
   100     {
       
   101     BaseConstructL();
       
   102     }
       
   103 
       
   104 // ---------------------------------------------------------
       
   105 // CUniEditorProcessTextOperation::~CUniEditorProcessTextOperation
       
   106 // ---------------------------------------------------------
       
   107 //
       
   108 CUniEditorProcessTextOperation::~CUniEditorProcessTextOperation()
       
   109     {
       
   110     Cancel();
       
   111     
       
   112     delete iEditStore;
       
   113     delete iParser;
       
   114     }
       
   115 
       
   116 // ---------------------------------------------------------
       
   117 // CUniEditorProcessTextOperation::Start
       
   118 // ---------------------------------------------------------
       
   119 //
       
   120 void CUniEditorProcessTextOperation::Start()
       
   121     {
       
   122     ResetErrors();
       
   123     
       
   124     iOperationState = EUniProcessTxtCheck;
       
   125     
       
   126     CompleteSelf( KErrNone );
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------
       
   130 // CUniEditorProcessTextOperation::RunL
       
   131 // ---------------------------------------------------------
       
   132 //
       
   133 void CUniEditorProcessTextOperation::RunL()
       
   134     {
       
   135     PrintOperationAndState();
       
   136     if ( iStatus.Int() != KErrNone )
       
   137         {
       
   138         SetError( iStatus.Int() );
       
   139         iOperationState = EUniProcessTxtError;
       
   140         }
       
   141     
       
   142     switch ( iOperationState )
       
   143         {
       
   144         case EUniProcessTxtCheck:
       
   145             {
       
   146             DoStartCheck();
       
   147             break;
       
   148             }
       
   149         case EUniProcessTxtProcess:
       
   150             {
       
   151             DoStartProcessL();
       
   152             break;
       
   153             }
       
   154         case EUniProcessTxtReady:
       
   155             {
       
   156             NotifyObserver( EUniEditorOperationComplete );
       
   157             break;
       
   158             }
       
   159         case EUniProcessTxtError:
       
   160             {
       
   161             NotifyObserver( EUniEditorOperationError );
       
   162             break;
       
   163             }
       
   164         default:
       
   165             {
       
   166             SetError( KErrUnknown );
       
   167             NotifyObserver( EUniEditorOperationError );
       
   168             break;
       
   169             }
       
   170         }    
       
   171     }
       
   172 
       
   173 // ---------------------------------------------------------
       
   174 // CUniEditorProcessTextOperation::DoCancelCleanup
       
   175 // ---------------------------------------------------------
       
   176 //
       
   177 void CUniEditorProcessTextOperation::DoCancelCleanup()
       
   178     {
       
   179     if ( iParser )
       
   180         {
       
   181         iParser->Cancel();
       
   182         }
       
   183         
       
   184     Reset();
       
   185     }
       
   186 
       
   187 // ---------------------------------------------------------
       
   188 // CUniEditorProcessTextOperation::DoStartCheck
       
   189 // ---------------------------------------------------------
       
   190 //
       
   191 void CUniEditorProcessTextOperation::DoStartCheck()
       
   192     {
       
   193     TInt slideCount = iDocument.DataModel()->SmilModel().SlideCount();
       
   194     
       
   195     iProcessedObject = NULL;
       
   196     
       
   197     for ( TInt currentSlide = 0; 
       
   198           currentSlide < slideCount && iProcessedObject == NULL;
       
   199           currentSlide++ )
       
   200         {
       
   201         TInt objectCount = iDocument.DataModel()->SmilModel().SlideObjectCount( currentSlide );
       
   202         for ( TInt currentObject = 0; 
       
   203               currentObject < objectCount && iProcessedObject == NULL; 
       
   204               currentObject++ )
       
   205             {
       
   206             CUniObject* object = iDocument.DataModel()->SmilModel().GetObjectByIndex( currentSlide, currentObject );
       
   207             
       
   208             if ( object &&
       
   209                  object->MediaType() == EMsgMediaXhtml )
       
   210                 {
       
   211                 iProcessedObject = object;
       
   212                 iSlideNum = currentSlide;
       
   213                 }
       
   214             }
       
   215         }  
       
   216     
       
   217     if ( iProcessedObject )
       
   218         {
       
   219         iOperationState = EUniProcessTxtProcess;
       
   220         CompleteSelf( KErrNone );
       
   221         }
       
   222     else
       
   223         {
       
   224         iOperationState = EUniProcessTxtReady;
       
   225         CompleteSelf( KErrNone );
       
   226         }
       
   227     }
       
   228   
       
   229 // ---------------------------------------------------------
       
   230 // CUniEditorProcessTextOperation::DoStartProcessL
       
   231 // ---------------------------------------------------------
       
   232 //
       
   233 void CUniEditorProcessTextOperation::DoStartProcessL()
       
   234     {
       
   235     if ( !iEditStore )
       
   236         {
       
   237         iEditStore = iDocument.Mtm().Entry().EditStoreL();
       
   238         }
       
   239     
       
   240     if ( !iParser )
       
   241         {
       
   242         iParser = CXhtmlParser::NewL( this );
       
   243         iParser->SetMode( ETrue, ETrue );
       
   244         }
       
   245     
       
   246     RFile sourceFile = 
       
   247         iEditStore->AttachmentManagerL().GetAttachmentFileL( iProcessedObject->AttachmentId() );
       
   248     
       
   249     iParser->CreateDomL( sourceFile ); // Takes ownership
       
   250     SetPending();
       
   251     }
       
   252 
       
   253 // ---------------------------------------------------------
       
   254 // CUniEditorProcessTextOperation::Reset
       
   255 // ---------------------------------------------------------
       
   256 //
       
   257 void CUniEditorProcessTextOperation::Reset()
       
   258     {
       
   259     delete iEditStore;
       
   260     iEditStore = NULL;
       
   261     }
       
   262 
       
   263 // ---------------------------------------------------------
       
   264 // CUniEditorProcessTextOperation::ParseCompleteL
       
   265 // ---------------------------------------------------------
       
   266 //
       
   267 void CUniEditorProcessTextOperation::ParseCompleteL()
       
   268     {
       
   269     CRichText* text = CRichText::NewL( iDocument.EikonEnv()->SystemParaFormatLayerL(), 
       
   270                                        iDocument.EikonEnv()->SystemCharFormatLayerL() );
       
   271     CleanupStack::PushL( text );
       
   272     
       
   273     iParser->ParseL( *text );
       
   274     
       
   275     RFile fileHandle;
       
   276     CreateEmptyTextAttachmentL( fileHandle );
       
   277     
       
   278     CleanupClosePushL( fileHandle );
       
   279     
       
   280     WriteTextToFileL( *text, fileHandle );
       
   281     
       
   282     ReplaceXhtmlAttachmentL( fileHandle );
       
   283     
       
   284     CleanupStack::PopAndDestroy( 2, text );
       
   285     
       
   286     iOperationState = EUniProcessTxtCheck;
       
   287     CompleteOperation( KErrNone );
       
   288     }
       
   289 
       
   290 // ---------------------------------------------------------
       
   291 // CUniEditorProcessTextOperation::ParseError
       
   292 // ---------------------------------------------------------
       
   293 //
       
   294 void CUniEditorProcessTextOperation::ParseError( TInt aError )
       
   295     {
       
   296     TRAP_IGNORE( HandleParserErrorL( aError ) );
       
   297     
       
   298     iOperationState = EUniProcessTxtError;
       
   299     CompleteOperation( aError );
       
   300     }
       
   301 
       
   302 // ---------------------------------------------------------
       
   303 // CUniEditorProcessTextOperation::HandleParserErrorL
       
   304 // ---------------------------------------------------------
       
   305 //
       
   306 void CUniEditorProcessTextOperation::HandleParserErrorL( TInt /*aError*/ )
       
   307     {
       
   308     if ( iNewAttachmentId != KMsvNullIndexEntryId )
       
   309         {
       
   310         if ( !iEditStore )
       
   311             {
       
   312             iEditStore = iDocument.Mtm().Entry().EditStoreL();
       
   313             }
       
   314             
       
   315         MMsvAttachmentManager& manager = iEditStore->AttachmentManagerL();
       
   316         MMsvAttachmentManagerSync& managerSync = iEditStore->AttachmentManagerExtensionsL();    
       
   317         managerSync.RemoveAttachmentL( CUniDataUtils::IndexPositionOfAttachmentL( manager, iNewAttachmentId ) );
       
   318         iEditStore->CommitL();
       
   319         
       
   320         iNewAttachmentId = KMsvNullIndexEntryId;
       
   321         }
       
   322     }
       
   323 
       
   324 // ---------------------------------------------------------
       
   325 // CUniEditorProcessTextOperation::CreateEmptyTextAttachmentL
       
   326 // ---------------------------------------------------------
       
   327 //
       
   328 void CUniEditorProcessTextOperation::CreateEmptyTextAttachmentL( RFile& aFileHandle )
       
   329     {
       
   330     // Get the file name from original full path name.
       
   331     TParsePtrC parser( iProcessedObject->MimeInfo()->ContentLocation() );
       
   332 
       
   333     MMsvAttachmentManagerSync& managerSync = iEditStore->AttachmentManagerExtensionsL();
       
   334     
       
   335     CMsvAttachment* attachment = CMsvAttachment::NewL( CMsvAttachment::EMsvFile );
       
   336     CleanupStack::PushL( attachment );
       
   337     
       
   338     attachment->SetMimeTypeL( KMsgMimeTextPlain );
       
   339     
       
   340     TFileName* newFileName = new( ELeave ) TFileName;
       
   341     CleanupStack::PushL( newFileName );
       
   342     
       
   343     newFileName->Append( parser.Name() );
       
   344     newFileName->Append( KUniExtPlainText() );    
       
   345     
       
   346     managerSync.CreateAttachmentL( *newFileName, aFileHandle, attachment );
       
   347     
       
   348     CleanupStack::PopAndDestroy( newFileName );
       
   349     CleanupStack::Pop( attachment ); // ownership transferred
       
   350     
       
   351     // Store the ID as it needs to be removed if something goes wrong after
       
   352     // this point and before the attachment is completely initialized.
       
   353     iNewAttachmentId = attachment->Id();
       
   354     
       
   355     iEditStore->CommitL();
       
   356     }
       
   357 
       
   358 // ---------------------------------------------------------
       
   359 // CUniEditorProcessTextOperation::ReplaceXhtmlAttachmentL
       
   360 // ---------------------------------------------------------
       
   361 //
       
   362 void CUniEditorProcessTextOperation::ReplaceXhtmlAttachmentL( RFile& aFileHandle )
       
   363     {   
       
   364     // edit store must be closed as RemoveObject tries to open new store
       
   365     // that will fail if other handles to store are open.
       
   366     delete iEditStore;
       
   367     iEditStore = NULL;
       
   368     
       
   369     TDataType mime( KMsgMimeTextPlain );
       
   370     CMsgTextInfo* info = CMsgTextInfo::NewL( aFileHandle,
       
   371                                              mime,
       
   372                                              iFs );
       
   373     CleanupStack::PushL( info );
       
   374     
       
   375     info->SetCharacterSet( KCharacterSetIdentifierUtf8 );
       
   376     
       
   377     iDocument.DataModel()->SmilModel().RemoveObjectL( iSlideNum, iProcessedObject );
       
   378     iDocument.DataModel()->SmilModel().AddStoredObjectL( iSlideNum, iNewAttachmentId, info );
       
   379     
       
   380     // Attachment ownership is transferred to newly created UniObject
       
   381     iNewAttachmentId = KMsvNullIndexEntryId;
       
   382     
       
   383     // Info ownership is transferred to newly created UniObject
       
   384     CleanupStack::Pop( info ); 
       
   385     
       
   386     iDocument.SetBodyModified( ETrue );
       
   387     }
       
   388         
       
   389 // ---------------------------------------------------------
       
   390 // CUniEditorProcessTextOperation::RunError
       
   391 // ---------------------------------------------------------
       
   392 //
       
   393 TInt CUniEditorProcessTextOperation::RunError( TInt aError )
       
   394     {    
       
   395     return CUniEditorOperation::RunError( aError );
       
   396     }
       
   397 
       
   398 // ---------------------------------------------------------
       
   399 // CUniEditorProcessTextOperation::WriteTextToFileL
       
   400 // ---------------------------------------------------------
       
   401 //
       
   402 void CUniEditorProcessTextOperation::WriteTextToFileL( CRichText& aText, RFile& aFileHandle )
       
   403     {
       
   404     RFile writerHandle;
       
   405     writerHandle.Duplicate( aFileHandle );
       
   406     
       
   407     RFileWriteStream writer( writerHandle );
       
   408     CleanupClosePushL( writer );
       
   409     
       
   410     HBufC* buffer = NULL;
       
   411     
       
   412 	TInt textLength = aText.DocumentLength();
       
   413 	if ( textLength > 0 )
       
   414 		{
       
   415 		buffer = HBufC::NewL( textLength );
       
   416 		TPtr bufferPtr = buffer->Des();
       
   417 		aText.Extract( bufferPtr );
       
   418 		}
       
   419     else 
       
   420         {
       
   421         return;
       
   422         }
       
   423         
       
   424     CleanupStack::PushL( buffer );
       
   425 
       
   426     // Convert the text to utf8
       
   427     CCnvCharacterSetConverter* converter = CCnvCharacterSetConverter::NewLC();
       
   428     
       
   429     if ( converter->PrepareToConvertToOrFromL( KCharacterSetIdentifierUtf8, iFs ) != 
       
   430                                                     CCnvCharacterSetConverter::EAvailable )
       
   431         {
       
   432         User::Leave( KErrNotSupported );
       
   433         }
       
   434 
       
   435     TBuf8<128> outputBuffer;
       
   436     TPtrC16 remainderOfUnicodeText( *buffer );
       
   437     FOREVER
       
   438         {
       
   439         TInt returnValue = converter->ConvertFromUnicode( outputBuffer, remainderOfUnicodeText );
       
   440         if ( returnValue == CCnvCharacterSetConverter::EErrorIllFormedInput )
       
   441             {
       
   442             User::Leave( KErrCorrupt );
       
   443             }
       
   444         else if ( returnValue < 0 ) // future-proof against "TError" expanding
       
   445             {
       
   446             User::Leave( KErrGeneral );
       
   447             }
       
   448 
       
   449         // ? - do something here with outputBuffer
       
   450         writer.WriteL( outputBuffer );
       
   451 
       
   452         if ( returnValue == 0 )
       
   453             {
       
   454             break; // all of aUnicodeText has been converted and handled
       
   455             }
       
   456         remainderOfUnicodeText.Set( remainderOfUnicodeText.Right( returnValue ) );
       
   457         }
       
   458 
       
   459     CleanupStack::PopAndDestroy( 3, &writer ); //converter, buffer, writer
       
   460     }
       
   461 
       
   462 // ---------------------------------------------------------
       
   463 // CUniEditorProcessTextOperation::NotifyObserver
       
   464 // ---------------------------------------------------------
       
   465 //
       
   466 void CUniEditorProcessTextOperation::NotifyObserver( TUniEditorOperationEvent aEvent )
       
   467     {
       
   468     Reset();
       
   469     iObserver.EditorOperationEvent( EUniEditorOperationProcessText,
       
   470                                     aEvent );
       
   471     }
       
   472 
       
   473 // End of file