summaryrefslogtreecommitdiff
path: root/src/app_resource_manager.c
blob: 33504a221670f5b72bbc7a0d1858e8726c297050 (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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <bundle.h>
#include <bundle_internal.h>
#include <aul.h>
#include <dlog.h>
#include <unistd.h>
#include <system_info.h>
#include <pkgmgr-info.h>
#include <pkgmgrinfo_resource.h>
#include <vconf.h>

#include "app_resource_manager.h"
#include "app_common.h"

#ifndef API
#define API __attribute__ ((visibility("default")))
#endif

#ifdef LOG_TAG
#undef LOG_TAG
#endif

#define LOG_TAG "CAPI_APPFW_RESOURCE_MANAGER"

#define WEIGHT_SCREEN_DPI 10000
#define WEIGHT_SCREEN_DPI_RANGE 10000
#define WEIGHT_SCREEN_BPP 1000
#define WEIGHT_SCREEN_WIDTH_RANGE 100
#define WEIGHT_SCREEN_LARGE 10
#define WEIGHT_PLATFORM_VERSION 1000000
#define WEIGHT_LANGUAGE 100000

#define THRESHOLD_TO_CLEAN 50	/* app_resource_manager_trim_cache */

#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
#define MAX_PATH  1024

typedef struct {
	resource_data_t *data;
	GHashTable *cache;
} resource_manager_t;

typedef struct {
	char *output;
	int hit_cnt;
	bool remove;
} resource_cache_context_t;

typedef struct {
	const char *bundle_attr_key;
	unsigned int bundle_attr_value;
} resource_node_attr_t;

enum {
	NODE_ATTR_MIN = 0,
	NODE_ATTR_SCREEN_DPI,
	NODE_ATTR_SCREEN_DPI_RANGE,
	NODE_ATTR_SCREEN_WIDTH_RANGE,
	NODE_ATTR_SCREEN_LARGE,
	NODE_ATTR_SCREEN_BPP,
	NODE_ATTR_PLATFORM_VER,
	NODE_ATTR_LANGUAGE,
	NODE_ATTR_MAX
};

static resource_manager_t *resource_handle = NULL;

static resource_node_attr_t map[] = {
		{ RSC_NODE_ATTR_SCREEN_DPI, NODE_ATTR_SCREEN_DPI },
		{ RSC_NODE_ATTR_SCREEN_DPI_RANGE, NODE_ATTR_SCREEN_DPI_RANGE },
		{ RSC_NODE_ATTR_SCREEN_WIDTH_RANGE, NODE_ATTR_SCREEN_WIDTH_RANGE },
		{ RSC_NODE_ATTR_SCREEN_LARGE, NODE_ATTR_SCREEN_LARGE },
		{ RSC_NODE_ATTR_SCREEN_BPP, NODE_ATTR_SCREEN_BPP },
		{ RSC_NODE_ATTR_PLATFORM_VER, NODE_ATTR_PLATFORM_VER },
		{ RSC_NODE_ATTR_LANGUAGE, NODE_ATTR_LANGUAGE },
};

static GHashTable *attr_key = NULL;
static const char *res_path = NULL;
static char *cur_language = NULL;

static gint __resource_manager_comp(gconstpointer a, gconstpointer b)
{
	resource_group_t *rsc_group = (resource_group_t *) a;

	return strcmp(rsc_group->type, b);
}

static void __bundle_iterator_get_valid_nodes(const char *key, const int type,
		const bundle_keyval_t *kv, void *data)
{
	unsigned int node_attr;
	bool *invalid = (bool *) data;
	bool ret_bool = true;
	int min, max;
	char from[5] = { 0, };
	char to[3] = { 0, };
	bool t_val;
	char *val;
	size_t size;
	static int screen_dpi = -1;
	static int screen_width = -1;
	static int screen_size_large = -1;
	static char *version = NULL;
	static int screen_bpp = -1;

	if (*invalid)
		return;

	bundle_keyval_get_basic_val((bundle_keyval_t *) kv, (void**) &val, &size);

	node_attr = (guint) g_hash_table_lookup(attr_key, key);
	if (node_attr <= NODE_ATTR_MIN || node_attr >= NODE_ATTR_MAX) {
		LOGE("INVALID_PARAMETER(0x%08x), node_attr(%d)",
				APP_RESOURCE_ERROR_INVALID_PARAMETER, node_attr);
		*invalid = true;
		return;
	}

	switch (node_attr) {
		case NODE_ATTR_SCREEN_DPI:
			if (screen_dpi == -1)
				system_info_get_platform_int("http://tizen.org/feature/screen.dpi",
						&screen_dpi);
			if (screen_dpi != atoi(val))
				*invalid = true;
			break;

		case NODE_ATTR_SCREEN_DPI_RANGE:
			sscanf(val, "%s %d %s %d", from, &min, to, &max);
			if (screen_dpi == -1)
				system_info_get_platform_int("http://tizen.org/feature/screen.dpi",
						&screen_dpi);

			if (!(min <= screen_dpi && screen_dpi <= max))
				*invalid = true;
			break;

		case NODE_ATTR_SCREEN_WIDTH_RANGE:
			sscanf(val, "%s %d %s %d", from, &min, to, &max);
			if (screen_width == -1)
				system_info_get_platform_int(
						"http://tizen.org/feature/screen.width", &screen_width);
			if (!(min <= screen_width && screen_width <= max))
				*invalid = true;
			break;

		case NODE_ATTR_SCREEN_LARGE:
			if (!(strcmp(val, "true")))
				t_val = true;
			else
				t_val = false;
			if (screen_size_large == -1) {
				system_info_get_platform_bool(
						"http://tizen.org/feature/screen.size.large", &ret_bool);
				if (ret_bool)
					screen_size_large = 1;
				else
					screen_size_large = 0;
			}
			if (((bool)screen_size_large) != t_val)
				*invalid = true;
			break;

		case NODE_ATTR_SCREEN_BPP:
			if (screen_bpp == -1)
				system_info_get_platform_int("http://tizen.org/feature/screen.bpp",
						&screen_bpp);
			if (screen_bpp != atoi(val))
				*invalid = true;
			break;

		case NODE_ATTR_PLATFORM_VER:
			if (version == NULL)
				system_info_get_platform_string(
						"http://tizen.org/feature/platform.version", &version);
			if (strcmp(version, val))
				*invalid = true;
			break;

		case NODE_ATTR_LANGUAGE:
			if (cur_language == NULL)
				cur_language = vconf_get_str(VCONFKEY_LANGSET);
			if (strncmp(cur_language, val, strlen(val)))
				*invalid = true;
			break;

		default:
			break;
	}
}

static void __bundle_iterator_get_best_node(const char *key, const char *val,
		void *data)
{
	unsigned int node_attr;
	unsigned int *weight = (unsigned int *) data;

	node_attr = (guint) g_hash_table_lookup(attr_key, key);
	if (node_attr <= NODE_ATTR_MIN || node_attr >= NODE_ATTR_MAX) {
		LOGE("INVALID_PARAMETER(0x%08x), node_attr(%d)",
				APP_RESOURCE_ERROR_INVALID_PARAMETER, node_attr);
		return;
	}

	switch (node_attr) {
		case NODE_ATTR_SCREEN_DPI:
			*weight += WEIGHT_SCREEN_DPI;
			break;

		case NODE_ATTR_SCREEN_DPI_RANGE:
			*weight += WEIGHT_SCREEN_DPI_RANGE;
			break;

		case NODE_ATTR_SCREEN_WIDTH_RANGE:
			*weight += WEIGHT_SCREEN_WIDTH_RANGE;
			break;

		case NODE_ATTR_SCREEN_LARGE:
			*weight += WEIGHT_SCREEN_LARGE;
			break;

		case NODE_ATTR_SCREEN_BPP:
			*weight += WEIGHT_SCREEN_BPP;
			break;

		case NODE_ATTR_PLATFORM_VER:
			*weight += WEIGHT_PLATFORM_VERSION;
			break;

		case NODE_ATTR_LANGUAGE:
			*weight += WEIGHT_LANGUAGE;
			break;

		default:
			break;
	}
}

static const char *__get_cache(app_resource_e type,
		const char *id)
{
	unsigned int total_len = 0;
	char *key = NULL;
	char *rsc_type;
	resource_cache_context_t *resource_cache = NULL;

	if (id == NULL) {
		LOGE("INVALID_PARAMETER(0x%08x), id",
				APP_RESOURCE_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	if (type < APP_RESOURCE_TYPE_MIN || type > APP_RESOURCE_TYPE_MAX) {
		LOGE("INVALID_PARAMETER(0x%08x), type(%d)",
				APP_RESOURCE_ERROR_INVALID_PARAMETER, type);
		return NULL;
	} else {
		switch (type) {
			case APP_RESOURCE_TYPE_IMAGE:
				rsc_type = RSC_GROUP_TYPE_IMAGE;
				break;

			case APP_RESOURCE_TYPE_LAYOUT:
				rsc_type = RSC_GROUP_TYPE_LAYOUT;
				break;

			case APP_RESOURCE_TYPE_SOUND:
				rsc_type = RSC_GROUP_TYPE_SOUND;
				break;

			case APP_RESOURCE_TYPE_BIN:
				rsc_type = RSC_GROUP_TYPE_BIN;
				break;

			default:
				rsc_type = "NULL";
		}
	}

	if (resource_handle->cache == NULL) {
		LOGE("INVALID_PARAMETER(0x%08x), hashtable",
				APP_RESOURCE_ERROR_INVALID_PARAMETER);
		return NULL;
	} else {
		total_len = strlen(rsc_type) + strlen(id) + 2;

		key = (char *) calloc(1, total_len);
		if (key == NULL) {
			LOGE("failed to create a resource_cache(0x%08x)",
					APP_RESOURCE_ERROR_OUT_OF_MEMORY);
			free(resource_cache);
			return NULL;
		}

		snprintf(key, total_len, "%s:%s", rsc_type, id);
		LOGD("key : %s", key);

		resource_cache = g_hash_table_lookup(resource_handle->cache, key);
		free(key);
		if (resource_cache == NULL) {
			LOGE("IO_ERROR(0x%08x), find list resource_cache",
					APP_RESOURCE_ERROR_IO_ERROR);
			return NULL;
		}

		resource_cache->hit_cnt++;
	}

	return resource_cache->output;
}

static gint __cache_hit_compare(gconstpointer a, gconstpointer b)
{
	const resource_cache_context_t *lhs = (const resource_cache_context_t *) a;
	const resource_cache_context_t *rhs = (const resource_cache_context_t *) b;

	return lhs->hit_cnt - rhs->hit_cnt;
}

static gboolean __cache_remove(gpointer key, gpointer value, gpointer user_data)
{
	resource_cache_context_t *c = (resource_cache_context_t *) (value);

	if (c->remove) {
		free(key);
		free(c->output);
		free(c);
		return TRUE;
	}

	return FALSE;
}

static void __trim_cache(void)
{
	GList *values = g_hash_table_get_values(resource_handle->cache);
	values = g_list_sort(values, __cache_hit_compare);

	int i = 0;
	GList *iter_list = values;
	while (iter_list != NULL) {
		if (i >= (THRESHOLD_TO_CLEAN / 2))
			break;

		resource_cache_context_t *c =
				(resource_cache_context_t *) (iter_list->data);
		c->remove = true;
		iter_list = g_list_next(iter_list);
		i++;
	}

	g_list_free(values);
	g_hash_table_foreach_remove(resource_handle->cache, __cache_remove, NULL);

}

static void __put_cache(app_resource_e type, const char *id,
		const char *val)
{
	unsigned int total_len = 0;
	char *key;
	char *rsc_type;
	resource_cache_context_t *resource_cache;

	/* To remove chache from the low frequency of use. */
	if (val == NULL) {
		LOGE("INVALID_PARAMETER(0x%08x), fname",
				APP_RESOURCE_ERROR_INVALID_PARAMETER);
		return;
	}

	if (id == NULL) {
		LOGE("INVALID_PARAMETER(0x%08x), id",
				APP_RESOURCE_ERROR_INVALID_PARAMETER);
		return;
	}

	if (type < APP_RESOURCE_TYPE_MIN || type > APP_RESOURCE_TYPE_MAX) {
		LOGE("INVALID_PARAMETER(0x%08x), type(%d)",
				APP_RESOURCE_ERROR_INVALID_PARAMETER, type);
		return;
	} else {
		switch (type) {
			case APP_RESOURCE_TYPE_IMAGE:
				rsc_type = RSC_GROUP_TYPE_IMAGE;
				break;

			case APP_RESOURCE_TYPE_LAYOUT:
				rsc_type = RSC_GROUP_TYPE_LAYOUT;
				break;

			case APP_RESOURCE_TYPE_SOUND:
				rsc_type = RSC_GROUP_TYPE_SOUND;
				break;

			case APP_RESOURCE_TYPE_BIN:
				rsc_type = RSC_GROUP_TYPE_BIN;
				break;

			default:
				rsc_type = "NULL";
		}
	}

	if (g_hash_table_size(resource_handle->cache) > THRESHOLD_TO_CLEAN)
		__trim_cache();

	resource_cache = (resource_cache_context_t *) calloc(1,
			sizeof(resource_cache_context_t));
	if (resource_cache == NULL) {
		LOGE("failed to create a resource_group(0x%08x)",
				APP_RESOURCE_ERROR_OUT_OF_MEMORY);
		return;
	}

	total_len = strlen(rsc_type) + strlen(id) + 2;

	key = (char *) calloc(1, total_len);
	if (key == NULL) {
		LOGE("failed to create a resource_cache(0x%08x)",
				APP_RESOURCE_ERROR_OUT_OF_MEMORY);
		free(resource_cache);
		return;
	}

	snprintf(key, total_len, "%s:%s", rsc_type, id);
	LOGD("key : %s", key);

	resource_cache->output = strdup(val);
	resource_cache->hit_cnt = 0;
	resource_cache->remove = false;

	g_hash_table_insert(resource_handle->cache, key, resource_cache);
}

static resource_group_t *__find_group(resource_data_t *data,
		int type)
{
	resource_group_t *rsc_group = NULL;
	char *rsc_type;

	if (data == NULL) {
		LOGE("INVALID_PARAMETER(0x%08x), resource_data_t",
				APP_RESOURCE_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	if (type < APP_RESOURCE_TYPE_MIN || type > APP_RESOURCE_TYPE_MAX) {
		LOGE("INVALID_PARAMETER(0x%08x), type(%d)",
				APP_RESOURCE_ERROR_INVALID_PARAMETER, type);
		return NULL;
	} else {
		switch (type) {
			case APP_RESOURCE_TYPE_IMAGE:
				rsc_type = RSC_GROUP_TYPE_IMAGE;
				break;

			case APP_RESOURCE_TYPE_LAYOUT:
				rsc_type = RSC_GROUP_TYPE_LAYOUT;
				break;

			case APP_RESOURCE_TYPE_SOUND:
				rsc_type = RSC_GROUP_TYPE_SOUND;
				break;

			case APP_RESOURCE_TYPE_BIN:
				rsc_type = RSC_GROUP_TYPE_BIN;
				break;

			default:
				rsc_type = "NULL";
		}
	}

	GList* found = g_list_find_custom(data->group_list, rsc_type,
			__resource_manager_comp);
	if (found == NULL) {
		LOGE("IO_ERROR(0x%08x), find list resource_group %s",
				APP_RESOURCE_ERROR_IO_ERROR, rsc_type);
		return NULL;
	}

	rsc_group = (resource_group_t *) (found->data);

	return rsc_group;
}

static GList *__get_valid_nodes(resource_group_t *group,
		const char *id)
{
	GList *list = NULL;
	GList *valid_list = NULL;
	resource_node_t *valid_node = NULL;
	resource_node_t *rsc_node = NULL;

	if (group->node_list == NULL) {
		LOGE("INVALID_PARAMETER(0x%08x), resource_group",
				APP_RESOURCE_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	list = g_list_first(group->node_list);

	char path_buf[MAX_PATH] = { 0, };
	while (list) {
		bool invalid = false;
		rsc_node = (resource_node_t *) list->data;

		snprintf(path_buf, MAX_PATH - 1, "%s%s/%s", res_path,
				rsc_node->folder, id);
		if (access(path_buf, R_OK) == 0) {
			bundle_foreach(rsc_node->attr, __bundle_iterator_get_valid_nodes,
					&invalid);

			if (!invalid) {
				valid_node = (resource_node_t *) list->data;
				valid_list = g_list_append(valid_list, valid_node);
			}
		}

		list = g_list_next(list);
	}

	return valid_list;
}

static resource_node_t *__get_best_node(GList *nodes)
{
	unsigned int weight_tmp = 0;
	resource_node_t *best_node = NULL;
	GList *list = NULL;

	if (nodes == NULL) {
		LOGE("INVALID_PARAMETER(0x%08x), resource_node lists",
				APP_RESOURCE_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	list = g_list_first(nodes);

	while (list != NULL) {
		unsigned int weight = 0;
		resource_node_t *res_node = (resource_node_t *) (list->data);

		bundle_iterate(res_node->attr, __bundle_iterator_get_best_node, &weight);
		if (weight > weight_tmp) {
			best_node = res_node;
			weight_tmp = weight;
		}
		list = g_list_next(list);
	}

	return best_node;
}

static int __open(resource_manager_t **handle)
{
	int retval = APP_RESOURCE_ERROR_NONE;
	resource_manager_t *rsc_manager = NULL;
	char buf[1024] = { 0, };

	rsc_manager = (resource_manager_t *) calloc(1, sizeof(resource_manager_t));
	if (!rsc_manager) {
		LOGE("failed to create a resource_manager(0x%08x)",
				APP_RESOURCE_ERROR_OUT_OF_MEMORY);
		return APP_RESOURCE_ERROR_OUT_OF_MEMORY;
	}

	snprintf(buf, 1023, "%sres.xml", res_path);
	retval = pkgmgrinfo_resource_open(buf, &(rsc_manager->data));
	if (retval != PMINFO_R_OK) {
		LOGE("IO_ERROR(0x%08x), failed to get db for resource manager",
				APP_RESOURCE_ERROR_IO_ERROR);
		free(rsc_manager);
		return APP_RESOURCE_ERROR_IO_ERROR;
	}

	rsc_manager->cache = g_hash_table_new(g_str_hash, g_str_equal);
	*handle = rsc_manager;

	return APP_RESOURCE_ERROR_NONE;
}

static void __invalidate_cache()
{
	if (resource_handle != NULL) {
		if (resource_handle->cache != NULL) {
			GHashTableIter iter;
			gpointer key, value;

			g_hash_table_iter_init(&iter, resource_handle->cache);
			while (g_hash_table_iter_next(&iter, &key, &value)) {
				free(key);
				resource_cache_context_t *c = (resource_cache_context_t *) value;
				free(c->output);
				free(value);
			}
			g_hash_table_remove_all(resource_handle->cache);
			if (cur_language) {
				free(cur_language);
				cur_language = NULL;
			}
		}
	}
}

static int __close(resource_manager_t *handle)
{
	if (handle == NULL) {
		LOGE("INVALID_PARAMETER(0x%08x), resource_manager",
				APP_RESOURCE_ERROR_INVALID_PARAMETER);
		return APP_RESOURCE_ERROR_INVALID_PARAMETER;
	}

	__invalidate_cache();
	if (handle->cache != NULL) {
		g_hash_table_destroy(handle->cache);
	}

	if (handle->data != NULL) {
		pkgmgrinfo_resource_close(handle->data);
	}

	free(handle);

	return APP_RESOURCE_ERROR_NONE;
}

static void __vconf_cb(keynode_t *key, void *data)
{
	__invalidate_cache();
}

int app_resource_manager_init()
{
	if (resource_handle == NULL) {
		int retval = APP_RESOURCE_ERROR_NONE;

		res_path = aul_get_app_resource_path();
		if (res_path == NULL) {
			LOGE("IO_ERROR(0x%08x), failed to get resource path",
					APP_RESOURCE_ERROR_IO_ERROR);
			return APP_RESOURCE_ERROR_IO_ERROR;
		}

		retval = __open(&resource_handle);
		if (retval != APP_RESOURCE_ERROR_NONE) {
			LOGE("IO_ERROR(0x%08x), failed to get resource_handle(%d)",
					APP_RESOURCE_ERROR_IO_ERROR, retval);
			return APP_RESOURCE_ERROR_IO_ERROR;
		}

		if (attr_key == NULL) {
			attr_key = g_hash_table_new(g_str_hash, g_str_equal);

			if (attr_key == NULL)
				return APP_RESOURCE_ERROR_OUT_OF_MEMORY;

			unsigned int i;
			for (i = 0; i < ARRAY_SIZE(map); i++) {
				g_hash_table_insert(attr_key, (char *) map[i].bundle_attr_key,
						(gpointer) (map[i].bundle_attr_value));
			}
		}

		int r = vconf_notify_key_changed(VCONFKEY_LANGSET, __vconf_cb, NULL);

		if (r < 0) {
			LOGE("IO_ERROR(0x%08x), failed to register vconf(%d)",
					APP_RESOURCE_ERROR_IO_ERROR, r);
			return APP_RESOURCE_ERROR_IO_ERROR;
		}

	}

	return APP_RESOURCE_ERROR_NONE;
}

int app_resource_manager_get(app_resource_e type, const char *id, char **path)
{
	int retval = APP_RESOURCE_ERROR_NONE;
	char *put_fname = NULL;
	const char *cached_path = NULL;
	GList *list = NULL;
	resource_group_t *resource_group = NULL;
	resource_node_t *resource_node = NULL;

	*path = NULL;

	if (id == NULL) {
		LOGE("INVALID_PARAMETER(0x%08x), resource_data_t",
				APP_RESOURCE_ERROR_INVALID_PARAMETER);
		return APP_RESOURCE_ERROR_INVALID_PARAMETER;
	}

	if (type < APP_RESOURCE_TYPE_MIN || type > APP_RESOURCE_TYPE_MAX) {
		LOGE("INVALID_PARAMETER(0x%08x), type(%d)",
				APP_RESOURCE_ERROR_INVALID_PARAMETER, type);
		return APP_RESOURCE_ERROR_INVALID_PARAMETER;
	}

	if (resource_handle == NULL) {
		retval = app_resource_manager_init();
		if (retval != APP_RESOURCE_ERROR_NONE)
			return retval;
	}

	/* To get fname from cache */
	cached_path = __get_cache(type, id);
	if (cached_path != NULL) {
		*path = strdup(cached_path);
		return APP_RESOURCE_ERROR_NONE;
	} else { /* can't find fname from cache */
		if (resource_handle == NULL)
			return APP_RESOURCE_ERROR_IO_ERROR;

		resource_group = __find_group(resource_handle->data, type);
		if (resource_group == NULL) {
			LOGE("IO_ERROR(0x%08x), failed to get resource_group",
					APP_RESOURCE_ERROR_IO_ERROR);
			retval = APP_RESOURCE_ERROR_IO_ERROR;
			goto Exception;
		}

		list = __get_valid_nodes(resource_group, id);
		if (list == NULL) {
			retval = APP_RESOURCE_ERROR_IO_ERROR;
			goto Exception;
		}

		resource_node = __get_best_node(list);
		if (resource_node == NULL) {
			retval = APP_RESOURCE_ERROR_IO_ERROR;
			goto Exception;
		} else {
			unsigned int total_len = strlen(res_path)
					+ strlen(resource_node->folder) + strlen(id) + 3;
			put_fname = (char *) calloc(1, total_len);
			snprintf(put_fname, total_len, "%s%s/%s", res_path,
					resource_node->folder, id);
			*path = strdup(put_fname);
		}
		__put_cache(type, id, put_fname);
	}

Exception:
	if (list != NULL)
		g_list_free(list);

	if (put_fname == NULL && resource_group != NULL) {
		char path_buf[MAX_PATH] = { 0, };

		snprintf(path_buf, MAX_PATH - 1, "%s%s/%s", res_path,
				resource_group->folder, id);
		if (access(path_buf, R_OK) == 0) {
			__put_cache(type, id, path_buf);
			*path = strdup(path_buf);
			retval = APP_RESOURCE_ERROR_NONE;
		}
	}

	if (put_fname != NULL)
		free(put_fname);

	return retval;
}

int app_resource_manager_release()
{
	if (resource_handle != NULL) {
		__close(resource_handle);
		resource_handle = NULL;
	}

	if (attr_key != NULL) {
		g_hash_table_destroy(attr_key);
		attr_key = NULL;
	}

	if (cur_language) {
		free(cur_language);
		cur_language = NULL;
	}

	vconf_ignore_key_changed(VCONFKEY_LANGSET, __vconf_cb);
	return APP_RESOURCE_ERROR_NONE;
}