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

00001 /*
00002  * Copyright (c) 2003-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003  *    
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *    
00007  *  * Redistributions of source code must retain the above copyright notice, this
00008  *    list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright notice,
00010  *    this list of conditions and the following disclaimer in the documentation
00011  *    and/or other materials provided with the distribution.
00012  *  * Neither the name of Nokia Corporation nor the names of its contributors
00013  *    may be used to endorse or promote products derived from this software
00014  *    without specific prior written permission.
00015  *    
00016  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  *    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  *    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  *    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023  *    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024  *    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *    
00027  *    Description:  
00028  */
00029 
00030 #include <avkon.hrh>
00031 #include <aknnotewrappers.h>    // CAknInformationNote
00032 #include "EComCalculatorAppUi.h"
00033 #include "EComCalculatorAppView.h"
00034 #include "EComCalculator.hrh"   // menu commands
00035 #include "EComInterfaceDefinition.h"
00036 
00037 
00038 // Names of plugin operations. Valid values are defined in plugin's
00039 // resource file. See
00040 //      ..\..\plugin\101F5465.RSS
00041 // Typically plugin provider gives these in the plugin documentation.
00042 _LIT8(KSumOperationName,"sum");
00043 _LIT8(KMultiplyOperationName,"multiply");
00044 _LIT(KImplNotFound,"Implementation not found: ");
00045 _LIT(KEComErr,"ECOM error: ");
00046 _LIT(KEComErrCont,". Is the Calculation plugin installed?");
00047 
00048 const TInt KOpNameLength=32;
00049 const TInt KOpLength = 64;
00050 const TInt KErrMsgLength = 128;
00051 
00052 
00053 // Perform the second phase construction. This is allowed to leave.
00054 void CEComCalculatorAppUi::ConstructL()
00055     {
00056     BaseConstructL(EAknEnableSkin);
00057 
00058     // Create the view. Note that the view in this example is not CAknView,
00059     // but a simple Avkon UI compound control.
00060     iAppView = CEComCalculatorAppView::NewL(ClientRect());
00061 
00062     // Add the view to control stack so that it is able to receive key events.
00063     // The CEComCalculatorAppView handles them in the OfferKeyEventL method.
00064     AddToStackL(iAppView);
00065     }
00066 
00067 // Construct the AppUi. This may not leave.
00068 CEComCalculatorAppUi::CEComCalculatorAppUi()
00069     {
00070     // No implementation required
00071     }
00072 
00073 // Destructor. Release reserved resources.
00074 CEComCalculatorAppUi::~CEComCalculatorAppUi()
00075     {
00076     if (iAppView)
00077         {
00078         // Remember to remove the view from control stack.
00079         iEikonEnv->RemoveFromStack(iAppView);
00080         }
00081 
00082     delete iAppView;
00083     iAppView = NULL;
00084     REComSession::FinalClose();
00085     }
00086 
00087 // Handle menu commands. The commands are defined in EComCalculator.hrh
00088 void CEComCalculatorAppUi::HandleCommandL(TInt aCommand)
00089     {
00090     switch(aCommand)
00091         {
00092     case ECalculateSumCmd:
00093     case ECalculateMultiplyCmd:
00094         CalculateL(aCommand);
00095         break;
00096 
00097     case EAknSoftkeyExit:
00098     case EEikCmdExit:
00099         Exit();
00100         break;
00101 
00102     default:
00103         break;
00104         }
00105     }
00106 
00107 // Perform calculation and tell the view to show the result.
00108 void CEComCalculatorAppUi::CalculateL(TInt aCommand)
00109     {
00110     TBuf8<KOpNameLength> operationName; // Data used in ECOM resolution.
00111                              // Either "sum" or "multiply"
00112     TReal A, B, calcResult;
00113     _LIT(KFailedA,"Failed. A is not a valid number.");
00114     _LIT(KFailedB,"Failed. B is not a valid number.");
00115 
00116     // Gather required info to perform ECOM interface call.
00117     if( iAppView->GetA(A) != KErrNone )
00118         {
00119         ShowNoteL(KFailedA);
00120         return;
00121         }
00122     if( iAppView->GetB(B) != KErrNone )
00123         {
00124         ShowNoteL(KFailedB);
00125         return;
00126         }
00127 
00128     if(aCommand == ECalculateSumCmd)
00129         {
00130         operationName.Append(KSumOperationName);      // "sum"
00131         }
00132     else
00133         {
00134         operationName.Append(KMultiplyOperationName); // "multiply"
00135         }
00136 
00137     if(DoEComCalculationL(operationName, A, B, calcResult) == KErrNone)
00138         iAppView->ShowResultL(operationName, calcResult);
00139     }
00140 
00141 // Let the ECOM plugin do the calculation.
00142 TInt CEComCalculatorAppUi::DoEComCalculationL(const TDesC8& aOperationName,
00143     TReal aA, TReal aB, TReal& aResult)
00144     {
00145         // Create instance of our own ECOM interface definition. The NewL
00146         // method will ask ECOM framework to find appropriate implementation
00147         // for the interface. The file
00148         //     ..\..\interface\EComInterfaceDefinition.inl
00149         // defines, how the framework will search. It uses default resolver.
00150         //
00151         // The NewL fails, if there is no plugin installed in the system,
00152         // or if an implementation for aOperationName cannot be found
00153         // (resolved).
00154         //
00155         //  - aOperationName must match one of the names of plugin
00156         //    implementations. The valid names are defined in plugin
00157         //    implementation resource file, see
00158         //        ..\..\plugin\101F5465.RSS
00159         //    The field "default_data" specifies name for a implementation.
00160         //    (In more detail, it is the identification data used by resolver.
00161         //    Default resolver used in this example just checks the matching
00162         //    string.)
00163         //
00164         //  - In emulator, the plugin implementation project must be built.
00165         //    In device, the plugin dll providing implementations must be
00166         //    installed. See
00167         //        ..\..\plugin.
00168         //    If there is no plugin available, the ECOM framework causes leave.
00169         //
00170         CCalculationInterfaceDefinition* calculateInterface = NULL;
00171         TRAPD(error,
00172             calculateInterface =
00173                 CCalculationInterfaceDefinition::NewL(aOperationName);
00174         )
00175         if( error )
00176         {
00177             HandleEComInitializationErrorL(error, aOperationName);
00178             return error;
00179         }
00180         CleanupStack::PushL(calculateInterface);
00181 
00182         // Use given interface implementation to perform calculation.
00183         aResult = calculateInterface->Calculate(aA, aB);
00184 
00185         CleanupStack::PopAndDestroy(calculateInterface);
00186         return KErrNone;
00187     }
00188 
00189 // Show error note for ECOM errors
00190 void CEComCalculatorAppUi::HandleEComInitializationErrorL(TInt aError,
00191     const TDesC8& aOperationName) const
00192     {
00193 
00194     TBuf<KOpLength> operationName16bit;
00195     TBuf<KErrMsgLength> errorMsg;
00196     operationName16bit.Copy(aOperationName);
00197 
00198     if(aError == KErrNotFound)  // Plugin implementation not found (no match)
00199         {
00200         errorMsg.Append(KImplNotFound);
00201         errorMsg.Append(operationName16bit);
00202         ShowNoteL(errorMsg);
00203         }
00204     else if(aError != KErrNone) // Some other ECOM error. See in SDK help
00205         {
00206         errorMsg.Append(KEComErr);
00207         errorMsg.AppendNum(aError);
00208         errorMsg.Append(KEComErrCont);
00209         ShowNoteL(errorMsg);
00210         }
00211     }
00212 
00213 // Show a note. Note that successive frequent calls to this method results in
00214 // showing the latest message only.
00215 void CEComCalculatorAppUi::ShowNoteL(const TDesC& aMessage) const
00216     {
00217 
00218     CAknInformationNote* note = new(ELeave)CAknInformationNote;
00219     note->ExecuteLD(aMessage); // Deletes itself, when returns
00220     }
00221 
00222 void CEComCalculatorAppUi::HandleResourceChangeL(TInt aType)
00223     {
00224     CAknAppUi::HandleResourceChangeL(aType); //call to upper class
00225 
00226     // ADDED FOR SCALABLE UI SUPPORT
00227     // *****************************
00228     if ( aType == KEikDynamicLayoutVariantSwitch )
00229         {
00230         iAppView->SetRect( ClientRect() );
00231         }
00232     }

Generated by  doxygen 1.6.2