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-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"
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 = MS_MEDIA_ERR_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 != MS_MEDIA_ERR_NONE) {
42 if(ret == MS_MEDIA_ERR_DB_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)
61 int ret = MS_MEDIA_ERR_NONE;
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 ret = _media_svc_sql_query(handle, sql, uid);
73 SAFE_FREE(folder_name_pinyin);
78 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)
80 int ret = MS_MEDIA_ERR_NONE;
81 int modified_time = 0;
83 modified_time = _media_svc_get_file_time(folder_path);
85 char *sql = sqlite3_mprintf("UPDATE %s SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
88 ret = _media_svc_sql_query(handle, sql, uid);
91 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
97 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)
99 char *path_name = NULL;
100 int ret = MS_MEDIA_ERR_NONE;
102 path_name = g_path_get_dirname(path);
104 ret = _media_svc_get_folder_id_by_foldername(handle, path_name, folder_id);
106 if(ret == MS_MEDIA_ERR_DB_NO_RECORD) {
107 char *folder_name = NULL;
108 int folder_modified_date = 0;
109 char *folder_uuid = _media_info_generate_uuid();
110 if(folder_uuid == NULL ) {
111 media_svc_error("Invalid UUID");
112 SAFE_FREE(path_name);
113 return MS_MEDIA_ERR_INTERNAL;
116 folder_name = g_path_get_basename(path_name);
117 folder_modified_date = _media_svc_get_file_time(path_name);
119 ret = _media_svc_append_folder(handle, storage_type, folder_uuid, path_name, folder_name, folder_modified_date, uid);
120 SAFE_FREE(folder_name);
121 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE+1);
124 SAFE_FREE(path_name);
129 int _media_svc_update_folder_table(sqlite3 *handle, uid_t uid)
131 int ret = MS_MEDIA_ERR_NONE;
134 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))",
135 MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_MEDIA);
137 ret = _media_svc_sql_query(handle, sql, uid);