change the query
[framework/api/media-content.git] / src / media_info.c
1 /*\r
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved\r
3 *\r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5 * you may not use this file except in compliance with the License.\r
6 * You may obtain a copy of the License at\r
7 *\r
8 * http://www.apache.org/licenses/LICENSE-2.0\r
9 *\r
10 * Unless required by applicable law or agreed to in writing, software\r
11 * distributed under the License is distributed on an "AS IS" BASIS,\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 * See the License for the specific language governing permissions and\r
14 * limitations under the License. \r
15 */\r
16 \r
17 \r
18 #include <media_content.h>\r
19 #include <media_info_private.h>\r
20 #include <media-svc.h>\r
21 #include <audio-svc.h>\r
22 #include <audio-svc-error.h>\r
23 #include <audio-svc-types.h>\r
24 \r
25 #include <visual-svc-error.h>\r
26 #include <visual-svc-types.h>\r
27 #include <visual-svc.h>\r
28 \r
29 #include <drm-service.h>\r
30 #include <aul.h>\r
31 \r
32 #include <dlog.h>\r
33 \r
34 #ifdef LOG_TAG\r
35 #undef LOG_TAG\r
36 #endif\r
37 \r
38 #define LOG_TAG "TIZEN_N_MEDIACONTENT"\r
39 \r
40 extern MediaSvcHandle* db_handle;\r
41 \r
42 \r
43 #define SELECT_AUDIO_FROM_MEDIA "select audio_uuid,genre,album,artist,author,year,copyright,description,format,bitrate,track_num,duration,rating,played_count,last_played_time,added_time,size,category from audio_media where audio_uuid='%s' "\r
44 #define SELECT_TAG_LIST_FROM_MEDIA "select t._id, t.tag_name from (select _id, tag_name from visual_tag ORDER BY tag_name ASC ) t, ( select visual_uuid, tag_id from visual_tag_map where visual_uuid='%s' ) tm, ( select visual_uuid, folder_uuid from visual_media) m, ( select folder_uuid, lock_status from visual_folder where valid=1 ) f where tm.tag_id = t._id and m.visual_uuid = tm.visual_uuid and m.folder_uuid = f.folder_uuid and f.lock_status=0; "\r
45 \r
46 int media_info_insert_to_db(media_content_type_e type,const char* path)\r
47 {\r
48         int ret= MEDIA_CONTENT_ERROR_NONE;\r
49         int storage_type;\r
50         int category=0;\r
51         minfo_file_type _type;\r
52         if(path == NULL)\r
53         {\r
54                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
55                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
56         }\r
57 \r
58         switch(type)\r
59         {\r
60                 case MEDIA_CONTENT_TYPE_IMAGE:\r
61                         _type = MINFO_ITEM_IMAGE;\r
62                         ret = minfo_add_media(db_handle, path, _type);\r
63                         ret = _content_error_capi(MEDIA_CONTENT_TYPE, ret);\r
64                 break;\r
65                 case MEDIA_CONTENT_TYPE_VIDEO:\r
66                         _type = MINFO_ITEM_VIDEO;\r
67                         ret = minfo_add_media(db_handle, path, _type);\r
68                         ret = _content_error_capi(MEDIA_CONTENT_TYPE, ret);                     \r
69                 break;\r
70 \r
71                 case MEDIA_CONTENT_TYPE_AUDIO:\r
72                         storage_type = _content_get_storage_type(path);\r
73                         ret = _content_get_audio_category(path,&category);\r
74                         if(ret >= 0)\r
75                         {\r
76                                 ret = audio_svc_insert_item(db_handle, storage_type,path,category);                             \r
77                         }\r
78                         else\r
79                         {\r
80                                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
81                                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;                    \r
82                         }\r
83                 break;\r
84 \r
85                 default:\r
86                 {\r
87                         LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
88                         ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
89                 }\r
90                 break;\r
91 \r
92         }\r
93 \r
94         return ret;\r
95 }\r
96 \r
97 \r
98 \r
99 int media_info_destroy(media_info_h item)\r
100 {\r
101 \r
102         int ret;\r
103         media_info_s* _item = (media_info_s*)item;      \r
104         if(_item) \r
105         {\r
106                 if(_item->item_id)\r
107                 {\r
108                         free(_item->item_id);\r
109                 }\r
110 \r
111                 if(_item->file_path) \r
112                 {\r
113                         free(_item->file_path);\r
114                 }                                               \r
115                 if(_item->display_name) \r
116                 {\r
117                         free(_item->display_name);\r
118                 }                       \r
119                 if(_item->thumbnail) \r
120                 {\r
121                         free(_item->thumbnail);\r
122                 }\r
123                 free(_item);\r
124                 ret = MEDIA_CONTENT_ERROR_NONE;\r
125         }\r
126         else \r
127         {\r
128                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
129                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
130         }\r
131         return ret;\r
132 \r
133 \r
134 \r
135 }\r
136 int media_info_clone(media_info_h* dst, media_info_h src)\r
137 {\r
138         int ret;\r
139         media_info_s* _src = (media_info_s*)src;        \r
140 \r
141 \r
142         if(_src != NULL )\r
143                 {\r
144                 media_info_s* _dst = (media_info_s*)calloc(1,sizeof(media_info_s));             \r
145 \r
146                 if(_dst == NULL)\r
147                 {\r
148                         LOGE("[%s]OUT_OF_MEMORY(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);\r
149                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;       \r
150                 }\r
151                 \r
152                 _dst->item_id = strdup(_src->item_id);\r
153                 if(     _dst->item_id == NULL)\r
154                 {\r
155                         LOGE("[%s]OUT_OF_MEMORY(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);\r
156                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;\r
157                 }\r
158                 _dst->date_modified= _src->date_modified;\r
159                 _dst->favorite = _src->favorite;\r
160                 _dst->media_type = _src->media_type;\r
161                 if((_src->file_path != NULL) && (strlen(_src->file_path) > 0))\r
162                 {\r
163                         _dst->file_path = strdup(_src->file_path);\r
164                         if(_dst->file_path == NULL)\r
165                         {\r
166                                 LOGE("[%s]OUT_OF_MEMORY(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);\r
167                                 media_info_destroy((media_info_h)_dst);\r
168                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;\r
169                         }\r
170                 }\r
171                 if((_src->display_name != NULL) && (strlen(_src->display_name) > 0))\r
172                 {\r
173                         _dst->display_name = strdup(_src->display_name);\r
174                         if(_dst->display_name == NULL)\r
175                         {\r
176                                 LOGE("[%s]OUT_OF_MEMORY(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);\r
177                                 media_info_destroy((media_info_h)_dst);\r
178                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;                       \r
179                         }\r
180                 }\r
181                 if((_src->thumbnail != NULL) && (strlen(_src->thumbnail) > 0))\r
182                 {\r
183                         _dst->thumbnail = strdup(_src->thumbnail);\r
184                         if(_dst->thumbnail == NULL)\r
185                         {\r
186                                 LOGE("[%s]OUT_OF_MEMORY(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);\r
187                                 media_info_destroy((media_info_h)_dst);\r
188                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;                       \r
189                         }\r
190 \r
191                 }               \r
192                 *dst = (media_info_h)_dst;\r
193                 ret = MEDIA_CONTENT_ERROR_NONE;         \r
194         }\r
195         else \r
196         {\r
197                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
198                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
199         }       \r
200                 \r
201         return ret;     \r
202 }\r
203 \r
204 int media_info_get_file_path(media_info_h item, char** path)\r
205 {       \r
206         int ret;\r
207         media_info_s* _item = (media_info_s*)item;      \r
208         \r
209         if(_item) \r
210         {\r
211                 if((_item->file_path != NULL) && (strlen(_item->file_path) > 0))\r
212                 {\r
213                         *path = strdup(_item->file_path);\r
214                         if(*path == NULL)\r
215                         {\r
216                                 LOGE("[%s]OUT_OF_MEMORY(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);\r
217                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;\r
218                         }\r
219                 }\r
220                 else\r
221                 {\r
222                         *path = NULL;\r
223                 }\r
224                 ret = MEDIA_CONTENT_ERROR_NONE;                                 \r
225 \r
226         }\r
227         else \r
228         {\r
229                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
230                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
231         }       \r
232 \r
233         return ret;\r
234 }\r
235         \r
236 \r
237 int media_info_get_display_name(media_info_h item, char** name)\r
238 {       \r
239         int ret;\r
240         media_info_s* _item = (media_info_s*)item;      \r
241         if(_item) \r
242         {\r
243                 if((_item->display_name != NULL) && (strlen(_item->display_name) > 0))\r
244                 {\r
245                         *name = strdup(_item->display_name);\r
246                         if(*name == NULL)\r
247                         {\r
248                                 LOGE("[%s]OUT_OF_MEMORY(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);\r
249                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;                       \r
250                         }\r
251                 }\r
252                 else\r
253                 {\r
254                         *name = NULL;\r
255                 }\r
256                 ret = MEDIA_CONTENT_ERROR_NONE;                                 \r
257 \r
258         }\r
259         else \r
260         {\r
261                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
262                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
263         }       \r
264 \r
265         return ret;\r
266 }\r
267         \r
268 \r
269 int media_info_get_thumbnail_path(media_info_h item, char** path)\r
270 {       \r
271         int ret;\r
272         media_info_s* _item = (media_info_s*)item;      \r
273         if(_item) \r
274         {\r
275                 if((_item->thumbnail != NULL) && (strlen(_item->thumbnail) > 0))\r
276                 {\r
277                         *path = strdup(_item->thumbnail);\r
278                         if(*path == NULL)\r
279                         {\r
280                                 LOGE("[%s]OUT_OF_MEMORY(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);\r
281                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;                               \r
282                         }\r
283                 }\r
284                 else\r
285                 {\r
286                         *path = NULL;\r
287                 }\r
288                 ret = MEDIA_CONTENT_ERROR_NONE;                                 \r
289 \r
290         }\r
291         else \r
292         {\r
293                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
294                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
295         }       \r
296 \r
297         return ret;\r
298 }\r
299         \r
300 \r
301 int media_info_get_date_modified(media_info_h item, time_t* time)\r
302 {\r
303         int ret;\r
304         media_info_s* _item = (media_info_s*)item;      \r
305         if(_item) \r
306         {\r
307                 *time = _item->date_modified;\r
308                 ret = MEDIA_CONTENT_ERROR_NONE; \r
309         }\r
310         else \r
311         {\r
312                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
313                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
314         }       \r
315 \r
316         return ret;\r
317 \r
318 }\r
319 \r
320 int media_info_get_media_type(media_info_h item, int* type)\r
321 {\r
322         int ret;\r
323         media_info_s* _item = (media_info_s*)item;      \r
324         if(_item) \r
325         {\r
326                 *type = _item->media_type;\r
327                 ret = MEDIA_CONTENT_ERROR_NONE; \r
328         }\r
329         else \r
330         {\r
331                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
332                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
333         }       \r
334 \r
335         return ret;\r
336 \r
337 }\r
338 \r
339 int media_info_get_favorite(media_info_h media, int* favorite)\r
340 {\r
341         int ret;\r
342         media_info_s* _media = (media_info_s*)media;\r
343         if(_media)\r
344         {\r
345 \r
346                 *favorite = _media->favorite;\r
347                 ret = MEDIA_CONTENT_ERROR_NONE;\r
348         }\r
349         else\r
350         {\r
351                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
352                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
353         }\r
354 \r
355         return ret;\r
356 \r
357 }\r
358 \r
359 \r
360 int media_info_update_favorite_to_db(media_info_h media, int favorite)\r
361 {\r
362         int ret = MEDIA_CONTENT_ERROR_NONE;\r
363         media_info_s* _media = (media_info_s*)media;\r
364 \r
365         if(media != NULL)\r
366         {\r
367                 if(_media->media_type == MEDIA_CONTENT_TYPE_AUDIO)\r
368                 {\r
369                         if(favorite)\r
370                         {\r
371                                 ret = audio_svc_add_item_to_playlist( db_handle, AUDIO_SVC_FAVORITE_LIST_ID, _media->item_id);\r
372                         }\r
373                         else\r
374                         {\r
375                                 ret = audio_svc_remove_item_from_playlist_by_audio_id(db_handle, AUDIO_SVC_FAVORITE_LIST_ID, _media->item_id);\r
376                         }\r
377                 }\r
378                 else\r
379                 {       \r
380                         ret = minfo_update_media_favorite(db_handle, _media->item_id,favorite);\r
381                 }\r
382                 ret = _content_error_capi(MEDIA_CONTENT_TYPE,ret);\r
383         }\r
384         else\r
385         {\r
386                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
387                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
388 \r
389         }\r
390         return ret;\r
391 \r
392 }\r
393 \r
394 \r
395 \r
396 int media_info_foreach_media_from_db(media_info_filter_h filter,media_info_cb callback, void* user_data)\r
397 {\r
398         int ret= MEDIA_CONTENT_ERROR_NONE;\r
399 \r
400         char limit_query[MIN_QUERY_SIZE];\r
401         char condition_query[DEFAULT_QUERY_SIZE];       \r
402         char search_query[MAX_KEYWORD_SIZE];\r
403         char condition_type[MIN_QUERY_SIZE];\r
404         char select_query[DEFAULT_QUERY_SIZE];  \r
405         char order_query[MIN_QUERY_SIZE];\r
406 \r
407 \r
408         sqlite3_stmt *stmt = NULL;\r
409 \r
410 \r
411         media_info_filter_s* _filter = NULL;\r
412                 \r
413         if( callback == NULL )\r
414         {\r
415                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
416                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
417         }\r
418 \r
419 \r
420 \r
421         if(filter != NULL)\r
422         {\r
423                 _filter = (media_info_filter_s*)filter;\r
424                 \r
425                 if(!(_filter->media_type & (MEDIA_CONTENT_TYPE_IMAGE | MEDIA_CONTENT_TYPE_VIDEO | MEDIA_CONTENT_TYPE_AUDIO)) )\r
426                 {\r
427                         LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
428                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
429                 }\r
430         }       \r
431         else\r
432         {\r
433                 ret = media_info_filter_create((media_info_filter_h*)&_filter);\r
434 \r
435                 if(ret != MEDIA_CONTENT_ERROR_NONE)\r
436                 {\r
437                         return ret;\r
438                 }\r
439                                 \r
440                 if(_filter->media_type == 0)\r
441                 {\r
442                         _filter->media_type = (MEDIA_CONTENT_TYPE_IMAGE | MEDIA_CONTENT_TYPE_VIDEO | MEDIA_CONTENT_TYPE_AUDIO); \r
443                 }\r
444         }\r
445 \r
446         memset(order_query,0x00,sizeof(order_query));   \r
447         memset(condition_type,0x00,sizeof(condition_type));             \r
448         memset(condition_query,0x00,sizeof(condition_query));   \r
449         memset(limit_query,0x00,sizeof(limit_query));   \r
450         memset(search_query,0x00,sizeof(search_query));         \r
451         memset(select_query,0x00,sizeof(select_query));         \r
452 \r
453 \r
454         if((_filter->keyword != NULL) && strlen(_filter->keyword) > 0)\r
455         {\r
456                 if(strlen(_filter->keyword) < sizeof(search_query))\r
457                 {\r
458                         snprintf(search_query,sizeof(search_query)," and display_name like '%%%s%%'", _filter->keyword);\r
459                 }\r
460                 else\r
461                 {\r
462                         LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
463                         if((media_info_filter_s*)filter != _filter)\r
464                                 media_info_filter_destroy((media_info_filter_h)_filter);                \r
465                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
466                 }\r
467         }\r
468         else\r
469         {\r
470                 search_query[0] = ' ';\r
471         }\r
472 \r
473         snprintf(limit_query,sizeof(limit_query),"%s %d,%d",QUERY_KEYWORD_LIMIT,_filter->offset,_filter->count);\r
474         \r
475 \r
476         if(_filter->media_type & MEDIA_CONTENT_TYPE_IMAGE)\r
477         {\r
478                 strncat(condition_type,CONDITION_TYPE_IMAGE,strlen(CONDITION_TYPE_IMAGE));\r
479         }\r
480         if(_filter->media_type & MEDIA_CONTENT_TYPE_VIDEO)\r
481         {\r
482                 if(strlen(condition_type) > 0 )\r
483                 {\r
484                         strncat(condition_type,QUERY_KEYWORD_SPACE, strlen(QUERY_KEYWORD_SPACE));\r
485                         strncat(condition_type,QUERY_KEYWORD_OR, strlen(QUERY_KEYWORD_OR));\r
486                         strncat(condition_type,QUERY_KEYWORD_SPACE, strlen(QUERY_KEYWORD_SPACE));                       \r
487                 }\r
488                 strncat(condition_type,CONDITION_TYPE_VIDEO,strlen(CONDITION_TYPE_VIDEO));\r
489 \r
490         }\r
491         if(_filter->media_type & MEDIA_CONTENT_TYPE_AUDIO)\r
492         {\r
493                 if(strlen(condition_type) > 0 )\r
494                 {\r
495                         strncat(condition_type,QUERY_KEYWORD_SPACE, strlen(QUERY_KEYWORD_SPACE));               \r
496                         strncat(condition_type,QUERY_KEYWORD_OR, strlen(QUERY_KEYWORD_OR));\r
497                         strncat(condition_type,QUERY_KEYWORD_SPACE, strlen(QUERY_KEYWORD_SPACE));                       \r
498                 }\r
499         \r
500                 strncat(condition_type,CONDITION_TYPE_AUDIO,strlen(CONDITION_TYPE_AUDIO));\r
501         }\r
502 \r
503 \r
504         snprintf(select_query,sizeof(select_query),"%s", SELECT_MEDIA_ITEM);\r
505 \r
506         snprintf(condition_query,sizeof(condition_query), "%s (%s)", QUERY_KEYWORD_AND, condition_type);\r
507 \r
508         if(_filter->order == MEDIA_CONTENT_SORT_BY_NAME_ASC)\r
509         {\r
510                 snprintf(order_query,sizeof(order_query),"%s %s",QUERY_KEYWORD_ORDER_BY,DB_FILED_DISPLAY_NAME);\r
511         }\r
512         else if(_filter->order == MEDIA_CONTENT_SORT_BY_NAME_DESC)\r
513         {\r
514                 snprintf(order_query,sizeof(order_query),"%s %s %s",QUERY_KEYWORD_ORDER_BY,DB_FILED_DISPLAY_NAME,QUERY_KEYWORD_DESC);\r
515         }\r
516         else if(_filter->order == MEDIA_CONTENT_SORT_BY_DATE_ASC)\r
517         {\r
518                 snprintf(order_query,sizeof(order_query),"%s %s",QUERY_KEYWORD_ORDER_BY,DB_FILED_DATE_MODIFIED);\r
519         }\r
520         else if(_filter->order == MEDIA_CONTENT_SORT_BY_DATE_DESC)\r
521         {\r
522                 snprintf(order_query,sizeof(order_query),"%s %s %s",QUERY_KEYWORD_ORDER_BY,DB_FILED_DATE_MODIFIED,QUERY_KEYWORD_DESC);          \r
523         }\r
524 \r
525         \r
526         if((media_info_filter_s*)filter != _filter)\r
527                 media_info_filter_destroy((media_info_filter_h)_filter);                \r
528 \r
529 \r
530         ret = _content_query_prepare(&stmt,select_query,condition_query, search_query,limit_query,order_query);\r
531         if( ret != MEDIA_CONTENT_ERROR_NONE)\r
532                 return ret;\r
533 \r
534         \r
535         while( sqlite3_step(stmt) == SQLITE_ROW)\r
536         {\r
537                 media_info_s* item1 = (media_info_s*)calloc(1,sizeof(media_info_s));\r
538 \r
539                 if(item1 == NULL)\r
540                 {\r
541                         LOGE("[%s]OUT_OF_MEMORY(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);\r
542                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;\r
543                 }\r
544                 _content_item_get_detail(stmt,(media_info_h)item1);\r
545 \r
546                 if( callback((media_info_h)item1,user_data) == false)\r
547                 {\r
548                         media_info_destroy((media_info_h) item1);\r
549                         break;\r
550                 }\r
551                 media_info_destroy((media_info_h) item1);               \r
552         }\r
553         \r
554         if(stmt != NULL)\r
555         {\r
556                 sqlite3_finalize(stmt);\r
557         }\r
558 \r
559         return ret;\r
560 \r
561 }\r
562 \r
563 \r
564 \r
565 int media_info_get_image_from_db(media_info_h item, image_meta_h* image)\r
566 {\r
567         int p_ret;\r
568         int ret = MEDIA_CONTENT_ERROR_NONE;\r
569         Mitem* p_item = NULL;\r
570         image_meta_s* _image = NULL; \r
571         \r
572 \r
573         \r
574         media_info_s* _item = (media_info_s*)item;\r
575 \r
576         if( _item == NULL )\r
577         {\r
578                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
579                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
580         }\r
581 \r
582         if(_item->media_type != MEDIA_CONTENT_TYPE_IMAGE)\r
583         {\r
584                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
585                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
586         }\r
587 \r
588         p_ret = minfo_get_item_by_id(db_handle, _item->item_id,&p_item);\r
589 \r
590         ret =  _content_error_capi(MEDIA_CONTENT_TYPE,p_ret);\r
591         if( (ret == MEDIA_CONTENT_ERROR_NONE) && ( p_item != NULL))\r
592         {\r
593                 ret = minfo_get_meta_info(db_handle, _item->item_id,&(p_item->meta_info));\r
594                 ret =  _content_error_capi(MEDIA_CONTENT_TYPE,p_ret);   \r
595 \r
596                 if( ret != MEDIA_CONTENT_ERROR_NONE)\r
597                 {\r
598                         minfo_destroy_mtype_item(p_item);\r
599                         return ret;\r
600                 }\r
601                 _image = (image_meta_s*)calloc(1, sizeof(image_meta_s));\r
602                 if(_image == NULL)\r
603                 {\r
604                         LOGE("[%s]OUT_OF_MEMORY(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);\r
605                         minfo_destroy_mtype_item(p_item);\r
606                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;               \r
607 \r
608                 }\r
609 \r
610                 _image->image_id = strdup(p_item->uuid);\r
611                 if(p_item->thumb_url != NULL)\r
612                         _image->thumbnail = strdup(p_item->thumb_url);\r
613                 if(p_item->rate > 0)\r
614                         _image->favorite = true;\r
615                 else\r
616                         _image->favorite = false;\r
617 \r
618                 if(p_item->meta_info != NULL)\r
619                 {\r
620                         _image->longitude = p_item->meta_info->longitude;\r
621                         _image->latitude = p_item->meta_info->latitude;\r
622                         _image->width = p_item->meta_info->width;\r
623                         _image->height = p_item->meta_info->height;\r
624                         _image->date_taken  = p_item->meta_info->datetaken;\r
625                         if(p_item->meta_info->description != NULL)\r
626                                 _image->description = strdup(p_item->meta_info->description);           \r
627                 }\r
628                 if((p_item->meta_info != NULL) && (p_item->meta_info->image_info != NULL))\r
629                 {\r
630                         _image->orientation = p_item->meta_info->image_info->orientation;\r
631                 }\r
632                 \r
633                 *image = (image_meta_h)_image;\r
634                 p_ret = minfo_destroy_mtype_item(p_item);\r
635                 ret = _content_error_capi(MEDIA_CONTENT_TYPE,p_ret);\r
636         }\r
637 \r
638         \r
639         return ret;\r
640 }\r
641 \r
642 \r
643 int media_info_get_video_from_db(media_info_h item, video_meta_h* video)\r
644 {\r
645 \r
646         int p_ret;\r
647         int ret = MEDIA_CONTENT_ERROR_NONE;\r
648         Mitem* p_item = NULL;\r
649         video_meta_s* _video = NULL;\r
650 \r
651 \r
652         media_info_s* _item = (media_info_s*)item;      \r
653         if( _item == NULL )\r
654         {\r
655                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
656                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
657         }\r
658 \r
659         if(_item->media_type != MEDIA_CONTENT_TYPE_VIDEO)\r
660         {\r
661                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
662                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
663         }\r
664 \r
665         p_ret = minfo_get_item_by_id(db_handle, _item->item_id,&p_item);\r
666 \r
667 \r
668         ret =  _content_error_capi(MEDIA_CONTENT_TYPE,p_ret);\r
669         if( (ret == MEDIA_CONTENT_ERROR_NONE) && ( p_item != NULL))\r
670         {\r
671                 ret = minfo_get_meta_info(db_handle, _item->item_id,&(p_item->meta_info));\r
672                 ret =  _content_error_capi(MEDIA_CONTENT_TYPE,p_ret);   \r
673                 if( ret != MEDIA_CONTENT_ERROR_NONE)\r
674                 {\r
675                         minfo_destroy_mtype_item(p_item);\r
676                         return ret;\r
677                 }\r
678 \r
679                 _video = (video_meta_s*)calloc(1,sizeof(video_meta_s));\r
680                 if( _video == NULL)\r
681                 {\r
682                         LOGE("[%s]OUT_OF_MEMORY(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);\r
683                         minfo_destroy_mtype_item(p_item);\r
684                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;\r
685                 }\r
686 \r
687                 _video->video_id = strdup(p_item->uuid);\r
688                 if(p_item->thumb_url)\r
689                         _video->thumbnail = strdup(p_item->thumb_url);\r
690                 \r
691                 if(p_item->rate > 0)\r
692                         _video->favorite = true;\r
693                 else\r
694                         _video->favorite = false;\r
695 \r
696 \r
697                 if(p_item->meta_info != NULL)\r
698                 {\r
699                         _video->longitude = p_item->meta_info->longitude;\r
700                         _video->latitude = p_item->meta_info->latitude;\r
701                         _video->width = p_item->meta_info->width; \r
702                         _video->height = p_item->meta_info->height;\r
703                         _video->date_taken = p_item->meta_info->datetaken;\r
704                         if(p_item->meta_info->description)\r
705                                 _video->description = strdup(p_item->meta_info->description);                   \r
706                 }                       \r
707                 if((p_item->meta_info != NULL) && (p_item->meta_info->video_info != NULL))\r
708                 {\r
709                         if(p_item->meta_info->video_info->album_name != NULL)\r
710                         {\r
711                                 _video->album = strdup(p_item->meta_info->video_info->album_name);\r
712                         }\r
713                         if(p_item->meta_info->video_info->artist_name != NULL)\r
714                                 _video->artist = strdup(p_item->meta_info->video_info->artist_name);\r
715                         if(p_item->meta_info->video_info->title != NULL)\r
716                                 _video->title = strdup(p_item->meta_info->video_info->title);\r
717 \r
718                         \r
719                         _video->time_played = p_item->meta_info->video_info->last_played_pos;\r
720                         _video->duration = p_item->meta_info->video_info->duration;\r
721                 }\r
722                 *video = (video_meta_h)_video;\r
723                 p_ret = minfo_destroy_mtype_item(p_item);\r
724                 ret =  _content_error_capi(MEDIA_CONTENT_TYPE,p_ret);\r
725         \r
726         }\r
727         return ret;\r
728 }\r
729 \r
730 int media_info_get_audio_from_db(media_info_h item, audio_meta_h* audio)\r
731 {\r
732         int ret = MEDIA_CONTENT_ERROR_NONE;\r
733         \r
734         media_info_s* _item = (media_info_s*)item;\r
735         \r
736         char select_query[DEFAULT_QUERY_SIZE];\r
737         sqlite3_stmt *stmt = NULL;\r
738 \r
739         if(_item == NULL)\r
740         {\r
741                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
742                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
743         }\r
744 \r
745         if(_item->media_type != MEDIA_CONTENT_TYPE_AUDIO)\r
746         {\r
747                 LOGE("[%s] the type of media is not an audio.",__func__);\r
748                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
749         }\r
750 \r
751         \r
752         memset(select_query,0x00,sizeof(select_query)); \r
753 \r
754         snprintf(select_query,sizeof(select_query), SELECT_AUDIO_FROM_MEDIA,_item->item_id);\r
755 \r
756         ret = _content_query_prepare(&stmt,select_query,NULL,NULL,NULL,NULL);\r
757 \r
758 \r
759         if( ret != MEDIA_CONTENT_ERROR_NONE)\r
760                 return ret;\r
761 \r
762         \r
763         audio_meta_s* _audio = (audio_meta_s*)calloc(1,sizeof(audio_meta_s));\r
764         if(_audio == NULL)\r
765         {\r
766                 LOGE("[%s]OUT_OF_MEMORY(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);\r
767                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;\r
768         }\r
769 \r
770 \r
771         while( sqlite3_step(stmt) == SQLITE_ROW)\r
772         {\r
773                 _audio->audio_id = strdup((const char *)sqlite3_column_text(stmt, 0));\r
774                 _audio->genre = strdup((const char *)sqlite3_column_text(stmt, 1));                             \r
775                 _audio->album = strdup((const char *)sqlite3_column_text(stmt, 2));\r
776                 _audio->artist= strdup((const char *)sqlite3_column_text(stmt, 3));\r
777                 _audio->author= strdup((const char *)sqlite3_column_text(stmt, 4));\r
778                 _audio->year= strdup((const char *)sqlite3_column_text(stmt, 5));\r
779                 _audio->copyright= strdup((const char *)sqlite3_column_text(stmt, 6));\r
780                 _audio->description= strdup((const char *)sqlite3_column_text(stmt, 7));\r
781                 _audio->format= strdup((const char *)sqlite3_column_text(stmt, 8));\r
782                 _audio->bitrate = (int)sqlite3_column_int(stmt,9);\r
783                 _audio->track_num = (int)sqlite3_column_int(stmt,10);\r
784                 _audio->duration = (int)sqlite3_column_int(stmt,11);\r
785                 _audio->rating = (int)sqlite3_column_int(stmt,12);\r
786                 _audio->count_played = (int)sqlite3_column_int(stmt,13);\r
787                 _audio->time_played = (int)sqlite3_column_int(stmt,14);\r
788                 _audio->time_added = (int)sqlite3_column_int(stmt,15);\r
789                 _audio->size = (int)sqlite3_column_int(stmt,16);\r
790                 _audio->category = (int)sqlite3_column_int(stmt,17);\r
791         }\r
792         *audio = (audio_meta_h)_audio;\r
793         if(stmt != NULL)\r
794         {\r
795                 sqlite3_finalize(stmt);\r
796         }\r
797         \r
798 \r
799         return ret;\r
800 }\r
801 \r
802 \r
803 \r
804 int media_info_foreach_tag_from_db(media_info_h item, media_tag_cb callback, void *user_data)\r
805 {\r
806         int ret = MEDIA_CONTENT_ERROR_NONE;\r
807         char select_query[DEFAULT_QUERY_SIZE];\r
808         sqlite3_stmt *stmt = NULL;\r
809 \r
810         media_info_s* _item = (media_info_s*)item;      \r
811         \r
812         if(_item == NULL)\r
813         {\r
814                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
815                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
816         }\r
817         if(callback == NULL)\r
818         {\r
819                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
820                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
821         }\r
822 \r
823         snprintf(select_query,sizeof(select_query),SELECT_TAG_LIST_FROM_MEDIA,_item->item_id);\r
824         ret = _content_query_prepare(&stmt,select_query,NULL,NULL,NULL,NULL);\r
825         if(ret != MEDIA_CONTENT_ERROR_NONE)\r
826                 return ret;\r
827 \r
828         while( sqlite3_step(stmt) == SQLITE_ROW)\r
829         {\r
830                 media_tag_s* _tag = (media_tag_s*)calloc(1,sizeof(media_tag_s));\r
831 \r
832                 if(_tag == NULL)\r
833                 {\r
834                         LOGE("[%s]OUT_OF_MEMORY(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);\r
835                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;       \r
836                 }\r
837 \r
838                 _tag->tag_id = (int)sqlite3_column_int(stmt,0);\r
839                 _tag->name = strdup((const char *)sqlite3_column_text(stmt, 1));        \r
840                 if(callback((media_tag_h)_tag,user_data) == false)\r
841                 {\r
842                         media_tag_destroy((media_tag_h) _tag);\r
843                         break;\r
844                 }\r
845                 media_tag_destroy((media_tag_h) _tag);\r
846                 \r
847         }\r
848 \r
849         if(stmt != NULL)\r
850         {\r
851                 sqlite3_finalize(stmt);\r
852         }\r
853 \r
854         return ret;\r
855 \r
856 }\r
857 \r
858 void _content_item_get_detail(sqlite3_stmt* stmt, media_info_h item)\r
859 {\r
860         //item_id, file_path, display_name, thumbnail, date_modified,content_type, valid\r
861         media_info_s* _item = (media_info_s*)item;\r
862         _item->item_id = strdup((const char *)sqlite3_column_text(stmt, 0));\r
863         _item->file_path= strdup((const char *)sqlite3_column_text(stmt, 1));\r
864         _item->display_name=strdup((const char *)sqlite3_column_text(stmt, 2));\r
865         _item->thumbnail=strdup((const char *)sqlite3_column_text(stmt, 3));    \r
866         _item->date_modified = (int)sqlite3_column_int(stmt,4);\r
867         _item->media_type = (int)sqlite3_column_int(stmt,5);\r
868         _item->media_type = _item->media_type==3?4:_item->media_type;\r
869         _item->favorite = (int)sqlite3_column_int(stmt,6);\r
870 }\r
871 \r
872 int _content_get_storage_type(const char* full_path)\r
873 {\r
874         if (full_path != NULL) {                \r
875                 if (strncmp(full_path, MEDIA_CONTENT_PATH_PHONE , strlen(MEDIA_CONTENT_PATH_PHONE )) == 0) \r
876                 {\r
877                         return MEDIA_CONTENT_STORAGE_INTERNAL;\r
878                 }\r
879                 else if (strncmp(full_path, MEDIA_CONTENT_PATH_MMC , strlen(MEDIA_CONTENT_PATH_MMC )) == 0) \r
880                 {\r
881                         return MEDIA_CONTENT_STORAGE_EXTERNAL;\r
882                 }\r
883         }\r
884         \r
885         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
886 }\r
887 \r
888 \r
889 #define CONTENT_TYPE_NUM 4\r
890 #define FEX_CATEGORY_UNKNOWN    0x00000000      /**< Default */\r
891 #define FEX_CATEGORY_ETC                0x00000001      /**< ETC category */\r
892 #define FEX_CATEGORY_IMAGE      0x00000002      /**< Image category */\r
893 #define FEX_CATEGORY_VIDEO              0x00000004      /**< Video category */\r
894 #define FEX_CATEGORY_MUSIC      0x00000008      /**< Music category */\r
895 #define FEX_CATEGORY_SOUND      0x00000010      /**< Sound category */\r
896 #define FEX_CATEGORY_DRM                0x00000020      /**< DRM category */\r
897 #define SOUND_MIME_NUM 1\r
898 #define MINE_LENGTH 30\r
899 #define MUSIC_MIME_NUM 12\r
900 \r
901 \r
902 typedef struct\r
903 {\r
904         char content_type[15];\r
905         int  category_by_mime;\r
906 }fex_content_table_t;\r
907 \r
908 static const fex_content_table_t content_category[CONTENT_TYPE_NUM] = {\r
909         {"audio", FEX_CATEGORY_SOUND},\r
910         {"image", FEX_CATEGORY_IMAGE},\r
911         {"video", FEX_CATEGORY_VIDEO},\r
912         {"application", FEX_CATEGORY_ETC},\r
913 };\r
914 \r
915 static const char sound_mime_table[SOUND_MIME_NUM][MINE_LENGTH] = {\r
916         "x-smaf",\r
917 };\r
918 static const char music_mime_table[MUSIC_MIME_NUM][MINE_LENGTH] = {\r
919         "ac3",\r
920         "mp2",\r
921         "mpeg",\r
922         "ogg",\r
923         "vnd.rn-realaudio",\r
924         "x-ape",\r
925         "x-flac",\r
926         "x-flac+ogg",\r
927         "x-matroska",\r
928         "x-ms-asx",\r
929         "x-ms-wma",\r
930         "x-vorbis+ogg",\r
931 };\r
932 \r
933 #define FMS_DRM_CONTENT_TYPE_LENGTH 100\r
934 \r
935 int _content_get_audio_category(const char* file_full_path,int* category)\r
936 {\r
937         int i = 0;\r
938         char mimetype[255] = {0,};\r
939         int result = 0;\r
940         bool is_drm = false;\r
941         drm_content_info_t contentInfo = {0};\r
942 \r
943         if (file_full_path == NULL)\r
944                 return FEX_CATEGORY_UNKNOWN;\r
945 \r
946 \r
947         if (drm_svc_is_drm_file(file_full_path) == DRM_TRUE)\r
948         {\r
949 \r
950                 // 2008.8.13 han. In this case, use drm_svc_get_content_info api.\r
951                 result = drm_svc_get_content_info(file_full_path, &contentInfo);\r
952                 if (result != DRM_RESULT_SUCCESS)\r
953                 {\r
954                         *category = FEX_CATEGORY_UNKNOWN;\r
955                 }\r
956 \r
957                 strncpy(mimetype,  contentInfo.contentType, strlen(contentInfo.contentType)+1);\r
958                 *category = *category | FEX_CATEGORY_DRM;\r
959                 is_drm = true;\r
960         }\r
961         else\r
962         {\r
963                 result = aul_get_mime_from_file(file_full_path, mimetype, sizeof(mimetype));\r
964                 if(result < 0)\r
965                 {\r
966                         *category = FEX_CATEGORY_UNKNOWN;\r
967                 }\r
968 \r
969         }\r
970 \r
971         for(i = 0; i < CONTENT_TYPE_NUM; i ++)\r
972         {\r
973                 if (strstr(mimetype, content_category[i].content_type) != NULL)\r
974                 {\r
975                         *category = (*category | content_category[i].category_by_mime);\r
976                         break;\r
977                 }\r
978         }\r
979         if (*category & FEX_CATEGORY_ETC)\r
980         {\r
981                 int prefix_len = strlen(content_category[0].content_type);\r
982 \r
983                 for(i = 0; i < SOUND_MIME_NUM; i ++)\r
984                 {\r
985                         if (strstr(mimetype+prefix_len, sound_mime_table[i]) != NULL)\r
986                         {\r
987                                 *category = FEX_CATEGORY_SOUND;\r
988                                 break;\r
989                         }\r
990                 }\r
991 \r
992                 if(is_drm)\r
993                         *category = *category | FEX_CATEGORY_DRM;;\r
994         }\r
995 \r
996         if (!(*category & FEX_CATEGORY_SOUND))\r
997         {\r
998                 return result;\r
999         }\r
1000         else\r
1001         {\r
1002                 if(is_drm) //if sound file is DRM, it is always music file.\r
1003                 {\r
1004                         *category = FEX_CATEGORY_MUSIC;\r
1005                         *category = *category | FEX_CATEGORY_DRM;\r
1006                 }\r
1007                 else //check music file in sound files.\r
1008                 {\r
1009                         int prefix_len = strlen(content_category[0].content_type);\r
1010 \r
1011                         for(i = 0; i < MUSIC_MIME_NUM; i ++)\r
1012                         {\r
1013                                 if (strstr(mimetype+prefix_len, music_mime_table[i]) != NULL)\r
1014                                 {\r
1015                                         *category = FEX_CATEGORY_MUSIC;\r
1016                                         break;\r
1017                                 }\r
1018                         }\r
1019                 }\r
1020         }\r
1021 \r
1022         return result;\r
1023 \r
1024 \r
1025 \r
1026 }\r
1027 \r