2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <media_info_private.h>
21 static void __media_tag_item_free(gpointer item)
23 media_tag_item_s *_item = (media_tag_item_s *)item;
25 g_free(_item->media_id);
26 g_free(_item->tag_name);
30 static void __media_tag_item_release(media_tag_s *tag)
32 if (!tag || !tag->item_list)
35 g_list_free_full(tag->item_list, __media_tag_item_free);
36 tag->item_list = NULL;
39 static int __media_tag_insert_item_to_tag(int tag_id, const char *media_id)
41 int ret = MEDIA_CONTENT_ERROR_NONE;
42 char *query_str = NULL;
44 query_str = sqlite3_mprintf("INSERT OR IGNORE INTO %q (tag_id, media_id) values (%d, '%q');", DB_TABLE_TAG_MAP, tag_id, media_id);
45 ret = media_svc_append_query(query_str, _content_get_uid());
47 return _content_error_capi(ret);
50 static int __media_tag_remove_item_from_tag(int tag_id, const char *media_id)
52 int ret = MEDIA_CONTENT_ERROR_NONE;
53 char *query_str = NULL;
55 query_str = sqlite3_mprintf(REMOVE_TAG_ITEM_FROM_TAG_MAP, tag_id, media_id);
56 ret = media_svc_append_query(query_str, _content_get_uid());
58 return _content_error_capi(ret);
61 static int __media_tag_update_tag_name(int tag_id, const char *tag_name)
63 int ret = MEDIA_CONTENT_ERROR_NONE;
64 char *query_str = NULL;
66 query_str = sqlite3_mprintf(UPDATE_TAG_NAME_FROM_TAG, tag_name, tag_id);
67 ret = media_svc_append_query(query_str, _content_get_uid());
69 return _content_error_capi(ret);
72 static int __media_tag_get_tag_info_from_db(const char *name, media_tag_h tag)
74 int ret = MEDIA_CONTENT_ERROR_NONE;
75 sqlite3_stmt *stmt = NULL;
76 char *select_query = NULL;
77 media_tag_s *_tag = (media_tag_s *)tag;
79 content_retip_if_fail(tag);
81 select_query = sqlite3_mprintf(SELECT_TAG_BY_NAME, name);
83 ret = _content_get_result(select_query, &stmt);
84 sqlite3_free(select_query);
85 content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
87 if (sqlite3_step(stmt) == SQLITE_ROW) {
88 _tag->tag_id = sqlite3_column_int(stmt, 0);
89 _tag->name = g_strdup(name);
91 content_error("Nonexistent tag name[%s]", name);
92 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
95 sqlite3_finalize(stmt);
100 int media_tag_insert_to_db(const char *tag_name, media_tag_h *tag)
102 content_warn("DEPRECATION WARNING: media_tag_insert_to_db() is deprecated and will be removed from next release.");
103 int ret = MEDIA_CONTENT_ERROR_NONE;
104 char *query_str = NULL;
105 media_tag_s *_tag = NULL;
107 content_retip_if_fail(STRING_VALID(tag_name));
108 content_retip_if_fail(tag);
110 query_str = sqlite3_mprintf(INSERT_TAG_TO_TAG, tag_name);
111 ret = _content_query_sql(query_str);
112 sqlite3_free(query_str);
113 content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, ret, "fail to insert tag");
115 _tag = g_new0(media_tag_s, 1);
117 ret = __media_tag_get_tag_info_from_db(tag_name, (media_tag_h)_tag);
118 *tag = (media_tag_h)_tag;
123 int media_tag_delete_from_db(int tag_id)
125 content_warn("DEPRECATION WARNING: media_tag_delete_from_db() is deprecated and will be removed from next release.");
126 int ret = MEDIA_CONTENT_ERROR_NONE;
127 char *query_str = NULL;
129 content_retipm_if_fail(tag_id > 0, "%d", tag_id);
131 query_str = sqlite3_mprintf(DELETE_TAG_FROM_TAG, tag_id);
132 ret = _content_query_sql(query_str);
134 sqlite3_free(query_str);
139 int media_tag_get_tag_count_from_db(filter_h filter, int *tag_count)
141 content_warn("DEPRECATION WARNING: media_tag_get_tag_count_from_db() is deprecated and will be removed from next release.");
142 content_retip_if_fail(tag_count);
144 return _media_db_get_group_count(filter, MEDIA_GROUP_TAG, tag_count);
147 int media_tag_foreach_tag_from_db(filter_h filter, media_tag_cb callback, void *user_data)
149 content_warn("DEPRECATION WARNING: media_tag_foreach_tag_from_db() is deprecated and will be removed from next release.");
150 content_retip_if_fail(callback);
152 return _media_db_get_tag(NULL, filter, callback, user_data);
155 int media_tag_get_media_count_from_db(int tag_id, filter_h filter, int *media_count)
157 content_warn("DEPRECATION WARNING: media_tag_get_media_count_from_db() is deprecated and will be removed from next release.");
158 content_retipm_if_fail(tag_id > 0, "%d", tag_id);
159 content_retip_if_fail(media_count);
161 return _media_db_get_group_item_count_by_id(tag_id, filter, MEDIA_GROUP_TAG, media_count);
164 int media_tag_foreach_media_from_db(int tag_id, filter_h filter, media_info_cb callback, void *user_data)
166 content_warn("DEPRECATION WARNING: media_tag_foreach_media_from_db() is deprecated and will be removed from next release.");
167 content_retip_if_fail(callback);
169 return _media_db_get_group_item_by_id(tag_id, filter, callback, user_data, MEDIA_GROUP_TAG);
172 int media_tag_destroy(media_tag_h tag)
174 content_warn("DEPRECATION WARNING: media_tag_destroy() is deprecated and will be removed from next release.");
175 media_tag_s *_tag = (media_tag_s *)tag;
177 content_retip_if_fail(tag);
179 __media_tag_item_release(_tag);
184 return MEDIA_CONTENT_ERROR_NONE;
187 int media_tag_clone(media_tag_h *dst, media_tag_h src)
189 content_warn("DEPRECATION WARNING: media_tag_clone() is deprecated and will be removed from next release.");
190 media_tag_s *_src = (media_tag_s *)src;
191 media_tag_s *_dst = NULL;
193 content_retip_if_fail(src);
194 content_retip_if_fail(dst);
196 _dst = g_new0(media_tag_s, 1);
198 _dst->tag_id = _src->tag_id;
199 _dst->name = g_strdup(_src->name);
201 *dst = (media_tag_h)_dst;
203 return MEDIA_CONTENT_ERROR_NONE;
206 int media_tag_get_tag_id(media_tag_h tag, int *tag_id)
208 content_warn("DEPRECATION WARNING: media_tag_get_tag_id() is deprecated and will be removed from next release.");
209 media_tag_s *_tag = (media_tag_s *)tag;
211 content_retip_if_fail(tag);
212 content_retip_if_fail(tag_id);
214 *tag_id = _tag->tag_id;
216 return MEDIA_CONTENT_ERROR_NONE;
219 int media_tag_get_name(media_tag_h tag, char **name)
221 content_warn("DEPRECATION WARNING: media_tag_get_name() is deprecated and will be removed from next release.");
222 media_tag_s *_tag = (media_tag_s *)tag;
224 content_retip_if_fail(tag);
225 content_retip_if_fail(name);
227 *name = g_strdup(_tag->name);
229 return MEDIA_CONTENT_ERROR_NONE;
232 int media_tag_get_tag_from_db(int tag_id, media_tag_h *tag)
234 content_warn("DEPRECATION WARNING: media_tag_get_tag_from_db() is deprecated and will be removed from next release.");
235 int ret = MEDIA_CONTENT_ERROR_NONE;
236 sqlite3_stmt *stmt = NULL;
238 media_tag_s *_tag = NULL;
240 content_retipm_if_fail(tag_id > 0, "%d", tag_id);
241 content_retip_if_fail(tag);
243 query = sqlite3_mprintf(SELECT_TAG_FROM_TAG, tag_id);
244 ret = _content_get_result(query, &stmt);
246 content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
248 if (sqlite3_step(stmt) == SQLITE_ROW) {
249 _tag = g_new0(media_tag_s, 1);
251 _tag->tag_id = tag_id;
252 _tag->name = g_strdup((const char *)sqlite3_column_text(stmt, 0));
254 *tag = (media_tag_h)_tag;
256 content_error("Nonexistent tag id[%d]", tag_id);
257 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
260 sqlite3_finalize(stmt);
265 int media_tag_add_media(media_tag_h tag, const char *media_id)
267 content_warn("DEPRECATION WARNING: media_tag_add_media() is deprecated and will be removed from next release.");
268 int ret = MEDIA_CONTENT_ERROR_NONE;
269 media_tag_s *_tag = (media_tag_s *)tag;
270 media_tag_item_s *_item = NULL;
272 content_retip_if_fail(tag);
273 content_retip_if_fail(STRING_VALID(media_id));
275 _item = g_new0(media_tag_item_s, 1);
277 _item->media_id = g_strdup(media_id);
278 _item->function = MEDIA_TAG_ADD;
280 _tag->item_list = g_list_append(_tag->item_list, _item);
285 int media_tag_remove_media(media_tag_h tag, const char *media_id)
287 content_warn("DEPRECATION WARNING: media_tag_remove_media() is deprecated and will be removed from next release.");
288 int ret = MEDIA_CONTENT_ERROR_NONE;
289 media_tag_s *_tag = (media_tag_s *)tag;
290 media_tag_item_s *_item = NULL;
292 content_retip_if_fail(tag);
293 content_retip_if_fail(STRING_VALID(media_id));
295 _item = g_new0(media_tag_item_s, 1);
297 _item->media_id = g_strdup(media_id);
298 _item->function = MEDIA_TAG_REMOVE;
300 _tag->item_list = g_list_append(_tag->item_list, _item);
305 int media_tag_set_name(media_tag_h tag, const char *tag_name)
307 content_warn("DEPRECATION WARNING: media_tag_set_name() is deprecated and will be removed from next release.");
308 int ret = MEDIA_CONTENT_ERROR_NONE;
309 media_tag_s *_tag = (media_tag_s *)tag;
310 media_tag_item_s *_item = NULL;
312 content_retip_if_fail(tag);
313 content_retip_if_fail(STRING_VALID(tag_name));
317 _item = g_new0(media_tag_item_s, 1);
319 _item->tag_name = g_strdup(tag_name);
320 _item->function = MEDIA_TAG_UPDATE_TAG_NAME;
322 _tag->name = g_strdup(tag_name);
324 _tag->item_list = g_list_append(_tag->item_list, _item);
329 void __media_tag_foreach_request(gpointer data, gpointer user_data)
331 int ret = MEDIA_CONTENT_ERROR_NONE;
332 media_tag_item_s *_tag_item = (media_tag_item_s *)data;
333 int tag_id = GPOINTER_TO_INT(user_data);
338 switch (_tag_item->function) {
340 ret = __media_tag_insert_item_to_tag(tag_id, _tag_item->media_id);
343 case MEDIA_TAG_REMOVE:
344 ret = __media_tag_remove_item_from_tag(tag_id, _tag_item->media_id);
347 case MEDIA_TAG_UPDATE_TAG_NAME:
348 ret = __media_tag_update_tag_name(tag_id, _tag_item->tag_name);
355 if (ret != MEDIA_CONTENT_ERROR_NONE)
356 content_error("Failed some operation[%d]", _tag_item->function);
359 int media_tag_update_to_db(media_tag_h tag)
361 content_warn("DEPRECATION WARNING: media_tag_update_to_db() is deprecated and will be removed from next release.");
362 int ret = MEDIA_CONTENT_ERROR_NONE;
363 media_tag_s *_tag = (media_tag_s *)tag;
365 content_retip_if_fail(tag);
366 content_retvm_if(_tag->item_list == NULL, MEDIA_CONTENT_ERROR_NONE, "operation list length is 0");
368 g_list_foreach(_tag->item_list, __media_tag_foreach_request, GINT_TO_POINTER(_tag->tag_id));
370 ret = media_svc_send_query(_content_get_uid());
372 __media_tag_item_release(_tag);
374 return _content_error_capi(ret);
377 int media_tag_create(media_tag_h *tag)
379 content_warn("DEPRECATION WARNING: media_tag_create() is deprecated and will be removed from next release.");
380 content_retip_if_fail(tag);
382 media_tag_s *_tag = g_new0(media_tag_s, 1);
386 _tag->item_list = NULL;
388 *tag = (media_tag_h)_tag;
390 return MEDIA_CONTENT_ERROR_NONE;
393 int media_tag_insert_to_db_v2(media_tag_h tag)
395 content_warn("DEPRECATION WARNING: media_tag_insert_to_db_v2() is deprecated and will be removed from next release.");
396 int ret = MEDIA_CONTENT_ERROR_NONE;
397 media_tag_s *_tag = (media_tag_s *)tag;
401 content_retip_if_fail(tag);
402 content_retip_if_fail(STRING_VALID(_tag->name));
403 content_retip_if_fail(_tag->tag_id == -1);
405 ret = media_tag_insert_to_db(_tag->name, &tag_1);
406 if (ret != MEDIA_CONTENT_ERROR_NONE) {
407 content_error("fail media_tag_insert_to_db");
411 ret = media_tag_get_tag_id(tag_1, &tag_id);
412 if (ret != MEDIA_CONTENT_ERROR_NONE) {
413 content_error("fail media_tag_get_tag_id");
417 _tag->tag_id = tag_id;
419 ret = media_tag_update_to_db(tag);
422 __media_tag_item_release(_tag);
423 media_tag_destroy(tag_1);
428 int media_tag_update_to_db_v2(int tag_id, media_tag_h tag)
430 content_warn("DEPRECATION WARNING: media_tag_update_to_db_v2() is deprecated and will be removed from next release.");
431 media_tag_s *_tag = (media_tag_s *)tag;
433 content_retip_if_fail(tag);
434 content_retip_if_fail(tag_id > 0);
436 _tag->tag_id = tag_id;
438 return media_tag_update_to_db(tag);