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

Generated by  doxygen 1.6.2