summaryrefslogtreecommitdiff
path: root/common/src/email-utils.c
blob: 05a3902482f04e04e7bfa3db86e43dd2f821e367 (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
438
439
440
441
442
443
444
445
446
447
448
/*
 * 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 <sys/time.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <appcore-common.h>
#include <vconf.h>
#include <stdlib.h>
#include <regex.h>
#include <errno.h>
#include <drm_client.h>
#include <drm_client_types.h>

#include "email-utils.h"

char *icu_locale = NULL;
static UDateTimePatternGenerator *icu_pattern_generator = NULL;
static UDateFormat *icu_formatter_hmm = NULL;
static UDateFormat *icu_formatter_Hmm = NULL;
static UDateFormat *icu_formatter_MMMyyyy = NULL;
static UDateFormat *icu_formatter_MMMd = NULL;
static UDateFormat *icu_formatter_EEEMMMd = NULL;
enum appcore_time_format icu_timeformat;


static void _generate_best_pattern(const char *locale, UChar * customSkeleton, char *formattedString, void *time);
static int _open_icu_pattern_n_formatter(const char *locale, char *skeleton, UDateFormat **formatter);
static int _close_icu_pattern_n_formatter(UDateFormat *formatter);

/*
 * Function implementation
 */
char *email_get_current_theme_name(void)
{
	char *theme_name = NULL;
	char *save_ptr = NULL;

	/* Get current theme path. Return value is a full path of theme file. ex) blue-hd:default */
	const char *current_theme_path = elm_theme_get(NULL);
	debug_log("current_theme_path [%s]", current_theme_path);

	if (current_theme_path == NULL) {
		debug_log("current_theme_path is NULL !!");
		return NULL;
	}

	theme_name = strdup(current_theme_path);
	if (theme_name == NULL) {
		debug_log("theme_name is NULL !!");
		return NULL;
	}

	theme_name = strtok_r(theme_name, ":", &save_ptr);
	if (theme_name == NULL) {
		debug_log("theme_name is NULL !!");
		return NULL;
	}

	debug_log("theme_name [%s]", theme_name);

	return theme_name;
}

gboolean email_check_file_exist(const gchar *path)
{
	debug_log("");
	RETURN_VAL_IF_FAIL(STR_VALID(path), FALSE);

	if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
		return FALSE;
	}
	return TRUE;
}

