733dc2ca5670ad70dcf6748ec15a7cf3662c1201
[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_internal(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_internal 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_internal 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_update_type_e update_type, const char *path, media_type_e media_type, const char *uuid, const char *mime_type)
52 {
53         int ret = MS_MEDIA_ERR_NONE;
54         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
55
56         ret = media_db_update_send_internal(getpid(), MS_MEDIA_ITEM_FILE, update_type, (char *)path, (char *)uuid, media_type, (char *)mime_type);
57         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Send noti failed[%d][%s]", ret, path);
58
59         media_svc_debug("Send noti [%s]", path);
60
61         return ret;
62 }
63
64 int _media_svc_publish_dir_noti(media_item_update_type_e update_type, const char *path, const char *uuid, int pid)
65 {
66         int ret = MS_MEDIA_ERR_NONE;
67         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
68
69         ret = media_db_update_send_internal(pid, MS_MEDIA_ITEM_DIRECTORY, update_type, (char *)path, (char *)uuid, MS_MEDIA_UNKNOWN, NULL);
70         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Send dir noti failed[%d][%s]", ret, path);
71
72         media_svc_debug("Send dir noti [%s]", path);
73
74         return ret;
75 }
76
77
78 void _media_svc_set_noti_from_pid(int pid)
79 {
80         g_noti_from_pid = pid;
81 }
82
83 int _media_svc_create_noti_list(int count)
84 {
85         SAFE_FREE(g_inserted_noti_list);
86
87         g_inserted_noti_list = calloc(count, sizeof(media_svc_noti_item));
88         if (g_inserted_noti_list == NULL) {
89                 media_svc_error("Failed to prepare noti items");
90                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
91         }
92
93         return MS_MEDIA_ERR_NONE;
94 }
95
96 int _media_svc_insert_item_to_noti_list(media_svc_content_info_s *content_info, int cnt)
97 {
98         media_svc_noti_item *noti_list = g_inserted_noti_list;
99
100         if (noti_list && content_info) {
101                 noti_list[cnt].pid = g_noti_from_pid;
102                 noti_list[cnt].update_item = MS_MEDIA_ITEM_INSERT; /* INSERT */
103                 noti_list[cnt].update_type = MS_MEDIA_ITEM_FILE;
104                 noti_list[cnt].media_type = content_info->media_type;
105                 if (content_info->media_uuid)
106                         noti_list[cnt].media_uuid = strdup(content_info->media_uuid);
107                 if (content_info->path)
108                         noti_list[cnt].path = strdup(content_info->path);
109                 if (content_info->mime_type)
110                         noti_list[cnt].mime_type = strdup(content_info->mime_type);
111         }
112
113         return MS_MEDIA_ERR_NONE;
114 }
115
116 int _media_svc_destroy_noti_list(int all_cnt)
117 {
118         int i = 0;
119         media_svc_noti_item *noti_list = g_inserted_noti_list;
120
121         if (noti_list) {
122                 for (i = 0; i < all_cnt; i++) {
123                         SAFE_FREE(noti_list[i].media_uuid);
124                         SAFE_FREE(noti_list[i].path);
125                         SAFE_FREE(noti_list[i].mime_type);
126                 }
127
128                 SAFE_FREE(g_inserted_noti_list);
129                 g_inserted_noti_list = NULL;
130         }
131
132         return MS_MEDIA_ERR_NONE;
133 }
134
135 int _media_svc_publish_noti_list(int all_cnt)
136 {
137         int ret = MS_MEDIA_ERR_NONE;
138         int idx = 0;
139         media_svc_noti_item *noti_list = g_inserted_noti_list;
140
141         if (noti_list) {
142                 for (idx = 0; idx < all_cnt; idx++)
143                         ret = __media_svc_publish_noti_by_item(&(noti_list[idx]));
144         }
145
146         return ret;
147 }
148
149 int _media_svc_destroy_noti_item(media_svc_noti_item *item)
150 {
151         if (item) {
152                 SAFE_FREE(item->media_uuid);
153                 SAFE_FREE(item->path);
154                 SAFE_FREE(item->mime_type);
155
156                 SAFE_FREE(item);
157         }
158
159         return MS_MEDIA_ERR_NONE;
160 }