Apply tizen coding rule
[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                 ret = _content_error_capi(MEDIA_CONTENT_TYPE, ret);
51                 SAFE_FREE(storage_uuid);
52                 return ret;
53         }
54
55         _storage = (media_storage_s*)calloc(1, sizeof(media_storage_s));
56         media_content_retvm_if(_storage == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
57
58         _storage->storage_id = strdup(storage_uuid);
59         _storage->storage_path = strdup(storage_path);
60         _storage->storage_name = strdup(storage_name);
61         _storage->storage_type = storage_type;
62
63         *storage = (media_storage_h)_storage;
64
65         SAFE_FREE(storage_uuid);
66
67         return ret;
68 }
69
70 int media_storage_delete_from_db(const char *storage_id)
71 {
72         int ret = MEDIA_CONTENT_ERROR_NONE;
73
74         if (!STRING_VALID(storage_id)) {
75                 media_content_error("Invalid Storage ID");
76                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
77         }
78
79         ret = media_svc_delete_storage(_content_get_db_handle(), storage_id, NULL, tzplatform_getuid(TZ_USER_NAME));
80
81         return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
82 }
83
84 int media_storage_get_storage_info_from_db(const char *storage_id, media_storage_h *storage)
85 {
86         int ret = MEDIA_CONTENT_ERROR_NONE;
87         char select_query[DEFAULT_QUERY_SIZE];
88         sqlite3_stmt *stmt = NULL;
89
90         if (!STRING_VALID(storage_id) || (storage == NULL)) {
91                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
92                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
93         }
94
95         memset(select_query, 0x00, sizeof(select_query));
96         snprintf(select_query, sizeof(select_query), SELECT_STORAGE_INFO_FROM_STORAGE, storage_id);
97
98         ret = _content_query_prepare(&stmt, select_query, NULL, NULL);
99         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
100
101         while (sqlite3_step(stmt) == SQLITE_ROW) {
102                 media_storage_s *_storage = (media_storage_s*)calloc(1, sizeof(media_storage_s));
103
104                 if (_storage == NULL) {
105                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
106                         SQLITE3_FINALIZE(stmt);
107                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
108                 }
109
110                 __media_storage_get_detail(stmt, (media_storage_h)_storage);
111
112                 *storage = (media_storage_h)_storage;
113         }
114
115         SQLITE3_FINALIZE(stmt);
116
117         return ret;
118 }
119
120 int media_storage_get_storage_count_from_db(filter_h filter, int *storage_count)
121 {
122         int ret = MEDIA_CONTENT_ERROR_NONE;
123
124         if (storage_count) {
125                 ret = _media_db_get_group_count(filter, MEDIA_GROUP_STORAGE, storage_count);
126         } else {
127                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
128                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
129         }
130
131         return ret;
132 }
133
134 int media_storage_foreach_storage_from_db(filter_h filter, media_storage_cb callback, void *user_data)
135 {
136         int ret = MEDIA_CONTENT_ERROR_NONE;
137
138         if (callback != NULL) {
139                 ret = _media_db_get_storage(filter, callback, user_data);
140         } else {
141                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
142                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
143         }
144
145         return ret;
146 }
147
148 int media_storage_get_media_count_from_db(const char *storage_id, filter_h filter, int *media_count)
149 {
150         int ret = MEDIA_CONTENT_ERROR_NONE;
151
152         if (STRING_VALID(storage_id) && media_count) {
153                 ret = _media_db_get_group_item_count(storage_id, filter, MEDIA_GROUP_STORAGE, media_count);
154         } else {
155                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
156                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
157         }
158
159         return ret;
160 }
161
162 int media_storage_foreach_media_from_db(const char *storage_id, filter_h filter, media_info_cb callback, void *user_data)
163 {
164         int ret = MEDIA_CONTENT_ERROR_NONE;
165
166         if ((callback != NULL) && STRING_VALID(storage_id)) {
167                 ret = _media_db_get_group_item(storage_id, filter, callback, user_data, MEDIA_GROUP_STORAGE);
168         } else {
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_destroy(media_storage_h storage)
177 {
178         int ret = MEDIA_CONTENT_ERROR_NONE;
179         media_storage_s *_storage = (media_storage_s*)storage;
180         if (_storage) {
181                 SAFE_FREE(_storage->storage_id);
182                 SAFE_FREE(_storage->storage_path);
183                 SAFE_FREE(_storage->storage_name);
184                 SAFE_FREE(_storage->storage_account);
185                 SAFE_FREE(_storage);
186
187                 ret = MEDIA_CONTENT_ERROR_NONE;
188         } else {
189                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
190                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
191         }
192
193         return ret;
194 }
195
196 int media_storage_clone(media_storage_h *dst, media_storage_h src)
197 {
198         int ret = MEDIA_CONTENT_ERROR_NONE;
199         media_storage_s *_src = (media_storage_s*)src;
200
201         if (_src != NULL) {
202                 media_storage_s *_dst = (media_storage_s*)calloc(1, sizeof(media_storage_s));
203                 media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
204
205                 if (STRING_VALID(_src->storage_id)) {
206                         _dst->storage_id = strdup(_src->storage_id);
207                         if (_dst->storage_id == NULL) {
208                                 media_storage_destroy((media_storage_h)_dst);
209                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
210                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
211                         }
212                 }
213
214                 if (STRING_VALID(_src->storage_name)) {
215                         _dst->storage_name = strdup(_src->storage_name);
216                         if (_dst->storage_name == NULL) {
217                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
218                                 media_storage_destroy((media_storage_h)_dst);
219                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
220                         }
221                 }
222
223                 if (STRING_VALID(_src->storage_path)) {
224                         _dst->storage_path = strdup(_src->storage_path);
225                         if (_dst->storage_path == NULL) {
226                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
227                                 media_storage_destroy((media_storage_h)_dst);
228                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
229                         }
230                 }
231
232                 if (STRING_VALID(_src->storage_account)) {
233                         _dst->storage_account = strdup(_src->storage_account);
234                         if (_dst->storage_account == NULL) {
235                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
236                                 media_storage_destroy((media_storage_h)_dst);
237                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
238                         }
239                 }
240
241                 _dst->storage_type = _src->storage_type;
242
243                 *dst = (media_storage_h)_dst;
244
245                 ret = MEDIA_CONTENT_ERROR_NONE;
246         } else {
247                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
248                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
249         }
250
251         return ret;
252 }
253
254 int media_storage_get_id(media_storage_h storage, char **storage_id)
255 {
256         int ret = MEDIA_CONTENT_ERROR_NONE;
257         media_storage_s *_storage = (media_storage_s*)storage;
258
259         if (_storage && storage_id) {
260                 if (STRING_VALID(_storage->storage_id)) {
261                         *storage_id = strdup(_storage->storage_id);
262                         media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
263                 } else {
264                         *storage_id = NULL;
265                 }
266
267                 ret = MEDIA_CONTENT_ERROR_NONE;
268         } else {
269                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
270                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
271         }
272
273         return ret;
274 }
275
276 int media_storage_get_name(media_storage_h storage, char **storage_name)
277 {
278         int ret = MEDIA_CONTENT_ERROR_NONE;
279         media_storage_s *_storage = (media_storage_s*)storage;
280
281         if (_storage && storage_name) {
282                 if (STRING_VALID(_storage->storage_name)) {
283                         *storage_name = strdup(_storage->storage_name);
284                         media_content_retvm_if(*storage_name == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
285                 } else {
286                         *storage_name = NULL;
287                 }
288
289                 ret = MEDIA_CONTENT_ERROR_NONE;
290         } else {
291                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
292                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
293         }
294
295         return ret;
296 }
297
298 int media_storage_get_path(media_storage_h storage, char **storage_path)
299 {
300         int ret = MEDIA_CONTENT_ERROR_NONE;
301         media_storage_s *_storage = (media_storage_s*)storage;
302
303         if (_storage && storage_path) {
304                 if (STRING_VALID(_storage->storage_path)) {
305                         *storage_path = strdup(_storage->storage_path);
306                         media_content_retvm_if(*storage_path == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
307                 } else {
308                         *storage_path = NULL;
309                 }
310
311                 ret = MEDIA_CONTENT_ERROR_NONE;
312         } else {
313                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
314                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
315         }
316
317         return ret;
318 }
319
320 int media_storage_get_storage_account(media_storage_h storage, char **storage_account)
321 {
322         int ret = MEDIA_CONTENT_ERROR_NONE;
323         media_storage_s *_storage = (media_storage_s*)storage;
324
325         if (_storage && storage_account) {
326                 if (STRING_VALID(_storage->storage_account)) {
327                         *storage_account = strdup(_storage->storage_account);
328                         media_content_retvm_if(*storage_account == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
329                 } else {
330                         *storage_account = NULL;
331                 }
332
333                 ret = MEDIA_CONTENT_ERROR_NONE;
334         } else {
335                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
336                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
337         }
338
339         return ret;
340 }
341
342 int media_storage_get_type(media_storage_h storage, media_content_storage_e *storage_type)
343 {
344         int ret = MEDIA_CONTENT_ERROR_NONE;
345         media_storage_s *_storage = (media_storage_s*)storage;
346
347         if (_storage && storage_type) {
348                 *storage_type = _storage->storage_type;
349                 ret = MEDIA_CONTENT_ERROR_NONE;
350         } else {
351                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
352                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
353         }
354
355         return ret;
356 }
357
358 int media_storage_get_scan_status(const char *storage_uuid, media_storage_scan_status_e *scan_status)
359 {
360         int ret = MEDIA_CONTENT_ERROR_NONE;
361         media_svc_scan_status_type_e status = MEDIA_STORAGE_SCAN_NONE;
362
363         if (STRING_VALID(storage_uuid)) {
364                 ret = media_svc_get_storage_scan_status(_content_get_db_handle(), storage_uuid, &status);
365                 if (ret != MS_MEDIA_ERR_NONE) {
366                         media_content_error("media_svc_get_storage_scan_status failed");
367                         ret = _content_error_capi(MEDIA_CONTENT_TYPE, ret);
368                 } else {
369                         *scan_status = status;
370                 }
371         } else {
372                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
373                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
374         }
375
376         return ret;
377 }