*/
#include <string.h>
+#include <grp.h>
+#include <pwd.h>
#include "media-svc-media.h"
#include "media-svc-media-folder.h"
#include "media-svc-error.h"
#include "media-svc-debug.h"
#include "media-svc-util.h"
#include "media-svc-db-utils.h"
+#include "media-svc-noti.h"
+
+#define GLOBAL_USER 0 //#define tzplatform_getenv(TZ_GLOBAL) //TODO
typedef struct{
char thumbnail_path[MEDIA_SVC_PATHNAME_SIZE];
int idx = 0;
sqlite3_stmt *sql_stmt = NULL;
- char *sql = sqlite3_mprintf("select thumbnail_path from %s WHERE validity=0 AND storage_type=%d AND thumbnail_path IS NOT NULL",
+ char *sql = sqlite3_mprintf("SELECT thumbnail_path from (select thumbnail_path, validity from %s WHERE storage_type=%d AND thumbnail_path IS NOT NULL GROUP BY thumbnail_path HAVING count() = 1) WHERE validity=0",
MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
media_svc_debug("[SQL query] : %s", sql);
int idx = 0;
sqlite3_stmt *sql_stmt = NULL;
- char *sql = sqlite3_mprintf("select thumbnail_path from %s WHERE validity=0 AND path LIKE '%q/%%' AND thumbnail_path IS NOT NULL",
+ char *sql = sqlite3_mprintf("SELECT thumbnail_path from (select thumbnail_path, validity from %s WHERE path LIKE '%q/%%' AND thumbnail_path IS NOT NULL GROUP BY thumbnail_path HAVING count() = 1) WHERE validity=0",
MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
media_svc_debug("[SQL query] : %s", sql);
return MEDIA_INFO_ERROR_NONE;
}
-int _media_svc_insert_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info, bool stack_query)
+char* _media_svc_get_thumb_default_path(uid_t uid)
+{
+ char *result_psswd = NULL;
+ struct group *grpinfo = NULL;
+ char * dir = NULL;
+ if(uid == GLOBAL_USER)
+ {
+ result_psswd = strdup(MEDIA_SVC_THUMB_DEFAULT_PATH);
+ grpinfo = getgrnam("root");
+ if(grpinfo == NULL) {
+ media_svc_error("getgrnam(users) returns NULL !");
+ return NULL;
+ }
+ }
+ else
+ {
+ struct passwd *userinfo = getpwuid(uid);
+ if(userinfo == NULL) {
+ media_svc_error("getpwuid(%d) returns NULL !", uid);
+ return NULL;
+ }
+ grpinfo = getgrnam("users");
+ if(grpinfo == NULL) {
+ media_svc_error("getgrnam(users) returns NULL !");
+ return NULL;
+ }
+ // Compare git_t type and not group name
+ if (grpinfo->gr_gid != userinfo->pw_gid) {
+ media_svc_error("UID [%d] does not belong to 'users' group!", uid);
+ return NULL;
+ }
+ asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/thumb_default.png", userinfo->pw_dir);
+ }
+
+ return result_psswd;
+}
+
+int _media_svc_insert_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info, int is_burst, bool stack_query, uid_t uid)
{
media_svc_debug("");
int err = -1;
+ char *burst_id = NULL;
char * db_fields = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_uuid, \
thumbnail_path, title, album_id, album, artist, genre, composer, year, recorded_date, copyright, track_num, description,\
bitrate, samplerate, channel, duration, longitude, latitude, altitude, width, height, datetaken, orientation,\
- rating, is_drm, storage_type";
+ rating, is_drm, storage_type, burst_id";
/* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
/* This code will be removed when sqlite3_mprintf works clearly */
char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
sqlite3_free(test_sql);
+ if (is_burst) {
+ int burst_id_int = 0;
+ err = _media_svc_get_burst_id(handle, &burst_id_int);
+ if (err < 0) {
+ burst_id = NULL;
+ }
+
+ if (burst_id_int > 0) {
+ media_svc_debug("Burst id : %d", burst_id_int);
+ burst_id = sqlite3_mprintf("%d", burst_id_int);
+ }
+
+ /* Get thumbnail for burst shot */
+ char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
+ int width = 0;
+ int height = 0;
+
+ err = thumbnail_request_from_db_with_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height, uid);
+ if (err < 0) {
+ media_svc_error("thumbnail_request_from_db failed: %d", err);
+ } else {
+ media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
+ err = __media_svc_malloc_and_strncpy(&(content_info->thumbnail_path), thumb_path);
+ if (err < 0) {
+ content_info->thumbnail_path = NULL;
+ }
+ }
+
+ if (content_info->media_meta.width <= 0)
+ content_info->media_meta.width = width;
+
+ if (content_info->media_meta.height <= 0)
+ content_info->media_meta.height = height;
+ }
+
char *sql = sqlite3_mprintf("INSERT INTO %s (%s) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, \
%Q, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, \
%d, %d, %d, %d, %.2f, %.2f, %.2f, %d, %d, %Q, %d, \
- %d, %d, %d);",
+ %d, %d, %d, %Q);",
MEDIA_SVC_DB_TABLE_MEDIA, db_fields,
content_info->media_uuid,
content_info->path,
content_info->media_meta.orientation,
content_info->media_meta.rating,
content_info->is_drm,
- content_info->storage_type);
+ content_info->storage_type,
+ burst_id);
+
+ if (burst_id) sqlite3_free(burst_id);
+ burst_id = NULL;
if(!stack_query) {
- err = _media_svc_sql_query(handle, sql);
+ err = _media_svc_sql_query(handle, sql, uid);
sqlite3_free(sql);
if (err != SQLITE_OK) {
media_svc_error("failed to insert item");
return MEDIA_INFO_ERROR_NONE;
}
-int _media_svc_update_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info)
+int _media_svc_update_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
{
int err = -1;
content_info->path
);
- err = _media_svc_sql_query(handle, sql);
+ err = _media_svc_sql_query(handle, sql, uid);
sqlite3_free(sql);
if (err != SQLITE_OK) {
media_svc_error("failed to update item");
return MEDIA_INFO_ERROR_NONE;
}
-int _media_svc_delete_item_by_path(sqlite3 *handle, const char *path)
+int _media_svc_delete_item_by_path(sqlite3 *handle, const char *path, uid_t uid)
{
int err = -1;
char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity=1 AND path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
- err = _media_svc_sql_query(handle, sql);
+ err = _media_svc_sql_query(handle, sql, uid);
sqlite3_free(sql);
if (err != SQLITE_OK) {
media_svc_error("It failed to delete item (%d)", err);
return MEDIA_INFO_ERROR_NONE;
}
-int _media_svc_truncate_table(sqlite3 *handle, media_svc_storage_type_e storage_type)
+int _media_svc_truncate_table(sqlite3 *handle, media_svc_storage_type_e storage_type, uid_t uid)
{
int err = -1;
char *sql = sqlite3_mprintf("DELETE FROM %s WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
- err = _media_svc_sql_query(handle, sql);
+ err = _media_svc_sql_query(handle, sql, uid);
sqlite3_free(sql);
if (err != SQLITE_OK) {
media_svc_error("It failed to truncate table (%d)", err);
}
-int _media_svc_delete_invalid_items(sqlite3 *handle, media_svc_storage_type_e storage_type)
+int _media_svc_delete_invalid_items(sqlite3 *handle, media_svc_storage_type_e storage_type, uid_t uid)
{
int idx = 0;
media_svc_thumbnailpath_s *thumbpath_record = NULL;
}
char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
- err = _media_svc_sql_query(handle, sql);
+ err = _media_svc_sql_query(handle, sql, uid);
sqlite3_free(sql);
if (err != SQLITE_OK) {
media_svc_error("To delete invalid items is failed(%d)", err);
/*Delete thumbnails*/
for (idx = 0; idx < invalid_count; idx++) {
- if (strlen(thumbpath_record[idx].thumbnail_path) > 0) {
+ if ((strlen(thumbpath_record[idx].thumbnail_path) > 0) && (strncmp(thumbpath_record[idx].thumbnail_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) != 0)) {
if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
media_svc_error("fail to remove thumbnail file.");
//SAFE_FREE(thumbpath_record);
return MEDIA_INFO_ERROR_NONE;
}
-int _media_svc_delete_invalid_folder_items(sqlite3 *handle, const char *folder_path)
+int _media_svc_delete_invalid_folder_items(sqlite3 *handle, const char *folder_path, uid_t uid)
{
int idx = 0;
media_svc_thumbnailpath_s *thumbpath_record = NULL;
}
char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
- err = _media_svc_sql_query(handle, sql);
+ err = _media_svc_sql_query(handle, sql, uid);
sqlite3_free(sql);
if (err != SQLITE_OK) {
media_svc_error("To delete invalid items is failed(%d)", err);
/*Delete thumbnails*/
for (idx = 0; idx < invalid_count; idx++) {
- if (strlen(thumbpath_record[idx].thumbnail_path) > 0) {
+ if ((strlen(thumbpath_record[idx].thumbnail_path) > 0) && (strncmp(thumbpath_record[idx].thumbnail_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) != 0)) {
if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
media_svc_error("fail to remove thumbnail file [%s].", thumbpath_record[idx].thumbnail_path);
//SAFE_FREE(thumbpath_record);
}
-int _media_svc_update_item_validity(sqlite3 *handle, const char *path, int validity, bool stack_query)
+int _media_svc_update_item_validity(sqlite3 *handle, const char *path, int validity, bool stack_query, uid_t uid)
{
int err = -1;
char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path= '%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, path);
if(!stack_query) {
- err = _media_svc_sql_query(handle, sql);
+ err = _media_svc_sql_query(handle, sql, uid);
sqlite3_free(sql);
if (err != SQLITE_OK) {
media_svc_error("To update item as valid is failed(%d)", err);
return MEDIA_INFO_ERROR_NONE;
}
-int _media_svc_update_thumbnail_path(sqlite3 *handle, const char *path, const char *thumb_path)
+int _media_svc_update_thumbnail_path(sqlite3 *handle, const char *path, const char *thumb_path, uid_t uid)
{
int err = -1;
char *sql = sqlite3_mprintf("UPDATE %s SET thumbnail_path=%Q WHERE path= %Q", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, path);
- err = _media_svc_sql_query(handle, sql);
+ err = _media_svc_sql_query(handle, sql, uid);
sqlite3_free(sql);
if (err != SQLITE_OK) {
media_svc_error("To update thumb path failed(%d)", err);
return MEDIA_INFO_ERROR_NONE;
}
-int _media_svc_update_storage_item_validity(sqlite3 *handle, media_svc_storage_type_e storage_type, int validity)
+int _media_svc_update_storage_item_validity(sqlite3 *handle, media_svc_storage_type_e storage_type, int validity, uid_t uid)
{
int err = -1;
char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, validity, storage_type);
- err = _media_svc_sql_query(handle, sql);
+ err = _media_svc_sql_query(handle, sql, uid);
sqlite3_free(sql);
if (err != SQLITE_OK) {
media_svc_error("To update item as valid is failed(%d)", err);
return MEDIA_INFO_ERROR_NONE;
}
-int _media_svc_update_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity)
+int _media_svc_update_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity, uid_t uid)
{
int err = -1;
int ret = MEDIA_INFO_ERROR_NONE;
/*Update folder item validity*/
sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE folder_uuid='%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_uuid);
- err = _media_svc_sql_query(handle, sql);
+ err = _media_svc_sql_query(handle, sql, uid);
sqlite3_free(sql);
if (err != SQLITE_OK) {
media_svc_error("To update folder item as valid is failed(%d)", err);
return MEDIA_INFO_ERROR_NONE;
}
-int _media_svc_update_recursive_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity)
+int _media_svc_update_recursive_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity, uid_t uid)
{
int err = -1;
/*Update folder item validity*/
char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_path);
- err = _media_svc_sql_query(handle, sql);
+ err = _media_svc_sql_query(handle, sql, uid);
sqlite3_free(sql);
if (err != SQLITE_OK) {
media_svc_error("To update recursive folder item validity is failed(%d)", err);
}
int _media_svc_update_item_by_path(sqlite3 *handle, const char *src_path, media_svc_storage_type_e dest_storage, const char *dest_path,
- const char *file_name, int modified_time, const char *folder_uuid, const char *thumb_path, bool stack_query)
+ const char *file_name, int modified_time, const char *folder_uuid, const char *thumb_path, bool stack_query, uid_t uid)
{
/* update path, filename, modified_time, folder_uuid, thumbnail_path, */
/* played_count, last_played_time, last_played_position, favourite, storaget_type*/
}
if(!stack_query) {
- err = _media_svc_sql_query(handle, sql);
+ err = _media_svc_sql_query(handle, sql, uid);
sqlite3_free(sql);
if (err != SQLITE_OK) {
media_svc_error("It failed to update metadata (%d)", err);
return MEDIA_INFO_ERROR_NONE;
}
-int _media_svc_list_query_do(sqlite3 *handle, media_svc_query_type_e query_type)
+int _media_svc_list_query_do(sqlite3 *handle, media_svc_query_type_e query_type, uid_t uid)
{
int ret = MEDIA_INFO_ERROR_NONE;
- ret = _media_svc_sql_begin_trans(handle);
+ ret = _media_svc_sql_begin_trans(handle, uid);
media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
if (query_type == MEDIA_SVC_QUERY_SET_ITEM_VALIDITY)
- ret = _media_svc_sql_query_list(handle, &g_media_svc_item_validity_query_list);
+ ret = _media_svc_sql_query_list(handle, &g_media_svc_item_validity_query_list,uid);
else if (query_type == MEDIA_SVC_QUERY_MOVE_ITEM)
- ret = _media_svc_sql_query_list(handle, &g_media_svc_move_item_query_list);
+ ret = _media_svc_sql_query_list(handle, &g_media_svc_move_item_query_list, uid);
else if (query_type == MEDIA_SVC_QUERY_INSERT_ITEM)
- ret = _media_svc_sql_query_list(handle, &g_media_svc_insert_item_query_list);
+ ret = _media_svc_sql_query_list(handle, &g_media_svc_insert_item_query_list, uid);
else
ret = MEDIA_INFO_ERROR_INVALID_PARAMETER;
if (ret != MEDIA_INFO_ERROR_NONE) {
media_svc_error("_media_svc_list_query_do failed. start rollback");
- _media_svc_sql_rollback_trans(handle);
+ _media_svc_sql_rollback_trans(handle,uid);
return ret;
}
- ret = _media_svc_sql_end_trans(handle);
+ ret = _media_svc_sql_end_trans(handle, uid);
if (ret != MEDIA_INFO_ERROR_NONE) {
media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback\n");
- _media_svc_sql_rollback_trans(handle);
+ _media_svc_sql_rollback_trans(handle,uid);
return ret;
}
SQLITE3_FINALIZE(sql_stmt);
return MEDIA_INFO_ERROR_NONE;
+}
+
+int _media_svc_get_burst_id(sqlite3 *handle, int *id)
+{
+ int ret = MEDIA_INFO_ERROR_NONE;
+ int cur_id = -1;
+ sqlite3_stmt *sql_stmt = NULL;
+ char *sql = sqlite3_mprintf("SELECT max(CAST(burst_id AS INTEGER)) FROM %s", MEDIA_SVC_DB_TABLE_MEDIA);
+
+ ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
+
+ if (ret != MEDIA_INFO_ERROR_NONE) {
+ media_svc_error("error when _media_svc_get_burst_id. err = [%d]", ret);
+ return ret;
+ }
+
+ cur_id = sqlite3_column_int(sql_stmt, 0);
+ *id = ++cur_id;
+ SQLITE3_FINALIZE(sql_stmt);
+
+ return MEDIA_INFO_ERROR_NONE;
+}
+
+int _media_svc_get_noti_info(sqlite3 *handle, const char *path, int update_item, media_svc_noti_item **item)
+{
+ int ret = MEDIA_INFO_ERROR_NONE;
+ sqlite3_stmt *sql_stmt = NULL;
+ char *sql = NULL;
+ int is_root_dir = FALSE;
+
+ if (item == NULL) {
+ media_svc_error("_media_svc_get_noti_info failed");
+ return MEDIA_INFO_ERROR_INVALID_PARAMETER;
+ }
+
+ if (update_item == MS_MEDIA_ITEM_FILE) {
+ sql = sqlite3_mprintf("SELECT media_uuid, media_type, mime_type FROM %s WHERE path=%Q", MEDIA_SVC_DB_TABLE_MEDIA, path);
+ } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
+ sql = sqlite3_mprintf("SELECT folder_uuid FROM %s WHERE path=%Q", MEDIA_SVC_DB_TABLE_FOLDER, path);
+ } else {
+ media_svc_error("_media_svc_get_noti_info failed : update item");
+ return MEDIA_INFO_ERROR_INVALID_PARAMETER;
+ }
+
+ ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
+
+ if (ret != MEDIA_INFO_ERROR_NONE) {
+ if (ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD && update_item == MS_MEDIA_ITEM_DIRECTORY) {
+ media_svc_error("This is root directory of media");
+ sql_stmt = NULL;
+ is_root_dir = TRUE;
+ } else {
+ media_svc_error("error when _media_svc_get_noti_info. err = [%d]", ret);
+ return ret;
+ }
+ }
+
+ *item = calloc(1, sizeof(media_svc_noti_item));
+ if (*item == NULL) {
+ media_svc_error("_media_svc_get_noti_info failed : calloc");
+ return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
+ }
+
+ if (update_item == MS_MEDIA_ITEM_FILE) {
+ if (sqlite3_column_text(sql_stmt, 0))
+ (*item)->media_uuid = strdup((const char *)sqlite3_column_text(sql_stmt, 0));
+
+ (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
+
+ if (sqlite3_column_text(sql_stmt, 2))
+ (*item)->mime_type = strdup((const char *)sqlite3_column_text(sql_stmt, 2));
+ } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
+ if (is_root_dir) {
+ (*item)->media_uuid = NULL;
+ } else {
+ if (sqlite3_column_text(sql_stmt, 0))
+ (*item)->media_uuid = strdup((const char *)sqlite3_column_text(sql_stmt, 0));
+ }
+ }
+
+ SQLITE3_FINALIZE(sql_stmt);
+
+ return MEDIA_INFO_ERROR_NONE;
+}
+int _media_svc_count_invalid_folder_items(sqlite3 *handle, const char *folder_path, int *count)
+{
+ int ret = MEDIA_INFO_ERROR_NONE;
+ sqlite3_stmt *sql_stmt = NULL;
+ char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND path LIKE '%q/%%'",
+ MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
+
+ ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
+
+ if (ret != MEDIA_INFO_ERROR_NONE) {
+ media_svc_error("error when __media_svc_count_invalid_folder_records_with_thumbnail. err = [%d]", ret);
+ return ret;
+ }
+
+ *count = sqlite3_column_int(sql_stmt, 0);
+
+ SQLITE3_FINALIZE(sql_stmt);
+
+ return MEDIA_INFO_ERROR_NONE;
+}
+
+int _media_svc_get_thumbnail_count(sqlite3 *handle, const char *thumb_path, int *count)
+{
+ int ret = MEDIA_INFO_ERROR_NONE;
+ sqlite3_stmt *sql_stmt = NULL;
+ char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE thumbnail_path=%Q", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path);
+
+ ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
+
+ if (ret != MEDIA_INFO_ERROR_NONE) {
+ media_svc_error("error when _media_svc_get_thumbnail_count. err = [%d]", ret);
+ return ret;
+ }
+
+ *count = sqlite3_column_int(sql_stmt, 0);
+
+ SQLITE3_FINALIZE(sql_stmt);
+
+ return MEDIA_INFO_ERROR_NONE;
}