summaryrefslogtreecommitdiff
path: root/main/src/view/ivug-detail-info.c
blob: 77061e3cce649e1adcca4946c8b292388d3fb524 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	  http://www.tizenopensource.org/license
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "ivug-detail-info.h"

#include <vconf.h>
#include <vconf-keys.h>

#include <appcore-efl.h>
#include <appcore-common.h>

#include <unicode/udat.h>
#include <unicode/ustring.h>
#include <unicode/uloc.h>
#include <unicode/ucal.h>
#include <unicode/udatpg.h>
#include <unicode/utmscale.h>

#include "ivug-details-view.h"
#include <libexif/exif-data.h>

#include "ivug-file-info.h"


#define NOMAL_BUF   		128

#define DETAILS_DIR_PATH_LEN_MAX	4096

enum IVUG_SIZE_TYPE
{
	SIZE_BYTE = 0,
	SIZE_KB,
	SIZE_MB,
	SIZE_GB
};

/*
	Shuld free returned value after use
*/
static char*
_get_icu_time_string(const char *locale, const char *customSkeleton, const char *timezone, UDate date)
{
#define UG_ICU_ARR_LENGTH 			256

	IV_ASSERT(customSkeleton != NULL);
	IV_ASSERT(timezone != NULL);

	/*
		Copy a byte string encoded in the default codepage to a ustring.
		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
	*/
	UChar ucustomSkeleton[UG_ICU_ARR_LENGTH] = {0,};

	if(u_uastrncpy(ucustomSkeleton, customSkeleton, UG_ICU_ARR_LENGTH) == NULL)
	{
		MSG_UTIL_ERROR("u_uastrncpy() error.");
		return NULL;
	}

	UChar utimezone[UG_ICU_ARR_LENGTH] = {0,};

	if ( u_uastrncpy(utimezone, timezone, UG_ICU_ARR_LENGTH) == NULL )
	{
		MSG_UTIL_ERROR("u_uastrncpy() error.");
		return NULL;
	}

	UErrorCode status = U_ZERO_ERROR;
	UDateTimePatternGenerator *generator;
	UDateFormat	*formatter;

	UChar bestPattern[UG_ICU_ARR_LENGTH] = {0,};
	UChar formatted[UG_ICU_ARR_LENGTH] = {0,};
	char formattedString[UG_ICU_ARR_LENGTH] = {0,};
	int32_t bestPatternLength, formattedLength;

	ucal_setDefaultTimeZone(utimezone , &status);

	if (U_FAILURE(status))
	{
		MSG_UTIL_ERROR("ucal_setDefaultTimeZone() is failed");
		return NULL;
	}

	uloc_setDefault(getenv("LC_TIME"), &status);

	if (U_FAILURE(status))
	{
		MSG_UTIL_ERROR("ucal_setDefaultTimeZone() is failed");
		return NULL;
	}

	generator = udatpg_open(locale, &status);
	if(generator == NULL)
	{
		return NULL;
	}

	bestPatternLength = udatpg_getBestPattern(generator, ucustomSkeleton, u_strlen(ucustomSkeleton), bestPattern, UG_ICU_ARR_LENGTH, &status);
	if(bestPatternLength <= 0)
	{
		return NULL;
	}

	formatter = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern, -1, &status);
	if(formatter == 0)
	{
		return NULL;
	}

	formattedLength = udat_format(formatter, date, formatted, UG_ICU_ARR_LENGTH, NULL, &status);
	if(formattedLength <= 0)
	{
		return NULL;
	}

	u_austrcpy(formattedString, formatted);
	udatpg_close(generator);
	udat_close(formatter);

	if(strlen(formattedString) == 0)
	{
		return NULL;
	}

	return strdup(formattedString);
}


