summaryrefslogtreecommitdiff
path: root/test/media-service-test.c
blob: 4c21532c9724e7456e548f88a45bcdd206a03453 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * 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 <stdio.h>
#include <unistd.h>
#include <media-svc.h>
#include <media-svc-noti.h>

#define SAFE_FREE(src)	{ if (src) { free(src); src = NULL; } }

GMainLoop *g_loop = NULL;
MediaSvcHandle *g_db_handle = NULL;

void _noti_cb(int pid,
		media_item_type_e update_item,
		media_item_update_type_e update_type,
		char *path,
		char *uuid,
		media_type_e content_type,
		char *mime_type,
		void *user_data)
{
	media_svc_debug("Noti from PID(%d)", pid);

	if (update_item == MS_MEDIA_ITEM_FILE)
		media_svc_debug("Noti item : MS_MEDIA_ITEM_FILE");
	else if (update_item == MS_MEDIA_ITEM_DIRECTORY)
		media_svc_debug("Noti item : MS_MEDIA_ITEM_DIRECTORY");

	if (update_type == MS_MEDIA_ITEM_INSERT)
		media_svc_debug("Noti type : MS_MEDIA_ITEM_INSERT");
	else if (update_type == MS_MEDIA_ITEM_DELETE)
		media_svc_debug("Noti type : MS_MEDIA_ITEM_DELETE");
	else if (update_type == MS_MEDIA_ITEM_UPDATE)
		media_svc_debug("Noti type : MS_MEDIA_ITEM_UPDATE");

	/*media_svc_debug("content type : %d", content_type); */
	printf("content type : %d\n", content_type);

	if (path)
		printf("path : %s\n", path);
	else
		printf("path not");

	if (mime_type)
		printf("mime_type : %s", mime_type);
	else
		printf("mime not");

	if (user_data) printf("String : %s\n", (char *)user_data);
	else
		printf("user not");

	return;
}

#if 1
gboolean _send_noti_batch_operations(gpointer data)
{
	int ret = MS_MEDIA_ERR_NONE;

	/* First of all, noti subscription */
	char *user_str = strdup("hi");
	/* 1. media_svc_insert_item_immediately */
	char *path = tzplatform_mkpath(TZ_USER_CONTENT, "test/image1.jpg");

	media_svc_storage_type_e storage_type;

	ret = media_svc_get_storage_type(path, &storage_type, tzplatform_getuid(TZ_USER_NAME));
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("media_svc_get_storage_type failed : %d (%s)", ret, path);
		SAFE_FREE(user_str);
		return FALSE;
	}

	int idx = 0;
	char *file_list[10];

	ret = media_svc_insert_item_begin(100, TRUE, getpid());
	/*ret = media_svc_insert_item_begin(g_db_handle, 100); */
	for (idx = 0; idx < 10; idx++) {
		char filepath[255] = {0, };
		snprintf(filepath, sizeof(filepath), "%s%d.jpg", tzplatform_mkpath(TZ_USER_CONTENT, "test/image"), idx + 1);
		media_svc_debug("File : %s\n", filepath);
		file_list[idx] = strdup(filepath);
		ret = media_svc_insert_item_bulk(g_db_handle, storage_type, file_list[idx], FALSE);
		if (ret != 0)
			media_svc_error("media_svc_insert_item_bulk[%d] failed", idx);
		else
			media_svc_debug("media_svc_insert_item_bulk[%d] success", idx);
	}

	ret = media_svc_insert_item_end(tzplatform_getuid(TZ_USER_NAME));

	SAFE_FREE(user_str);
	return FALSE;
}
#endif

gboolean _send_noti_operations(gpointer data)
{
	int ret = MS_MEDIA_ERR_NONE;

	/* First of all, noti subscription */
	char *user_str = strdup("hi");

	/* 1. media_svc_insert_item_immediately */
	char *path = tzplatform_mkpath(TZ_USER_CONTENT, "test/image1.jpg");
	media_svc_storage_type_e storage_type;

	ret = media_svc_get_storage_type(path, &storage_type, tzplatform_getuid(TZ_USER_NAME));
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("media_svc_get_storage_type failed : %d (%s)", ret, path);
		SAFE_FREE(user_str);
		return FALSE;
	}

	ret = media_svc_insert_item_immediately(g_db_handle, storage_type, path);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("media_svc_insert_item_immediately failed : %d", ret);
		SAFE_FREE(user_str);
		return FALSE;
	}

	media_svc_debug("media_svc_insert_item_immediately success");

	/* 2. media_svc_move_item */
	const char *dst_path = tzplatform_mkpath(TZ_USER_CONTENT, "test/image11.jpg");
	ret = media_svc_move_item(g_db_handle, path, dst_path);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("media_svc_move_item failed : %d", ret);
		return FALSE;
	}
	media_svc_debug("media_svc_move_item success");

	ret = media_svc_move_item(g_db_handle, storage_type, dst_path, storage_type, path);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("media_svc_move_item failed : %d", ret);
		return FALSE;
	}
	media_svc_debug("media_svc_move_item success");

	/* 4. media_svc_delete_item_by_path */
	ret = media_svc_delete_item_by_path(g_db_handle, path);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("media_svc_delete_item_by_path failed : %d", ret);
		return FALSE;
	}
	media_svc_debug("media_svc_delete_item_by_path success");

	return FALSE;
}

int test_noti()
{
	GSource *source = NULL;
	GMainContext *context = NULL;

	g_loop = g_main_loop_new(NULL, FALSE);
	context = g_main_loop_get_context(g_loop);
	source = g_idle_source_new();
#if 0
	g_source_set_callback(source, _send_noti_operations, NULL, NULL);
#else
	g_source_set_callback(source, _send_noti_batch_operations, NULL, NULL);
#endif
	g_source_attach(source, context);

	g_main_loop_run(g_loop);

	g_main_loop_unref(g_loop);

	return MS_MEDIA_ERR_NONE;
}

int main()
{
	int ret = MS_MEDIA_ERR_NONE;
	ret = media_svc_connect(&g_db_handle, tzplatform_getuid(TZ_USER_NAME), true);
	if (ret != MS_MEDIA_ERR_NONE)
		media_svc_error("media_svc_connect failed : %d", ret);
	else
		media_svc_debug("media_svc_connect success");

	ret = test_noti();
	if (ret < MS_MEDIA_ERR_NONE)
		media_svc_error("test_noti failed : %d", ret);
	else
		media_svc_debug("test_noti success");

	media_svc_disconnect(g_db_handle);
	return ret;
}