Change folder_id to row id instead of uuid
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-media-folder.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 <glib/gstdio.h>
23 #include <media-util-err.h>
24 #include "media-svc-media-folder.h"
25 #include "media-svc-debug.h"
26 #include "media-svc-env.h"
27 #include "media-svc-util.h"
28 #include "media-svc-db-utils.h"
29
30 static int __media_svc_get_folder_id(sqlite3 *handle, const char *path, long long int *folder_id)
31 {
32         int ret = MS_MEDIA_ERR_NONE;
33         sqlite3_stmt *sql_stmt = NULL;
34         char *sql = NULL;
35
36         sql = sqlite3_mprintf("SELECT folder_id FROM %q WHERE folder_path=%Q", DB_TABLE_FOLDER, path);
37
38         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
39         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_sql_prepare_to_step failed [%d]", ret);
40
41         *folder_id = sqlite3_column_int64(sql_stmt, 0);
42
43         SQLITE3_FINALIZE(sql_stmt);
44
45         return ret;
46 }
47
48 static int __media_svc_append_folder(bool is_direct, const char *storage_id, ms_user_storage_type_e storage_type, const char *folder_path, uid_t uid)
49 {
50         int ret = MS_MEDIA_ERR_NONE;
51         char *folder_name = NULL;
52         int folder_modified_date = 0;
53
54         folder_name = g_path_get_basename(folder_path);
55         folder_modified_date = _media_svc_get_file_time(folder_path);
56
57         /*Update Pinyin If Support Pinyin*/
58         char *folder_name_pinyin = NULL;
59         if (_media_svc_check_pinyin_support())
60                 _media_svc_get_pinyin_str(folder_name, &folder_name_pinyin);
61         /* Sometime SQLITE3 returns NO_RECORD, so need to consider conflict case.. */
62         char *sql = sqlite3_mprintf("INSERT OR IGNORE INTO %q (folder_path, folder_name, storage_uuid, folder_storage_type, folder_modified_time, folder_name_pinyin) VALUES (%Q, %Q, %Q, '%d', '%d', %Q);",
63                                 DB_TABLE_FOLDER, folder_path, folder_name, storage_id, storage_type, folder_modified_date, folder_name_pinyin);
64
65         if (is_direct)
66                 ret = _media_svc_sql_query_direct(sql, uid);
67         else
68                 ret = _media_svc_sql_query(sql, uid);
69
70         SQLITE3_SAFE_FREE(sql);
71
72         g_free(folder_name);
73         g_free(folder_name_pinyin);
74
75         return ret;
76 }
77
78 int _media_svc_update_folder_modified_time(const char *folder_path, uid_t uid)
79 {
80         int ret = MS_MEDIA_ERR_NONE;
81         int time = 0;
82         char *query = NULL;
83
84         time = _media_svc_get_file_time(folder_path);
85         query = sqlite3_mprintf("UPDATE %q SET folder_modified_time=%d WHERE folder_path=%Q", DB_TABLE_FOLDER, time, folder_path);
86
87         ret = _media_svc_sql_query(query, uid);
88         SQLITE3_SAFE_FREE(query);
89
90         return ret;
91 }
92
93 static int __media_svc_append_parent_folder(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, uid_t uid)
94 {
95         int ret = MS_MEDIA_ERR_NONE;
96         unsigned int next_pos = 0;
97         char *next = NULL;
98         char *dir_path = NULL;
99         const char *token = "/";
100         char *internal_path = NULL;
101
102         if (storage_type == MS_USER_STORAGE_INTERNAL) {
103                 ret = ms_user_get_internal_root_path(uid, &internal_path);
104                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
105
106                 next_pos = strlen(internal_path);
107                 g_free(internal_path);
108         } else {
109                 media_svc_retvm_if(!STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL), MS_MEDIA_ERR_INTERNAL, "Failed to get root path");
110                 next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL) + 1;
111         }
112
113         do {
114                 next = strstr(path + next_pos, token);
115                 if (next) {
116                         next_pos = (next - path);
117                         dir_path = g_strndup(path, next_pos);
118                         next_pos++;
119                 } else {
120                         dir_path = g_strdup(path);
121                 }
122
123                 ret = _media_svc_check_folder_by_path(handle, dir_path);
124                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
125                         ret = __media_svc_append_folder(is_direct, storage_id, storage_type, dir_path, uid);
126                         if (ret != MS_MEDIA_ERR_NONE)
127                                 media_svc_error("__media_svc_append_folder is failed");
128                         else
129                                 media_svc_sec_debug("Append new folder path[%s]", dir_path);
130                 }
131
132                 g_free(dir_path);
133         } while (next);
134
135         return MS_MEDIA_ERR_NONE;
136 }
137
138 int _media_svc_get_and_append_folder_id_by_path(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, long long int *folder_id, uid_t uid)
139 {
140         char *dir_path = NULL;
141         int ret = MS_MEDIA_ERR_NONE;
142
143         dir_path = g_path_get_dirname(path);
144
145         ret = __media_svc_get_folder_id(handle, dir_path, folder_id);
146         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
147                 ret = __media_svc_append_parent_folder(handle, is_direct, storage_id, dir_path, storage_type, uid);
148                 if (ret != MS_MEDIA_ERR_NONE) {
149                         media_svc_error("__media_svc_append_parent_folder failed");
150                         goto FINALIZE;
151                 }
152
153                 ret = __media_svc_get_folder_id(handle, dir_path, folder_id);
154         }
155 FINALIZE:
156         g_free(dir_path);
157
158         return ret;
159 }
160
161 int _media_svc_append_by_folder_path(sqlite3 *handle, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, uid_t uid)
162 {
163         int ret = MS_MEDIA_ERR_NONE;
164
165         ret = _media_svc_check_folder_by_path(handle, path);
166         if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
167                 ret = __media_svc_append_parent_folder(handle, true, storage_id, path, storage_type, uid);
168         else
169                 ret = _media_svc_set_folder_validity(true, path, 1, false, uid);
170
171         return ret;
172 }
173
174 int _media_svc_set_folder_validity(bool is_direct, const char *start_path, int validity, bool is_recursive, uid_t uid)
175 {
176         int ret = MS_MEDIA_ERR_NONE;
177         char *sql = NULL;
178
179         if (is_recursive) {
180                 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE folder_path LIKE '%q/%%' OR folder_path=%Q",
181                         DB_TABLE_FOLDER, validity, start_path, start_path);
182         } else {
183                 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE folder_path=%Q", DB_TABLE_FOLDER, validity, start_path);
184         }
185
186         if (is_direct)
187                 ret = _media_svc_sql_query_direct(sql, uid);
188         else
189                 ret = _media_svc_sql_query(sql, uid);
190
191         SQLITE3_SAFE_FREE(sql);
192
193         return ret;
194 }
195
196 int _media_svc_check_folder_by_path(sqlite3 *handle, const char *path)
197 {
198         int ret = MS_MEDIA_ERR_NONE;
199         sqlite3_stmt *sql_stmt = NULL;
200         char *sql = NULL;
201
202         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
203
204         sql = sqlite3_mprintf("SELECT 1 FROM %q WHERE folder_path=%Q", DB_TABLE_FOLDER, path);
205         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
206         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
207
208         SQLITE3_FINALIZE(sql_stmt);
209
210         return MS_MEDIA_ERR_NONE;
211 }