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