tizen 2.3.1 release
[framework/appfw/aul-1.git] / am_daemon / amd_appinfo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <assert.h>
6 #include <glib.h>
7 #include <dirent.h>
8
9 #include <pkgmgr-info.h>
10 #include <vconf.h>
11 #include "amd_config.h"
12 #include "simple_util.h"
13 #include "amd_appinfo.h"
14
15
16 #define SERVICE_GROUP "Service"
17
18 struct appinfomgr {
19         GHashTable *tbl; /* key is filename, value is struct appinfo */
20 };
21
22 enum _appinfo_idx {
23         _AI_FILE = 0, /* service filename */
24         _AI_NAME,
25         _AI_COMP,
26         _AI_EXEC,
27         _AI_TYPE,
28         _AI_ONBOOT,
29         _AI_RESTART,
30         _AI_MULTI,
31         _AI_HWACC,
32         _AI_PERM,
33         _AI_PKGID,
34         _AI_TASKMANAGE,
35         _AI_PRELOAD,
36         _AI_INDICATORDISP,
37         _AI_EFFECTIMAGEPORT,
38         _AI_EFFECTIMAGELAND,
39 #ifdef _APPFW_FEATURE_CHANGEABLE_COLOR
40         _AI_EFFECTTYPE,
41 #endif
42         _AI_STATUS,
43 #ifdef _APPFW_FEATURE_PROCESS_POOL
44         _AI_POOL,
45 #endif
46         _AI_COMPTYPE,
47 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
48         _AI_MULTI_INSTANCE,
49         _AI_MULTI_INSTANCE_MAINID,
50         _AI_TOGGLE_ORDER,
51 #endif
52 #ifdef _APPFW_FEATURE_MULTI_WINDOW
53         _AI_MULTI_WINDOW,
54 #endif
55 #ifdef _APPFW_FEATURE_TTS_MODE
56         _AI_TTS,
57 #endif
58 #ifdef _APPFW_FEATURE_ULTRA_POWER_SAVING_MODE
59         _AI_UPS,
60 #endif
61         _AI_MAX,
62 };
63 #define _AI_START _AI_NAME /* start index */
64
65 struct appinfo_t {
66         char *name;
67         enum appinfo_type type;
68 };
69
70 static struct appinfo_t _appinfos[] = {
71         [_AI_NAME] = { "Name", AIT_NAME, },
72         [_AI_COMP] = { "Component", AIT_COMP, },
73         [_AI_EXEC] = { "Exec", AIT_EXEC, },
74         [_AI_TYPE] = { "PkgType", AIT_TYPE, },
75         [_AI_ONBOOT] = { "StartOnBoot", AIT_ONBOOT, },
76         [_AI_RESTART] = { "AutoRestart", AIT_RESTART, },
77         [_AI_MULTI] = { "Multiple", AIT_MULTI, },
78         [_AI_HWACC] = { "Hwacceleration", AIT_HWACC, },
79         [_AI_PERM] = { "PermissionType", AIT_PERM, },
80         [_AI_PKGID] = { "PackageId", AIT_PKGID, },
81         [_AI_TASKMANAGE] = { "Taskmanage", AIT_TASKMANAGE, },
82         [_AI_PRELOAD] = { "Preload", AIT_PRELOAD, },
83         [_AI_INDICATORDISP] = { "indicatordisplay", AIT_INDICATOR_DISP, },
84         [_AI_EFFECTIMAGEPORT] = { "portaitimg", AIT_EFFECTIMAGEPORT, },
85         [_AI_EFFECTIMAGELAND] = { "landscapeimg", AIT_EFFECTIMAGELAND, },
86 #ifdef _APPFW_FEATURE_CHANGEABLE_COLOR
87         [_AI_EFFECTTYPE] = { "EffectType", AIT_EFFECTTYPE, },
88 #endif
89         [_AI_STATUS] = { "status", AIT_STATUS, },
90 #ifdef _APPFW_FEATURE_PROCESS_POOL
91         [_AI_POOL] = { "ProcessPool", AIT_POOL, },
92 #endif
93         [_AI_COMPTYPE] = { "ComponentType", AIT_COMPTYPE, },
94 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
95         [_AI_MULTI_INSTANCE] = { "multi-instance", AIT_MULTI_INSTANCE, },
96         [_AI_MULTI_INSTANCE_MAINID] = { "multi-instance-mainid", AIT_MULTI_INSTANCE_MAINID, },
97         [_AI_TOGGLE_ORDER] = { "toggleOrder", AIT_TOGGLE_ORDER, },
98 #endif
99 #ifdef _APPFW_FEATURE_MULTI_WINDOW
100         [_AI_MULTI_WINDOW] = { "multi-window", AIT_MULTI_WINDOW, },
101 #endif
102 #ifdef _APPFW_FEATURE_TTS_MODE
103         [_AI_TTS] = { "TTS", AIT_TTS, },
104 #endif
105 #ifdef _APPFW_FEATURE_ULTRA_POWER_SAVING_MODE
106         [_AI_UPS] = { "UPS", AIT_UPS, },
107 #endif
108 };
109
110 struct appinfo {
111         char *val[_AI_MAX];
112 };
113
114 struct pkginfo {
115         char *pkgid;
116         char *op;
117 };
118
119 int gles = 1;
120
121 GHashTable *pkg_tbl;
122
123 static void _free_appinfo(gpointer data)
124 {
125         struct appinfo *c = data;
126         int i;
127
128         if (!c)
129                 return;
130
131         for (i = 0; i < sizeof(c->val)/sizeof(c->val[0]); i++) {
132                 if(c->val[i] != NULL)
133                         free(c->val[i]);
134         }
135
136         free(c);
137 }
138
139 static void _fini(struct appinfomgr *cf)
140 {
141         assert(cf);
142
143         g_hash_table_destroy(cf->tbl);
144         g_hash_table_destroy(pkg_tbl);
145         free(cf);
146 }
147
148 static int __app_info_insert_handler (const pkgmgrinfo_appinfo_h handle, void *data)
149 {
150         struct appinfo *c;
151         struct appinfomgr *cf = (struct appinfomgr *)data;
152         gboolean r;
153         char *exec;
154         char *portraitimg;
155         char *landscapeimg;
156 #ifdef _APPFW_FEATURE_CHANGEABLE_COLOR
157         char *effectimg_type;
158 #endif
159         char *type;
160         char *appid;
161         char *pkgid;
162         char *component_type;
163         char *multi_mainid;
164         bool multiple;
165 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
166         bool multi_instance;
167 #endif
168 #ifdef _APPFW_FEATURE_MULTI_WINDOW
169         bool multi_window;
170 #endif
171         bool onboot;
172         bool restart;
173         pkgmgrinfo_app_hwacceleration hwacc;
174         pkgmgrinfo_app_component component;
175         pkgmgrinfo_permission_type permission;
176         bool indicator_display;
177         int ret = -1;
178         bool taskmanage;
179         bool preload;
180 #ifdef _APPFW_FEATURE_PROCESS_POOL
181         bool process_pool = 0;
182 #endif
183         int support_mode = 0;
184
185         _D("__app_info_insert_handler");
186
187         if (!handle) {
188                 _E("null app handle");
189                 return -1;
190         }
191
192         ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
193         if (ret < 0) {
194                 _E("fail to get appinfo");
195                 return -1;
196         }
197
198         g_hash_table_remove(cf->tbl, appid);
199
200         c = calloc(1, sizeof(*c));
201         if (!c) {
202                 _E("create appinfo: %s", strerror(errno));
203                 return -1;
204         }
205
206         memset(c, 0, sizeof(struct appinfo));
207
208         c->val[_AI_FILE] = strdup(appid);
209         if (!c->val[_AI_FILE]) {
210                 _E("create appinfo: %s", strerror(errno));
211                 _free_appinfo(c);
212                 return -1;
213         }
214
215         c->val[_AI_NAME] = strdup(appid); //TODO :
216
217         ret = pkgmgrinfo_appinfo_get_component(handle, &component);
218         if (ret < 0) {
219                 _E("fail to get component");
220                 _free_appinfo(c);
221                 return -1;
222         }
223         if(component == PMINFO_UI_APP) {
224                 c->val[_AI_COMP] = strdup("ui"); //TODO :
225
226                 r = pkgmgrinfo_appinfo_is_multiple(handle, &multiple);
227                 if(multiple == true)
228                         c->val[_AI_MULTI] = strdup("true");
229                 else c->val[_AI_MULTI] = strdup("false");
230
231                 r = pkgmgrinfo_appinfo_is_taskmanage(handle, &taskmanage);
232                 if (taskmanage == false) {
233                         c->val[_AI_TASKMANAGE] = strdup("false");
234                 } else {
235                         c->val[_AI_TASKMANAGE] = strdup("true");
236                 }
237
238                 r = pkgmgrinfo_appinfo_is_preload(handle, &preload);
239                 if (preload == false) {
240                         c->val[_AI_PRELOAD] = strdup("false");
241                 } else {
242                         c->val[_AI_PRELOAD] = strdup("true");
243                 }
244
245                 if(gles == 0) {
246                         c->val[_AI_HWACC] = strdup("NOT_USE");
247                 } else {
248
249                         r = pkgmgrinfo_appinfo_get_hwacceleration(handle, &hwacc);
250                         if (hwacc == PMINFO_HWACCELERATION_ON) {
251                                 c->val[_AI_HWACC] = strdup("USE");
252                         } else if (hwacc == PMINFO_HWACCELERATION_DEFAULT) {
253                                 c->val[_AI_HWACC] = strdup("SYS");
254                         } else {
255                                 c->val[_AI_HWACC] = strdup("NOT_USE");
256                         }
257                 }
258
259                 r = pkgmgrinfo_appinfo_is_indicator_display_allowed(handle, &indicator_display);
260                 if (r <0 ){
261                         _E("ERROR IN FETCHING INDICATOR DISP INFO\n");
262                         c->val[_AI_INDICATORDISP] = strdup("true");
263                 }else{
264                         if (indicator_display == true){
265                                 c->val[_AI_INDICATORDISP] = strdup("true");
266                         }else{
267                                 c->val[_AI_INDICATORDISP] = strdup("false");
268                         }
269                 }
270
271                 r = pkgmgrinfo_appinfo_get_effectimage(handle, &portraitimg, &landscapeimg);
272                 if (r < 0){
273                         _E("ERROR IN FETCHING EFFECT IMAGES\n");
274                         c->val[_AI_EFFECTIMAGEPORT] = NULL;
275                         c->val[_AI_EFFECTIMAGELAND] = NULL;
276                 }else{
277                         if (portraitimg)
278                                 c->val[_AI_EFFECTIMAGEPORT] = strdup(portraitimg);
279                         else
280                                 c->val[_AI_EFFECTIMAGEPORT] = NULL;
281                         if (landscapeimg)
282                                 c->val[_AI_EFFECTIMAGELAND] = strdup(landscapeimg);
283                         else
284                                 c->val[_AI_EFFECTIMAGELAND] = NULL;
285                 }
286 #ifdef _APPFW_FEATURE_CHANGEABLE_COLOR
287                 r = pkgmgrinfo_appinfo_get_effectimage_type(handle, &effectimg_type);
288                 c->val[_AI_EFFECTTYPE] = strdup(effectimg_type);
289 #endif
290
291 #ifdef _APPFW_FEATURE_PROCESS_POOL
292                 r = pkgmgrinfo_appinfo_is_process_pool(handle, &process_pool);
293                 if (process_pool == false) {
294                         c->val[_AI_POOL] = strdup("false");
295                 } else {
296                         c->val[_AI_POOL] = strdup("true");
297                 }
298 #endif
299                 r = pkgmgrinfo_appinfo_get_component_type(handle, &component_type);
300                 c->val[_AI_COMPTYPE] = strdup(component_type);
301
302                 r = pkgmgrinfo_appinfo_is_onboot(handle, &onboot);
303                 if(onboot == true)
304                         c->val[_AI_ONBOOT] = strdup("true");
305                 else c->val[_AI_ONBOOT] = strdup("false");
306
307                 r = pkgmgrinfo_appinfo_is_autorestart(handle, &restart);
308                 if(restart == true)
309                         c->val[_AI_RESTART] = strdup("true");
310                 else c->val[_AI_RESTART] = strdup("false");
311
312 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
313                 r = pkgmgrinfo_appinfo_is_multi_instance(handle, &multi_instance);
314                 if(multi_instance == true)
315                         c->val[_AI_MULTI_INSTANCE] = strdup("true");
316                 else
317                         c->val[_AI_MULTI_INSTANCE] = strdup("false");
318
319                 r = pkgmgrinfo_appinfo_get_multi_instance_mainid(handle, &multi_mainid);
320                 c->val[_AI_MULTI_INSTANCE_MAINID] = strdup(multi_mainid);
321
322                 // Toggle order
323                 c->val[_AI_TOGGLE_ORDER] = strdup("0");
324 #endif
325 #ifdef _APPFW_FEATURE_MULTI_WINDOW
326                 r = pkgmgrinfo_appinfo_is_multi_window(handle, &multi_window);
327                 if((r == PMINFO_R_OK) && (multi_window == true))
328                         c->val[_AI_MULTI_WINDOW] = strdup("true");
329                 else
330                         c->val[_AI_MULTI_WINDOW] = strdup("false");
331 #endif
332         } else {
333                 c->val[_AI_COMP] = strdup("svc");
334
335                 r = pkgmgrinfo_appinfo_is_onboot(handle, &onboot);
336                 if(onboot == true)
337                         c->val[_AI_ONBOOT] = strdup("true");
338                 else c->val[_AI_ONBOOT] = strdup("false");
339
340                 r = pkgmgrinfo_appinfo_is_autorestart(handle, &restart);
341                 if(restart == true)
342                         c->val[_AI_RESTART] = strdup("true");
343                 else c->val[_AI_RESTART] = strdup("false");
344         }
345
346         r = pkgmgrinfo_appinfo_get_exec(handle, &exec);
347         c->val[_AI_EXEC] = strdup(exec);
348
349         r = pkgmgrinfo_appinfo_get_apptype(handle, &type);
350         if(strncmp(type, "capp", 4) == 0 ) {
351                 c->val[_AI_TYPE] = strdup("rpm");
352         } else if (strncmp(type, "c++app", 6) == 0 || strncmp(type, "ospapp", 6) == 0) {
353                 c->val[_AI_TYPE] = strdup("tpk");
354         } else if (strncmp(type, "webapp", 6) == 0) {
355                 c->val[_AI_TYPE] = strdup("wgt");
356         }
357
358         r = pkgmgrinfo_appinfo_get_permission_type(handle, &permission);
359         if (permission == PMINFO_PERMISSION_SIGNATURE) {
360                 c->val[_AI_PERM] = strdup("signature");
361         } else if (permission == PMINFO_PERMISSION_PRIVILEGE) {
362                 c->val[_AI_PERM] = strdup("privilege");
363         } else {
364                 c->val[_AI_PERM] = strdup("normal");
365         }
366
367         pkgmgrinfo_appinfo_get_support_mode(handle, &support_mode);
368 #ifdef _APPFW_FEATURE_TTS_MODE
369         if(support_mode & PMINFO_MODE_PROP_SCREEN_READER) {
370                 c->val[_AI_TTS] = strdup("true");
371         } else {
372                 c->val[_AI_TTS] = strdup("false");
373         }
374 #endif
375 #ifdef _APPFW_FEATURE_ULTRA_POWER_SAVING_MODE
376         if(support_mode & PMINFO_MODE_PROP_ULTRA_POWER_SAVING) {
377                 c->val[_AI_UPS] = strdup("true");
378         } else {
379                 c->val[_AI_UPS] = strdup("false");
380         }
381 #endif
382
383         r = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
384         c->val[_AI_PKGID] = strdup(pkgid);
385
386         c->val[_AI_STATUS] = strdup("installed");
387
388         SECURE_LOGD("appinfo file:%s, comp:%s, type:%s", c->val[_AI_FILE], c->val[_AI_COMP], c->val[_AI_TYPE]);
389
390         g_hash_table_insert(cf->tbl, c->val[_AI_FILE], c);
391
392         return 0;
393 }
394
395 static int __app_info_delete_handler (const pkgmgrinfo_appinfo_h handle, void *data)
396 {
397         struct appinfomgr *cf = (struct appinfomgr *)data;
398         char *appid;
399
400         pkgmgrinfo_appinfo_get_appid(handle, &appid);
401
402         g_hash_table_remove(cf->tbl, appid);
403
404         return 0;
405 }
406
407 static int _read_pkg_info(struct appinfomgr *cf)
408 {
409         int ret = pkgmgrinfo_appinfo_get_install_list(__app_info_insert_handler, cf);
410
411         assert(ret == PMINFO_R_OK);
412
413         return ret;
414 }
415
416 static struct appinfomgr *_init()
417 {
418         struct appinfomgr *cf;
419
420         cf = calloc(1, sizeof(*cf));
421         if (!cf) {
422                 _E("appinfo init: %s", strerror(errno));
423                 return NULL;
424         }
425
426         cf->tbl = g_hash_table_new_full(g_str_hash, g_str_equal,
427                         NULL, _free_appinfo);
428
429         pkg_tbl = g_hash_table_new(g_str_hash, g_str_equal);
430         _E("pkg_tbl : %x", pkg_tbl);
431         return cf;
432 }
433
434 static gboolean __amd_pkgmgrinfo_start_handler (gpointer key, gpointer value, gpointer user_data)
435 {
436         struct appinfo *c = value;
437         char *pkgid = (char *)user_data;
438
439         if (c != NULL && strcmp(c->val[_AI_PKGID], pkgid) == 0) {
440                 free(c->val[_AI_STATUS]);
441                 c->val[_AI_STATUS] = strdup("blocking");
442                 SECURE_LOGD("pkgmgr working for this application(%s)", c->val[_AI_NAME]);
443         }
444         return TRUE;
445 }
446
447 static gboolean __amd_pkgmgrinfo_fail_handler (gpointer key, gpointer value, gpointer user_data)
448 {
449         struct appinfo *c = value;
450         char *pkgid = (char *)user_data;
451
452         if (c != NULL && strcmp(c->val[_AI_PKGID], pkgid) == 0) {
453                 free(c->val[_AI_STATUS]);
454                 c->val[_AI_STATUS] = strdup("installed");
455                 SECURE_LOGD("pkgmgr fail(%s)", c->val[_AI_NAME]);
456         }
457         return TRUE;
458 }
459
460 static int __amd_pkgmgrinfo_install_end_handler(pkgmgrinfo_appinfo_h handle, void *user_data)
461 {
462         char *appid = NULL;
463         struct appinfomgr *cf = (struct appinfomgr *)user_data;
464         struct appinfo *c;
465         const char *componet;
466         int r;
467
468         pkgmgrinfo_appinfo_get_appid(handle, &appid);
469
470         __app_info_insert_handler(handle, user_data);
471         c = g_hash_table_lookup(cf->tbl, appid);
472
473         componet = appinfo_get_value(c, AIT_COMPTYPE);
474         r = appinfo_get_boolean(c, AIT_ONBOOT);
475
476         if (r == 1 && componet && strncmp(componet, "svcapp", 6) == 0)
477         {
478                 _D("start service - %s", appid);
479                 _start_srv(c, NULL);
480         }
481
482         return 0;
483 }
484
485 static int __amd_pkgmgrinfo_update_end_handler(pkgmgrinfo_appinfo_h handle, void *user_data)
486 {
487         char *appid = NULL;
488         struct appinfomgr *cf = (struct appinfomgr *)user_data;
489         struct appinfo *c;
490         const char *componet;
491         int r;
492
493         pkgmgrinfo_appinfo_get_appid(handle, &appid);
494         c = g_hash_table_lookup(cf->tbl, appid);
495
496         if (c != NULL && strncmp(c->val[_AI_STATUS], "restart", 7) == 0) {
497                 __app_info_insert_handler(handle, user_data);
498                 __release_srv(appid);
499         } else {
500                 __app_info_insert_handler(handle, user_data);
501                 c = g_hash_table_lookup(cf->tbl, appid);
502
503                 componet = appinfo_get_value(c, AIT_COMPTYPE);
504                 r = appinfo_get_boolean(c, AIT_ONBOOT);
505
506                 if (r == 1 && componet && strncmp(componet, "svcapp", 6) == 0)
507                 {
508                         _D("start service - %s", appid);
509                         _start_srv(c, NULL);
510                 }
511         }
512
513         return 0;
514 }
515
516 static gboolean __amd_pkgmgrinfo_uninstall_end_handler (gpointer key, gpointer value, gpointer user_data)
517 {
518         struct appinfo *c = value;
519         char *pkgid = (char *)user_data;
520
521         if (strcmp(c->val[_AI_PKGID], pkgid) == 0) {
522                 SECURE_LOGD("appid(%s), pkgid(%s)", c->val[_AI_NAME], pkgid);
523                 return TRUE;
524         }
525         return FALSE;
526 }
527
528 static int __amd_pkgmgrinfo_status_cb(int req_id, const char *pkg_type,
529                        const char *pkgid, const char *key, const char *val,
530                        const void *pmsg, void *user_data)
531 {
532         int ret = 0;
533         pkgmgrinfo_pkginfo_h handle;
534         char *op = NULL;
535         struct pkginfo *p = NULL;
536         struct appinfomgr *cf = (struct appinfomgr *)user_data;
537
538         SECURE_LOGD("pkgid(%s), key(%s), value(%s)", pkgid, key, val);
539
540         if(strncmp(key,"start", 5) == 0) {
541                 g_hash_table_remove(pkg_tbl, pkgid);
542                 p = calloc(1, sizeof(*p));
543                 if (p == NULL) {
544                         _E("out of memory");
545                         return -1;
546                 }
547
548                 p->pkgid = strdup(pkgid);
549                 p->op = strdup(val);
550                 g_hash_table_insert(pkg_tbl, p->pkgid, p);
551                 if (strncmp(val, "install", 7) == 0) {
552
553                 }
554                 else if ((strncmp(val, "update", 6) == 0) || (strncmp(val, "uninstall", 9) == 0)) {
555                         g_hash_table_foreach(cf->tbl, __amd_pkgmgrinfo_start_handler, (gpointer)pkgid);
556                         _D("__amd_pkgmgrinfo_start_handler");
557                         ret = 0;
558                 }
559         }
560         else if (strncmp(key,"end", 3) == 0) {
561                 p = g_hash_table_lookup(pkg_tbl, pkgid);
562                 if(p) {
563                         op = p->op;
564                         SECURE_LOGD("op(%s), value(%s)", op, val);
565                         if (strncmp(val, "ok", 2) == 0) {
566                                 if (op && strncmp(op, "install", 7)== 0) {
567                                         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
568                                         if (ret != PMINFO_R_OK)
569                                                 return -1;
570                                         ret = pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, __amd_pkgmgrinfo_install_end_handler, user_data);
571                                         if (ret != PMINFO_R_OK) {
572                                                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
573                                                 return -1;
574                                         }
575                                         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
576                                 }
577                                 else if (op && strncmp(op, "update", 6) == 0){
578                                         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
579                                         if (ret != PMINFO_R_OK)
580                                                 return -1;
581                                         ret = pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, __amd_pkgmgrinfo_update_end_handler, user_data);
582                                         if (ret != PMINFO_R_OK) {
583                                                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
584                                                 return -1;
585                                         }
586                                         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
587                                 }
588                                 else if (op && strncmp(op, "uninstall", 9) == 0) {
589                                         ret = g_hash_table_foreach_remove(cf->tbl, __amd_pkgmgrinfo_uninstall_end_handler, (gpointer)pkgid);
590                                         _D("g_hash_table_foreach_remove, %d", ret);
591                                         ret = 0;
592                                 }
593                         }
594                         else if (strncmp(val, "fail", 4) == 0) {
595                                 if ((op && strncmp(op, "update", 6) == 0) || (op && strncmp(op, "uninstall", 9) ==  0) ) {
596                                         g_hash_table_foreach(cf->tbl, __amd_pkgmgrinfo_fail_handler, (gpointer)pkgid);
597                                         _D("__amd_pkgmgrinfo_fail_handler");
598                                         ret = 0;
599                                 }
600                         }
601                         g_hash_table_remove(pkg_tbl, p->pkgid);
602                         free(p->pkgid);
603                         free(p->op);
604                         free(p);
605                 }
606         }
607         return ret;
608 }
609
610 static int __unmounted_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
611 {
612         char *appid;
613         struct appinfomgr *cf = (struct appinfomgr *)user_data;
614         struct appinfo *c;
615
616         pkgmgrinfo_appinfo_get_appid(handle, &appid);
617         c = g_hash_table_lookup(cf->tbl, appid);
618         if (c == NULL)
619                 return -1;
620
621         SECURE_LOGD("%s : %s", c->val[_AI_FILE], c->val[_AI_STATUS]);
622         free(c->val[_AI_STATUS]);
623         c->val[_AI_STATUS] = strdup("unmounted");
624         SECURE_LOGD("unmounted(%s)", appid);
625
626         return 0;
627 }
628
629 static int __mounted_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
630 {
631         char *appid;
632         struct appinfomgr *cf = (struct appinfomgr *)user_data;
633         struct appinfo *c;
634
635         pkgmgrinfo_appinfo_get_appid(handle, &appid);
636         c = g_hash_table_lookup(cf->tbl, appid);
637         if (c == NULL)
638                 return -1;
639
640         SECURE_LOGD("%s : %s", c->val[_AI_FILE], c->val[_AI_STATUS]);
641         if(strncmp(c->val[_AI_STATUS], "unmounted", 9) ==0 ) {
642                 free(c->val[_AI_STATUS]);
643                 c->val[_AI_STATUS] = strdup("installed");
644         }
645         SECURE_LOGD("mounted(%s)", appid);
646
647         return 0;
648 }
649
650 static void __amd_mmc_vconf_cb(keynode_t *key, void *data)
651 {
652         int status;
653         struct appinfomgr *cf = (struct appinfomgr *)data;
654         int ret = 0;
655
656         status = vconf_keynode_get_int(key);
657         if( status < 0 ) {
658                 return;
659         }
660
661         if(status == VCONFKEY_SYSMAN_MMC_REMOVED || status == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) {
662                 ret = pkgmgrinfo_appinfo_get_unmounted_list(__unmounted_list_cb, data);
663                 if (ret != PMINFO_R_OK){
664                         _E("pkgmgrinfo_appinfo_get_unmounted_list failed: %d", ret);
665                 }
666         } else if(status == VCONFKEY_SYSMAN_MMC_MOUNTED){
667                 ret = pkgmgrinfo_appinfo_get_mounted_list(__mounted_list_cb, data);
668                 if (ret != PMINFO_R_OK){
669                         _E("pkgmgrinfo_appinfo_get_mounted_list failed: %d", ret);
670                 }
671         }
672 }
673
674 int appinfo_init(struct appinfomgr **cf)
675 {
676         struct appinfomgr *_cf;
677         int r;
678         FILE *fp = NULL;
679         char buf[4096] = {0,};
680         char *tmp = NULL;
681
682         if (!cf) {
683                 errno = EINVAL;
684                 _E("appinfo init: %s", strerror(errno));
685                 return -1;
686         }
687
688         fp = fopen("/proc/cmdline", "r");
689         if (fp == NULL){
690                 _E("appinfo init failed: %s", strerror(errno));
691                 return -1;
692         }
693         r = fgets(buf, sizeof(buf), fp);
694         tmp = strstr(buf, "gles");
695         if(tmp != NULL) {
696                 sscanf(tmp,"gles=%d", &gles);
697         }
698         fclose(fp);
699
700         _cf = _init();
701         if (!_cf) {
702                 assert(_cf);
703                 return -1;
704         }
705
706         r = _read_pkg_info(_cf);
707         if (r != PMINFO_R_OK) {
708                 _fini(_cf);
709                 return -1;
710         }
711
712         r = vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, __amd_mmc_vconf_cb, _cf);
713         if (r < 0)
714                 _E("Unable to register vconf notification callback for VCONFKEY_SYSMAN_MMC_STATUS\n");
715
716         int event_type = PMINFO_CLIENT_STATUS_UPGRADE | PMINFO_CLIENT_STATUS_UNINSTALL | PMINFO_CLIENT_STATUS_INSTALL;
717         pkgmgrinfo_client *pc = NULL;
718         pc = pkgmgrinfo_client_new(PMINFO_LISTENING);
719         pkgmgrinfo_client_set_status_type(pc, event_type);
720         pkgmgrinfo_client_listen_status(pc, __amd_pkgmgrinfo_status_cb , _cf);
721
722         *cf = _cf;
723
724         return 0;
725 }
726
727 void appinfo_fini(struct appinfomgr **cf)
728 {
729         if (!cf || !*cf)
730                 return;
731
732         _fini(*cf);
733         *cf = NULL;
734 }
735
736 const struct appinfo *appinfo_insert(struct appinfomgr *cf, const char *pkg_name)
737 {
738         pkgmgrinfo_pkginfo_h handle;
739         if (pkgmgrinfo_pkginfo_get_pkginfo(pkg_name, &handle) == PMINFO_R_OK){
740                 pkgmgrinfo_appinfo_get_list(handle, PMINFO_SVC_APP, __app_info_insert_handler, cf);
741                 pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, __app_info_insert_handler, cf);
742                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
743         }
744         return cf;
745 }
746
747 void appinfo_delete(struct appinfomgr *cf, const char *pkg_name)
748 {
749         pkgmgrinfo_pkginfo_h handle;
750         if (pkgmgrinfo_pkginfo_get_pkginfo(pkg_name, &handle) != PMINFO_R_OK)
751                 return;
752         pkgmgrinfo_appinfo_get_list(handle, PMINFO_SVC_APP, __app_info_delete_handler, cf);
753         pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, __app_info_delete_handler, cf);
754         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
755 }
756
757 const struct appinfo *appinfo_find(struct appinfomgr *cf, const char *filename)
758 {
759         if (!cf || !filename || !*filename) {
760                 errno = EINVAL;
761                 return NULL;
762         }
763
764         return g_hash_table_lookup(cf->tbl, FILENAME(filename));
765 }
766
767 const char *appinfo_get_value(const struct appinfo *c, enum appinfo_type type)
768 {
769         enum _appinfo_idx i;
770
771         if (!c) {
772                 errno = EINVAL;
773                 _E("appinfo get value: %s, %d", strerror(errno), type);
774                 return NULL;
775         }
776
777         for (i = _AI_START; i < sizeof(_appinfos)/sizeof(_appinfos[0]); i++) {
778                 if (type == _appinfos[i].type)
779                         return c->val[i];
780         }
781
782         errno = ENOENT;
783         _E("appinfo get value: %s", strerror(errno));
784
785         return NULL;
786 }
787
788 const char *appinfo_set_value(struct appinfo *c, enum appinfo_type type, const char* val)
789 {
790         enum _appinfo_idx i;
791         for (i = _AI_START; i < sizeof(_appinfos)/sizeof(_appinfos[0]); i++) {
792                 if (type == _appinfos[i].type) {
793                         SECURE_LOGD("%s : %s : %s", c->val[_AI_FILE], c->val[i], val);
794                         free(c->val[i]);
795                         c->val[i] = strdup(val);
796                 }
797         }
798
799         return NULL;
800 }
801
802 const char *appinfo_get_filename(const struct appinfo *c)
803 {
804         if (!c) {
805                 errno = EINVAL;
806                 SECURE_LOGE("appinfo get filename: %s", strerror(errno));
807                 return NULL;
808         }
809
810         return c->val[_AI_FILE];
811 }
812
813 struct _cbinfo {
814         appinfo_iter_callback cb;
815         void *cb_data;
816 };
817
818 static void _iter_cb(gpointer key, gpointer value, gpointer user_data)
819 {
820         struct _cbinfo *cbi = user_data;
821
822         assert(cbi);
823
824         cbi->cb(cbi->cb_data, key, value);
825 }
826
827 void appinfo_foreach(struct appinfomgr *cf, appinfo_iter_callback cb, void *user_data)
828 {
829         struct _cbinfo cbi;
830
831         if (!cf || !cb) {
832                 errno = EINVAL;
833                 _E("appinfo foreach: %s", strerror(errno));
834                 return;
835         }
836
837         cbi.cb = cb;
838         cbi.cb_data = user_data;
839
840         g_hash_table_foreach(cf->tbl, _iter_cb, &cbi);
841 }
842
843 int appinfo_get_boolean(const struct appinfo *c, enum appinfo_type type)
844 {
845         const char *v;
846
847         v = appinfo_get_value(c, type);
848         if (!v)
849                 return -1;
850
851         if (!strcmp(v, "1") || !strcasecmp(v, "true"))
852                 return 1;
853
854         if (!strcmp(v, "0") || !strcasecmp(v, "false"))
855                 return 0;
856
857         errno = EFAULT;
858
859         return -1;
860 }
861