c74a79322d508f0d72f01a1549cb8199ad0606d0
[apps/core/preloaded/email.git] / composer / src / email-composer.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 #ifndef UG_MODULE_API
18 #define UG_MODULE_API __attribute__ ((visibility("default")))
19 #endif
20
21 #include "email-composer.h"
22 #include "email-composer-util.h"
23 #include "email-composer-attachment.h"
24 #include "email-composer-recipient.h"
25 #include "email-composer-contents.h"
26 #include "email-composer-callback.h"
27 #include "email-composer-predictive-search.h"
28 #include "email-composer-js.h"
29
30 #include <sys/vfs.h>
31 #include "Ecore_X.h"
32
33 static void *on_create(ui_gadget_h ug, enum ug_mode mode, service_h data, void *priv);
34 static void on_start(ui_gadget_h ug, service_h data, void *priv);
35 static void on_pause(ui_gadget_h ug, service_h data, void *priv);
36 static void on_resume(ui_gadget_h ug, service_h data, void *priv);
37 static void on_destroy(ui_gadget_h ug, service_h data, void *priv);
38 static void on_message(ui_gadget_h ug, service_h msg, service_h data, void *priv);
39 static void on_event(ui_gadget_h ug, enum ug_event event, service_h data, void *priv);
40
41 static Evas_Object *create_fullview(Evas_Object *parent, EmailComposerUGD *ugd);
42 static Evas_Object *create_frameview(Evas_Object *parent, EmailComposerUGD *ugd);
43 static int _composer_check_storage_full(char *path, int mb_size);
44
45 static void _composer_delete_evas_objects(EmailComposerUGD *ugd);
46 static void _composer_delete_all_popup(EmailComposerUGD *ugd);
47 static void _composer_free_email_info(EmailComposerUGD *ugd);
48 static Eina_Bool _composer_launch_email_app_cb(void *data);
49 static void _composer_popup_warning(EmailComposerUGD *ugd, char *header, char *content);
50 static void _composer_ug_destroy_cb(void *data, Evas_Object *obj, void *event_info);
51
52 static int _composer_pre_parse_bundle(EmailComposerUGD *ugd, service_h data);
53 static void _composer_post_parse_bundle(EmailComposerUGD *ugd, service_h data);
54 static void _composer_init_data(void *data);
55 static int _composer_init_service(void *data);
56 static void _composer_finish_service(void *data);
57 static Eina_Bool _composer_set_object_focus(void *data);
58 static Eina_Bool _composer_register_scroller_callback(void *data);
59 static void _composer_remove_temp_folder();
60
61 static void _composer_main_scroller_reach_top_cb(void *data, Evas_Object *obj, void *event_info);
62 static void _composer_main_scroller_reach_bottom_cb(void *data, Evas_Object *obj, void *event_info);
63 static void _composer_main_scroller_drag_start_cb(void *data, Evas_Object *obj, void *event_info);
64
65 static int _composer_dbus_receiver_setup(EmailComposerUGD *ugd);
66 static void _on_edbus_event_composer_receive(void *data, DBusMessage * message);
67 static void _on_edbus_remove_receiver(EmailComposerUGD *ugd);
68 static Eina_Bool _on_edbus_popup_del(void *data);
69
70 Eina_List * _composer_create_initial_recipients_list(Evas_Object *mbe);
71 void _composer_save_initial_email_content(EmailComposerUGD *ugd);
72 void _composer_free_initial_email_content(EmailComposerUGD *ugd);
73
74 E_DBus_Connection *_g_composer_dbus_conn = NULL;
75 E_DBus_Signal_Handler *_g_composer_signal_handler = NULL;
76 int attach_all_cnt;
77
78 EmailComposerUGD *g_ugd = NULL;
79
80 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
81 {
82         debug_log("");
83         EmailComposerUGD *ugd;
84
85         if (!ops)
86                 return -1;
87
88         ugd = calloc(1, sizeof(EmailComposerUGD));
89         if (!ugd)
90                 return -1;
91
92         ops->create = on_create;
93         ops->start = on_start;
94         ops->pause = on_pause;
95         ops->resume = on_resume;
96         ops->destroy = on_destroy;
97         ops->message = on_message;
98         ops->event = on_event;
99         ops->priv = ugd;
100         ops->opt = UG_OPT_INDICATOR_PORTRAIT_ONLY;      //UG_OPT_INDICATOR_ENABLE
101
102         return 0;
103 }
104
105 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
106 {
107         debug_log("IN");
108         EmailComposerUGD *ugd;
109
110         if (!ops)
111                 return;
112
113         ugd = ops->priv;
114         if (ugd)
115                 free(ugd);
116
117         debug_log("OUT");
118 }
119
120 static void *on_create(ui_gadget_h ug, enum ug_mode mode, service_h data, void *priv)
121 {
122         debug_log("");
123         Evas_Object *parent;
124         EmailComposerUGD *ugd;
125         email_address_info_list_t *addrs_info_list = NULL;
126
127         if (!ug || !priv)
128                 return NULL;
129
130         ugd = (EmailComposerUGD *)priv;
131         ugd->ug_main = ug;
132
133         bindtextdomain(PACKAGE, LOCALEDIR);
134
135         parent = ug_get_window();
136
137         if (!parent)
138                 return NULL;
139
140         ugd->win_main = parent;
141
142         if (mode == UG_MODE_FULLVIEW)
143                 ugd->main_layout = create_fullview(parent, ugd);
144         else
145                 ugd->main_layout = create_frameview(parent, ugd);
146
147         ugd->composer_type = RUN_TYPE_UNKNOWN;
148         ugd->eComposerErrorType = COMPOSER_ERROR_NONE;
149
150         if (_composer_check_storage_full(DIR_DEFAULT_EMAIL_STORAGE, MIN_FREE_SPACE)
151                 /*|| _composer_check_storage_full(DIR_TMP_PREFIX, MIN_FREE_SPACE)*/) {
152                 ugd->eComposerErrorType = COMPOSER_ERROR_STORAGE_FULL;
153                 return ugd->main_layout;
154         }
155
156         if (!email_engine_initialize()) {
157                 ugd->eComposerErrorType = COMPOSER_ERROR_ENGINE_INIT_FAILED;
158                 return ugd->main_layout;
159         }
160
161         ugd->eComposerErrorType = _composer_init_service(ugd);
162
163         if (ugd->eComposerErrorType != COMPOSER_ERROR_NONE)
164                 return ugd->main_layout;
165
166         ugd->eComposerErrorType = _composer_pre_parse_bundle(ugd, data);
167
168         if (ugd->eComposerErrorType == COMPOSER_ERROR_INVALID_ARG || ugd->eComposerErrorType == COMPOSER_ERROR_UNKOWN_TYPE) {
169                 return ugd->main_layout;
170
171         } else if (ugd->eComposerErrorType == COMPOSER_ERROR_NO_DEFAULT_ACCOUNT) {
172                 return ugd->main_layout;
173
174         } else if (ugd->eComposerErrorType == COMPOSER_ERROR_NONE) {
175
176                 debug_log("def account_id = %d", ugd->account_info->account_id);
177
178                 ugd->eComposerErrorType = _composer_get_account_list(ugd);
179
180                 if (ugd->eComposerErrorType == COMPOSER_ERROR_NO_ACCOUNT_LIST) {
181                         return ugd->main_layout;
182                 }
183
184                 ugd->has_body_html = EINA_TRUE;
185                 ugd->saved_html_path = g_strconcat(EMAIL_TMP_FOLDER, "/", SAVE_HTML_FILE_NAME, NULL);
186
187                 if (ugd->composer_type == RUN_COMPOSER_EDIT || ugd->composer_type == RUN_COMPOSER_REPLY || ugd->composer_type == RUN_COMPOSER_REPLY_ALL || ugd->composer_type == RUN_COMPOSER_FORWARD) {
188                         if (email_get_mail_data(ugd->nExistingMailID, &ugd->existing_mail_info->mail_data) != EMAIL_ERROR_NONE) {
189                                 debug_log("Failed to email_get_mail_data");
190                                 goto FINISH_UG;
191                         }
192
193                         if (email_get_attachment_data_list(ugd->nExistingMailID, &ugd->existing_mail_info->attachment_list, &ugd->existing_mail_info->mail_data->attachment_count) != EMAIL_ERROR_NONE) {
194                                 debug_log("Failed to email_get_attachment_data_list");
195                                 goto FINISH_UG;
196                         }
197
198                         email_get_address_info_list(ugd->nExistingMailID, &addrs_info_list);
199
200                         if (addrs_info_list) {
201                                 ugd->from_list = addrs_info_list->from;
202                                 ugd->to_list = addrs_info_list->to;
203                                 ugd->cc_list = addrs_info_list->cc;
204                                 ugd->bcc_list = addrs_info_list->bcc;
205                                 ugd->addrs_info_list = addrs_info_list;
206                         }
207
208                         if (NULL != ugd->existing_mail_info->mail_data->file_path_html) {
209                                 debug_log("original html file path(%s)", ugd->existing_mail_info->mail_data->file_path_html);
210                         }
211
212                         debug_log("account_id:%d, original account_id:%d", ugd->account_info->account_id, ugd->existing_mail_info->mail_data->account_id);
213                         ugd->account_info->account_id = ugd->existing_mail_info->mail_data->account_id;
214                 }
215
216                 if (ugd->account_info->account_count > 0) {
217                         if (!email_engine_get_account_full_data(ugd->account_info->account_id, &(ugd->account_info->account))) {
218                                 debug_log("Failed to Get account full data");
219                                 ugd->eComposerErrorType = COMPOSER_ERROR_NO_ACCOUNT;
220                                 return ugd->main_layout;
221                         }
222
223                         if (ugd->account_info->account != NULL) {
224                                 if (ugd->account_info->account) {
225                                         email_option_t option;
226                                         memcpy(&option, &(ugd->account_info->account->options), sizeof(email_option_t));
227                                         debug_log("read report : [%d], delivery report : [%d]", option.req_read_receipt, option.req_delivery_receipt);
228                                         ugd->tracking_option[0] = option.req_read_receipt;
229                                         ugd->tracking_option[1] = option.req_delivery_receipt;
230                                 }
231                         }
232
233                         ugd->account_info->account_type = ugd->account_info->account->incoming_server_type;
234                         ugd->account_info->account_name = _composer_get_email_addr_from_account_id(ugd, ugd->account_info->account_id);
235                 }
236
237                 return ugd->main_layout;
238         }
239
240 FINISH_UG:
241         _composer_finish_service(ugd);
242         return NULL;
243 }
244
245 static void on_start(ui_gadget_h ug, service_h data, void *priv)
246 {
247         debug_log("### Begin of on_start ###");
248
249         EmailComposerUGD *ugd;
250
251         if (!ug || !priv)
252                 return;
253
254         ugd = (EmailComposerUGD *)priv;
255
256         if (ugd->eComposerErrorType != COMPOSER_ERROR_NONE) {
257
258                 switch (ugd->eComposerErrorType) {
259                         case COMPOSER_ERROR_STORAGE_FULL:
260                                 debug_log("Storage full error");
261                                 _composer_popup_warning(ugd, dgettext("sys_string", "IDS_COM_POP_WARNING"), N_("Storage full"));
262                                 elm_object_style_set(ugd->bg, "transparent");
263                                 break;
264
265                         case COMPOSER_ERROR_ENGINE_INIT_FAILED:
266                                 debug_log("Failed to initialize email engine");
267                                 _composer_popup_warning(ugd, dgettext("sys_string", "IDS_COM_POP_WARNING"), _("IDS_EMAIL_POP_FAILED_TO_START_EMAIL_APPLICATION"));
268                                 elm_object_style_set(ugd->bg, "transparent");
269                                 break;
270
271                         case COMPOSER_ERROR_DBUS_FAIL:
272                                 debug_log("Failed to setup DBUS");
273                                 _composer_popup_warning(ugd, dgettext("sys_string", "IDS_COM_POP_WARNING"), _("IDS_EMAIL_POP_UNABLE_TO_COMPOSE_EMAIL_MESSAGE"));
274                                 elm_object_style_set(ugd->bg, "transparent");
275                                 break;
276
277                         case COMPOSER_ERROR_SERVICE_INIT_FAIL:
278                                 debug_log("Failed to create temp folder");
279                                 _composer_popup_warning(ugd, dgettext("sys_string", "IDS_COM_POP_WARNING"), _("IDS_EMAIL_POP_UNABLE_TO_COMPOSE_EMAIL_MESSAGE"));
280                                 elm_object_style_set(ugd->bg, "transparent");
281                                 break;
282
283                         case COMPOSER_ERROR_NO_DEFAULT_ACCOUNT:
284                         case COMPOSER_ERROR_NO_ACCOUNT_LIST:
285                         case COMPOSER_ERROR_NO_ACCOUNT:
286                                 debug_log("Failed to get default account");
287                                 _composer_popup_warning(ugd, dgettext("sys_string", "IDS_COM_POP_WARNING"), _("IDS_EMAIL_POP_THERE_IS_NO_ACCOUNT_CREATE_A_NEW_ACCOUNT_FIRST"));
288                                 if (!ugd->launch_timer)
289                                         ugd->launch_timer = ecore_timer_add(1.9f, _composer_launch_email_app_cb, ugd);
290                                 elm_object_style_set(ugd->bg, "transparent");
291                                 break;
292
293                         case COMPOSER_ERROR_UNKOWN_TYPE:
294                         case COMPOSER_ERROR_INVALID_ARG:
295                                 debug_log("Invaild argument / Unknown composer type");
296                                 _composer_popup_warning(ugd, dgettext("sys_string", "IDS_COM_POP_WARNING"), _("IDS_EMAIL_POP_UNABLE_TO_COMPOSE_EMAIL_MESSAGE"));
297                                 elm_object_style_set(ugd->bg, "transparent");
298                                 break;
299                         default :
300                                 break;
301                 }
302
303                 return;
304         }
305
306         create_composer_frame(ugd);
307
308         _composer_set_mail_info(ugd);
309         _composer_post_parse_bundle(ugd, data);
310         _composer_save_initial_email_content(ugd);
311
312         debug_log("### End of on_start ###");
313 }
314
315 static void on_pause(ui_gadget_h ug, service_h data, void *priv)
316 {
317         debug_log("");
318
319         EmailComposerUGD *ugd;
320
321         if (!ug || !priv)
322                 return;
323
324         ugd = priv;
325
326         if (ugd->selected_entry == ugd->body_ewkview) {
327                 evas_object_focus_set(ugd->body_ewkview, EINA_FALSE);
328                 ugd->focus_status = COMPOSER_FOCUS_STATUS_ON_PAUSE; // focus handling
329         }
330
331         ugd->is_hided = TRUE;
332         debug_log("### is_hided ###");
333 }
334
335 static void on_resume(ui_gadget_h ug, service_h data, void *priv)
336 {
337         debug_log("");
338
339         EmailComposerUGD *ugd;
340
341         if (!ug || !priv)
342                 return;
343
344         ugd = priv;
345
346         if (!_composer_check_popup_exist(ugd))
347                 _composer_mbe_set_focus(ugd);
348
349         ugd->is_hided = FALSE;
350         debug_log("*** is_hided: FALSE");
351 }
352
353 static void on_destroy(ui_gadget_h ug, service_h data, void *priv)
354 {
355         debug_log("");
356         EmailComposerUGD *ugd;
357         service_h reply_service;
358
359         if (!ug || !priv)
360                 return;
361
362         ugd = priv;
363
364         int ret = 0;
365
366         ret = service_create(&reply_service);
367         debug_log("service_create: %d", ret);
368         if (!reply_service) {
369                 debug_log("service create failed");
370         }
371         ret = service_reply_to_launch_request(reply_service, data, SERVICE_RESULT_SUCCEEDED);
372         debug_log("service_reply_to_launch_request: %d", ret);
373         ret = service_destroy(reply_service);
374         debug_log("service_destroy: %d", ret);
375
376         ethumb_shutdown();
377
378         debug_log("contact service end");
379         contacts_disconnect2();
380
381         _on_edbus_remove_receiver(ugd);
382
383         ret = e_dbus_shutdown();
384         debug_log("ret: %d", ret);
385
386         _composer_delete_evas_objects(ugd);
387
388         _composer_free_email_info(ugd);
389
390         email_engine_finalize();
391
392         /* deinitialize ewk */
393         debug_log("has_body_html(%d), composer_type(%d), saved_html_path(%s)", ugd->has_body_html, ugd->composer_type, ugd->saved_html_path);
394         if (EINA_TRUE == ugd->has_body_html) {
395                 if (ugd->saved_html_path) {
396                         g_free(ugd->saved_html_path);
397                         ugd->saved_html_path = NULL;
398                 }
399                 if (ugd->latest_info_content) {
400                         g_free(ugd->latest_info_content);
401                         ugd->latest_info_content = NULL;
402                 }
403                 if (ugd->latest_html_content) {
404                         g_free(ugd->latest_html_content);
405                         ugd->latest_html_content = NULL;
406                 }
407                 if (ugd->plain_content) {
408                         g_free(ugd->plain_content);
409                         ugd->plain_content = NULL;
410                 }
411                 if (ugd->original_info_content) {
412                         g_free(ugd->original_info_content);
413                         ugd->original_info_content = NULL;
414                 }
415                 if (ugd->original_html_content) {
416                         g_free(ugd->original_html_content);
417                         ugd->original_html_content = NULL;
418                 }
419         }
420
421         _composer_remove_temp_folder();
422         _composer_free_initial_email_content(ugd);
423 }
424
425 static void on_message(ui_gadget_h ug, service_h msg, service_h data, void *priv)
426 {
427         debug_log("");
428 }
429
430 static void on_event(ui_gadget_h ug, enum ug_event event, service_h data, void *priv)
431 {
432         debug_log("");
433
434         EmailComposerUGD *ugd = (EmailComposerUGD *)priv;
435         int win_main_angle = 0;
436
437         switch (event) {
438         case UG_EVENT_LOW_MEMORY:
439                 break;
440         case UG_EVENT_LOW_BATTERY:
441                 break;
442         case UG_EVENT_LANG_CHANGE:
443                 if (ugd->navi_item) {
444                         char title_str[50] = { 0, };
445                         if (ugd->composer_type == RUN_COMPOSER_REPLY || ugd->composer_type == RUN_COMPOSER_REPLY_ALL)
446                                 snprintf(title_str, sizeof(title_str), "%s", _("IDS_EMAIL_HEADER_REPLY_ABB"));
447                         else if (ugd->composer_type == RUN_COMPOSER_FORWARD)
448                                 snprintf(title_str, sizeof(title_str), "%s", _("IDS_EMAIL_OPT_FORWARD"));
449                         else if (ugd->composer_type == RUN_COMPOSER_EDIT)
450                                 snprintf(title_str, sizeof(title_str), "%s", dgettext("sys_string", "IDS_COM_HEADER_EDIT"));
451                         else
452                                 snprintf(title_str, sizeof(title_str), "%s", _("IDS_EMAIL_BODY_NEW_EMAIL"));
453                         elm_object_item_text_set(ugd->navi_item, title_str);
454                 }
455
456                 if (ugd->send_btm_btn)
457                         elm_object_text_set(ugd->send_btm_btn, dgettext("sys_string", "IDS_COM_SK_SEND_ABB"));
458                 if (ugd->send_btn)
459                         elm_object_text_set(ugd->send_btn, dgettext("sys_string", "IDS_COM_SK_SEND_ABB"));
460                 if (ugd->subject_editfield)
461                         elm_object_part_text_set(ugd->subject_editfield, "elm.guidetext", dgettext("sys_string", "IDS_COM_BODY_SUBJECT"));
462
463                 char recp_string[MAX_STR_LEN] = { 0, };
464                 if (ugd->to_mbe) {
465                         snprintf(recp_string, sizeof(recp_string), "<font_size=36><color=#000000>%s%s</color></font_size>", dgettext("sys_string", "IDS_COM_BODY_TO"), ":");
466                         elm_object_text_set(ugd->to_mbe, recp_string);
467                 }
468                 if (ugd->cc_mbe) {
469                         snprintf(recp_string, sizeof(recp_string), "<font_size=36><color=#000000>%s%s</color></font_size>", _("IDS_EMAIL_BODY_CC"), ":");
470                         elm_object_text_set(ugd->cc_mbe, recp_string);
471                 }
472                 if (ugd->bcc_mbe) {
473                         snprintf(recp_string, sizeof(recp_string), "<font_size=36><color=#000000>%s%s</color></font_size>", _("IDS_EMAIL_BODY_BCC"), ":");
474                         elm_object_text_set(ugd->bcc_mbe, recp_string);
475                 }
476                 if (ugd->from_mbe) {
477                         snprintf(recp_string, sizeof(recp_string), "<font_size=36><color=#000000>%s%s</color></font_size>", _("IDS_EMAIL_BODY_FROM"), ":");
478                         elm_object_text_set(ugd->from_mbe, recp_string);
479                 }
480                 break;
481         case UG_EVENT_ROTATE_PORTRAIT:
482                 elm_layout_theme_set(ugd->main_layout, "layout", "application", "default");
483                 ugd->isHorizontal = false;
484                 win_main_angle = 0;
485                 break;
486         case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
487                 elm_layout_theme_set(ugd->main_layout, "layout", "application", "default");
488                 ugd->isHorizontal = false;
489                 win_main_angle = 180;
490                 break;
491         case UG_EVENT_ROTATE_LANDSCAPE:
492                 elm_layout_theme_set(ugd->main_layout, "layout", "application", "noindicator");
493                 ugd->isHorizontal = true;
494                 win_main_angle = 270;
495                 break;
496         case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
497                 elm_layout_theme_set(ugd->main_layout, "layout", "application", "noindicator");
498                 ugd->isHorizontal = true;
499                 win_main_angle = 90;
500                 break;
501         case UG_EVENT_REGION_CHANGE:
502                 break;
503         default:
504                 break;
505         }
506         _composer_resize_body_webview(ugd, 0);
507         elm_layout_sizing_eval(ugd->c_layout);
508
509         debug_log("win_main_angle: %d, win_rotation: %d, device_orientation: %d, event: %d", win_main_angle, elm_win_rotation_get(ugd->win_main), app_get_device_orientation(), event);
510
511         if (ugd->selected_entry != ugd->body_ewkview) {
512                 debug_log("webview: scroll to [0,0]");
513                 // If there is a focus on other entry, scroll moves to it because of bringing. so webview inner scroll has to be moved to top.
514                 ewk_view_scroll_set(ugd->body_ewkview, 0, 0);
515
516                 // main scrolling is moved to the selected entry, so that scrolling status of main scroller and webview have to be reset
517                 ewk_view_vertical_panning_hold_set(ugd->body_ewkview, EINA_TRUE);
518
519                 if (elm_widget_scroll_freeze_get(ugd->main_scroller) > 0)
520                         elm_object_scroll_freeze_pop(ugd->main_scroller);
521         } else {
522                 if (ugd->isHorizontal)
523                         ugd->temporary_fix_for_rotation = true;
524         }
525
526         if (ugd->popup_win) {
527                 elm_win_rotation_with_resize_set(ugd->popup_win, win_main_angle);
528         }
529
530         elm_win_rotation_with_resize_set(ugd->win_main, win_main_angle);
531         ugd->isRotated = true;
532 }
533
534 static Evas_Object *create_fullview(Evas_Object *parent, EmailComposerUGD *ugd)
535 {
536         debug_log("");
537         Evas_Object *base;
538         Evas_Object *bg;
539
540         base = elm_layout_add(parent);
541
542         if (!base)
543                 return NULL;
544
545         elm_layout_theme_set(base, "layout", "application", "default");
546         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
547         evas_object_size_hint_align_set(base, EVAS_HINT_FILL, EVAS_HINT_FILL);
548
549         bg = elm_bg_add(base);
550         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
551         evas_object_size_hint_align_set(bg, EVAS_HINT_FILL, EVAS_HINT_FILL);
552         elm_win_resize_object_add(parent, bg);
553         evas_object_show(bg);
554
555         ugd->bg = bg;
556
557         elm_object_part_content_set(base, "elm.swallow.bg", bg);
558
559         evas_object_show(base);
560
561         return base;
562 }
563
564 static Evas_Object *create_frameview(Evas_Object *parent, EmailComposerUGD *ugd)
565 {
566         debug_log("");
567         Evas_Object *base;
568
569         /* Create Frame view */
570         base = elm_layout_add(parent);
571         if (!base)
572                 return NULL;
573
574         /* In case of frameview, do not show indicator area */
575         elm_layout_theme_set(base, "layout", "application", "default");
576         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
577
578         evas_object_show(base);
579         return base;
580 }
581
582 static int _composer_check_storage_full(char *path, int mb_size)
583 {
584         debug_enter();
585
586         int ret = false;
587         struct statfs buf = { 0 };
588
589         debug_log("path(%s)", path);
590         if (statfs(path, &buf) == -1) {
591                 debug_error("statfs Error: errno(%d), errmsg(%s)", errno, strerror(errno));
592                 ret = false;
593         } else {
594                 long i_free = (buf.f_bfree * buf.f_bsize) / (1024 * 1024);
595                 debug_log("f_bfree[%d], f_bsize[%d]", buf.f_bfree, buf.f_bsize);
596                 debug_log("Free space: [%ld]MB. Required min space: [%d]MB.", i_free, mb_size);
597                 if (i_free < mb_size) { //Limited size is 5MB
598                         ret = true;
599                         debug_log("Storage is almost full.");
600                 }
601         }
602
603         return ret;
604 }
605
606 /* Temporary folder /tmp/email is created when composer is launched. */
607 int _composer_create_temp_folder()
608 {
609         debug_log("");
610
611         if (!email_check_dir_exist(EMAIL_TMP_FOLDER)) {
612                 int nErr = -1;
613                 nErr = mkdir(EMAIL_TMP_FOLDER, EMAIL_TMP_FOLDER_PERMISSION);
614                 debug_log("errno: %d", nErr);
615                 if (nErr == -1) {
616                         debug_log("Email temp folder creation failed");
617                         return -1;
618                 }
619         } else
620                 debug_log("Email temp folder already exists.");
621         return 0;
622 }
623
624 /* Temporary folder '/tmp/email' and its contents are deleted when composer is destroyed. */
625 static void _composer_remove_temp_folder()
626 {
627         debug_log("");
628
629         struct dirent *dir_entry = NULL;
630         DIR *dir;
631         char buffer[256];
632
633         dir = opendir(EMAIL_TMP_FOLDER);
634         if (!dir) {
635                 debug_log("Unable to open %s", EMAIL_TMP_FOLDER);
636                 return;
637         }
638         while ((dir_entry = readdir(dir))) {
639                 debug_log("%s", dir_entry->d_name);
640                 if (g_strcmp0(".", dir_entry->d_name) == 0 || g_strcmp0("..", dir_entry->d_name) == 0)
641                         continue;
642                 snprintf(buffer, 256, "%s/%s", EMAIL_TMP_FOLDER, dir_entry->d_name);
643                 if (-1 == remove(buffer)) {
644                         debug_log("Failed to remove buffer");
645                 }
646         }
647         closedir(dir);
648         rmdir(EMAIL_TMP_FOLDER);
649 }
650
651 Eina_List * _composer_create_initial_recipients_list(Evas_Object *mbe)
652 {
653         debug_log("");
654
655         if (!mbe)
656                 return NULL;
657
658         const Eina_List *l = NULL;
659         Eina_List *initial_list = NULL;
660         Elm_Object_Item *item;
661
662         const Eina_List *items_list = elm_multibuttonentry_items_get(mbe);
663
664         if (items_list) {
665                 EINA_LIST_FOREACH(items_list, l, item) {
666                         initial_list = eina_list_append(initial_list, item);
667                 }
668         }
669
670         return initial_list;
671 }
672
673 void _composer_save_initial_email_content(EmailComposerUGD *ugd)
674 {
675         debug_log("");
676
677         if (ugd->to_mbe) {
678                 ugd->to_mbe_initial_list = _composer_create_initial_recipients_list(ugd->to_mbe);
679         }
680         if (ugd->cc_mbe) {
681                 ugd->cc_mbe_initial_list = _composer_create_initial_recipients_list(ugd->cc_mbe);
682         }
683         if (ugd->bcc_mbe) {
684                 ugd->bcc_mbe_initial_list = _composer_create_initial_recipients_list(ugd->bcc_mbe);
685         }
686
687         ugd->saved_subject = g_strdup(elm_entry_entry_get(ugd->subject_entry));
688
689         Eina_List *initial_list = NULL;
690         int nAttachmentObjListCount = eina_list_count(ugd->attachment_item_obj_list);
691
692         int i = 0;
693         for (i = 0; i < nAttachmentObjListCount; i++)
694                 initial_list = eina_list_append(initial_list, eina_list_nth(ugd->attachment_item_obj_list, i));
695
696         ugd->attach_initial_list = initial_list;
697 }
698
699 void _composer_free_initial_email_content(EmailComposerUGD *ugd)
700 {
701         debug_log("");
702
703         if (ugd->to_mbe_initial_list) {
704                 eina_list_free(ugd->to_mbe_initial_list);
705                 ugd->to_mbe_initial_list = NULL;
706         }
707         if (ugd->cc_mbe_initial_list) {
708                 eina_list_free(ugd->cc_mbe_initial_list);
709                 ugd->cc_mbe_initial_list = NULL;
710         }
711         if (ugd->bcc_mbe_initial_list) {
712                 eina_list_free(ugd->bcc_mbe_initial_list);
713                 ugd->bcc_mbe_initial_list = NULL;
714         }
715         if (ugd->attach_initial_list) {
716                 eina_list_free(ugd->attach_initial_list);
717                 ugd->attach_initial_list = NULL;
718         }
719         if (ugd->saved_subject) {
720                 g_free(ugd->saved_subject);
721                 ugd->saved_subject = NULL;
722         }
723         if (ugd->listOfImageUrls) {
724                 eina_list_free(ugd->listOfImageUrls);
725                 ugd->listOfImageUrls = NULL;
726         }
727 }
728
729 void create_composer_frame(EmailComposerUGD *ugd)
730 {
731         debug_log("Begin");
732
733         if (ugd->win_main == NULL) {
734                 debug_log("ugd->win_main == NULL");
735                 return;
736         }
737
738         Evas_Object *win;
739
740         Evas_Object *cancel_btn;
741         Evas_Object *more_btn;
742         Evas_Object *send_btn;
743
744         Evas_Object *nf;
745         Evas_Object *outer_layout;
746         Evas_Object *inner_layout;
747         Evas_Object *inner_sc;
748
749         win = ugd->win_main;
750         ugd->evas = evas_object_evas_get(win);
751
752         nf = _composer_create_navigation_layout(ugd);
753         outer_layout = _composer_create_outer_layout(nf);
754         inner_layout = _composer_create_composer_layout(nf);
755         inner_sc = _composer_create_main_scroller(nf);
756
757         elm_object_content_set(inner_sc, inner_layout);
758         elm_object_part_content_set(outer_layout, "elm.swallow.content", inner_sc);
759
760         ugd->navi_bar = nf;
761         ugd->main_scroller = inner_sc;
762         ugd->c_layout = inner_layout;
763
764         _composer_create_view(inner_layout, ugd);
765
766         char title_str[50] = { 0, };
767         if (ugd->composer_type == RUN_COMPOSER_REPLY || ugd->composer_type == RUN_COMPOSER_REPLY_ALL)
768                 snprintf(title_str, sizeof(title_str), "%s", _("IDS_EMAIL_HEADER_REPLY_ABB"));
769         else if (ugd->composer_type == RUN_COMPOSER_FORWARD)
770                 snprintf(title_str, sizeof(title_str), "%s", _("IDS_EMAIL_OPT_FORWARD"));
771         else if (ugd->composer_type == RUN_COMPOSER_EDIT)
772                 snprintf(title_str, sizeof(title_str), "%s", dgettext("sys_string", "IDS_COM_HEADER_EDIT"));
773         else
774                 snprintf(title_str, sizeof(title_str), "%s", _("IDS_EMAIL_BODY_NEW_EMAIL"));
775
776         // Bottom Back Button
777         Evas_Object *cancel_btm_btn = NULL;
778         cancel_btm_btn = elm_button_add(ugd->navi_bar);
779         elm_object_style_set(cancel_btm_btn, "naviframe/end_btn/default");
780         evas_object_smart_callback_add(cancel_btm_btn, "clicked", _composer_back_button_cb, ugd);
781         ugd->cancel_btm_btn = cancel_btm_btn;
782         debug_log("cancel_btm_btn of composer: %p", ugd->cancel_btm_btn);
783
784         ugd->navi_item = elm_naviframe_item_push(ugd->navi_bar, title_str, cancel_btm_btn, NULL, outer_layout, NULL);
785
786         // Right : cancel button
787         cancel_btn = elm_button_add(ugd->navi_bar);
788         elm_object_style_set(cancel_btn, "naviframe/back_btn/default");
789         elm_object_item_part_content_set(ugd->navi_item, "title_prev_btn", cancel_btn);
790         evas_object_smart_callback_add(cancel_btn, "clicked", _composer_back_button_cb, ugd);
791         ugd->cancel_btn = cancel_btn;
792         debug_log("cancel_btn of composer: %p", ugd->cancel_btn);
793
794         // Middle : more button
795         more_btn = elm_button_add(ugd->navi_bar);
796         elm_object_style_set(more_btn, "naviframe/more/default");
797         elm_object_focus_allow_set(more_btn, EINA_FALSE);
798         elm_object_item_part_content_set(ugd->navi_item, "title_more_btn", more_btn);
799         evas_object_smart_callback_add(more_btn, "clicked", _composer_more_toolbar_button_cb, ugd);
800
801         Evas_Object *more_btm_btn = elm_button_add(ugd->navi_bar);
802         elm_object_style_set(more_btm_btn, "naviframe/more/default");
803         elm_object_focus_allow_set(more_btm_btn, EINA_FALSE);
804         elm_object_item_part_content_set(ugd->navi_item, "toolbar_more_btn", more_btm_btn);
805         evas_object_smart_callback_add(more_btm_btn, "clicked", _composer_more_toolbar_button_cb, ugd);
806
807         Evas_Object *send_btm_btn = elm_button_add(ugd->navi_bar);
808         elm_object_style_set(send_btm_btn, "naviframe/toolbar/default");
809         elm_object_text_set(send_btm_btn, dgettext("sys_string", "IDS_COM_SK_SEND_ABB"));
810         elm_object_item_part_content_set(ugd->navi_item, "toolbar_button1", send_btm_btn);
811         evas_object_smart_callback_add(send_btm_btn, "clicked", _composer_cbar_send_clicked, ugd);
812         ugd->send_btm_btn = send_btm_btn;
813
814         // Left : send button
815         send_btn = elm_button_add(ugd->navi_bar);
816         elm_object_style_set(send_btn, "naviframe/toolbar/default");
817         elm_object_text_set(send_btn, dgettext("sys_string", "IDS_COM_SK_SEND_ABB"));
818         elm_object_item_part_content_set(ugd->navi_item, "title_toolbar_button1", send_btn);
819         evas_object_smart_callback_add(send_btn, "clicked", _composer_cbar_send_clicked, ugd);
820         ugd->send_btn = send_btn;
821
822         _composer_register_scroller_callback(ugd);
823
824         if (_composer_check_recipient_is_empty(ugd)) {
825                 elm_object_disabled_set(ugd->send_btn, EINA_TRUE);
826                 elm_object_disabled_set(ugd->send_btm_btn, EINA_TRUE);
827                 ugd->bSendBtnDisabled = true;
828         }
829
830         debug_log("End");
831 }
832
833 void _composer_create_view(Evas_Object *parent, EmailComposerUGD *ugd)
834 {
835         debug_log("Begin");
836
837         g_ugd = ugd;
838
839         /* Create from field */
840         if (ugd->account_info->account_count > 1) {
841                 debug_log("from field is not created, create");
842                 email_composer_create_from_field(ugd);
843                 elm_object_part_content_set(ugd->c_layout, "from_field", ugd->from_ly);
844                 edje_object_signal_emit(_EDJ(ugd->c_layout), "show.from", "from");
845
846                 _composer_add_from_address(ugd);
847         }
848
849         /* Create to field */
850         email_composer_create_to_field(ugd);
851         edje_object_signal_emit(_EDJ(ugd->c_layout), "show.to", "*");
852
853         edje_object_signal_emit(_EDJ(parent), "hide_to_ps", "*");
854         edje_object_signal_emit(_EDJ(parent), "hide_cc_ps", "*");
855         edje_object_signal_emit(_EDJ(parent), "hide_bcc_ps", "*");
856
857         /* Create content field */
858         _composer_content_create_content_field(parent, ugd);
859 }
860
861 static void _composer_init_data(void *data)
862 {
863         debug_log("");
864
865         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
866
867         ugd->send_timer = NULL;
868         ugd->bringin_timer = NULL;
869         ugd->mbe_unfocus_timer = NULL;
870         ugd->focus_timer = NULL;
871         ugd->launch_timer = NULL;
872         ugd->ps_timer = NULL;
873         ugd->sc_timer = NULL;
874
875         ugd->subject_entry = NULL;
876         ugd->priv_selected_entry = NULL;
877         ugd->selected_entry = NULL;
878
879         ugd->body_ewkview = NULL;
880         ugd->has_body_html = EINA_FALSE;
881         ugd->saved_html_path = NULL;
882         ugd->latest_info_content = NULL;
883         ugd->latest_html_content = NULL;
884         ugd->plain_content = NULL;
885         ugd->original_info_content = NULL;
886         ugd->original_html_content = NULL;
887         ugd->to_recipients_cnt = 0;
888         ugd->cc_recipients_cnt = 0;
889         ugd->bcc_recipients_cnt = 0;
890
891         ugd->to_mbe_initial_list = NULL;
892         ugd->cc_mbe_initial_list = NULL;
893         ugd->bcc_mbe_initial_list = NULL;
894         ugd->attach_initial_list = NULL;
895         ugd->saved_subject = NULL;
896         ugd->nExistingMailID = 0;
897         ugd->attachment_list_compressed = EINA_FALSE;
898
899         ugd->idler_save_draft = NULL;
900         ugd->idler_set_focus = NULL;
901         ugd->idler_show_progress = NULL;
902         ugd->bSendBtnDisabled = false;
903         ugd->is_hided = false;
904         ugd->is_ime_hided = TRUE;
905
906         ugd->composer_noti = NULL;
907         ugd->timeout_noti = NULL;
908         ugd->change_addr_noti = NULL;
909         ugd->composer_popup = NULL;
910         ugd->dn_noti_popup = NULL;
911         ugd->dn_prog_popup = NULL;
912
913         ugd->fw_attach_cnt = 0;
914         ugd->fw_dn_cnt = 0;
915         ugd->b_cc_ps_open = false;
916         ugd->b_bcc_ps_open = false;
917         ugd->b_cc_bcc = false;
918         ugd->clipboard_on = false;
919         ugd->b_sending = false;
920         ugd->ps_on = false;
921         ugd->cc_added = false;
922         ugd->bcc_added = false;
923         ugd->me_added = false;
924         ugd->isRotated = false;
925         ugd->isHorizontal = false;
926         ugd->is_recipient_duplicated = false;
927
928         ugd->temporary_fix_for_rotation = false;
929         ugd->is_prediction = EINA_FALSE;
930         ugd->is_main_scroller_scrolling = false;
931         ugd->is_webview_scrolling = false;
932
933         ugd->focus_status = COMPOSER_FOCUS_STATUS_NONE;
934         ugd->listOfImageUrls = NULL;
935
936         ugd->b_load_finished = EINA_FALSE;
937         ugd->need_download = EINA_FALSE;
938
939         Elm_Theme *th = elm_theme_new();
940         elm_theme_ref_set(th, NULL);
941         ugd->th = th;
942         elm_theme_extension_add(ugd->th, COMPOSER_EDJ_NAME);
943
944         if (ethumb_init() != EINA_TRUE) {
945                 debug_log("Fail to ethumb_init()");
946         }
947
948         ugd->composer_type = RUN_TYPE_UNKNOWN;
949
950         ugd->mailbox_info = (EmailComposerMailbox *) calloc(1, sizeof(EmailComposerMailbox));
951
952         ugd->existing_mail_info = (EmailComposerMail *)calloc(1, sizeof(EmailComposerMail));
953         memset(ugd->existing_mail_info, 0x00, sizeof(EmailComposerMail));
954
955         ugd->new_mail_info = (EmailComposerMail *)calloc(1, sizeof(EmailComposerMail));
956         memset(ugd->new_mail_info, 0x00, sizeof(EmailComposerMail));
957         ugd->new_mail_info->mail_data = (email_mail_data_t *) calloc(1, sizeof(email_mail_data_t));
958
959         ugd->account_info = (EmailComposerAccount *) calloc(1, sizeof(EmailComposerAccount));
960         memset(ugd->account_info, 0x00, sizeof(EmailComposerMail));
961
962         ugd->priority_option = EMAIL_MAIL_PRIORITY_NORMAL;
963 }
964
965 static int _composer_init_service(void *data)
966 {
967         debug_log("");
968
969         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
970
971         COMPOSER_ERROR_TYPE_E ret = COMPOSER_ERROR_NONE;
972
973         debug_log("contact service begin");
974         contacts_connect2();
975
976         /* DBUS */
977         if (_composer_dbus_receiver_setup(ugd) < 0)
978                 return COMPOSER_ERROR_DBUS_FAIL;
979
980         if (_composer_create_temp_folder() < 0)
981                 return COMPOSER_ERROR_SERVICE_INIT_FAIL;
982
983         _composer_init_data(ugd);
984
985         return ret;
986 }
987
988 static void _composer_finish_service(void *data)
989 {
990         debug_log("");
991
992         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
993
994         ethumb_shutdown();
995         _composer_delete_evas_objects(ugd);
996
997         _composer_free_email_info(ugd);
998
999         email_engine_finalize();
1000
1001         debug_log("contact service end");
1002         contacts_disconnect2();
1003
1004         _on_edbus_remove_receiver(ugd);
1005
1006         int ret = e_dbus_shutdown();
1007         debug_log("ret: %d", ret);
1008
1009         /* deinitialize ewk */
1010         debug_log("has_body_html(%d), composer_type(%d), saved_html_path(%s)", ugd->has_body_html, ugd->composer_type, ugd->saved_html_path);
1011         if (EINA_TRUE == ugd->has_body_html) {
1012                 if (ugd->saved_html_path) {
1013                         g_free(ugd->saved_html_path);
1014                         ugd->saved_html_path = NULL;
1015                 }
1016                 if (ugd->latest_info_content) {
1017                         g_free(ugd->latest_info_content);
1018                         ugd->latest_info_content = NULL;
1019                 }
1020                 if (ugd->latest_html_content) {
1021                         g_free(ugd->latest_html_content);
1022                         ugd->latest_html_content = NULL;
1023                 }
1024                 if (ugd->plain_content) {
1025                         g_free(ugd->plain_content);
1026                         ugd->plain_content = NULL;
1027                 }
1028                 if (ugd->original_info_content) {
1029                         g_free(ugd->original_info_content);
1030                         ugd->original_info_content = NULL;
1031                 }
1032                 if (ugd->original_html_content) {
1033                         g_free(ugd->original_html_content);
1034                         ugd->original_html_content = NULL;
1035                 }
1036         }
1037
1038         _composer_remove_temp_folder();
1039 }
1040
1041 static void _composer_delete_evas_objects(EmailComposerUGD *ugd)
1042 {
1043         debug_log("");
1044
1045         _composer_attachment_reset(ugd);
1046
1047         if (ugd->main_layout) {
1048                 evas_object_del(ugd->main_layout);
1049                 ugd->main_layout = NULL;
1050         }
1051
1052         if (ugd->bg) {
1053                 evas_object_del(ugd->bg);
1054                 ugd->bg = NULL;
1055         }
1056
1057         if (ugd->th) {
1058                 elm_theme_extension_del(ugd->th, COMPOSER_EDJ_NAME);
1059                 elm_theme_free(ugd->th);
1060                 ugd->th = NULL;
1061         }
1062
1063         if (ugd->idler_save_draft) {
1064                 debug_log("delete idler_save_draft");
1065                 ecore_idler_del(ugd->idler_save_draft);
1066                 ugd->idler_save_draft = NULL;
1067         }
1068
1069         if (ugd->send_timer) {
1070                 debug_log("delete send_timer");
1071                 ecore_timer_del(ugd->send_timer);
1072                 ugd->send_timer = NULL;
1073         }
1074
1075         if (ugd->launch_timer) {
1076                 debug_log("delete launch_timer");
1077                 ecore_timer_del(ugd->launch_timer);
1078                 ugd->launch_timer = NULL;
1079         }
1080
1081         _composer_delete_all_popup(ugd);
1082
1083         if (ugd->ps_list) {
1084                 elm_genlist_clear(ugd->ps_list);
1085                 evas_object_del(ugd->ps_list);
1086                 ugd->ps_list = NULL;
1087         }
1088
1089         if (ugd->from_ly) {
1090                 evas_object_del(ugd->from_ly);
1091                 ugd->from_ly = NULL;
1092         }
1093
1094         if (ugd->to_ly) {
1095                 evas_object_del(ugd->to_ly);
1096                 ugd->to_ly = NULL;
1097         }
1098
1099         if (ugd->cc_ly) {
1100                 evas_object_del(ugd->cc_ly);
1101                 ugd->cc_ly = NULL;
1102         }
1103
1104         if (ugd->bcc_ly) {
1105                 evas_object_del(ugd->bcc_ly);
1106                 ugd->bcc_ly = NULL;
1107         }
1108
1109         if (ugd->subject_entry) {
1110                 evas_object_del(ugd->subject_entry);
1111                 ugd->subject_entry = NULL;
1112         }
1113
1114         if (ugd->body_ewkview) {
1115                 evas_object_del(ugd->body_ewkview);
1116                 ugd->body_ewkview = NULL;
1117         }
1118
1119         if (ugd->fw_attachment_list) {
1120                 debug_log("Free the existing attachments..");
1121                 int i;
1122                 for (i = 0; i < g_list_length(ugd->fw_attachment_list); ++i) {
1123                         EMAIL_ATTACHMENT_INFO_S *info = (EMAIL_ATTACHMENT_INFO_S *) g_list_nth_data(ugd->fw_attachment_list, i);
1124                         if (info->name)
1125                                 free(info->name);
1126                         if (info->path)
1127                                 free(info->path);
1128                 }
1129
1130                 g_list_free(ugd->fw_attachment_list);
1131                 ugd->fw_attachment_list = NULL;
1132         }
1133 }
1134
1135 static void _composer_delete_all_popup(EmailComposerUGD *ugd)
1136 {
1137         debug_log("");
1138
1139         if (ugd->popup_list == NULL)
1140                 return;
1141
1142         Evas_Object * popup;
1143         EINA_LIST_FREE(ugd->popup_list, popup) {
1144                 if (popup)
1145                         evas_object_del(popup);
1146         }
1147 }
1148
1149 static void _composer_free_email_info(EmailComposerUGD *ugd)
1150 {
1151         debug_log("");
1152
1153         if (ugd->account_info) {
1154                 debug_log("free account_info: account_id(%d), account_count(%d)", ugd->account_info->account_id, ugd->account_info->account_count);
1155                 if (ugd->account_info->account_list) {
1156                         debug_log("free account_list: account_name(%s), account_id(%d)", ugd->account_info->account_list->account_name, ugd->account_info->account_list->account_id);
1157
1158                         if (ugd->account_info->account_name) {
1159                                 debug_log("free account_name(%s)", ugd->account_info->account_name);
1160                                 free(ugd->account_info->account_name);
1161                                 ugd->account_info->account_name = NULL;
1162                         }
1163
1164                         email_free_account(&(ugd->account_info->account_list), ugd->account_info->account_count);
1165                 }
1166
1167                 if (ugd->account_info->account) {
1168                         debug_log("free account: account_name(%s), account_id(%d)", ugd->account_info->account->account_name, ugd->account_info->account->account_id);
1169                         email_free_account(&(ugd->account_info->account), 1);
1170                 }
1171
1172                 free(ugd->account_info);
1173                 ugd->account_info = NULL;
1174         }
1175
1176         if (ugd->mailbox_info) {
1177                 email_free_mailbox(&(ugd->mailbox_info->mail_box), 1);
1178                 free(ugd->mailbox_info);
1179         }
1180
1181         if (ugd->existing_mail_info) {
1182                 if (ugd->existing_mail_info->attachment_list) {
1183                         email_free_attachment_data(&ugd->existing_mail_info->attachment_list, ugd->existing_mail_info->mail_data->attachment_count);
1184                         ugd->existing_mail_info->attachment_list = NULL;
1185                 }
1186
1187                 if (ugd->existing_mail_info->mail_data)
1188                         email_free_mail_data(&(ugd->existing_mail_info->mail_data), 1);
1189
1190                 free(ugd->existing_mail_info);
1191                 ugd->existing_mail_info = NULL;
1192         }
1193
1194         if (ugd->new_mail_info) {
1195                 if (ugd->new_mail_info->attachment_list) {
1196                         email_free_attachment_data(&ugd->new_mail_info->attachment_list, ugd->new_mail_info->mail_data->attachment_count);
1197                         ugd->new_mail_info->attachment_list = NULL;
1198                 }
1199
1200                 if (ugd->new_mail_info->mail_data)
1201                         email_free_mail_data(&(ugd->new_mail_info->mail_data), 1);
1202
1203                 free(ugd->new_mail_info);
1204                 ugd->new_mail_info = NULL;
1205         }
1206
1207         if (ugd->addrs_info_list) {
1208                 debug_log("free addrs_info_list");
1209                 email_free_address_info_list(&ugd->addrs_info_list);
1210         }
1211 }
1212
1213 static Eina_Bool _composer_launch_email_app_cb(void *data)
1214 {
1215         debug_log("");
1216
1217         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1218
1219         int ret;
1220         service_h service = NULL;
1221         ret = service_create(&service);
1222         debug_log("service_create: %d", ret);
1223         if (!service) {
1224                 debug_log("service create failed");
1225         }
1226         ret = service_add_extra_data(service, EMAIL_BUNDLE_KEY_RUN_TYPE, "9");
1227         debug_log("service_add_extra_data: %d", ret);
1228         ret = service_set_package(service, PKGNAME);
1229         debug_log("service_set_package: %d", ret);
1230         ret = service_send_launch_request(service, NULL, NULL);
1231         debug_log("service_send_launch_request: %d", ret);
1232         ret = service_destroy(service);
1233         debug_log("service_destroy: %d", ret);
1234
1235         if (ugd->launch_timer) {
1236                 debug_log("delete launch_timer");
1237                 ecore_timer_del(ugd->launch_timer);
1238                 ugd->launch_timer = NULL;
1239         }
1240
1241         return ECORE_CALLBACK_CANCEL;
1242 }
1243
1244 static void _composer_popup_warning(EmailComposerUGD *ugd, char *header, char *content)
1245 {
1246         debug_log("");
1247
1248         Evas_Object *pu;
1249
1250         pu = elm_popup_add(ugd->win_main);
1251         if (!pu)
1252                 return;
1253
1254         if (header) {
1255                 elm_object_part_text_set(pu, "title,text", header);
1256         }
1257
1258         if (content) {
1259                 elm_object_text_set(pu, content);
1260         }
1261         elm_popup_timeout_set(pu, 2.0);
1262
1263         evas_object_smart_callback_add(pu, "timeout", _composer_ug_destroy_cb, ugd);
1264         evas_object_smart_callback_add(pu, "block,clicked", _composer_ug_destroy_cb, ugd);
1265
1266         evas_object_show(pu);
1267
1268         ugd->composer_noti = pu;
1269 }
1270
1271 static void _composer_ug_destroy_cb(void *data, Evas_Object *obj, void *event_info)
1272 {
1273         debug_log("");
1274
1275         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1276
1277         if (ugd->composer_noti) {
1278                 evas_object_del(ugd->composer_noti);
1279                 ugd->composer_noti = NULL;
1280         }
1281
1282         ug_destroy_me(ugd->ug_main);
1283 }
1284
1285 char *_composer_parse_recipients_arg(EmailComposerUGD *ugd, const char *source)
1286 {
1287         debug_log("");
1288
1289         int i = 0;
1290         char buf[MAX_RECIPIENT_ADDRESSES_LEN] = { 0, };
1291
1292         char *item_str;
1293
1294         char *display_name = NULL;
1295         char email_addr[MAX_RECIPIENT_ADDRESS_LEN];
1296
1297         gchar **vector;
1298         gchar **vector_sub;
1299         int written_size = 0;
1300
1301         /* Newly allocated array of strings returned */
1302         vector = g_strsplit_set(source, ";", -1);
1303         if (vector == NULL) {
1304                 debug_log("vector == NULL");
1305                 return NULL;
1306         }
1307
1308         guint recipients_num = g_strv_length(vector);
1309         debug_log("recipient number: %d", recipients_num);
1310
1311         for (i = 0; i < recipients_num; i++) {
1312                 memset(email_addr, 0x00, sizeof(email_addr));
1313                 item_str = vector[i];
1314
1315                 if (strlen(item_str) > MAX_RECIPIENT_ADDRESS_LEN) {
1316                         if (ugd->composer_noti) {
1317                                 evas_object_del(ugd->composer_noti);
1318                                 ugd->composer_noti = NULL;
1319                         }
1320                         ugd->composer_noti = _composer_create_noti(ugd, false, dgettext("sys_string", "IDS_COM_POP_WARNING"), _("IDS_EMAIL_POP_MAXIMUM_RECIPIENT_EMAIL_LENGTH_EXCEEDED"),
1321                                 0, NULL, NULL, 2.0, _composer_noti_response_cb);
1322
1323                         continue;
1324                 }
1325
1326                 if (g_strcmp0(item_str, "") != 0) {
1327                         /* Newly allocated array of strings returned */
1328                         vector_sub = g_strsplit_set(item_str, "<>", -1);
1329                         if (g_strv_length(vector_sub) > 2) {
1330                                 display_name = vector_sub[1];
1331                                 written_size = snprintf(email_addr, sizeof(email_addr), "%s", vector_sub[2]);
1332                                 if (written_size > sizeof(email_addr)) {
1333                                         debug_warning("Email address data truncated");
1334                                 }
1335
1336                                 if (i == 0) {
1337                                         written_size = snprintf(buf, sizeof(buf), "<%s> %s;", display_name, email_addr);
1338                                                 if (written_size > sizeof(buf)) {
1339                                                 debug_warning("Display name data truncated");
1340                                         }
1341                                 } else {
1342                                         written_size = snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "<%s> %s;", display_name, email_addr);
1343                                         if (written_size > sizeof(buf)) {
1344                                                 debug_warning("Display name data truncated");
1345                                         }
1346                                 }
1347
1348                         }
1349
1350                         if (g_strv_length(vector_sub) == 1) {
1351                                 display_name = vector_sub[0];
1352                                 written_size = snprintf(email_addr, sizeof(email_addr), "%s", vector_sub[0]);
1353
1354                                 if (written_size > sizeof(email_addr)) {
1355                                         debug_warning("Email address data truncated");
1356                                 }
1357
1358                                 if (i == 0) {
1359                                         written_size = snprintf(buf, sizeof(buf), "%s;", email_addr);
1360                                         if (written_size > sizeof(buf)) {
1361                                                 debug_warning("Display name data truncated");
1362                                         }
1363                                 } else {
1364                                         written_size = snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%s;", email_addr);
1365                                         if (written_size > sizeof(buf)) {
1366                                                 debug_warning("Display name data truncated");
1367                                         }
1368                                 }
1369                         }
1370
1371                         /* allocated array of strings freed */
1372                         g_strfreev(vector_sub);
1373                 }
1374         }
1375
1376         /* allocated array of strings freed */
1377         g_strfreev(vector);
1378
1379         debug_log("buf = %s", buf);
1380
1381         return g_strdup(buf);
1382 }
1383
1384 static int _composer_pre_parse_bundle(EmailComposerUGD *ugd, service_h data)
1385 {
1386         debug_log("");
1387
1388         char *argv[7] = { 0, };
1389
1390         if (data) {
1391                 int ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_RUN_TYPE, (char **)&argv[0]);
1392                 debug_log("service_get_extra_data: %d", ret);
1393                 debug_log("argv[0]:%s", argv[0]);
1394         }
1395
1396         int ret;
1397         char *operation = NULL;
1398         ret = service_get_operation(data, &operation);
1399         debug_log("service_get_operation: %d", ret);
1400         debug_log("operation = %s", operation);
1401
1402         if (email_engine_get_default_account(&ugd->account_info->account_id) == false)
1403                 return COMPOSER_ERROR_NO_DEFAULT_ACCOUNT;
1404
1405         if (argv[0]) {
1406                 debug_log("argv[0] = %s", argv[0]);
1407
1408                 ugd->composer_type = atoi(argv[0]);
1409                 debug_log("composer type = %d", ugd->composer_type);
1410
1411                 if (ugd->composer_type == RUN_COMPOSER_EXTERNAL) {
1412                         if (operation == NULL) {        /* ug called by ug_create */
1413                                 debug_log("ug called by ug_create");
1414                                 ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_TO, (char **)&argv[1]);
1415                                 debug_log("service_get_extra_data: %d", ret);
1416                                 debug_log("to:%s", argv[1]);
1417                                 ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_CC, (char **)&argv[2]);
1418                                 debug_log("service_get_extra_data: %d", ret);
1419                                 debug_log("cc:%s", argv[2]);
1420                                 ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_BCC, (char **)&argv[3]);
1421                                 debug_log("service_get_extra_data: %d", ret);
1422                                 debug_log("bcc:%s", argv[3]);
1423                                 ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_SUBJECT, (char **)&argv[4]);
1424                                 debug_log("service_get_extra_data: %d", ret);
1425                                 debug_log("subject:%s", argv[4]);
1426                                 ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_BODY, (char **)&argv[5]);
1427                                 debug_log("service_get_extra_data: %d", ret);
1428                                 debug_log("body:%s", argv[5]);
1429                         } else {        /* ug called by appcontrol request */
1430                                 debug_log("ug called by appcontrol request");
1431                                 ret = service_get_extra_data(data, SERVICE_DATA_TO, (char **)&argv[1]);
1432                                 debug_log("service_get_extra_data: %d", ret);
1433                                 debug_log("to:%s", argv[1]);
1434                                 ret = service_get_extra_data(data, SERVICE_DATA_CC, (char **)&argv[2]);
1435                                 debug_log("service_get_extra_data: %d", ret);
1436                                 debug_log("cc:%s", argv[2]);
1437                                 ret = service_get_extra_data(data, SERVICE_DATA_BCC, (char **)&argv[3]);
1438                                 debug_log("service_get_extra_data: %d", ret);
1439                                 debug_log("bcc:%s", argv[3]);
1440                                 ret = service_get_extra_data(data, SERVICE_DATA_SUBJECT, (char **)&argv[4]);
1441                                 debug_log("service_get_extra_data: %d", ret);
1442                                 debug_log("subject:%s", argv[4]);
1443                                 ret = service_get_extra_data(data, SERVICE_DATA_TEXT, (char **)&argv[5]);
1444                                 debug_log("service_get_extra_data: %d", ret);
1445                                 debug_log("body:%s", argv[5]);
1446                         }
1447                 } else {
1448                         int ret;
1449                         ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_ACCOUNT_ID, (char **)&argv[1]);
1450                         debug_log("service_get_extra_data: %d", ret);
1451                         debug_log("account_id:%s", argv[1]);
1452                         ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_MAILBOX, (char **)&argv[2]);
1453                         debug_log("service_get_extra_data: %d", ret);
1454                         debug_log("mailbox_id:%s", argv[2]);
1455                         ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_MAIL_ID, (char **)&argv[3]);
1456                         debug_log("service_get_extra_data: %d", ret);
1457                         debug_log("mail_id:%s", argv[3]);
1458                 }
1459
1460                 switch (ugd->composer_type) {
1461                 case RUN_COMPOSER_NEW:
1462                         if (argv[1])
1463                                 ugd->account_info->account_id = atoi(argv[1]);
1464
1465                         ugd->save_drafts = 0;
1466                         break;
1467                 case RUN_COMPOSER_EDIT:
1468                 case RUN_COMPOSER_REPLY_ALL:
1469                 case RUN_COMPOSER_REPLY:
1470                 case RUN_COMPOSER_FORWARD:
1471                         if (argv[1])
1472                                 ugd->account_info->account_id = atoi(argv[1]);
1473
1474                         if (argv[2])
1475                                 ugd->mailbox_info->mailbox_id = atoi(argv[2]);
1476
1477                         if (argv[3])
1478                                 ugd->nExistingMailID = atoi(argv[3]);
1479
1480                         ugd->save_drafts = 1;
1481                         debug_log("");
1482
1483                         break;
1484                 case RUN_COMPOSER_EXTERNAL:
1485                         if (argv[1] != NULL && strlen(argv[1]) != 0) {
1486                                 ugd->new_mail_info->mail_data->full_address_to = _composer_parse_recipients_arg(ugd, argv[1]);
1487                         }
1488                         if (argv[2] != NULL && strlen(argv[2]) != 0) {
1489                                 ugd->new_mail_info->mail_data->full_address_cc = _composer_parse_recipients_arg(ugd, argv[2]);
1490                         }
1491                         if (argv[3] != NULL && strlen(argv[3]) != 0) {
1492                                 ugd->new_mail_info->mail_data->full_address_bcc = _composer_parse_recipients_arg(ugd, argv[3]);
1493                         }
1494                         if (argv[4] != NULL && strlen(argv[4]) != 0)
1495                                 ugd->new_mail_info->mail_data->subject = g_strdup(argv[4]);
1496                         if (argv[5] != NULL && strlen(argv[5]) != 0) {
1497                                 ugd->new_mail_info->mail_data->file_path_plain = g_strdup(argv[5]);
1498                                 ugd->new_mail_info->mail_data->body_download_status = 1;
1499                         }
1500
1501                         ugd->save_drafts = 1;
1502
1503                         break;
1504                 default:
1505                         debug_log("[email-composer] unknown composer type!!");
1506                         return COMPOSER_ERROR_UNKOWN_TYPE;
1507                         break;
1508                 }
1509
1510                 if (ugd->composer_type == RUN_COMPOSER_EDIT) {
1511                         debug_log("Change state to READ");
1512                         email_set_flags_field(ugd->account_info->account_id, &ugd->nExistingMailID, 1, EMAIL_FLAGS_SEEN_FIELD, 1, 1);
1513                 }
1514
1515                 int i;
1516                 for (i = 0; i < 7;i++) {
1517                         if (argv[i])
1518                                 g_free(argv[i]);
1519                 }
1520
1521                 return COMPOSER_ERROR_NONE;
1522
1523         } else {
1524                 if (operation == NULL) {        /* ug called by ug_create */
1525                         debug_log("ug called by ug_create");
1526                         debug_log("Invaild argument!!");
1527                         return COMPOSER_ERROR_INVALID_ARG;
1528                 } else {        /* ug called by appcontrol request */
1529                         debug_log("ug called by appcontrol request");
1530
1531                         /* default run type: RUN_COMPOSER_EXTERNAL */
1532                         ugd->composer_type = RUN_COMPOSER_EXTERNAL;
1533
1534                         int ret;
1535                         ret = service_get_extra_data(data, SERVICE_DATA_TO, (char **)&argv[1]);
1536                         debug_log("service_get_extra_data: %d", ret);
1537                         debug_log("to:%s", argv[1]);
1538                         ret = service_get_extra_data(data, SERVICE_DATA_CC, (char **)&argv[2]);
1539                         debug_log("service_get_extra_data: %d", ret);
1540                         debug_log("cc:%s", argv[2]);
1541                         ret = service_get_extra_data(data, SERVICE_DATA_BCC, (char **)&argv[3]);
1542                         debug_log("service_get_extra_data: %d", ret);
1543                         debug_log("bcc:%s", argv[3]);
1544                         ret = service_get_extra_data(data, SERVICE_DATA_SUBJECT, (char **)&argv[4]);
1545                         debug_log("service_get_extra_data: %d", ret);
1546                         debug_log("subject:%s", argv[4]);
1547                         ret = service_get_extra_data(data, SERVICE_DATA_TEXT, (char **)&argv[5]);
1548                         debug_log("service_get_extra_data: %d", ret);
1549                         debug_log("body:%s", argv[5]);
1550
1551                         if (argv[1] != NULL && strlen(argv[1]) != 0) {
1552                                 ugd->new_mail_info->mail_data->full_address_to = _composer_parse_recipients_arg(ugd, argv[1]);
1553                         }
1554                         if (argv[2] != NULL && strlen(argv[2]) != 0) {
1555                                 ugd->new_mail_info->mail_data->full_address_cc = _composer_parse_recipients_arg(ugd, argv[2]);
1556                         }
1557                         if (argv[3] != NULL && strlen(argv[3]) != 0) {
1558                                 ugd->new_mail_info->mail_data->full_address_bcc = _composer_parse_recipients_arg(ugd, argv[3]);
1559                         }
1560                         if (argv[4] != NULL && strlen(argv[4]) != 0)
1561                                 ugd->new_mail_info->mail_data->subject = g_strdup(argv[4]);
1562                         if (argv[5] != NULL && strlen(argv[5]) != 0) {
1563                                 ugd->new_mail_info->mail_data->file_path_plain = g_strdup(argv[5]);
1564                                 ugd->new_mail_info->mail_data->body_download_status = 1;
1565                         }
1566
1567                         ugd->save_drafts = 1;
1568
1569                         int i;
1570                         for (i = 0; i < 7;i++) {
1571                                 if (argv[i])
1572                                         g_free(argv[i]);
1573                         }
1574
1575                         return COMPOSER_ERROR_NONE;
1576                 }
1577         }
1578 }
1579
1580 static void _composer_post_parse_bundle(EmailComposerUGD *ugd, service_h data)
1581 {
1582         debug_log("composer_type: %d", ugd->composer_type);
1583
1584         if (ugd->composer_type == RUN_COMPOSER_EXTERNAL) {
1585                 char *argv[1] = { 0, };
1586                 char *item_str;
1587                 gchar **vector;
1588                 int i = 0;
1589
1590                 if (data) {
1591                         int ret;
1592                         ret = service_get_uri(data, (char **)&argv[0]);
1593                         debug_log("service_get_uri: %d", ret);
1594                         debug_log("attachment:%s", argv[0]);
1595                         if (!argv[0]) {
1596                                 ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_ATTACHMENT, (char **)&argv[0]);
1597                                 debug_log("service_get_extra_data: %d", ret);
1598                                 debug_log("attachment:%s", argv[0]);
1599                         }
1600                 }
1601
1602                 if (argv[0] != NULL) {
1603                         Eina_List *list = NULL;
1604                         char tmp_file_path[MAX_PATH_LEN + 1] = { 0, };
1605                         char *file_ext = NULL;
1606
1607                         debug_log("str = %s", argv[0]);
1608
1609                         if (ecore_file_exists(argv[0]) && !ecore_file_is_dir(argv[0])) {
1610                                 if (email_drm_file_forward_lock_check(argv[0])) {
1611                                         if (ugd->composer_noti) {
1612                                                 evas_object_del(ugd->composer_noti);
1613                                                 ugd->composer_noti = NULL;
1614                                         }
1615                                         ugd->composer_noti = _composer_create_noti(ugd, false, dgettext("sys_string", "IDS_COM_POP_WARNING"),
1616                                                 _("IDS_EMAIL_POP_UNABLE_TO_FORWARD_DRM_CONTENTS"), 0, NULL, NULL, 2.0, _composer_noti_response_cb);
1617                                 } else {
1618                                         file_ext = _composer_get_file_ext(argv[0]);
1619                                         debug_log("file_ext:%s", file_ext);
1620
1621                                         if (strcasecmp(file_ext, "vcf") == 0 || strcasecmp(file_ext, "vcs") == 0) {
1622                                                 if (_composer_copy_temp_file(argv[0], tmp_file_path, MAX_PATH_LEN))
1623                                                         list = eina_list_append(list, tmp_file_path);
1624                                                 else
1625                                                         list = eina_list_append(list, argv[0]);
1626                                         } else
1627                                                 list = eina_list_append(list, argv[0]);
1628                                 }
1629                         } else {
1630                                 vector = g_strsplit_set(argv[0], ";\n", -1);
1631                                 if (vector == NULL) {
1632                                         debug_log("vector == NULL");
1633                                         return;
1634                                 }
1635
1636                                 guint attach_num = g_strv_length(vector);
1637                                 debug_log("attachment number: %d", attach_num);
1638
1639                                 for (i = 0; i < attach_num; i++) {
1640                                         item_str = g_strdup(vector[i]);
1641
1642                                         debug_log("item_str: %s", item_str);
1643
1644                                         if (g_strcmp0(item_str, "") != 0) {
1645                                                 if (email_drm_file_forward_lock_check((item_str))) {
1646                                                         if (ugd->composer_noti) {
1647                                                                 evas_object_del(ugd->composer_noti);
1648                                                                 ugd->composer_noti = NULL;
1649                                                         }
1650                                                         ugd->composer_noti = _composer_create_noti(ugd, false, dgettext("sys_string", "IDS_COM_POP_WARNING"),
1651                                                                 _("IDS_EMAIL_POP_UNABLE_TO_FORWARD_DRM_CONTENTS"), 0, NULL, NULL, 2.0, _composer_noti_response_cb);
1652
1653                                                         g_free(item_str);
1654                                                 } else {
1655                                                         file_ext = _composer_get_file_ext(item_str);
1656                                                         debug_log("file_ext:%s", file_ext);
1657
1658                                                         if (strcasecmp(file_ext, "vcf") == 0 || strcasecmp(file_ext, "vcs") == 0) {
1659                                                                 if (_composer_copy_temp_file(item_str, tmp_file_path, MAX_PATH_LEN))
1660                                                                         list = eina_list_append(list, tmp_file_path);
1661                                                                 else
1662                                                                         list = eina_list_append(list, item_str);
1663                                                         } else
1664                                                                 list = eina_list_append(list, item_str);
1665                                                 }
1666                                         } else {
1667                                                 g_free(item_str);
1668                                         }
1669                                 }
1670
1671                                 g_strfreev(vector);
1672                         }
1673                         _composer_attachment_create_list(ugd, list, EINA_FALSE);
1674
1675                         g_free(argv[0]);
1676                 }
1677         }
1678
1679         ////////////////////////////////////////////////////////////////////////////////////////////////////
1680         if (ugd->composer_type != RUN_TYPE_UNKNOWN) {
1681                 int att_cnt = 0;
1682
1683                 if (ugd->fw_attachment_list)
1684                         att_cnt = g_list_length(ugd->fw_attachment_list);
1685
1686                 if (elm_multibuttonentry_first_item_get(ugd->to_mbe) != NULL) {
1687                         if (g_strcmp0(elm_entry_entry_get(ugd->subject_entry), "") == 0) {
1688                                 debug_log("To field is not empty, setting focus to subject_entry field.");
1689                                 if (ugd->composer_type != RUN_COMPOSER_FORWARD || !ugd->account_info->account->options.forward_with_files || !att_cnt) {
1690                                         if (!_composer_check_popup_exist(ugd))
1691                                                 ugd->focus_timer = ecore_timer_add(0.1f, _composer_set_object_focus, ugd->subject_editfield);
1692                                 }
1693
1694                                 ugd->selected_entry = ugd->subject_entry;
1695                         } else if (ugd->body_ewkview) {
1696                                 debug_log("To field is not empty, setting focus to body_ewkview field.");
1697                                 if (ugd->composer_type != RUN_COMPOSER_FORWARD || !ugd->account_info->account->options.forward_with_files || !att_cnt) {
1698                                         if (!_composer_check_popup_exist(ugd))
1699                                                 ugd->focus_timer = ecore_timer_add(0.1f, _composer_set_object_focus, ugd->body_ewkview);
1700                                 }
1701
1702                                 ugd->selected_entry = ugd->body_ewkview;
1703                                 if (ewk_view_script_execute(ugd->body_ewkview, COMPOSER_JS_SET_FOCUS, _composer_focus_script_executed_cb, ugd) == EINA_FALSE)
1704                                         debug_log("COMPOSER_JS_SET_FOCUS error.");
1705                         }
1706                 } else {
1707                         debug_log("To field is empty, setting focus to 'To' field.");
1708                         if (ugd->composer_type != RUN_COMPOSER_FORWARD || !ugd->account_info->account->options.forward_with_files || !att_cnt) {
1709                                 if (!_composer_check_popup_exist(ugd))
1710                                         ugd->focus_timer = ecore_timer_add(0.2f, _composer_set_object_focus, ugd->to_mbe);
1711                         }
1712                 }
1713         }
1714 }
1715
1716 static Eina_Bool _composer_set_object_focus(void *data)
1717 {
1718         debug_log("");
1719
1720         Evas_Object *obj = (Evas_Object *)data;
1721
1722         if (obj == g_ugd->to_mbe) {
1723                 debug_log("Focus to TO");
1724                 elm_object_focus_set(g_ugd->to_mbe, EINA_TRUE);
1725                 g_ugd->selected_entry = elm_multibuttonentry_entry_get(obj);
1726         } else if (obj == g_ugd->subject_editfield) {
1727                 debug_log("Focus to SUBJECT");
1728                 elm_object_focus_set(g_ugd->subject_editfield, EINA_TRUE);
1729                 g_ugd->selected_entry = elm_object_part_content_get(obj, "elm.swallow.content");
1730         } else if (obj == g_ugd->body_ewkview) {
1731                 debug_log("Focus to BODY WEBKIT");
1732                 g_ugd->selected_entry = obj;
1733         }
1734
1735         if (g_ugd->focus_timer) {
1736                 ecore_timer_del(g_ugd->focus_timer);
1737                 g_ugd->focus_timer = NULL;
1738         }
1739
1740         return ECORE_CALLBACK_CANCEL;
1741 }
1742
1743 static void _composer_main_scroller_reach_top_cb(void *data, Evas_Object *obj, void *event_info)
1744 {
1745         debug_log("");
1746
1747         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1748
1749         if (ugd->ps_list)
1750                 return;
1751 }
1752
1753 static void _composer_main_scroller_reach_bottom_cb(void *data, Evas_Object *obj, void *event_info)
1754 {
1755         debug_log("");
1756
1757         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1758
1759         // It's workaround fixes to avoid a problem regarding too many signals of 'edge,bottom' for main_scroller.
1760         if (ugd->is_main_scroller_scrolling) {
1761                 debug_log("Main scroller hold push");
1762                 elm_object_scroll_freeze_push(ugd->main_scroller);
1763                 ewk_view_vertical_panning_hold_set(ugd->body_ewkview, EINA_FALSE);
1764                 ugd->is_main_scroller_scrolling = EINA_FALSE;
1765                 ugd->is_webview_scrolling = EINA_TRUE;
1766         }
1767 }
1768
1769 static void _composer_main_scroller_drag_start_cb(void *data, Evas_Object *obj, void *event_info)
1770 {
1771         debug_log("");
1772
1773         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1774
1775         ugd->is_main_scroller_scrolling = EINA_TRUE;
1776 }
1777
1778 static Eina_Bool _composer_register_scroller_callback(void *data)
1779 {
1780         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1781
1782         evas_object_smart_callback_add(ugd->main_scroller, "scroll,drag,start", _composer_main_scroller_drag_start_cb, ugd);
1783
1784         evas_object_smart_callback_add(ugd->main_scroller, "edge,top", _composer_main_scroller_reach_top_cb, ugd);
1785         evas_object_smart_callback_add(ugd->main_scroller, "edge,bottom", _composer_main_scroller_reach_bottom_cb, ugd);
1786
1787         return EINA_FALSE;
1788 }
1789
1790 static int _composer_dbus_receiver_setup(EmailComposerUGD *ugd)
1791 {
1792         debug_log("");
1793
1794         int err = COMPOSER_ERROR_NONE;
1795
1796         int ret = e_dbus_init();
1797         debug_log("ret: %d", ret);
1798
1799         DBusError derror;
1800
1801         if (_g_composer_dbus_conn == NULL) {
1802                 debug_log("");
1803                 dbus_error_init(&derror);
1804                 _g_composer_dbus_conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
1805
1806                 if (e_dbus_request_name(_g_composer_dbus_conn, "User.Email.NetworkStatus", 0, NULL, NULL) == NULL) {
1807                         debug_log("Failed to e_dbus_request_name()");
1808                         return COMPOSER_ERROR_DBUS_FAIL;
1809                 }
1810
1811                 if (_g_composer_signal_handler != NULL) {
1812                         debug_log("_g_composer_signal_handler != NULL");
1813                 }
1814
1815                 _g_composer_signal_handler = e_dbus_signal_handler_add(_g_composer_dbus_conn, NULL, "/User/Email/NetworkStatus", "User.Email.NetworkStatus", "email", _on_edbus_event_composer_receive, ugd);
1816
1817                 if (_g_composer_signal_handler == NULL) {
1818                         debug_log("Failed to e_dbus_signal_handler_add()");
1819                         return COMPOSER_ERROR_DBUS_FAIL;
1820                 }
1821         }
1822
1823         return err;
1824 }
1825
1826 static void _on_edbus_event_composer_receive(void *data, DBusMessage * message)
1827 {
1828         debug_log("");
1829
1830         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1831
1832         DBusError error;
1833
1834         if (dbus_message_is_signal(message, "User.Email.NetworkStatus", "email")) {
1835                 debug_log("User.Email.NetworkStatus");
1836
1837                 int subtype = 0;
1838                 int data1 = 0;
1839                 char *data2 = NULL;
1840                 int data3 = 0;
1841                 int data4 = 0;
1842                 dbus_error_init(&error);
1843
1844                 if (dbus_message_get_args(message, &error, DBUS_TYPE_INT32, &subtype, DBUS_TYPE_INT32, &data1, DBUS_TYPE_STRING, &data2, DBUS_TYPE_INT32, &data3, DBUS_TYPE_INT32, &data4, DBUS_TYPE_INVALID)) {
1845                         debug_log("subtype: %d, data1: %d, data2: %s, data3: %d, data4: %d", subtype, data1, data2, data3, data4);
1846
1847                         switch (subtype) {
1848                         case NOTI_DOWNLOAD_ATTACH_START:
1849                                 /* DATA1[mail_id] DATA2[file_id] DATA3[attachment_id] DATA4[percentage] */
1850                                 debug_log("receive noti, DOWNLOAD_ATTACHMENT");
1851                                 char buf[128] = { 0, };
1852
1853                                 debug_log("Download : %d / %d", data4 + (ugd->fw_dn_idx * 100), (ugd->fw_dn_total_cnt * 100));
1854                                 snprintf(buf, sizeof(buf), "%s [%d/%d]", _("IDS_EMAIL_POP_DOWNLOADING_ATTACHMENT_ING"), ugd->fw_dn_idx + 1, ugd->fw_dn_total_cnt);
1855                                 elm_object_text_set(ugd->fw_dn_label, buf);
1856                                 elm_progressbar_value_set(ugd->fw_dn_progress, (double)(data4 + (ugd->fw_dn_idx * 100)) / (ugd->fw_dn_total_cnt * 100));
1857                                 evas_render(evas_object_evas_get(ugd->main_layout));
1858
1859                                 break;
1860
1861                         case NOTI_DOWNLOAD_ATTACH_FINISH:
1862                                 /* DATA1[mail_id] DATA2[NULL] DATA3[attachment_id] */
1863                                 debug_log("receive noti, DOWNLOAD_ATTACH_FINISH");
1864
1865                                 if (ugd->fw_dn_cnt > 0) {
1866                                         ugd->fw_dn_cnt--;
1867
1868                                         if (ugd->fw_dn_cnt == 0) {
1869                                                 debug_log("Last Download");
1870
1871                                                 _on_edbus_popup_del(ugd);
1872
1873                                                 _composer_update_attachment_info(ugd, data3);
1874
1875                                                 if (ugd->need_download == EINA_TRUE) {
1876                                                         if (ugd->b_sending == true) {
1877                                                                 debug_log("send mail");
1878                                                                 ugd->bSendBtnDisabled = true;
1879                                                                 ugd->send_timer = ecore_timer_add(0.5, _composer_send_mail_cb, ugd);
1880                                                         } else {
1881                                                                 if (ugd->save_drafts == 1) {
1882                                                                         debug_log("draft mail");
1883                                                                         ugd->send_timer = ecore_timer_add(0.5, _composer_save_draft_mail, ugd);
1884                                                                 }
1885                                                         }                                                       
1886                                                         ugd->need_download = EINA_FALSE;
1887                                                 }
1888                                         } else {
1889                                                 debug_log("Download Not Finished");
1890
1891                                                 char buf[128] = { 0, };
1892
1893                                                 snprintf(buf, sizeof(buf), "%s [%d/%d]", _("IDS_EMAIL_POP_DOWNLOADING_ATTACHMENT_ING"), ugd->fw_dn_idx + 1, ugd->fw_dn_total_cnt);
1894                                                 elm_object_text_set(ugd->fw_dn_label, buf);
1895
1896                                                 _composer_update_attachment_info(ugd, data3);
1897
1898                                                 ugd->fw_dn_idx++;
1899                                         }
1900                                 }
1901
1902                                 break;
1903
1904                         case NOTI_DOWNLOAD_ATTACH_FAIL:
1905                                 /* DATA1[mail_id] DATA2[NULL] DATA3[attachment_id] */
1906                                 debug_log("receive noti, DOWNLOAD_ATTACH_FAIL");
1907
1908                                 if (ugd->dn_prog_popup) {
1909                                         if (data4 != EMAIL_ERROR_CANCELLED) {
1910                                                 _on_edbus_popup_del(ugd);
1911
1912                                                 char fail_msg[512] = { 0, };
1913                                                 char *err_msg = _composer_get_service_fail_type(data4);
1914                                                 snprintf(fail_msg, sizeof(fail_msg), "%s<br>%s", _("IDS_EMAIL_POP_UNABLE_TO_DOWNLOAD"), err_msg);
1915
1916                                                 if (ugd->composer_noti) {
1917                                                         evas_object_del(ugd->composer_noti);
1918                                                         ugd->composer_noti = NULL;
1919                                                 }
1920                                                 ugd->composer_noti = _composer_create_noti(ugd, false, dgettext("sys_string", "IDS_COM_POP_WARNING"), fail_msg, 1,
1921                                                         dgettext("sys_string", "IDS_COM_SK_OK"), NULL, 0.0, _composer_noti_response_cb);
1922
1923                                                 free(err_msg);
1924                                         }
1925                                 }
1926
1927                                 break;
1928
1929                         default:
1930                                 debug_log("unknown type");
1931                                 break;
1932                         }
1933
1934                 } else {
1935                         debug_log("receive data error: %s", error.message);
1936                         dbus_error_free(&error);
1937                 }
1938
1939                 return;
1940         }
1941
1942         return;
1943 }
1944
1945 static void _on_edbus_remove_receiver(EmailComposerUGD *ugd)
1946 {
1947         debug_log("");
1948
1949         if (_g_composer_signal_handler != NULL) {
1950                 e_dbus_signal_handler_del(_g_composer_dbus_conn, _g_composer_signal_handler);
1951                 _g_composer_signal_handler = NULL;
1952         }
1953
1954         if (_g_composer_dbus_conn != NULL) {
1955                 _g_composer_dbus_conn = NULL;
1956         }
1957 }
1958
1959 static Eina_Bool _on_edbus_popup_del(void *data)
1960 {
1961         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1962
1963         if (ugd->dn_prog_popup) {
1964                 evas_object_del(ugd->dn_prog_popup);
1965                 ugd->dn_prog_popup = NULL;
1966         }
1967
1968         return EINA_FALSE;
1969 }
1970