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