Merged the latest code
[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->attachment_inline_item_list) {
727                 eina_list_free(ugd->attachment_inline_item_list);
728                 ugd->attachment_inline_item_list = 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->inline_cnt = 0;
938         ugd->attachment_inline_item_list = NULL;
939
940         ugd->b_load_finished = EINA_FALSE;
941         ugd->need_download = EINA_FALSE;
942
943         Elm_Theme *th = elm_theme_new();
944         elm_theme_ref_set(th, NULL);
945         ugd->th = th;
946         elm_theme_extension_add(ugd->th, COMPOSER_EDJ_NAME);
947
948         if (ethumb_init() != EINA_TRUE) {
949                 debug_log("Fail to ethumb_init()");
950         }
951
952         ugd->composer_type = RUN_TYPE_UNKNOWN;
953
954         ugd->mailbox_info = (EmailComposerMailbox *) calloc(1, sizeof(EmailComposerMailbox));
955
956         ugd->existing_mail_info = (EmailComposerMail *)calloc(1, sizeof(EmailComposerMail));
957         memset(ugd->existing_mail_info, 0x00, sizeof(EmailComposerMail));
958
959         ugd->new_mail_info = (EmailComposerMail *)calloc(1, sizeof(EmailComposerMail));
960         memset(ugd->new_mail_info, 0x00, sizeof(EmailComposerMail));
961         ugd->new_mail_info->mail_data = (email_mail_data_t *) calloc(1, sizeof(email_mail_data_t));
962
963         ugd->account_info = (EmailComposerAccount *) calloc(1, sizeof(EmailComposerAccount));
964         memset(ugd->account_info, 0x00, sizeof(EmailComposerMail));
965
966         ugd->priority_option = EMAIL_MAIL_PRIORITY_NORMAL;
967 }
968
969 static int _composer_init_service(void *data)
970 {
971         debug_log("");
972
973         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
974
975         COMPOSER_ERROR_TYPE_E ret = COMPOSER_ERROR_NONE;
976
977         debug_log("contact service begin");
978         contacts_connect2();
979
980         /* DBUS */
981         if (_composer_dbus_receiver_setup(ugd) < 0)
982                 return COMPOSER_ERROR_DBUS_FAIL;
983
984         if (_composer_create_temp_folder() < 0)
985                 return COMPOSER_ERROR_SERVICE_INIT_FAIL;
986
987         _composer_init_data(ugd);
988
989         return ret;
990 }
991
992 static void _composer_finish_service(void *data)
993 {
994         debug_log("");
995
996         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
997
998         ethumb_shutdown();
999         _composer_delete_evas_objects(ugd);
1000
1001         _composer_free_email_info(ugd);
1002
1003         email_engine_finalize();
1004
1005         debug_log("contact service end");
1006         contacts_disconnect2();
1007
1008         _on_edbus_remove_receiver(ugd);
1009
1010         int ret = e_dbus_shutdown();
1011         debug_log("ret: %d", ret);
1012
1013         /* deinitialize ewk */
1014         debug_log("has_body_html(%d), composer_type(%d), saved_html_path(%s)", ugd->has_body_html, ugd->composer_type, ugd->saved_html_path);
1015         if (EINA_TRUE == ugd->has_body_html) {
1016                 if (ugd->saved_html_path) {
1017                         g_free(ugd->saved_html_path);
1018                         ugd->saved_html_path = NULL;
1019                 }
1020                 if (ugd->latest_info_content) {
1021                         g_free(ugd->latest_info_content);
1022                         ugd->latest_info_content = NULL;
1023                 }
1024                 if (ugd->latest_html_content) {
1025                         g_free(ugd->latest_html_content);
1026                         ugd->latest_html_content = NULL;
1027                 }
1028                 if (ugd->plain_content) {
1029                         g_free(ugd->plain_content);
1030                         ugd->plain_content = NULL;
1031                 }
1032                 if (ugd->original_info_content) {
1033                         g_free(ugd->original_info_content);
1034                         ugd->original_info_content = NULL;
1035                 }
1036                 if (ugd->original_html_content) {
1037                         g_free(ugd->original_html_content);
1038                         ugd->original_html_content = NULL;
1039                 }
1040         }
1041
1042         _composer_remove_temp_folder();
1043 }
1044
1045 static void _composer_delete_evas_objects(EmailComposerUGD *ugd)
1046 {
1047         debug_log("");
1048
1049         _composer_attachment_reset(ugd);
1050
1051         if (ugd->main_layout) {
1052                 evas_object_del(ugd->main_layout);
1053                 ugd->main_layout = NULL;
1054         }
1055
1056         if (ugd->bg) {
1057                 evas_object_del(ugd->bg);
1058                 ugd->bg = NULL;
1059         }
1060
1061         if (ugd->th) {
1062                 elm_theme_extension_del(ugd->th, COMPOSER_EDJ_NAME);
1063                 elm_theme_free(ugd->th);
1064                 ugd->th = NULL;
1065         }
1066
1067         if (ugd->idler_save_draft) {
1068                 debug_log("delete idler_save_draft");
1069                 ecore_idler_del(ugd->idler_save_draft);
1070                 ugd->idler_save_draft = NULL;
1071         }
1072
1073         if (ugd->send_timer) {
1074                 debug_log("delete send_timer");
1075                 ecore_timer_del(ugd->send_timer);
1076                 ugd->send_timer = NULL;
1077         }
1078
1079         if (ugd->launch_timer) {
1080                 debug_log("delete launch_timer");
1081                 ecore_timer_del(ugd->launch_timer);
1082                 ugd->launch_timer = NULL;
1083         }
1084
1085         _composer_delete_all_popup(ugd);
1086
1087         if (ugd->ps_list) {
1088                 elm_genlist_clear(ugd->ps_list);
1089                 evas_object_del(ugd->ps_list);
1090                 ugd->ps_list = NULL;
1091         }
1092
1093         if (ugd->from_ly) {
1094                 evas_object_del(ugd->from_ly);
1095                 ugd->from_ly = NULL;
1096         }
1097
1098         if (ugd->to_ly) {
1099                 evas_object_del(ugd->to_ly);
1100                 ugd->to_ly = NULL;
1101         }
1102
1103         if (ugd->cc_ly) {
1104                 evas_object_del(ugd->cc_ly);
1105                 ugd->cc_ly = NULL;
1106         }
1107
1108         if (ugd->bcc_ly) {
1109                 evas_object_del(ugd->bcc_ly);
1110                 ugd->bcc_ly = NULL;
1111         }
1112
1113         if (ugd->subject_entry) {
1114                 evas_object_del(ugd->subject_entry);
1115                 ugd->subject_entry = NULL;
1116         }
1117
1118         if (ugd->body_ewkview) {
1119                 evas_object_del(ugd->body_ewkview);
1120                 ugd->body_ewkview = NULL;
1121         }
1122
1123         if (ugd->fw_attachment_list) {
1124                 debug_log("Free the existing attachments..");
1125                 int i;
1126                 for (i = 0; i < g_list_length(ugd->fw_attachment_list); ++i) {
1127                         EMAIL_ATTACHMENT_INFO_S *info = (EMAIL_ATTACHMENT_INFO_S *) g_list_nth_data(ugd->fw_attachment_list, i);
1128                         if (info->name)
1129                                 free(info->name);
1130                         if (info->path)
1131                                 free(info->path);
1132                 }
1133
1134                 g_list_free(ugd->fw_attachment_list);
1135                 ugd->fw_attachment_list = NULL;
1136         }
1137 }
1138
1139 static void _composer_delete_all_popup(EmailComposerUGD *ugd)
1140 {
1141         debug_log("");
1142
1143         if (ugd->popup_list == NULL)
1144                 return;
1145
1146         Evas_Object * popup;
1147         EINA_LIST_FREE(ugd->popup_list, popup) {
1148                 if (popup)
1149                         evas_object_del(popup);
1150         }
1151 }
1152
1153 static void _composer_free_email_info(EmailComposerUGD *ugd)
1154 {
1155         debug_log("");
1156
1157         if (ugd->account_info) {
1158                 debug_log("free account_info: account_id(%d), account_count(%d)", ugd->account_info->account_id, ugd->account_info->account_count);
1159                 if (ugd->account_info->account_list) {
1160                         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);
1161
1162                         if (ugd->account_info->account_name) {
1163                                 debug_log("free account_name(%s)", ugd->account_info->account_name);
1164                                 free(ugd->account_info->account_name);
1165                                 ugd->account_info->account_name = NULL;
1166                         }
1167
1168                         email_free_account(&(ugd->account_info->account_list), ugd->account_info->account_count);
1169                 }
1170
1171                 if (ugd->account_info->account) {
1172                         debug_log("free account: account_name(%s), account_id(%d)", ugd->account_info->account->account_name, ugd->account_info->account->account_id);
1173                         email_free_account(&(ugd->account_info->account), 1);
1174                 }
1175
1176                 free(ugd->account_info);
1177                 ugd->account_info = NULL;
1178         }
1179
1180         if (ugd->mailbox_info) {
1181                 email_free_mailbox(&(ugd->mailbox_info->mail_box), 1);
1182                 free(ugd->mailbox_info);
1183         }
1184
1185         if (ugd->existing_mail_info) {
1186                 if (ugd->existing_mail_info->attachment_list) {
1187                         email_free_attachment_data(&ugd->existing_mail_info->attachment_list, ugd->existing_mail_info->mail_data->attachment_count);
1188                         ugd->existing_mail_info->attachment_list = NULL;
1189                 }
1190
1191                 if (ugd->existing_mail_info->mail_data)
1192                         email_free_mail_data(&(ugd->existing_mail_info->mail_data), 1);
1193
1194                 free(ugd->existing_mail_info);
1195                 ugd->existing_mail_info = NULL;
1196         }
1197
1198         if (ugd->new_mail_info) {
1199                 if (ugd->new_mail_info->attachment_list) {
1200                         email_free_attachment_data(&ugd->new_mail_info->attachment_list, ugd->new_mail_info->mail_data->attachment_count);
1201                         ugd->new_mail_info->attachment_list = NULL;
1202                 }
1203
1204                 if (ugd->new_mail_info->mail_data)
1205                         email_free_mail_data(&(ugd->new_mail_info->mail_data), 1);
1206
1207                 free(ugd->new_mail_info);
1208                 ugd->new_mail_info = NULL;
1209         }
1210
1211         if (ugd->addrs_info_list) {
1212                 debug_log("free addrs_info_list");
1213                 email_free_address_info_list(&ugd->addrs_info_list);
1214         }
1215 }
1216
1217 static Eina_Bool _composer_launch_email_app_cb(void *data)
1218 {
1219         debug_log("");
1220
1221         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1222
1223         int ret;
1224         service_h service = NULL;
1225         ret = service_create(&service);
1226         debug_log("service_create: %d", ret);
1227         if (!service) {
1228                 debug_log("service create failed");
1229         }
1230         ret = service_add_extra_data(service, EMAIL_BUNDLE_KEY_RUN_TYPE, "9");
1231         debug_log("service_add_extra_data: %d", ret);
1232         ret = service_set_package(service, PKGNAME);
1233         debug_log("service_set_package: %d", ret);
1234         ret = service_send_launch_request(service, NULL, NULL);
1235         debug_log("service_send_launch_request: %d", ret);
1236         ret = service_destroy(service);
1237         debug_log("service_destroy: %d", ret);
1238
1239         if (ugd->launch_timer) {
1240                 debug_log("delete launch_timer");
1241                 ecore_timer_del(ugd->launch_timer);
1242                 ugd->launch_timer = NULL;
1243         }
1244
1245         return ECORE_CALLBACK_CANCEL;
1246 }
1247
1248 static void _composer_popup_warning(EmailComposerUGD *ugd, char *header, char *content)
1249 {
1250         debug_log("");
1251
1252         Evas_Object *pu;
1253
1254         pu = elm_popup_add(ugd->win_main);
1255         if (!pu)
1256                 return;
1257
1258         if (header) {
1259                 elm_object_part_text_set(pu, "title,text", header);
1260         }
1261
1262         if (content) {
1263                 elm_object_text_set(pu, content);
1264         }
1265         elm_popup_timeout_set(pu, 2.0);
1266
1267         evas_object_smart_callback_add(pu, "timeout", _composer_ug_destroy_cb, ugd);
1268         evas_object_smart_callback_add(pu, "block,clicked", _composer_ug_destroy_cb, ugd);
1269
1270         evas_object_show(pu);
1271
1272         ugd->composer_noti = pu;
1273 }
1274
1275 static void _composer_ug_destroy_cb(void *data, Evas_Object *obj, void *event_info)
1276 {
1277         debug_log("");
1278
1279         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1280
1281         if (ugd->composer_noti) {
1282                 evas_object_del(ugd->composer_noti);
1283                 ugd->composer_noti = NULL;
1284         }
1285
1286         ug_destroy_me(ugd->ug_main);
1287 }
1288
1289 char *_composer_parse_recipients_arg(EmailComposerUGD *ugd, const char *source)
1290 {
1291         debug_log("");
1292
1293         int i = 0;
1294         char buf[MAX_RECIPIENT_ADDRESSES_LEN] = { 0, };
1295
1296         char *item_str;
1297
1298         char *display_name = NULL;
1299         char email_addr[MAX_RECIPIENT_ADDRESS_LEN];
1300
1301         gchar **vector;
1302         gchar **vector_sub;
1303         int written_size = 0;
1304
1305         /* Newly allocated array of strings returned */
1306         vector = g_strsplit_set(source, ";", -1);
1307         if (vector == NULL) {
1308                 debug_log("vector == NULL");
1309                 return NULL;
1310         }
1311
1312         guint recipients_num = g_strv_length(vector);
1313         debug_log("recipient number: %d", recipients_num);
1314
1315         for (i = 0; i < recipients_num; i++) {
1316                 memset(email_addr, 0x00, sizeof(email_addr));
1317                 item_str = vector[i];
1318
1319                 if (strlen(item_str) > MAX_RECIPIENT_ADDRESS_LEN) {
1320                         if (ugd->composer_noti) {
1321                                 evas_object_del(ugd->composer_noti);
1322                                 ugd->composer_noti = NULL;
1323                         }
1324                         ugd->composer_noti = _composer_create_noti(ugd, false, dgettext("sys_string", "IDS_COM_POP_WARNING"), _("IDS_EMAIL_POP_MAXIMUM_RECIPIENT_EMAIL_LENGTH_EXCEEDED"),
1325                                 0, NULL, NULL, 2.0, _composer_noti_response_cb);
1326
1327                         continue;
1328                 }
1329
1330                 if (g_strcmp0(item_str, "") != 0) {
1331                         /* Newly allocated array of strings returned */
1332                         vector_sub = g_strsplit_set(item_str, "<>", -1);
1333                         if (g_strv_length(vector_sub) > 2) {
1334                                 display_name = vector_sub[1];
1335                                 written_size = snprintf(email_addr, sizeof(email_addr), "%s", vector_sub[2]);
1336                                 if (written_size > sizeof(email_addr)) {
1337                                         debug_warning("Email address data truncated");
1338                                 }
1339
1340                                 if (i == 0) {
1341                                         written_size = snprintf(buf, sizeof(buf), "<%s> %s;", display_name, email_addr);
1342                                                 if (written_size > sizeof(buf)) {
1343                                                 debug_warning("Display name data truncated");
1344                                         }
1345                                 } else {
1346                                         written_size = snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "<%s> %s;", display_name, email_addr);
1347                                         if (written_size > sizeof(buf)) {
1348                                                 debug_warning("Display name data truncated");
1349                                         }
1350                                 }
1351
1352                         }
1353
1354                         if (g_strv_length(vector_sub) == 1) {
1355                                 display_name = vector_sub[0];
1356                                 written_size = snprintf(email_addr, sizeof(email_addr), "%s", vector_sub[0]);
1357
1358                                 if (written_size > sizeof(email_addr)) {
1359                                         debug_warning("Email address data truncated");
1360                                 }
1361
1362                                 if (i == 0) {
1363                                         written_size = snprintf(buf, sizeof(buf), "%s;", email_addr);
1364                                         if (written_size > sizeof(buf)) {
1365                                                 debug_warning("Display name data truncated");
1366                                         }
1367                                 } else {
1368                                         written_size = snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%s;", email_addr);
1369                                         if (written_size > sizeof(buf)) {
1370                                                 debug_warning("Display name data truncated");
1371                                         }
1372                                 }
1373                         }
1374
1375                         /* allocated array of strings freed */
1376                         g_strfreev(vector_sub);
1377                 }
1378         }
1379
1380         /* allocated array of strings freed */
1381         g_strfreev(vector);
1382
1383         debug_log("buf = %s", buf);
1384
1385         return g_strdup(buf);
1386 }
1387
1388 static int _composer_pre_parse_bundle(EmailComposerUGD *ugd, service_h data)
1389 {
1390         debug_log("");
1391
1392         char *argv[7] = { 0, };
1393
1394         if (data) {
1395                 int ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_RUN_TYPE, (char **)&argv[0]);
1396                 debug_log("service_get_extra_data: %d", ret);
1397                 debug_log("argv[0]:%s", argv[0]);
1398         }
1399
1400         int ret;
1401         char *operation = NULL;
1402         ret = service_get_operation(data, &operation);
1403         debug_log("service_get_operation: %d", ret);
1404         debug_log("operation = %s", operation);
1405
1406         if (email_engine_get_default_account(&ugd->account_info->account_id) == false)
1407                 return COMPOSER_ERROR_NO_DEFAULT_ACCOUNT;
1408
1409         if (argv[0]) {
1410                 debug_log("argv[0] = %s", argv[0]);
1411
1412                 ugd->composer_type = atoi(argv[0]);
1413                 debug_log("composer type = %d", ugd->composer_type);
1414
1415                 if (ugd->composer_type == RUN_COMPOSER_EXTERNAL) {
1416                         if (operation == NULL) {        /* ug called by ug_create */
1417                                 debug_log("ug called by ug_create");
1418                                 ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_TO, (char **)&argv[1]);
1419                                 debug_log("service_get_extra_data: %d", ret);
1420                                 debug_log("to:%s", argv[1]);
1421                                 ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_CC, (char **)&argv[2]);
1422                                 debug_log("service_get_extra_data: %d", ret);
1423                                 debug_log("cc:%s", argv[2]);
1424                                 ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_BCC, (char **)&argv[3]);
1425                                 debug_log("service_get_extra_data: %d", ret);
1426                                 debug_log("bcc:%s", argv[3]);
1427                                 ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_SUBJECT, (char **)&argv[4]);
1428                                 debug_log("service_get_extra_data: %d", ret);
1429                                 debug_log("subject:%s", argv[4]);
1430                                 ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_BODY, (char **)&argv[5]);
1431                                 debug_log("service_get_extra_data: %d", ret);
1432                                 debug_log("body:%s", argv[5]);
1433
1434                                 ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_ATTACHMENT, (char **)&argv[6]);
1435                                 debug_log("service_get_extra_data: %d", ret);
1436                                 debug_log("attachment:%s", argv[6]);
1437                                 if (argv[6] && !argv[5]) {
1438                                         if (g_str_has_prefix(argv[6], "http://")) {
1439                                                 argv[5] = g_strdup(argv[6]);
1440                                                 debug_log("body:%s", argv[5]);
1441                                         }
1442                                 }
1443                         } else {        /* ug called by appcontrol request */
1444                                 debug_log("ug called by appcontrol request");
1445                                 ret = service_get_extra_data(data, SERVICE_DATA_TO, (char **)&argv[1]);
1446                                 debug_log("service_get_extra_data: %d", ret);
1447                                 debug_log("to:%s", argv[1]);
1448                                 ret = service_get_extra_data(data, SERVICE_DATA_CC, (char **)&argv[2]);
1449                                 debug_log("service_get_extra_data: %d", ret);
1450                                 debug_log("cc:%s", argv[2]);
1451                                 ret = service_get_extra_data(data, SERVICE_DATA_BCC, (char **)&argv[3]);
1452                                 debug_log("service_get_extra_data: %d", ret);
1453                                 debug_log("bcc:%s", argv[3]);
1454                                 ret = service_get_extra_data(data, SERVICE_DATA_SUBJECT, (char **)&argv[4]);
1455                                 debug_log("service_get_extra_data: %d", ret);
1456                                 debug_log("subject:%s", argv[4]);
1457                                 ret = service_get_extra_data(data, SERVICE_DATA_TEXT, (char **)&argv[5]);
1458                                 debug_log("service_get_extra_data: %d", ret);
1459                                 debug_log("body:%s", argv[5]);
1460
1461                                 ret = service_get_uri(data, (char **)&argv[6]);
1462                                 debug_log("service_get_uri: %d", ret);
1463                                 debug_log("uri:%s", argv[6]);
1464                                 if (argv[6] && !argv[5]) {
1465                                         if (g_str_has_prefix(argv[6], "http://")) {
1466                                                 argv[5] = g_strdup(argv[6]);
1467                                                 debug_log("body:%s", argv[5]);
1468                                         }
1469                                 }
1470                         }
1471                 } else {
1472                         int ret;
1473                         ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_ACCOUNT_ID, (char **)&argv[1]);
1474                         debug_log("service_get_extra_data: %d", ret);
1475                         debug_log("account_id:%s", argv[1]);
1476                         ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_MAILBOX, (char **)&argv[2]);
1477                         debug_log("service_get_extra_data: %d", ret);
1478                         debug_log("mailbox_id:%s", argv[2]);
1479                         ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_MAIL_ID, (char **)&argv[3]);
1480                         debug_log("service_get_extra_data: %d", ret);
1481                         debug_log("mail_id:%s", argv[3]);
1482                 }
1483
1484                 switch (ugd->composer_type) {
1485                 case RUN_COMPOSER_NEW:
1486                         if (argv[1])
1487                                 ugd->account_info->account_id = atoi(argv[1]);
1488
1489                         ugd->save_drafts = 0;
1490                         break;
1491
1492                 case RUN_COMPOSER_EDIT:
1493                 case RUN_COMPOSER_REPLY_ALL:
1494                 case RUN_COMPOSER_REPLY:
1495                 case RUN_COMPOSER_FORWARD:
1496                         if (argv[1])
1497                                 ugd->account_info->account_id = atoi(argv[1]);
1498                         if (argv[2])
1499                                 ugd->mailbox_info->mailbox_id = atoi(argv[2]);
1500                         if (argv[3])
1501                                 ugd->nExistingMailID = atoi(argv[3]);
1502
1503                         ugd->save_drafts = 1;
1504                         break;
1505
1506                 case RUN_COMPOSER_EXTERNAL:
1507                         if (argv[1] != NULL && strlen(argv[1]) != 0) {
1508                                 ugd->new_mail_info->mail_data->full_address_to = _composer_parse_recipients_arg(ugd, argv[1]);
1509                         }
1510                         if (argv[2] != NULL && strlen(argv[2]) != 0) {
1511                                 ugd->new_mail_info->mail_data->full_address_cc = _composer_parse_recipients_arg(ugd, argv[2]);
1512                         }
1513                         if (argv[3] != NULL && strlen(argv[3]) != 0) {
1514                                 ugd->new_mail_info->mail_data->full_address_bcc = _composer_parse_recipients_arg(ugd, argv[3]);
1515                         }
1516                         if (argv[4] != NULL && strlen(argv[4]) != 0)
1517                                 ugd->new_mail_info->mail_data->subject = g_strdup(argv[4]);
1518                         if (argv[5] != NULL && strlen(argv[5]) != 0) {
1519                                 ugd->new_mail_info->mail_data->file_path_plain = g_strdup(argv[5]);
1520                                 ugd->new_mail_info->mail_data->body_download_status = 1;
1521                         }
1522
1523                         ugd->save_drafts = 1;
1524                         break;
1525
1526                 default:
1527                         debug_log("[email-composer] unknown composer type!!");
1528                         return COMPOSER_ERROR_UNKOWN_TYPE;
1529                         break;
1530                 }
1531
1532                 if (ugd->composer_type == RUN_COMPOSER_EDIT) {
1533                         debug_log("Change state to READ");
1534                         email_set_flags_field(ugd->account_info->account_id, &ugd->nExistingMailID, 1, EMAIL_FLAGS_SEEN_FIELD, 1, 1);
1535                 }
1536
1537                 int i;
1538                 for (i = 0; i < 7;i++) {
1539                         if (argv[i])
1540                                 g_free(argv[i]);
1541                 }
1542
1543                 return COMPOSER_ERROR_NONE;
1544
1545         } else {
1546                 if (operation == NULL) {        /* ug called by ug_create */
1547                         debug_log("ug called by ug_create");
1548                         debug_log("Invaild argument!!");
1549                         return COMPOSER_ERROR_INVALID_ARG;
1550                 } else {        /* ug called by appcontrol request */
1551                         debug_log("ug called by appcontrol request");
1552
1553                         /* default run type: RUN_COMPOSER_EXTERNAL */
1554                         ugd->composer_type = RUN_COMPOSER_EXTERNAL;
1555
1556                         int ret;
1557                         ret = service_get_extra_data(data, SERVICE_DATA_TO, (char **)&argv[1]);
1558                         debug_log("service_get_extra_data: %d", ret);
1559                         debug_log("to:%s", argv[1]);
1560                         ret = service_get_extra_data(data, SERVICE_DATA_CC, (char **)&argv[2]);
1561                         debug_log("service_get_extra_data: %d", ret);
1562                         debug_log("cc:%s", argv[2]);
1563                         ret = service_get_extra_data(data, SERVICE_DATA_BCC, (char **)&argv[3]);
1564                         debug_log("service_get_extra_data: %d", ret);
1565                         debug_log("bcc:%s", argv[3]);
1566                         ret = service_get_extra_data(data, SERVICE_DATA_SUBJECT, (char **)&argv[4]);
1567                         debug_log("service_get_extra_data: %d", ret);
1568                         debug_log("subject:%s", argv[4]);
1569                         ret = service_get_extra_data(data, SERVICE_DATA_TEXT, (char **)&argv[5]);
1570                         debug_log("service_get_extra_data: %d", ret);
1571                         debug_log("body:%s", argv[5]);
1572
1573                         if (argv[1] != NULL && strlen(argv[1]) != 0) {
1574                                 ugd->new_mail_info->mail_data->full_address_to = _composer_parse_recipients_arg(ugd, argv[1]);
1575                         }
1576                         if (argv[2] != NULL && strlen(argv[2]) != 0) {
1577                                 ugd->new_mail_info->mail_data->full_address_cc = _composer_parse_recipients_arg(ugd, argv[2]);
1578                         }
1579                         if (argv[3] != NULL && strlen(argv[3]) != 0) {
1580                                 ugd->new_mail_info->mail_data->full_address_bcc = _composer_parse_recipients_arg(ugd, argv[3]);
1581                         }
1582                         if (argv[4] != NULL && strlen(argv[4]) != 0)
1583                                 ugd->new_mail_info->mail_data->subject = g_strdup(argv[4]);
1584                         if (argv[5] != NULL && strlen(argv[5]) != 0) {
1585                                 ugd->new_mail_info->mail_data->file_path_plain = g_strdup(argv[5]);
1586                                 ugd->new_mail_info->mail_data->body_download_status = 1;
1587                         }
1588
1589                         ugd->save_drafts = 1;
1590
1591                         int i;
1592                         for (i = 0; i < 7;i++) {
1593                                 if (argv[i])
1594                                         g_free(argv[i]);
1595                         }
1596
1597                         return COMPOSER_ERROR_NONE;
1598                 }
1599         }
1600 }
1601
1602 static void _composer_post_parse_bundle(EmailComposerUGD *ugd, service_h data)
1603 {
1604         debug_log("composer_type: %d", ugd->composer_type);
1605
1606         if (ugd->composer_type == RUN_COMPOSER_EXTERNAL) {
1607                 char *argv[1] = { 0, };
1608                 char *item_str;
1609                 gchar **vector;
1610                 int i = 0;
1611
1612                 if (data) {
1613                         int ret;
1614                         ret = service_get_uri(data, (char **)&argv[0]);
1615                         debug_log("service_get_uri: %d", ret);
1616                         debug_log("uri:%s", argv[0]);
1617                         if (!argv[0]) {
1618                                 ret = service_get_extra_data(data, EMAIL_BUNDLE_KEY_ATTACHMENT, (char **)&argv[0]);
1619                                 debug_log("service_get_extra_data: %d", ret);
1620                                 debug_log("attachment:%s", argv[0]);
1621                         }
1622                 }
1623
1624                 if (argv[0] != NULL) {
1625                 if (!g_str_has_prefix(argv[0], "http://")) {
1626                         Eina_List *list = NULL;
1627                         char tmp_file_path[MAX_PATH_LEN + 1] = { 0, };
1628                         char *file_ext = NULL;
1629
1630                         debug_log("str = %s", argv[0]);
1631
1632                         if (ecore_file_exists(argv[0]) && !ecore_file_is_dir(argv[0])) {
1633                                 if (email_drm_file_forward_lock_check(argv[0])) {
1634                                         if (ugd->composer_noti) {
1635                                                 evas_object_del(ugd->composer_noti);
1636                                                 ugd->composer_noti = NULL;
1637                                         }
1638                                         ugd->composer_noti = _composer_create_noti(ugd, false, dgettext("sys_string", "IDS_COM_POP_WARNING"),
1639                                                 _("IDS_EMAIL_POP_UNABLE_TO_FORWARD_DRM_CONTENTS"), 0, NULL, NULL, 2.0, _composer_noti_response_cb);
1640                                 } else {
1641                                         file_ext = _composer_get_file_ext(argv[0]);
1642                                         debug_log("file_ext:%s", file_ext);
1643
1644                                         if (strcasecmp(file_ext, "vcf") == 0 || strcasecmp(file_ext, "vcs") == 0) {
1645                                                 if (_composer_copy_temp_file(argv[0], tmp_file_path, MAX_PATH_LEN))
1646                                                         list = eina_list_append(list, tmp_file_path);
1647                                                 else
1648                                                         list = eina_list_append(list, argv[0]);
1649                                         } else
1650                                                 list = eina_list_append(list, argv[0]);
1651                                 }
1652                         } else {
1653                                 vector = g_strsplit_set(argv[0], ";\n", -1);
1654                                 if (vector == NULL) {
1655                                         debug_log("vector == NULL");
1656                                         return;
1657                                 }
1658
1659                                 guint attach_num = g_strv_length(vector);
1660                                 debug_log("attachment number: %d", attach_num);
1661
1662                                 for (i = 0; i < attach_num; i++) {
1663                                         item_str = g_strdup(vector[i]);
1664
1665                                         debug_log("item_str: %s", item_str);
1666
1667                                         if (g_strcmp0(item_str, "") != 0) {
1668                                                 if (email_drm_file_forward_lock_check((item_str))) {
1669                                                         if (ugd->composer_noti) {
1670                                                                 evas_object_del(ugd->composer_noti);
1671                                                                 ugd->composer_noti = NULL;
1672                                                         }
1673                                                         ugd->composer_noti = _composer_create_noti(ugd, false, dgettext("sys_string", "IDS_COM_POP_WARNING"),
1674                                                                 _("IDS_EMAIL_POP_UNABLE_TO_FORWARD_DRM_CONTENTS"), 0, NULL, NULL, 2.0, _composer_noti_response_cb);
1675
1676                                                         g_free(item_str);
1677                                                 } else {
1678                                                         file_ext = _composer_get_file_ext(item_str);
1679                                                         debug_log("file_ext:%s", file_ext);
1680
1681                                                         if (strcasecmp(file_ext, "vcf") == 0 || strcasecmp(file_ext, "vcs") == 0) {
1682                                                                 if (_composer_copy_temp_file(item_str, tmp_file_path, MAX_PATH_LEN))
1683                                                                         list = eina_list_append(list, tmp_file_path);
1684                                                                 else
1685                                                                         list = eina_list_append(list, item_str);
1686                                                         } else
1687                                                                 list = eina_list_append(list, item_str);
1688                                                 }
1689                                         } else {
1690                                                 g_free(item_str);
1691                                         }
1692                                 }
1693
1694                                 g_strfreev(vector);
1695                         }
1696                         _composer_attachment_create_list(ugd, list, EINA_FALSE);
1697
1698                         g_free(argv[0]);
1699                 }
1700                 }
1701         }
1702
1703         ////////////////////////////////////////////////////////////////////////////////////////////////////
1704         if (ugd->composer_type != RUN_TYPE_UNKNOWN) {
1705                 int att_cnt = 0;
1706
1707                 if (ugd->fw_attachment_list)
1708                         att_cnt = g_list_length(ugd->fw_attachment_list);
1709
1710                 if (elm_multibuttonentry_first_item_get(ugd->to_mbe) != NULL) {
1711                         if (g_strcmp0(elm_entry_entry_get(ugd->subject_entry), "") == 0) {
1712                                 debug_log("To field is not empty, setting focus to subject_entry field.");
1713                                 if (ugd->composer_type != RUN_COMPOSER_FORWARD || !ugd->account_info->account->options.forward_with_files || !att_cnt) {
1714                                         if (!_composer_check_popup_exist(ugd))
1715                                                 ugd->focus_timer = ecore_timer_add(0.1f, _composer_set_object_focus, ugd->subject_editfield);
1716                                 }
1717
1718                                 ugd->selected_entry = ugd->subject_entry;
1719                         } else if (ugd->body_ewkview) {
1720                                 debug_log("To field is not empty, setting focus to body_ewkview field.");
1721                                 if (ugd->composer_type != RUN_COMPOSER_FORWARD || !ugd->account_info->account->options.forward_with_files || !att_cnt) {
1722                                         if (!_composer_check_popup_exist(ugd))
1723                                                 ugd->focus_timer = ecore_timer_add(0.1f, _composer_set_object_focus, ugd->body_ewkview);
1724                                 }
1725
1726                                 ugd->selected_entry = ugd->body_ewkview;
1727                                 if (ewk_view_script_execute(ugd->body_ewkview, COMPOSER_JS_SET_FOCUS, _composer_focus_script_executed_cb, ugd) == EINA_FALSE)
1728                                         debug_log("COMPOSER_JS_SET_FOCUS error.");
1729                         }
1730                 } else {
1731                         debug_log("To field is empty, setting focus to 'To' field.");
1732                         if (ugd->composer_type != RUN_COMPOSER_FORWARD || !ugd->account_info->account->options.forward_with_files || !att_cnt) {
1733                                 if (!_composer_check_popup_exist(ugd))
1734                                         ugd->focus_timer = ecore_timer_add(0.2f, _composer_set_object_focus, ugd->to_mbe);
1735                         }
1736                 }
1737         }
1738 }
1739
1740 static Eina_Bool _composer_set_object_focus(void *data)
1741 {
1742         debug_log("");
1743
1744         Evas_Object *obj = (Evas_Object *)data;
1745
1746         if (obj == g_ugd->to_mbe) {
1747                 debug_log("Focus to TO");
1748                 elm_object_focus_set(g_ugd->to_mbe, EINA_TRUE);
1749                 g_ugd->selected_entry = elm_multibuttonentry_entry_get(obj);
1750         } else if (obj == g_ugd->subject_editfield) {
1751                 debug_log("Focus to SUBJECT");
1752                 elm_object_focus_set(g_ugd->subject_editfield, EINA_TRUE);
1753                 g_ugd->selected_entry = elm_object_part_content_get(obj, "elm.swallow.content");
1754         } else if (obj == g_ugd->body_ewkview) {
1755                 debug_log("Focus to BODY WEBKIT");
1756                 g_ugd->selected_entry = obj;
1757         }
1758
1759         if (g_ugd->focus_timer) {
1760                 ecore_timer_del(g_ugd->focus_timer);
1761                 g_ugd->focus_timer = NULL;
1762         }
1763
1764         return ECORE_CALLBACK_CANCEL;
1765 }
1766
1767 static void _composer_main_scroller_reach_top_cb(void *data, Evas_Object *obj, void *event_info)
1768 {
1769         debug_log("");
1770
1771         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1772
1773         if (ugd->ps_list)
1774                 return;
1775 }
1776
1777 static void _composer_main_scroller_reach_bottom_cb(void *data, Evas_Object *obj, void *event_info)
1778 {
1779         debug_log("");
1780
1781         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1782
1783         // It's workaround fixes to avoid a problem regarding too many signals of 'edge,bottom' for main_scroller.
1784         if (ugd->is_main_scroller_scrolling) {
1785                 debug_log("Main scroller hold push");
1786                 elm_object_scroll_freeze_push(ugd->main_scroller);
1787                 ewk_view_vertical_panning_hold_set(ugd->body_ewkview, EINA_FALSE);
1788                 ugd->is_main_scroller_scrolling = EINA_FALSE;
1789                 ugd->is_webview_scrolling = EINA_TRUE;
1790         }
1791 }
1792
1793 static void _composer_main_scroller_drag_start_cb(void *data, Evas_Object *obj, void *event_info)
1794 {
1795         debug_log("");
1796
1797         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1798
1799         ugd->is_main_scroller_scrolling = EINA_TRUE;
1800 }
1801
1802 static Eina_Bool _composer_register_scroller_callback(void *data)
1803 {
1804         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1805
1806         evas_object_smart_callback_add(ugd->main_scroller, "scroll,drag,start", _composer_main_scroller_drag_start_cb, ugd);
1807
1808         evas_object_smart_callback_add(ugd->main_scroller, "edge,top", _composer_main_scroller_reach_top_cb, ugd);
1809         evas_object_smart_callback_add(ugd->main_scroller, "edge,bottom", _composer_main_scroller_reach_bottom_cb, ugd);
1810
1811         return EINA_FALSE;
1812 }
1813
1814 static int _composer_dbus_receiver_setup(EmailComposerUGD *ugd)
1815 {
1816         debug_log("");
1817
1818         int err = COMPOSER_ERROR_NONE;
1819
1820         int ret = e_dbus_init();
1821         debug_log("ret: %d", ret);
1822
1823         DBusError derror;
1824
1825         if (_g_composer_dbus_conn == NULL) {
1826                 debug_log("");
1827                 dbus_error_init(&derror);
1828                 _g_composer_dbus_conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
1829
1830                 if (e_dbus_request_name(_g_composer_dbus_conn, "User.Email.NetworkStatus", 0, NULL, NULL) == NULL) {
1831                         debug_log("Failed to e_dbus_request_name()");
1832                         return COMPOSER_ERROR_DBUS_FAIL;
1833                 }
1834
1835                 if (_g_composer_signal_handler != NULL) {
1836                         debug_log("_g_composer_signal_handler != NULL");
1837                 }
1838
1839                 _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);
1840
1841                 if (_g_composer_signal_handler == NULL) {
1842                         debug_log("Failed to e_dbus_signal_handler_add()");
1843                         return COMPOSER_ERROR_DBUS_FAIL;
1844                 }
1845         }
1846
1847         return err;
1848 }
1849
1850 static void _on_edbus_event_composer_receive(void *data, DBusMessage * message)
1851 {
1852         debug_log("");
1853
1854         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1855
1856         DBusError error;
1857
1858         if (dbus_message_is_signal(message, "User.Email.NetworkStatus", "email")) {
1859                 debug_log("User.Email.NetworkStatus");
1860
1861                 int subtype = 0;
1862                 int data1 = 0;
1863                 char *data2 = NULL;
1864                 int data3 = 0;
1865                 int data4 = 0;
1866                 dbus_error_init(&error);
1867
1868                 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)) {
1869                         debug_log("subtype: %d, data1: %d, data2: %s, data3: %d, data4: %d", subtype, data1, data2, data3, data4);
1870
1871                         switch (subtype) {
1872                         case NOTI_DOWNLOAD_ATTACH_START:
1873                                 /* DATA1[mail_id] DATA2[file_id] DATA3[attachment_id] DATA4[percentage] */
1874                                 debug_log("receive noti, DOWNLOAD_ATTACHMENT");
1875                                 char buf[128] = { 0, };
1876
1877                                 debug_log("Download : %d / %d", data4 + (ugd->fw_dn_idx * 100), (ugd->fw_dn_total_cnt * 100));
1878                                 snprintf(buf, sizeof(buf), "%s [%d/%d]", _("IDS_EMAIL_POP_DOWNLOADING_ATTACHMENT_ING"), ugd->fw_dn_idx + 1, ugd->fw_dn_total_cnt);
1879                                 elm_object_text_set(ugd->fw_dn_label, buf);
1880                                 elm_progressbar_value_set(ugd->fw_dn_progress, (double)(data4 + (ugd->fw_dn_idx * 100)) / (ugd->fw_dn_total_cnt * 100));
1881                                 evas_render(evas_object_evas_get(ugd->main_layout));
1882
1883                                 break;
1884
1885                         case NOTI_DOWNLOAD_ATTACH_FINISH:
1886                                 /* DATA1[mail_id] DATA2[NULL] DATA3[attachment_id] */
1887                                 debug_log("receive noti, DOWNLOAD_ATTACH_FINISH");
1888
1889                                 if (ugd->fw_dn_cnt > 0) {
1890                                         ugd->fw_dn_cnt--;
1891
1892                                         if (ugd->fw_dn_cnt == 0) {
1893                                                 debug_log("Last Download");
1894
1895                                                 _on_edbus_popup_del(ugd);
1896
1897                                                 _composer_update_attachment_info(ugd, data3);
1898
1899                                                 if (ugd->need_download == EINA_TRUE) {
1900                                                         if (ugd->b_sending == true) {
1901                                                                 debug_log("send mail");
1902                                                                 ugd->bSendBtnDisabled = true;
1903                                                                 ugd->send_timer = ecore_timer_add(0.5, _composer_send_mail_cb, ugd);
1904                                                         } else {
1905                                                                 if (ugd->save_drafts == 1) {
1906                                                                         debug_log("draft mail");
1907                                                                         ugd->send_timer = ecore_timer_add(0.5, _composer_save_draft_mail, ugd);
1908                                                                 }
1909                                                         }
1910                                                         ugd->need_download = EINA_FALSE;
1911                                                 }
1912                                         } else {
1913                                                 debug_log("Download Not Finished");
1914
1915                                                 char buf[128] = { 0, };
1916
1917                                                 snprintf(buf, sizeof(buf), "%s [%d/%d]", _("IDS_EMAIL_POP_DOWNLOADING_ATTACHMENT_ING"), ugd->fw_dn_idx + 1, ugd->fw_dn_total_cnt);
1918                                                 elm_object_text_set(ugd->fw_dn_label, buf);
1919
1920                                                 _composer_update_attachment_info(ugd, data3);
1921
1922                                                 ugd->fw_dn_idx++;
1923                                         }
1924                                 }
1925
1926                                 break;
1927
1928                         case NOTI_DOWNLOAD_ATTACH_FAIL:
1929                                 /* DATA1[mail_id] DATA2[NULL] DATA3[attachment_id] */
1930                                 debug_log("receive noti, DOWNLOAD_ATTACH_FAIL");
1931
1932                                 if (ugd->dn_prog_popup) {
1933                                         if (data4 != EMAIL_ERROR_CANCELLED) {
1934                                                 _on_edbus_popup_del(ugd);
1935
1936                                                 char fail_msg[512] = { 0, };
1937                                                 char *err_msg = _composer_get_service_fail_type(data4);
1938                                                 snprintf(fail_msg, sizeof(fail_msg), "%s<br>%s", _("IDS_EMAIL_POP_UNABLE_TO_DOWNLOAD"), err_msg);
1939
1940                                                 if (ugd->composer_noti) {
1941                                                         evas_object_del(ugd->composer_noti);
1942                                                         ugd->composer_noti = NULL;
1943                                                 }
1944                                                 ugd->composer_noti = _composer_create_noti(ugd, false, dgettext("sys_string", "IDS_COM_POP_WARNING"), fail_msg, 1,
1945                                                         dgettext("sys_string", "IDS_COM_SK_OK"), NULL, 0.0, _composer_noti_response_cb);
1946
1947                                                 free(err_msg);
1948                                         }
1949                                 }
1950
1951                                 break;
1952
1953                         default:
1954                                 debug_log("unknown type");
1955                                 break;
1956                         }
1957
1958                 } else {
1959                         debug_log("receive data error: %s", error.message);
1960                         dbus_error_free(&error);
1961                 }
1962
1963                 return;
1964         }
1965
1966         return;
1967 }
1968
1969 static void _on_edbus_remove_receiver(EmailComposerUGD *ugd)
1970 {
1971         debug_log("");
1972
1973         if (_g_composer_signal_handler != NULL) {
1974                 e_dbus_signal_handler_del(_g_composer_dbus_conn, _g_composer_signal_handler);
1975                 _g_composer_signal_handler = NULL;
1976         }
1977
1978         if (_g_composer_dbus_conn != NULL) {
1979                 _g_composer_dbus_conn = NULL;
1980         }
1981 }
1982
1983 static Eina_Bool _on_edbus_popup_del(void *data)
1984 {
1985         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1986
1987         if (ugd->dn_prog_popup) {
1988                 evas_object_del(ugd->dn_prog_popup);
1989                 ugd->dn_prog_popup = NULL;
1990         }
1991
1992         return EINA_FALSE;
1993 }
1994