notepad/notepad1/LibSrc/NpdViewerDialog.cpp
branchRCL_3
changeset 30 bd7edf625bdd
equal deleted inserted replaced
29:12af337248b1 30:bd7edf625bdd
       
     1 /*
       
     2 * Copyright (c) 2002 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:  Implementation of NotepadViewer mode.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <aknappui.h>
       
    21 #include <avkon.hrh>
       
    22 #include <eikmenup.h>
       
    23 #include <txtetext.h>
       
    24 #include <AknForm.h>
       
    25 #include <aknnotedialog.h>
       
    26 #include <eikapp.h>
       
    27 #include <aknnotewrappers.h>
       
    28 #include <sendui.h>
       
    29 #include <SenduiMtmUids.h>
       
    30 #include <Sendnorm.rsg>
       
    31 #include <barsread.h>
       
    32 #include <featmgr.h>
       
    33 #include <hlplch.h>
       
    34 #include <finditemmenu.h>
       
    35 #include <ItemFinder.h> 
       
    36 #include <CommonContentPolicy.h>
       
    37 #include <StringLoader.h>
       
    38 #include <centralrepository.h>
       
    39 #include <CommonUiInternalCRKeys.h>
       
    40 #include <akntitle.h>
       
    41 #include <touchfeedback.h>
       
    42 
       
    43 #include <NpdLib.rsg>
       
    44 #include "NpdLib.hrh"
       
    45 #include <finditem.hrh>
       
    46 #include <CMessageData.h>
       
    47 #include "NpdViewerDialog.h"
       
    48 #include "NpdApi.h"
       
    49 #include "NpdLib.h"
       
    50 #include "NpdUtil.h"
       
    51 #include "NpdRichTextEditor.h"
       
    52 #include "NpdLibPanic.h"
       
    53 #include <csxhelp/nmake.hlp.hrh>
       
    54 
       
    55 // CONSTANTS
       
    56 
       
    57 // when the file length is larger than this value, that need to load by steps 
       
    58 static const TInt KFileLengthNeedLoadBySteps = 1024 * 100;
       
    59 // the time value for interval to start the timer for close the notepad dialog 
       
    60 static const TUint KCloseNotepadDialogInterval = 500000; //microseconds
       
    61 // the time value for delay to start the timer for close the notepad dialog 
       
    62 static const TUint KCloseNotepadDialogDelay = 1; // microseconds
       
    63 
       
    64 // ============================ MEMBER FUNCTIONS ===============================
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CNotepadViewerDialog::NewL
       
    68 // constructor.
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 EXPORT_C CNotepadViewerDialog* CNotepadViewerDialog::NewL(
       
    72     const TInt aResId, 
       
    73     CEikDialog** aSelfPtr )
       
    74     {
       
    75     CNotepadViewerDialog* self = new(ELeave) CNotepadViewerDialog;
       
    76     self->iSelfPtr = aSelfPtr;
       
    77     CleanupStack::PushL(self);
       
    78 
       
    79     TResourceReader rr;
       
    80     self->iCoeEnv->CreateResourceReaderLC(rr, aResId); // Push rr
       
    81     self->ConstructL( rr );
       
    82     CleanupStack::PopAndDestroy(); // rr
       
    83     CleanupStack::Pop(); // self
       
    84     return self;
       
    85     }
       
    86 
       
    87 void CNotepadViewerDialog::ConstructL(TResourceReader &rr)
       
    88     {
       
    89     CNotepadDialogBase::ConstructL(rr);
       
    90     iAutoFinder = CItemFinder::NewL();
       
    91     iFindMenu = CFindItemMenu::NewL( EFindItemMenuPlaceHolder );
       
    92     iFindMenu->AttachItemFinderMenuL(0);
       
    93     iFindMenu->SetCallSubMenuVisibility( EFalse ); // Click-To-Call
       
    94     iForwardLocked = EFalse;
       
    95     iReturnValue = KErrNone;
       
    96     iFileExist = EFalse;
       
    97     iAutomaticHlValue = ETrue;
       
    98     iAutomaticHlInitialized = EFalse;
       
    99     iTaskSwapperFlag = EFalse;
       
   100     ReadAutoHlCenRepValueAndSetNotifyL();
       
   101     SetAutomaticHighlightL(iAutomaticHlValue);
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CNotepadViewerDialog::~CNotepadViewerDialog
       
   106 // Destructor
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 EXPORT_C CNotepadViewerDialog::~CNotepadViewerDialog()
       
   110     {
       
   111     if( iPeriodicTimer )
       
   112         {
       
   113         iPeriodicTimer->Cancel();
       
   114         delete iPeriodicTimer;
       
   115         iPeriodicTimer = NULL;
       
   116         }
       
   117     delete iFilename;
       
   118     delete iSendUi;
       
   119     TRAP_IGNORE( iAutoFinder->SetItemFinderObserverL( 0 ) );
       
   120     delete iAutoFinder;
       
   121     delete iFindMenu;
       
   122     if( iNotifier )
       
   123         {
       
   124         iNotifier->StopListening();
       
   125         delete iNotifier;        
       
   126         }
       
   127     delete iCenRepSession;
       
   128     iCenRepSession = NULL;
       
   129     delete iLoadFileAO;
       
   130     iLoadFileAO = NULL;
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CNotepadViewerDialog::SetFileL
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 EXPORT_C void CNotepadViewerDialog::SetFileL(
       
   138     const TDesC& aFilename, 
       
   139     const TDesC* aTitle,
       
   140     TBool aGuessEncoding,
       
   141     TUint aEncoding )
       
   142     {
       
   143     HBufC* filename = aFilename.AllocL();
       
   144     delete iFilename;
       
   145     iFilename = filename;
       
   146     if ( aTitle )
       
   147         {
       
   148         CNotepadDialogBase::SetTitleL(aTitle);
       
   149         }
       
   150     else
       
   151         {
       
   152         TPtr ptr = filename->Des();
       
   153         TParsePtr parse(ptr);
       
   154         TPtrC name = parse.NameAndExt();
       
   155         CNotepadDialogBase::SetTitleL(&name); // SetTitleL copies the name.
       
   156         }
       
   157 
       
   158     // Check whether the file is forward locked.
       
   159     CCommonContentPolicy *ccp = CCommonContentPolicy::NewLC();
       
   160     if(ccp->IsClosedFileL(aFilename))
       
   161         {
       
   162         iForwardLocked = ETrue;
       
   163         }
       
   164     CleanupStack::PopAndDestroy(ccp);
       
   165 
       
   166     iGuessEncoding = aGuessEncoding;
       
   167     iEncoding = aEncoding;
       
   168     iDescriptorViewer = EFalse;
       
   169     iEditable = EFalse;
       
   170     }
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CNotepadViewerDialog::SetFileL file handle
       
   174 // -----------------------------------------------------------------------------
       
   175 //
       
   176 EXPORT_C void CNotepadViewerDialog::SetFileL(
       
   177     RFile& aFile, 
       
   178     const TDesC* aTitle,
       
   179     TBool aGuessEncoding,
       
   180     TUint aEncoding )
       
   181     {
       
   182     iFile = &aFile;
       
   183     CNotepadDialogBase::SetFileHandleL(aFile);
       
   184     if ( aTitle )
       
   185         {
       
   186         CNotepadDialogBase::SetTitleL(aTitle);
       
   187         }
       
   188     else
       
   189         {
       
   190         TBuf<256> name;
       
   191         aFile.Name(name);
       
   192         CNotepadDialogBase::SetTitleL(&name); // SetTitleL copies the name.
       
   193         }
       
   194 
       
   195     // Check whether the file is forward locked.
       
   196     CCommonContentPolicy *ccp = CCommonContentPolicy::NewLC();
       
   197     if(ccp->IsClosedFileL(aFile))
       
   198         {
       
   199         iForwardLocked = ETrue;
       
   200         }
       
   201     CleanupStack::PopAndDestroy(ccp);
       
   202 
       
   203     iGuessEncoding = aGuessEncoding;
       
   204     iEncoding = aEncoding;
       
   205     iDescriptorViewer = EFalse;
       
   206     iEditable = EFalse;
       
   207     iFileExist = ETrue;
       
   208     }
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 // CNotepadViewerDialog::InitDescriptorViewerL
       
   212 // -----------------------------------------------------------------------------
       
   213 //
       
   214 void CNotepadViewerDialog::InitDescriptorViewerL
       
   215     (HBufC** aContentPointer,
       
   216     TInt& aReturnStatus,
       
   217     TBool aReadOnly,
       
   218     const TDesC& aText,
       
   219     const TDesC& aTitle, 
       
   220     const TDesC& aDelConfirmation 
       
   221     )
       
   222     {
       
   223     if ( aTitle.Length() != 0 )
       
   224         {
       
   225         CNotepadDialogBase::SetTitleL(&aTitle);
       
   226         }
       
   227     iContent.Set(aText);
       
   228     iContentPointer = aContentPointer;
       
   229     iDescriptorViewer = ETrue;
       
   230     iEditable = !aReadOnly;
       
   231     iTitle.Set(aTitle);
       
   232     iDelConfirmation.Set(aDelConfirmation);
       
   233     iReturnValue = &aReturnStatus;
       
   234     }
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // CNotepadViewerDialog::LoadFileL
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 EXPORT_C void CNotepadViewerDialog::LoadFileL()
       
   241     {
       
   242     __ASSERT_DEBUG( iEditor, Panic(ENotepadLibraryPanicNoEdwin) );
       
   243     __ASSERT_DEBUG( iFilename, Panic(ENotepadLibraryPanicNoFileSpecified) );
       
   244     iEditor->SetTextL(NULL); 
       
   245     NotepadUtil::LoadFileL( *iCoeEnv, *iFilename, iGuessEncoding,
       
   246         iEncoding, *(iEditor->Text()) );
       
   247     iEditor->SetAmountToFormat(iEditor->Text()->DocumentLength());
       
   248     iEditor->HandleTextChangedL();
       
   249     iAutoFinder->SetEditor((CEikRichTextEditor**)&iEditor);
       
   250     iAutoFinder->SetItemFinderObserverL( this );
       
   251     RefreshTitleL();
       
   252     }
       
   253 
       
   254 // -----------------------------------------------------------------------------
       
   255 // CNotepadViewerDialog::LoadFileL file handle
       
   256 // -----------------------------------------------------------------------------
       
   257 //
       
   258 EXPORT_C void CNotepadViewerDialog::LoadFileL(RFile& aFile)
       
   259     {
       
   260     __ASSERT_DEBUG( iEditor, Panic(ENotepadLibraryPanicNoEdwin) );
       
   261     //__ASSERT_DEBUG( iFilename, Panic(ENotepadLibraryPanicNoFileSpecified) );
       
   262    
       
   263     TInt fileSize(0);
       
   264     User::LeaveIfError( aFile.Size( fileSize ) );  
       
   265     if ( fileSize >= KFileLengthNeedLoadBySteps )
       
   266        {
       
   267        LoadFileByStepsL( aFile );
       
   268        return;
       
   269        }
       
   270     
       
   271     iEditor->SetTextL(NULL);
       
   272     TBool error = NotepadUtil::LoadFileL( *iCoeEnv, aFile, iGuessEncoding,
       
   273         iEncoding, *(iEditor->Text()) );
       
   274     iEditor->SetAmountToFormat(iEditor->Text()->DocumentLength()); 
       
   275     iEditor->HandleTextChangedL();
       
   276     iAutoFinder->SetEditor((CEikRichTextEditor**)&iEditor);
       
   277     iAutoFinder->SetItemFinderObserverL( this );
       
   278     RefreshTitleL();
       
   279     if( error != KErrNone)
       
   280         {     
       
   281         HBufC* text = StringLoader::LoadLC(R_NOTEPAD_QTN_FILE_CORRUPTED);
       
   282         CAknErrorNote* mErrorNote;
       
   283         mErrorNote = new (ELeave) CAknErrorNote( ETrue );
       
   284         mErrorNote->ExecuteLD(*text);
       
   285         CleanupStack::PopAndDestroy( text );
       
   286          
       
   287         iPeriodicTimer = CPeriodic::NewL( CActive::EPriorityStandard );    
       
   288         if( !iPeriodicTimer->IsActive() )
       
   289             {
       
   290             iPeriodicTimer->Start( KCloseNotepadDialogDelay, KCloseNotepadDialogInterval, 
       
   291                            TCallBack( CNotepadViewerDialog::TimerCallbackL, this ) );
       
   292             }
       
   293         }
       
   294     }
       
   295 
       
   296 
       
   297 // -----------------------------------------------------------------------------
       
   298 // CNotepadViewerDialog::OpenFileL
       
   299 // -----------------------------------------------------------------------------
       
   300 //
       
   301 EXPORT_C void CNotepadViewerDialog::OpenFileL(
       
   302     const TDesC& aFilename, 
       
   303     const TDesC* aTitle,
       
   304     TBool aGuessEncoding,
       
   305     TUint aEncoding )
       
   306     {
       
   307     SetFileL(aFilename, aTitle, aGuessEncoding, aEncoding);
       
   308     LoadFileL();
       
   309     }
       
   310 
       
   311 // -----------------------------------------------------------------------------
       
   312 // CNotepadViewerDialog::OpenFileL file handle
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 EXPORT_C void CNotepadViewerDialog::OpenFileL(
       
   316     RFile& aFile, 
       
   317     const TDesC* aTitle,
       
   318     TBool aGuessEncoding,
       
   319     TUint aEncoding )
       
   320     {
       
   321     SetFileL(aFile, aTitle, aGuessEncoding, aEncoding);
       
   322     LoadFileL(aFile);
       
   323     }
       
   324 
       
   325 // ----------------------------------------------------
       
   326 // CNotepadViewerDialog::SetAutomaticHighlightL
       
   327 // 
       
   328 // ----------------------------------------------------
       
   329 void CNotepadViewerDialog::SetAutomaticHighlightL( const TBool aSwitchON )
       
   330     {
       
   331     if ( iAutoFinder )
       
   332         {
       
   333         // content highlight
       
   334         if ( aSwitchON )
       
   335             { // switch ON
       
   336             iAutoFinder->SetFindModeL( 
       
   337                 CItemFinder::EPhoneNumber |
       
   338                 CItemFinder::EUrlAddress |
       
   339                 CItemFinder::EEmailAddress );
       
   340             }
       
   341         else
       
   342             { // switch OFF
       
   343             iAutoFinder->SetFindModeL( 
       
   344                 CItemFinder::ENoneSelected );
       
   345             }
       
   346         }
       
   347     }
       
   348 
       
   349 // ----------------------------------------------------
       
   350 // CNotepadViewerDialog::ReadAutoHlCenRepValueAndSetNotifyL
       
   351 // 
       
   352 // ----------------------------------------------------
       
   353 void CNotepadViewerDialog::ReadAutoHlCenRepValueAndSetNotifyL()
       
   354     {
       
   355     if ( iAutomaticHlInitialized )
       
   356         return;
       
   357     // Create the session
       
   358     iCenRepSession = CRepository::NewL( KCRUidCommonUi );
       
   359     if( iCenRepSession )
       
   360         {
       
   361         // Get the value of AutomaticHighlight key
       
   362         iCenRepSession->Get( KCuiAutomaticHighlight, iAutomaticHlValue );
       
   363         // Create the notifer
       
   364         iNotifier = 
       
   365             CCenRepNotifyHandler::NewL( 
       
   366                 *this, *iCenRepSession, CCenRepNotifyHandler::EIntKey,
       
   367                 KCuiAutomaticHighlight );
       
   368         // Start listening
       
   369         iNotifier->StartListeningL();       
       
   370         }
       
   371     iAutomaticHlInitialized = ETrue; // Done once per viewer
       
   372     }
       
   373 
       
   374 // -----------------------------------------------------------------------------
       
   375 // CNotepadViewerDialog::OkToExitL
       
   376 // -----------------------------------------------------------------------------
       
   377 //
       
   378 TBool CNotepadViewerDialog::OkToExitL( TInt aButtonId )
       
   379     {
       
   380     iFlags &= ~ENotepadMenuByOkKey;
       
   381     TBool isOk(ETrue);
       
   382     switch ( aButtonId )
       
   383         {
       
   384 		//MSK        
       
   385         case EAknSoftkeyContextOptions:
       
   386 	   		{
       
   387 	   		iFlags |= ENotepadMenuByOkKey;
       
   388 	   		iFlags &= ~EMenuByItemActication;
       
   389 	   		DisplayMenuL();
       
   390 	   		isOk= EFalse;
       
   391 	   		}
       
   392 	   		break;
       
   393         case EAknSoftkeyBack:
       
   394             if ( !IsWaiting() ) // not API, but viewer application
       
   395                 {
       
   396                 iAvkonAppUi->HandleCommandL(EEikCmdExit);
       
   397                 isOk = EFalse ;
       
   398                 }
       
   399             break;
       
   400         case EAknSoftkeyOptions:
       
   401              iTaskSwapperFlag = ETrue;
       
   402         default:
       
   403             iFlags &= ~EMenuByItemActication;
       
   404             isOk = CAknDialog::OkToExitL(aButtonId);
       
   405             break;
       
   406         }
       
   407     return isOk;
       
   408     }
       
   409 
       
   410 // ----------------------------------------------------------------------------
       
   411 // CNotepadViewerDialog::HandleNotifyInt
       
   412 // ----------------------------------------------------------------------------    
       
   413 //
       
   414 void CNotepadViewerDialog::HandleNotifyInt( 
       
   415     TUint32  /*aId*/, 
       
   416     TInt aNewValue )
       
   417     {   
       
   418     // Notifies changes on KCuiAutomaticHighlight
       
   419     TRAPD(error,SetAutomaticHighlightL( aNewValue ));
       
   420 	if ( error != KErrNone )
       
   421 		{
       
   422 		_LIT(KNpdViewerNotifierPanicType,"CNotepadViewerDialog::SetAutomaticHighlightL");
       
   423 		User::Panic( KNpdViewerNotifierPanicType, error );
       
   424 		}
       
   425     }
       
   426 
       
   427 // ----------------------------------------------------------------------------
       
   428 // CMsgSmsViewerAppUi::HandleNotifyError
       
   429 // ----------------------------------------------------------------------------    
       
   430 //
       
   431 void CNotepadViewerDialog::HandleNotifyError( 
       
   432     TUint32 /*aId*/, 
       
   433     TInt /* aError */, 
       
   434     CCenRepNotifyHandler* /* aHandler */ )
       
   435     {
       
   436     // Remove session and notifier
       
   437     if( iNotifier )
       
   438         {
       
   439         iNotifier->StopListening();       
       
   440         delete iNotifier;
       
   441         iNotifier = NULL;
       
   442         }
       
   443     delete iCenRepSession;    
       
   444     iCenRepSession = NULL;
       
   445     }
       
   446 
       
   447 // ---------------------------------------------------------
       
   448 // CNotepadViewerDialog::HandleResourceChange
       
   449 // from CCoeControl
       
   450 // ---------------------------------------------------------
       
   451 //
       
   452 void CNotepadViewerDialog::HandleResourceChange(TInt aType)
       
   453     {
       
   454     
       
   455     if(aType == KAknsMessageSkinChange)
       
   456     	{
       
   457     	if(iEditor && IsActivated())
       
   458     		{
       
   459     		iEditor->HandleResourceChange(aType);
       
   460     		}
       
   461     	}
       
   462     if (aType == KEikDynamicLayoutVariantSwitch)
       
   463         {
       
   464         CAknDialog::HandleResourceChange(aType);
       
   465 		CEikDialog::Layout();
       
   466 		if (iEditor && IsActivated())
       
   467 			{
       
   468 			iEditor->HandleResourceChange(aType);
       
   469 			}
       
   470 		iEditor->DrawDeferred();
       
   471 		}
       
   472     }
       
   473 
       
   474 // ---------------------------------------------------------
       
   475 // CNotepadViewerDialog::SizeChanged
       
   476 // from CCoeControl
       
   477 // ---------------------------------------------------------
       
   478 //
       
   479 void CNotepadViewerDialog::SizeChanged()
       
   480     {
       
   481     CNotepadDialogBase::SizeChanged();
       
   482     if (iEditor && IsActivated())
       
   483         {
       
   484         TRAP_IGNORE(iEditor->DoEditorLayoutL());
       
   485         }
       
   486     }
       
   487 
       
   488 // -----------------------------------------------------------------------------
       
   489 // CNotepadViewerDialog::ShowConfirmationNoteL
       
   490 // -----------------------------------------------------------------------------
       
   491 //
       
   492 void CNotepadViewerDialog::ShowConfirmationNoteL()
       
   493     {
       
   494     HBufC* text = StringLoader::LoadLC(R_NOTEPAD_MEMO_ATTA_SAVED); //<NOTEPAD_SAVE_NOTE_MAXLENGTH> text;
       
   495     CAknConfirmationNote* note = new(ELeave) CAknConfirmationNote();
       
   496     note->SetMopParent(this);
       
   497     note->ExecuteLD(*text);
       
   498     CleanupStack::PopAndDestroy(text);
       
   499     }
       
   500 
       
   501 // -----------------------------------------------------------------------------
       
   502 // CNotepadViewerDialog::PostLayoutDynInitL
       
   503 // -----------------------------------------------------------------------------
       
   504 //
       
   505 void CNotepadViewerDialog::PostLayoutDynInitL()
       
   506     {
       
   507     iEditor = STATIC_CAST(CNotepadRichTextEditor*, Control(ENotepadIdRichTextEditor));
       
   508     iEditor->ConstructBaseL(this);
       
   509     __ASSERT_DEBUG( iEditor, Panic(ENotepadLibraryPanicNoEdwin) );
       
   510     iEditor->EnableCcpuSupportL(EFalse);
       
   511     iEditor->CreatePreAllocatedScrollBarFrameL()->SetScrollBarVisibilityL(
       
   512         CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
       
   513      
       
   514      CEikAppUi* container = iAvkonAppUi->ContainerAppUi();
       
   515     //cannot find UIDs in SenduiMtmUids.h
       
   516     TUid appuid = KNullUid;
       
   517     if ( container )
       
   518         {
       
   519         appuid = container->Application()->AppDllUid();
       
   520         }
       
   521     if ( appuid != KSenduiMMSEditorUid && appuid != KSenduiMailEditorUid )
       
   522         {
       
   523         iSendUi = CSendUi::NewL();
       
   524         }
       
   525 
       
   526     TRect rect(iEikonEnv->EikAppUi()->ClientRect());
       
   527     iEditor->SetRect(rect);
       
   528 	
       
   529     DrawNow();
       
   530     }
       
   531 
       
   532 // -----------------------------------------------------------------------------
       
   533 // CNotepadViewerDialog::ActivateL
       
   534 // -----------------------------------------------------------------------------
       
   535 //
       
   536 void CNotepadViewerDialog::ActivateL()
       
   537     {
       
   538     __ASSERT_DEBUG( iEditor, Panic(ENotepadLibraryPanicNoEdwin) );
       
   539     CNotepadDialogBase::ActivateL();
       
   540     CAknTitlePane*    TitlePane = 
       
   541         STATIC_CAST(CAknTitlePane*, 
       
   542             (((CAknAppUi*)iEikonEnv->EikAppUi())->StatusPane())
       
   543                 ->ControlL(TUid::Uid(EEikStatusPaneUidTitle)));
       
   544  	TitlePane->SetTextL (_L(""));  
       
   545     iEditor->InitNotepadEditorL(this);
       
   546     if (iFileExist) // when SetFileL before execute
       
   547         {
       
   548         LoadFileL(*iFile);
       
   549         }
       
   550     else if (iContent.Length() != 0)
       
   551         {
       
   552         iEditor->SetTextL(NULL);
       
   553         iEditor->SetTextL(&iContent);
       
   554         iEditor->SetAmountToFormat(iContent.Length()); // Will ensure first Format in SetTextL is in correct mode
       
   555         iEditor->HandleTextChangedL();
       
   556         iAutoFinder->SetEditor((CEikRichTextEditor**)&iEditor);
       
   557         iAutoFinder->SetItemFinderObserverL( this );
       
   558         RefreshTitleL();
       
   559         iEditor->EnableKineticScrollingL( ETrue );
       
   560         }
       
   561     }
       
   562 
       
   563 // -----------------------------------------------------------------------------
       
   564 // CNotepadViewerDialog::OfferKeyEventL
       
   565 // from CoeControl
       
   566 // -----------------------------------------------------------------------------
       
   567 //
       
   568 TKeyResponse CNotepadViewerDialog::OfferKeyEventL(
       
   569     const TKeyEvent& aKeyEvent, 
       
   570     TEventCode aType )
       
   571     {
       
   572     if ( MenuShowing() || aType != EEventKey )
       
   573         {
       
   574         return CAknDialog::OfferKeyEventL(aKeyEvent, aType);
       
   575         }
       
   576     TKeyResponse keyResponse(EKeyWasConsumed);
       
   577     switch (aKeyEvent.iCode)
       
   578         {
       
   579         case EKeyOK: // into next case
       
   580         case EKeyEnter:
       
   581             if ( aKeyEvent.iRepeats == 0 &&
       
   582                 !(aKeyEvent.iModifiers & (EAllStdModifiers|EModifierSpecial)) )
       
   583                 {
       
   584                 iFlags |= ENotepadMenuByOkKey;
       
   585                 DisplayMenuL();
       
   586                 }
       
   587             break;
       
   588         case EKeyRightArrow: // into next case
       
   589         case EKeyLeftArrow:
       
   590             break;
       
   591         case EKeyUpArrow:
       
   592    	        iAutoFinder->NextItemOrScrollL( CItemFinder::ENextUp );
       
   593             break;
       
   594         case EKeyDownArrow:
       
   595    	        iAutoFinder->NextItemOrScrollL( CItemFinder::ENextDown );
       
   596             break;
       
   597         case EKeyPhoneSend:
       
   598              HandleNumberCallL();
       
   599             break;
       
   600         default:
       
   601             keyResponse = CAknDialog::OfferKeyEventL(aKeyEvent, aType);
       
   602         }
       
   603  
       
   604     return keyResponse;
       
   605     }
       
   606 void CNotepadViewerDialog::HandleFindItemEventL(
       
   607                 const CItemFinder::CFindItemExt& aItem,
       
   608                 MAknItemFinderObserver::TEventFlag aEvent,
       
   609                 TUint aFlags)
       
   610     {
       
   611     if ( MAknItemFinderObserver::EPointerEvent == aEvent )
       
   612         {
       
   613         iFlags |= ENotepadMenuByOkKey;
       
   614         }
       
   615     iFlags |= EMenuByItemActication;
       
   616     }
       
   617 // -----------------------------------------------------------------------------
       
   618 // CNotepadViewerDialog::HandleDialogPointerEventL
       
   619 // from CoeControl
       
   620 // -----------------------------------------------------------------------------
       
   621 //
       
   622 void CNotepadViewerDialog::HandleDialogPointerEventL( const TPointerEvent& aPointerEvent )
       
   623     {
       
   624     if(!AknLayoutUtils::PenEnabled())
       
   625         {
       
   626         return;
       
   627         }
       
   628     if( CItemFinder::ENoneSelected == iAutoFinder->CurrentItemExt().iItemType )
       
   629 	{
       
   630 	// when you selected is not item, return
       
   631 	return;
       
   632 	}
       
   633     MTouchFeedback* feedback = MTouchFeedback::Instance();
       
   634     TTouchLogicalFeedback fbLogicalType;
       
   635     TTouchFeedbackType fbType;
       
   636     switch( aPointerEvent.iType )
       
   637     	{
       
   638     	case TPointerEvent::EButton1Down:
       
   639     		{
       
   640     		fbLogicalType = ETouchFeedbackBasic;
       
   641     		fbType = TTouchFeedbackType( ETouchFeedbackAudio|ETouchFeedbackVibra );
       
   642     		}
       
   643     		break;
       
   644     	case TPointerEvent::EButton1Up:
       
   645     		{
       
   646     		fbLogicalType = ETouchFeedbackIncreasingPopUp;
       
   647     		fbType = ETouchFeedbackVibra;
       
   648     		}
       
   649     		break;
       
   650     	default:
       
   651     		return;
       
   652     	}
       
   653     if ( feedback )
       
   654     	{
       
   655     	feedback->InstantFeedback( this,
       
   656     			fbLogicalType, fbType, TPointerEvent() );
       
   657     	if( ETouchFeedbackVibra == fbType )
       
   658     		{
       
   659     		DisplayMenuL();
       
   660     		}
       
   661     	}
       
   662     }
       
   663 
       
   664 // -----------------------------------------------------------------------------
       
   665 // CNotepadViewerDialog::HandleNumberCallL
       
   666 // -----------------------------------------------------------------------------
       
   667 //
       
   668 void CNotepadViewerDialog::HandleNumberCallL()
       
   669     {
       
   670      const CItemFinder::CFindItemExt& findItem = iAutoFinder->CurrentItemExt(); 
       
   671      
       
   672      if (findItem.iItemType == CItemFinder::EEmailAddress)
       
   673     	 {
       
   674     	 iFindMenu->HandleVoIPCallL(findItem.iItemDescriptor->Des());
       
   675     	 }
       
   676      else if(findItem.iItemType == CItemFinder::EPhoneNumber )
       
   677         {
       
   678         iFindMenu->HandleCallL( findItem.iItemDescriptor->Des() );
       
   679         }         
       
   680     }
       
   681 
       
   682 // -----------------------------------------------------------------------------
       
   683 // CNotepadViewerDialog::GetHelpContext
       
   684 // -----------------------------------------------------------------------------
       
   685 //
       
   686 void CNotepadViewerDialog::GetHelpContext(TCoeHelpContext& aContext) const
       
   687     {
       
   688     aContext.iMajor = KUidNotepad;
       
   689     if(iEditable)
       
   690     	{
       
   691     	aContext.iContext = KNMAKE_HLP_MEMO_VIEWER_EDIT;
       
   692     	}
       
   693     else
       
   694       	{
       
   695     	aContext.iContext = KNMAKE_HLP_MEMO_VIEWER;
       
   696     	}
       
   697     
       
   698     }
       
   699 
       
   700 // -----------------------------------------------------------------------------
       
   701 // CNotepadViewerDialog::ProcessCommandL
       
   702 // from MEikCommandObserver
       
   703 // -----------------------------------------------------------------------------
       
   704 //
       
   705 void CNotepadViewerDialog::ProcessCommandL(TInt aCommandId)
       
   706     {
       
   707     HideMenu();
       
   708     switch (aCommandId)
       
   709         {
       
   710         case ENotepadCmdViewerSave:
       
   711             {
       
   712             TInt length(iEditor->TextLength());
       
   713             if ( length > 0 )
       
   714                 {
       
   715                 CNotepadApi::AddContentL(
       
   716                     iEditor->Text()->Read(0, length) );
       
   717                 ShowConfirmationNoteL();
       
   718                 }
       
   719             }
       
   720             break;
       
   721         case ENotepadCmdViewerEdit:
       
   722             {
       
   723             (*iContentPointer) = CNotepadApi::ExecTextEditorL(*iReturnValue, iContent , iTitle, iDelConfirmation);
       
   724             TryExitL(0);
       
   725             }
       
   726             break;
       
   727          case ENotepadCmdViewerDelete:
       
   728             {
       
   729             TBool deleted;
       
   730             if(iDelConfirmation.Length() != 0)
       
   731                 {
       
   732                 deleted = CNotepadDialogBase::ExecuteConfirmationQueryL( &iDelConfirmation );
       
   733                 }
       
   734             else
       
   735                 {
       
   736                 deleted = CNotepadDialogBase::ExecuteConfirmationQueryL( R_NOTEPAD_QUERY_DELETE_MEMO );
       
   737                 }
       
   738             if(deleted)
       
   739                 {
       
   740                 *iReturnValue = CNotepadApi::ENpdDataDeleted;
       
   741                 TryExitL(0);
       
   742                 }
       
   743             }
       
   744             break;
       
   745         case ENotepadCmdSend:
       
   746        		SendAsL( *iSendUi, aCommandId, iFileExist, NULL, iEditor->Text() );
       
   747             break;
       
   748         default:
       
   749             {
       
   750             if ( iFindMenu->CommandIsValidL(aCommandId) ) // can't really leave
       
   751                 {
       
   752                 TBool selectionVisibility ( ETrue );
       
   753                 if ( aCommandId == EFindItemCmdCopy && iAutoFinder && iEditor && iEditor->TextView() )
       
   754                     {
       
   755                     selectionVisibility = iEditor->TextView()->SelectionVisible();
       
   756                     iEditor->TextView()->SetSelectionVisibilityL( EFalse );
       
   757                     const CItemFinder::CFindItemExt& item = iAutoFinder->CurrentItemExt();
       
   758                     iEditor->SetSelectionL( item.iStart, item.iEnd + 1 );
       
   759                     }
       
   760 
       
   761                 iFindMenu->HandleItemFinderCommandL(aCommandId);
       
   762 
       
   763                 if ( aCommandId == EFindItemCmdCopy && iAutoFinder && iEditor && iEditor->TextView() )
       
   764                     {
       
   765                     iEditor->TextView()->SetSelectionVisibilityL( selectionVisibility );
       
   766                     iEditor->ClearSelectionL();
       
   767                     iEditor->DrawDeferred(); // sometimes editor does not update itself
       
   768                     }
       
   769 
       
   770                 return;
       
   771                 }
       
   772             CNotepadDialogBase::ProcessCommandL(aCommandId);
       
   773             break;
       
   774             }
       
   775         }
       
   776     }
       
   777 
       
   778 // -----------------------------------------------------------------------------
       
   779 // CNotepadViewerDialog::DynInitMenuPaneL
       
   780 // from MEikMenuObserver
       
   781 // -----------------------------------------------------------------------------
       
   782 //
       
   783 void CNotepadViewerDialog::DynInitMenuPaneL(
       
   784     TInt aResourceId,
       
   785     CEikMenuPane* aMenuPane )
       
   786     {
       
   787     __ASSERT_DEBUG(aResourceId > 0, Panic(ENotepadLibraryPanicNoMenuResource));
       
   788     __ASSERT_DEBUG(aMenuPane, Panic(ENotepadLibraryPanicNullMenuPane));
       
   789     __ASSERT_DEBUG(iEditor, Panic(ENotepadLibraryPanicNoEdwin));
       
   790 
       
   791     const TBool activatedByItemActication = iFlags & EMenuByItemActication;
       
   792     if ( !activatedByItemActication && iEditor && iAutoFinder
       
   793             && iEditor->SelectionLength() !=
       
   794             ( iAutoFinder->CurrentItemExt().iEnd -
       
   795               iAutoFinder->CurrentItemExt().iStart + 1 ) )
       
   796         {
       
   797         iAutoFinder->ResetCurrentItem(); // do not show item specific commands
       
   798         }
       
   799 
       
   800     TInt index;
       
   801     switch ( aResourceId )
       
   802         {
       
   803         case R_SENDUI_MENU:
       
   804             index = 2;
       
   805 			iSendUi->AddSendMenuItemL(*aMenuPane, index, ENotepadCmdSend );
       
   806             break;
       
   807         case R_NOTEPAD_VIEWER_MENU:
       
   808 
       
   809             if(iEditable)
       
   810                 {
       
   811                 aMenuPane->DeleteMenuItem(ENotepadCmdViewerSave);
       
   812 
       
   813                 if ( iFlags & ENotepadMenuByOkKey )
       
   814                     {
       
   815     				aMenuPane->DeleteMenuItem(EAknCmdExit);
       
   816     				if(FeatureManager::FeatureSupported(KFeatureIdHelp))
       
   817 						{
       
   818     				aMenuPane->DeleteMenuItem(EAknCmdHelp);
       
   819 						}
       
   820                     }
       
   821                 }
       
   822             else 
       
   823                 {
       
   824                 aMenuPane->DeleteMenuItem(ENotepadCmdViewerEdit);
       
   825                 aMenuPane->DeleteMenuItem(ENotepadCmdViewerDelete);
       
   826 
       
   827                 if (iForwardLocked || iDescriptorViewer)
       
   828                     {
       
   829                     aMenuPane->SetItemDimmed(ENotepadCmdViewerSave, ETrue);
       
   830                     }
       
   831 
       
   832                 if ( iSendUi && // not called from MMS/Mail editor
       
   833                     !NotepadUtil::IsEmpty(iEditor->Text()->Read(0)) 
       
   834                     && !iForwardLocked)
       
   835                     {
       
   836                     InsertSendMenuItemAfterL( *iSendUi, *aMenuPane, 
       
   837                         EFindItemMenuPlaceHolder);
       
   838                     }
       
   839 
       
   840                 if ( iFlags & ENotepadMenuByOkKey )
       
   841                     {
       
   842                     aMenuPane->DeleteMenuItem(EAknCmdExit);
       
   843                     if(FeatureManager::FeatureSupported(KFeatureIdHelp))
       
   844 						{
       
   845                     aMenuPane->DeleteMenuItem(EAknCmdHelp);
       
   846 						}
       
   847                     if(!iForwardLocked)
       
   848                         {
       
   849                         CEikMenuPaneItem::SData item = aMenuPane->ItemData(ENotepadCmdViewerSave);
       
   850                         aMenuPane->DeleteMenuItem(ENotepadCmdViewerSave);
       
   851                 	    aMenuPane->AddMenuItemL(item, EFindItemMenuPlaceHolder);
       
   852                         }
       
   853                     }
       
   854                 }
       
   855             iFindMenu->AddItemFindMenuL(iAutoFinder,aMenuPane,EFindItemMenuPlaceHolder,KNullDesC);
       
   856             
       
   857    			if(!FeatureManager::FeatureSupported(KFeatureIdHelp))
       
   858 				{
       
   859 				aMenuPane->DeleteMenuItem(EAknCmdHelp);
       
   860 				}
       
   861 
       
   862             break;
       
   863         default:
       
   864        		iFindMenu->UpdateItemFinderMenuL(aResourceId,aMenuPane);
       
   865             break;
       
   866         }
       
   867     }
       
   868 // -----------------------------------------------------------------------------
       
   869 // CNotepadViewerDialog::DynInitMenuBarL
       
   870 // from MEikMenuObserver
       
   871 // -----------------------------------------------------------------------------
       
   872 //    
       
   873 void CNotepadViewerDialog::DynInitMenuBarL(TInt aResourceId,CEikMenuBar* aMenuBar)
       
   874     {
       
   875     __ASSERT_DEBUG(aResourceId > 0, Panic(ENotepadLibraryPanicNoMenuResource));
       
   876     __ASSERT_DEBUG(aMenuBar, Panic(ENotepadLibraryPanicNoMenuResource));
       
   877     
       
   878     if(!iTaskSwapperFlag)
       
   879         {
       
   880         aMenuBar->SetMenuType(CEikMenuBar::EMenuOptionsNoTaskSwapper);
       
   881         }
       
   882     else
       
   883         {
       
   884         aMenuBar->SetMenuType(CEikMenuBar::EMenuOptions);
       
   885         iTaskSwapperFlag=EFalse;
       
   886         }
       
   887     }
       
   888 // -----------------------------------------------------------------------------
       
   889 // CNotepadViewerDialog::ExitDialogOnTimerExpireL
       
   890 // -----------------------------------------------------------------------------
       
   891 //    
       
   892 void CNotepadViewerDialog::ExitDialogOnTimerExpireL()
       
   893     {
       
   894        if ( !IsWaiting() ) // not API, but viewer application
       
   895         {
       
   896          iAvkonAppUi->HandleCommandL(EEikCmdExit);
       
   897         }
       
   898        else
       
   899         {
       
   900          TryExitL(0);    
       
   901         }    
       
   902     }
       
   903     
       
   904 // -----------------------------------------------------------------------------
       
   905 // CNotepadViewerDialog::TimerCallbackL
       
   906 // -----------------------------------------------------------------------------
       
   907 //
       
   908 TInt CNotepadViewerDialog::TimerCallbackL(TAny* aPtr)
       
   909     {
       
   910        static_cast<CNotepadViewerDialog*>( aPtr )->ExitDialogOnTimerExpireL(); 
       
   911        return ETrue;
       
   912     }
       
   913 
       
   914 // -----------------------------------------------------------------------------
       
   915 // CNotepadViewerDialog::CNotepadViewerDialog_Reserved
       
   916 // -----------------------------------------------------------------------------
       
   917 //
       
   918 EXPORT_C void CNotepadViewerDialog::CNotepadViewerDialog_Reserved()
       
   919     {
       
   920     }
       
   921 
       
   922 // -----------------------------------------------------------------------------
       
   923 // CNotepadViewerDialog::LoadFileByStepsL 
       
   924 // -----------------------------------------------------------------------------
       
   925 //
       
   926 void CNotepadViewerDialog::LoadFileByStepsL(RFile& aFile)
       
   927     {
       
   928     __ASSERT_DEBUG( iEditor, Panic(ENotepadLibraryPanicNoEdwin) );
       
   929     //__ASSERT_DEBUG( iFilename, Panic(ENotepadLibraryPanicNoFileSpecified) );
       
   930     iEditor->SetTextL(NULL); 
       
   931     
       
   932     // lanuch the progress bar
       
   933     CleanProgressDialogL();  
       
   934     iProgressDialogLoadfile = new( ELeave ) CAknProgressDialog(
       
   935       reinterpret_cast< CEikDialog** >( &iProgressDialogLoadfile ),
       
   936       EFalse );
       
   937     iProgressDialogLoadfile->SetCallback( this );
       
   938     iProgressDialogLoadfile->ExecuteLD( R_NOTEPAD_LOADFILE_PROGRESS_DIALOG );
       
   939     
       
   940     // start a active object to load file
       
   941     if ( iLoadFileAO ) 
       
   942         {
       
   943         delete iLoadFileAO;
       
   944         iLoadFileAO = NULL;
       
   945         }
       
   946     iLoadFileAO = CNotepadLoadFileAO::NewL( this,  *iCoeEnv, aFile, iGuessEncoding,
       
   947             iEncoding, *(iEditor->Text()) );
       
   948     
       
   949     if ( KErrNone != iLoadFileAO->StartLoadFile() )
       
   950         {
       
   951         ExitDialogOnTimerExpireL();
       
   952         }
       
   953     }
       
   954 
       
   955 
       
   956 // -----------------------------------------------------------------------------
       
   957 // CNotepadViewerDialog::CleanProgressDialogL()
       
   958 // -----------------------------------------------------------------------------
       
   959 //
       
   960 void CNotepadViewerDialog::CleanProgressDialogL()
       
   961     {
       
   962     if ( iProgressDialogLoadfile )
       
   963         {
       
   964         iProgressDialogLoadfile->ProcessFinishedL();
       
   965         iProgressDialogLoadfile = NULL;
       
   966         }
       
   967     }
       
   968 
       
   969 // -----------------------------------------------------------------------------
       
   970 // CNotepadViewerDialog::DialogDismissedL
       
   971 // -----------------------------------------------------------------------------
       
   972 //
       
   973 void CNotepadViewerDialog::DialogDismissedL( TInt aButtonId )
       
   974     {
       
   975     if ( ( EAknSoftkeyCancel == aButtonId ) && iLoadFileAO )
       
   976         {
       
   977         iLoadFileAO->CancelLoadFile();
       
   978         }
       
   979     }
       
   980 
       
   981 // -----------------------------------------------------------------------------
       
   982 // CNotepadViewerDialog::NotifyCompletedL
       
   983 // -----------------------------------------------------------------------------
       
   984 //
       
   985 void CNotepadViewerDialog::NotifyCompletedL( TInt aErrCode )
       
   986     {  
       
   987  
       
   988     // load file is completed for one step
       
   989     if ( KErrStep == aErrCode )
       
   990         {
       
   991         iEditor->SetAmountToFormat(iEditor->Text()->DocumentLength());
       
   992         iEditor->HandleTextChangedL();
       
   993         }
       
   994     // load file is completed 
       
   995     else if ( KErrNone == aErrCode ) 
       
   996         {
       
   997         CleanProgressDialogL(); 
       
   998         iEditor->SetAmountToFormat(iEditor->Text()->DocumentLength());
       
   999         iEditor->HandleTextChangedL();
       
  1000         iAutoFinder->SetEditor((CEikRichTextEditor**)&iEditor);
       
  1001         iAutoFinder->SetItemFinderObserverL( this );
       
  1002         RefreshTitleL();
       
  1003         }
       
  1004     // load file cancel
       
  1005     else if ( KErrCancel == aErrCode )
       
  1006         {
       
  1007         ExitDialogOnTimerExpireL();
       
  1008         }
       
  1009     // that occur error during load file
       
  1010     else
       
  1011         {
       
  1012         CleanProgressDialogL(); 
       
  1013         HBufC* text = StringLoader::LoadLC(R_NOTEPAD_QTN_FILE_CORRUPTED);
       
  1014         CAknErrorNote* mErrorNote;
       
  1015         mErrorNote = new (ELeave) CAknErrorNote( ETrue );
       
  1016         mErrorNote->ExecuteLD(*text);
       
  1017         CleanupStack::PopAndDestroy( text );
       
  1018         iPeriodicTimer = CPeriodic::NewL( CActive::EPriorityStandard );        
       
  1019         if ( !iPeriodicTimer->IsActive() )
       
  1020             {
       
  1021             iPeriodicTimer->Start( KCloseNotepadDialogDelay, KCloseNotepadDialogInterval, 
       
  1022                                TCallBack( CNotepadViewerDialog::TimerCallbackL, this ) );
       
  1023             }
       
  1024         }
       
  1025   
       
  1026     }
       
  1027         
       
  1028 
       
  1029 // End of File