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