2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "cam_error.h"
21 #include "cam_debug.h"
25 #include <sys/times.h>
28 #include <media_content.h>
30 #define CAM_FILE_CONDITION_LENGTH 256
32 gboolean cam_file_init(GError **error)
39 void cam_file_finalize()
45 gboolean cam_file_get_size(const char *filename, guint64 *size)
49 if (stat(filename, &buf) != 0)
51 *size = (guint64)buf.st_size;
55 gboolean cam_file_check_exists(gchar *filename)
58 FILE *fd = fopen(filename, "r");
67 gboolean cam_file_register(const gchar *filename, GError **error)
71 CAM_TA_ACUM_ITEM_BEGIN(" cam_file_register", 0);
73 cam_debug(LOG_UI, "");
75 if (filename == NULL) {
76 cam_critical(LOG_FILE, "The filename is null");
77 CAM_TA_ACUM_ITEM_END(" cam_file_register", 0);
81 if (!g_file_test(filename, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
82 cam_critical(LOG_FILE, "The File is not existed");
83 CAM_TA_ACUM_ITEM_END(" cam_file_register", 0);
86 if (g_str_has_suffix(filename, ".jpg")) {
87 cam_debug(LOG_FILE, "jpg file!");
89 } else if (g_str_has_suffix(filename, ".3gp")) {
90 cam_debug(LOG_FILE, ".3gp file!");
91 } else if (g_str_has_suffix(filename, ".mp4")) {
92 cam_debug(LOG_FILE, ".mp4 file!");
94 cam_critical(LOG_FILE, "failed to get file type : [%s]",
99 media_info_h info = NULL;
100 err_code = media_info_insert_to_db(filename, &info);
102 if (err_code != MEDIA_CONTENT_ERROR_NONE) {
103 cam_critical(LOG_FILE, "failed to media_file_register() : [%s], [%d]", filename, err_code);
104 CAM_TA_ACUM_ITEM_END(" cam_file_register", 0);
105 media_info_destroy(info);
108 media_info_destroy(info);
110 cam_debug(LOG_FILE, "register success!");
111 CAM_TA_ACUM_ITEM_END(" cam_file_register", 0);
115 gboolean cam_file_delete(const gchar *filename, GError **error)
117 cam_debug(LOG_FILE, "%s", filename);
119 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
120 cam_warning(LOG_FILE, "invalid file:%s", filename);
121 cam_set_error(error, CAM_ERROR_FILE_NOT_EXISTS,
122 "File not exists:%s", filename);
125 return (g_unlink(filename) == 0) ? TRUE : FALSE;
129 cam_file_rename(const gchar *filename, const gchar *new_name, GError **error)
131 gboolean bret = FALSE;
133 cam_debug(LOG_FILE, "[%s] -> [%s]", filename, new_name);
135 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
136 cam_warning(LOG_FILE, "invalid file:%s", filename);
137 cam_set_error(error, CAM_ERROR_FILE_NOT_EXISTS,
138 "File not exists:%s", filename);
142 bret = (g_rename(filename, new_name) == 0) ? TRUE : FALSE;
145 cam_critical(LOG_FILE, "operation failed");
148 if (!g_file_test(new_name, G_FILE_TEST_IS_REGULAR)) {
149 cam_warning(LOG_FILE, "renamed file not exists:%s", new_name);
156 const gchar *cam_file_get_internal_image_path(void)
158 gchar * spath = INTERNAL_FILE_PATH;
163 const gchar *cam_file_get_internal_video_path(void)
165 gchar * spath = INTERNAL_FILE_PATH;
170 const gchar *cam_file_get_external_image_path(void)
172 gchar * spath = EXTERNAL_FILE_PATH;
177 const gchar *cam_file_get_external_video_path(void)
179 gchar * spath = EXTERNAL_FILE_PATH;
184 gboolean cam_check_dir()
186 DIR *internal_dir = NULL;
187 DIR *external_dir = NULL;
189 internal_dir = opendir(INTERNAL_FILE_PATH);
191 if(internal_dir == NULL) {
192 ret = mkdir(INTERNAL_FILE_PATH,0777);
193 DEBUG_TRACE("ret = [%d]",ret);
195 DEBUG_TRACE("Cannot make default path");
196 /*closedir(internal_dir);*/
201 closedir(internal_dir);
205 external_dir = opendir(EXTERNAL_FILE_PATH);
206 if(external_dir == NULL) {
207 ret = mkdir(EXTERNAL_FILE_PATH,0777);
208 DEBUG_TRACE("ret = [%d]",ret);
210 DEBUG_TRACE("MMC is fault or not inserted");
215 closedir(external_dir);
222 gchar *cam_file_get_next_filename_for_multishot(const gchar *storage_root,
223 const gchar *first_file_name,
229 char *check_full_path = NULL;
230 char *check_full_name = NULL;
232 gboolean bFinal = FALSE;
234 prefix = g_strdup_printf("%s/%s", storage_root, first_file_name);
237 check_full_name = g_strdup_printf("%s", prefix);
240 g_strdup_printf("%s_%d", prefix, shot_num);
242 check_full_path = g_strdup_printf("%s%s", check_full_name, suffix);
243 /*NOTE: if worst sceanio: always failed.set max count 1000*/
244 #define MAX_SUB_NAME_NUMBER (1000)
245 while (bFinal == FALSE && check_full_path && (cnt < MAX_SUB_NAME_NUMBER)) {
246 if (!cam_file_check_exists(check_full_path)) {
248 result = g_strdup(check_full_path);
252 g_free(check_full_path);
253 check_full_path = NULL;
255 g_strdup_printf("%s-%d%s", check_full_name, cnt, suffix);
262 g_free(check_full_path);
263 check_full_path = NULL;
265 g_free(check_full_name);
266 check_full_name = NULL;
272 cam_debug(LOG_FILE, "%s", result);
277 /*callback for:media_folder_foreach_media_from_db*/
278 static bool __cam_file_get_latest_file_path_cb(media_info_h item, void *user_data)
280 char **file_url = (char **) user_data;
281 if (media_info_get_file_path(item, file_url) != MEDIA_CONTENT_ERROR_NONE) {
282 DEBUG_TRACE("Clone folder handle error");
289 /*callback for:media_info_foreach_media_from_db*/
290 /*note:here have to use bool, not gboolean, for callback define*/
291 static bool __cam_file_get_cam_file_thumbnail_path_cb(media_info_h item, void *user_data)
293 char **thumbnail_path = (char **) user_data;
294 if (media_info_get_thumbnail_path(item, thumbnail_path) != MEDIA_CONTENT_ERROR_NONE) {
295 DEBUG_TRACE("get thumbnail path error");
299 DEBUG_TRACE("thumbnail_path = %s", *thumbnail_path);
303 /*callback for:media_folder_foreach_folder_from_db*/
304 static bool __cam_file_get_storage_folder_id_cb(media_folder_h item, void *user_data)
306 char **folder_uuid = (char **) user_data;
307 if (media_folder_get_folder_id(item, folder_uuid) != MEDIA_CONTENT_ERROR_NONE) {
308 DEBUG_TRACE("Get folder id error");
315 char *cam_file_get_cam_storage_folder_id(const gchar *storage_root)
317 char condition[CAM_FILE_CONDITION_LENGTH + 1] = { '\0', };
318 snprintf(condition, CAM_FILE_CONDITION_LENGTH, "%s = \'%s\'", FOLDER_PATH, (char *)storage_root);
319 filter_h filter = NULL;
320 int ret = media_filter_create(&filter);
321 if(ret != MEDIA_CONTENT_ERROR_NONE) {
322 DEBUG_TRACE("Fail to create filter");
326 ret = media_filter_set_condition(filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
327 if (ret != MEDIA_CONTENT_ERROR_NONE) {
331 char *folder_uuid = NULL;
332 ret = media_folder_foreach_folder_from_db(filter,
333 __cam_file_get_storage_folder_id_cb,
335 if (ret != MEDIA_CONTENT_ERROR_NONE) {
341 media_filter_destroy(filter);
350 media_filter_destroy(filter);
355 gboolean cam_file_get_cam_file_thumbnail_path(const gchar *file_path, gchar **thumbnail_path)
357 if (thumbnail_path == NULL)
359 char condition[CAM_FILE_CONDITION_LENGTH + 1] = { '\0', };
360 snprintf(condition, CAM_FILE_CONDITION_LENGTH, "%s = \'%s\'", MEDIA_PATH, (char *)file_path);
361 filter_h filter = NULL;
362 int ret = media_filter_create(&filter);
363 if(ret != MEDIA_CONTENT_ERROR_NONE) {
364 DEBUG_TRACE("Fail to create filter");
368 ret = media_filter_set_condition(filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
369 if (ret != MEDIA_CONTENT_ERROR_NONE) {
373 char *temp_thumbnail_path = NULL;
374 ret = media_info_foreach_media_from_db(filter,
375 __cam_file_get_cam_file_thumbnail_path_cb,
376 &temp_thumbnail_path);
377 if (ret != MEDIA_CONTENT_ERROR_NONE) {
380 *thumbnail_path = temp_thumbnail_path;
382 media_filter_destroy(filter);
388 media_filter_destroy(filter);
393 gchar *cam_file_get_last_file_path(void *data, const gchar *storage_root)
395 struct appdata *ad =data;
396 cam_retvm_if(ad == NULL, NULL, "appdata is NULL");
397 cam_retvm_if(storage_root == NULL, NULL, "storage_root is NULL");
398 DEBUG_TRACE("storage_root : %s", storage_root);
403 char *folder_uuid = cam_file_get_cam_storage_folder_id(storage_root);
405 DEBUG_TRACE("not find %s", storage_root);
409 char condition[CAM_FILE_PATH_MAX + 1] = { '\0', };
411 snprintf(condition, CAM_FILE_PATH_MAX, "(%s=0 OR %s=1)", MEDIA_TYPE, MEDIA_TYPE);
413 filter_h filter = NULL;
414 int ret = media_filter_create(&filter);
415 if(ret != MEDIA_CONTENT_ERROR_NONE) {
416 DEBUG_TRACE("Fail to create filter");
420 ret = media_filter_set_condition(filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
421 if (ret != MEDIA_CONTENT_ERROR_NONE) {
425 ret = media_filter_set_order(filter,
426 MEDIA_CONTENT_ORDER_DESC,
428 MEDIA_CONTENT_COLLATE_DEFAULT);
429 if (ret != MEDIA_CONTENT_ERROR_NONE) {
432 ret = media_filter_set_offset(filter,
435 char *file_url = NULL;
436 ret = media_folder_foreach_media_from_db(folder_uuid,
438 __cam_file_get_latest_file_path_cb,
441 DEBUG_TRACE("Failed to media_folder_foreach_media_from_db");
445 if (file_url == NULL) {
446 DEBUG_TRACE(" Failed ");
449 /*make last filename */
450 gchar last_file_path[255] = { 0, };
451 snprintf(last_file_path, sizeof(last_file_path), "%s", file_url);
454 media_filter_destroy(filter);
457 return g_strdup(last_file_path);
461 media_filter_destroy(filter);
467 gchar *cam_file_get_next_filename(const gchar *storage_root,
468 const gchar *prefix_string,
471 gchar target[255] = { 0, };
477 /*localtime_r : available since libc 5.2.5 */
478 if(localtime_r(&t,&tm) == NULL) {
481 snprintf(target, sizeof(target), "%s/%04i%02i%02i-%02i%02i%02i%s",
483 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
484 tm.tm_hour, tm.tm_min, tm.tm_sec, suffix);
486 cam_debug(LOG_FILE, "filename: %s", target);
488 return g_strdup(target);