fix build break
[apps/core/preloaded/email.git] / viewer / src / email-viewer-webview.c
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 #include "email-viewer-webview.h"
18 #include "email-viewer-js.h"
19
20 #include "email-debug.h"
21 #include "email-engine.h"
22 #include "email-html-converter.h"
23
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);
31
32 /* Double_Scroller */
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);
38
39 extern EmailViewerUGD *_g_ug_data;
40 extern ui_gadget_h _g_mailbox_ug;
41
42 Evas_Object *viewer_get_webview(EmailViewerUGD *ug_data, gboolean has_html)
43 {
44         debug_log("");
45
46         Evas_Object *ewk_view = ewk_view_add(evas_object_evas_get(ug_data->win_main/*layout_main*/));
47         ug_data->webview = ewk_view;
48
49         Ewk_Setting *setting = ewk_view_setting_get(ewk_view);
50
51         ewk_setting_auto_fitting_set(setting, EINA_TRUE);
52         ewk_setting_enable_text_zoom_set(setting, EINA_FALSE);
53
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!");
56         }
57
58         /* Double_Scroller */
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);
64
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);
71
72         evas_object_show(ug_data->webview);
73
74         return ug_data->webview;
75 }
76
77 void viewer_set_webview_content(EmailViewerUGD *ug_data, int reload)
78 {
79         debug_log("");
80
81         if (!ug_data) {
82                 debug_log("ug_data is NULL");
83                 return;
84         }
85
86         if (ug_data->webview_data == NULL) {
87                 debug_log("webview_data is NULL");
88                 return;
89         }
90
91         EmailViewerWebview *wvd = ug_data->webview_data;
92
93         if (reload && (wvd->body_type == wvd->body_type_prev)) {
94                 debug_log("reload current uri");
95                 ewk_view_reload(ug_data->webview);
96                 return;
97         }
98
99         char tmp_file_path[MAX_STR_LEN] = { 0, };
100         char *tmp_file_name = NULL;
101
102         /* set content */
103         if (wvd->body_type == BODY_TYPE_HTML) {
104                 if (wvd->uri == NULL) {
105                         debug_log("invalid uri");
106                         return;
107                 }
108
109                 snprintf(tmp_file_path, sizeof(tmp_file_path), "file://%s", wvd->uri);
110                 debug_log("file://%s", wvd->uri);
111
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);
116                 if (!status) {
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);
121                                 return;
122                         }
123                 }
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);
127
128                 snprintf(tmp_file_path, sizeof(tmp_file_path), "file://%s", tmp_file_name);
129                 debug_log("file://%s", tmp_file_name);
130         }
131
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);
137 }
138
139 char *viewer_set_html_content_file(EmailViewerWebview *wvd)
140 {
141         debug_log("");
142
143         FILE *fp;
144         char *html;
145         char *tmp_file_name;
146         char *email_content = "\0";
147
148         tmp_file_name = EMAIL_TMP_FILE_PATH ".html";
149         unlink(tmp_file_name);  /*remove previous file*/
150
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");
155         if (fp != NULL) {
156                 fputs(html, fp);
157                 fclose(fp);
158         }
159
160         if (html)
161                 g_free(html);
162
163         return tmp_file_name;
164 }
165
166 void viewer_coords_ewk_to_evas(Evas_Object *view, int ewkX, int ewkY, int *evasX, int *evasY)
167 {
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);
171
172         *evasX = ewkX - scrollX + viewX;
173         *evasY = ewkY - scrollY + viewY;
174 }
175
176 void viewer_coords_evas_to_ewk(Evas_Object *view, int evasX, int evasY, int *ewkX, int *ewkY)
177 {
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);
183
184         *ewkX = evasX + scrollX - viewX;
185         *ewkY = evasY + scrollY - viewY;
186 }
187
188 Eina_Bool viewer_send_message(void *data)
189 {
190         debug_log("");
191
192         EmailViewerUGD *ug_data = (EmailViewerUGD *)data;
193
194         if (ug_data->timer) {
195                 ecore_timer_del(ug_data->timer);
196                 ug_data->timer = NULL;
197         }
198
199         int ret;
200         service_h service = NULL;
201
202         ret = service_create(&service);
203         debug_log("service_create: %d", ret);
204         if (!service) {
205                 debug_log("service create failed");
206                 return EINA_FALSE;
207         }
208
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);
211
212         ug_send_message(_g_mailbox_ug, service);
213
214         ret = service_destroy(service);
215         debug_log("service_destroy: %d", ret);
216
217         return EINA_FALSE;
218 }
219
220 static void _webview_script_executed_cb(Evas_Object *o, const char *result, void *data)
221 {
222         debug_log("");
223 }
224
225 static void _webview_get_html_content_cb(Evas_Object *o, const char *result, void *data)
226 {
227         debug_log("");
228
229 //      EmailViewerUGD *ug_data = (EmailViewerUGD *)data;
230
231 //      ug_data->saved_html_content = g_strdup(result);
232 //      debug_log("ug_data->saved_html_content => %s", ug_data->saved_html_content);
233 }
234
235 static Eina_Bool _loading_prog_finish_cb(void *data)
236 {
237         debug_log("");
238
239         EmailViewerUGD *ug_data = (EmailViewerUGD *)data;
240
241         if (ug_data->timeout_popup) {
242                 evas_object_del(ug_data->timeout_popup);
243                 ug_data->timeout_popup = NULL;
244         }
245
246         if (ug_data->waiting_timer) {
247                 ecore_timer_del(ug_data->waiting_timer);
248                 ug_data->waiting_timer = NULL;
249         }
250
251         return EINA_FALSE;
252 }
253
254 static void _webview_load_finished_cb(void *data, Evas_Object *obj, void *event_info)
255 {
256         debug_log("");
257         if (_g_ug_data == NULL) {
258                 debug_log("_g_ug_data == NULL");
259                 return;
260         }
261
262         EmailViewerUGD *ug_data = (EmailViewerUGD *)data;
263
264         ug_data->b_load_finished = EINA_TRUE;
265
266         /*_measure_webview_xy(ug_data);*/
267
268         if (ug_data->timer) {
269                 ecore_timer_del(ug_data->timer);
270                 ug_data->timer = NULL;
271         }
272         ug_data->timer = ecore_timer_add(0.0, viewer_send_message, ug_data);
273
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.");
276
277         double scale, t_scale, ratio, mins, maxs;
278         int w, h;
279         scale = ewk_view_scale_get(ug_data->webview);
280         debug_log("scale is %f", scale);
281
282         t_scale = ewk_view_text_zoom_get(ug_data->webview);
283         debug_log("t_scale is %f", t_scale);
284
285         ewk_view_text_zoom_set(ug_data->webview, 2.3f/scale);
286
287         ewk_view_contents_size_get(ug_data->webview, &w, &h);
288         debug_log("contents width : %d, heigth : %d", w, h);
289
290         ewk_view_scroll_size_get(ug_data->webview, &w, &h);
291         debug_log("scroll width : %d, heigth : %d", w, h);
292
293         ewk_view_scroll_pos_get(ug_data->webview, &w, &h);
294         debug_log("scroll x : %d, y : %d", w, h);
295
296         ratio = ewk_view_device_pixel_ratio_get(ug_data->webview);
297         debug_log("ratio is %f", ratio);
298
299         ewk_view_scale_range_get(ug_data->webview, &mins, &maxs);
300         debug_log("mins : %f , maxs : %f", mins, maxs);
301
302
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);
309                 }
310         }
311
312         if (ug_data->waiting_timer) {
313                 ecore_timer_del(ug_data->waiting_timer);
314                 ug_data->waiting_timer = NULL;
315         }
316         ug_data->waiting_timer = ecore_timer_add(0.5, _loading_prog_finish_cb, ug_data);
317 }
318
319 static void _webview_load_error_cb(void *data, Evas_Object *obj, void *event_info)
320 {
321         debug_log("");
322 }
323
324 static void _webview_load_committed_cb(void *data, Evas_Object *obj, void *event_info)
325 {
326         debug_log("");
327 }
328
329 static void _webview_load_noemptylayout_finished_cb(void *data, Evas_Object *obj, void *event_info)
330 {
331         debug_log("");
332 }
333
334 static void _webview_load_progress_cb(void *data, Evas_Object *obj, void *event_info)
335 {
336         debug_log("");
337 }
338
339 /* Double_Scroller */
340 static void _webview_edge_top_cb(void *data, Evas_Object *obj, void *event_info)
341 {
342         debug_log("");
343         if (_g_ug_data == NULL) {
344                 debug_log("_g_ug_data == NULL");
345                 return;
346         }
347
348         EmailViewerUGD *ug_data = (EmailViewerUGD *)data;
349
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
353
354                 if (elm_widget_scroll_freeze_get(ug_data->scroller) > 0)
355                         elm_object_scroll_freeze_pop(ug_data->scroller);        //restart
356
357                 /* WORKAROUND. To fix scrolling problem when screen is on the bottom of webview. (Do panning to top fast) */
358                 int x, y, w, h;
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);
362
363                 ug_data->is_main_scroller_scrolling = EINA_TRUE;
364                 ug_data->is_webview_scrolling = EINA_FALSE;
365         }
366 }
367
368 static void _webview_edge_bottom_cb(void *data, Evas_Object *obj, void *event_info)
369 {
370         debug_log("");
371 }
372
373 static void _webview_scroll_down_cb(void *data, Evas_Object *obj, void *event_info)
374 {
375 //      debug_log("");
376 }
377
378 static void _webview_scroll_up_cb(void *data, Evas_Object *obj, void *event_info)
379 {
380 //      debug_log("");
381 }
382
383 static int _send_read_report_mail(EmailViewerUGD *ug_data)
384 {
385         debug_log("");
386
387         int err = 0;
388
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);
392                 return 0;
393         }
394         debug_log("output_receipt_mail_id of saved email = %d", output_receipt_mail_id);
395
396         int handle = 0;
397
398         if ((err = email_send_mail(output_receipt_mail_id, &handle)) != EMAIL_ERROR_NONE) {
399                 debug_log("   fail sending [%d]", err);
400                 return 0;
401         } else {
402                 debug_log("   finish sending");
403         }
404
405         return 1;
406 }
407
408 #if 0
409 static void _measure_webview_xy(EmailViewerUGD *ug_data)
410 {
411         debug_log("");
412
413         int a, b, c, d;
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);
422 }
423 #endif
424
425 /* EOF */