gboolean email_check_dir_exist(const gchar *path)
{
	debug_log("");
	RETURN_VAL_IF_FAIL(STR_VALID(path), FALSE);

	if (!g_file_test(path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
		return FALSE;
	}
	return TRUE;
}

gchar *email_parse_get_filename_from_path(const gchar *path)
{
	debug_log("");
	RETURN_VAL_IF_FAIL(STR_VALID(path), NULL);

	guint index = 0;

	gchar *file_path = g_strdup(path);
	file_path = g_strstrip(file_path);
	gchar **token_list = g_strsplit_set(file_path, "/", -1);
	g_free(file_path);	/* MUST BE. */

	RETURN_VAL_IF_FAIL(token_list != NULL, NULL);

	while (token_list[index] != NULL) {
		index++;
	}

	gchar *file_name = g_strdup(token_list[index - 1]);

	g_strfreev(token_list);	/* MUST BE. */

	/*token_list = g_strsplit_set(file_name, ".", -1);
	RETURN_VAL_IF_FAIL(token_list != NULL, NULL);
	gchar *file_name_without_ext = g_strdup(token_list[0]);
	g_strfreev(token_list);*/

	int len = 0;
	if (file_name)
		len = strlen(file_name);

	gchar *ext = g_strrstr(file_name, ".");
	if (ext)
		len = len - strlen(ext);

	gchar *file_name_without_ext = g_strndup(file_name, len);

	g_free(file_name);	/* MUST BE. */

	debug_log("file name (%s)", file_name_without_ext);

	return file_name_without_ext;
}

void email_parse_get_filename_n_ext_from_path(const gchar *path, gchar **ret_file_name, gchar **ret_ext)
{
	debug_log("");
	RETURN_IF_FAIL(STR_VALID(path));

	guint index = 0;

	gchar *file_path = g_strdup(path);
	file_path = g_strstrip(file_path);
	gchar **token_list = g_strsplit_set(file_path, "/", -1);
	g_free(file_path);	/* MUST BE. */

	RETURN_IF_FAIL(token_list != NULL);

	while (token_list[index] != NULL) {
		index++;
	}

	gchar *file_name = g_strdup(token_list[index - 1]);

	g_strfreev(token_list);	/* MUST BE. */

	/*token_list = g_strsplit_set(file_name, ".", -1);
	RETURN_VAL_IF_FAIL(token_list != NULL, NULL);
	gchar *file_name_without_ext = g_strdup(token_list[0]);
	g_strfreev(token_list);*/

	int len = 0;
	if (file_name)
		len = strlen(file_name);

	gchar *ext = g_strrstr(file_name, ".");
	if (ext)
		len = len - strlen(ext);

	*ret_file_name = g_strndup(file_name, len);

	*ret_ext = g_strdup(ext);

	g_free(file_name);
}

void email_save_file(const gchar *path, const gchar *buf, gsize len)
{
	debug_log("");
	if (STR_LEN((gchar *)buf) > 0 && len > 0) {
		int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
		if (fd != -1) {
			int size = write(fd, (const void *)buf, (size_t) len);
			debug_log("size:%d", size);
			close(fd);
		}
	}
}

gchar *email_get_buff_from_file(const gchar *path, guint max_kbyte)
{
	debug_log("");
	RETURN_VAL_IF_FAIL(STR_VALID(path), NULL);

	debug_log("path (%s)", path);

	FILE *fp = fopen(path, "r");

	if (fp) {

		char body[MAX_STR_LEN + 1] = { 0, };
		size_t size = 0;
		guint kbyte = 0;
		gchar *buff = NULL;
		gchar *temp = NULL;

		while (1) {

			memset(body, 0, sizeof(body));

			size = fread(body, sizeof(unsigned char), MAX_STR_LEN, fp);
			body[MAX_STR_LEN] = '\0';

			if (size <= 0 || STR_LEN(body) == 0)
				break;

			if (max_kbyte > 0 && kbyte > max_kbyte)
				break;

			if (STR_VALID(buff)) {
				temp = g_strconcat(buff, body, NULL);
				g_free(buff);
				buff = temp;
				temp = NULL;
			} else {
				buff = g_strdup(body);
			}

			++kbyte;
		}

		fclose(fp);

		/*
		debug_log("buff [%s]", buff);
		*/

		return buff;
	}

	return NULL;
}

gchar *email_get_file_size_string(guint64 size)
{
	debug_log("");
	gchar str_buf[16] = "\0";

	if (size < 1024) {
		g_sprintf(str_buf, "%d %s", (gint) size, dgettext("sys_string", "IDS_COM_BODY_B"));
	} else {
		gdouble tmpsize = (gdouble) (size / 1024.);

		if (tmpsize < 1024) {
			g_sprintf(str_buf, "%.2f %s", tmpsize, dgettext("sys_string", "IDS_COM_BODY_KB"));
		} else {
			tmpsize /= 1024.;

			if (tmpsize < 1024) {
				g_sprintf(str_buf, "%.2f %s", tmpsize, dgettext("sys_string", "IDS_COM_BODY_MB"));
			} else {
				tmpsize /= 1024.;
				g_sprintf(str_buf, "%.2f %s", tmpsize, dgettext("sys_string", "IDS_COM_BODY_GB"));
			}
		}
	}
	return STR_LEN(str_buf) > 0 ? g_strdup(str_buf) : NULL;
}

guint64 email_get_file_size(const gchar *path)
{
	debug_log("");
	guint64 size = 0;
	struct stat st;
	memset(&st, 0, sizeof(struct stat));

	if (stat(path, &st) == 0) {
		if (S_ISREG(st.st_mode)) {
			size = (guint64) st.st_size;
			debug_log("size (%llu)", size);
		}
	} else {
		size = 0;
	}
	return size;
}

char *email_get_date_text(const char *locale, char *skeleton, void *time)
{
	debug_log("");
	char formattedString[128] = { 0, };
	UChar customSkeleton[64] = { 0, };
	int skeletonLength = strlen(skeleton);

	u_uastrncpy(customSkeleton, skeleton, skeletonLength);
	_generate_best_pattern(locale, customSkeleton, formattedString, time);
	return g_strdup(formattedString);
}

int email_open_icu_pattern_generator(void)
{
	debug_log("");
	UErrorCode status = U_ZERO_ERROR;

	uloc_setDefault(getenv("LC_TIME"), &status);
	if (U_FAILURE(status)) {
		debug_critical("uloc_setDefault() failed: %s\n", u_errorName(status));
		return -1;
	}

	status = U_ZERO_ERROR;
	icu_locale = (char *)uloc_getDefault();
	debug_log("uloc_getDefault: %s", icu_locale);
	appcore_get_timeformat(&icu_timeformat);
	icu_pattern_generator = udatpg_open(icu_locale, &status);

	if (!icu_pattern_generator) {
		debug_log("udatpg_open() failed: %s", u_errorName(status));
		return -1;
	}

	_open_icu_pattern_n_formatter(icu_locale, "hmm", &icu_formatter_hmm);
	_open_icu_pattern_n_formatter(icu_locale, "Hmm", &icu_formatter_Hmm);
	_open_icu_pattern_n_formatter(icu_locale, "MMMyyyy", &icu_formatter_MMMyyyy);
	_open_icu_pattern_n_formatter(icu_locale, "MMMd", &icu_formatter_MMMd);
	_open_icu_pattern_n_formatter(icu_locale, "EEEMMMd", &icu_formatter_EEEMMMd);

	return 0;
}

int email_close_icu_pattern_generator(void)
{
	debug_log("");

	if (icu_pattern_generator) {
		udatpg_close(icu_pattern_generator);
		icu_pattern_generator = NULL;
	}

	_close_icu_pattern_n_formatter(icu_formatter_hmm);
	_close_icu_pattern_n_formatter(icu_formatter_Hmm);
	_close_icu_pattern_n_formatter(icu_formatter_MMMyyyy);
	_close_icu_pattern_n_formatter(icu_formatter_MMMd);
	_close_icu_pattern_n_formatter(icu_formatter_EEEMMMd);

	return 0;
}

static int _open_icu_pattern_n_formatter(const char *locale, char *skeleton, UDateFormat **formatter)
{
	debug_log("");
	UErrorCode status = U_ZERO_ERROR;
	UChar bestPattern[64] = { 0, };
	UChar customSkeleton[64] = { 0, };
	int32_t bestPatternCapacity;
	int32_t bestPatternLength;
	int skeletonLength = strlen(skeleton);

	u_uastrncpy(customSkeleton, skeleton, skeletonLength);

	bestPatternCapacity = (int32_t) (sizeof(bestPattern) / sizeof(bestPattern[0]));
	bestPatternLength = udatpg_getBestPattern(icu_pattern_generator, customSkeleton, u_strlen(customSkeleton), bestPattern, bestPatternCapacity, &status);
	*formatter = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern, -1, &status);

	return 0;
}

