Improve mime type verification
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-noti.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <unistd.h>
23 #include "media-svc-noti.h"
24 #include "media-svc-util.h"
25 #include "media-svc-debug.h"
26
27 static __thread GSList *g_inserted_noti_list = NULL;
28 static __thread int g_noti_from_pid = -1;
29
30 static void __media_svc_publish_noti_by_item(gpointer data, gpointer user_data)
31 {
32         int ret = MS_MEDIA_ERR_NONE;
33         media_svc_noti_item *item = (media_svc_noti_item *)data;
34
35         if (item && item->path) {
36                 ret = media_db_update_send(item->pid, item->update_item, item->update_type, item->path, item->media_uuid, item->media_type, item->mime_type);
37                 if (ret != MS_MEDIA_ERR_NONE) {
38                         media_svc_sec_error("media_db_update_send failed : %d [%s]", ret, item->path);
39                 } else {
40                         media_svc_debug("media_db_update_send success");
41                 }
42         } else {
43                 media_svc_debug("invalid path");
44         }
45 }
46
47 static void __media_svc_destroy_noti_item(gpointer data)
48 {
49         media_svc_noti_item *item = (media_svc_noti_item *)data;
50
51         g_free(item->media_uuid);
52         g_free(item->path);
53         g_free(item->mime_type);
54         g_free(item);
55 }
56
57 int _media_svc_publish_noti(media_item_update_type_e update_type, const char *path, media_type_e media_type, const char *uuid, const char *mime_type)
58 {
59         int ret = MS_MEDIA_ERR_NONE;
60         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
61
62         ret = media_db_update_send(getpid(), MS_MEDIA_ITEM_FILE, update_type, path, uuid, media_type, mime_type);
63         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Send noti failed[%d][%s]", ret, path);
64
65         media_svc_sec_debug("Send noti [%s]", path);
66
67         return ret;
68 }
69
70 int _media_svc_publish_dir_noti(media_item_update_type_e update_type, const char *path, const char *uuid, int pid)
71 {
72         int ret = MS_MEDIA_ERR_NONE;
73         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
74
75         ret = media_db_update_send(pid, MS_MEDIA_ITEM_DIRECTORY, update_type, path, uuid, MS_MEDIA_UNKNOWN, NULL);
76         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Send dir noti failed[%d][%s]", ret, path);
77
78         media_svc_sec_debug("Send dir noti [%s]", path);
79
80         return ret;
81 }
82
83 void _media_svc_set_noti_from_pid(int pid)
84 {
85         g_noti_from_pid = pid;
86 }
87
88 void _media_svc_initialize_noti_list(void)
89 {
90         if (g_inserted_noti_list)
91                 g_slist_free_full(g_inserted_noti_list, __media_svc_destroy_noti_item);
92
93         g_inserted_noti_list = NULL;
94 }
95
96 void _media_svc_insert_item_to_noti_list(media_svc_content_info_s *content_info)
97 {
98         media_svc_noti_item *item = NULL;
99
100         if (!content_info)
101                 return;
102
103         item = g_new0(media_svc_noti_item, 1);
104
105         item->pid = g_noti_from_pid;
106         item->update_item = MS_MEDIA_ITEM_INSERT; /* INSERT */
107         item->update_type = MS_MEDIA_ITEM_FILE;
108         item->media_type = content_info->media_type;
109         item->media_uuid = g_strdup(content_info->media_uuid);
110         item->path = g_strdup(content_info->path);
111         item->mime_type = g_strdup(content_info->mime_type);
112
113         g_inserted_noti_list = g_slist_append(g_inserted_noti_list, (gpointer)item);
114 }
115
116 void _media_svc_publish_noti_list(void)
117 {
118         g_slist_foreach(g_inserted_noti_list, __media_svc_publish_noti_by_item, NULL);
119
120         _media_svc_initialize_noti_list();
121 }
122
123 void _media_svc_destroy_noti_item(media_svc_noti_item *item)
124 {
125         if (!item)
126                 return;
127
128         g_free(item->media_uuid);
129         g_free(item->path);
130         g_free(item->mime_type);
131         g_free(item);
132 }