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