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