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_filter.h>
19 #include <media_info.h>
20 #include <media_info_private.h>
21 #include <media_playlist.h>
23 #define PLAYLIST_ARRAY_SIZE 20
24 #define PLAYLIST_ARRAY_EXPAND 10
25 #define MAX_TMP_STR 2048
27 static __thread GList *g_playlist_item_list = NULL;
29 static void __media_playlist_item_add(media_playlist_item_s *item_s);
30 static void __media_playlist_item_release(void);
31 static int __media_playlist_insert_playlist_record(const char *playlist_name, int *playlist_id);
32 static int __media_playlist_insert_item_to_playlist(int playlist_id, const char *media_id);
33 static int __media_playlist_remove_item_from_playlist(int playlist_id, int playlist_member_id);
34 static int __media_playlist_update_playlist_name(int playlist_id, const char *playlist_name);
35 static int __media_playlist_update_thumbnail_path(int playlist_id, const char *path);
36 static int __media_playlist_update_play_order(int playlist_id, int playlist_member_id, int play_order);
37 static int __media_playlist_reset_file(const char* playlist_path);
38 static int __media_playlist_append_to_file(const char* playlist_path, const char* path);
39 static int __media_playlist_import_item_from_file(const char* playlist_path, char*** const item_list, int* item_count);
40 static int __media_playlist_destroy_import_item(char** item_list, int item_count);
41 static void __media_playlist_destroy_export_item(gpointer data);
43 static void __media_playlist_item_add(media_playlist_item_s *item_s)
45 g_playlist_item_list = g_list_append(g_playlist_item_list, item_s);
48 static void __media_playlist_item_release(void)
52 media_playlist_item_s *item = NULL;
54 list_cnt = g_list_length(g_playlist_item_list);
56 media_content_debug("list_cnt : [%d]", list_cnt);
58 for(idx = 0; idx < list_cnt; idx++)
60 item = (media_playlist_item_s*)g_list_nth_data(g_playlist_item_list, idx);
63 SAFE_FREE(item->media_id);
64 SAFE_FREE(item->playlist_name);
65 SAFE_FREE(item->thumbnail_path);
70 g_list_free(g_playlist_item_list);
71 g_playlist_item_list = NULL;
75 static int __media_playlist_insert_playlist_record(const char *playlist_name, int *playlist_id)
77 int ret = MEDIA_CONTENT_ERROR_NONE;
78 char *query_str = NULL;
79 char *select_query = NULL;
80 sqlite3_stmt *stmt = NULL;
82 query_str = sqlite3_mprintf(INSERT_PLAYLIST_TO_PLAYLIST, playlist_name);
84 ret = _content_query_sql(query_str);
85 sqlite3_free(query_str);
86 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
88 select_query = sqlite3_mprintf(SELECT_PLAYLIST_ID_FROM_PLAYLIST, playlist_name);
90 ret = _content_query_prepare(&stmt, select_query, NULL, NULL);
91 sqlite3_free(select_query);
92 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
94 while(sqlite3_step(stmt) == SQLITE_ROW)
96 *playlist_id = (int)sqlite3_column_int(stmt,0);
99 SQLITE3_FINALIZE(stmt);
101 return MEDIA_CONTENT_ERROR_NONE;
104 static int __media_playlist_insert_item_to_playlist(int playlist_id, const char *media_id)
106 int ret = MEDIA_CONTENT_ERROR_NONE;
107 sqlite3_stmt *stmt = NULL;
108 char select_query[DEFAULT_QUERY_SIZE];
109 char *query_str = NULL;
112 memset(select_query, 0x00, sizeof(select_query));
114 snprintf(select_query, sizeof(select_query), SELECT_MAX_PLAY_ORDER_FROM_PLAYLIST_VIEW, playlist_id);
116 /* get the max play_order */
117 ret = _content_query_prepare(&stmt, select_query, NULL, NULL);
118 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
120 while(sqlite3_step(stmt) == SQLITE_ROW)
122 play_order = (int)sqlite3_column_int(stmt, 0);
125 SQLITE3_FINALIZE(stmt);
129 query_str = sqlite3_mprintf("INSERT INTO %q (playlist_id, media_uuid, play_order) values (%d, '%q', %d)",
130 DB_TABLE_PLAYLIST_MAP, playlist_id, media_id, play_order);
131 ret = _content_query_sql(query_str);
132 sqlite3_free(query_str);
137 static int __media_playlist_remove_item_from_playlist(int playlist_id, int playlist_member_id)
139 int ret = MEDIA_CONTENT_ERROR_NONE;
140 char *query_str = NULL;
142 query_str = sqlite3_mprintf(REMOVE_PLAYLIST_ITEM_FROM_PLAYLIST_MAP, playlist_id, playlist_member_id);
144 ret = _content_query_sql(query_str);
145 sqlite3_free(query_str);
150 static int __media_playlist_update_playlist_name(int playlist_id, const char *playlist_name)
152 int ret = MEDIA_CONTENT_ERROR_NONE;
153 char *query_str = NULL;
155 query_str = sqlite3_mprintf(UPDATE_PLAYLIST_NAME_FROM_PLAYLIST, playlist_name, playlist_id);
157 ret = _content_query_sql(query_str);
158 sqlite3_free(query_str);
163 static int __media_playlist_update_thumbnail_path(int playlist_id, const char *path)
165 int ret = MEDIA_CONTENT_ERROR_NONE;
166 char *query_str = NULL;
168 query_str = sqlite3_mprintf(UPDATE_PLAYLIST_THUMBNAIL_FROM_PLAYLIST, path, playlist_id);
170 ret = _content_query_sql(query_str);
171 sqlite3_free(query_str);
176 static int __media_playlist_update_play_order(int playlist_id, int playlist_member_id, int play_order)
178 int ret = MEDIA_CONTENT_ERROR_NONE;
179 char *query_str = NULL;
181 query_str = sqlite3_mprintf(UPDATE_PLAYLIST_ORDER_FROM_PLAYLIST_MAP, play_order, playlist_id, playlist_member_id);
183 ret = _content_query_sql(query_str);
184 sqlite3_free(query_str);
189 static bool __media_playlist_media_info_cb(media_info_h media, void *user_data)
191 int ret = MEDIA_CONTENT_ERROR_NONE;
192 char **media_id = (char**)user_data;
193 ret = media_info_get_media_id(media, media_id);
194 if(ret != MEDIA_CONTENT_ERROR_NONE)
196 media_content_error("Fail to get file path");
202 static bool __media_playlist_member_cb(int playlist_member_id, media_info_h media, void *user_data)
204 int ret = MEDIA_CONTENT_ERROR_NONE;
205 GList **list = (GList**)user_data;
208 ret = media_info_get_file_path(media, &path);
209 if(ret != MEDIA_CONTENT_ERROR_NONE)
211 media_content_error("Fail to get file path");
215 *list = g_list_append(*list, path);
220 static int __media_playlist_reset_file(const char* playlist_path)
224 fp = fopen(playlist_path, "wb");
227 media_content_stderror("fopen fail");
228 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
231 fputs("", fp); // remove previous playlist
235 return MEDIA_CONTENT_ERROR_NONE;
238 static int __media_playlist_append_to_file(const char* playlist_path, const char* path)
242 fp = fopen(playlist_path, "a"); // append only
245 media_content_stderror("fopen fail");
246 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
255 return MEDIA_CONTENT_ERROR_NONE;
258 static int __media_playlist_import_item_from_file(const char* playlist_path, char*** const item_list, int* item_count)
260 int current_index = 0; // Current record number
261 int current_max_size = PLAYLIST_ARRAY_SIZE; // Current max number of records in array
262 int tmp_str_len = 0; // Length of the string
264 char *tmp_buf = NULL;
265 char *tmp_str = NULL; // Next line from buffer, this string is used for parsing
268 long int file_size = 0;
270 *item_list = NULL; *item_count = 0;
272 fp = fopen(playlist_path, "rb"); // Open as binary for precise estimation of file length
275 media_content_stderror("fopen fail");
276 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
279 fseek(fp, 0, SEEK_END); // Move to the end of file
280 file_size = ftell(fp); // Here we can find the size of file
281 fseek(fp, 0 , SEEK_SET); // Return to the beginning of file
284 media_content_debug("file is empty.");
286 return MEDIA_CONTENT_ERROR_NONE;
288 // Allocate the memory and copy file content there
290 buf = malloc(file_size + 1);
294 media_content_error("Out of Memory");
296 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
301 if(fread(buf, file_size, 1, fp) != 1) {
304 media_content_stderror("fread fail");
305 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
310 // Preliminary memory allocation
311 *item_list = calloc(current_max_size, sizeof(char*));
312 tmp_str = malloc(MAX_TMP_STR);
313 if (tmp_str == NULL || *item_list == NULL) {
314 SAFE_FREE(*item_list);
317 media_content_error("Out of Memory");
318 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
320 // Here we create format string for sscanf(...) that allows to get a line from buffer
322 snprintf(format, 25, "%%%d[^\n]", MAX_TMP_STR);
324 // This cycle gets lines one by one from buffer till the end of buffer. Empty line ("\n") must be treated specifically
325 while((sscanf(tmp_buf, format, tmp_str) == 1) || (*tmp_buf == '\n')) {
326 if(*tmp_buf == '\n') {// Check if there is an empty line, skip '\n' symbol
329 if(tmp_buf < (buf + file_size))
330 continue; // We are still in buffer
332 break; // Empty line was in the end of buffer
335 tmp_str_len = strlen(tmp_str); // Save the length of line
337 if(tmp_str[0] != '#') { // Check that the line is not a comment
338 if(!(current_index < (current_max_size - 1))) { // Check if we have completely filled record array
339 // Expand array size and relocate the array (records will be copied into new one)
340 current_max_size += PLAYLIST_ARRAY_EXPAND;
341 char **tmp_ptr = calloc(current_max_size, sizeof(char*));
342 if (tmp_ptr == NULL) {
343 __media_playlist_destroy_import_item(*item_list, current_index);
346 media_content_error("Out of Memory");
347 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
349 memmove(tmp_ptr, *item_list, sizeof(char*) * current_index);
350 SAFE_FREE(*item_list);
351 *item_list = tmp_ptr;
354 // Save new file path (current string in tmp_str)
355 (*item_list)[current_index] = malloc(tmp_str_len + 1);
356 if ((*item_list)[current_index] == NULL) {
357 __media_playlist_destroy_import_item(*item_list, current_index);
360 media_content_error("Out of Memory");
361 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
363 memmove((*item_list)[current_index], tmp_str, tmp_str_len + 1);
365 // Increase the index of buffer
369 tmp_buf += (tmp_str_len + 1); // Move position in buffer after the string that was parsed
372 *item_count = current_index; // Now we need to save the number of records in array
375 SAFE_FREE(tmp_str); // Free temporary variables
377 return MEDIA_CONTENT_ERROR_NONE;
380 static int __media_playlist_destroy_import_item(char** item_list, int item_count)
384 for(i = 0; i < item_count; ++i) {
385 SAFE_FREE(item_list[i]);
389 if (item_list != NULL) {
390 SAFE_FREE(item_list);
394 return MEDIA_CONTENT_ERROR_NONE;
397 static void __media_playlist_destroy_export_item(gpointer data)
403 int media_playlist_insert_to_db(const char *name, media_playlist_h *playlist)
405 int ret = MEDIA_CONTENT_ERROR_NONE;
408 if(!STRING_VALID(name))
410 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
411 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
414 media_playlist_s *_playlist = (media_playlist_s*)calloc(1, sizeof(media_playlist_s));
415 if(_playlist == NULL)
417 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
418 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
421 ret = __media_playlist_insert_playlist_record(name, &playlist_id);
423 if(ret != MEDIA_CONTENT_ERROR_NONE)
425 SAFE_FREE(_playlist);
429 _playlist->playlist_id = playlist_id;
430 _playlist->name = strdup(name);
432 if(_playlist->name == NULL)
434 SAFE_FREE(_playlist);
435 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
436 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
439 *playlist = (media_playlist_h)_playlist;
444 int media_playlist_delete_from_db(int playlist_id)
446 int ret = MEDIA_CONTENT_ERROR_NONE;
447 char *query_str = NULL;
451 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
452 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
455 query_str = sqlite3_mprintf(DELETE_PLAYLIST_FROM_PLAYLIST, playlist_id);
457 ret = _content_query_sql(query_str);
459 sqlite3_free(query_str);
464 int media_playlist_get_playlist_count_from_db(filter_h filter, int *playlist_count)
466 int ret = MEDIA_CONTENT_ERROR_NONE;
468 if(playlist_count != NULL)
470 ret = _media_db_get_group_count(filter, MEDIA_GROUP_PLAYLIST, playlist_count);
474 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
475 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
481 int media_playlist_foreach_playlist_from_db(filter_h filter, media_playlist_cb callback, void *user_data)
483 int ret = MEDIA_CONTENT_ERROR_NONE;
487 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
488 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
491 ret = _media_db_get_playlist(filter, callback, user_data);
496 int media_playlist_get_media_count_from_db(int playlist_id, filter_h filter, int *media_count)
498 int ret = MEDIA_CONTENT_ERROR_NONE;
500 if((playlist_id > 0) && (media_count != NULL))
502 ret = _media_db_get_group_item_count_by_id(playlist_id, filter, MEDIA_GROUP_PLAYLIST, media_count);
506 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
507 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
513 int media_playlist_foreach_media_from_db(int playlist_id, filter_h filter, playlist_member_cb callback, void *user_data)
515 int ret = MEDIA_CONTENT_ERROR_NONE;
517 if((playlist_id > 0) && (callback != NULL))
519 ret = _media_db_get_playlist_item(playlist_id, filter, callback, user_data);
523 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
524 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
530 int media_playlist_destroy(media_playlist_h playlist)
532 int ret = MEDIA_CONTENT_ERROR_NONE;
533 media_playlist_s *_playlist = (media_playlist_s*)playlist;
537 SAFE_FREE(_playlist->name);
538 SAFE_FREE(_playlist->thumbnail_path);
539 SAFE_FREE(_playlist);
541 ret = MEDIA_CONTENT_ERROR_NONE;
545 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
546 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
552 int media_playlist_clone(media_playlist_h *dst, media_playlist_h src)
554 int ret = MEDIA_CONTENT_ERROR_NONE;
555 media_playlist_s *_src = (media_playlist_s*)src;
559 media_playlist_s *_dst = (media_playlist_s*)calloc(1, sizeof(media_playlist_s));
562 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
563 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
566 _dst->playlist_id = _src->playlist_id;
568 if(STRING_VALID(_src->name))
570 _dst->name = strdup(_src->name);
571 if(_dst->name == NULL)
573 media_playlist_destroy((media_playlist_h)_dst);
574 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
575 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
579 *dst = (media_playlist_h)_dst;
581 ret = MEDIA_CONTENT_ERROR_NONE;
585 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
586 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
592 int media_playlist_get_playlist_from_db(int playlist_id, media_playlist_h *playlist)
594 int ret = MEDIA_CONTENT_ERROR_NONE;
595 sqlite3_stmt *stmt = NULL;
596 char select_query[DEFAULT_QUERY_SIZE];
600 memset(select_query, 0x00, sizeof(select_query));
602 snprintf(select_query, sizeof(select_query), SELECT_PLAYLIST_FROM_PLAYLIST, playlist_id);
604 ret = _content_query_prepare(&stmt, select_query, NULL, NULL);
605 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
607 while(sqlite3_step(stmt) == SQLITE_ROW)
609 media_playlist_s *_playlist = (media_playlist_s*)calloc(1, sizeof(media_playlist_s));
610 if(_playlist == NULL)
612 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
613 SQLITE3_FINALIZE(stmt);
614 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
617 _playlist->playlist_id = (int)sqlite3_column_int(stmt, 0);
618 if(STRING_VALID((const char *)sqlite3_column_text(stmt, 1)))
619 _playlist->name = strdup((const char *)sqlite3_column_text(stmt, 1));
620 if(STRING_VALID((const char *)sqlite3_column_text(stmt, 3)))
621 _playlist->thumbnail_path = strdup((const char *)sqlite3_column_text(stmt, 3));
623 *playlist = (media_playlist_h)_playlist;
626 SQLITE3_FINALIZE(stmt);
630 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
631 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
637 int media_playlist_get_playlist_id(media_playlist_h playlist, int *playlist_id)
639 int ret = MEDIA_CONTENT_ERROR_NONE;
641 media_playlist_s *_playlist = (media_playlist_s*)playlist;
643 if((_playlist != NULL) && (playlist_id != NULL))
645 *playlist_id = _playlist->playlist_id;
649 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
650 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
656 int media_playlist_get_name(media_playlist_h playlist, char **name)
658 int ret = MEDIA_CONTENT_ERROR_NONE;
659 media_playlist_s *_playlist = (media_playlist_s*)playlist;
662 if(STRING_VALID(_playlist->name))
664 *name = strdup(_playlist->name);
667 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
668 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
676 ret = MEDIA_CONTENT_ERROR_NONE;
680 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
681 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
687 int media_playlist_get_thumbnail_path(media_playlist_h playlist, char **path)
689 int ret = MEDIA_CONTENT_ERROR_NONE;
690 media_playlist_s *_playlist = (media_playlist_s*)playlist;
693 if(STRING_VALID(_playlist->thumbnail_path))
695 *path = strdup(_playlist->thumbnail_path);
698 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
699 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
707 ret = MEDIA_CONTENT_ERROR_NONE;
711 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
712 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
718 int media_playlist_get_play_order(media_playlist_h playlist, int playlist_member_id, int *play_order)
720 int ret = MEDIA_CONTENT_ERROR_NONE;
721 media_playlist_s *_playlist = (media_playlist_s*)playlist;
723 sqlite3_stmt *stmt = NULL;
724 char select_query[DEFAULT_QUERY_SIZE];
726 if((_playlist == NULL) || (playlist_member_id < 0) || (play_order == NULL))
728 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
729 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
732 playlist_id = _playlist->playlist_id;
734 memset(select_query, 0x00, sizeof(select_query));
736 snprintf(select_query, sizeof(select_query), SELECT_PLAY_ORDER_FROM_PLAYLIST_VIEW, playlist_id, playlist_member_id);
738 ret = _content_query_prepare(&stmt, select_query, NULL, NULL);
739 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
741 while(sqlite3_step(stmt) == SQLITE_ROW)
743 *play_order = (int)sqlite3_column_int(stmt, 0);
746 SQLITE3_FINALIZE(stmt);
751 int media_playlist_set_name(media_playlist_h playlist, const char *playlist_name)
753 int ret = MEDIA_CONTENT_ERROR_NONE;
754 media_playlist_s *_playlist = (media_playlist_s*)playlist;
756 if(_playlist != NULL && STRING_VALID(playlist_name))
758 SAFE_FREE(_playlist->name);
760 media_playlist_item_s *item = (media_playlist_item_s*)calloc(1, sizeof(media_playlist_item_s));
763 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
764 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
767 item->playlist_name = strdup(playlist_name);
768 item->function = MEDIA_PLAYLIST_UPDATE_PLAYLIST_NAME;
769 if(item->playlist_name == NULL)
772 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
773 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
776 _playlist->name = strdup(playlist_name);
777 if(_playlist->name == NULL)
779 SAFE_FREE(item->playlist_name);
781 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
782 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
785 __media_playlist_item_add(item);
789 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
790 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
796 int media_playlist_set_thumbnail_path(media_playlist_h playlist, const char *path)
798 int ret = MEDIA_CONTENT_ERROR_NONE;
799 media_playlist_s *_playlist = (media_playlist_s*)playlist;
801 if(_playlist != NULL && STRING_VALID(path))
803 SAFE_FREE(_playlist->thumbnail_path);
805 media_playlist_item_s *item = (media_playlist_item_s*)calloc(1, sizeof(media_playlist_item_s));
808 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
809 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
812 item->thumbnail_path = strdup(path);
813 item->function = MEDIA_PLAYLIST_UPDATE_THUMBNAIL_PATH;
814 if(item->thumbnail_path == NULL)
817 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
818 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
821 _playlist->thumbnail_path = strdup(path);
822 if(_playlist->thumbnail_path == NULL)
824 SAFE_FREE(item->thumbnail_path);
826 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
827 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
830 __media_playlist_item_add(item);
834 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
835 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
841 int media_playlist_set_play_order(media_playlist_h playlist, int playlist_member_id, int play_order)
843 int ret = MEDIA_CONTENT_ERROR_NONE;
844 media_playlist_s *_playlist = (media_playlist_s*)playlist;
846 if((_playlist != NULL) && (playlist_member_id > 0) && (play_order >= 0))
848 media_playlist_item_s *item = (media_playlist_item_s*)calloc(1, sizeof(media_playlist_item_s));
851 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
852 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
855 item->playlist_member_id = playlist_member_id;
856 item->function = MEDIA_PLAYLIST_UPDATE_PLAY_ORDER;
857 item->play_order = play_order;
859 __media_playlist_item_add(item);
863 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
864 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
870 int media_playlist_add_media(media_playlist_h playlist, const char *media_id)
872 int ret = MEDIA_CONTENT_ERROR_NONE;
873 media_playlist_s *_playlist = (media_playlist_s*)playlist;
875 if(_playlist != NULL && STRING_VALID(media_id))
877 media_playlist_item_s *item = (media_playlist_item_s*)calloc(1, sizeof(media_playlist_item_s));
880 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
881 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
884 item->media_id = strdup(media_id);
885 item->function = MEDIA_PLAYLIST_ADD;
887 if(item->media_id == NULL)
890 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
891 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
894 __media_playlist_item_add(item);
898 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
899 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
906 int media_playlist_remove_media(media_playlist_h playlist, int playlist_member_id)
908 int ret = MEDIA_CONTENT_ERROR_NONE;
909 media_playlist_s *_playlist = (media_playlist_s*)playlist;
911 if((_playlist != NULL) && (playlist_member_id > 0))
913 media_playlist_item_s *item = (media_playlist_item_s*)calloc(1, sizeof(media_playlist_item_s));
916 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
917 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
920 item->playlist_member_id = playlist_member_id;
921 item->function = MEDIA_PLAYLIST_REMOVE;
923 __media_playlist_item_add(item);
927 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
928 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
934 int media_playlist_update_to_db(media_playlist_h playlist)
936 int ret = MEDIA_CONTENT_ERROR_NONE;
937 media_playlist_s *_playlist = (media_playlist_s*)playlist;
940 media_playlist_item_s *_playlist_item = NULL;
942 if(_playlist == NULL)
944 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
945 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
948 length = g_list_length(g_playlist_item_list);
950 for (idx = 0; idx < length; idx++) {
951 _playlist_item = (media_playlist_item_s*)g_list_nth_data(g_playlist_item_list, idx);
952 if(_playlist_item != NULL) {
953 switch(_playlist_item->function) {
954 case MEDIA_PLAYLIST_ADD:
956 ret = __media_playlist_insert_item_to_playlist(_playlist->playlist_id, _playlist_item->media_id);
960 case MEDIA_PLAYLIST_REMOVE:
962 ret = __media_playlist_remove_item_from_playlist(_playlist->playlist_id, _playlist_item->playlist_member_id);
966 case MEDIA_PLAYLIST_UPDATE_PLAYLIST_NAME:
968 ret = __media_playlist_update_playlist_name(_playlist->playlist_id, _playlist_item->playlist_name);
972 case MEDIA_PLAYLIST_UPDATE_THUMBNAIL_PATH:
974 ret = __media_playlist_update_thumbnail_path(_playlist->playlist_id, _playlist_item->thumbnail_path);
978 case MEDIA_PLAYLIST_UPDATE_PLAY_ORDER:
980 ret = __media_playlist_update_play_order(_playlist->playlist_id, _playlist_item->playlist_member_id, _playlist_item->play_order);
987 __media_playlist_item_release();
992 int media_playlist_import_from_file(const char *path, const char *playlist_name, media_playlist_h *playlist)
994 int ret = MEDIA_CONTENT_ERROR_NONE;
995 char** import_item_list = NULL;
996 int import_item_count = 0;
999 if(!STRING_VALID(path))
1001 media_content_error("Invalid path");
1002 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1005 if(!STRING_VALID(playlist_name))
1007 media_content_error("Invalid playlist_name");
1008 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1011 ret = media_playlist_insert_to_db(playlist_name, playlist);
1012 if(ret != MEDIA_CONTENT_ERROR_NONE)
1014 media_content_error("Fail to insert playlist to db");
1018 ret = __media_playlist_import_item_from_file(path, &import_item_list, &import_item_count);
1019 if(ret != MEDIA_CONTENT_ERROR_NONE)
1021 __media_playlist_destroy_import_item(import_item_list, import_item_count);
1022 media_content_error("Fail to get playlist from file");
1026 if (import_item_count == 0)
1028 media_content_debug("The playlist from file is empty");
1031 for (idx=0; idx < import_item_count; idx++)
1033 filter_h filter = NULL;
1034 char *media_id = NULL;
1035 char *condition = NULL;
1037 ret = media_filter_create(&filter);
1038 if(ret != MEDIA_CONTENT_ERROR_NONE)
1040 __media_playlist_destroy_import_item(import_item_list, import_item_count);
1041 media_filter_destroy(filter);
1042 media_content_error("error media_filter_create");
1045 condition = sqlite3_mprintf("path = '%q'", import_item_list[idx]);
1046 ret = media_filter_set_condition(filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
1047 if(ret != MEDIA_CONTENT_ERROR_NONE)
1049 __media_playlist_destroy_import_item(import_item_list, import_item_count);
1050 media_filter_destroy(filter);
1051 sqlite3_free(condition);
1052 media_content_error("error media_filter_set_condition");
1055 ret = _media_db_get_group_item(NULL, filter, __media_playlist_media_info_cb, &media_id, MEDIA_GROUP_NONE);
1056 if(ret != MEDIA_CONTENT_ERROR_NONE)
1058 __media_playlist_destroy_import_item(import_item_list, import_item_count);
1059 media_filter_destroy(filter);
1060 SAFE_FREE(media_id);
1061 sqlite3_free(condition);
1062 media_content_error("error media_info_foreach_media_from_db");
1065 ret = media_playlist_add_media((media_playlist_h)*playlist, media_id);
1066 if(ret != MEDIA_CONTENT_ERROR_NONE)
1068 __media_playlist_destroy_import_item(import_item_list, import_item_count);
1069 media_filter_destroy(filter);
1070 SAFE_FREE(media_id);
1071 sqlite3_free(condition);
1072 media_content_error("error media_playlist_add_media");
1075 media_filter_destroy(filter);
1076 SAFE_FREE(media_id);
1077 sqlite3_free(condition);
1080 ret = media_playlist_update_to_db(*playlist);
1081 if(ret != MEDIA_CONTENT_ERROR_NONE)
1083 __media_playlist_destroy_import_item(import_item_list, import_item_count);
1084 media_content_error("Fail to update playlist to db");
1087 __media_playlist_destroy_import_item(import_item_list, import_item_count);
1092 int media_playlist_export_to_file(media_playlist_h playlist, const char* path)
1094 int ret = MEDIA_CONTENT_ERROR_NONE;
1095 media_playlist_s *_playlist = (media_playlist_s*)playlist;
1096 GList *item_list = NULL;
1099 if(!STRING_VALID(path))
1101 media_content_error("Invalid path");
1102 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1105 if(_playlist == NULL)
1107 media_content_error("Invalid playlist");
1108 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1111 if(_playlist->playlist_id <= 0)
1113 media_content_error("Invalid playlist id");
1114 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1117 ret = _media_db_get_playlist_item(_playlist->playlist_id, NULL, __media_playlist_member_cb, &item_list);
1118 if(ret != MEDIA_CONTENT_ERROR_NONE)
1120 media_content_error("Fail to get playlist from db");
1124 ret = __media_playlist_reset_file(path);
1125 if(ret != MEDIA_CONTENT_ERROR_NONE)
1127 g_list_free_full(item_list, __media_playlist_destroy_export_item);
1128 media_content_error("Fail to init playlist file");
1132 for (idx=0; idx < g_list_length(item_list); idx++)
1134 char *item = g_list_nth_data(item_list, idx);
1135 ret = __media_playlist_append_to_file(path, item);
1136 if(ret != MEDIA_CONTENT_ERROR_NONE)
1138 g_list_free_full(item_list, __media_playlist_destroy_export_item);
1139 media_content_error("Fail to export paths into file");
1144 g_list_free_full(item_list, __media_playlist_destroy_export_item);