static int _close_icu_pattern_n_formatter(UDateFormat *formatter)
{
	debug_log("");

	udat_close(formatter);

	return 0;
}

static void _generate_best_pattern(const char *locale, UChar * customSkeleton, char *formattedString, void *time)
{
	debug_log("");
	UErrorCode status = U_ZERO_ERROR;
	UDateFormat *formatter;
	UDate date = 0;
	UChar bestPattern[64] = { 0, };
	UChar formatted[64] = { 0, };
	int32_t bestPatternCapacity, formattedCapacity;
	int32_t bestPatternLength, formattedLength;

	bestPatternCapacity = (int32_t) (sizeof(bestPattern) / sizeof(bestPattern[0]));
	bestPatternLength = udatpg_getBestPattern(icu_pattern_generator, customSkeleton, u_strlen(customSkeleton), bestPattern, bestPatternCapacity, &status);

	formatter = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern, -1, &status);
	formattedCapacity = (int32_t) (sizeof(formatted) / sizeof(formatted[0]));
	if (time) {
		time_t msg_time = *(time_t *)time;
		date = (UDate)msg_time * 1000;	/* Equivalent to Date = ucal_getNow() in Milliseconds */
	}
	formattedLength = udat_format(formatter, date, formatted, formattedCapacity, NULL, &status);
	u_austrncpy(formattedString, formatted, 128);
	udat_close(formatter);
}

gboolean email_drm_file_is_right(const gchar *path)
{
	RETURN_VAL_IF_FAIL(STR_VALID(path), FALSE);

	debug_log("");

	drm_bool_type_e is_drm_file = FALSE;

	if (drm_is_drm_file(path, &is_drm_file) != DRM_RETURN_SUCCESS) {
		debug_log("drm_is_drm_file is failed");
		return FALSE;
	}

	return is_drm_file;
}

gboolean email_drm_file_forward_lock_check(const gchar *path)
{
	RETURN_VAL_IF_FAIL(STR_VALID(path), FALSE);
	RETURN_VAL_IF_FAIL(email_drm_file_is_right(path), FALSE);

	debug_log("");

	drm_file_info_s drm_info;

	memset(&drm_info, 0, sizeof(drm_file_info_s));

	if (drm_get_file_info(path, &drm_info)) {
		debug_log("drm_info.oma_info.method (%d)", drm_info.oma_info.method);
		if (drm_info.oma_info.method == DRM_METHOD_TYPE_FORWARD_LOCK || drm_info.oma_info.method == DRM_METHOD_TYPE_COMBINED_DELIVERY) {
			debug_log("Forward Lock");
			return TRUE;
		}
	}

	return FALSE;
}
/* EOF */