Merge "check src before doing g_strlcpy and g_strlcat" into tizen
[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 GSList *g_inserted_noti_list = NULL;
27 static __thread int g_noti_from_pid = -1;
28
29 static void __media_svc_publish_noti_by_item(gpointer data, gpointer user_data)
30 {
31         int ret = MS_MEDIA_ERR_NONE;
32         media_svc_noti_item *item = (media_svc_noti_item *)data;
33
34         if (item && item->path) {
35                 ret = media_db_update_send_internal(item->pid, item->update_item, item->update_type, item->path, item->media_uuid, item->media_type, item->mime_type);
36                 if (ret != MS_MEDIA_ERR_NONE) {
37                         media_svc_sec_error("media_db_update_send_internal failed : %d [%s]", ret, item->path);
38                 } else {
39                         media_svc_debug("media_db_update_send_internal success");
40                 }
41         } else {
42                 media_svc_debug("invalid path");
43         }
44 }
45
46 static void __media_svc_destroy_noti_item(gpointer data)
47 {
48         media_svc_noti_item *item = (media_svc_noti_item *)data;
49
50         SAFE_FREE(item->media_uuid);
51         SAFE_FREE(item->path);
52         SAFE_FREE(item->mime_type);
53         SAFE_FREE(item);
54 }
55
56 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)
57 {
58         int ret = MS_MEDIA_ERR_NONE;
59         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
60
61         ret = media_db_update_send_internal(getpid(), MS_MEDIA_ITEM_FILE, update_type, (char *)path, (char *)uuid, media_type, (char *)mime_type);
62         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Send noti failed[%d][%s]", ret, path);
63
64         media_svc_debug("Send noti [%s]", path);
65
66         return ret;
67 }
68
69 int _media_svc_publish_dir_noti(media_item_update_type_e update_type, const char *path, const char *uuid, int pid)
70 {
71         int ret = MS_MEDIA_ERR_NONE;
72         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
73
74         ret = media_db_update_send_internal(pid, MS_MEDIA_ITEM_DIRECTORY, update_type, (char *)path, (char *)uuid, MS_MEDIA_UNKNOWN, NULL);
75         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Send dir noti failed[%d][%s]", ret, path);
76
77         media_svc_debug("Send dir noti [%s]", path);
78
79         return ret;
80 }
81
82
83 void _media_svc_set_noti_from_pid(int pid)
84 {
85         g_noti_from_pid = pid;
86 }
87
88 void _media_svc_initialize_noti_list(void)
89 {
90         if (g_inserted_noti_list)
91                 g_slist_free_full(g_inserted_noti_list, __media_svc_destroy_noti_item);
92
93         g_inserted_noti_list = NULL;
94 }
95
96 void _media_svc_insert_item_to_noti_list(media_svc_content_info_s *content_info)
97 {
98         media_svc_noti_item *item = NULL;
99
100         if (!content_info)
101                 return;
102
103         item = (media_svc_noti_item *) malloc(sizeof(media_svc_noti_item));
104         if (item) {
105                 item->pid = g_noti_from_pid;
106                 item->update_item = MS_MEDIA_ITEM_INSERT; /* INSERT */
107                 item->update_type = MS_MEDIA_ITEM_FILE;
108                 item->media_type = content_info->media_type;
109                 item->media_uuid = g_strdup(content_info->media_uuid);
110                 item->path = g_strdup(content_info->path);
111                 item->mime_type = g_strdup(content_info->mime_type);
112
113                 g_inserted_noti_list = g_slist_append(g_inserted_noti_list, (gpointer)item);
114         } else {
115                 media_svc_error("Allocation failed");
116         }
117 }
118
119 void _media_svc_publish_noti_list(void)
120 {
121         g_slist_foreach(g_inserted_noti_list, __media_svc_publish_noti_by_item, NULL);
122
123         _media_svc_initialize_noti_list();
124 }
125
126 void _media_svc_destroy_noti_item(media_svc_noti_item *item)
127 {
128         if (!item)
129                 return;
130
131         SAFE_FREE(item->media_uuid);
132         SAFE_FREE(item->path);
133         SAFE_FREE(item->mime_type);
134         SAFE_FREE(item);
135 }