Update scanner process
[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
26 static int __media_svc_publish_noti_by_item(media_svc_noti_item *noti_item);
27
28 static __thread media_svc_noti_item *g_inserted_noti_list = NULL;
29 static __thread int g_noti_from_pid = -1;
30
31 static int __media_svc_publish_noti_by_item(media_svc_noti_item *noti_item)
32 {
33         int ret = MS_MEDIA_ERR_NONE;
34
35         if (noti_item && noti_item->path) {
36                 ret = media_db_update_send(noti_item->pid, noti_item->update_item, noti_item->update_type, noti_item->path, noti_item->media_uuid, noti_item->media_type, noti_item->mime_type);
37                 if (ret != MS_MEDIA_ERR_NONE) {
38                         media_svc_error("media_db_update_send failed : %d [%s]", ret, noti_item->path);
39                         ret = MS_MEDIA_ERR_SEND_NOTI_FAIL;
40                 } else {
41                         media_svc_debug("media_db_update_send success");
42                 }
43         } else {
44                 media_svc_debug("invalid path");
45                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
46         }
47
48         return ret;
49 }
50
51 int _media_svc_publish_noti(media_item_type_e update_item,
52                             media_item_update_type_e update_type,
53                             const char *path,
54                             media_type_e media_type,
55                             const char *uuid,
56                             const char *mime_type
57                            )
58 {
59         int ret = MS_MEDIA_ERR_NONE;
60
61         if (STRING_VALID(path)) {
62                 ret = media_db_update_send(getpid(), update_item, update_type, (char *)path, (char *)uuid, media_type, (char *)mime_type);
63                 if (ret != MS_MEDIA_ERR_NONE) {
64                         media_svc_error("Send noti failed : %d [%s]", ret, path);
65                         ret = MS_MEDIA_ERR_SEND_NOTI_FAIL;
66                 } else {
67                         media_svc_debug("Send noti success [%d][%d]", update_item, update_type);
68                 }
69         } else {
70                 media_svc_debug("invalid path");
71                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
72         }
73
74         return ret;
75 }
76
77 int _media_svc_publish_dir_noti(media_item_type_e update_item,
78                                                         media_item_update_type_e update_type,
79                                                         const char *path,
80                                                         media_type_e media_type,
81                                                         const char *uuid,
82                                                         const char *mime_type,
83                                                         int pid
84 )
85 {
86         int ret = MS_MEDIA_ERR_NONE;
87
88         if(STRING_VALID(path))
89         {
90                 ret = media_db_update_send(pid, update_item, update_type, (char *)path, (char *)uuid, media_type, (char *)mime_type);
91                 if(ret != MS_MEDIA_ERR_NONE)
92                 {
93                         media_svc_error("Send noti failed : %d [%s]", ret, path);
94                         ret = MS_MEDIA_ERR_SEND_NOTI_FAIL;
95                 }
96                 else
97                 {
98                         media_svc_debug("Send noti success [%d][%d]", update_item, update_type);
99                 }
100         }
101         else
102         {
103                 media_svc_debug("invalid path");
104                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
105         }
106
107         return ret;
108 }
109
110
111 int _media_svc_publish_dir_noti_v2(media_item_type_e update_item,
112                                                         media_item_update_type_e update_type,
113                                                         const char *path,
114                                                         media_type_e media_type,
115                                                         const char *uuid,
116                                                         const char *mime_type,
117                                                         int pid
118 )
119 {
120         int ret = MS_MEDIA_ERR_NONE;
121
122         if(STRING_VALID(path))
123         {
124                 ret = media_db_update_send_internal(pid, update_item, update_type, (char *)path, (char *)uuid, media_type, (char *)mime_type);
125                 if(ret != MS_MEDIA_ERR_NONE)
126                 {
127                         media_svc_error("Send noti failed : %d [%s]", ret, path);
128                         ret = MS_MEDIA_ERR_SEND_NOTI_FAIL;
129                 }
130                 else
131                 {
132                         media_svc_debug("Send noti success [%d][%d]", update_item, update_type);
133                 }
134         }
135         else
136         {
137                 media_svc_debug("invalid path");
138                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
139         }
140
141         return ret;
142 }
143
144
145 void _media_svc_set_noti_from_pid(int pid)
146 {
147         g_noti_from_pid = pid;
148 }
149
150 int _media_svc_create_noti_list(int count)
151 {
152         SAFE_FREE(g_inserted_noti_list);
153
154         g_inserted_noti_list = calloc(count, sizeof(media_svc_noti_item));
155         if (g_inserted_noti_list == NULL) {
156                 media_svc_error("Failed to prepare noti items");
157                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
158         }
159
160         return MS_MEDIA_ERR_NONE;
161 }
162
163 int _media_svc_insert_item_to_noti_list(media_svc_content_info_s *content_info, int cnt)
164 {
165         media_svc_noti_item *noti_list = g_inserted_noti_list;
166
167         if (noti_list && content_info) {
168                 noti_list[cnt].pid = g_noti_from_pid;
169                 noti_list[cnt].update_item = MS_MEDIA_ITEM_INSERT; /* INSERT */
170                 noti_list[cnt].update_type = MS_MEDIA_ITEM_FILE;
171                 noti_list[cnt].media_type = content_info->media_type;
172                 if (content_info->media_uuid)
173                         noti_list[cnt].media_uuid = strdup(content_info->media_uuid);
174                 if (content_info->path)
175                         noti_list[cnt].path = strdup(content_info->path);
176                 if (content_info->mime_type)
177                         noti_list[cnt].mime_type = strdup(content_info->mime_type);
178         }
179
180         return MS_MEDIA_ERR_NONE;
181 }
182
183 int _media_svc_destroy_noti_list(int all_cnt)
184 {
185         int i = 0;
186         media_svc_noti_item *noti_list = g_inserted_noti_list;
187
188         if (noti_list) {
189                 for (i = 0; i < all_cnt; i++) {
190                         SAFE_FREE(noti_list[i].media_uuid);
191                         SAFE_FREE(noti_list[i].path);
192                         SAFE_FREE(noti_list[i].mime_type);
193                 }
194
195                 SAFE_FREE(g_inserted_noti_list);
196                 g_inserted_noti_list = NULL;
197         }
198
199         return MS_MEDIA_ERR_NONE;
200 }
201
202 int _media_svc_publish_noti_list(int all_cnt)
203 {
204         int ret = MS_MEDIA_ERR_NONE;
205         int idx = 0;
206         media_svc_noti_item *noti_list = g_inserted_noti_list;
207
208         if (noti_list) {
209                 for (idx = 0; idx < all_cnt; idx++) {
210                         ret = __media_svc_publish_noti_by_item(&(noti_list[idx]));
211                 }
212         }
213
214         return ret;
215 }
216
217 int _media_svc_destroy_noti_item(media_svc_noti_item *item)
218 {
219         if (item) {
220                 SAFE_FREE(item->media_uuid);
221                 SAFE_FREE(item->path);
222                 SAFE_FREE(item->mime_type);
223
224                 SAFE_FREE(item);
225         }
226
227         return MS_MEDIA_ERR_NONE;
228 }