c9c61094c50a0b7be80e13bd4459f7786e712143
[framework/api/media-content.git] / src / media_content.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 \r
19 #include <media_content.h>\r
20 #include <media_info_private.h>\r
21 #include <audio-svc.h>\r
22 #include <audio-svc-error.h>\r
23 #include <audio-svc-types.h>\r
24 #include <visual-svc-types.h>\r
25 #include <visual-svc.h>\r
26 \r
27 \r
28 #include <dlog.h>\r
29 \r
30 #ifdef LOG_TAG\r
31 #undef LOG_TAG\r
32 #endif\r
33 \r
34 #define LOG_TAG "TIZEN_N_MEDIACONTENT"\r
35 \r
36 static MediaSvcHandle* db_handle = NULL;\r
37 static int ref_count = 0;\r
38 \r
39 int media_content_connect()\r
40 {\r
41         int ret = MEDIA_CONTENT_ERROR_NONE;\r
42 \r
43         if(ref_count == 0)\r
44         {\r
45                 if(db_handle == NULL)\r
46         {\r
47                         ret = media_svc_connect(&db_handle);\r
48         }\r
49 \r
50                 ret = _content_error_capi(MEDIA_CONTENT_TYPE,ret);\r
51         }\r
52 \r
53         ref_count++;\r
54         \r
55         return ret;\r
56 }\r
57 \r
58 \r
59 int media_content_disconnect()\r
60 {\r
61         int ret = MEDIA_CONTENT_ERROR_NONE;\r
62         \r
63         if(ref_count > 0)\r
64         {\r
65                 ref_count--;\r
66         }\r
67         else\r
68         {\r
69                 LOGE("[%s]DB_FAILED(0x%08x) database is not connected", __FUNCTION__, MEDIA_CONTENT_ERROR_DB_FAILED);\r
70                 return MEDIA_CONTENT_ERROR_DB_FAILED;\r
71         }\r
72 \r
73         if(ref_count == 0)\r
74         {\r
75         if(db_handle != NULL)\r
76         {\r
77                         ret = media_svc_disconnect(db_handle);\r
78                         ret =  _content_error_capi(MEDIA_CONTENT_TYPE,ret);\r
79                 if( ret == MEDIA_CONTENT_ERROR_NONE)\r
80                 {               \r
81                         db_handle = NULL;\r
82                 }\r
83         }\r
84         \r
85         }\r
86         \r
87         return ret;\r
88 }\r
89 \r
90 MediaSvcHandle* _content_get_db_handle()\r
91 {\r
92         return db_handle;\r
93 }\r
94 \r
95 \r
96 int _content_query_prepare(sqlite3_stmt** stmt,char* select_query,char* condition_query, char* search_query,char* limit_query,char* order)\r
97 {\r
98         char* query = NULL;\r
99         int len_query;\r
100         \r
101         if(db_handle == NULL)\r
102         {\r
103                 LOGE("[%s]DB_FAILED(0x%08x) database is not connected", __FUNCTION__, MEDIA_CONTENT_ERROR_DB_FAILED);\r
104                 return MEDIA_CONTENT_ERROR_DB_FAILED;   \r
105         }\r
106         \r
107         if((select_query != NULL) && (select_query[0] != 0x00))\r
108         {\r
109                 if((search_query == NULL) || search_query[0] == 0x00)\r
110                 {\r
111                         search_query = " ";\r
112                 }\r
113                 if((limit_query == NULL) || limit_query[0] == 0x00)\r
114                 {\r
115                         limit_query = " ";\r
116                 }               \r
117                 if((order == NULL) || order[0] == 0x00)\r
118                 {\r
119                         order = " ";\r
120                 }\r
121                 if((condition_query == NULL) ||condition_query[0] == 0x00)\r
122                 {\r
123                         condition_query = " ";\r
124                 }\r
125 \r
126                 len_query= strlen(select_query) + 1 + strlen(search_query)+ 1 + strlen(condition_query)+ 1 + strlen(limit_query)+ 1 + strlen(order)+1;\r
127 \r
128                 query = (char*)calloc(len_query,sizeof(char));\r
129 \r
130                 if(query == NULL)\r
131                 {\r
132                         LOGE("[%s]OUT_OF_MEMORY(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);\r
133                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;\r
134                 }\r
135                 \r
136                 snprintf(query,len_query,"%s %s %s %s %s",select_query,condition_query,search_query,order,limit_query );\r
137 \r
138                 LOGV("[%s]Query : %s",__func__,query); \r
139                 if( sqlite3_prepare((sqlite3*)db_handle, query, strlen(query),stmt, NULL) != SQLITE_OK )\r
140                 {\r
141                         LOGE("[%s]DB_FAILED(0x%08x) fail to sqlite3_prepare(), %s", __FUNCTION__, MEDIA_CONTENT_ERROR_DB_FAILED,sqlite3_errmsg((sqlite3*)db_handle));\r
142                         exit(1);\r
143                 }       \r
144                 free(query);\r
145         }\r
146         else\r
147         {\r
148                 LOGE("[%s]INVALID_PARAMETER(0x%08x)", __FUNCTION__, MEDIA_CONTENT_ERROR_INVALID_PARAMETER);\r
149                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
150         }\r
151         return MEDIA_CONTENT_ERROR_NONE;\r
152 \r
153 }\r
154 \r
155 \r
156 \r
157 int _content_error_capi(media_content_type_e type, int content_error)\r
158 {\r
159         if(type == MEDIA_CONTENT_TYPE)\r
160         {\r
161                 if(content_error == MB_SVC_ERROR_NONE)\r
162                         return MEDIA_CONTENT_ERROR_NONE;\r
163                 else if(content_error == MB_SVC_ERROR_INVALID_PARAMETER || content_error == AUDIO_SVC_ERROR_INVALID_PARAMETER)\r
164                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;\r
165                 else if(content_error == MB_SVC_ERROR_DB_INTERNAL || content_error == AUDIO_SVC_ERROR_DB_INTERNAL)\r
166                         return MEDIA_CONTENT_ERROR_DB_FAILED;\r
167                 else if(content_error == MEDIA_INFO_ERROR_DATABASE_CONNECT || content_error == AUDIO_SVC_ERROR_DB_CONNECT)\r
168                         return MEDIA_CONTENT_ERROR_DB_FAILED;\r
169                 else if(content_error == MEDIA_INFO_ERROR_DATABASE_DISCONNECT)\r
170                         return MEDIA_CONTENT_ERROR_DB_FAILED;                           \r
171                         \r
172         }\r
173         return MEDIA_CONTENT_ERROR_NONE;\r
174         \r
175 }\r
176 \r
177 \r
178 int _content_query_sql(char *query_str)\r
179 {\r
180         int err = MEDIA_CONTENT_ERROR_NONE;\r
181         char *err_msg;\r
182 \r
183         if(db_handle == NULL)\r
184         {\r
185                 LOGE("[%s]DB_FAILED(0x%08x) database is not connected",__FUNCTION__, MEDIA_CONTENT_ERROR_DB_FAILED);\r
186                 return MEDIA_CONTENT_ERROR_DB_FAILED;   \r
187         }\r
188 \r
189 \r
190         err = sqlite3_exec(db_handle, query_str, NULL, NULL, &err_msg);\r
191         if (SQLITE_OK != err) \r
192         {\r
193                 if (err_msg) \r
194                 {\r
195                         sqlite3_free(err_msg);\r
196                 }\r
197                 LOGE("[%s]DB_FAILED(0x%08x) database operation is failed",__FUNCTION__, MEDIA_CONTENT_ERROR_DB_FAILED);\r
198                 return MEDIA_CONTENT_ERROR_DB_FAILED;\r
199         }\r
200 \r
201         if (err_msg)\r
202                 sqlite3_free(err_msg);\r
203         \r
204         return err;\r
205 }\r
206 \r
207 \r