|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 ============================================================================ 00003 Name : HsWidgetExample.cpp 00004 Author : 00005 Version : 1.1 00006 Copyright : This material, including documentation and any related computer 00007 programs, is protected by copyright controlled by Nokia Corporation. 00008 Description : CHsWidgetExample implementation 00009 ============================================================================ 00010 */ 00011 00012 #include "HsWidgetExample.h" 00013 #include <HsWidget.rsg> 00014 #include <hswidgetpublisher.h> 00015 #include <hswidget.h> 00016 #include <hsexception.h> 00017 00018 #include <coemain.h> 00019 #include <eikenv.h> 00020 #include <utf.h> 00021 #include <apgtask.h> 00022 #include <StringLoader.h> 00023 00024 // logging 00025 #include "logger.h" 00026 // the name of widget template's image item 00027 const char* image = "image1"; 00028 // the name of widget template's text item 00029 const char* text = "text1"; 00030 00031 // ----------------------------------------------------------------------------- 00032 // CHsWidgetExample::CHsWidgetExample() 00033 // C++ constructor for performing 1st stage construction 00034 // ----------------------------------------------------------------------------- 00035 // 00036 CHsWidgetExample::CHsWidgetExample() 00037 { 00038 // No implementation required 00039 } 00040 00041 // ----------------------------------------------------------------------------- 00042 // CHsWidgetExample::~CHsWidgetExample() 00043 // C++ destructor 00044 // ----------------------------------------------------------------------------- 00045 // 00046 00047 CHsWidgetExample::~CHsWidgetExample() 00048 { 00049 delete iHsWidgetPublisher; 00050 } 00051 00052 // ----------------------------------------------------------------------------- 00053 // CHsWidgetExample::NewLC() 00054 // Two-phased constructor. Leaves the constructed object on the cleanup stack 00055 // ----------------------------------------------------------------------------- 00056 // 00057 CHsWidgetExample* CHsWidgetExample::NewLC() 00058 { 00059 CHsWidgetExample* self = new (ELeave)CHsWidgetExample(); 00060 CleanupStack::PushL(self); 00061 self->ConstructL(); 00062 return self; 00063 } 00064 00065 // ----------------------------------------------------------------------------- 00066 // CHsWidgetExample::NewL() 00067 // Two-phased constructor 00068 // ----------------------------------------------------------------------------- 00069 // 00070 CHsWidgetExample* CHsWidgetExample::NewL() 00071 { 00072 CHsWidgetExample* self=CHsWidgetExample::NewLC(); 00073 CleanupStack::Pop(); // self; 00074 return self; 00075 } 00076 00077 // ----------------------------------------------------------------------------- 00078 // CHsWidgetExample::ConstructL() 00079 // Two-phased constructor. 00080 // Create instance of the home Screen Publishing API's HsWidgetPublisher and loads 00081 // widget's definition from the resource file 00082 // ----------------------------------------------------------------------------- 00083 // 00084 void CHsWidgetExample::ConstructL() 00085 { 00086 __LOGSTR_TOFILE("Enter CHsWidgetExample::ConstructL"); 00087 00088 try 00089 { 00090 iHsWidgetPublisher = new Hs::HsWidgetPublisher( this ); 00091 } 00092 catch( Hs::HsException& e) 00093 { 00094 //catch error from Homescreen Publishing API 00095 __LOGSTR_TOFILE1("[-] HSPAPI exception : %d", e.getReason()); 00096 User::Leave( e.getReason() ); 00097 } 00098 catch( ... ) 00099 { 00100 //catch error from other libraries 00101 __LOGSTR_TOFILE("[-] Generic exception"); 00102 User::Leave( KErrGeneral ); 00103 } 00104 //converitng string from UNICODE to UTF-8 std::string 00105 HBufC* buf1 = StringLoader::LoadLC( R_TEMPLATE_TYPE ); 00106 templateType = ToStringL( *buf1 ); 00107 CleanupStack::PopAndDestroy(buf1); // delete buf1 00108 buf1 = StringLoader::LoadLC( R_WIDGET_NAME ); 00109 widgetName = ToStringL( *buf1 ); 00110 CleanupStack::PopAndDestroy(buf1); // delete buf1 00111 buf1 = StringLoader::LoadLC( R_WIDGET_ID ); 00112 widgetId = ToStringL( *buf1 ); 00113 CleanupStack::PopAndDestroy(buf1); // delete buf1 00114 00115 __LOGSTR_TOFILE("Exit CHsWidgetExample::ConstructL"); 00116 } 00117 00118 // ----------------------------------------------------------------------------- 00119 // CHsWidgetExample::RegisterWidget() 00120 // Once registered the widget will become available to the user for adding it to 00121 // the home screen. If the widgetId is already registered the iHsWidgetPublisher 00122 // will now point to it. Also, note that the Home Screen Publishing API will 00123 // record this application's UID3/SID as the publiser of this widget and based 00124 // on this information the application will be started in the background if at 00125 // any given time an event is due to be sent to it but the application is found 00126 // not to be running. 00127 // ----------------------------------------------------------------------------- 00128 // 00129 void CHsWidgetExample::RegisterWidgetL() 00130 { 00131 __LOGSTR_TOFILE("Enter CHsWidgetExample::RegisterWidgetL"); 00132 try 00133 { 00134 iHsWidgetPublisher->createHsWidget( templateType, 00135 widgetName, widgetId ); 00136 } 00137 catch( Hs::HsException& e) 00138 { 00139 //catch error from Homescreen Publishing API 00140 __LOGSTR_TOFILE1("[-] HSPAPI exception : %d", e.getReason()); 00141 User::Leave( e.getReason() ); 00142 } 00143 catch( ... ) 00144 { 00145 //catch error from other libraries 00146 __LOGSTR_TOFILE("[-] Generic exception"); 00147 User::Leave( KErrGeneral ); 00148 } 00149 __LOGSTR_TOFILE("Exit CHsWidgetExample::RegisterWidgetL"); 00150 } 00151 00152 00153 // ----------------------------------------------------------------------------- 00154 // CHsWidgetExample::PublishWidget() 00155 // The template is published by the Home Screen Publishing API. If the home screen 00156 // gets the template's data it obtains actual information. 00157 // ----------------------------------------------------------------------------- 00158 // 00159 void CHsWidgetExample::PublishWidgetL() 00160 { 00161 __LOGSTR_TOFILE("Enter CHsWidgetExample::PublishWidgetL"); 00162 try 00163 { 00164 iHsWidgetPublisher->publishHsWidget( 00165 iHsWidgetPublisher->getHsWidget( templateType, 00166 widgetName, widgetId ) ); 00167 } 00168 catch( Hs::HsException& e) 00169 { 00170 //catch error from Homescreen Publishing API 00171 __LOGSTR_TOFILE1("[-] HSPAPI exception : %d", e.getReason()); 00172 User::Leave( e.getReason() ); 00173 } 00174 catch( ... ) 00175 { 00176 //catch error from other libraries 00177 __LOGSTR_TOFILE("[-] Generic exception"); 00178 User::Leave( KErrGeneral ); 00179 } 00180 __LOGSTR_TOFILE("Exit CHsWidgetExample::PublishWidgetL"); 00181 } 00182 00183 // ----------------------------------------------------------------------------- 00184 // CHsWidgetExample::ChangeWidgetValues() 00185 // Change templates items values. 00186 // ----------------------------------------------------------------------------- 00187 // 00188 void CHsWidgetExample::ChangeWidgetValuesL() 00189 { 00190 __LOGSTR_TOFILE("Enter CHsWidgetExample::ChangeWidgetValuesL"); 00191 try 00192 { 00193 HBufC* buf1 = NULL; 00194 HBufC* buf2 = NULL; 00195 if( !( iCounter % 2 ) ) 00196 { 00197 buf1 = CCoeEnv::Static()-> 00198 AllocReadResourceL( R_FILE_BEGINING ); 00199 buf2 = CCoeEnv::Static()-> 00200 AllocReadResourceL( R_TEXT_BEGINING ); 00201 } 00202 else 00203 { 00204 buf1 = CCoeEnv::Static()-> 00205 AllocReadResourceL( R_FILE_UPDATED ); 00206 buf2 = CCoeEnv::Static()-> 00207 AllocReadResourceL( R_TEXT_UPDATED ); 00208 } 00209 00210 iHsWidgetPublisher->getHsWidget( templateType, widgetName, widgetId ). 00211 setItem( image, ToStringL( *buf1 ) ); 00212 iHsWidgetPublisher->getHsWidget( templateType, widgetName, widgetId ). 00213 setItem( text, ToStringL( *buf2 ) ); 00214 00215 delete buf1; 00216 delete buf2; 00217 } 00218 catch( Hs::HsException& e) 00219 { 00220 //catch error from Homescreen Publishing API 00221 __LOGSTR_TOFILE1("HSPAPI exception : %d", e.getReason()); 00222 User::Leave( e.getReason() ); 00223 } 00224 catch( ... ) 00225 { 00226 //catch error from other libraries 00227 __LOGSTR_TOFILE("Generic exception"); 00228 User::Leave( KErrGeneral ); 00229 } 00230 iCounter++; 00231 __LOGSTR_TOFILE("Exit CHsSMWidgetExample::ChangeWidgetValuesL"); 00232 } 00233 00234 // ----------------------------------------------------------------------------- 00235 // CHsWidgetExample::RemoveWidget() 00236 // Remove template from the Home Screen Publishing API. If the widget is currently 00237 // visible on the home screen it will be removed. 00238 // Once removed the widget is now not available for selection on home screen and 00239 // it doesn't report events 00240 // ----------------------------------------------------------------------------- 00241 // 00242 void CHsWidgetExample::RemoveWidgetL() 00243 { 00244 __LOGSTR_TOFILE("Enter CHsWidgetExample::RemoveWidgetL"); 00245 try 00246 { 00247 iHsWidgetPublisher->removeHsWidget( templateType, 00248 widgetName, widgetId); 00249 } 00250 catch( Hs::HsException& e) 00251 { 00252 //catch error from Homescreen Publishing API 00253 __LOGSTR_TOFILE1("HSPAPI exception : %d", e.getReason()); 00254 User::Leave( e.getReason() ); 00255 } 00256 catch( ... ) 00257 { 00258 //catch error from other libraries 00259 __LOGSTR_TOFILE("Generic exception"); 00260 User::Leave( KErrGeneral ); 00261 } 00262 __LOGSTR_TOFILE("Exit CHsWidgetExample::RemoveWidgetL"); 00263 } 00264 00265 // ----------------------------------------------------------------------------- 00266 // CHsWidgetExample::handleEvent 00267 // Template is being published when the received event is EActivate or EResume 00268 // ----------------------------------------------------------------------------- 00269 // 00270 void CHsWidgetExample::handleEvent( std::string /*aWidgetName*/, 00271 Hs::IHsDataObserver::EEvent aEvent ) 00272 { 00273 __LOGSTR_TOFILE("Enter CHsWidgetExample::handleEvent"); 00274 00275 // this method should not leave 00276 // TRAP all leaving methods 00277 switch(aEvent) 00278 { 00279 case EActivate: 00280 case EResume: 00281 { 00282 TRAP_IGNORE(ChangeWidgetValuesL()); 00283 TRAP_IGNORE(PublishWidgetL()); 00284 } 00285 break; 00286 default: 00287 break; 00288 } 00289 00290 __LOGSTR_TOFILE("Exit CHsWidgetExample::handleEvent"); 00291 } 00292 00293 // ----------------------------------------------------------------------------- 00294 // If the ESelect event was triggerred by the image item it will trigger the 00295 // content of the widget items to be changed. If the event is triggered by the 00296 // text item the publisher application will be sent to foreground. 00297 // To be noted that if the publisher application is not running the home screen 00298 // application will start the publisher. The subsequent events will be sent to 00299 // the now running publisher application. 00300 // ----------------------------------------------------------------------------- 00301 // 00302 void CHsWidgetExample::handleItemEvent( std::string /*aWidgetName*/, 00303 std::string aTemplateItemName, 00304 Hs::IHsDataObserver::EItemEvent /*aEvent*/) 00305 { 00306 __LOGSTR_TOFILE("Enter CHsWidgetExample::handleItemEvent"); 00307 00308 // this method should not leave 00309 // TRAP all leaving methods 00310 if(aTemplateItemName.compare(image)==0) 00311 { 00312 TRAP_IGNORE(ChangeWidgetValuesL()); 00313 TRAP_IGNORE(PublishWidgetL()); 00314 } 00315 else 00316 { 00317 TApaTask task( CEikonEnv::Static()->WsSession() ); 00318 task.SetWgId(CEikonEnv::Static()->RootWin().Identifier()); 00319 task.BringToForeground(); 00320 } 00321 00322 __LOGSTR_TOFILE("Exit CHsWidgetExample::handleItemEvent"); 00323 } 00324 00325 // ----------------------------------------------------------------------------- 00326 // CHsWidgetExample::ToStringL 00327 // Converts string from UNICODE to UTF-8 std::string. 00328 // Add one extra character for the ending zero. 00329 // ----------------------------------------------------------------------------- 00330 // 00331 std::string CHsWidgetExample::ToStringL(const TDesC& aText) 00332 { 00333 HBufC8* text = HBufC8::NewLC( aText.Length() + 4 /*for ending zero*/ ); 00334 TPtr8 dest( text->Des() ); 00335 CnvUtfConverter::ConvertFromUnicodeToUtf8( dest, aText ); 00336 std::string ret((const char*)dest.PtrZ()); 00337 CleanupStack::PopAndDestroy(text); // delete text 00338 return ret; 00339 } 00340 00341 00342
|
© Nokia 2009 |