examples/SDKExamples/helloworldbasic/src/helloworldbasicappui.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : helloworldbasicappui.cpp
00004 *  Part of     : Helloworldbasic
00005 *  Interface   :
00006 *  Description :
00007 *  Version     :
00008 *
00009 *  Copyright (c) 2005 - 2006 Nokia Corporation and/or its subsidiary(-ies).
00010 *  All rights reserved.
00011 *  This component and the accompanying materials are made available
00012 *  under the terms of "Eclipse Public License v1.0"
00013 *  which accompanies this distribution, and is available
00014 *  at the URL "http://www.eclipse.org/legal/epl-v10.html".
00015 * ==============================================================================
00016 */
00017 
00018 // INCLUDE FILES
00019 #include <avkon.hrh>
00020 #include <aknnotewrappers.h>
00021 #include <stringloader.h>
00022 #include <HelloWorldBasic.rsg>
00023 #include <f32file.h>
00024 #include <s32file.h>
00025 
00026 #include "HelloWorldBasic.pan"
00027 #include "HelloWorldBasicAppUi.h"
00028 #include "HelloWorldBasicAppView.h"
00029 #include "HelloWorldBasic.hrh"
00030 #include "HelloWorldBasicQueryDialog.h"
00031 
00032 _LIT( KHelloFileName, "Hello.txt" );
00033 
00034 // ============================ MEMBER FUNCTIONS ===============================
00035 
00036 
00037 // -----------------------------------------------------------------------------
00038 // CHelloWorldBasicAppUi::ConstructL()
00039 // Symbian 2nd phase constructor can leave.
00040 // -----------------------------------------------------------------------------
00041 //
00042 void CHelloWorldBasicAppUi::ConstructL()
00043     {
00044     // Initialise app UI with standard value.
00045     BaseConstructL(CAknAppUi::EAknEnableSkin);
00046 
00047     // Here the Hello.txt file is created. Because HelloWorld application is
00048     // localized to various languages, the Hello.txt-file is created every time
00049     // with current localization language
00050     //
00051     RFs fsSession;
00052     User::LeaveIfError(fsSession.Connect());
00053     CleanupClosePushL( fsSession );
00054     TInt objectsInStack = 1;
00055 
00056     #if defined(__WINS__) || defined(__WINSCW__)
00057     // create private folder, when testing in emulator.
00058     // ignore the return value; if this fails, then file.Replace() will fail
00059     // and a warning note will be printed.
00060     //
00061     fsSession.CreatePrivatePath(EDriveC);
00062     #endif
00063 
00064     RFile file;
00065 
00066     // Create a file to write the text to
00067     TInt err = file.Replace(fsSession, KHelloFileName, EFileWrite );
00068     if (KErrNone == err)
00069         {
00070         CleanupClosePushL( file );
00071 
00072         RFileWriteStream outputFileStream( file );
00073         CleanupClosePushL( outputFileStream );
00074 
00075         // Load a string from the resource file and stream it to file
00076         HBufC* textResource = StringLoader::LoadLC( R_HEWB_FILE_TEXT );
00077         objectsInStack += 3; // file, outputFileStream, testResource
00078 
00079         outputFileStream << *textResource;
00080         }
00081     else
00082         {
00083         _LIT(KFileWriteFailed,"Writing file %S failed: error %d");
00084         CAknWarningNote* note = new ( ELeave ) CAknWarningNote(ETrue);
00085 
00086         TBuf<64> text;
00087         text.Format(KFileWriteFailed, &KHelloFileName, err);
00088         note->ExecuteLD( text );
00089         }
00090 
00091     CleanupStack::PopAndDestroy(objectsInStack, &fsSession);
00092 
00093     // Create view object
00094     iAppView = CHelloWorldBasicAppView::NewL( ClientRect() );
00095 
00096     }
00097 // -----------------------------------------------------------------------------
00098 // CHelloWorldBasicAppUi::CHelloWorldBasicAppUi()
00099 // C++ default constructor can NOT contain any code, that might leave.
00100 // -----------------------------------------------------------------------------
00101 //
00102 CHelloWorldBasicAppUi::CHelloWorldBasicAppUi()
00103     {
00104     // No implementation required
00105     }
00106 
00107 // -----------------------------------------------------------------------------
00108 // CHelloWorldBasicAppUi::~CHelloWorldBasicAppUi()
00109 // Destructor.
00110 // -----------------------------------------------------------------------------
00111 //
00112 CHelloWorldBasicAppUi::~CHelloWorldBasicAppUi()
00113     {
00114     if ( iAppView )
00115         {
00116         delete iAppView;
00117         iAppView = NULL;
00118         }
00119     }
00120 
00121 // -----------------------------------------------------------------------------
00122 // CHelloWorldBasicAppUi::HandleCommandL()
00123 // Takes care of command handling.
00124 // -----------------------------------------------------------------------------
00125 //
00126 void CHelloWorldBasicAppUi::HandleCommandL( TInt aCommand )
00127     {
00128     // clear possible old user-given text
00129     if (iAppView->GetText().Size() > 0)
00130         {
00131         iAppView->GetText().Zero();
00132         iAppView->DrawNow();
00133         }
00134 
00135     switch( aCommand )
00136         {
00137         case EEikCmdExit:
00138         case EAknSoftkeyExit:
00139             Exit();
00140             break;
00141 
00142         case EHelloWorldBasicCommand1:
00143             {
00144             // Load a string from the resource file and display it
00145             HBufC* textResource = StringLoader::LoadLC( R_HEWB_COMMAND1_TEXT );
00146             CAknInformationNote* note = new ( ELeave ) CAknInformationNote;
00147 
00148             // Show the information Note with
00149             // textResource loaded with StringLoader.
00150             note->ExecuteLD( *textResource );
00151 
00152             // Pop HBuf from CleanUpStack and Destroy it.
00153             CleanupStack::PopAndDestroy( textResource );
00154             }
00155             break;
00156 
00157         case EHelloWorldBasicCommand2:
00158             {
00159             RFs fsSession;
00160             RFile rFile;
00161 
00162             // Connects a client process to the fileserver
00163             User::LeaveIfError(fsSession.Connect());
00164             CleanupClosePushL(fsSession);
00165 
00166             //Open file where the stream text is
00167             User::LeaveIfError(rFile.Open(fsSession,KHelloFileName, EFileStreamText));
00168             CleanupClosePushL(rFile);
00169 
00170             // copy stream from file to RFileStream object
00171             RFileReadStream inputFileStream(rFile);
00172             CleanupClosePushL(inputFileStream);
00173 
00174             // HBufC descriptor is created from the RFileStream object.
00175             HBufC* fileData = HBufC::NewLC(inputFileStream, 32);
00176 
00177             CAknInformationNote* note = new ( ELeave ) CAknInformationNote;
00178 
00179             // Show the information Note
00180             note->ExecuteLD( *fileData );
00181 
00182             // Pop loaded resources from the cleanup stack:
00183             // filedata, inputFileStream, rFile, fsSession
00184             CleanupStack::PopAndDestroy(4, &fsSession);
00185             }
00186             break;
00187 
00188         case EHelloWorldBasicCommand3:
00189             {
00190             // Load a string from the resources and use it as a default value
00191             HBufC* defaultText = StringLoader::LoadLC( R_HEWB_FILE_TEXT );
00192 
00193             CHelloWorldQueryDialog *dlg = new (ELeave)
00194                 CHelloWorldQueryDialog( iAppView->GetText(), defaultText );
00195 
00196             dlg->ExecuteLD( R_DIALOG_TEXT_EDIT_QUERY );
00197             iAppView->DrawNow();
00198 
00199             // Pop HBuf from CleanUpStack and Destroy it.
00200             CleanupStack::PopAndDestroy( defaultText );
00201             }
00202             break;
00203 
00204         default:
00205             break;
00206         }
00207     }
00208 // -----------------------------------------------------------------------------
00209 //  Called by framework when layout is changed.
00210 //  Passes the new client rectangle to the AppView
00211 // -----------------------------------------------------------------------------
00212 //
00213 void CHelloWorldBasicAppUi::HandleResourceChangeL( TInt aType )
00214 {
00215     // base-class call also
00216     CAknAppUi::HandleResourceChangeL(aType);
00217     if (aType == KEikDynamicLayoutVariantSwitch)
00218         {
00219         if (iAppView)
00220             iAppView->SetRect( ClientRect() );
00221         }
00222 }
00223 
00224 // End of File
00225 

Generated by  doxygen 1.6.2