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