Closes app now when you hit the back button. This is a temp fix to keep media workin...
[profile/ivi/ug-camera-efl.git] / src / cam_file.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
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
7  *
8  *        http://floralicense.org/license/
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include "cam_file.h"
20 #include "cam_error.h"
21 #include "cam_debug.h"
22 #include "cam_ta.h"
23
24 #include <sys/time.h>
25 #include <sys/times.h>
26 #include <sys/stat.h>
27 #include <dirent.h>
28 #include <media_content.h>
29
30 #define CAM_FILE_CONDITION_LENGTH 256
31
32 gboolean cam_file_init(GError **error)
33 {
34         debug_fenter(LOG_UI);
35
36         return TRUE;
37 }
38
39 void cam_file_finalize()
40 {
41         debug_fenter(LOG_UI);
42         return;
43 }
44
45 gboolean cam_file_get_size(const char *filename, guint64 *size)
46 {
47         struct stat buf;
48
49         if (stat(filename, &buf) != 0)
50                 return FALSE;
51         *size = (guint64)buf.st_size;
52         return TRUE;
53 }
54
55 gboolean cam_file_check_exists(gchar *filename)
56 {
57
58         FILE *fd = fopen(filename, "r");
59         if (fd == NULL) {
60                 return FALSE;
61         } else {
62                 fclose(fd);
63                 return TRUE;
64         }
65 }
66
67 gboolean cam_file_register(const gchar *filename, GError **error)
68 {
69         int err_code = 0;
70
71         CAM_TA_ACUM_ITEM_BEGIN("      cam_file_register", 0);
72
73         cam_debug(LOG_UI, "");
74
75         if (filename == NULL) {
76                 cam_critical(LOG_FILE, "The filename is null");
77                 CAM_TA_ACUM_ITEM_END("      cam_file_register", 0);
78                 return FALSE;
79         }
80
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);
84                 return FALSE;
85         }
86         if (g_str_has_suffix(filename, ".jpg")) {
87                 cam_debug(LOG_FILE, "jpg file!");
88
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!");
93         } else {
94                 cam_critical(LOG_FILE, "failed to get file type : [%s]",
95                              filename);
96                 return FALSE;
97         }
98
99         media_info_h info = NULL;
100         err_code = media_info_insert_to_db(filename, &info);
101
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);
106                 return FALSE;
107         }
108         media_info_destroy(info);
109
110         cam_debug(LOG_FILE, "register success!");
111         CAM_TA_ACUM_ITEM_END("      cam_file_register", 0);
112         return TRUE;
113 }
114
115 gboolean cam_file_delete(const gchar *filename, GError **error)
116 {
117         cam_debug(LOG_FILE, "%s", filename);
118
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);
123                 return FALSE;
124         }
125         return (g_unlink(filename) == 0) ? TRUE : FALSE;
126 }
127
128 gboolean
129 cam_file_rename(const gchar *filename, const gchar *new_name, GError **error)
130 {
131         gboolean bret = FALSE;
132
133         cam_debug(LOG_FILE, "[%s] -> [%s]", filename, new_name);
134
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);
139                 return FALSE;
140         }
141
142         bret = (g_rename(filename, new_name) == 0) ? TRUE : FALSE;
143
144         if (!bret) {
145                 cam_critical(LOG_FILE, "operation failed");
146         }
147
148         if (!g_file_test(new_name, G_FILE_TEST_IS_REGULAR)) {
149                 cam_warning(LOG_FILE, "renamed file not exists:%s", new_name);
150                 bret = FALSE;
151         }
152
153         return bret;
154 }
155
156 const gchar *cam_file_get_internal_image_path(void)
157 {
158         gchar * spath = INTERNAL_FILE_PATH;
159
160         return spath;
161 }
162
163 const gchar *cam_file_get_internal_video_path(void)
164 {
165         gchar * spath = INTERNAL_FILE_PATH;
166
167         return spath;
168 }
169
170 const gchar *cam_file_get_external_image_path(void)
171 {
172         gchar * spath = EXTERNAL_FILE_PATH;
173
174         return spath;
175 }
176
177 const gchar *cam_file_get_external_video_path(void)
178 {
179         gchar * spath = EXTERNAL_FILE_PATH;
180
181         return spath;
182 }
183
184 gboolean cam_check_dir()
185 {
186         DIR *internal_dir = NULL;
187         DIR *external_dir = NULL;
188         int ret;
189         internal_dir = opendir(INTERNAL_FILE_PATH);
190
191         if(internal_dir == NULL) {
192                 ret = mkdir(INTERNAL_FILE_PATH,0777);
193                 DEBUG_TRACE("ret = [%d]",ret);
194                 if(ret <0) {
195                         DEBUG_TRACE("Cannot make default path");
196                         /*closedir(internal_dir);*/
197                         return FALSE;
198                 }
199         }
200         if (internal_dir) {
201                 closedir(internal_dir);
202                 internal_dir = NULL;
203         }
204
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);
209                 if(ret <0) {
210                         DEBUG_TRACE("MMC is fault or not inserted");
211                 }
212
213         }
214         if (external_dir) {
215                 closedir(external_dir);
216                 external_dir = NULL;
217         }
218
219         return TRUE;
220
221 }
222 gchar *cam_file_get_next_filename_for_multishot(const gchar *storage_root,
223                                                 const gchar *first_file_name,
224                                                 const gint shot_num,
225                                                 const gchar *suffix)
226 {
227         char *prefix = NULL;
228         char *result = NULL;
229         char *check_full_path = NULL;
230         char *check_full_name = NULL;
231         int cnt = 0;
232         gboolean bFinal = FALSE;
233
234         prefix = g_strdup_printf("%s/%s", storage_root, first_file_name);
235
236         if (shot_num == 0) {
237                 check_full_name = g_strdup_printf("%s", prefix);
238         } else {
239                 check_full_name =
240                     g_strdup_printf("%s_%d", prefix, shot_num);
241         }
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)) {
247                         bFinal = TRUE;
248                         result = g_strdup(check_full_path);
249                 } else {
250                         cnt++;
251                         if (check_full_path)
252                                 g_free(check_full_path);
253                         check_full_path = NULL;
254                         check_full_path =
255                             g_strdup_printf("%s-%d%s", check_full_name, cnt, suffix);
256
257                         bFinal = FALSE;
258                 }
259         }
260
261         if (check_full_path)
262                 g_free(check_full_path);
263         check_full_path = NULL;
264         if (check_full_name)
265                 g_free(check_full_name);
266         check_full_name = NULL;
267
268         if (prefix)
269                 g_free(prefix);
270         prefix = NULL;
271
272         cam_debug(LOG_FILE, "%s", result);
273
274         return result;
275 }
276
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)
279 {
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");
283                 return FALSE;
284         }
285
286         return TRUE;
287 }
288
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)
292 {
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");
296                 return FALSE;
297         }
298         if (*thumbnail_path)
299                 DEBUG_TRACE("thumbnail_path = %s", *thumbnail_path);
300         return TRUE;
301 }
302
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)
305 {
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");
309                 return FALSE;
310         }
311
312         return TRUE;
313 }
314
315 char *cam_file_get_cam_storage_folder_id(const gchar *storage_root)
316 {
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");
323                 return NULL;
324         }
325
326         ret = media_filter_set_condition(filter, condition,     MEDIA_CONTENT_COLLATE_DEFAULT);
327         if (ret != MEDIA_CONTENT_ERROR_NONE) {
328                 goto ERROR_THROW;
329         }
330
331         char *folder_uuid = NULL;
332         ret = media_folder_foreach_folder_from_db(filter,
333                                                 __cam_file_get_storage_folder_id_cb,
334                                                 &folder_uuid);
335         if (ret != MEDIA_CONTENT_ERROR_NONE) {
336                 goto ERROR_THROW;
337         }
338
339         if (folder_uuid) {
340                 if (filter)
341                         media_filter_destroy(filter);
342                 filter = NULL;
343                 return folder_uuid;
344         } else {
345                 goto ERROR_THROW;
346         }
347
348 ERROR_THROW:
349         if (filter)
350                 media_filter_destroy(filter);
351         filter = NULL;
352         return NULL;
353 }
354
355 gboolean cam_file_get_cam_file_thumbnail_path(const gchar *file_path, gchar **thumbnail_path)
356 {
357         if (thumbnail_path == NULL)
358                 return FALSE;
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");
365                 goto ERROR_THROW;
366         }
367
368         ret = media_filter_set_condition(filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
369         if (ret != MEDIA_CONTENT_ERROR_NONE) {
370                 goto ERROR_THROW;
371         }
372
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) {
378                 goto ERROR_THROW;
379         }
380         *thumbnail_path = temp_thumbnail_path;
381         if (filter)
382                 media_filter_destroy(filter);
383         filter = NULL;
384         return TRUE;
385
386 ERROR_THROW:
387         if (filter)
388                 media_filter_destroy(filter);
389         filter = NULL;
390         return FALSE;
391 }
392
393 gchar *cam_file_get_last_file_path(void *data, const gchar *storage_root)
394 {
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);
399
400
401         /*get folder id*/
402
403         char *folder_uuid = cam_file_get_cam_storage_folder_id(storage_root);
404         if (!folder_uuid) {
405                 DEBUG_TRACE("not find %s", storage_root);
406                 return NULL;
407         }
408
409         char condition[CAM_FILE_PATH_MAX + 1] = { '\0', };
410
411         snprintf(condition, CAM_FILE_PATH_MAX, "(%s=0 OR %s=1)", MEDIA_TYPE, MEDIA_TYPE);
412
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");
417                 goto ERROR_THROW;
418         }
419
420         ret = media_filter_set_condition(filter, condition,     MEDIA_CONTENT_COLLATE_DEFAULT);
421         if (ret != MEDIA_CONTENT_ERROR_NONE) {
422                 goto ERROR_THROW;
423         }
424
425         ret = media_filter_set_order(filter,
426                                      MEDIA_CONTENT_ORDER_DESC,
427                                      MEDIA_MODIFIED_TIME,
428                                      MEDIA_CONTENT_COLLATE_DEFAULT);
429         if (ret != MEDIA_CONTENT_ERROR_NONE) {
430                 goto ERROR_THROW;
431         }
432         ret = media_filter_set_offset(filter,
433                                         0,/*offset*/
434                                         1);/*count*/
435         char *file_url = NULL;
436         ret = media_folder_foreach_media_from_db(folder_uuid,
437                                                 filter,
438                                                 __cam_file_get_latest_file_path_cb,
439                                                 &file_url);
440         if (ret < 0) {
441                 DEBUG_TRACE("Failed to media_folder_foreach_media_from_db");
442                 goto ERROR_THROW;
443         }
444
445         if (file_url == NULL) {
446                 DEBUG_TRACE(" Failed ");
447                 goto ERROR_THROW;
448         }
449         /*make last filename */
450         gchar last_file_path[255] = { 0, };
451         snprintf(last_file_path, sizeof(last_file_path), "%s", file_url);
452         free(file_url);
453         file_url = NULL;
454         media_filter_destroy(filter);
455         filter = NULL;
456
457         return g_strdup(last_file_path);
458
459 ERROR_THROW:
460         if (filter)
461                 media_filter_destroy(filter);
462         filter = NULL;
463         return NULL;
464
465 }
466
467 gchar *cam_file_get_next_filename(const gchar *storage_root,
468                                   const gchar *prefix_string,
469                                   const gchar *suffix)
470 {
471         gchar target[255] = { 0, };
472         time_t t;
473         struct tm tm;
474
475         t = time(NULL);
476         tzset();
477                 /*localtime_r : available since libc 5.2.5 */
478         if(localtime_r(&t,&tm) == NULL) {
479                 return NULL;
480         }
481         snprintf(target, sizeof(target), "%s/%04i%02i%02i-%02i%02i%02i%s",
482                  storage_root,
483                  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
484                  tm.tm_hour, tm.tm_min, tm.tm_sec, suffix);
485
486         cam_debug(LOG_FILE, "filename: %s", target);
487
488         return g_strdup(target);
489 }