Nokia N97 SDK
Example Applications Guide

HsWidgetExample.cpp

00001 /*
00002  ============================================================================
00003  Name           : HsWidgetExample.cpp
00004  Author   : 
00005  Version         : 1.0
00006  Copyright   : Copyright (c) 2006 Nokia Corporation.
00007  Description : CHsWidgetExample implementation
00008  ============================================================================
00009  */
00010 
00011 #include "HsWidgetExample.h"
00012 #include <HsWidget.rsg>
00013 #include <hswidgetpublisher.h>
00014 #include <hswidget.h>
00015 #include <hsexception.h>
00016 #include <hsdataobserver.h>
00017 
00018 #include <coemain.h> 
00019 #include <eikenv.h> 
00020 #include <utf.h> 
00021 #include <apgtask.h>
00022 
00023 const char* image = "image1";
00024 const char* text = "text1";
00025 
00026 // -----------------------------------------------------------------------------
00027 // CHsWidgetExample::CHsWidgetExample()
00028 // 
00029 // -----------------------------------------------------------------------------
00030 //
00031 CHsWidgetExample::CHsWidgetExample()
00032         {
00033         // No implementation required
00034         }
00035 
00036 // -----------------------------------------------------------------------------
00037 // CHsWidgetExample::~CHsWidgetExample()
00038 // 
00039 // -----------------------------------------------------------------------------
00040 //
00041 
00042 CHsWidgetExample::~CHsWidgetExample()
00043         {
00044         delete iHsWidgetPublisher;
00045         }
00046 
00047 // -----------------------------------------------------------------------------
00048 // CHsWidgetExample::NewLC()
00049 // 
00050 // -----------------------------------------------------------------------------
00051 //
00052 CHsWidgetExample* CHsWidgetExample::NewLC()
00053         {
00054         CHsWidgetExample* self = new (ELeave)CHsWidgetExample();
00055         CleanupStack::PushL(self);
00056         self->ConstructL();
00057         return self;
00058         }
00059 
00060 // -----------------------------------------------------------------------------
00061 // CHsWidgetExample::NewL()
00062 // 
00063 // -----------------------------------------------------------------------------
00064 //
00065 CHsWidgetExample* CHsWidgetExample::NewL()
00066         {
00067         CHsWidgetExample* self=CHsWidgetExample::NewLC();
00068         CleanupStack::Pop(); // self;
00069         return self;
00070         }
00071 
00072 // -----------------------------------------------------------------------------
00073 // CHsWidgetExample::ConstructL()
00074 // Create inscance of HSPApi
00075 // -----------------------------------------------------------------------------
00076 //
00077 void CHsWidgetExample::ConstructL()
00078         {
00079         try
00080                 {
00081                 iHsWidgetPublisher = new Hs::HsWidgetPublisher( this );
00082                 }
00083         catch( Hs::HsException& e)
00084                 {
00085                 //catch error from HSPApi
00086                 User::Leave( e.getReason() );
00087                 }
00088         catch( ... )
00089                 {
00090                 //catch error from other libraries
00091                 User::Leave( KErrGeneral );
00092                 }
00093         //converitng string from UNICODE to UTF-8 std::string
00094         HBufC* buf1 = CCoeEnv::Static()->AllocReadResourceL( R_TEMPLATE_TYPE );
00095         templateType = ToString( *buf1 );
00096         delete buf1;
00097         buf1 = CCoeEnv::Static()->AllocReadResourceL( R_WIDGET_NAME );
00098         widgetName = ToString( *buf1 );
00099         delete buf1;
00100         buf1 = CCoeEnv::Static()->AllocReadResourceL( R_WIDGET_ID );
00101         widgetId = ToString( *buf1 );
00102         delete buf1;
00103         }
00104 
00105 // -----------------------------------------------------------------------------
00106 // CHsWidgetExample::RegisterWidget()
00107 // 
00108 // -----------------------------------------------------------------------------
00109 //
00110 void CHsWidgetExample::RegisterWidget()
00111         {
00112         try
00113                 {
00114                 iHsWidgetPublisher->createHsWidget( templateType, widgetName, widgetId );
00115                 }
00116         catch( Hs::HsException& e)
00117                 {
00118                 //catch error from HSPApi
00119                 User::Leave( e.getReason() );
00120                 }
00121         catch( ... )
00122                 {
00123                 //catch error from other libraries
00124                 User::Leave( KErrGeneral );
00125                 }
00126         }
00127 
00128 // -----------------------------------------------------------------------------
00129 // CHsWidgetExample::PublishWidget()
00130 // Template is published by HSPApi. If HS gets template's data it obtain actual
00131 // information. 
00132 // -----------------------------------------------------------------------------
00133 //
00134 void CHsWidgetExample::PublishWidget()
00135         {
00136         try
00137                 {       
00138                 iHsWidgetPublisher->publishHsWidget( 
00139                         iHsWidgetPublisher->getHsWidget( templateType, widgetName,
00140                                 widgetId ) );
00141                 }
00142         catch( Hs::HsException& e)
00143                 {
00144                 //catch error from HSPApi
00145                 User::Leave( e.getReason() );
00146                 }
00147         catch( ... )
00148                 {
00149                 //catch error from other libraries
00150                 User::Leave( KErrGeneral );
00151                 }
00152         }
00153 
00154 // -----------------------------------------------------------------------------
00155 // CHsWidgetExample::ChangeWidgetValues()
00156 // Change templates items values.
00157 // -----------------------------------------------------------------------------
00158 //
00159 void CHsWidgetExample::ChangeWidgetValuesL()
00160         {
00161         try
00162                 {
00163                 HBufC* buf1 = NULL;
00164                 HBufC* buf2 = NULL;
00165                 if( !( iCounter % 2 ) )
00166                         {
00167                         buf1 = CCoeEnv::Static()->AllocReadResourceL( R_FILE_BEGINING );
00168                         buf2 = CCoeEnv::Static()->AllocReadResourceL( R_TEXT_BEGINING );
00169                         }
00170                 else
00171                         {
00172                         buf1 = CCoeEnv::Static()->AllocReadResourceL( R_FILE_UPDATED );
00173                         buf2 = CCoeEnv::Static()->AllocReadResourceL( R_TEXT_UPDATED );
00174                         }
00175                 
00176                 iHsWidgetPublisher->getHsWidget( templateType, widgetName, widgetId ).
00177                         setItem( image, ToString( *buf1 ) );
00178                 iHsWidgetPublisher->getHsWidget( templateType, widgetName, widgetId ).
00179                         setItem( text, ToString( *buf2 ) );
00180                 
00181                 delete buf1;
00182                 delete buf2;
00183                 }
00184         catch( Hs::HsException& e)
00185                 {
00186                 //catch error from HSPApi
00187                 User::Leave( e.getReason() );
00188                 }
00189         catch( ... )
00190                 {
00191                 //catch error from other libraries
00192                 User::Leave( KErrGeneral );
00193                 }
00194         iCounter++;
00195         }
00196 
00197 // -----------------------------------------------------------------------------
00198 // CHsWidgetExample::RemoveWidget()
00199 // Remove template from HSPApi. 
00200 // -----------------------------------------------------------------------------
00201 //
00202 void CHsWidgetExample::RemoveWidget()
00203         {
00204         try
00205                 {       
00206                 iHsWidgetPublisher->removeHsWidget( templateType, widgetName, widgetId);
00207                 }
00208         catch( Hs::HsException& e)
00209                 {
00210                 //catch error from HSPApi
00211                 User::Leave( e.getReason() );
00212                 }
00213         catch( ... )
00214                 {
00215                 //catch error from other libraries
00216                 User::Leave( KErrGeneral );
00217                 }
00218         }
00219 
00220 // -----------------------------------------------------------------------------
00221 // CHsWidgetExample::handleEvent
00222 // Template is being published when comming action is EActivated or EResumed
00223 // -----------------------------------------------------------------------------
00224 //
00225 void CHsWidgetExample::handleEvent( std::string /*aWidgetName*/, 
00226         Hs::IHsDataObserver::EEvent aEvent )    
00227         {
00228         if ( aEvent == EResume )
00229                 {
00230                 PublishWidget();
00231                 }
00232         }
00233 
00234 // -----------------------------------------------------------------------------
00235 // CHsWidgetExample::handleItemEvent
00236 // Brings application to foreground.
00237 // -----------------------------------------------------------------------------
00238 //
00239 void CHsWidgetExample::handleItemEvent( std::string /*aWidgetName*/,
00240     std::string /*aTemplateItemName*/,
00241     Hs::IHsDataObserver::EItemEvent /*aEvent*/)
00242         {
00243         TApaTask task( CEikonEnv::Static()->WsSession() );
00244         task.SetWgId(CEikonEnv::Static()->RootWin().Identifier());
00245         task.BringToForeground();
00246         }
00247 
00248 // -----------------------------------------------------------------------------
00249 // CHsWidgetExample::ToString
00250 // Converts string from UNICODE to UTF-8 std::string.
00251 // Add one extra character fot ending zero. 
00252 // -----------------------------------------------------------------------------
00253 //
00254 std::string CHsWidgetExample::ToString(const TDesC& aText)
00255     {
00256     HBufC8* text = HBufC8::NewL( aText.Length() + 4 /*for ending zero*/ );
00257     TPtr8 dest( text->Des() );
00258     CnvUtfConverter::ConvertFromUnicodeToUtf8( dest, aText );    
00259     std::string ret((const char*)dest.PtrZ());
00260     delete text;
00261     return ret;
00262     }
00263 

© Nokia 2009

Back to top