ae0128f4fd1eac6c88e8a17504b94386af79de1c
[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 extern __thread GList *g_media_svc_move_item_query_list;
31
32 int _media_svc_get_folder_id_by_foldername(sqlite3 *handle, const char *folder_name, char *folder_id)
33 {
34         int ret = MS_MEDIA_ERR_NONE;
35         sqlite3_stmt *sql_stmt = NULL;
36
37         char *sql = sqlite3_mprintf("SELECT folder_uuid FROM %s WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, folder_name);
38
39         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
40
41         if (ret != MS_MEDIA_ERR_NONE) {
42                 if(ret == MS_MEDIA_ERR_DB_NO_RECORD) {
43                         media_svc_debug("there is no folder.");
44                 }
45                 else {
46                         media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
47                 }
48                 return ret;
49         }
50
51         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
52
53         SQLITE3_FINALIZE(sql_stmt);
54
55         return ret;
56 }
57
58 int _media_svc_append_folder(sqlite3 *handle, media_svc_storage_type_e storage_type,
59                                     const char *folder_id, const char *path_name, const char *folder_name, int modified_date, uid_t uid)
60 {
61         int ret = MS_MEDIA_ERR_NONE;
62
63         /*Update Pinyin If Support Pinyin*/
64         char *folder_name_pinyin = NULL;
65         if(_media_svc_check_pinyin_support())
66                 _media_svc_get_pinyin_str(folder_name, &folder_name_pinyin);
67
68         char *sql = sqlite3_mprintf("INSERT INTO %s (folder_uuid, path, name, storage_type, modified_time, name_pinyin) values (%Q, %Q, %Q, '%d', '%d', %Q); ",
69                                              MEDIA_SVC_DB_TABLE_FOLDER, folder_id, path_name, folder_name, storage_type, modified_date, folder_name_pinyin);
70         ret = _media_svc_sql_query(handle, sql, uid);
71         sqlite3_free(sql);
72         if (ret != SQLITE_OK) {
73                 media_svc_error("failed to insert folder");
74                 return MS_MEDIA_ERR_DB_INTERNAL;
75         }
76         
77         SAFE_FREE(folder_name_pinyin);
78
79         return ret;
80 }
81
82 int _media_svc_update_folder_modified_time_by_folder_uuid(sqlite3 *handle, const char *folder_uuid, const char *folder_path, bool stack_query, uid_t uid)
83 {
84         int ret = MS_MEDIA_ERR_NONE;
85         int modified_time = 0;
86
87         modified_time = _media_svc_get_file_time(folder_path);
88
89         char *sql = sqlite3_mprintf("UPDATE %s SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
90
91         if(!stack_query) {
92                 ret = _media_svc_sql_query(handle, sql, uid);
93                 sqlite3_free(sql);
94                 if (ret != SQLITE_OK) {
95                         media_svc_error("failed to update folder");
96                         return MS_MEDIA_ERR_DB_INTERNAL;
97                 }
98         } else {
99                 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
100         }
101
102         return ret;
103 }
104
105 int _media_svc_get_and_append_folder_id_by_path(sqlite3 *handle, const char *path, media_svc_storage_type_e storage_type, char *folder_id, uid_t uid)
106 {
107         char *path_name = NULL;
108         int ret = MS_MEDIA_ERR_NONE;
109
110         path_name = g_path_get_dirname(path);
111
112         ret = _media_svc_get_folder_id_by_foldername(handle, path_name, folder_id);
113
114         if(ret == MS_MEDIA_ERR_DB_NO_RECORD) {
115                 char *folder_name = NULL;
116                 int folder_modified_date = 0;
117                 char *folder_uuid = _media_info_generate_uuid();
118                 if(folder_uuid == NULL ) {
119                         media_svc_error("Invalid UUID");
120                         SAFE_FREE(path_name);
121                         return MS_MEDIA_ERR_INTERNAL;
122                 }
123
124                 folder_name = g_path_get_basename(path_name);
125                 folder_modified_date = _media_svc_get_file_time(path_name);
126
127                 ret = _media_svc_append_folder(handle, storage_type, folder_uuid, path_name, folder_name, folder_modified_date, uid);
128                 SAFE_FREE(folder_name);
129                 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE+1);
130         }
131
132         SAFE_FREE(path_name);
133
134         return ret;
135 }
136
137 int _media_svc_update_folder_table(sqlite3 *handle, uid_t uid)
138 {
139         int ret = MS_MEDIA_ERR_NONE;
140         char *sql = NULL;
141
142         sql = sqlite3_mprintf("DELETE FROM %s WHERE folder_uuid IN (SELECT folder_uuid FROM %s WHERE folder_uuid NOT IN (SELECT folder_uuid FROM %s))",
143              MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_MEDIA);
144
145         ret = _media_svc_sql_query(handle, sql, uid);
146         sqlite3_free(sql);
147         if (ret != SQLITE_OK) {
148                 media_svc_error("failed to delete folder item");
149                 return MS_MEDIA_ERR_DB_INTERNAL;
150         }
151
152         return MS_MEDIA_ERR_NONE;
153 }