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