4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <glib/gstdio.h>
23 #include "media-svc-media-folder.h"
24 #include "media-svc-error.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"
30 extern __thread GList *g_media_svc_move_item_query_list;
32 int _media_svc_get_folder_id_by_foldername(sqlite3 *handle, const char *folder_name, char *folder_id)
34 int ret = MEDIA_INFO_ERROR_NONE;
35 sqlite3_stmt *sql_stmt = NULL;
37 char *sql = sqlite3_mprintf("SELECT folder_uuid FROM %s WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, folder_name);
39 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
41 if (ret != MEDIA_INFO_ERROR_NONE) {
42 if(ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
43 media_svc_debug("there is no folder.");
46 media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
51 _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
53 SQLITE3_FINALIZE(sql_stmt);
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)
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);
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 err = _media_svc_sql_query(handle, sql, uid);
72 if (err != SQLITE_OK) {
73 media_svc_error("failed to insert folder");
74 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
77 SAFE_FREE(folder_name_pinyin);
79 return MEDIA_INFO_ERROR_NONE;
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)
85 int modified_time = 0;
87 modified_time = _media_svc_get_file_time(folder_path);
89 char *sql = sqlite3_mprintf("UPDATE %s SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
92 err = _media_svc_sql_query(handle, sql, uid);
94 if (err != SQLITE_OK) {
95 media_svc_error("failed to update folder");
96 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
99 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
102 return MEDIA_INFO_ERROR_NONE;
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)
107 char *path_name = NULL;
108 int ret = MEDIA_INFO_ERROR_NONE;
110 path_name = g_path_get_dirname(path);
112 ret = _media_svc_get_folder_id_by_foldername(handle, path_name, folder_id);
114 if(ret == MEDIA_INFO_ERROR_DATABASE_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 MEDIA_INFO_ERROR_INTERNAL;
124 folder_name = g_path_get_basename(path_name);
125 folder_modified_date = _media_svc_get_file_time(path_name);
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);
132 SAFE_FREE(path_name);
137 int _media_svc_update_folder_table(sqlite3 *handle, uid_t uid)
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);
145 err = _media_svc_sql_query(handle, sql, uid);
147 if (err != SQLITE_OK) {
148 media_svc_error("failed to delete folder item");
149 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
152 return MEDIA_INFO_ERROR_NONE;