2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://www.tizenopensource.org/license
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.
18 #include <sys/types.h>
24 #include <metadata_extractor.h>
25 #include <image_util.h>
26 #include <Efreet_Mime.h>
29 #include "email-viewer-utils.h"
30 #include "email-common-types.h"
31 #include "email-utils.h"
32 #include "email-engine.h"
33 #include "email-debug.h"
35 #define MYFILE_IMGE_PATH "/opt/apps/com.samsung.myfile/res/images"
36 #define MB_SVC_FILE_PATH_LEN_MAX 255
38 static void _thumbnail_generated_cb(void *data, Ethumb *e, Eina_Bool success);
41 char *category; /* Not used at present */
44 char *exe_path; /* Not used at present */
45 char *param; /* Not used at present */
46 int is_direct_launch; /* Not used at present */
47 int need_sort_param; /* Not used at present */
48 int having_a_thumbnail; /* Not used at present */
51 static struct _mtd mtd_main[] = {
52 {"ETC", "etc", MYFILE_IMGE_PATH "/myfile_icon_etc.png", NULL, NULL, TRUE, FALSE, FALSE},
53 {"Music", "audio/mpeg", IMGDIR "/U01_icon_music.png", "music", NULL, TRUE, TRUE, TRUE},
54 {"Music", "audio/x-wav", IMGDIR "/U01_icon_music.png", "music", NULL, TRUE, TRUE, TRUE},
55 {"Music", "audio/x-ms-wma", IMGDIR "/U01_icon_music.png", "music", NULL, TRUE, TRUE, TRUE},
56 {"Voice", "audio/AMR", IMGDIR "/U01_icon_music.png", "voicerecorder", NULL, TRUE, FALSE, FALSE},
57 {"Video", "video/x-msvideo", IMGDIR "/U01_icon_video.png", "video-player", NULL, TRUE, FALSE, TRUE},
58 {"Video", "video/mp4", IMGDIR "/U01_icon_video.png", "video-player", NULL, TRUE, FALSE, TRUE},
59 {"Video", "video/3gpp", IMGDIR "/U01_icon_video.png", "video-player", NULL, TRUE, FALSE, TRUE},
60 {"Video", "video/x-ms-asf", IMGDIR "/U01_icon_video.png", "video-player", NULL, TRUE, FALSE, TRUE},
61 {"Video", "video/x-ms-wmv", IMGDIR "/U01_icon_video.png", "video-player", NULL, TRUE, FALSE, TRUE},
62 {"PDF", "application/pdf", IMGDIR "/U01_icon_pdf.png", "documents", NULL, TRUE, FALSE, FALSE},
63 {"Doc", "application/msword", IMGDIR "/U01_icon_word.png", "documents", NULL, TRUE, FALSE, FALSE},
64 {"Doc", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", IMGDIR "/U01_icon_word.png", "documents", NULL, TRUE, FALSE, FALSE},
65 {"PPT", "application/vnd.ms-powerpoint", IMGDIR "/U01_icon_ppt.png", "documents", NULL, TRUE, FALSE, FALSE},
66 {"PPT", "application/vnd.openxmlformats-officedocument.presentationml.presentation", IMGDIR "/U01_icon_ppt.png", "documents", NULL, TRUE, FALSE, FALSE},
67 {"Text", "text/plain", IMGDIR "/U01_icon_text.png", "documents", NULL, TRUE, FALSE, FALSE},
68 {"Text", "text/html", IMGDIR "/U01_icon_html.png", "documents", NULL, TRUE, FALSE, FALSE},
69 {"Excel", "application/vnd.ms-excel", IMGDIR "/U01_icon_excel.png", "documents", NULL, TRUE, FALSE, FALSE},
70 {"Excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", IMGDIR "/U01_icon_excel.png", "documents", NULL, TRUE, FALSE, FALSE},
71 {"Flash", "application/x-shockwave-flash", IMGDIR "/U01_icon_swf.png", "flash-player", NULL, TRUE, FALSE, TRUE},
72 {"None", "video/x-flv", IMGDIR "/U01_icon_video.png", NULL, NULL, FALSE, FALSE, FALSE},
73 {"Java", "text/vnd.sun.j2me.app-descriptor", MYFILE_IMGE_PATH "/myfile_icon_java.png", NULL, NULL, FALSE, FALSE, FALSE},
74 {"Java", "application/x-java-archive", MYFILE_IMGE_PATH "/myfile_icon_java.png", NULL, NULL, FALSE, FALSE, FALSE},
75 {"SVG", "image/svg+xml", MYFILE_IMGE_PATH "/myfile_icon_svg.png", "svg-viewer", NULL, TRUE, FALSE, FALSE},
76 {"VCARD", "text/directory", MYFILE_IMGE_PATH "/myfile_icon_etc.png", NULL, NULL, FALSE, FALSE, FALSE},
77 {"VCALENDAR", "text/calendar", MYFILE_IMGE_PATH "/myfile_icon_etc.png", NULL, NULL, FALSE, FALSE, FALSE},
78 {NULL, NULL, NULL, NULL}
81 static void _popup_response_cb(void *data, Evas_Object *obj, void *event_info)
86 debug_log("data is NULL");
90 EmailViewerUGD *ug_data = (EmailViewerUGD *)data;
92 if (ug_data->notify) {
93 evas_object_del(ug_data->notify);
94 ug_data->notify = NULL;
98 Evas_Object *viewer_make_thumbnail(char *path, Evas_Object *parent)
102 if (parent == NULL) {
103 debug_log("parent obj is NULL");
107 Evas_Object *icon = NULL;
108 icon = elm_icon_add(parent);
110 if (STR_INVALID(path)) {
111 elm_icon_file_set(icon, MYFILE_IMGE_PATH "/myfile_icon_etc.png", NULL);
112 evas_object_size_hint_min_set(icon, 30, 30);
116 char *default_icon_path = NULL;
117 char thumb_path[MB_SVC_FILE_PATH_LEN_MAX] = { 0, };
119 debug_log("file path: %s", path);
121 char *type = (char *)efreet_mime_type_get(ecore_file_file_get(path));
122 char *mime_type = g_strdup(type);
123 debug_log("mime type: %s", mime_type);
124 efreet_mime_shutdown();
127 struct _mtd *mtd = (struct _mtd *)mtd_main;
129 while (mtd && mtd->key) {
130 if (strncmp(mime_type, mtd->key, strlen(mime_type)) == 0) {
131 debug_log("key (%s)", mtd->key);
132 debug_log("type(%s)", type);
133 default_icon_path = g_strdup(mtd->icon_path);
134 debug_log("default_icon_path: %s", default_icon_path);
140 if (strncmp(mime_type, "image", 5) == 0) {
144 ethumb_thumb_format_set(e, ETHUMB_THUMB_FDO);
145 ethumb_thumb_aspect_set(e, ETHUMB_THUMB_KEEP_ASPECT);
146 ethumb_thumb_size_set(e, 40, 40);
147 ethumb_thumb_category_set(e, "thumbnails");
148 ethumb_thumb_dir_path_set(e, DATADIR);
149 ethumb_file_set(e, path, NULL);
151 const char *thumb_path_tmp = NULL;
152 const char *key = NULL;
153 ethumb_thumb_path_get(e, &thumb_path_tmp, &key);
155 if (!ecore_file_exists(thumb_path_tmp)) {
156 if (!ethumb_exists(e)) {
157 ethumb_generate(e, _thumbnail_generated_cb, NULL, NULL);
161 if (thumb_path_tmp) {
162 debug_log("thumbnail path = %s", thumb_path);
163 snprintf(thumb_path, sizeof(thumb_path), "%s", thumb_path_tmp);
167 } else if (strncmp(mime_type, "video", 5) == 0) {
168 metadata_extractor_h metadata = NULL;
169 int ret = METADATA_EXTRACTOR_ERROR_NONE;
170 char *video_width = NULL;
171 char *video_height = NULL;
172 char *video_track_cnt = NULL;
173 void *video_thumbnail = NULL;
174 int video_thumbnail_len = 0;
178 ret = metadata_extractor_create(&metadata);
179 debug_log("metadata_extractor_create: %d", ret);
181 debug_log("metadata extractor create failed");
185 ret = metadata_extractor_set_path(metadata, path);
186 debug_log("metadata_extractor_set_path: %d", ret);
188 ret = metadata_extractor_get_metadata(metadata, METADATA_VIDEO_WIDTH, &video_width);
189 debug_log("metadata_extractor_get_metadata: %d [video_width:%s]", ret, video_width);
190 ret = metadata_extractor_get_metadata(metadata, METADATA_VIDEO_HEIGHT, &video_height);
191 debug_log("metadata_extractor_get_metadata: %d [video_height:%s]", ret, video_height);
192 ret = metadata_extractor_get_metadata(metadata, METADATA_HAS_VIDEO, &video_track_cnt);
193 debug_log("metadata_extractor_get_metadata: %d [video_track_cnt:%s]", ret, video_track_cnt);
195 ret = metadata_extractor_get_frame(metadata, &video_thumbnail, &video_thumbnail_len);
196 debug_log("metadata_extractor_get_frame: %d (video_thumbnail_len:%d)", ret, video_thumbnail_len);
198 if (video_thumbnail) {
200 char filename[1024] = { 0, };
201 char *file_name = NULL;
202 char *file_ext = NULL;
204 email_parse_get_filename_n_ext_from_path(path, &file_name, &file_ext);
205 snprintf(filename, sizeof(filename), "%s%s%s%s", DATADIR"/", file_name, file_ext, ".jpg");
210 video_w = atoi(video_width);
212 video_h = atoi(video_height);
213 mm_ret = image_util_encode_jpeg(video_thumbnail,
215 IMAGE_UTIL_COLORSPACE_RGB888, 70, filename);
217 if (ecore_file_exists(filename)) {
218 snprintf(thumb_path, sizeof(thumb_path), "%s", filename);
219 debug_log("file : %s, thumb_path : %s", path, thumb_path);
221 g_free(video_thumbnail);
227 g_free(video_height);
229 g_free(video_track_cnt);
231 ret = metadata_extractor_destroy(metadata);
232 debug_log("metadata_extractor_destroy: %d", ret);
233 } else if (strncmp(mime_type, "audio", 5) == 0) {
234 metadata_extractor_h metadata = NULL;
235 int ret = METADATA_EXTRACTOR_ERROR_NONE;
236 void *artwork = NULL;
237 int artwork_size = 0;
238 char *artwork_mime = NULL;
240 ret = metadata_extractor_create(&metadata);
241 debug_log("metadata_extractor_create: %d", ret);
243 debug_log("metadata extractor create failed");
247 ret = metadata_extractor_set_path(metadata, path);
248 debug_log("metadata_extractor_set_path: %d", ret);
250 ret = metadata_extractor_get_artwork(metadata, &artwork, &artwork_size, &artwork_mime);
251 debug_log("metadata_extractor_get_artwork: %d", ret);
252 debug_log("artwork_mime: %s, artwork_size: %d", artwork_mime, artwork_size);
255 gchar *mm_path = NULL;
256 int fd = g_file_open_tmp(NULL, &mm_path, NULL);
259 FILE *fp = fdopen(fd, "w");
261 debug_log("fail to fdopen()");
264 int n = fwrite((unsigned char *)artwork, sizeof(char), artwork_size, fp);
265 if (n != artwork_size) {
266 debug_log("fail to fwrite()");
277 snprintf(thumb_path, sizeof(thumb_path), "%s", mm_path);
278 debug_log("file : %s, album art_path : %s", path, thumb_path);
287 g_free(artwork_mime);
289 ret = metadata_extractor_destroy(metadata);
290 debug_log("metadata_extractor_destroy: %d", ret);
293 if (strlen(thumb_path) > 0) {
294 debug_log("thumbpath: %s", thumb_path);
295 elm_icon_file_set(icon, thumb_path, NULL);
297 if (default_icon_path) {
298 debug_log("thumbpath: %s", default_icon_path);
299 elm_icon_file_set(icon, default_icon_path, NULL);
301 elm_icon_file_set(icon, MYFILE_IMGE_PATH "/myfile_icon_etc.png", NULL);
302 evas_object_size_hint_min_set(icon, 30, 30);
306 elm_icon_file_set(icon, MYFILE_IMGE_PATH "/myfile_icon_etc.png", NULL);
307 evas_object_size_hint_min_set(icon, 30, 30);
311 g_free(default_icon_path);
316 static void _thumbnail_generated_cb(void *data, Ethumb *e, Eina_Bool success)
321 debug_log("Succeed in thumbnail generation");
323 debug_log("Fail to create thumbnail");
327 void _create_notify(EmailViewerUGD *ug_data, char *header, char *content,
328 int btn_num, char *btn1_lb, popup_cb resp_cb1,
329 char *btn2_lb, popup_cb resp_cb2, popup_cb resp_block_cb)
332 Evas_Object *notify = NULL;
334 if (ug_data->notify) {
335 evas_object_del(ug_data->notify);
336 ug_data->notify = NULL;
339 notify = elm_popup_add(ug_data->win_main);
340 debug_log("notify: %p, win_main: %p", notify, ug_data->win_main);
342 debug_log("elm_popup_add returns NULL");
345 evas_object_size_hint_weight_set(notify, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
346 ug_data->notify = notify;
349 elm_object_part_text_set(notify, "title,text", header);
353 elm_object_text_set(notify, content);
357 elm_popup_timeout_set(notify, 2.0);
358 evas_object_smart_callback_add(notify, "block,clicked", resp_block_cb, ug_data);
362 Evas_Object *btn1 = elm_button_add(notify);
363 elm_object_style_set(btn1, "popup_button/default");
364 elm_object_text_set(btn1, btn1_lb);
365 elm_object_part_content_set(notify, "button1", btn1);
366 evas_object_smart_callback_add(btn1, "clicked", resp_cb1, ug_data);
369 Evas_Object *btn1 = elm_button_add(notify);
370 elm_object_style_set(btn1, "popup_button/default");
371 elm_object_text_set(btn1, btn1_lb);
372 elm_object_part_content_set(notify, "button1", btn1);
373 evas_object_smart_callback_add(btn1, "clicked", resp_cb1, ug_data);
375 Evas_Object *btn2 = elm_button_add(notify);
376 elm_object_style_set(btn2, "popup_button/default");
377 elm_object_text_set(btn2, btn2_lb);
378 elm_object_part_content_set(notify, "button2", btn2);
379 evas_object_smart_callback_add(btn2, "clicked", resp_cb2, ug_data);
382 evas_object_show(notify);
385 int _find_folder_id_using_folder_type(EmailViewerUGD *ug_data, email_mailbox_type_e mailbox_type)
388 if (ug_data->folder_list == NULL) {
393 int cnt = g_list_length(ug_data->folder_list);
394 for (i = 0; i < cnt; i++) {
395 EmailMailboxNameAndAlias *nameandalias = (EmailMailboxNameAndAlias *) g_list_nth_data(ug_data->folder_list, i);
396 int folder_id = nameandalias->mailbox_id;
397 if (mailbox_type == nameandalias->mailbox_type) {
405 void _move_email(EmailViewerUGD *ug_data, int dest_folder_id, gboolean is_delete)
409 EmailViewerPrivate *priv = ug_data->email_data;
411 if (!email_engine_move_mail(priv->account_id, dest_folder_id, priv->mail_id)) {
412 debug_log("Moving email is failed.");
415 _create_notify(ug_data, _("IDS_EMAIL_POP_ALERT"),
416 _("IDS_EMAIL_POP_FAILED_TO_DELETE"), 0,
417 NULL, NULL, NULL, NULL, _popup_response_cb);
419 _create_notify(ug_data, _("IDS_EMAIL_POP_ALERT"),
420 _("IDS_EMAIL_POP_FAILED_TO_MOVE"), 0,
421 NULL, NULL, NULL, NULL, _popup_response_cb);
424 debug_log("account_id: %d, moveto folder id: %d, mail_id: %d", priv->account_id, dest_folder_id, priv->mail_id);
425 char str[128] = { 0, };
427 snprintf(str, sizeof(str), "%s", dgettext("sys_string", "IDS_COM_POP_DELETED"));
429 snprintf(str, sizeof(str), "%s", dgettext("sys_string", "IDS_COM_POP_MOVED"));
430 int ret = status_message_post(str);
432 debug_log("status_message_post failed: %d", ret);
436 void _delete_email(EmailViewerUGD *ug_data)
440 EmailViewerPrivate *priv = ug_data->email_data;
442 int sync = EMAIL_DELETE_LOCAL_AND_SERVER;
443 if (ug_data->account_type == EMAIL_SERVER_TYPE_POP3) {
444 email_account_t *account_data = NULL;
445 if (email_engine_get_account_full_data(ug_data->account_id, &account_data)) {
446 debug_log("email_engine_get_account");
448 account_user_data_t *user_data = (account_user_data_t*)account_data->user_data;
449 if (user_data != NULL) {
450 debug_log("pop3_deleting_option:%d", user_data->pop3_deleting_option);
451 if (user_data->pop3_deleting_option == 0) {
452 sync = EMAIL_DELETE_LOCALLY;
453 } else if (user_data->pop3_deleting_option == 1) {
454 sync = EMAIL_DELETE_LOCAL_AND_SERVER;
457 email_free_account(&account_data, 1);
463 if (!email_engine_delete_mail(priv->account_id, priv->mailbox_id, priv->mail_id, sync)) {
464 debug_log("Deleting email is failed.");
465 _create_notify(ug_data, _("IDS_EMAIL_POP_ALERT"),
466 _("IDS_EMAIL_POP_FAILED_TO_DELETE"), 0,
467 NULL, NULL, NULL, NULL, _popup_response_cb);
469 debug_log("account_id: %d, mailbox_id: %d, mail_id: %d", priv->account_id, priv->mailbox_id, priv->mail_id);
470 char str[128] = { 0, };
471 snprintf(str, sizeof(str), "%s", dgettext("sys_string", "IDS_COM_POP_DELETED"));
472 int ret = status_message_post(str);
474 debug_log("status_message_post failed: %d", ret);
478 void _notify_mailbox_size(EmailViewerUGD *ug_data, char *size)
481 RETURN_IF_FAIL(ug_data != NULL);
482 RETURN_IF_FAIL(ug_data->ug != NULL);
484 service_h service = NULL;
486 ret = service_create(&service);
487 debug_log("service_create: %d", ret);
489 debug_log("service create failed");
493 /* Send mailbox resize notification to mailbox */
494 ret = service_add_extra_data(service, EMAIL_BUNDLE_KEY_MAILBOX_SIZE, size);
495 debug_log("service_add_extra_data: %d", ret);
497 ug_send_result(ug_data->ug, service);
499 ret = service_destroy(service);
500 debug_log("service_destroy: %d", ret);