Merged the latest code
[apps/core/preloaded/email.git] / composer / src / email-composer-attachment.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <sys/stat.h>
18 #include <metadata_extractor.h>
19 #include <image_util.h>
20 #include <Ethumb.h>
21 #include <app.h>
22
23 #include "email-debug.h"
24 #include "email-composer.h"
25 #include "email-composer-util.h"
26 #include "email-composer-attachment.h"
27 #include "email-composer-filetype.h"
28 #include "email-composer-callback.h"
29 #include "email-composer-recipient.h"
30 #include "email-composer-js.h"
31
32 static int toggle = 0;
33 static Evas_Object *_g_attach_btn;
34
35 EmailComposerUGD *g_ugd;
36
37 static void _composer_attachment_delete_icon_clicked_cb(void *data, Evas_Object *obj, void *event_info);
38 static void _composer_attachment_contracted_item_clicked_cb(void *data, Evas_Object *obj, void *event_info);
39 static void _composer_attachment_unfocus(void *data, Evas_Object *obj, const char *emission, const char *source);
40 static void _composer_attachment_mouse_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
41
42 /**
43  * Returns total size of attachments including attachments, inline images and new files to be attached/inserted.
44  *
45  * @param [in] ugd composer ui gadget data
46  * @param [in] files_list contains 'to be attached/inserted' filepaths. NULL is passed if there is no new files
47  *
48  * @return total size or -1 incase of 'stat' error
49  */
50 int _composer_get_attachments_total_size(EmailComposerUGD *ugd)
51 {
52         debug_log("");
53
54         int attach_size = 0;
55         int inline_size = 0;
56         int total_attachments_size = 0;
57
58         struct stat file_info;
59         int return_stat;
60
61         Eina_Iterator *it = NULL;
62         Evas_Object *attachment_obj = NULL;
63         email_attachment_data_t *attachment_data = NULL;
64
65         it = eina_list_iterator_new(ugd->attachment_item_obj_list);
66
67         while (eina_iterator_next(it, (void **)&attachment_obj)) {
68
69                 attachment_data = evas_object_data_get(attachment_obj, "attachment_data");
70
71                 if (attachment_data) {
72
73                         if ((return_stat = stat(attachment_data->attachment_path, &file_info)) == -1) {
74                                 debug_error("stat Error(%d): %s", errno, strerror(errno));
75                                 attach_size += attachment_data->attachment_size;
76                                 continue;
77                         }
78
79                         debug_log("file size in byte : %d", file_info.st_size);
80
81                         attach_size += file_info.st_size;
82                 }
83         }
84
85         if (it) {
86                 eina_iterator_free(it);
87         }
88
89         if (EINA_TRUE == ugd->has_body_html) {
90
91                 inline_size = _composer_get_inline_images_size(ugd);
92                 if (inline_size < 0) {
93                         debug_log("Failed to get inline images total size");
94                         return COMPOSER_ERROR_INVALID_FILE_SIZE;
95                 }
96         }
97
98         debug_log("existing attachments size = %d, existing inline images size = %d", attach_size, inline_size);
99
100         total_attachments_size = inline_size + attach_size;
101
102         debug_log("total size = %d", total_attachments_size);
103
104         return total_attachments_size;
105 }
106
107 /**
108  * Returns total size of files
109  *
110  * @param [in] files_list contains file paths
111  *
112  * @return total size or -1 incase of 'stat' error
113  */
114 int _composer_get_files_size(Eina_List *files_list)
115 {
116         debug_log("");
117
118         if (!files_list)
119                 return COMPOSER_ERROR_NULL_POINTER;
120
121         Eina_List *list = files_list;
122         Eina_List *l = NULL;
123         char *recv = NULL;
124
125         int temp_size = 0;
126         int file_size = 0;
127
128         EINA_LIST_FOREACH(list, l, recv) {
129                 debug_log("File : %s", recv);
130
131                 if (ecore_file_exists(recv))
132                         file_size = ecore_file_size(recv);
133                 else
134                         file_size = 0;
135                 debug_log("file_size : %d", file_size);
136                 temp_size = temp_size + file_size;
137                 debug_log("total_size : %d", temp_size);
138         }
139
140         debug_log("Files total size : %d", temp_size);
141         return temp_size;
142 }
143
144 /**
145  * Returns total size of inline images in mail body
146  *
147  * @param [in] ugd composer ui gadget data
148  *
149  * @return total size or -1 incase of 'stat' error
150  */
151 int _composer_get_inline_images_size(EmailComposerUGD *ugd)
152 {
153         debug_log("");
154         int total_size = 0;
155
156         if (EINA_TRUE == ugd->has_body_html) {
157                 debug_log("attachment_inline_item_list count : %d", eina_list_count(ugd->attachment_inline_item_list));         
158                 if (eina_list_count(ugd->attachment_inline_item_list) > 0) {
159
160                         Eina_List *list = ugd->attachment_inline_item_list;
161                         Eina_List *l = NULL;
162                         email_attachment_data_t *attachment_data = NULL;
163                 
164                         Eina_List *inline_list = NULL;
165                 
166                         EINA_LIST_FOREACH(list, l, attachment_data) {
167                                 if (attachment_data != NULL) {
168                                         if (attachment_data->attachment_path != NULL) {
169                                                 debug_log("Inline image : %s", attachment_data->attachment_path);
170                                                 inline_list = eina_list_append(inline_list, attachment_data->attachment_path);                                                  
171                                         }
172                                 }
173                         }
174                         if (eina_list_count(inline_list) > 0 && (inline_list != NULL)) {
175                                 total_size = _composer_get_files_size(inline_list); // -1 on 'stat' error of inline image file
176                         }
177
178                         eina_list_free(inline_list);
179                 }
180         }
181
182         debug_log("Inline images total size : %d", total_size);
183         return total_size;
184 }
185
186 void _composer_attachment_reset(EmailComposerUGD *ugd)
187 {
188         debug_log("");
189
190         Eina_Iterator *it = NULL;
191         Evas_Object *attachment_obj = NULL;
192         email_attachment_data_t *attachment_data = NULL;
193
194         it = eina_list_iterator_new(ugd->attachment_item_obj_list);
195
196         while (eina_iterator_next(it, (void **)&attachment_obj)) {
197
198                 attachment_data = evas_object_data_get(attachment_obj, "attachment_data");
199
200                 if (attachment_data) {
201                         debug_log("attachment_data file name to be removed : %s", attachment_data->attachment_path);
202
203                         email_free_attachment_data(&attachment_data, 1);
204                         attachment_data = NULL;
205                 }
206
207                 elm_box_unpack(ugd->attachment_item_box, attachment_obj);
208                 evas_object_del(attachment_obj);
209         }
210
211         if (ugd->new_mail_info) {
212                 if (ugd->new_mail_info->mail_data) {
213                         ugd->new_mail_info->mail_data->attachment_count = 0;
214                 }
215         }
216
217         if (ugd->attachment_item_box) {
218                 evas_object_del(ugd->attachment_item_box);
219                 ugd->attachment_item_box = NULL;
220         }
221
222         if (it) {
223                 eina_iterator_free(it);
224         }
225
226         if (ugd->attachment_item_obj_list) {
227                 eina_list_free(ugd->attachment_item_obj_list);
228                 ugd->attachment_item_obj_list = NULL;
229         }
230
231         if (ugd->attachment_contracted_item) {
232                 elm_box_unpack(ugd->attachment_item_box, ugd->attachment_contracted_item);
233                 evas_object_del(ugd->attachment_contracted_item);
234                 ugd->attachment_contracted_item = NULL;
235         }
236 }
237
238 void _composer_attachment_create_list(EmailComposerUGD *ugd, Eina_List *attachment_list, Eina_Bool is_inline)
239 {
240         debug_log("");
241
242         if (attachment_list == NULL)
243                 return;
244
245         int nTobeAttachedCount = eina_list_count(attachment_list);
246         debug_log("nTobeAttachedCount : %d", nTobeAttachedCount);
247
248         if (nTobeAttachedCount <= 0)
249                 return;
250
251         int nAttachmentObjListCount = eina_list_count(ugd->attachment_item_obj_list);
252         debug_log("nAttachmentObjListCount : %d", nAttachmentObjListCount);
253
254         if (ugd->attachment_item_box == NULL) {
255                 Evas_Object *attachment_item_box;
256                 attachment_item_box = _composer_create_box(ugd->main_layout);
257                 elm_object_focus_allow_set(attachment_item_box, EINA_TRUE);
258                 elm_object_part_content_set(ugd->c_layout, "_attachment_field", attachment_item_box);
259                 ugd->attachment_item_box = attachment_item_box;
260         } else {
261                 _composer_attachment_expand_items(ugd);
262         }
263
264         bool bDuplicated = FALSE;
265         char *recv = NULL;
266         Eina_List *l = NULL;
267
268         struct stat file_info;
269         int return_stat;
270
271         email_attachment_data_t *attachment_data = NULL;
272
273         int nCount = nAttachmentObjListCount;
274         int total_attachments_size = _composer_get_attachments_total_size(ugd);
275
276         EINA_LIST_FOREACH(attachment_list, l, recv) {
277
278                 bDuplicated = _composer_attachment_duplicate_check(ugd, recv);
279
280                 if (bDuplicated) {
281                         debug_log("Attachment is duplicated");
282
283                         if (ugd->composer_noti) {
284                                 evas_object_del(ugd->composer_noti);
285                                 ugd->composer_noti = NULL;
286                         }
287                         ugd->composer_noti = _composer_create_noti(ugd, false, dgettext("sys_string", "IDS_COM_POP_WARNING"), _("IDS_EMAIL_POP_FILE_ALREADY_EXISTS"), 0, NULL, NULL, 2.0,
288                                 _composer_noti_response_cb);
289
290                         break;
291                 }
292         }
293
294         EINA_LIST_FOREACH(attachment_list, l, recv) {
295
296                 bDuplicated = _composer_attachment_duplicate_check(ugd, recv);
297
298                 if (bDuplicated)
299                         continue;
300
301                 if ((return_stat = stat(recv, &file_info)) == -1) {
302                         debug_error("stat Error(%d): %s", errno, strerror(errno));
303                         eina_list_free(attachment_list);
304                         attachment_list = NULL;
305
306                         if (ugd->composer_noti) {
307                                 evas_object_del(ugd->composer_noti);
308                                 ugd->composer_noti = NULL;
309                         }
310                         ugd->composer_noti = _composer_create_noti(ugd, false, dgettext("sys_string", "IDS_COM_POP_WARNING"),
311                                 _("IDS_EMAIL_POP_UNABLE_TO_ATTACH_FILE"), 0, NULL, NULL, 1.5, _composer_noti_response_cb);
312                         break;
313                 }
314
315                 total_attachments_size += file_info.st_size;
316
317                 if (total_attachments_size > MAX_ATTACHMENT_SIZE) {
318                         debug_log("Total size is over");
319
320                         char msg[MAX_STR_LEN] = { 0, };
321                         snprintf(msg, sizeof(msg), _("IDS_EMAIL_POP_UNABLE_TO_ATTACH_MAXIMUM_SIZE_OF_FILES_IS_PD_KB"), MAX_ATTACHMENT_SIZE / 1024);
322
323                         if (ugd->composer_noti) {
324                                 evas_object_del(ugd->composer_noti);
325                                 ugd->composer_noti = NULL;
326                         }
327                         ugd->composer_noti = _composer_create_noti(ugd, false, dgettext("sys_string", "IDS_COM_POP_WARNING"), msg, 0, NULL, NULL, 2.0,
328                                 _composer_noti_response_cb);
329
330                         eina_list_free(attachment_list);
331                         attachment_list = NULL;
332
333                         break;
334                 }
335
336                 if (nCount >= MAX_ATTACHMENT_ITEM) {
337                         debug_log("Total count is over");
338
339                         char msg[MAX_STR_LEN] = { 0, };
340                         snprintf(msg, sizeof(msg), _("IDS_EMAIL_POP_UNABLE_TO_ATTACH_MAXIMUM_NUMBER_OF_FILES_IS_PD"), MAX_ATTACHMENT_ITEM);
341
342                         if (ugd->composer_noti) {
343                                 evas_object_del(ugd->composer_noti);
344                                 ugd->composer_noti = NULL;
345                         }
346                         ugd->composer_noti = _composer_create_noti(ugd, false, dgettext("sys_string", "IDS_COM_POP_WARNING"), msg, 0, NULL, NULL, 2.0,
347                                 _composer_noti_response_cb);
348
349                         eina_list_free(attachment_list);
350                         attachment_list = NULL;
351
352                         break;
353                 }
354
355                 attachment_data = (email_attachment_data_t *)calloc(1, sizeof(email_attachment_data_t));
356
357                 gchar **tokens;
358                 tokens = g_strsplit(recv, "/", -1);
359
360                 attachment_data->attachment_name = COMPOSER_STRDUP(tokens[g_strv_length(tokens) - 1]);
361                 attachment_data->attachment_path = COMPOSER_STRDUP(recv);
362                 attachment_data->save_status = 1;
363                 attachment_data->attachment_size = file_info.st_size;
364
365                 if (is_inline)
366                         attachment_data->inline_content_status = 1;
367
368                 debug_log("attachment_name = %s, attachment_path = %s", attachment_data->attachment_name, attachment_data->attachment_path);
369
370                 g_strfreev(tokens);
371
372                 _composer_attachment_create_list_box(ugd, attachment_data);
373
374                 nCount++;
375         }
376
377         if (_composer_check_recipient_is_empty(ugd)) {
378                 if (!ugd->bSendBtnDisabled) {
379                         elm_object_disabled_set(ugd->send_btn, EINA_TRUE);
380                         elm_object_disabled_set(ugd->send_btm_btn, EINA_TRUE);
381                         ugd->bSendBtnDisabled = true;
382                 }
383         } else {
384                 if (ugd->bSendBtnDisabled) {
385                         elm_object_disabled_set(ugd->send_btn, EINA_FALSE);
386                         elm_object_disabled_set(ugd->send_btm_btn, EINA_FALSE);
387                         ugd->bSendBtnDisabled = false;
388                 }
389         }
390
391         eina_list_free(attachment_list);
392         attachment_list = NULL;
393
394         _composer_attachment_contract_items(ugd);
395 }
396
397 void _composer_attachment_create_list_box(EmailComposerUGD *ugd, email_attachment_data_t *attachment_data)
398 {
399         debug_log("file path: %s", attachment_data->attachment_path);
400
401         if (attachment_data->inline_content_status == EINA_FALSE) {
402                 g_ugd = ugd;
403
404                 Evas_Object *attachment_item = elm_layout_add(ugd->attachment_item_box);
405
406                 elm_layout_file_set(attachment_item, COMPOSER_EDJ_NAME, "layout.attachment");
407                 evas_object_size_hint_weight_set(attachment_item, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
408                 evas_object_size_hint_align_set(attachment_item, EVAS_HINT_FILL, EVAS_HINT_FILL);
409
410                 Evas_Object *delete_icon = NULL;
411
412                 _composer_attachment_add_thumbnail(ugd, attachment_item, attachment_data->attachment_path);
413                 delete_icon = _composer_attachment_add_delete_icon(ugd, attachment_item);
414                 _composer_attachment_add_filename(ugd, attachment_data, attachment_item);
415
416                 Evas_Object *attachment_base = elm_layout_add(ugd->attachment_item_box);
417                 elm_layout_theme_set(attachment_base, "layout", "application", "noindicator");
418                 evas_object_size_hint_weight_set(attachment_base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
419                 evas_object_size_hint_align_set(attachment_base, EVAS_HINT_FILL, EVAS_HINT_FILL);
420                 evas_object_show(attachment_base);
421                 elm_object_part_content_set(attachment_base, "elm.swallow.content", attachment_item);
422
423                 elm_box_pack_end(ugd->attachment_item_box, attachment_base);
424                 evas_object_show(attachment_base);
425
426                 evas_object_data_set(delete_icon, "delete_attachment", attachment_base);
427
428                 ugd->attachment_item_obj_list = eina_list_append(ugd->attachment_item_obj_list, attachment_base);
429                 _composer_attachment_set_attach_data(ugd, attachment_base, attachment_data);
430
431                 if (attachment_data->attachment_path) {
432                         edje_object_signal_callback_add(_EDJ(attachment_item), "clicked", "*", _composer_attachment_mouse_clicked, attachment_data);
433                 }
434                 edje_object_signal_callback_add(_EDJ(attachment_item), "clicked", "*", _composer_attachment_unfocus, ugd);
435         } else {
436                 Evas_Object *attachment_base = elm_layout_add(ugd->attachment_item_box);
437
438                 ugd->attachment_item_obj_list = eina_list_append(ugd->attachment_item_obj_list, attachment_base);
439                 _composer_attachment_set_attach_data(ugd, attachment_base, attachment_data);
440         }
441 }
442
443 void _composer_attachment_update_thumbnail(EmailComposerUGD *ugd, Evas_Object *parent, char *filePath)
444 {
445         debug_log("");
446
447         Evas_Object *thumb_nail;
448         const char *path = NULL;
449
450         thumb_nail = elm_object_part_content_get(parent, "attachment.thumbnail.icon");
451
452         if (thumb_nail != NULL) {
453                 evas_object_del(thumb_nail);
454                 thumb_nail = NULL;
455         }
456
457         if (ugd->attachment_list_compressed) {
458                 thumb_nail = elm_icon_add(parent);
459
460                 path = g_strdup(COMPOSER_ICON_DIR "/05_email_icon_attach_40x40.png");
461                 debug_log("path = %s", path);
462                 elm_image_file_set(thumb_nail, path, NULL);
463         } else {
464                 if (!filePath) {
465                         debug_log("savename = %s", filePath);
466
467                         thumb_nail = elm_icon_add(parent);
468
469                         path = g_strdup(MYFILE_IMGE_PATH "/myfile_icon_etc.png");
470                         elm_image_file_set(thumb_nail, path, NULL);
471                 } else {
472                         thumb_nail = _composer_attachment_make_thumbnail(ugd, filePath, parent);
473                 }
474         }
475
476         elm_object_part_content_set(parent, "attachment.thumbnail.icon", thumb_nail);
477         evas_object_show(thumb_nail);
478
479         if (path)
480                 g_free((char *)path);
481 }
482
483 void _composer_attachment_add_thumbnail(EmailComposerUGD *ugd, Evas_Object *parent, char *filePath)
484 {
485         debug_log("");
486
487         Evas_Object *thumb_nail;
488         const char *path = NULL;
489
490         if (ugd->attachment_list_compressed) {
491                 thumb_nail = elm_icon_add(parent);
492
493                 path = g_strdup(COMPOSER_ICON_DIR "/05_email_icon_attach_40x40.png");
494                 debug_log("path = %s", path);
495                 elm_image_file_set(thumb_nail, path, NULL);
496         } else {
497                 if (!filePath) {
498                         debug_log("savename = %s", filePath);
499
500                         thumb_nail = elm_icon_add(parent);
501
502                         path = g_strdup(MYFILE_IMGE_PATH "/myfile_icon_etc.png");
503                         elm_image_file_set(thumb_nail, path, NULL);
504                 } else {
505                         thumb_nail = _composer_attachment_make_thumbnail(ugd, filePath, parent);
506                 }
507         }
508
509         elm_object_part_content_set(parent, "attachment.thumbnail.icon", thumb_nail);
510         evas_object_show(thumb_nail);
511
512         if (path)
513                 g_free((char *)path);
514 }
515
516 Evas_Object *_composer_attachment_add_delete_icon(EmailComposerUGD *ugd, Evas_Object *parent)
517 {
518         debug_log("");
519
520         Evas_Object *delete_icon = elm_button_add(parent);
521         evas_object_show(delete_icon);
522
523         if (ugd->attachment_list_compressed) {
524                 _g_attach_btn = delete_icon;
525                 elm_object_style_set(delete_icon, "expand/closed");
526                 elm_object_part_content_set(parent, "attachment.delete.icon", delete_icon);
527                 toggle = 0;
528                 evas_object_smart_callback_add(delete_icon, "clicked", _composer_attachment_contracted_item_clicked_cb, ugd);
529         } else {
530                 elm_object_style_set(delete_icon, "minus");
531                 elm_object_part_content_set(parent, "attachment.delete.icon", delete_icon);
532
533                 evas_object_smart_callback_add(delete_icon, "clicked", _composer_attachment_delete_icon_clicked_cb, ugd);
534         }
535
536         return delete_icon;
537 }
538
539 void _composer_attachment_add_filename(EmailComposerUGD *ugd, email_attachment_data_t * attachment_data, Evas_Object *parent)
540 {
541         debug_log("");
542
543         int size = 0;
544         char *file_name;
545         char file_string[256] = { 0, };
546
547         if (ugd->attachment_list_compressed) {
548                 size = _composer_get_attachments_total_size(ugd);
549
550                 char temp_name[128] = { 0, };
551                 snprintf(temp_name, sizeof(temp_name), _("IDS_EMAIL_BODY_PD_ATTACHMENTS"), eina_list_count(ugd->attachment_item_obj_list));
552                 file_name = g_strdup(temp_name);
553         } else {
554                 if (ugd->new_mail_info->mail_data->file_path_html && (g_strcmp0(ugd->new_mail_info->mail_data->file_path_html, attachment_data->attachment_path) == 0)) {
555                         size = email_get_file_size(attachment_data->attachment_path);
556                         debug_log("Original html body size(%d)", size);
557                 } else {
558                         size = attachment_data->attachment_size;
559                         debug_log("Attachment size(%d)", size);
560                 }
561                 file_name = g_strdup(attachment_data->attachment_name);
562         }
563
564         char *file_size = (char *)email_get_file_size_string((guint64)size);
565
566         snprintf(file_string, sizeof(file_string), "%s (%s)", file_name, file_size);
567         edje_object_part_text_set(_EDJ(parent), "attachment.filename", file_string);
568
569         g_free(file_size);
570         g_free(file_name);
571 }
572
573 Evas_Object *_composer_attachment_make_thumbnail(EmailComposerUGD *ugd, char *filePath, Evas_Object *parent)
574 {
575         debug_log("");
576
577         Evas_Object *icon = NULL;
578         icon = elm_icon_add(parent);
579
580         char *path = NULL;
581         const char *key = NULL;
582
583         char *type = NULL;
584
585         debug_log("file path: %s", filePath);
586         efreet_mime_init();
587         char *m_type = (char *)efreet_mime_type_get(ecore_file_file_get(filePath));
588
589         if (m_type != NULL)
590                 type = g_strdup(m_type);
591
592         debug_log("mime type: %s", type);
593         efreet_mime_shutdown();
594
595         char thumb_path[MAX_ATTACHMENT_FILE_LEN + 1] = { 0, };
596
597         path = g_strdup(ATTACHMENT_ETC_FILE_PATH);
598
599         char *temp = g_strdup(filePath);
600         char *temp_filepath = g_strdup(filePath);
601         char *token = strtok(temp, "/");
602
603         if (temp)
604                 token = strtok(NULL, "/");
605
606         if (token) {
607                 if (type) {
608                         struct _mtd *mtd = (struct _mtd *)mtd_main;
609
610                         while (mtd && mtd->key) {
611                                 if (strncmp(type, mtd->key, strlen(type)) == 0) {
612                                         if (path) {
613                                                 free(path);
614                                                 path = NULL;
615                                         }
616
617                                         path = g_strdup(mtd->icon_path);
618                                         break;
619                                 }
620                                 mtd++;
621                         }
622
623                         debug_log("file path : %s", filePath);
624
625                         if (strncmp(type, ATTACHMENT_MEDIA_IMAGE, 5) == 0 || strncmp(type, ATTACHMENT_MEDIA_VIDEO, 5) == 0) {
626                                 int err = _composer_attachment_make_ethumb(filePath, thumb_path);
627
628                                 if (err != COMPOSER_ERROR_NONE)
629                                         goto FINISH_OFF;
630                         } else if (strncmp(type, ATTACHMENT_MEDIA_AUDIO, 5) == 0) {
631                                 metadata_extractor_h metadata = NULL;
632                                 int ret = METADATA_EXTRACTOR_ERROR_NONE;
633                                 void *artwork = NULL;
634                                 int artwork_size = 0;
635                                 char *artwork_mime = NULL;
636
637                                 ret = metadata_extractor_create(&metadata);
638                                 debug_log("metadata_extractor_create: %d", ret);
639                                 if (!metadata) {
640                                         debug_log("metadata extractor create failed");
641                                         return NULL;
642                                 }
643
644                                 ret = metadata_extractor_set_path(metadata, filePath);
645                                 debug_log("metadata_extractor_set_path: %d", ret);
646
647                                 ret = metadata_extractor_get_artwork(metadata, &artwork, &artwork_size, &artwork_mime);
648                                 debug_log("metadata_extractor_get_artwork: %d", ret);
649                                 debug_log("artwork_mime: %s, artwork_size: %d", artwork_mime, artwork_size);
650
651                                 if (artwork) {
652                                         gchar *mm_path = NULL;
653                                         int fd = g_file_open_tmp(NULL, &mm_path, NULL);
654
655                                         if (fd != -1) {
656                                                 FILE *fp = fdopen(fd, "w");
657                                                 if (fp == NULL) {
658                                                         debug_log("fail to fdopen()");
659                                                         close(fd);
660                                                 } else {
661                                                         if (fwrite((unsigned char *)artwork, 1, artwork_size, fp) != artwork_size/*1*/) {
662                                                                 debug_log("fail to fwrite()");
663                                                                 fclose(fp);
664                                                                 close(fd);
665                                                         } else {
666                                                                 fflush(fp);
667                                                                 fclose(fp);
668                                                                 close(fd);
669                                                         }
670                                                 }
671                                         }
672
673                                         snprintf(thumb_path, sizeof(thumb_path), "%s", mm_path);
674
675                                         debug_log("file : %s, album art_path : %s", filePath, thumb_path);
676
677                                         if (mm_path) {
678                                                 free(mm_path);
679                                                 mm_path = NULL;
680                                         }
681
682                                         g_free(artwork);
683                                 }
684
685                                 if (artwork_mime)
686                                         g_free(artwork_mime);
687
688                                 ret = metadata_extractor_destroy(metadata);
689                                 debug_log("metadata_extractor_destroy: %d", ret);
690                         }
691                 }
692
693                 token = strtok(temp_filepath, ".");
694                 if (token) {
695                         token = strtok(NULL, ".");
696
697                         if (g_strcmp0(token, ATTACHMENT_MEDIA_VCALENDAR) == 0) {
698                                 snprintf(thumb_path, sizeof(thumb_path), "%s", ATTACHMENT_VCALENDAR_FILE_PATH);
699                         } else if (g_strcmp0(token, ATTACHMENT_MEDIA_VCARD) == 0) {
700                                 snprintf(thumb_path, sizeof(thumb_path), "%s", ATTACHMENT_VCARD_FILE_PATH);
701                         }
702
703                         debug_log("thumb_path: %s", thumb_path);
704                 }
705
706                 if (strlen(thumb_path)) {
707                         elm_image_file_set(icon, thumb_path, NULL);
708                 } else {
709                         elm_image_file_set(icon, path, key);
710                 }
711
712         } else {
713                 if (type != NULL) {
714                         if (strncmp(type, ATTACHMENT_MEDIA_IMAGE, 5) == 0) {
715                                 _composer_make_default_thumbnail(icon, filePath, COMPOSER_ATTACHMENT_ITEM_IMAGE);
716                         } else if (strncmp(type, ATTACHMENT_MEDIA_AUDIO, 5) == 0) {
717                                 _composer_make_default_thumbnail(icon, filePath, COMPOSER_ATTACHMENT_ITEM_SOUND);
718                         } else if (strncmp(type, ATTACHMENT_MEDIA_VIDEO, 5) == 0) {
719                                 _composer_make_default_thumbnail(icon, filePath, COMPOSER_ATTACHMENT_ITEM_VIDEO);
720                         } else {
721                                 _composer_make_default_thumbnail(icon, filePath, COMPOSER_ATTACHMENT_ITEM_ETC);
722                         }
723                 }
724         }
725
726  FINISH_OFF:
727
728         if (path) {
729                 free(path);
730                 path = NULL;
731         }
732         if (type) {
733                 free(type);
734                 type = NULL;
735         }
736
737         if (temp) {
738                 free(temp);
739                 temp = NULL;
740         }
741         if (temp_filepath) {
742                 free(temp_filepath);
743                 temp_filepath = NULL;
744         }
745
746         return icon;
747 }
748
749 int _composer_attachment_make_ethumb(const char *source, char *target)
750 {
751         debug_log("");
752
753 /*      debug_log("file path: %s", source);
754         efreet_mime_init();
755         char *type = (char *)efreet_mime_type_get(ecore_file_file_get(source));
756         char *mime_type = g_strdup(type);
757         debug_log("mime type: %s", mime_type);
758         efreet_mime_shutdown();
759
760         if (mime_type) {
761                 if (strncmp(mime_type, "image", 5) == 0) {
762                         strncpy(target, source, MAX_ATTACHMENT_FILE_LEN);
763 #endif
764                 } else if (strncmp(mime_type, "video", 5) == 0) {*/
765                         metadata_extractor_h metadata = NULL;
766                         int ret = METADATA_EXTRACTOR_ERROR_NONE;
767                         char *video_width = NULL;
768                         char *video_height = NULL;
769                         char *video_track_cnt = NULL;
770                         void *video_thumbnail = NULL;
771                         int video_thumbnail_len = 0;
772                         int video_w = 0;
773                         int video_h = 0;
774
775                         ret = metadata_extractor_create(&metadata);
776                         debug_log("metadata_extractor_create: %d", ret);
777                         if (!metadata) {
778                                 debug_log("metadata extractor create failed");
779                                 return COMPOSER_ERROR_ETHUMB_FAIL;
780                         }
781
782                         ret = metadata_extractor_set_path(metadata, source);
783                         debug_log("metadata_extractor_set_path: %d", ret);
784
785                         ret = metadata_extractor_get_metadata(metadata, METADATA_VIDEO_WIDTH, &video_width);
786                         debug_log("metadata_extractor_get_metadata: %d [video_width:%s]", ret, video_width);
787                         ret = metadata_extractor_get_metadata(metadata, METADATA_VIDEO_HEIGHT, &video_height);
788                         debug_log("metadata_extractor_get_metadata: %d [video_height:%s]", ret, video_height);
789                         ret = metadata_extractor_get_metadata(metadata, METADATA_HAS_VIDEO, &video_track_cnt);
790                         debug_log("metadata_extractor_get_metadata: %d [video_track_cnt:%s]", ret, video_track_cnt);
791
792                         ret = metadata_extractor_get_frame(metadata, &video_thumbnail, &video_thumbnail_len);
793                         debug_log("metadata_extractor_get_frame: %d (video_thumbnail_len:%d)", ret, video_thumbnail_len);
794
795                         if (video_thumbnail) {
796                                 int mm_ret = 0;
797                                 char filename[MAX_ATTACHMENT_FILE_LEN] = { 0, };
798                                 char *file_name = NULL;
799                                 char *file_ext = NULL;
800
801                                 email_parse_get_filename_n_ext_from_path(source, &file_name, &file_ext);
802                                 snprintf(filename, sizeof(filename), "%s%s%s%s", EMAIL_TMP_FOLDER"/", file_name, file_ext, ".jpg");
803                                 g_free(file_name);
804                                 g_free(file_ext);
805
806                                 if (video_width)
807                                         video_w = atoi(video_width);
808                                 if (video_height)
809                                         video_h = atoi(video_height);
810                                 mm_ret = image_util_encode_jpeg(video_thumbnail,
811                                                                                                         video_w, video_h,
812                                                                                                         IMAGE_UTIL_COLORSPACE_RGB888, 70, filename);
813
814                                 if (ecore_file_exists(filename)) {
815                                         strncpy(target, filename, MAX_ATTACHMENT_FILE_LEN);
816                                         debug_log("file : %s, thumb_path : %s", source, target);
817                                 }
818                                 g_free(video_thumbnail);
819                         }
820
821                         if (video_width)
822                                 g_free(video_width);
823                         if (video_height)
824                                 g_free(video_height);
825                         if (video_track_cnt)
826                                 g_free(video_track_cnt);
827
828                         ret = metadata_extractor_destroy(metadata);
829                         debug_log("metadata_extractor_destroy: %d", ret);
830 /*              }
831         }
832
833         g_free(mime_type);
834 */
835         return COMPOSER_ERROR_NONE;
836 }
837
838 void _composer_ethumb_generate_cb(void *data, Ethumb *e, Eina_Bool success)
839 {
840         if (success == EINA_TRUE) {
841                 debug_log("Succeed in thumbnail generation");
842         } else {
843                 debug_log("Fail to create thumbnail");
844         }
845 }
846
847 void _composer_attachment_set_attach_data(EmailComposerUGD *ugd, Evas_Object *parent, email_attachment_data_t *attachment_data)
848 {
849         debug_log("");
850
851         evas_object_data_set(parent, "attachment_data", attachment_data);
852 }
853
854 static void _composer_attachment_delete_item_from_fw_attachment_list(EmailComposerUGD *ugd, email_attachment_data_t *attachment_data)
855 {
856         debug_log("");
857         int i = 0;
858         if(ugd == NULL) {
859                 debug_log("ugd == NULL");
860                 return;
861         }
862         debug_log("1. %d", g_list_length(ugd->fw_attachment_list));
863
864         for (i = 0; i < g_list_length(ugd->fw_attachment_list); ++i) {
865                 EMAIL_ATTACHMENT_INFO_S *info = (EMAIL_ATTACHMENT_INFO_S *) g_list_nth_data(ugd->fw_attachment_list, i);
866                 debug_log("%x", info);
867                 if (info) {
868                         debug_log("%d , %d", info->attach_id, attachment_data->attachment_id);
869                         if (info->attach_id == attachment_data->attachment_id) {
870                                 ugd->fw_attachment_list = g_list_remove(ugd->fw_attachment_list, info);
871                                 debug_log("2. %d", g_list_length(ugd->fw_attachment_list));
872                                 ugd->fw_dn_cnt = g_list_length(ugd->fw_attachment_list);
873                         }
874                 }
875         }
876
877         if (ugd->fw_dn_cnt == 0)
878                 ugd->need_download = EINA_FALSE;
879 }
880
881 static void _composer_attachment_delete_icon_clicked_cb(void *data, Evas_Object *obj, void *event_info)
882 {
883         debug_log("");
884
885         if (data == NULL)
886                 return;
887
888         if (obj == NULL)
889                 return;
890
891         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
892
893         if (EINA_FALSE == ewk_view_script_execute(ugd->body_ewkview, COMPOSER_JS_GET_IMAGE_LIST, _composer_get_image_list_cb, (void *)ugd)) {
894                 debug_log("COMPOSER_JS_GET_IMAGE_LIST error.");
895         }
896
897         Evas_Object *object = NULL;
898         object = evas_object_data_get(obj, "delete_attachment");
899
900         ugd->attachment_item_obj_list = eina_list_remove(ugd->attachment_item_obj_list, object);
901
902         email_attachment_data_t *attachment_data = NULL;
903         attachment_data = evas_object_data_get(object, "attachment_data");
904
905         _composer_attachment_delete_item_from_fw_attachment_list(ugd, attachment_data);
906
907         elm_box_unpack(ugd->attachment_item_box, object);
908         evas_object_del(object);
909
910         if (ugd->attachment_contracted_item) {
911                 Evas_Object *contracted_item = evas_object_data_get(ugd->attachment_contracted_item, "attachment_item");
912                 debug_log("contracted_item = %p", contracted_item);
913                 if (eina_list_count(ugd->attachment_item_obj_list) < 2) {
914                         elm_box_unpack(ugd->attachment_item_box, ugd->attachment_contracted_item);
915                         evas_object_del(ugd->attachment_contracted_item);
916                         ugd->attachment_contracted_item = NULL;
917                         ugd->attachment_list_compressed = FALSE;
918                         _composer_attachment_add_filename(ugd, attachment_data, contracted_item);
919                 } else {
920                         ugd->attachment_list_compressed = TRUE;
921                         _composer_attachment_add_filename(ugd, attachment_data, contracted_item);
922                         ugd->attachment_list_compressed = FALSE;
923                 }
924         }
925
926         if (ugd->attachment_item_obj_list == NULL) {
927                 ugd->selected_entry = ugd->priv_selected_entry;
928                 ugd->priv_selected_entry = NULL;
929         }
930
931         if (attachment_data) {
932                 debug_log("attachment_data file name = %s", attachment_data->attachment_path);
933
934                 email_free_attachment_data(&attachment_data, 1);
935                 attachment_data = NULL;
936         }
937
938         _composer_mbe_set_focus(ugd);
939
940         elm_box_recalculate(ugd->attachment_item_box);
941         elm_layout_sizing_eval(ugd->main_layout);
942 }
943
944 bool _composer_attachment_duplicate_check(EmailComposerUGD *ugd, char *pszAttachedFilePath)
945 {
946         debug_log("to be attached file path : %s", pszAttachedFilePath);
947
948         if (ugd == NULL)
949                 return FALSE;
950
951         bool bDuplicated = FALSE;
952
953         Eina_Iterator *it = NULL;
954         Evas_Object *attachment_obj = NULL;
955         email_attachment_data_t *attachment_data = NULL;
956
957         it = eina_list_iterator_new(ugd->attachment_item_obj_list);
958
959         while (eina_iterator_next(it, (void **)&attachment_obj)) {
960                 attachment_data = evas_object_data_get(attachment_obj, "attachment_data");
961
962                 debug_log("attachment_data->attachment_path = %s", attachment_data->attachment_path);
963                 if (attachment_data->attachment_path != NULL) {
964                         if (strcmp(pszAttachedFilePath, attachment_data->attachment_path) == 0) {
965                                 debug_log("Attached file path is duplicated");
966                                 bDuplicated = TRUE;
967                                 break;
968                         }
969                 }
970         }
971
972         if (it) {
973                 eina_iterator_free(it);
974         }
975
976         return bDuplicated;
977 }
978
979 void _composer_attachment_expand_items(EmailComposerUGD *ugd)
980 {
981         debug_log("");
982
983         if (ugd->attachment_list_compressed == EINA_FALSE)
984                 return;
985
986         Eina_List *list = NULL;
987         Evas_Object *attachment_item = NULL;
988
989         EINA_LIST_FOREACH(ugd->attachment_item_obj_list, list, attachment_item) {
990                 if (attachment_item) {
991                         elm_box_pack_end(ugd->attachment_item_box, attachment_item);
992                         evas_object_show(attachment_item);
993                 }
994         }
995
996         ugd->attachment_list_compressed = EINA_FALSE;
997
998         elm_box_recalculate(ugd->attachment_item_box);
999         elm_layout_sizing_eval(ugd->main_layout);
1000 }
1001
1002 void _composer_attachment_contract_items(EmailComposerUGD *ugd)
1003 {
1004         debug_log("");
1005
1006         int nAttachmentObjCount = eina_list_count(ugd->attachment_item_obj_list);
1007
1008         debug_log("attachment_count = %d", nAttachmentObjCount);
1009         debug_log("attachment_list_compressed = %d", ugd->attachment_list_compressed);
1010
1011         if (nAttachmentObjCount > 1 && !ugd->attachment_list_compressed) {
1012                 ugd->attachment_list_compressed = EINA_TRUE;
1013
1014                 Eina_List *list = NULL;
1015                 Evas_Object *attachment_item = NULL;
1016
1017                 EINA_LIST_FOREACH(ugd->attachment_item_obj_list, list, attachment_item) {
1018                         if (attachment_item) {
1019                                 elm_box_unpack(ugd->attachment_item_box, attachment_item);
1020                                 evas_object_hide(attachment_item);
1021                         }
1022                 }
1023
1024                 if (ugd->attachment_contracted_item) {
1025                         Evas_Object *contracted_item = evas_object_data_get(ugd->attachment_contracted_item, "attachment_item");
1026                         debug_log("contracted_item = %p", contracted_item);
1027                         _composer_attachment_add_filename(ugd, NULL, contracted_item);
1028                 } else {
1029                         _composer_attachment_create_contracted_box(ugd);
1030                 }
1031
1032                 if (toggle == 0) {
1033                         char buf[PATH_MAX];
1034                         snprintf(buf, sizeof(buf), "expand/closed");
1035                         elm_object_style_set(_g_attach_btn, buf);
1036                         toggle = 1;
1037                 }
1038         }
1039
1040         elm_box_recalculate(ugd->attachment_item_box);
1041         elm_layout_sizing_eval(ugd->main_layout);
1042 }
1043
1044 void _composer_attachment_create_contracted_box(EmailComposerUGD *ugd)
1045 {
1046         debug_log("");
1047
1048         Evas_Object *contracted_item = NULL;
1049         contracted_item = elm_layout_add(ugd->attachment_item_box);
1050
1051         elm_layout_file_set(contracted_item, COMPOSER_EDJ_NAME, "layout.attachment");
1052         evas_object_size_hint_weight_set(contracted_item, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1053         evas_object_size_hint_align_set(contracted_item, EVAS_HINT_FILL, EVAS_HINT_FILL);
1054
1055         _composer_attachment_add_thumbnail(ugd, contracted_item, NULL);
1056         _composer_attachment_add_delete_icon(ugd, contracted_item);
1057         _composer_attachment_add_filename(ugd, NULL, contracted_item);
1058
1059         Evas_Object *contracted_base = elm_layout_add(ugd->attachment_item_box);
1060         elm_layout_theme_set(contracted_base, "layout", "application", "noindicator");
1061         evas_object_size_hint_weight_set(contracted_base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1062         evas_object_size_hint_align_set(contracted_base, EVAS_HINT_FILL, EVAS_HINT_FILL);
1063
1064         evas_object_show(contracted_base);
1065         elm_object_part_content_set(contracted_base, "elm.swallow.content", contracted_item);
1066
1067         elm_box_pack_start(ugd->attachment_item_box, contracted_base);
1068
1069         ugd->attachment_contracted_item = contracted_base;
1070
1071         evas_object_show(contracted_base);
1072
1073         evas_object_data_set(ugd->attachment_contracted_item, "attachment_item", contracted_item);
1074 }
1075
1076 static void _composer_attachment_contracted_item_clicked_cb(void *data, Evas_Object *obj, void *event_info)
1077 {
1078         debug_log("");
1079
1080         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1081
1082         char buf[PATH_MAX];
1083         Evas_Object *btn = obj;
1084
1085         if (toggle == 1) {
1086                 /* expand button closed style */
1087                 snprintf(buf, sizeof(buf), "expand/opened");
1088                 elm_object_style_set(btn, buf);
1089
1090                 if (ugd->selected_entry == ugd->body_ewkview) {
1091                         debug_log("Webkit unfocused");
1092                         evas_object_focus_set(ugd->body_ewkview, EINA_FALSE);
1093                         if (ewk_view_script_execute(ugd->body_ewkview, COMPOSER_JS_SET_UNFOCUS, _composer_script_executed_cb, NULL) == EINA_FALSE)
1094                                 debug_log("COMPOSER_JS_SET_UNFOCUS failed.");
1095                 }
1096
1097                 elm_object_focus_set(obj, EINA_TRUE);
1098
1099                 _composer_attachment_expand_items(ugd);
1100
1101                 ugd->priv_selected_entry = ugd->selected_entry;
1102                 ugd->selected_entry = ugd->attachment_contracted_item;
1103
1104                 toggle = 0;
1105         } else {
1106                 /* expand button opened style */
1107                 snprintf(buf, sizeof(buf), "expand/closed");
1108                 elm_object_style_set(btn, buf);
1109
1110                 if (ugd->selected_entry == ugd->body_ewkview) {
1111                         debug_log("Webkit unfocused");
1112                         evas_object_focus_set(ugd->body_ewkview, EINA_FALSE);
1113                         if (ewk_view_script_execute(ugd->body_ewkview, COMPOSER_JS_SET_UNFOCUS, _composer_script_executed_cb, NULL) == EINA_FALSE)
1114                                 debug_log("COMPOSER_JS_SET_UNFOCUS failed.");
1115                 }
1116
1117                 elm_object_focus_set(obj, EINA_TRUE);
1118
1119                 _composer_attachment_contract_items(ugd);
1120
1121                 ugd->priv_selected_entry = ugd->selected_entry;
1122                 ugd->selected_entry = ugd->attachment_contracted_item;
1123
1124                 toggle = 1;
1125         }
1126 }
1127
1128 static void _composer_attachment_mouse_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
1129 {
1130         debug_log("");
1131
1132         email_attachment_data_t *item_list = (email_attachment_data_t *) data;
1133
1134         debug_log("savename = %s", item_list->attachment_path);
1135
1136         int ret;
1137         service_h service = NULL;
1138         ret = service_create(&service);
1139         debug_log("service_create: %d", ret);
1140         if (!service) {
1141                 debug_log("service create failed");
1142                 return;
1143         }
1144         ret = service_set_operation(service, SERVICE_OPERATION_VIEW);
1145         debug_log("service_set_operation: %d", ret);
1146         ret = service_set_uri(service, item_list->attachment_path);
1147         debug_log("service_set_uri: %d", ret);
1148         ret = service_add_extra_data(service, EMAIL_BUNDLE_KEY_VIDEO_PLAYER_LAUNCH_APP, "email");
1149         debug_log("service_add_extra_data: %d", ret);
1150         ret = service_send_launch_request(service, NULL, NULL);
1151         debug_log("service_send_launch_request: %d", ret);
1152         if (ret != SERVICE_ERROR_NONE) {
1153                 if (g_ugd->composer_noti) {
1154                         evas_object_del(g_ugd->composer_noti);
1155                         g_ugd->composer_noti = NULL;
1156                 }
1157                 g_ugd->composer_noti = _composer_create_noti(g_ugd, false, dgettext("sys_string", "IDS_COM_POP_WARNING"),
1158                         _("IDS_EMAIL_POP_THIS_ATTACHMENT_CANNOT_BE_DISPLAYED"), 0, NULL, NULL, 1.5, _composer_noti_response_cb);
1159         }
1160         ret = service_destroy(service);
1161         debug_log("service_destroy: %d", ret);
1162 }
1163
1164 static void _composer_attachment_unfocus(void *data, Evas_Object *obj, const char *emission, const char *source)
1165 {
1166         debug_log("");
1167
1168         EmailComposerUGD *ugd = (EmailComposerUGD *)data;
1169
1170         Evas_Object *mbe = NULL;
1171         if (ugd->selected_entry == ugd->to_mbe_entry)
1172                 mbe = ugd->to_mbe;
1173         else if (ugd->selected_entry == ugd->cc_mbe_entry)
1174                 mbe = ugd->cc_mbe;
1175         else if (ugd->selected_entry == ugd->bcc_mbe_entry)
1176                 mbe = ugd->bcc_mbe;
1177
1178         /* For unfocus TO mbe */
1179         if (ugd->to_mbe)
1180                 elm_object_focus_set(ugd->to_mbe, EINA_FALSE);
1181         if (ugd->cc_mbe)
1182                 elm_object_focus_set(ugd->cc_mbe, EINA_FALSE);
1183         if (ugd->bcc_mbe)
1184                 elm_object_focus_set(ugd->bcc_mbe, EINA_FALSE);
1185 }