summaryrefslogtreecommitdiff
path: root/src/common/media-svc-noti.c
blob: 405406c60c3b522e6dc6d7b570d756a429bcf0a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * libmedia-service
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <unistd.h>
#include "media-svc-noti.h"
#include "media-svc-util.h"

static int __media_svc_publish_noti_by_item(media_svc_noti_item *noti_item);

static __thread media_svc_noti_item *g_inserted_noti_list = NULL;
static __thread int g_noti_from_pid = -1;

static int __media_svc_publish_noti_by_item(media_svc_noti_item *noti_item)
{
	int ret = MS_MEDIA_ERR_NONE;

	if (noti_item && noti_item->path) {
		ret = media_db_update_send(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);
		if (ret != MS_MEDIA_ERR_NONE) {
			media_svc_error("media_db_update_send failed : %d [%s]", ret, noti_item->path);
			ret = MS_MEDIA_ERR_SEND_NOTI_FAIL;
		} else {
			media_svc_debug("media_db_update_send success");
		}
	} else {
		media_svc_debug("invalid path");
		ret = MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	return ret;
}

int _media_svc_publish_noti(media_item_type_e update_item,
                            media_item_update_type_e update_type,
                            const char *path,
                            media_type_e media_type,
                            const char *uuid,
                            const char *mime_type
                           )
{
	int ret = MS_MEDIA_ERR_NONE;

	if (STRING_VALID(path)) {
		ret = media_db_update_send(getpid(), update_item, update_type, (char *)path, (char *)uuid, media_type, (char *)mime_type);
		if (ret != MS_MEDIA_ERR_NONE) {
			media_svc_error("Send noti failed : %d [%s]", ret, path);
			ret = MS_MEDIA_ERR_SEND_NOTI_FAIL;
		} else {
			media_svc_debug("Send noti success [%d][%d]", update_item, update_type);
		}
	} else {
		media_svc_debug("invalid path");
		ret = MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	return ret;
}

void _media_svc_set_noti_from_pid(int pid)
{
	g_noti_from_pid = pid;
}

int _media_svc_create_noti_list(int count)
{
	SAFE_FREE(g_inserted_noti_list);

	g_inserted_noti_list = calloc(count, sizeof(media_svc_noti_item));
	if (g_inserted_noti_list == NULL) {
		media_svc_error("Failed to prepare noti items");
		return MS_MEDIA_ERR_OUT_OF_MEMORY;
	}

	return MS_MEDIA_ERR_NONE;
}

int _media_svc_insert_item_to_noti_list(media_svc_content_info_s *content_info, int cnt)
{
	media_svc_noti_item *noti_list = g_inserted_noti_list;

	if (noti_list && content_info) {
		noti_list[cnt].pid = g_noti_from_pid;
		noti_list[cnt].update_item = MS_MEDIA_ITEM_INSERT; /* INSERT */
		noti_list[cnt].update_type = MS_MEDIA_ITEM_FILE;
		noti_list[cnt].media_type = content_info->media_type;
		if (content_info->media_uuid)
			noti_list[cnt].media_uuid = strdup(content_info->media_uuid);
		if (content_info->path)
			noti_list[cnt].path = strdup(content_info->path);
		if (content_info->mime_type)
			noti_list[cnt].mime_type = strdup(content_info->mime_type);
	}

	return MS_MEDIA_ERR_NONE;
}

int _media_svc_destroy_noti_list(int all_cnt)
{
	int i = 0;
	media_svc_noti_item *noti_list = g_inserted_noti_list;

	if (noti_list) {
		for (i = 0; i < all_cnt; i++) {
			SAFE_FREE(noti_list[i].media_uuid);
			SAFE_FREE(noti_list[i].path);
			SAFE_FREE(noti_list[i].mime_type);
		}

		SAFE_FREE(g_inserted_noti_list);
		g_inserted_noti_list = NULL;
	}

	return MS_MEDIA_ERR_NONE;
}

int _media_svc_publish_noti_list(int all_cnt)
{
	int ret = MS_MEDIA_ERR_NONE;
	int idx = 0;
	media_svc_noti_item *noti_list = g_inserted_noti_list;

	if (noti_list) {
		for (idx = 0; idx < all_cnt; idx++) {
			ret = __media_svc_publish_noti_by_item(&(noti_list[idx]));
		}
	}

	return ret;
}

int _media_svc_destroy_noti_item(media_svc_noti_item *item)
{
	if (item) {
		SAFE_FREE(item->media_uuid);
		SAFE_FREE(item->path);
		SAFE_FREE(item->mime_type);

		SAFE_FREE(item);
	}

	return MS_MEDIA_ERR_NONE;
}