update code by Tizen coding convention
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-storage.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include "media-util-err.h"
23 #include "media-svc-debug.h"
24 #include "media-svc-env.h"
25 #include "media-svc-db-utils.h"
26 #include "media-svc-util.h"
27 #include "media-svc-storage.h"
28
29 int _media_svc_init_storage(sqlite3 *handle, uid_t uid)
30 {
31         int ret = MS_MEDIA_ERR_NONE;
32         char *sql = NULL;
33         sqlite3_stmt *sql_stmt = NULL;
34         int storage_cnt = 0;
35
36         /*Add Internal storage*/
37         sql = sqlite3_mprintf("SELECT COUNT(*) FROM '%s' WHERE storage_uuid='%s'", MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_TABLE_MEDIA);
38         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
39         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
40
41         storage_cnt = sqlite3_column_int(sql_stmt, 0);
42         SQLITE3_FINALIZE(sql_stmt);
43
44         if (storage_cnt == 0) {
45                 sql = sqlite3_mprintf("INSERT INTO %s (storage_uuid, storage_name, storage_path, storage_type) VALUES ('%s', '%s', '%s', 0);",
46                                       MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_TABLE_MEDIA, _media_svc_get_path(uid));
47
48                 ret = _media_svc_sql_query(handle, sql, uid);
49                 sqlite3_free(sql);
50                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
51         }
52
53         return ret;
54 }
55
56 int _media_svc_append_storage(sqlite3 *handle, const char *storage_id, const char *storage_name, const char *storage_path, const char *storage_account, media_svc_storage_type_e storage_type, uid_t uid)
57 {
58         int ret = MS_MEDIA_ERR_NONE;
59         char *sql = sqlite3_mprintf("INSERT INTO %s (storage_uuid, storage_name, storage_path, storage_account, storage_type) values (%Q, %Q, %Q, %Q, %d); ",
60                                     MEDIA_SVC_DB_TABLE_STORAGE, storage_id, storage_name, storage_path, storage_account, storage_type);
61
62         ret = _media_svc_sql_query(handle, sql, uid);
63         sqlite3_free(sql);
64
65         return ret;
66 }
67
68 int _media_svc_delete_storage(sqlite3 *handle, const char *storage_id, uid_t uid)
69 {
70         int ret = MS_MEDIA_ERR_NONE;
71         char *sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
72
73         ret = _media_svc_sql_query(handle, sql, uid);
74         sqlite3_free(sql);
75
76         return ret;
77 }
78
79 int _media_svc_update_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
80 {
81         int ret = MS_MEDIA_ERR_NONE;
82         char *sql = NULL;
83
84         if (storage_id == NULL) {
85                 sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE storage_uuid != 'media';", MEDIA_SVC_DB_TABLE_STORAGE, validity);
86         } else {
87                 sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, validity, storage_id);
88         }
89
90         ret = _media_svc_sql_query(handle, sql, uid);
91         sqlite3_free(sql);
92
93         return ret;
94 }