Fix media db open option
[platform/core/api/media-content.git] / src / media_storage.c
1  /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include <media_content.h>
19 #include <media_info_private.h>
20
21 static void __media_storage_get_detail(sqlite3_stmt* stmt, media_storage_h storage)
22 {
23         media_storage_s *_storage = (media_storage_s*)storage;
24
25         if(STRING_VALID((const char *)sqlite3_column_text(stmt, 0)))
26                 _storage->storage_id = strdup((const char *)sqlite3_column_text(stmt, 0));
27
28         if(STRING_VALID((const char *)sqlite3_column_text(stmt, 1)))
29                 _storage->storage_name = strdup((const char *)sqlite3_column_text(stmt, 1));
30
31         if(STRING_VALID((const char *)sqlite3_column_text(stmt, 2)))
32                 _storage->storage_path = strdup((const char *)sqlite3_column_text(stmt, 2));
33
34         if(STRING_VALID((const char *)sqlite3_column_text(stmt, 3)))
35                 _storage->storage_account = strdup((const char *)sqlite3_column_text(stmt, 3));
36
37         _storage->storage_type = (int)sqlite3_column_int(stmt, 4);
38
39         return;
40 }
41
42 int media_storage_insert_to_db(const char *storage_name, const char *storage_path, const char *storage_account, media_content_storage_e storage_type, media_storage_h *storage)
43 {
44         int ret = MEDIA_CONTENT_ERROR_NONE;
45         char *storage_uuid = NULL;
46         media_storage_s *_storage = NULL;
47
48         if(!STRING_VALID(storage_name))
49         {
50                 media_content_error("Invalid Storage Name");
51                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
52         }
53
54         if(!STRING_VALID(storage_path))
55         {
56                 media_content_error("Invalid path");
57                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
58         }
59
60         storage_uuid = media_info_generate_uuid();
61         if(storage_uuid == NULL)
62         {
63                 media_content_error("Fail to get storage_id");
64                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
65         }
66
67         ret = media_svc_insert_storage(_content_get_db_handle(), storage_uuid, storage_name, storage_path, storage_account, storage_type, tzplatform_getuid(TZ_USER_NAME));
68         if (ret != MS_MEDIA_ERR_NONE)
69         {
70                 ret = _content_error_capi(MEDIA_CONTENT_TYPE, ret);
71                 return ret;
72         }
73
74         _storage = (media_storage_s*)calloc(1, sizeof(media_storage_s));
75         media_content_retvm_if(_storage == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
76
77         _storage->storage_id = strdup(storage_uuid);
78         _storage->storage_path = strdup(storage_path);
79         _storage->storage_name = strdup(storage_name);
80         _storage->storage_type = storage_type;
81
82         *storage = (media_storage_h)_storage;
83
84         return ret;
85 }
86
87 int media_storage_delete_from_db(const char *storage_id)
88 {
89         int ret = MEDIA_CONTENT_ERROR_NONE;
90
91         if(!STRING_VALID(storage_id))
92         {
93                 media_content_error("Invalid Storage ID");
94                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
95         }
96
97         ret = media_svc_delete_storage(_content_get_db_handle(), storage_id, tzplatform_getuid(TZ_USER_NAME));
98
99         return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
100 }
101
102 int media_storage_get_storage_info_from_db(const char *storage_id, media_storage_h *storage)
103 {
104         int ret = MEDIA_CONTENT_ERROR_NONE;
105         char select_query[DEFAULT_QUERY_SIZE];
106         sqlite3_stmt *stmt = NULL;
107
108         if(!STRING_VALID(storage_id) || (storage == NULL))
109         {
110                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
111                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
112         }
113
114         memset(select_query, 0x00, sizeof(select_query));
115         snprintf(select_query, sizeof(select_query), SELECT_STORAGE_INFO_FROM_STORAGE, storage_id);
116
117         ret = _content_query_prepare(&stmt, select_query, NULL, NULL);
118         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
119
120         while(sqlite3_step(stmt) == SQLITE_ROW)
121         {
122                 media_storage_s *_storage = (media_storage_s*)calloc(1, sizeof(media_storage_s));
123
124                 if(_storage == NULL)
125                 {
126                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
127                         SQLITE3_FINALIZE(stmt);
128                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
129                 }
130
131                 __media_storage_get_detail(stmt, (media_storage_h)_storage);
132
133                 *storage = (media_storage_h)_storage;
134         }
135
136         SQLITE3_FINALIZE(stmt);
137
138         return ret;
139 }
140
141 int media_storage_get_storage_count_from_db(filter_h filter, int *storage_count)
142 {
143         int ret = MEDIA_CONTENT_ERROR_NONE;
144
145         if(storage_count)
146         {
147                 ret = _media_db_get_group_count(filter, MEDIA_GROUP_STORAGE, storage_count);
148         }
149         else
150         {
151                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
152                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
153         }
154
155         return ret;
156 }
157
158 int media_storage_foreach_storage_from_db(filter_h filter, media_storage_cb callback, void *user_data)
159 {
160         int ret = MEDIA_CONTENT_ERROR_NONE;
161
162         if(callback != NULL)
163         {
164                 ret = _media_db_get_storage(filter, callback, user_data);
165         }
166         else
167         {
168                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
169                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
170         }
171
172         return ret;
173 }
174
175 int media_storage_get_media_count_from_db(const char *storage_id, filter_h filter, int *media_count)
176 {
177         int ret = MEDIA_CONTENT_ERROR_NONE;
178
179         if(STRING_VALID(storage_id) && media_count)
180         {
181                 ret = _media_db_get_group_item_count(storage_id, filter, MEDIA_GROUP_STORAGE, media_count);
182         }
183         else
184         {
185                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
186                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
187         }
188
189         return ret;
190 }
191
192 int media_storage_foreach_media_from_db(const char *storage_id, filter_h filter, media_info_cb callback, void *user_data)
193 {
194         int ret = MEDIA_CONTENT_ERROR_NONE;
195
196         if((callback != NULL) && STRING_VALID(storage_id))
197         {
198                 ret = _media_db_get_group_item(storage_id, filter, callback, user_data, MEDIA_GROUP_STORAGE);
199         }
200         else
201         {
202                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
203                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
204         }
205
206         return ret;
207 }
208
209 int media_storage_destroy(media_storage_h storage)
210 {
211         int ret = MEDIA_CONTENT_ERROR_NONE;
212         media_storage_s *_storage = (media_storage_s*)storage;
213         if(_storage)
214         {
215                 SAFE_FREE(_storage->storage_id);
216                 SAFE_FREE(_storage->storage_path);
217                 SAFE_FREE(_storage->storage_name);
218                 SAFE_FREE(_storage->storage_account);
219                 SAFE_FREE(_storage);
220
221                 ret = MEDIA_CONTENT_ERROR_NONE;
222         }
223         else
224         {
225                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
226                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
227         }
228
229         return ret;
230 }
231
232 int media_storage_clone(media_storage_h *dst, media_storage_h src)
233 {
234         int ret = MEDIA_CONTENT_ERROR_NONE;
235         media_storage_s *_src = (media_storage_s*)src;
236
237         if(_src != NULL)
238         {
239                 media_storage_s *_dst = (media_storage_s*)calloc(1, sizeof(media_storage_s));
240                 media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
241
242                 if(STRING_VALID(_src->storage_id))
243                 {
244                         _dst->storage_id = strdup(_src->storage_id);
245                         if(_dst->storage_id == NULL)
246                         {
247                                 media_storage_destroy((media_storage_h)_dst);
248                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
249                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
250                         }
251                 }
252
253                 if(STRING_VALID(_src->storage_name))
254                 {
255                         _dst->storage_name = strdup(_src->storage_name);
256                         if(_dst->storage_name == NULL)
257                         {
258                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
259                                 media_storage_destroy((media_storage_h)_dst);
260                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
261                         }
262                 }
263
264                 if(STRING_VALID(_src->storage_path))
265                 {
266                         _dst->storage_path = strdup(_src->storage_path);
267                         if(_dst->storage_path == NULL)
268                         {
269                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
270                                 media_storage_destroy((media_storage_h)_dst);
271                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
272                         }
273                 }
274
275                 if(STRING_VALID(_src->storage_account))
276                 {
277                         _dst->storage_account = strdup(_src->storage_account);
278                         if(_dst->storage_account == NULL)
279                         {
280                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
281                                 media_storage_destroy((media_storage_h)_dst);
282                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
283                         }
284                 }
285
286                 _dst->storage_type = _src->storage_type;
287
288                 *dst = (media_storage_h)_dst;
289
290                 ret = MEDIA_CONTENT_ERROR_NONE;
291         }
292         else
293         {
294                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
295                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
296         }
297
298         return ret;
299 }
300
301 int media_storage_get_id(media_storage_h storage, char **storage_id)
302 {
303         int ret = MEDIA_CONTENT_ERROR_NONE;
304         media_storage_s *_storage = (media_storage_s*)storage;
305
306         if(_storage && storage_id)
307         {
308                 if(STRING_VALID(_storage->storage_id))
309                 {
310                         *storage_id = strdup(_storage->storage_id);
311                         media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
312                 }
313                 else
314                 {
315                         *storage_id = NULL;
316                 }
317
318                 ret = MEDIA_CONTENT_ERROR_NONE;
319         }
320         else
321         {
322                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
323                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
324         }
325
326         return ret;
327 }
328
329 int media_storage_get_name(media_storage_h storage, char **storage_name)
330 {
331         int ret = MEDIA_CONTENT_ERROR_NONE;
332         media_storage_s *_storage = (media_storage_s*)storage;
333
334         if(_storage && storage_name)
335         {
336                 if(STRING_VALID(_storage->storage_name))
337                 {
338                         *storage_name = strdup(_storage->storage_name);
339                         media_content_retvm_if(*storage_name == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
340                 }
341                 else
342                 {
343                         *storage_name = NULL;
344                 }
345
346                 ret = MEDIA_CONTENT_ERROR_NONE;
347         }
348         else
349         {
350                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
351                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
352         }
353
354         return ret;
355 }
356
357 int media_storage_get_path(media_storage_h storage, char **storage_path)
358 {
359         int ret = MEDIA_CONTENT_ERROR_NONE;
360         media_storage_s *_storage = (media_storage_s*)storage;
361
362         if(_storage && storage_path)
363         {
364                 if(STRING_VALID(_storage->storage_path))
365                 {
366                         *storage_path = strdup(_storage->storage_path);
367                         media_content_retvm_if(*storage_path == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
368                 }
369                 else
370                 {
371                         *storage_path = NULL;
372                 }
373
374                 ret = MEDIA_CONTENT_ERROR_NONE;
375         }
376         else
377         {
378                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
379                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
380         }
381
382         return ret;
383 }
384
385 int media_storage_get_storage_account(media_storage_h storage, char **storage_account)
386 {
387         int ret = MEDIA_CONTENT_ERROR_NONE;
388         media_storage_s *_storage = (media_storage_s*)storage;
389
390         if(_storage && storage_account)
391         {
392                 if(STRING_VALID(_storage->storage_account))
393                 {
394                         *storage_account = strdup(_storage->storage_account);
395                         media_content_retvm_if(*storage_account == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
396                 }
397                 else
398                 {
399                         *storage_account = NULL;
400                 }
401
402                 ret = MEDIA_CONTENT_ERROR_NONE;
403         }
404         else
405         {
406                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
407                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
408         }
409
410         return ret;
411 }
412
413 int media_storage_get_type(media_storage_h storage, media_content_storage_e *storage_type)
414 {
415         int ret = MEDIA_CONTENT_ERROR_NONE;
416         media_storage_s *_storage = (media_storage_s*)storage;
417
418         if(_storage && storage_type)
419         {
420                 *storage_type = _storage->storage_type;
421                 ret = MEDIA_CONTENT_ERROR_NONE;
422         }
423         else
424         {
425                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
426                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
427         }
428
429         return ret;
430 }