static char*
_get_icu_date(time_t mtime)
{
#define UG_DATE_FORMAT_12			"yMMMdhms"
#define UG_DATE_FORMAT_24			"yMMMdHms"

	char* skeleton = NULL;

	enum appcore_time_format timeformat;

	int ret = appcore_get_timeformat(&timeformat);
	if(ret == -1)
	{
		MSG_DETAIL_ERROR("Cannot get timeformat.");
		timeformat = APPCORE_TIME_FORMAT_12;		// Default value.
// Go through
	}

	if(timeformat == APPCORE_TIME_FORMAT_12)
	{
		skeleton = UG_DATE_FORMAT_12;
	}
	else if(timeformat == APPCORE_TIME_FORMAT_24)
	{
		skeleton = UG_DATE_FORMAT_24;
	}
	else
	{
		MSG_DETAIL_ERROR("Invalid time format : %d", timeformat);
		skeleton = UG_DATE_FORMAT_12;		// Default value.
	}

	char *locale = vconf_get_str(VCONFKEY_REGIONFORMAT);		/* eg. en_US.UTF-8*/
	if(locale == NULL)
	{
		MSG_DETAIL_ERROR("Cannot get region format.");
		locale = "en_US.UTF-8";		// Default value.
	}

	char *timezone = NULL;

	timezone = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);	// eg Asia/Seoul
	if(timezone == NULL)
	{
// TODO : How to get default time zone????
		MSG_DETAIL_ERROR("Cannot get time zone");
		return NULL;
	}

	MSG_DETAIL_HIGH("Locale:%s TimeZone:%s TimeFormat:%s", locale, skeleton, timezone);

	char* datestr = NULL;

	datestr = _get_icu_time_string(locale, skeleton, timezone, (UDate)mtime * 1000);

	if(datestr == NULL)
	{
		MSG_DETAIL_ERROR("Cannot get time string");
		return NULL;
	}

	MSG_DETAIL_HIGH("ICU Date : %s", datestr);
	return datestr;

}




static char *
_get_filesize_string(size_t size)
{
#define FILE_SIZE_LEN_MAX			64
#define BASIC_SIZE 			1024    //used for file size check

	int count = 0;

	unsigned long int lsize = (unsigned long int)size;

	char *tmp = (char *)calloc(1, sizeof(char)*FILE_SIZE_LEN_MAX + 1);
	if ( tmp == NULL)
	{
		return NULL;
	}

	while(size >= ( BASIC_SIZE ) )
	{
		lsize = size;
		size /= BASIC_SIZE;
		count++;
	}

	float remain = ((float)lsize/BASIC_SIZE) - size;

	if(count == SIZE_BYTE)
	{
		snprintf(tmp,FILE_SIZE_LEN_MAX,"%zu B", size);
	}
	else if(count == SIZE_KB)
	{
		snprintf(tmp,FILE_SIZE_LEN_MAX,"%zu KB", size);
	}
	else if(count == SIZE_MB)
	{
		// need another algorithm
		snprintf(tmp,FILE_SIZE_LEN_MAX,"%.1f MB", size+remain);
	}
	else if(count == SIZE_GB)
	{
		snprintf(tmp,FILE_SIZE_LEN_MAX,"%.1f GB", size+remain);
	}

	MSG_DETAIL_HIGH("Size = %lu, OrigSize=%zu", lsize, size);
	return tmp;
}

void
ivug_details_info_set_file_information(ivug_file_info_t * const pFileInfo, const char *path)
{
	ivug_ret_if( NULL == pFileInfo );
	ivug_ret_if( NULL == path );

/*** get file path*/
	pFileInfo->filepath = strdup(path);

	struct stat statbuf;

	if(stat(path,&statbuf) == -1)
	{
		MSG_DETAIL_ERROR("Cannot get stat()");
		return ;
	}

	pFileInfo->size = statbuf.st_size;
	pFileInfo->date = statbuf.st_mtime;

	pFileInfo->create_date = _get_icu_date(pFileInfo->date);

	if ( pFileInfo->create_date == NULL )
	{
		pFileInfo->create_date = strdup(IDS_N_A);
	}

	pFileInfo->file_ext = ivug_fileinfo_get_file_extension(path);

	if ( pFileInfo->file_ext == NULL )
	{
		pFileInfo->file_ext = strdup(IDS_UNKNOWN);
	}

	pFileInfo->filesize = _get_filesize_string(pFileInfo->size);

	MSG_DETAIL_HIGH("Basic Info : ext=%s size=%s", pFileInfo->file_ext, pFileInfo->filesize);

	return ;

}


