Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / cpp / Sample / Tizen C++ / WebViewer / WebViewer / project / src / WebViewer.cpp
1 //\r
2 // Open Service Platform\r
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.\r
4 //\r
5 // Licensed under the Flora License, Version 1.0 (the License);\r
6 // you may not use this file except in compliance with the License.\r
7 // You may obtain a copy of the License at\r
8 //\r
9 //     http://www.tizenopensource.org/license\r
10 //\r
11 // Unless required by applicable law or agreed to in writing, software\r
12 // distributed under the License is distributed on an AS IS BASIS,\r
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14 // See the License for the specific language governing permissions and\r
15 // limitations under the License.\r
16 //\r
17 //==================================================================\r
18 // APP. Name   : WebViewer.cpp\r
19 // Version     : 1.0.0\r
20 // Vendor      : \r
21 // Description : \r
22 //==================================================================\r
23 \r
24 #include <FSysPowerManager.h>\r
25 #include "WebViewer.h"\r
26 \r
27 using namespace Osp::Base;\r
28 using namespace Osp::Base::Utility;\r
29 using namespace Osp::Graphics;\r
30 using namespace Osp::Locales;\r
31 using namespace Osp::System;\r
32 using namespace Osp::App;\r
33 using namespace Osp::Ui;\r
34 using namespace Osp::Ui::Controls;\r
35 using namespace Osp::Web::Controls;\r
36 \r
37 #define AC_BUTTON_WEB_BACK                      0x3001\r
38 #define AC_BUTTON_WEB_FORWARD           0x3002\r
39 \r
40 #define AC_BUTTON_WEB_GOTO                      0x5001\r
41 #define AC_BUTTON_WEB_STOP                      0x5002\r
42 \r
43 #define WEB_URL_PREFIX_HTTP                     L"http://"\r
44 #define WEB_URL_EDIT_FIELD_MAX_SIZE 1024\r
45 \r
46 WebViewer::WebViewer()\r
47         : __pMainForm(null), __pWeb(null), __pEdit(null), __pButton(null)\r
48 {\r
49 }\r
50 \r
51 \r
52 WebViewer::~WebViewer()\r
53 {\r
54 }\r
55 \r
56 \r
57 Application*\r
58 WebViewer::CreateInstance(void)\r
59 {\r
60         // You can create the instance through another constructor.\r
61         return new WebViewer();\r
62 }\r
63 \r
64 \r
65 bool\r
66 WebViewer::OnAppInitializing(AppRegistry& appRegistry)\r
67 {\r
68         /////////////////////////////////////////////////////////////////////////////////////\r
69         //this codes is automatically inserted by Tizen C++ application wizard.\r
70         Frame* pAppFrame = new Frame();\r
71         pAppFrame->Construct();\r
72         AddFrame(*pAppFrame);\r
73         ////////////////////////////////////////////////////////////////////////////////////\r
74         result r = E_SUCCESS;\r
75 \r
76         r = CreateWebForm();\r
77         if (IsFailed(r))\r
78         {\r
79                 AppLog("CreateMainForm() has failed.\n");\r
80                 goto CATCH;\r
81         }\r
82 \r
83         // Uncomment the following statement to listen to the screen on/off events.\r
84         //PowerManager::SetScreenEventListener(*this);\r
85 \r
86         return true;\r
87 \r
88 CATCH:\r
89         return false;\r
90 }\r
91 \r
92 bool\r
93 WebViewer::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)\r
94 {\r
95         // TODO:\r
96         // Deallocate or close any resources still alive.\r
97         // Save the application's current states, if applicable.\r
98         // If this method is successful, return true; otherwise, return false.\r
99 \r
100         return true;\r
101 }\r
102 \r
103 \r
104 void\r
105 WebViewer::OnForeground(void)\r
106 {\r
107 }\r
108 \r
109 \r
110 void\r
111 WebViewer::OnBackground(void)\r
112 {\r
113 }\r
114 \r
115 \r
116 void\r
117 WebViewer::OnLowMemory(void)\r
118 {\r
119         // TODO:\r
120         // Deallocate as many resources as possible.\r
121 }\r
122 \r
123 \r
124 void\r
125 WebViewer::OnBatteryLevelChanged(BatteryLevel batteryLevel)\r
126 {\r
127 }\r
128 \r
129 result\r
130 WebViewer::CreateWebForm(void)\r
131 {\r
132         result r = E_SUCCESS;\r
133         Rectangle bound;\r
134         WebSetting setting;\r
135 \r
136         Header* pHeader;\r
137         Footer* pFooter;\r
138 \r
139         FooterItem  footerItem1;\r
140         FooterItem  footerItem2;\r
141         IAppFrame* pAppFrame = GetAppFrame();\r
142 \r
143         AppResource *pAppResource = Application::GetInstance()->GetAppResource();\r
144         Bitmap *pBitmap1 = pAppResource->GetBitmapN("/goto.png");\r
145         Bitmap *pBitmap2 = pAppResource->GetBitmapN("/goto_press.png");\r
146 \r
147         if (NULL == pAppFrame)\r
148         {\r
149                 AppLog("GetAppFrame() has failed.\n");\r
150                 goto CATCH;\r
151         }\r
152 \r
153         /*form*/\r
154         __pMainForm = new Form();\r
155         r = __pMainForm->Construct(FORM_STYLE_NORMAL|FORM_STYLE_INDICATOR|FORM_STYLE_HEADER|FORM_STYLE_FOOTER);\r
156         TryCatch(r == E_SUCCESS, , "Form Construction() has failed. \n ");\r
157 \r
158         __pMainForm->AddOrientationEventListener(*this);\r
159         __pMainForm->SetOrientation(ORIENTATION_AUTOMATIC);\r
160 \r
161         bound = __pMainForm->GetClientAreaBounds();\r
162 \r
163         pAppFrame->GetFrame()->AddControl(*__pMainForm);\r
164         pAppFrame->GetFrame()->SetCurrentForm(*__pMainForm);\r
165 \r
166         /*edit field*/\r
167         __pEdit = new EditField();\r
168         r = __pEdit->Construct(Rectangle(5, 0, bound.width - 72, 63), EDIT_FIELD_STYLE_URL_SMALL, INPUT_STYLE_FULLSCREEN, false, WEB_URL_EDIT_FIELD_MAX_SIZE /*GROUP_STYLE_NONE*/);\r
169         TryCatch(r == E_SUCCESS, ,"Editfield Construction() has failed.\n ");\r
170         __pEdit->AddTextEventListener(*this);\r
171         __pEdit->SetText(Osp::Base::String("http://www.tizen.org"));\r
172 \r
173         r = __pMainForm->AddControl(*__pEdit);\r
174         TryCatch(r == E_SUCCESS, ,"Addcontrol() has failed.\n ");\r
175 \r
176         /*header*/\r
177         pHeader = __pMainForm->GetHeader();\r
178         pHeader->SetStyle(HEADER_STYLE_TITLE);\r
179         pHeader->SetTitleText(L"Web Viewer");\r
180 \r
181         /*footer*/\r
182         pFooter = __pMainForm->GetFooter();\r
183         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT );\r
184         pFooter->SetBackButton();\r
185         pFooter->AddActionEventListener(*this);\r
186         __pMainForm->SetFormBackEventListener(this);\r
187 \r
188         /*footer item*/\r
189         footerItem1.Construct(AC_BUTTON_WEB_BACK);\r
190         footerItem1.SetText(L"Back");\r
191 \r
192         footerItem2.Construct(AC_BUTTON_WEB_FORWARD);\r
193         footerItem2.SetText(L"Forward");\r
194 \r
195         pFooter->AddItem(footerItem1);\r
196         pFooter->AddItem(footerItem2);\r
197 \r
198         /*button*/\r
199         __pButton = new Button();\r
200         r = __pButton->Construct(Rectangle(bound.width - 67,0,67,63),"");\r
201 \r
202         __pMainForm->SetBackgroundColor(pFooter->GetColor());\r
203 \r
204         __pButton->SetColor(BUTTON_STATUS_NORMAL ,pFooter->GetColor());\r
205         __pButton->SetColor(BUTTON_STATUS_PRESSED ,pFooter->GetColor());\r
206 \r
207         __pButton->SetActionId(AC_BUTTON_WEB_GOTO);\r
208         __pButton->SetNormalBackgroundBitmap(*pBitmap1);\r
209         __pButton->SetPressedBackgroundBitmap(*pBitmap2);\r
210         __pButton->AddActionEventListener(*this);\r
211 \r
212         r = __pMainForm->AddControl(*__pButton);\r
213         TryCatch(r == E_SUCCESS, ,"Addcontrol() has failed.\n ");\r
214 \r
215         /*Web*/\r
216         __pWeb = new Web();\r
217 \r
218         bound = __pMainForm->GetClientAreaBounds();\r
219         r = __pWeb->Construct(Rectangle(0,66, bound.width, bound.height - 66));\r
220         TryCatch(r == E_SUCCESS, ,"Web Construct() has failed.\n ");\r
221 \r
222         r = __pMainForm->AddControl(*__pWeb);\r
223         TryCatch(r == E_SUCCESS, ,"Addcontrol() has failed.\n ");\r
224 \r
225         setting.SetInputStyle(INPUT_STYLE_OVERLAY);\r
226         r = __pWeb->SetSetting(setting);\r
227         TryCatch(r == E_SUCCESS, ,"SetSetting() has failed.\n ");\r
228 \r
229         __pWeb->SetLoadingListener(this);\r
230         __pWeb->SetWebKeypadEventListener(this);\r
231         __pWeb->SetWebUiEventListener(this);\r
232 \r
233         __pWeb->SetFocus();\r
234 \r
235         delete pBitmap1;\r
236         delete pBitmap2;\r
237 \r
238         return r;\r
239 \r
240 CATCH:\r
241         AppLog("Error = %s\n", GetErrorMessage(r));\r
242 \r
243         delete pBitmap1;\r
244         delete pBitmap2;\r
245 \r
246         return r;\r
247 }\r
248 \r
249 void\r
250 WebViewer::OnActionPerformed(const Control& source, int actionId)\r
251 {\r
252         String url;\r
253         switch(actionId)\r
254         {\r
255                 case AC_BUTTON_WEB_BACK:\r
256                         __pWeb->GoBack();\r
257                         break;\r
258                 case AC_BUTTON_WEB_FORWARD:\r
259                         __pWeb->GoForward();\r
260                         break;\r
261                 case AC_BUTTON_WEB_GOTO :\r
262                         url = __pEdit->GetText();\r
263                         __pWeb->LoadUrl(GetValidUrl(url));\r
264                         __pMainForm->RequestRedraw (true);\r
265                         break;\r
266                 case AC_BUTTON_WEB_STOP :\r
267                         __pWeb->StopLoading();\r
268                         break;\r
269                 default:\r
270                         break;\r
271         }\r
272 \r
273         return;\r
274 \r
275 }\r
276 \r
277 void\r
278 WebViewer::OnOrientationChanged(const Control &source, OrientationStatus orientationStatus)\r
279 {\r
280         Rectangle bound = __pMainForm->GetClientAreaBounds();\r
281 \r
282         __pEdit->SetBounds(Rectangle(5, 0, bound.width - 72, 63));\r
283         __pButton->SetBounds(Rectangle(bound.width - 67,0,67,63));\r
284         \r
285         if(__pWeb->IsVisible())\r
286                 __pWeb->SetBounds(Rectangle(0,66, bound.width, bound.height - 66));\r
287 \r
288         __pMainForm->Draw();\r
289 }\r
290 \r
291 void\r
292 WebViewer::OnTextValueChangeCanceled  ( const Osp::Ui::Control &  source   )\r
293 {\r
294         __pMainForm->SetFocus();\r
295 }\r
296 \r
297 void\r
298 WebViewer::OnTextValueChanged  ( const Osp::Ui::Control &  source   )\r
299 {\r
300         __pMainForm->SetFocus();\r
301 }\r
302 \r
303 void\r
304 WebViewer::OnScreenOn (void)\r
305 {\r
306         // TODO:\r
307         // Get the released resources or resume the operations that were paused or stopped in OnScreenOff().\r
308 }\r
309 \r
310 void\r
311 WebViewer::OnScreenOff (void)\r
312 {\r
313         // TODO:\r
314         //  Unless there is a strong reason to do otherwise, release resources (such as 3D, media, and sensors) to allow the device to enter the sleep mode to save the battery.\r
315         // Invoking a lengthy asynchronous method within this listener method can be risky, because it is not guaranteed to invoke a callback before the device enters the sleep mode.\r
316         // Similarly, do not perform lengthy operations in this listener method. Any operation must be a quick one.\r
317 }\r
318 \r
319 String\r
320 WebViewer::GetValidUrl(Osp::Base::String& url)\r
321 {\r
322         Uri uri;\r
323         String absoluteUrl = url;\r
324 \r
325         uri.SetUri(url);\r
326         if (uri.GetScheme() == L"")\r
327         {\r
328                 absoluteUrl.Insert(WEB_URL_PREFIX_HTTP, 0);\r
329         }\r
330         return absoluteUrl;\r
331 }\r
332 \r
333 bool\r
334 WebViewer::CheckURLType(const Osp::Base::String& url)\r
335 {\r
336         result r = E_SUCCESS;\r
337         int index_pos = -1;\r
338 \r
339         r = url.IndexOf(':',0, index_pos);\r
340         if(r == E_SUCCESS)\r
341                 return true;\r
342         else\r
343                 return false;\r
344 }\r
345 \r
346 bool\r
347 WebViewer::OnHttpAuthenticationRequestedN(const Osp::Base::String& host, const Osp::Base::String& realm, const Osp::Web::Controls::AuthenticationChallenge& authentication)\r
348 {\r
349         return false;\r
350 }\r
351 \r
352 void\r
353 WebViewer::OnHttpAuthenticationCanceled()\r
354 {\r
355 \r
356 }\r
357 \r
358 void\r
359 WebViewer::OnLoadingStarted(void)\r
360 {\r
361         SetButtonCancelImage();\r
362 }\r
363 \r
364 void\r
365 WebViewer::OnLoadingCanceled()\r
366 {\r
367         SetButtonGotoImage();\r
368 }\r
369 \r
370 void\r
371 WebViewer::OnLoadingErrorOccurred(LoadingErrorType error, const String& reason)\r
372 {\r
373         Osp::Ui::Controls::MessageBox msgBox;\r
374         int modalresult = 0;\r
375         Osp::Base::String errReason;\r
376 \r
377         switch(error)\r
378         {\r
379                 case WEB_REQUEST_TIMEOUT:\r
380                         errReason = Osp::Base::String(L"Request timeout");\r
381                         break;\r
382                 case WEB_NO_CONNECTION:\r
383                         errReason = Osp::Base::String(L"Network is not in service");\r
384                         break;\r
385                 case WEB_MIME_NOT_SUPPORTED:\r
386                         errReason = Osp::Base::String(L"The content type is not supported");\r
387                         break;\r
388                 case WEB_BAD_URL:\r
389                         errReason = Osp::Base::String(L"The url is invalid");\r
390                         break;\r
391                 case WEB_HTTP_RESPONSE:\r
392                         errReason = Osp::Base::String(L"HTTP response");\r
393                         break;\r
394                 case WEB_OUT_OF_MEMORY:\r
395                         errReason = Osp::Base::String(L"Page Too Large");\r
396                         break;\r
397                 case WEB_REQUEST_MAX_EXCEEDED:\r
398                         errReason = Osp::Base::String(L"Request Max Exceeded");\r
399                         break;\r
400                 case WEB_ERROR_UNKNOWN:\r
401                 default:\r
402                         errReason = Osp::Base::String(L"An Unknown error");\r
403                         break;\r
404         }\r
405 \r
406         msgBox.Construct(L"LOADING ERROR TYPE", errReason, MSGBOX_STYLE_NONE, 3000);\r
407         // Call ShowAndWait - draw, show itself and process events\r
408         msgBox.ShowAndWait(modalresult);\r
409         SetButtonGotoImage();\r
410 }\r
411 \r
412 void \r
413 WebViewer::OnLoadingCompleted()\r
414 {\r
415         SetButtonGotoImage();\r
416 }\r
417 \r
418 void\r
419 WebViewer::OnEstimatedProgress(int progress)\r
420 {\r
421 \r
422 }\r
423 \r
424 void\r
425 WebViewer::OnPageTitleReceived(const Osp::Base::String& title)\r
426 {\r
427 \r
428 }\r
429 \r
430 bool\r
431 WebViewer::OnLoadingRequested(const Osp::Base::String& url, WebNavigationType type)\r
432 {\r
433         \r
434         \r
435         return false;\r
436 }\r
437 \r
438 DecisionPolicy \r
439 WebViewer::OnWebDataReceived(const Osp::Base::String& mime, const Osp::Net::Http::HttpHeader& header)\r
440 {\r
441         return WEB_DECISION_CONTINUE;\r
442 }\r
443 \r
444 void\r
445 WebViewer::OnFormBackRequested(Osp::Ui::Controls::Form& source)\r
446 {\r
447         AppLog("OnFormBackRequested.");\r
448         Terminate();\r
449 }\r
450 \r
451 void \r
452 WebViewer::OnWebPageBlockSelected(Osp::Web::Controls::Web& source, Osp::Graphics::Point& startPoint, Osp::Graphics::Point& endPoint)\r
453 {\r
454 \r
455 }\r
456 \r
457 void\r
458 WebViewer::OnWebWindowCloseRequested(Osp::Web::Controls::Web& source)\r
459 {\r
460 \r
461 }\r
462 \r
463 Osp::Web::Controls::Web*\r
464 WebViewer::OnWebWindowCreateRequested()\r
465 {\r
466         return null;\r
467 }\r
468 \r
469 void\r
470 WebViewer::OnWebPageShowRequested(Osp::Web::Controls::Web& source)\r
471 {\r
472         AppLog("OnWebPageShowRequested");\r
473         result r = E_SUCCESS;\r
474 \r
475         r = __pEdit->SetText(__pWeb->GetUrl());\r
476         TryReturn(r == E_SUCCESS, , "Fail to update URL in the EditField.");\r
477 \r
478         __pMainForm->RequestRedraw (true);\r
479 }\r
480 \r
481 void\r
482 WebViewer::OnWebKeypadWillOpen(Osp::Web::Controls::Web& source)\r
483 {\r
484         Rectangle bound = __pMainForm->GetClientAreaBounds();\r
485         AppLog("OnWebKeypadWillOpen.");\r
486 \r
487         if(source.IsVisible())\r
488                 source.SetSize(bound.width, bound.height-66);\r
489 \r
490 }\r
491 \r
492 void\r
493 WebViewer::OnWebKeypadOpened(Osp::Web::Controls::Web& source)\r
494 {\r
495         AppLog("OnWebKeypadOpened.");\r
496 }\r
497 \r
498 \r
499 void\r
500 WebViewer::OnWebKeypadClosed(Osp::Web::Controls::Web& source)\r
501 {\r
502         AppLog("OnWebKeypadClosed.");\r
503         Rectangle bound = __pMainForm->GetClientAreaBounds();\r
504         source.SetSize(bound.width, bound.height-66);\r
505 }\r
506 \r
507 void\r
508 WebViewer::SetButtonGotoImage()\r
509 {\r
510         AppResource *pAppResource = Application::GetInstance()->GetAppResource();\r
511         Bitmap *pBitmap1 = pAppResource->GetBitmapN("/goto.png");\r
512         Bitmap *pBitmap2 = pAppResource->GetBitmapN("/goto_press.png");\r
513 \r
514         __pButton->SetNormalBackgroundBitmap(*pBitmap1);\r
515         __pButton->SetPressedBackgroundBitmap(*pBitmap2);\r
516         __pButton->SetActionId(AC_BUTTON_WEB_GOTO);\r
517 \r
518         __pButton->Draw();\r
519 \r
520         delete pBitmap1;\r
521         delete pBitmap2;\r
522 }\r
523 \r
524 void\r
525 WebViewer::SetButtonCancelImage()\r
526 {\r
527         AppResource *pAppResource = Application::GetInstance()->GetAppResource();\r
528         Bitmap *pBitmap1 = pAppResource->GetBitmapN("/cancel.png");\r
529         Bitmap *pBitmap2 = pAppResource->GetBitmapN("/cancel_press.png");\r
530 \r
531         __pButton->SetNormalBackgroundBitmap(*pBitmap1);\r
532         __pButton->SetPressedBackgroundBitmap(*pBitmap2);\r
533         __pButton->SetActionId(AC_BUTTON_WEB_STOP);\r
534 \r
535         __pButton->Draw();\r
536 \r
537         delete pBitmap1;\r
538         delete pBitmap2;\r
539 }\r