Merge "[Title] Set divided selection info layout due to rotating issue." into 2.0_beta
[profile/ivi/org.tizen.browser.git] / src / browser-main.cpp
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 extern "C" {
19 #include <ITapiSat.h>
20 #include <TapiUtility.h>
21 }
22
23 #include <app.h>
24 #include "browser-config.h"
25 #include "browser-class.h"
26
27 struct browser_data {
28         Evas_Object *main_win;
29         Evas_Object *bg;
30         Evas_Object *main_layout;
31         Evas_Object *navi_bar;
32         Elm_Theme *browser_theme;
33
34         Browser_Class *browser_instance;
35
36         Eina_Bool is_paused;
37 };
38
39 static void __br_set_env(void)
40 {
41 #if !defined(TIZEN_PUBLIC)
42         /* manual enabling of CoreGL fastpath */        
43         setenv("COREGL_FASTPATH", "1", 1);
44 #endif
45
46         /* set image cache suze */
47         if (setenv("ELM_IMAGE_CACHE", "0", 1))
48                 BROWSER_LOGD("ELM_IMAGE_CACHE is set to 1MB");
49
50         /* This is because of showing navigation bar more quickly. */
51         if (setenv("ELM_BRING_IN_SCROLL_FRICTION", "0.2", 1))
52                 BROWSER_LOGD("ELM_BRING_IN_SCROLL_FRICTION is set");
53
54         if (setenv("ELM_PAGE_SCROLL_FRICTION", "0.4", 1))
55                 BROWSER_LOGD("ELM_PAGE_SCROLL_FRICTION is set");
56
57         if (setenv("ELM_THUMBSCROLL_BOUNCE_FRICTION", "0.2", 1))
58                 BROWSER_LOGD("ELM_THUMBSCROLL_BOUNCE_FRICTION is set");
59 }
60
61 static void __br_destroy(void *data)
62 {
63         BROWSER_LOGD("[%s]", __func__);
64         elm_exit();
65 }
66
67 static void __main_win_del_cb(void *data, Evas_Object *obj, void *event)
68 {
69         BROWSER_LOGD("[Browser-Launching time measure]<< window delete callback >>");
70 }
71
72 static bool __init_preference()
73 {
74         BROWSER_LOGD("[%s]", __func__);
75
76         if (!br_preference_create_bool(SHOW_MY_SITES_GUIDE, true))
77                 return false;
78         if (!br_preference_create_str(LAST_VISITED_URL_KEY, ""))
79                 return false;
80 #if defined(FEATURE_MOST_VISITED_SITES)
81         if (!br_preference_create_str(HOMEPAGE_KEY, MOST_VISITED_SITES))
82                 return false;
83 #else
84         if (!br_preference_create_str(HOMEPAGE_KEY, USER_HOMEPAGE))
85                 return false;
86 #endif
87         if (!br_preference_create_str(USER_HOMEPAGE_KEY, BROWSER_DEFAULT_USER_HOMEPAGE))
88                 return false;
89         if (!br_preference_create_str(SEARCHURL_KEY, "http://search.yahoo.com/search?p="))
90                 return false;
91         if (!br_preference_create_str(DEFAULT_VIEW_LEVEL_KEY, "READABLE"))
92                 return false;
93         if (!br_preference_create_bool(RUN_JAVASCRIPT_KEY, true))
94                 return false;
95         if (!br_preference_create_bool(DISPLAY_IMAGES_KEY, true))
96                 return false;
97         if (!br_preference_create_bool(BLOCK_POPUP_KEY, true))
98                 return false;
99         if (!br_preference_create_bool(SHOW_SECURITY_WARNINGS_KEY, true))
100                 return false;
101         if (!br_preference_create_bool(ACCEPT_COOKIES_KEY, true))
102                 return false;
103         if (!br_preference_create_bool(AUTO_SAVE_ID_PASSWORD_KEY, false))
104                 return false;
105         if (!br_preference_create_bool(AUTO_SAVE_FORM_DATA_KEY, false))
106                 return false;
107         if (!br_preference_create_bool(ENABLE_LOCATION_KEY, true))
108                 return false;
109 #ifdef ZOOM_BUTTON
110         if (!br_preference_create_bool(ZOOM_BUTTON_KEY, true))
111                 return false;
112 #endif
113         return true;
114 }
115
116 static Evas_Object *__create_main_win(void *app_data)
117 {
118         Evas_Object *window = elm_win_add(NULL, BROWSER_PACKAGE_NAME, ELM_WIN_BASIC);
119         if (window) {
120                 int w;
121                 int h;
122                 elm_win_title_set(window, BROWSER_PACKAGE_NAME);
123                 elm_win_borderless_set(window, EINA_TRUE);
124                 elm_win_conformant_set(window, EINA_TRUE);
125                 evas_object_smart_callback_add(window, "delete,request",
126                                                 __main_win_del_cb, app_data);
127                 ecore_x_window_size_get(ecore_x_window_root_first_get(),
128                                         &w, &h);
129                 evas_object_resize(window, w, h);
130                 elm_win_indicator_mode_set(window, ELM_WIN_INDICATOR_SHOW);
131                 evas_object_show(window);
132         }
133
134         return window;
135 }
136
137 static Evas_Object *__create_bg(Evas_Object *win)
138 {
139         Evas_Object *bg;
140         bg = elm_bg_add(win);
141         if (!bg)
142                 return NULL;
143
144         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
145         elm_win_resize_object_add(win, bg);
146         evas_object_show(bg);
147
148         return bg;
149 }
150
151 static Evas_Object *__create_main_layout(Evas_Object *win)
152 {
153         Evas_Object *layout;
154         layout = elm_layout_add(win);
155         if (!layout)
156                 return NULL;
157
158         if (!elm_layout_theme_set(layout, "layout", "application", "default"))
159                 BROWSER_LOGE("elm_layout_theme_set is failed.\n");
160
161         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
162         elm_win_resize_object_add(win, layout);
163         edje_object_signal_emit(elm_layout_edje_get(layout), "elm,state,show,indicator", "elm");
164         evas_object_show(layout);
165
166         return layout;
167 }
168
169 static Evas_Object *__create_navi_bar(void *app_data)
170 {
171         BROWSER_LOGD("[%s]", __func__);
172         Evas_Object *navi_bar;
173         struct browser_data *ad = (struct browser_data *)app_data;
174         navi_bar = elm_naviframe_add(ad->main_layout);
175         if (navi_bar) {
176                 elm_object_theme_set(navi_bar, ad->browser_theme);
177                 elm_object_style_set(navi_bar, "browser");
178                 elm_object_part_content_set(ad->main_layout, "elm.swallow.content", navi_bar);
179                 evas_object_show(navi_bar);
180         }
181
182         return navi_bar;
183 }
184
185 static Eina_Bool __process_app_service(service_h service, void *app_data)
186 {
187         BROWSER_LOGD("[%s]", __func__);
188         struct browser_data *ad = (struct browser_data *)app_data;
189
190         char *operation = NULL;
191         char *request_uri = NULL;
192         char *request_mime_type = NULL;
193
194         if (service_get_operation(service, &operation) != SERVICE_ERROR_NONE) {
195                 BROWSER_LOGD("get service operation failed");
196                 return EINA_FALSE;
197         }
198
199         if (service_get_uri(service, &request_uri) != SERVICE_ERROR_NONE) {
200                 BROWSER_LOGD("get service uri failed");
201         }
202
203         if (service_get_mime(service, &request_mime_type) != SERVICE_ERROR_NONE) {
204                 BROWSER_LOGD("get service mime failed");
205         }
206
207         if (!operation && !request_uri && !request_mime_type) {
208                 BROWSER_LOGD("Not app svc");
209                 return EINA_FALSE;
210         }
211
212         BROWSER_LOGD("operation[%s], request_uri[%s], request_mime_type[%s]", operation, request_uri, request_mime_type);
213
214         std::string full_path;
215
216         if (request_mime_type) {
217                 if (!strncmp(request_mime_type, "application/x-shockwave-flash", strlen("application/x-shockwave-flash"))
218                         || !strncmp(request_mime_type, "image/svg+xml", strlen("image/svg+xml"))
219                         || !strncmp(request_mime_type, "text/html", strlen("text/html"))
220                         || !strncmp(request_mime_type, "application/xml", strlen("application/xml"))) {
221                         if (request_uri)
222                                 full_path = std::string(request_uri);
223                 } else {
224                         BROWSER_LOGD("Not allowed mime type : [%s]", request_mime_type);
225                         return EINA_FALSE;
226                 }
227         } else if (request_uri) {
228                 full_path = std::string(request_uri);
229         }
230
231         BROWSER_LOGD("url=[%s]", full_path.c_str());
232
233         if (ad->main_win)
234                 elm_win_activate(ad->main_win);
235
236         ad->browser_instance->launch(full_path.c_str(), EINA_TRUE);
237
238         return EINA_TRUE;
239 }
240
241 static void __br_load_url(service_h service, void *app_data)
242 {
243         BROWSER_LOGD("[%s]", __func__);
244         struct browser_data *ad = (struct browser_data *)app_data;
245
246         if (__process_app_service(service, app_data)) {
247                 BROWSER_LOGD("app service");
248                 return;
249         }
250
251         std::string full_path;
252         char *mime_type = NULL;
253         char *search_keyword = NULL;
254         char *goto_url = NULL;
255         char *url = NULL;
256
257         if (service_get_uri(service, &url) != SERVICE_ERROR_NONE) {
258                 BROWSER_LOGD("get service uri failed");
259         }
260
261         if (service_get_mime(service, &mime_type) != SERVICE_ERROR_NONE) {
262                 BROWSER_LOGD("get service mime failed");
263         }
264
265         if (service_get_extra_data(service, "search_keyword", &search_keyword) != SERVICE_ERROR_NONE) {
266                 BROWSER_LOGD("get service extra data(search keyword) failed");
267         }
268
269         if (service_get_extra_data(service, "goto_url", &search_keyword) != SERVICE_ERROR_NONE) {
270                 BROWSER_LOGD("get service extra data(search keyword) failed");
271         }
272
273         if (mime_type && url) {
274                 BROWSER_LOGD("mime type=[%s], url=[%s]", mime_type, url);
275                 if (!strcmp(mime_type, "application/x-shockwave-flash")
276                         || !strcmp(mime_type, "image/svg+xml")
277                         || !strcmp(mime_type, "text/html")
278                         || !strcmp(mime_type, "application/xml")) {
279                         full_path = std::string(url);
280                 }
281         } else if (search_keyword) {
282                 BROWSER_LOGD("search_keyword=[%s]", search_keyword);
283                 if (search_keyword) {
284                         char *search_url = NULL;
285                         if (br_preference_get_str(SEARCHURL_KEY, &search_url) == false) {
286                                 BROWSER_LOGE("failed to get %s preference\n", SEARCHURL_KEY);
287                                 return;
288                         }
289                         if (search_url) {
290                                 full_path = std::string(search_url) + std::string(search_keyword);
291                                 free(search_url);
292                         }
293                 }
294         } else if (goto_url || url) {
295                 BROWSER_LOGD("goto_url=[%s], url=[%s]", goto_url, url);
296                 if (goto_url)
297                         full_path = goto_url;
298                 else
299                         full_path = url;
300         }
301         BROWSER_LOGD("url=[%s]", full_path.c_str());
302
303         if (ad->main_win)
304                 elm_win_activate(ad->main_win);
305
306         ad->browser_instance->launch(full_path.c_str(), EINA_TRUE);
307 }
308
309 static Eina_Bool __br_keydown_event(void *data, int type, void *event)
310 {
311         BROWSER_LOGD("[%s]", __func__);
312         Ecore_Event_Key *ev = (Ecore_Event_Key *)event;
313
314         if (!strcmp(ev->keyname, KEY_END)) {
315                 __br_destroy(data);
316         }
317
318         return EXIT_FAILURE;
319 }
320
321 #if defined(HORIZONTAL_UI)
322 static void __br_rotate_cb(app_device_orientation_e mode, void *data)
323 {
324         BROWSER_LOGD("[%s] rotation mode = %d", __func__, mode);
325         struct browser_data *ad = (struct browser_data *)data;
326         int rotation_value;
327
328         switch (mode) {
329         case APP_DEVICE_ORIENTATION_0:
330                 rotation_value = 0;
331                 ug_send_event(UG_EVENT_ROTATE_PORTRAIT);
332                 break;
333         case APP_DEVICE_ORIENTATION_90:
334                 rotation_value = 90;
335                 ug_send_event(UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN);
336                 break;
337         case APP_DEVICE_ORIENTATION_180:
338                 rotation_value = 180;
339                 ug_send_event(UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN);
340                 break;
341         case APP_DEVICE_ORIENTATION_270:
342                 rotation_value = 270;
343                 ug_send_event(UG_EVENT_ROTATE_LANDSCAPE);
344                 break;
345         default:
346                 rotation_value = -1;
347                 break;
348         }
349
350         if (rotation_value >= 0 && ad->browser_instance) {
351                 if (ad->browser_instance->is_available_to_rotate()) {
352                         elm_win_rotation_with_resize_set(ad->main_win, rotation_value);
353                         ad->browser_instance->rotate(rotation_value);
354                 }
355         }
356 }
357 #endif
358
359 static void __br_low_memory_cb(void* data)
360 {
361         BROWSER_LOGD("[%s]", __func__);
362         /* To do */
363         struct browser_data *ad = (struct browser_data *)data;
364
365         if (ad && ad->browser_instance)
366                 ad->browser_instance->clean_up_windows();
367 }
368
369 static void __br_low_battery_cb(void* data)
370 {
371         BROWSER_LOGD("[%s]", __func__);
372         /* To do */
373         return;
374 }
375
376 static void __br_lang_changed_cb(void *data)
377 {
378         BROWSER_LOGD("[%s]", __func__);
379         /* To do */
380         return;
381 }
382
383 static void __br_region_changed_cb(void *data)
384 {
385         BROWSER_LOGD("[%s]", __func__);
386         /* To do */
387         return;
388 }
389
390 static void __br_register_system_event(void *app_data)
391 {
392         /* To do */
393         struct browser_data *ad = (struct browser_data *)app_data;
394 }
395
396 static int __browser_set_i18n(const char *domain, const char *dir)
397 {
398     char *r = NULL;
399
400     if (domain == NULL) {
401                 BROWSER_LOGE("domain is NULL");
402         return -1;
403     }
404
405     r = setlocale(LC_ALL, "");
406
407     /* if locale is not set properly, try again to set as language base */
408     if (r == NULL) {
409         char *lang_set = vconf_get_str(VCONFKEY_LANGSET);
410         if (!lang_set && strlen(lang_set) > 0)
411             r = setlocale(LC_ALL, lang_set);
412
413         BROWSER_LOGE("setlocale as [%s]", r);
414         if (lang_set)
415             free(lang_set);
416     }
417
418     if (r == NULL) {
419                 BROWSER_LOGE("setlocale failed");
420         return -1;
421     }
422
423     r = bindtextdomain(domain, dir);
424     if (r == NULL) {
425                 BROWSER_LOGE("bindtextdomain failed");
426         return -1;
427     }
428
429     r = textdomain(domain);
430     if (r == NULL) {
431                 BROWSER_LOGE("textdomain failed");
432         return -1;
433     }
434
435     return 0;
436 }
437
438 static bool __br_app_create(void *app_data)
439 {
440         BROWSER_LOGD("[Browser-Launching time measure] << theme extenstion >>");
441         struct browser_data *ad = (struct browser_data *)app_data;
442
443         elm_config_preferred_engine_set("opengl_x11");
444
445         if(!__init_preference())
446                 return false;
447
448         ad->browser_theme = elm_theme_new();
449         elm_theme_ref_set(ad->browser_theme, NULL);
450         elm_theme_extension_add(ad->browser_theme, BROWSER_NAVIFRAME_THEME);
451         elm_theme_extension_add(ad->browser_theme, BROWSER_CONTROLBAR_THEME);
452         elm_theme_extension_add(ad->browser_theme, BROWSER_BUTTON_THEME);
453         elm_theme_extension_add(ad->browser_theme, BROWSER_PROGRESSBAR_THEME);
454         elm_theme_extension_add(ad->browser_theme, BROWSER_URL_LAYOUT_THEME);
455         elm_theme_extension_add(ad->browser_theme, BROWSER_PREDICTIVE_HISTORY_THEME);
456         elm_theme_extension_add(ad->browser_theme, BROWSER_SETTINGS_THEME);
457         elm_theme_extension_add(ad->browser_theme, BROWSER_BOOKMARK_THEME);
458         elm_theme_extension_add(ad->browser_theme, BROWSER_FIND_WORD_LAYOUT_THEME);
459
460         BROWSER_LOGD("[Browser-Launching time measure] << create main window >>");
461         ad->main_win = __create_main_win(ad);
462         if (!ad->main_win) {
463                 BROWSER_LOGE("fail to create window");
464                 return false;
465         }
466
467         BROWSER_LOGD("[Browser-Launching time measure] << create background >>");
468         ad->bg = __create_bg(ad->main_win);
469         if (!ad->bg) {
470                 BROWSER_LOGE("fail to create bg");
471                 return false;
472         }
473
474         BROWSER_LOGD("[Browser-Launching time measure] << create layout main >>");
475         ad->main_layout = __create_main_layout(ad->main_win);
476         if (!ad->main_layout) {
477                 BROWSER_LOGE("fail to create main layout");
478                 return false;
479         }
480
481         ad->navi_bar = __create_navi_bar(ad);
482         if (!ad->navi_bar) {
483                 BROWSER_LOGE("fail to create navi bar");
484                 return false;
485         }
486
487         /* create browser instance & init */
488         ad->browser_instance = new(nothrow) Browser_Class(ad->main_win, ad->navi_bar, ad->bg);
489         if (!ad->browser_instance) {
490                 BROWSER_LOGE("fail to Browser_Class");
491                 return false;
492         }
493         if (ad->browser_instance->init() == EINA_FALSE) {
494                 BROWSER_LOGE("fail to browser init");
495                 return false;
496         }
497
498         /* init internationalization */
499         int ret = __browser_set_i18n(BROWSER_PACKAGE_NAME, BROWSER_LOCALE_DIR);
500         if (ret) {
501                 BROWSER_LOGE("fail to __browser_set_i18n");
502                 return false;
503         }
504
505 #if defined(HORIZONTAL_UI)
506         app_device_orientation_e rotation_value = app_get_device_orientation();
507
508         if (rotation_value != APP_DEVICE_ORIENTATION_0) {
509                 elm_win_rotation_with_resize_set(ad->main_win, rotation_value);
510                 ad->browser_instance->rotate(rotation_value);
511         }
512 #endif
513
514         return true;
515 }
516
517 /* GCF test requirement */
518 static void __send_termination_event_to_tapi(void)
519 {
520         BROWSER_LOGD("[%s]", __func__);
521         int ret = TAPI_API_SUCCESS;
522         int request_id = -1;
523         TelSatEventDownloadReqInfo_t event_data;
524         event_data.eventDownloadType = TAPI_EVENT_SAT_DW_TYPE_BROWSER_TERMINATION;
525         event_data.u.browserTerminationEventReqInfo.browserTerminationCause = TAPI_SAT_BROWSER_TERMINATED_BY_USER;
526
527         TapiHandle *handle = NULL;
528         handle = tel_init(NULL);
529
530         ret = tel_download_sat_event(handle, &event_data, NULL, NULL);
531         if(ret != TAPI_API_SUCCESS && ret != TAPI_API_SAT_EVENT_NOT_REQUIRED_BY_USIM)
532                 BROWSER_LOGE("failed to tel_download_sat_event");
533
534         tel_deinit(handle);
535 }
536
537 static void __br_app_terminate(void *app_data)
538 {
539         BROWSER_LOGD("[%s]", __func__);
540         struct browser_data *ad = (struct browser_data *)app_data;
541
542         elm_theme_extension_del(ad->browser_theme, BROWSER_NAVIFRAME_THEME);
543         elm_theme_extension_del(ad->browser_theme, BROWSER_CONTROLBAR_THEME);
544         elm_theme_extension_del(ad->browser_theme, BROWSER_BUTTON_THEME);
545         elm_theme_extension_del(ad->browser_theme, BROWSER_URL_LAYOUT_THEME);
546         elm_theme_extension_del(ad->browser_theme, BROWSER_PROGRESSBAR_THEME);
547         elm_theme_extension_del(ad->browser_theme, BROWSER_PREDICTIVE_HISTORY_THEME);
548         elm_theme_extension_del(ad->browser_theme, BROWSER_SETTINGS_THEME);
549         elm_theme_extension_del(ad->browser_theme, BROWSER_BOOKMARK_THEME);
550         elm_theme_extension_del(ad->browser_theme, BROWSER_FIND_WORD_LAYOUT_THEME);
551         elm_theme_free(ad->browser_theme);
552
553         /* GCF test requirement */
554         __send_termination_event_to_tapi();
555
556         if (ad->main_win);
557                 evas_object_del(ad->main_win);
558
559         if (ad->browser_instance)
560                 delete ad->browser_instance;
561
562         BROWSER_LOGD("[Browser-Launching time measure] << __br_app_terminate ends >>");
563 }
564
565 static void __br_app_pause(void *app_data)
566 {
567         BROWSER_LOGD("[%s]", __func__);
568         struct browser_data *ad = (struct browser_data *)app_data;
569
570         if (!ad || !ad->browser_instance)
571                 return;
572
573         ad->browser_instance->pause();
574
575         ad->is_paused = EINA_TRUE;
576 }
577
578 static void __br_app_resume(void *app_data)
579 {
580         BROWSER_LOGD("[%s]", __func__);
581         struct browser_data *ad = (struct browser_data *)app_data;
582
583         if (!ad || !ad->browser_instance)
584                 return;
585
586         ad->browser_instance->resume();
587
588         ad->is_paused = EINA_FALSE;
589 }
590
591 static void __br_app_reset(service_h service, void *app_data)
592 {
593         struct browser_data *ad = (struct browser_data *)app_data;
594         BROWSER_LOGD("[%s]", __func__);
595
596         ad->browser_instance->reset();
597
598         __br_load_url(service, app_data);
599
600         ad->is_paused = EINA_FALSE;
601 }
602
603 int main(int argc, char *argv[])
604 {
605         __br_set_env();
606
607         app_event_callback_s ops;
608         memset(&ops, 0x0, sizeof(app_event_callback_s));
609
610         ops.create = __br_app_create;
611         ops.terminate = __br_app_terminate;
612         ops.pause = __br_app_pause;
613         ops.resume = __br_app_resume;
614         ops.service = __br_app_reset;
615         ops.low_memory = __br_low_memory_cb;
616         ops.low_battery = __br_low_battery_cb;
617 #if defined(HORIZONTAL_UI)
618         ops.device_orientation = __br_rotate_cb;
619 #endif
620         ops.language_changed = __br_lang_changed_cb;
621         ops.region_format_changed = __br_region_changed_cb;
622
623         struct browser_data ad;
624         memset(&ad, 0x0, sizeof(struct browser_data));
625
626         int ret = app_efl_main(&argc, &argv, &ops, &ad);
627
628         return ret;
629 }
630