2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://www.tizenopensource.org/license
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.
17 #include "email-viewer-webview.h"
18 #include "email-viewer-js.h"
20 #include "email-debug.h"
21 #include "email-engine.h"
22 #include "email-html-converter.h"
24 static void _webview_script_executed_cb(Evas_Object *o, const char *result, void *data);
25 static void _webview_get_html_content_cb(Evas_Object *o, const char *result, void *data);
26 static void _webview_load_finished_cb(void *data, Evas_Object *obj, void *event_info);
27 static void _webview_load_error_cb(void *data, Evas_Object *obj, void *event_info);
28 static void _webview_load_committed_cb(void *data, Evas_Object *obj, void *event_info);
29 static void _webview_load_noemptylayout_finished_cb(void *data, Evas_Object *obj, void *event_info);
30 static void _webview_load_progress_cb(void *data, Evas_Object *obj, void *event_info);
33 static void _webview_edge_top_cb(void *data, Evas_Object *obj, void *event_info);
34 static void _webview_edge_bottom_cb(void *data, Evas_Object *obj, void *event_info);
35 static void _webview_scroll_down_cb(void *data, Evas_Object *obj, void *event_info);
36 static void _webview_scroll_up_cb(void *data, Evas_Object *obj, void *event_info);
37 static int _send_read_report_mail(EmailViewerUGD *ug_data);
39 extern EmailViewerUGD *_g_ug_data;
40 extern ui_gadget_h _g_mailbox_ug;
42 Evas_Object *viewer_get_webview(EmailViewerUGD *ug_data, gboolean has_html)
46 Evas_Object *ewk_view = ewk_view_add(evas_object_evas_get(ug_data->win_main/*layout_main*/));
47 ug_data->webview = ewk_view;
49 Ewk_Setting *setting = ewk_view_setting_get(ewk_view);
51 ewk_setting_auto_fitting_set(setting, EINA_TRUE);
52 ewk_setting_enable_text_zoom_set(setting, EINA_FALSE);
54 if (ewk_setting_load_remote_images_set(setting, ug_data->b_show_remote_images) == EINA_FALSE) {
55 debug_log("SET remote images is FAILED!");
59 ewk_view_vertical_panning_hold_set(ug_data->webview, EINA_TRUE);
60 evas_object_smart_callback_add(ug_data->webview, "edge,top", _webview_edge_top_cb, ug_data);
61 evas_object_smart_callback_add(ug_data->webview, "edge,bottom", _webview_edge_bottom_cb, ug_data);
62 evas_object_smart_callback_add(ug_data->webview, "scroll,down", _webview_scroll_down_cb, ug_data);
63 evas_object_smart_callback_add(ug_data->webview, "scroll,up", _webview_scroll_up_cb, ug_data);
65 /*evas_object_smart_callback_add(ug_data->webkit, "load,progress", _webview_progressed_cb, ug_data);*/
66 evas_object_smart_callback_add(ug_data->webview, "load,committed", _webview_load_committed_cb, ug_data);
67 evas_object_smart_callback_add(ug_data->webview, "load,finished", _webview_load_finished_cb, ug_data);
68 evas_object_smart_callback_add(ug_data->webview, "load,nonemptylayout,finished", _webview_load_noemptylayout_finished_cb, ug_data);
69 evas_object_smart_callback_add(ug_data->webview, "load,progress", _webview_load_progress_cb, ug_data);
70 evas_object_smart_callback_add(ug_data->webview, "load,error", _webview_load_error_cb, ug_data);
72 evas_object_show(ug_data->webview);
74 return ug_data->webview;
77 void viewer_set_webview_content(EmailViewerUGD *ug_data, int reload)
82 debug_log("ug_data is NULL");
86 if (ug_data->webview_data == NULL) {
87 debug_log("webview_data is NULL");
91 EmailViewerWebview *wvd = ug_data->webview_data;
93 if (reload && (wvd->body_type == wvd->body_type_prev)) {
94 debug_log("reload current uri");
95 ewk_view_reload(ug_data->webview);
99 char tmp_file_path[MAX_STR_LEN] = { 0, };
100 char *tmp_file_name = NULL;
103 if (wvd->body_type == BODY_TYPE_HTML) {
104 if (wvd->uri == NULL) {
105 debug_log("invalid uri");
109 snprintf(tmp_file_path, sizeof(tmp_file_path), "file://%s", wvd->uri);
110 debug_log("file://%s", wvd->uri);
112 /* Fix for issue - Sometimes html content of previous mail is shown
113 * Set the default html page if file size is 0 */
114 struct stat statbuf = { 0 };
115 int status = lstat(wvd->uri, &statbuf);
117 debug_log("Total file size: %d", (int)statbuf.st_size);
118 if ((int)statbuf.st_size == 0) {
119 debug_log("Set URI with default html");
120 ewk_view_uri_set(ug_data->webview, EMAIL_DEFAULT_HTML);
124 } else if (wvd->body_type == BODY_TYPE_TEXT) {
125 /* generate temporary html file for viewing */
126 tmp_file_name = viewer_set_html_content_file(wvd);
128 snprintf(tmp_file_path, sizeof(tmp_file_path), "file://%s", tmp_file_name);
129 debug_log("file://%s", tmp_file_name);
132 debug_log("default encoding:%s", ug_data->property->charset);
133 Ewk_Setting *setting = ewk_view_setting_get(ug_data->webview);
134 ewk_setting_default_encoding_set(setting, ug_data->property->charset);
135 // ewk_view_encoding_custom_set(ug_data->webview, ug_data->property->charset);
136 ewk_view_uri_set(ug_data->webview, tmp_file_path);
139 char *viewer_set_html_content_file(EmailViewerWebview *wvd)
146 char *email_content = "\0";
148 tmp_file_name = EMAIL_TMP_FILE_PATH ".html";
149 unlink(tmp_file_name); /*remove previous file*/
151 email_content = wvd->text_content;
152 html = email_html_converter(email_content, wvd->charset);
153 debug_log("html:%s", html);
154 fp = fopen(tmp_file_name, "w");
163 return tmp_file_name;
166 void viewer_coords_ewk_to_evas(Evas_Object *view, int ewkX, int ewkY, int *evasX, int *evasY)
168 int scrollX, scrollY, viewX, viewY;
169 ewk_view_scroll_pos_get(view, &scrollX, &scrollY);
170 evas_object_geometry_get(view, &viewX, &viewY, NULL, NULL);
172 *evasX = ewkX - scrollX + viewX;
173 *evasY = ewkY - scrollY + viewY;
176 void viewer_coords_evas_to_ewk(Evas_Object *view, int evasX, int evasY, int *ewkX, int *ewkY)
178 debug_log("evasX:%d, evasY:%d", evasX, evasY);
179 int scrollX, scrollY, viewX, viewY;
180 ewk_view_scroll_pos_get(view, &scrollX, &scrollY);
181 evas_object_geometry_get(view, &viewX, &viewY, NULL, NULL);
182 debug_log("scrollX:%d, scrollY:%d, viewX:%d, viewY:%d", scrollX, scrollY, viewX, viewY);
184 *ewkX = evasX + scrollX - viewX;
185 *ewkY = evasY + scrollY - viewY;
188 Eina_Bool viewer_send_message(void *data)
192 EmailViewerUGD *ug_data = (EmailViewerUGD *)data;
194 if (ug_data->timer) {
195 ecore_timer_del(ug_data->timer);
196 ug_data->timer = NULL;
200 service_h service = NULL;
202 ret = service_create(&service);
203 debug_log("service_create: %d", ret);
205 debug_log("service create failed");
209 ret = service_add_extra_data(service, EMAIL_BUNDLE_KEY_MSG, EMAIL_BUNDLE_KEY_UNLOCK_LIST);
210 debug_log("service_add_extra_data: %d", ret);
212 ug_send_message(_g_mailbox_ug, service);
214 ret = service_destroy(service);
215 debug_log("service_destroy: %d", ret);
220 static void _webview_script_executed_cb(Evas_Object *o, const char *result, void *data)
225 static void _webview_get_html_content_cb(Evas_Object *o, const char *result, void *data)
229 // EmailViewerUGD *ug_data = (EmailViewerUGD *)data;
231 // ug_data->saved_html_content = g_strdup(result);
232 // debug_log("ug_data->saved_html_content => %s", ug_data->saved_html_content);
235 static Eina_Bool _loading_prog_finish_cb(void *data)
239 EmailViewerUGD *ug_data = (EmailViewerUGD *)data;
241 if (ug_data->timeout_popup) {
242 evas_object_del(ug_data->timeout_popup);
243 ug_data->timeout_popup = NULL;
246 if (ug_data->waiting_timer) {
247 ecore_timer_del(ug_data->waiting_timer);
248 ug_data->waiting_timer = NULL;
254 static void _webview_load_finished_cb(void *data, Evas_Object *obj, void *event_info)
257 if (_g_ug_data == NULL) {
258 debug_log("_g_ug_data == NULL");
262 EmailViewerUGD *ug_data = (EmailViewerUGD *)data;
264 ug_data->b_load_finished = EINA_TRUE;
266 /*_measure_webview_xy(ug_data);*/
268 if (ug_data->timer) {
269 ecore_timer_del(ug_data->timer);
270 ug_data->timer = NULL;
272 ug_data->timer = ecore_timer_add(0.0, viewer_send_message, ug_data);
274 if (ewk_view_script_execute(ug_data->webview, VIEWER_JS_INSERT_IM(VIEWER_JS_FILE_PATH), _webview_script_executed_cb, 0) == EINA_FALSE)
275 debug_log("VIEWER_JS_INSERT_IM(VIEWER_JS_FILE_PATH) failed.");
277 double scale, t_scale, ratio, mins, maxs;
279 scale = ewk_view_scale_get(ug_data->webview);
280 debug_log("scale is %f", scale);
282 t_scale = ewk_view_text_zoom_get(ug_data->webview);
283 debug_log("t_scale is %f", t_scale);
285 ewk_view_text_zoom_set(ug_data->webview, 2.3f/scale);
287 ewk_view_contents_size_get(ug_data->webview, &w, &h);
288 debug_log("contents width : %d, heigth : %d", w, h);
290 ewk_view_scroll_size_get(ug_data->webview, &w, &h);
291 debug_log("scroll width : %d, heigth : %d", w, h);
293 ewk_view_scroll_pos_get(ug_data->webview, &w, &h);
294 debug_log("scroll x : %d, y : %d", w, h);
296 ratio = ewk_view_device_pixel_ratio_get(ug_data->webview);
297 debug_log("ratio is %f", ratio);
299 ewk_view_scale_range_get(ug_data->webview, &mins, &maxs);
300 debug_log("mins : %f , maxs : %f", mins, maxs);
303 debug_log("flags_seen_field:%d, request_report:%d, report_status:%d", ug_data->email_data->mail_info->flags_seen_field, ug_data->property->request_report, ug_data->email_data->mail_info->report_status & EMAIL_MAIL_REQUEST_MDN);
304 if (!ug_data->email_data->mail_info->flags_seen_field && ug_data->property->request_report == TRUE && (ug_data->email_data->mail_info->report_status & EMAIL_MAIL_REQUEST_MDN)) {
305 int send_result = _send_read_report_mail(ug_data);
306 debug_log("send_result (%d)", send_result);
307 if (send_result != 1) {
308 debug_log("Failed to send read report mail (%s)", send_result);
312 if (ug_data->waiting_timer) {
313 ecore_timer_del(ug_data->waiting_timer);
314 ug_data->waiting_timer = NULL;
316 ug_data->waiting_timer = ecore_timer_add(0.5, _loading_prog_finish_cb, ug_data);
319 static void _webview_load_error_cb(void *data, Evas_Object *obj, void *event_info)
324 static void _webview_load_committed_cb(void *data, Evas_Object *obj, void *event_info)
329 static void _webview_load_noemptylayout_finished_cb(void *data, Evas_Object *obj, void *event_info)
334 static void _webview_load_progress_cb(void *data, Evas_Object *obj, void *event_info)
339 /* Double_Scroller */
340 static void _webview_edge_top_cb(void *data, Evas_Object *obj, void *event_info)
343 if (_g_ug_data == NULL) {
344 debug_log("_g_ug_data == NULL");
348 EmailViewerUGD *ug_data = (EmailViewerUGD *)data;
350 if (ug_data->is_webview_scrolling) {
351 debug_log("main scroller start");
352 ewk_view_vertical_panning_hold_set(ug_data->webview, EINA_TRUE); //stop
354 if (elm_widget_scroll_freeze_get(ug_data->scroller) > 0)
355 elm_object_scroll_freeze_pop(ug_data->scroller); //restart
357 /* WORKAROUND. To fix scrolling problem when screen is on the bottom of webview. (Do panning to top fast) */
359 elm_scroller_region_get(ug_data->scroller, &x, &y, &w, &h);
360 debug_log("main_scroller(x:%d, y:%d, w:%d, h:%d)", x, y, w, h);
361 elm_scroller_region_bring_in(ug_data->scroller, x, 0, w, h);
363 ug_data->is_main_scroller_scrolling = EINA_TRUE;
364 ug_data->is_webview_scrolling = EINA_FALSE;
368 static void _webview_edge_bottom_cb(void *data, Evas_Object *obj, void *event_info)
373 static void _webview_scroll_down_cb(void *data, Evas_Object *obj, void *event_info)
378 static void _webview_scroll_up_cb(void *data, Evas_Object *obj, void *event_info)
383 static int _send_read_report_mail(EmailViewerUGD *ug_data)
389 int output_receipt_mail_id = 0;
390 if ((err = email_add_read_receipt(ug_data->email_data->mail_id, &output_receipt_mail_id)) != EMAIL_ERROR_NONE) {
391 debug_log(" fail sending [%d]", err);
394 debug_log("output_receipt_mail_id of saved email = %d", output_receipt_mail_id);
398 if ((err = email_send_mail(output_receipt_mail_id, &handle)) != EMAIL_ERROR_NONE) {
399 debug_log(" fail sending [%d]", err);
402 debug_log(" finish sending");
409 static void _measure_webview_xy(EmailViewerUGD *ug_data)
414 evas_object_geometry_get(ug_data->navi_bar, &a, &b, &c, &d);
415 debug_log("navi_bar x[%d] y[%d] w[%d] h[%d]", a, b, c, d);
416 evas_object_geometry_get(ug_data->sub_ly, &a, &b, &c, &d);
417 debug_log("sub_ly x[%d] y[%d] w[%d] h[%d]", a, b, c, d);
418 evas_object_geometry_get(ug_data->ctr_bar, &a, &b, &c, &d);
419 debug_log("ctr_bar x[%d] y[%d] w[%d] h[%d]", a, b, c, d);
420 evas_object_geometry_get(ug_data->webview_bx, &a, &b, &c, &d);
421 debug_log("webview_bx x[%d] y[%d] w[%d] h[%d]", a, b, c, d);