messagingappbase/msgeditor/viewsrc/MsgXhtmlBodyControl.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 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:  MsgXhtmlBodyControl implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // ========== INCLUDE FILES ================================
       
    21 
       
    22 #include <AknUtils.h>
       
    23 #include <AknsUtils.h>
       
    24 #include <applayout.cdl.h>
       
    25 #include <aknlayoutscalable_apps.cdl.h>
       
    26 #include <ItemFinder.h>
       
    27 
       
    28 #include "MsgEditorCommon.h"
       
    29 #include "MsgXhtmlBodyControl.h"
       
    30 #include "MsgBodyControlEditor.h"
       
    31 #include "MsgEditorPanic.h"
       
    32 
       
    33 // ========== EXTERNAL DATA STRUCTURES =====================
       
    34 
       
    35 // ========== EXTERNAL FUNCTION PROTOTYPES =================
       
    36 
       
    37 // ========== CONSTANTS ====================================
       
    38 
       
    39 #ifdef RD_MSG_XHTML_SUPPORT 
       
    40 _LIT( KWwwUrlAddress, "www." );
       
    41 _LIT( KSchemeAddress, ":" );
       
    42 #endif
       
    43 
       
    44 // ========== MACROS =======================================
       
    45 
       
    46 // ========== LOCAL CONSTANTS AND MACROS ===================
       
    47 #ifdef RD_MSG_XHTML_SUPPORT 
       
    48 const TInt KObserverArrayGranularity = 2;
       
    49 #endif
       
    50 
       
    51 // ========== MODULE DATA STRUCTURES =======================
       
    52 
       
    53 // ========== LOCAL FUNCTION PROTOTYPES ====================
       
    54 
       
    55 // ========== LOCAL FUNCTIONS ==============================
       
    56 
       
    57 // ========== MEMBER FUNCTIONS =============================
       
    58 
       
    59 // ---------------------------------------------------------
       
    60 // CMsgXhtmlBodyControl::CMsgXhtmlBodyControl
       
    61 //
       
    62 // Constructor.
       
    63 // ---------------------------------------------------------
       
    64 //
       
    65 CMsgXhtmlBodyControl::CMsgXhtmlBodyControl( MMsgBaseControlObserver& aBaseControlObserver ) : 
       
    66     CMsgBodyControl( aBaseControlObserver )
       
    67     {
       
    68     }
       
    69 
       
    70 // ---------------------------------------------------------
       
    71 // CMsgXhtmlBodyControl::~CMsgXhtmlBodyControl
       
    72 //
       
    73 // Destructor.
       
    74 // ---------------------------------------------------------
       
    75 //
       
    76 CMsgXhtmlBodyControl::~CMsgXhtmlBodyControl()
       
    77     {
       
    78     delete iObservers;
       
    79 #ifdef RD_MSG_XHTML_SUPPORT 
       
    80     delete iParser;
       
    81 #endif
       
    82     }
       
    83 
       
    84 // ---------------------------------------------------------
       
    85 // CMsgXhtmlBodyControl::NewL
       
    86 //
       
    87 // Factory method that creates this control.
       
    88 // ---------------------------------------------------------
       
    89 //
       
    90 EXPORT_C CMsgXhtmlBodyControl* CMsgXhtmlBodyControl::NewL( CMsgEditorView& aParent,
       
    91                                                            MMsgAsyncControlObserver* aObserver )
       
    92     {
       
    93     CMsgXhtmlBodyControl* self = new ( ELeave ) CMsgXhtmlBodyControl( aParent );
       
    94     self->SetMopParent( &aParent );
       
    95     
       
    96     CleanupStack::PushL( self );
       
    97     self->ConstructL( aObserver );
       
    98     CleanupStack::Pop( self );
       
    99 
       
   100     return self;
       
   101     }
       
   102 
       
   103 // ---------------------------------------------------------
       
   104 // CMsgXhtmlBodyControl::ConstructL
       
   105 // ---------------------------------------------------------
       
   106 //
       
   107 #ifdef RD_MSG_XHTML_SUPPORT 
       
   108 void CMsgXhtmlBodyControl::ConstructL( MMsgAsyncControlObserver* aObserver )
       
   109     {
       
   110     CMsgBodyControl::ConstructL();
       
   111     
       
   112     SetControlType( EMsgXhtmlBodyControl );
       
   113     SetPlainTextMode( EFalse );
       
   114     
       
   115     iObservers = new( ELeave ) CArrayPtrFlat<MMsgAsyncControlObserver>( KObserverArrayGranularity );
       
   116     
       
   117     iParser = CXhtmlParser::NewL( this );
       
   118 
       
   119     ResolveLayout();
       
   120     
       
   121     if ( aObserver )
       
   122         {
       
   123         iObservers->AppendL( aObserver );
       
   124         }
       
   125     }
       
   126 #else
       
   127 void CMsgXhtmlBodyControl::ConstructL( MMsgAsyncControlObserver* /*aObserver*/ )
       
   128     {
       
   129     User::Leave( KErrNotSupported );
       
   130     }
       
   131 #endif
       
   132 
       
   133 
       
   134 // ---------------------------------------------------------
       
   135 // CMsgXhtmlBodyControl::LoadL
       
   136 // ---------------------------------------------------------
       
   137 //
       
   138 #ifdef RD_MSG_XHTML_SUPPORT 
       
   139 void CMsgXhtmlBodyControl::LoadL( RFile& aFileHandle )
       
   140     {
       
   141     RFile fileHandle;
       
   142     fileHandle.Duplicate( aFileHandle );
       
   143     
       
   144     iParser->CreateDomL( fileHandle ); // Takes ownership
       
   145 
       
   146     SetState( EMsgAsyncControlStateOpening );
       
   147     }
       
   148 #else
       
   149 void CMsgXhtmlBodyControl::LoadL( RFile& /*aFileHandle*/ )
       
   150     {
       
   151     }
       
   152 #endif
       
   153 
       
   154 // ---------------------------------------------------------
       
   155 // CMsgXhtmlBodyControl::Cancel
       
   156 // ---------------------------------------------------------
       
   157 //
       
   158 #ifdef RD_MSG_XHTML_SUPPORT 
       
   159 void CMsgXhtmlBodyControl::Cancel()
       
   160     {
       
   161     if ( iState == EMsgAsyncControlStateOpening )
       
   162         {
       
   163         iParser->Cancel();
       
   164         SetState( EMsgAsyncControlStateIdle );
       
   165         }
       
   166     }
       
   167 #else
       
   168 void CMsgXhtmlBodyControl::Cancel()
       
   169     {
       
   170     }
       
   171 #endif
       
   172 
       
   173 // ---------------------------------------------------------
       
   174 // CMsgXhtmlBodyControl::Close
       
   175 // ---------------------------------------------------------
       
   176 //
       
   177 void CMsgXhtmlBodyControl::Close()
       
   178     {
       
   179     }
       
   180 
       
   181 // ---------------------------------------------------------
       
   182 // CMsgXhtmlBodyControl::State
       
   183 // ---------------------------------------------------------
       
   184 //
       
   185 TMsgAsyncControlState CMsgXhtmlBodyControl::State() const
       
   186     {
       
   187     return iState;
       
   188     }
       
   189 
       
   190 // ---------------------------------------------------------
       
   191 // CMsgXhtmlBodyControl::Error
       
   192 // ---------------------------------------------------------
       
   193 //
       
   194 TInt CMsgXhtmlBodyControl::Error() const
       
   195     {
       
   196     return iError;
       
   197     }
       
   198         
       
   199 // ---------------------------------------------------------
       
   200 // CMsgXhtmlBodyControl::AddObserverL
       
   201 // ---------------------------------------------------------
       
   202 //
       
   203 void CMsgXhtmlBodyControl::AddObserverL( MMsgAsyncControlObserver& aObserver )
       
   204     {
       
   205     iObservers->AppendL( &aObserver );
       
   206     }
       
   207         
       
   208 // ---------------------------------------------------------
       
   209 // CMsgXhtmlBodyControl::RemoveObserver
       
   210 // ---------------------------------------------------------
       
   211 //
       
   212 void CMsgXhtmlBodyControl::RemoveObserver( MMsgAsyncControlObserver& aObserver )
       
   213     {
       
   214     for ( TInt current = 0; current < iObservers->Count(); current++ )
       
   215         {
       
   216         if ( iObservers->At( current ) == &aObserver )
       
   217             {
       
   218             iObservers->Delete( current );
       
   219             break;
       
   220             }
       
   221         }
       
   222     }
       
   223 
       
   224 // ---------------------------------------------------------
       
   225 // CMsgXhtmlBodyControl::ParseCompleteL
       
   226 // ---------------------------------------------------------
       
   227 //
       
   228 void CMsgXhtmlBodyControl::ParseCompleteL()
       
   229     {
       
   230     ParseXhtmlL();
       
   231     
       
   232     if ( iState ==  EMsgAsyncControlStateOpening )
       
   233         {
       
   234         SetState( EMsgAsyncControlStateReady );
       
   235         }
       
   236     }
       
   237 
       
   238 // ---------------------------------------------------------
       
   239 // CMsgXhtmlBodyControl::ParseError
       
   240 // ---------------------------------------------------------
       
   241 //
       
   242 void CMsgXhtmlBodyControl::ParseError( TInt aError )
       
   243     {
       
   244     iError = aError;
       
   245     SetState( EMsgAsyncControlStateError );
       
   246     }
       
   247 
       
   248 // ---------------------------------------------------------
       
   249 // CMsgXhtmlBodyControl::HandleResourceChange
       
   250 // ---------------------------------------------------------
       
   251 //
       
   252 void CMsgXhtmlBodyControl::HandleResourceChange( TInt aType )
       
   253     {
       
   254     CMsgBodyControl::HandleResourceChange( aType );
       
   255         
       
   256 	if ( aType == KEikDynamicLayoutVariantSwitch )
       
   257         {
       
   258         ResolveLayout();
       
   259         
       
   260         if ( iState == EMsgAsyncControlStateReady )
       
   261             {
       
   262             TRAPD( error, ParseXhtmlL() );
       
   263             if ( error != KErrNone )
       
   264                 {
       
   265                 ParseError( error );
       
   266                 }
       
   267             }
       
   268         }
       
   269     
       
   270     for ( TInt current = 0; current < iObservers->Count(); current++ )
       
   271         {
       
   272         iObservers->At( current )->MsgAsyncControlResourceChanged( *this, aType );
       
   273         }
       
   274     }
       
   275 
       
   276 // ---------------------------------------------------------
       
   277 // CMsgXhtmlBodyControl::SetState
       
   278 // ---------------------------------------------------------
       
   279 //
       
   280 void CMsgXhtmlBodyControl::SetState( TMsgAsyncControlState  aState )
       
   281     {
       
   282     TMsgAsyncControlState oldState = iState;
       
   283     iState = aState;
       
   284     
       
   285     for ( TInt current = 0; current < iObservers->Count(); current++ )
       
   286         {
       
   287         iObservers->At( current )->MsgAsyncControlStateChanged( *this, iState, oldState );
       
   288         }
       
   289     }
       
   290 
       
   291 // ---------------------------------------------------------
       
   292 // CMsgXhtmlBodyControl::ResolveLayout
       
   293 // ---------------------------------------------------------
       
   294 //
       
   295 #ifdef RD_MSG_XHTML_SUPPORT 
       
   296 void CMsgXhtmlBodyControl::ResolveLayout()
       
   297     {
       
   298     TAknLayoutRect msgTextPane;
       
   299     msgTextPane.LayoutRect( MsgEditorCommons::MsgDataPane(),
       
   300                             AknLayoutScalable_Apps::msg_text_pane( 0 ) );
       
   301                             
       
   302     TAknLayoutRect msgBodyPane;
       
   303     msgBodyPane.LayoutRect( msgTextPane.Rect(),
       
   304                             AknLayoutScalable_Apps::msg_body_pane() );
       
   305     
       
   306     TAknLayoutText normalTextLayout;
       
   307     normalTextLayout.LayoutText( msgBodyPane.Rect(),
       
   308                                  AknLayoutScalable_Apps::msg_body_pane_t1( 0 ) );
       
   309 
       
   310     TAknLayoutText largeTextLayout;
       
   311     largeTextLayout.LayoutText( msgBodyPane.Rect(),
       
   312                                 AknLayoutScalable_Apps::msg_body_pane_t1( 2 ) );
       
   313 
       
   314     TAknLayoutText smallTextLayout;
       
   315     smallTextLayout.LayoutText( msgBodyPane.Rect(),
       
   316                                 AknLayoutScalable_Apps::msg_body_pane_t1( 3 ) );
       
   317 
       
   318     
       
   319     TRgb textColor = normalTextLayout.Color();
       
   320     if ( AknsUtils::AvkonSkinEnabled() )
       
   321         {
       
   322         AknsUtils::GetCachedColor( AknsUtils::SkinInstance(),
       
   323                                    textColor,
       
   324                                    KAknsIIDQsnTextColors,
       
   325                                    EAknsCIQsnTextColorsCG6 );
       
   326         
       
   327         }
       
   328         
       
   329     iParser->SetDefaultTextColor( textColor );
       
   330     iParser->SetFonts( largeTextLayout.Font(),
       
   331                        normalTextLayout.Font(),
       
   332                        smallTextLayout.Font(),
       
   333                        normalTextLayout.Font() );   
       
   334     }
       
   335 #else
       
   336 void CMsgXhtmlBodyControl::ResolveLayout()
       
   337     {
       
   338     }
       
   339 #endif                      
       
   340 
       
   341 // ---------------------------------------------------------
       
   342 // CMsgXhtmlBodyControl::ParseXhtmlL
       
   343 // ---------------------------------------------------------
       
   344 //
       
   345 #ifdef RD_MSG_XHTML_SUPPORT 
       
   346 void CMsgXhtmlBodyControl::ParseXhtmlL()
       
   347     {
       
   348     CRichText& text = TextContent();
       
   349     text.Reset();
       
   350     
       
   351     iParser->SetMode( IsPlainTextMode(), IsPlainTextMode() );
       
   352     
       
   353     iParser->ParseL( text );
       
   354         
       
   355     CItemFinder* autoFinder = ItemFinder();
       
   356     
       
   357     TInt linkCount = iParser->HyperLinkCount();
       
   358     
       
   359     if ( autoFinder && linkCount > 0 )
       
   360         {
       
   361         CArrayPtrFlat<CItemFinder::CFindItemExt>* linkArray = 
       
   362                 new( ELeave ) CArrayPtrFlat<CItemFinder::CFindItemExt>( linkCount );
       
   363         CleanupStack::PushL( TCleanupItem( CleanupLinkArray, linkArray ) );
       
   364         
       
   365         TInt startPos( 0 );
       
   366         TInt endPos( 0 );
       
   367         TPtrC address;
       
   368         
       
   369         for( TInt current = 0; current < linkCount; current++ )
       
   370             {
       
   371             User::LeaveIfError( iParser->HyperLink( current, startPos, endPos, address ) );
       
   372             
       
   373             CItemFinder::CFindItemExt* findItem = new( ELeave ) CItemFinder::CFindItemExt();
       
   374             CleanupStack::PushL( findItem );
       
   375             
       
   376             findItem->iItemType = CItemFinder::ENoneSelected;
       
   377             
       
   378             if ( address.Length() >= KWwwUrlAddress().Length() &&
       
   379                  address.Left( KWwwUrlAddress().Length() ).MatchF( KWwwUrlAddress ) != KErrNotFound )
       
   380                 {
       
   381                 findItem->iItemType = CItemFinder::EUrlAddress;    
       
   382                 }
       
   383             else if ( address.Length() >= KSchemeAddress().Length() &&
       
   384                       address.FindF( KSchemeAddress() ) != KErrNotFound )
       
   385                 {
       
   386                 findItem->iItemType = CItemFinder::EUriScheme;
       
   387                 }
       
   388             
       
   389             if ( findItem->iItemType != CItemFinder::ENoneSelected )
       
   390                 {
       
   391                 findItem->iItemDescriptor = address.AllocL();
       
   392                 findItem->iStart = startPos;
       
   393                 findItem->iEnd = endPos;
       
   394         
       
   395                 linkArray->AppendL( findItem );
       
   396                 CleanupStack::Pop( findItem );
       
   397                 }
       
   398             else
       
   399                 {
       
   400                 CleanupStack::PopAndDestroy( findItem );
       
   401                 }
       
   402             };
       
   403         
       
   404         User::LeaveIfError( autoFinder->SetExternalLinks( linkArray ) );
       
   405         CleanupStack::PopAndDestroy( linkArray );
       
   406         }
       
   407     
       
   408     Editor().SetAmountToFormatL( ETrue );
       
   409     }
       
   410 #else
       
   411 void CMsgXhtmlBodyControl::ParseXhtmlL()
       
   412     {
       
   413     }
       
   414 #endif
       
   415 
       
   416 // ----------------------------------------------------
       
   417 // CMsgXhtmlBodyControl::CleanupLinkArray
       
   418 // ----------------------------------------------------
       
   419 //
       
   420 #ifdef RD_MSG_XHTML_SUPPORT 
       
   421 void CMsgXhtmlBodyControl::CleanupLinkArray( TAny* aArray )
       
   422     {
       
   423     CArrayPtrFlat<CItemFinder::CFindItemExt>* linkArray = static_cast<CArrayPtrFlat<CItemFinder::CFindItemExt>*>( aArray );
       
   424     
       
   425     linkArray->ResetAndDestroy();
       
   426     delete linkArray;
       
   427     }
       
   428 #else
       
   429 void CMsgXhtmlBodyControl::CleanupLinkArray( TAny* aArray )
       
   430     {
       
   431     }
       
   432 #endif
       
   433 
       
   434 //  End of File