examples/ForumNokia/EComCalculator/client/src/EComCalculatorAppui.cpp

00001 /*
00002  * ============================================================================
00003  *  Name     : CEComCalculatorAppUi from CEComCalculatorAppui.cpp
00004  *  Part of  : EComCalculator
00005  *  Created  : 29/05/2006 by Forum Nokia
00006  *  Version  : 2.0
00007  *  Copyright: Nokia Corporation
00008  * ============================================================================
00009  */
00010 
00011 #include <avkon.hrh>
00012 #include <aknnotewrappers.h>    // CAknInformationNote
00013 #include "EComCalculatorAppUi.h"
00014 #include "EComCalculatorAppView.h"
00015 #include "EComCalculator.hrh"   // menu commands
00016 #include "EComInterfaceDefinition.h"
00017 
00018 
00019 // Names of plugin operations. Valid values are defined in plugin's
00020 // resource file. See
00021 //      ..\..\plugin\101F5465.RSS
00022 // Typically plugin provider gives these in the plugin documentation.
00023 _LIT8(KSumOperationName,"sum");
00024 _LIT8(KMultiplyOperationName,"multiply");
00025 _LIT(KImplNotFound,"Implementation not found: ");
00026 _LIT(KEComErr,"ECOM error: ");
00027 _LIT(KEComErrCont,". Is the Calculation plugin installed?");
00028 
00029 const TInt KOpNameLength=32;
00030 const TInt KOpLength = 64;
00031 const TInt KErrMsgLength = 128;
00032 
00033 
00034 // Perform the second phase construction. This is allowed to leave.
00035 void CEComCalculatorAppUi::ConstructL()
00036     {
00037     BaseConstructL(EAknEnableSkin);
00038 
00039     // Create the view. Note that the view in this example is not CAknView,
00040     // but a simple Avkon UI compound control.
00041     iAppView = CEComCalculatorAppView::NewL(ClientRect());
00042 
00043     // Add the view to control stack so that it is able to receive key events.
00044     // The CEComCalculatorAppView handles them in the OfferKeyEventL method.
00045     AddToStackL(iAppView);
00046     }
00047 
00048 // Construct the AppUi. This may not leave.
00049 CEComCalculatorAppUi::CEComCalculatorAppUi()
00050     {
00051     // No implementation required
00052     }
00053 
00054 // Destructor. Release reserved resources.
00055 CEComCalculatorAppUi::~CEComCalculatorAppUi()
00056     {
00057     if (iAppView)
00058         {
00059         // Remember to remove the view from control stack.
00060         iEikonEnv->RemoveFromStack(iAppView);
00061         }
00062 
00063     delete iAppView;
00064     iAppView = NULL;
00065 
00066 #ifdef __SERIES60_3X__
00067     REComSession::FinalClose();
00068 #endif
00069     }
00070 
00071 // Handle menu commands. The commands are defined in EComCalculator.hrh
00072 void CEComCalculatorAppUi::HandleCommandL(TInt aCommand)
00073     {
00074     switch(aCommand)
00075         {
00076     case ECalculateSumCmd:
00077     case ECalculateMultiplyCmd:
00078         CalculateL(aCommand);
00079         break;
00080 
00081     case EAknSoftkeyExit:
00082     case EEikCmdExit:
00083         Exit();
00084         break;
00085 
00086     default:
00087         break;
00088         }
00089     }
00090 
00091 // Perform calculation and tell the view to show the result.
00092 void CEComCalculatorAppUi::CalculateL(TInt aCommand)
00093     {
00094     TBuf8<KOpNameLength> operationName; // Data used in ECOM resolution.
00095                              // Either "sum" or "multiply"
00096     TReal A, B, calcResult;
00097     _LIT(KFailedA,"Failed. A is not a valid number.");
00098     _LIT(KFailedB,"Failed. B is not a valid number.");
00099 
00100     // Gather required info to perform ECOM interface call.
00101     if( iAppView->GetA(A) != KErrNone )
00102         {
00103         ShowNoteL(KFailedA);
00104         return;
00105         }
00106     if( iAppView->GetB(B) != KErrNone )
00107         {
00108         ShowNoteL(KFailedB);
00109         return;
00110         }
00111 
00112     if(aCommand == ECalculateSumCmd)
00113         {
00114         operationName.Append(KSumOperationName);      // "sum"
00115         }
00116     else
00117         {
00118         operationName.Append(KMultiplyOperationName); // "multiply"
00119         }
00120 
00121     if(DoEComCalculationL(operationName, A, B, calcResult) == KErrNone)
00122         iAppView->ShowResultL(operationName, calcResult);
00123     }
00124 
00125 // Let the ECOM plugin do the calculation.
00126 TInt CEComCalculatorAppUi::DoEComCalculationL(const TDesC8& aOperationName,
00127     TReal aA, TReal aB, TReal& aResult)
00128     {
00129         // Create instance of our own ECOM interface definition. The NewL
00130         // method will ask ECOM framework to find appropriate implementation
00131         // for the interface. The file
00132         //     ..\..\interface\EComInterfaceDefinition.inl
00133         // defines, how the framework will search. It uses default resolver.
00134         //
00135         // The NewL fails, if there is no plugin installed in the system,
00136         // or if an implementation for aOperationName cannot be found
00137         // (resolved).
00138         //
00139         //  - aOperationName must match one of the names of plugin
00140         //    implementations. The valid names are defined in plugin
00141         //    implementation resource file, see
00142         //        ..\..\plugin\101F5465.RSS
00143         //    The field "default_data" specifies name for a implementation.
00144         //    (In more detail, it is the identification data used by resolver.
00145         //    Default resolver used in this example just checks the matching
00146         //    string.)
00147         //
00148         //  - In emulator, the plugin implementation project must be built.
00149         //    In device, the plugin dll providing implementations must be
00150         //    installed. See
00151         //        ..\..\plugin.
00152         //    If there is no plugin available, the ECOM framework causes leave.
00153         //
00154         CCalculationInterfaceDefinition* calculateInterface = NULL;
00155         TRAPD(error,
00156             calculateInterface =
00157                 CCalculationInterfaceDefinition::NewL(aOperationName);
00158         )
00159         if( error )
00160         {
00161             HandleEComInitializationErrorL(error, aOperationName);
00162             return error;
00163         }
00164         CleanupStack::PushL(calculateInterface);
00165 
00166         // Use given interface implementation to perform calculation.
00167         aResult = calculateInterface->Calculate(aA, aB);
00168 
00169         CleanupStack::PopAndDestroy(calculateInterface);
00170         return KErrNone;
00171     }
00172 
00173 // Show error note for ECOM errors
00174 void CEComCalculatorAppUi::HandleEComInitializationErrorL(TInt aError,
00175     const TDesC8& aOperationName) const
00176     {
00177 
00178     TBuf<KOpLength> operationName16bit;
00179     TBuf<KErrMsgLength> errorMsg;
00180     operationName16bit.Copy(aOperationName);
00181 
00182     if(aError == KErrNotFound)  // Plugin implementation not found (no match)
00183         {
00184         errorMsg.Append(KImplNotFound);
00185         errorMsg.Append(operationName16bit);
00186         ShowNoteL(errorMsg);
00187         }
00188     else if(aError != KErrNone) // Some other ECOM error. See in SDK help
00189         {
00190         errorMsg.Append(KEComErr);
00191         errorMsg.AppendNum(aError);
00192         errorMsg.Append(KEComErrCont);
00193         ShowNoteL(errorMsg);
00194         }
00195     }
00196 
00197 // Show a note. Note that successive frequent calls to this method results in
00198 // showing the latest message only.
00199 void CEComCalculatorAppUi::ShowNoteL(const TDesC& aMessage) const
00200     {
00201 
00202     CAknInformationNote* note = new(ELeave)CAknInformationNote;
00203     note->ExecuteLD(aMessage); // Deletes itself, when returns
00204     }
00205 
00206 void CEComCalculatorAppUi::HandleResourceChangeL(TInt aType)
00207     {
00208     CAknAppUi::HandleResourceChangeL(aType); //call to upper class
00209 
00210     // ADDED FOR SCALABLE UI SUPPORT
00211     // *****************************
00212     if ( aType == KEikDynamicLayoutVariantSwitch )
00213         {
00214         iAppView->SetRect( ClientRect() );
00215         }
00216     }

Generated by  doxygen 1.6.2