fix build break
[apps/core/preloaded/email.git] / viewer / src / email-viewer-utils.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/time.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include <time.h>
22 #include <errno.h>
23 #include <Ethumb.h>
24 #include <metadata_extractor.h>
25 #include <image_util.h>
26 #include <Efreet_Mime.h>
27 #include <status.h>
28
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"
34
35 #define MYFILE_IMGE_PATH "/opt/apps/com.samsung.myfile/res/images"
36 #define MB_SVC_FILE_PATH_LEN_MAX 255
37
38 static void _thumbnail_generated_cb(void *data, Ethumb *e, Eina_Bool success);
39
40 struct _mtd {
41         char *category; /* Not used at present */
42         char *key;
43         char *icon_path;
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 */
49 };
50
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}
79 };
80
81 static void _popup_response_cb(void *data, Evas_Object *obj, void *event_info)
82 {
83         debug_log("");
84
85         if (!data) {
86                 debug_log("data is NULL");
87                 return;
88         }
89
90         EmailViewerUGD *ug_data = (EmailViewerUGD *)data;
91
92         if (ug_data->notify) {
93                 evas_object_del(ug_data->notify);
94                 ug_data->notify = NULL;
95         }
96 }
97
98 Evas_Object *viewer_make_thumbnail(char *path, Evas_Object *parent)
99 {
100         debug_log("");
101
102         if (parent == NULL) {
103                 debug_log("parent obj is NULL");
104                 return NULL;
105         }
106
107         Evas_Object *icon = NULL;
108         icon = elm_icon_add(parent);
109
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);
113                 return icon;
114         }
115
116         char *default_icon_path = NULL;
117         char thumb_path[MB_SVC_FILE_PATH_LEN_MAX] = { 0, };
118
119         debug_log("file path: %s", path);
120         efreet_mime_init();
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();
125
126         if (mime_type) {
127                 struct _mtd *mtd = (struct _mtd *)mtd_main;
128
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);
135                                 break;
136                         }
137                         mtd++;
138                 }
139
140                 if (strncmp(mime_type, "image", 5) == 0) {
141                         Ethumb *e = NULL;
142                         e = ethumb_new();
143
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);
150
151                         const char *thumb_path_tmp = NULL;
152                         const char *key = NULL;
153                         ethumb_thumb_path_get(e, &thumb_path_tmp, &key);
154
155                         if (!ecore_file_exists(thumb_path_tmp)) {
156                                 if (!ethumb_exists(e)) {
157                                         ethumb_generate(e, _thumbnail_generated_cb, NULL, NULL);
158                                 }
159                         }
160
161                         if (thumb_path_tmp) {
162                                 debug_log("thumbnail path = %s", thumb_path);
163                                 snprintf(thumb_path, sizeof(thumb_path), "%s", thumb_path_tmp);
164                         }
165
166                         ethumb_free(e);
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;
175                         int video_w = 0;
176                         int video_h = 0;
177
178                         ret = metadata_extractor_create(&metadata);
179                         debug_log("metadata_extractor_create: %d", ret);
180                         if (!metadata) {
181                                 debug_log("metadata extractor create failed");
182                                 return NULL;
183                         }
184
185                         ret = metadata_extractor_set_path(metadata, path);
186                         debug_log("metadata_extractor_set_path: %d", ret);
187
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);
194
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);
197
198                         if (video_thumbnail) {
199                                 int mm_ret = 0;
200                                 char filename[1024] = { 0, };
201                                 char *file_name = NULL;
202                                 char *file_ext = NULL;
203
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");
206                                 g_free(file_name);
207                                 g_free(file_ext);
208
209                                 if (video_width)
210                                         video_w = atoi(video_width);
211                                 if (video_height)
212                                         video_h = atoi(video_height);
213                                 mm_ret = image_util_encode_jpeg(video_thumbnail,
214                                                                                                         video_w, video_h,
215                                                                                                         IMAGE_UTIL_COLORSPACE_RGB888, 70, filename);
216
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);
220                                 }
221                                 g_free(video_thumbnail);
222                         }
223
224                         if (video_width)
225                                 g_free(video_width);
226                         if (video_height)
227                                 g_free(video_height);
228                         if (video_track_cnt)
229                                 g_free(video_track_cnt);
230
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;
239
240                         ret = metadata_extractor_create(&metadata);
241                         debug_log("metadata_extractor_create: %d", ret);
242                         if (!metadata) {
243                                 debug_log("metadata extractor create failed");
244                                 return NULL;
245                         }
246
247                         ret = metadata_extractor_set_path(metadata, path);
248                         debug_log("metadata_extractor_set_path: %d", ret);
249
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);
253
254                         if (artwork) {
255                                 gchar *mm_path = NULL;
256                                 int fd = g_file_open_tmp(NULL, &mm_path, NULL);
257
258                                 if (fd != -1) {
259                                         FILE *fp = fdopen(fd, "w");
260                                         if (fp == NULL) {
261                                                 debug_log("fail to fdopen()");
262                                                 close(fd);
263                                         } else {
264                                                 int n = fwrite((unsigned char *)artwork, sizeof(char), artwork_size, fp);
265                                                 if (n != artwork_size) {
266                                                         debug_log("fail to fwrite()");
267                                                         fclose(fp);
268                                                         close(fd);
269                                                 } else {
270                                                         fflush(fp);
271                                                         fclose(fp);
272                                                         close(fd);
273                                                 }
274                                         }
275                                 }
276
277                                 snprintf(thumb_path, sizeof(thumb_path), "%s", mm_path);
278                                 debug_log("file : %s, album art_path : %s", path, thumb_path);
279
280                                 if (mm_path)
281                                         g_free(mm_path);
282
283                                 g_free(artwork);
284                         }
285
286                         if (artwork_mime)
287                                 g_free(artwork_mime);
288
289                         ret = metadata_extractor_destroy(metadata);
290                         debug_log("metadata_extractor_destroy: %d", ret);
291                 }
292
293                 if (strlen(thumb_path) > 0) {
294                         debug_log("thumbpath: %s", thumb_path);
295                         elm_icon_file_set(icon, thumb_path, NULL);
296                 } else {
297                         if (default_icon_path) {
298                                 debug_log("thumbpath: %s", default_icon_path);
299                                 elm_icon_file_set(icon, default_icon_path, NULL);
300                         } else {
301                                 elm_icon_file_set(icon, MYFILE_IMGE_PATH "/myfile_icon_etc.png", NULL);
302                                 evas_object_size_hint_min_set(icon, 30, 30);
303                         }
304                 }
305         } else {
306                 elm_icon_file_set(icon, MYFILE_IMGE_PATH "/myfile_icon_etc.png", NULL);
307                 evas_object_size_hint_min_set(icon, 30, 30);
308         }
309
310         g_free(mime_type);
311         g_free(default_icon_path);
312
313         return icon;
314 }
315
316 static void _thumbnail_generated_cb(void *data, Ethumb *e, Eina_Bool success)
317 {
318         debug_log("");
319
320         if (success) {
321                 debug_log("Succeed in thumbnail generation");
322         } else {
323                 debug_log("Fail to create thumbnail");
324         }
325 }
326
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)
330 {
331         debug_log("");
332         Evas_Object *notify = NULL;
333
334         if (ug_data->notify) {
335                 evas_object_del(ug_data->notify);
336                 ug_data->notify = NULL;
337         }
338
339         notify = elm_popup_add(ug_data->win_main);
340         debug_log("notify: %p, win_main: %p", notify, ug_data->win_main);
341         if (!notify) {
342                 debug_log("elm_popup_add returns NULL");
343                 return;
344         }
345         evas_object_size_hint_weight_set(notify, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
346         ug_data->notify = notify;
347
348         if (header) {
349                 elm_object_part_text_set(notify, "title,text", header);
350         }
351
352         if (content) {
353                 elm_object_text_set(notify, content);
354         }
355
356         if (btn_num == 0) {
357                 elm_popup_timeout_set(notify, 2.0);
358                 evas_object_smart_callback_add(notify, "block,clicked", resp_block_cb, ug_data);
359         }
360
361         if (btn_num == 1) {
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);
367         }
368         if (btn_num == 2) {
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);
374
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);
380         }
381
382         evas_object_show(notify);
383 }
384
385 int _find_folder_id_using_folder_type(EmailViewerUGD *ug_data, email_mailbox_type_e mailbox_type)
386 {
387         debug_log("");
388         if (ug_data->folder_list == NULL) {
389                 return -1;
390         }
391
392         int i = 0;
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) {
398                         return folder_id;
399                 }
400         }
401
402         return -1;
403 }
404
405 void _move_email(EmailViewerUGD *ug_data, int dest_folder_id, gboolean is_delete)
406 {
407         debug_log("");
408
409         EmailViewerPrivate *priv = ug_data->email_data;
410
411         if (!email_engine_move_mail(priv->account_id, dest_folder_id, priv->mail_id)) {
412                 debug_log("Moving email is failed.");
413
414                 if (is_delete)
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);
418                 else
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);
422
423         } else {
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, };
426                 if (is_delete)
427                         snprintf(str, sizeof(str), "%s", dgettext("sys_string", "IDS_COM_POP_DELETED"));
428                 else
429                         snprintf(str, sizeof(str), "%s", dgettext("sys_string", "IDS_COM_POP_MOVED"));
430                 int ret = status_message_post(str);
431                 if (ret)
432                         debug_log("status_message_post failed: %d", ret);
433         }
434 }
435
436 void _delete_email(EmailViewerUGD *ug_data)
437 {
438         debug_log("");
439
440         EmailViewerPrivate *priv = ug_data->email_data;
441
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");
447                         if (account_data) {
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;
455                                         }
456                                 }
457                                 email_free_account(&account_data, 1);
458                                 account_data = NULL;
459                         }
460                 }
461         }
462
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);
468         } else {
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);
473                 if (ret)
474                         debug_log("status_message_post failed: %d", ret);
475         }
476 }
477
478 void _notify_mailbox_size(EmailViewerUGD *ug_data, char *size)
479 {
480         debug_log("");
481         RETURN_IF_FAIL(ug_data != NULL);
482         RETURN_IF_FAIL(ug_data->ug != NULL);
483         int ret;
484         service_h service = NULL;
485
486         ret = service_create(&service);
487         debug_log("service_create: %d", ret);
488         if (!service) {
489                 debug_log("service create failed");
490                 return;
491         }
492
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);
496
497         ug_send_result(ug_data->ug, service);
498
499         ret = service_destroy(service);
500         debug_log("service_destroy: %d", ret);
501 }
502 /* EOF */