apply FSL(Flora Software License)
[apps/home/ug-image-viewer-efl.git] / main / src / view / ivug-detail-info.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://www.tizenopensource.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
18 #include "ivug-detail-info.h"
19
20 #include <vconf.h>
21 #include <vconf-keys.h>
22
23 #include <appcore-efl.h>
24 #include <appcore-common.h>
25
26 #include <unicode/udat.h>
27 #include <unicode/ustring.h>
28 #include <unicode/uloc.h>
29 #include <unicode/ucal.h>
30 #include <unicode/udatpg.h>
31 #include <unicode/utmscale.h>
32
33 #include "ivug-details-view.h"
34 #include <libexif/exif-data.h>
35
36 #include "ivug-file-info.h"
37
38
39 #define NOMAL_BUF               128
40
41 #define DETAILS_DIR_PATH_LEN_MAX        4096
42
43 enum IVUG_SIZE_TYPE
44 {
45         SIZE_BYTE = 0,
46         SIZE_KB,
47         SIZE_MB,
48         SIZE_GB
49 };
50
51 /*
52         Shuld free returned value after use
53 */
54 static char*
55 _get_icu_time_string(const char *locale, const char *customSkeleton, const char *timezone, UDate date)
56 {
57 #define UG_ICU_ARR_LENGTH                       256
58
59         IV_ASSERT(customSkeleton != NULL);
60         IV_ASSERT(timezone != NULL);
61
62         /*
63                 Copy a byte string encoded in the default codepage to a ustring.
64                 Copies at most n characters. The result will be null terminated if the length of src is less than n. Performs a host byte to UChar conversion
65         */
66         UChar ucustomSkeleton[UG_ICU_ARR_LENGTH] = {0,};
67
68         if(u_uastrncpy(ucustomSkeleton, customSkeleton, UG_ICU_ARR_LENGTH) == NULL)
69         {
70                 MSG_UTIL_ERROR("u_uastrncpy() error.");
71                 return NULL;
72         }
73
74         UChar utimezone[UG_ICU_ARR_LENGTH] = {0,};
75
76         if ( u_uastrncpy(utimezone, timezone, UG_ICU_ARR_LENGTH) == NULL )
77         {
78                 MSG_UTIL_ERROR("u_uastrncpy() error.");
79                 return NULL;
80         }
81
82         UErrorCode status = U_ZERO_ERROR;
83         UDateTimePatternGenerator *generator;
84         UDateFormat     *formatter;
85
86         UChar bestPattern[UG_ICU_ARR_LENGTH] = {0,};
87         UChar formatted[UG_ICU_ARR_LENGTH] = {0,};
88         char formattedString[UG_ICU_ARR_LENGTH] = {0,};
89         int32_t bestPatternLength, formattedLength;
90
91         ucal_setDefaultTimeZone(utimezone , &status);
92
93         if (U_FAILURE(status))
94         {
95                 MSG_UTIL_ERROR("ucal_setDefaultTimeZone() is failed");
96                 return NULL;
97         }
98
99         uloc_setDefault(getenv("LC_TIME"), &status);
100
101         if (U_FAILURE(status))
102         {
103                 MSG_UTIL_ERROR("ucal_setDefaultTimeZone() is failed");
104                 return NULL;
105         }
106
107         generator = udatpg_open(locale, &status);
108         if(generator == NULL)
109         {
110                 return NULL;
111         }
112
113         bestPatternLength = udatpg_getBestPattern(generator, ucustomSkeleton, u_strlen(ucustomSkeleton), bestPattern, UG_ICU_ARR_LENGTH, &status);
114         if(bestPatternLength <= 0)
115         {
116                 return NULL;
117         }
118
119         formatter = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern, -1, &status);
120         if(formatter == 0)
121         {
122                 return NULL;
123         }
124
125         formattedLength = udat_format(formatter, date, formatted, UG_ICU_ARR_LENGTH, NULL, &status);
126         if(formattedLength <= 0)
127         {
128                 return NULL;
129         }
130
131         u_austrcpy(formattedString, formatted);
132         udatpg_close(generator);
133         udat_close(formatter);
134
135         if(strlen(formattedString) == 0)
136         {
137                 return NULL;
138         }
139
140         return strdup(formattedString);
141 }
142
143
144 static char*
145 _get_icu_date(time_t mtime)
146 {
147 #define UG_DATE_FORMAT_12                       "yMMMdhms"
148 #define UG_DATE_FORMAT_24                       "yMMMdHms"
149
150         char* skeleton = NULL;
151
152         enum appcore_time_format timeformat;
153
154         int ret = appcore_get_timeformat(&timeformat);
155         if(ret == -1)
156         {
157                 MSG_DETAIL_ERROR("Cannot get timeformat.");
158                 timeformat = APPCORE_TIME_FORMAT_12;            // Default value.
159 // Go through
160         }
161
162         if(timeformat == APPCORE_TIME_FORMAT_12)
163         {
164                 skeleton = UG_DATE_FORMAT_12;
165         }
166         else if(timeformat == APPCORE_TIME_FORMAT_24)
167         {
168                 skeleton = UG_DATE_FORMAT_24;
169         }
170         else
171         {
172                 MSG_DETAIL_ERROR("Invalid time format : %d", timeformat);
173                 skeleton = UG_DATE_FORMAT_12;           // Default value.
174         }
175
176         char *locale = vconf_get_str(VCONFKEY_REGIONFORMAT);            /* eg. en_US.UTF-8*/
177         if(locale == NULL)
178         {
179                 MSG_DETAIL_ERROR("Cannot get region format.");
180                 locale = "en_US.UTF-8";         // Default value.
181         }
182
183         char *timezone = NULL;
184
185         timezone = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID); // eg Asia/Seoul
186         if(timezone == NULL)
187         {
188 // TODO : How to get default time zone????
189                 MSG_DETAIL_ERROR("Cannot get time zone");
190                 return NULL;
191         }
192
193         MSG_DETAIL_HIGH("Locale:%s TimeZone:%s TimeFormat:%s", locale, skeleton, timezone);
194
195         char* datestr = NULL;
196
197         datestr = _get_icu_time_string(locale, skeleton, timezone, (UDate)mtime * 1000);
198
199         if(datestr == NULL)
200         {
201                 MSG_DETAIL_ERROR("Cannot get time string");
202                 return NULL;
203         }
204
205         MSG_DETAIL_HIGH("ICU Date : %s", datestr);
206         return datestr;
207
208 }
209
210
211
212
213 static char *
214 _get_filesize_string(size_t size)
215 {
216 #define FILE_SIZE_LEN_MAX                       64
217 #define BASIC_SIZE                      1024    //used for file size check
218
219         int count = 0;
220
221         unsigned long int lsize = (unsigned long int)size;
222
223         char *tmp = (char *)calloc(1, sizeof(char)*FILE_SIZE_LEN_MAX + 1);
224         if ( tmp == NULL)
225         {
226                 return NULL;
227         }
228
229         while(size >= ( BASIC_SIZE ) )
230         {
231                 lsize = size;
232                 size /= BASIC_SIZE;
233                 count++;
234         }
235
236         float remain = ((float)lsize/BASIC_SIZE) - size;
237
238         if(count == SIZE_BYTE)
239         {
240                 snprintf(tmp,FILE_SIZE_LEN_MAX,"%zu B", size);
241         }
242         else if(count == SIZE_KB)
243         {
244                 snprintf(tmp,FILE_SIZE_LEN_MAX,"%zu KB", size);
245         }
246         else if(count == SIZE_MB)
247         {
248                 // need another algorithm
249                 snprintf(tmp,FILE_SIZE_LEN_MAX,"%.1f MB", size+remain);
250         }
251         else if(count == SIZE_GB)
252         {
253                 snprintf(tmp,FILE_SIZE_LEN_MAX,"%.1f GB", size+remain);
254         }
255
256         MSG_DETAIL_HIGH("Size = %lu, OrigSize=%zu", lsize, size);
257         return tmp;
258 }
259
260 void
261 ivug_details_info_set_file_information(ivug_file_info_t * const pFileInfo, const char *path)
262 {
263         ivug_ret_if( NULL == pFileInfo );
264         ivug_ret_if( NULL == path );
265
266 /*** get file path*/
267         pFileInfo->filepath = strdup(path);
268
269         struct stat statbuf;
270
271         if(stat(path,&statbuf) == -1)
272         {
273                 MSG_DETAIL_ERROR("Cannot get stat()");
274                 return ;
275         }
276
277         pFileInfo->size = statbuf.st_size;
278         pFileInfo->date = statbuf.st_mtime;
279
280         pFileInfo->create_date = _get_icu_date(pFileInfo->date);
281
282         if ( pFileInfo->create_date == NULL )
283         {
284                 pFileInfo->create_date = strdup(IDS_N_A);
285         }
286
287         pFileInfo->file_ext = ivug_fileinfo_get_file_extension(path);
288
289         if ( pFileInfo->file_ext == NULL )
290         {
291                 pFileInfo->file_ext = strdup(IDS_UNKNOWN);
292         }
293
294         pFileInfo->filesize = _get_filesize_string(pFileInfo->size);
295
296         MSG_DETAIL_HIGH("Basic Info : ext=%s size=%s", pFileInfo->file_ext, pFileInfo->filesize);
297
298         return ;
299
300 }
301
302
303 void
304 ivug_details_info_set_gps_info(ivug_file_info_t * const pFileInfo, const char *path)
305 {
306 #define UG_EXIF_ARR_LENGTH                      255
307
308         ivug_retm_if(!pFileInfo, "data is NULL");
309         ivug_retm_if(!path, "path is NULL");
310
311         pFileInfo->longitude = calloc(1, sizeof(char)*UG_EXIF_ARR_LENGTH);
312         if(pFileInfo->longitude == NULL)
313         {
314                 return;
315         }
316
317         pFileInfo->latitude = calloc(1, sizeof(char)*UG_EXIF_ARR_LENGTH);
318         if(pFileInfo->latitude == NULL)
319         {
320                 return;
321         }
322
323
324         double longtitude = 0.0;
325         double latitude = 0.0;
326
327         bool bret = false;
328
329         if(pFileInfo->file_type == IVUG_DETAIL_IMAGE_TYPE)              // this will be removed!!!!
330         {
331                 bret = ivug_fileinfo_get_image_gps_info(path, &latitude, &longtitude);
332         }
333         else if(pFileInfo->file_type == IVUG_DETAIL_VIDEO_TYPE)
334         {
335                 bret = ivug_fileinfo_get_video_gps_info(path, &latitude, &longtitude);
336         }
337
338         if ( bret == false)
339         {
340                 latitude = 0;
341                 longtitude = 0;
342         }
343
344         if(latitude != 0.0)
345         {
346                 snprintf(pFileInfo->latitude, UG_EXIF_ARR_LENGTH, "%.5f", latitude);
347         }
348         else
349         {
350                 snprintf(pFileInfo->latitude, UG_EXIF_ARR_LENGTH, "%s", IDS_UNKNOWN);
351         }
352         if(longtitude != 0.0)
353         {
354                 snprintf(pFileInfo->longitude, UG_EXIF_ARR_LENGTH, "%.5f", longtitude);
355         }
356         else
357         {
358                 snprintf(pFileInfo->longitude, UG_EXIF_ARR_LENGTH, "%s", IDS_UNKNOWN);
359         }
360 }
361
362 void
363 ivug_details_info_set_resolution(ivug_file_info_t * const pFileInfo, const char *path, int stype)
364 {
365         ivug_retm_if(!pFileInfo, "data is NULL");
366         ivug_retm_if(!path, "path is NULL");
367
368         MSG_DETAIL_HIGH("slide type is %d", stype);
369
370         pFileInfo->resolution = (char *)malloc(sizeof(char)*NOMAL_BUF+1);
371         if(pFileInfo->resolution == NULL)
372         {
373                 return ;
374         }
375         memset(pFileInfo->resolution,0,(sizeof(char)* NOMAL_BUF + 1));
376
377
378         int width, height;
379
380         switch(stype)
381         {
382                 case SLIDE_TYPE_IMAGE:
383                         pFileInfo->file_type = IVUG_DETAIL_IMAGE_TYPE;
384
385                         if ( ivug_fileinfo_get_image_resolution(path, &width, &height) == false)
386                         {
387                                 snprintf(pFileInfo->resolution, NOMAL_BUF, "%s", IDS_N_A);
388                         }
389                         else
390                         {
391                                 snprintf(pFileInfo->resolution, NOMAL_BUF,"%dX%d", width, height);
392                         }
393
394                         break;
395
396                 /*      Video type */
397                 case SLIDE_TYPE_VIDEO:
398                         pFileInfo->file_type = IVUG_DETAIL_VIDEO_TYPE;
399
400                         if ( ivug_fileinfo_get_video_resolution(path, &width, &height) == false)
401                         {
402                                 snprintf(pFileInfo->resolution, NOMAL_BUF, "%s", IDS_N_A);
403                         }
404                         else
405                         {
406                                 snprintf(pFileInfo->resolution, NOMAL_BUF,"%dX%d", width, height);
407                         }
408
409                         break;
410
411                 default:
412                         break;
413
414         }
415
416 }
417
418 void
419 ivug_details_info_set_location(ivug_file_info_t * const pFileInfo, const char *path)
420 {
421         ivug_retm_if(!pFileInfo, "data is NULL");
422         ivug_retm_if(!path, "path is NULL");
423
424         pFileInfo->file_location = strdup(path);
425 }
426
427 void
428 ivug_details_info_set_filename(ivug_file_info_t * const pFileInfo, const char *path)
429 {
430         ivug_retm_if(!pFileInfo, "data is NULL");
431         ivug_retm_if(!path, "path is NULL");
432
433         /** get file name */
434         pFileInfo->filename = ecore_file_strip_ext(ecore_file_file_get(path));
435 }
436
437