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