void
ivug_details_info_set_gps_info(ivug_file_info_t * const pFileInfo, const char *path)
{
#define UG_EXIF_ARR_LENGTH 			255

	ivug_retm_if(!pFileInfo, "data is NULL");
	ivug_retm_if(!path, "path is NULL");

	pFileInfo->longitude = calloc(1, sizeof(char)*UG_EXIF_ARR_LENGTH);
	if(pFileInfo->longitude == NULL)
	{
		return;
	}

	pFileInfo->latitude = calloc(1, sizeof(char)*UG_EXIF_ARR_LENGTH);
	if(pFileInfo->latitude == NULL)
	{
		return;
	}


	double longtitude = 0.0;
	double latitude = 0.0;

	bool bret = false;

	if(pFileInfo->file_type == IVUG_DETAIL_IMAGE_TYPE)		// this will be removed!!!!
	{
		bret = ivug_fileinfo_get_image_gps_info(path, &latitude, &longtitude);
	}
	else if(pFileInfo->file_type == IVUG_DETAIL_VIDEO_TYPE)
	{
		bret = ivug_fileinfo_get_video_gps_info(path, &latitude, &longtitude);
	}

	if ( bret == false)
	{
		latitude = 0;
		longtitude = 0;
	}

	if(latitude != 0.0)
	{
		snprintf(pFileInfo->latitude, UG_EXIF_ARR_LENGTH, "%.5f", latitude);
	}
	else
	{
		snprintf(pFileInfo->latitude, UG_EXIF_ARR_LENGTH, "%s", IDS_UNKNOWN);
	}
	if(longtitude != 0.0)
	{
		snprintf(pFileInfo->longitude, UG_EXIF_ARR_LENGTH, "%.5f", longtitude);
	}
	else
	{
		snprintf(pFileInfo->longitude, UG_EXIF_ARR_LENGTH, "%s", IDS_UNKNOWN);
	}
}

void
ivug_details_info_set_resolution(ivug_file_info_t * const pFileInfo, const char *path, int stype)
{
	ivug_retm_if(!pFileInfo, "data is NULL");
	ivug_retm_if(!path, "path is NULL");

	MSG_DETAIL_HIGH("slide type is %d", stype);

	pFileInfo->resolution = (char *)malloc(sizeof(char)*NOMAL_BUF+1);
	if(pFileInfo->resolution == NULL)
	{
		return ;
	}
	memset(pFileInfo->resolution,0,(sizeof(char)* NOMAL_BUF + 1));


	int width, height;

	switch(stype)
	{
		case SLIDE_TYPE_IMAGE:
			pFileInfo->file_type = IVUG_DETAIL_IMAGE_TYPE;

			if ( ivug_fileinfo_get_image_resolution(path, &width, &height) == false)
			{
				snprintf(pFileInfo->resolution, NOMAL_BUF, "%s", IDS_N_A);
			}
			else
			{
				snprintf(pFileInfo->resolution, NOMAL_BUF,"%dX%d", width, height);
			}

			break;

		/*	Video type */
		case SLIDE_TYPE_VIDEO:
			pFileInfo->file_type = IVUG_DETAIL_VIDEO_TYPE;

			if ( ivug_fileinfo_get_video_resolution(path, &width, &height) == false)
			{
				snprintf(pFileInfo->resolution, NOMAL_BUF, "%s", IDS_N_A);
			}
			else
			{
				snprintf(pFileInfo->resolution, NOMAL_BUF,"%dX%d", width, height);
			}

			break;

		default:
			break;

	}

}

void
ivug_details_info_set_location(ivug_file_info_t * const pFileInfo, const char *path)
{
	ivug_retm_if(!pFileInfo, "data is NULL");
	ivug_retm_if(!path, "path is NULL");

	pFileInfo->file_location = strdup(path);
}

void
ivug_details_info_set_filename(ivug_file_info_t * const pFileInfo, const char *path)
{
	ivug_retm_if(!pFileInfo, "data is NULL");
	ivug_retm_if(!path, "path is NULL");

	/** get file name */
	pFileInfo->filename = ecore_file_strip_ext(ecore_file_file_get(path));
}