Fix crash when sim card hotplug
[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_MAX,
33 };
34 #define _AI_START _AI_NAME /* start index */
35
36 struct appinfo_t {
37         char *name;
38         enum appinfo_type type;
39 };
40
41 static struct appinfo_t _appinfos[] = {
42         [_AI_NAME] = { "Name", AIT_NAME, },
43         [_AI_COMP] = { "Component", AIT_COMP, },
44         [_AI_EXEC] = { "Exec", AIT_EXEC, },
45         [_AI_TYPE] = { "PkgType", AIT_TYPE, },
46         [_AI_ONBOOT] = { "StartOnBoot", AIT_ONBOOT, },
47         [_AI_RESTART] = { "AutoRestart", AIT_RESTART, },
48         [_AI_MULTI] = { "Multiple", AIT_MULTI, },
49         [_AI_HWACC] = { "Hwacceleration", AIT_HWACC, },
50 };
51
52 struct appinfo {
53         char *val[_AI_MAX];
54 };
55
56 static void _free_appinfo(gpointer data)
57 {
58         struct appinfo *c = data;
59         int i;
60
61         if (!c)
62                 return;
63
64         for (i = 0; i < sizeof(c->val)/sizeof(c->val[0]); i++)
65                 free(c->val[i]);
66
67         free(c);
68 }
69
70 static void _fini(struct appinfomgr *cf)
71 {
72         assert(cf);
73
74         g_hash_table_destroy(cf->tbl);
75         free(cf);
76 }
77
78 static int __app_info_insert_handler (const pkgmgrinfo_appinfo_h handle, void *data)
79 {
80         struct appinfo *c;
81         struct appinfomgr *cf = (struct appinfomgr *)data;
82         gboolean r;
83         char *exec;
84         char *type;
85         char *appid;
86         bool multiple;
87         bool onboot;
88         bool restart;
89         pkgmgrinfo_app_hwacceleration hwacc;
90         pkgmgrinfo_app_component component;
91
92         if (!handle) {
93                 _E("null app handle");
94                 return -1;
95         }
96
97         pkgmgrinfo_appinfo_get_appid(handle, &appid);
98
99         g_hash_table_remove(cf->tbl, appid);
100
101         c = calloc(1, sizeof(*c));
102         if (!c) {
103                 _E("create appinfo: %s", strerror(errno));
104                 return -1;
105         }
106
107         memset(c, 0, sizeof(struct appinfo));
108
109         c->val[_AI_FILE] = strdup(appid);
110         if (!c->val[_AI_FILE]) {
111                 _E("create appinfo: %s", strerror(errno));
112                 _free_appinfo(c);
113                 return -1;
114         }
115
116         c->val[_AI_NAME] = strdup(appid); //TODO :
117
118         pkgmgrinfo_appinfo_get_component(handle, &component);
119         if(component == PMINFO_UI_APP) {
120                 c->val[_AI_COMP] = strdup("ui"); //TODO :
121
122                 r = pkgmgrinfo_appinfo_is_multiple(handle, &multiple);
123                 if(multiple == true)
124                         c->val[_AI_MULTI] = strdup("true");
125                 else c->val[_AI_MULTI] = strdup("false");
126
127                 r = pkgmgrinfo_appinfo_get_hwacceleration(handle, &hwacc);
128                 if (hwacc == PMINFO_HWACCELERATION_USE_GL) {
129                         c->val[_AI_HWACC] = strdup("USE");
130                 } else if (hwacc == PMINFO_HWACCELERATION_USE_SYSTEM_SETTING) {
131                         c->val[_AI_HWACC] = strdup("SYS");
132                 } else {
133                         c->val[_AI_HWACC] = strdup("NOT_USE");
134                 }
135         } else {
136                 c->val[_AI_COMP] = strdup("svc");
137
138                 r = pkgmgrinfo_appinfo_is_onboot(handle, &onboot);
139                 if(onboot == true)
140                         c->val[_AI_ONBOOT] = strdup("true");
141                 else c->val[_AI_ONBOOT] = strdup("false");
142
143                 r = pkgmgrinfo_appinfo_is_autorestart(handle, &restart);
144                 if(restart == true)
145                         c->val[_AI_RESTART] = strdup("true");
146                 else c->val[_AI_RESTART] = strdup("false");
147         }
148
149         r = pkgmgrinfo_appinfo_get_exec(handle, &exec);
150         c->val[_AI_EXEC] = strdup(exec);
151
152         r = pkgmgrinfo_appinfo_get_apptype(handle, &type);
153         if(strncmp(type, "capp", 4) == 0 ) {
154                 c->val[_AI_TYPE] = strdup("rpm");
155         } else if (strncmp(type, "c++app", 6) == 0 || strncmp(type, "ospapp", 6) == 0) {
156                 c->val[_AI_TYPE] = strdup("tpk");
157         } else if (strncmp(type, "webapp", 6) == 0) {
158                 c->val[_AI_TYPE] = strdup("wgt");
159         }
160
161         _D("%s : %s : %s", c->val[_AI_FILE], c->val[_AI_COMP], c->val[_AI_TYPE]);
162
163         g_hash_table_insert(cf->tbl, c->val[_AI_FILE], c);
164
165         return 0;
166 }
167
168 static int __app_info_delete_handler (const pkgmgrinfo_appinfo_h handle, void *data)
169 {
170         struct appinfomgr *cf = (struct appinfomgr *)data;
171         char *appid;
172
173         pkgmgrinfo_appinfo_get_appid(handle, &appid);
174
175         g_hash_table_remove(cf->tbl, appid);
176
177         return 0;
178 }
179
180 static int _read_pkg_info(struct appinfomgr *cf)
181 {
182         int r;
183
184         r = pkgmgrinfo_appinfo_get_installed_list(__app_info_insert_handler, cf);
185
186         return 0;
187 }
188
189 static struct appinfomgr *_init()
190 {
191         struct appinfomgr *cf;
192
193         cf = calloc(1, sizeof(*cf));
194         if (!cf) {
195                 _E("appinfo init: %s", strerror(errno));
196                 return NULL;
197         }
198
199         cf->tbl = g_hash_table_new_full(g_str_hash, g_str_equal,
200                         NULL, _free_appinfo);
201
202         return cf;
203 }
204
205 static void __vconf_cb(keynode_t *key, void *data)
206 {
207         char *noti_string;
208         char *type_string;
209         char *appid;
210         char *saveptr;
211         char *pkgname;
212         pkgmgrinfo_appinfo_h handle;
213         struct appinfomgr *cf = (struct appinfomgr *)data;
214
215         noti_string = vconf_keynode_get_str(key);
216         if( noti_string == NULL ) {
217                 return;
218         }
219
220         _D("noti_string : %s",noti_string);
221
222         type_string = strtok_r(noti_string, ":", &saveptr);
223         appid = strtok_r(NULL, ":", &saveptr);
224
225         if ( strncmp(type_string, "create", 6) == 0) {
226                 pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
227
228                 _D("appid : %s /handle : %x", appid, handle);
229
230                 __app_info_insert_handler(handle, data);
231
232                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
233         } else if ( strncmp(type_string, "delete", 6) == 0) {
234                 g_hash_table_remove(cf->tbl, appid);
235         }
236 }
237
238 int appinfo_init(struct appinfomgr **cf)
239 {
240         struct appinfomgr *_cf;
241         int r;
242
243         if (!cf) {
244                 errno = EINVAL;
245                 _E("appinfo init: %s", strerror(errno));
246                 return -1;
247         }
248
249         _cf = _init();
250         if (!_cf)
251                 return -1;
252
253         r = _read_pkg_info(_cf);
254         if (r == -1) {
255                 _fini(_cf);
256                 return -1;
257         }
258
259         r = vconf_notify_key_changed("memory/menuscreen/desktop", __vconf_cb, _cf);
260
261         *cf = _cf;
262
263         return 0;
264 }
265
266 void appinfo_fini(struct appinfomgr **cf)
267 {
268         if (!cf || !*cf)
269                 return;
270
271         _fini(*cf);
272         *cf = NULL;
273 }
274
275 const struct appinfo *appinfo_insert(struct appinfomgr *cf, const char *pkg_name)
276 {
277         int r;
278         pkgmgrinfo_pkginfo_h handle;
279
280         r = pkgmgrinfo_pkginfo_get_pkginfo(pkg_name, &handle);
281         r = pkgmgrinfo_appinfo_get_list(handle, PMINFO_SVC_APP, __app_info_insert_handler, cf);
282         r = pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, __app_info_insert_handler, cf);
283         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
284
285         return cf;
286 }
287
288 void appinfo_delete(struct appinfomgr *cf, const char *pkg_name)
289 {
290         int r;
291         pkgmgrinfo_pkginfo_h handle;
292
293         r = pkgmgrinfo_pkginfo_get_pkginfo(pkg_name, &handle);
294         r = pkgmgrinfo_appinfo_get_list(handle, PMINFO_SVC_APP, __app_info_delete_handler, cf);
295         r = pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, __app_info_delete_handler, cf);
296         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
297 }
298
299 const struct appinfo *appinfo_find(struct appinfomgr *cf, const char *filename)
300 {
301         if (!cf || !filename || !*filename) {
302                 errno = EINVAL;
303                 _E("appinfo find: %s", strerror(errno));
304                 return NULL;
305         }
306
307         return g_hash_table_lookup(cf->tbl, FILENAME(filename));
308 }
309
310 const char *appinfo_get_value(const struct appinfo *c, enum appinfo_type type)
311 {
312         enum _appinfo_idx i;
313
314         if (!c) {
315                 errno = EINVAL;
316                 _E("appinfo get value: %s", strerror(errno));
317                 return NULL;
318         }
319
320         for (i = _AI_START; i < sizeof(_appinfos)/sizeof(_appinfos[0]); i++) {
321                 if (type == _appinfos[i].type)
322                         return c->val[i];
323         }
324
325         errno = ENOENT;
326         _E("appinfo get value: %s", strerror(errno));
327
328         return NULL;
329 }
330
331 const char *appinfo_get_filename(const struct appinfo *c)
332 {
333         if (!c) {
334                 errno = EINVAL;
335                 _E("appinfo get filename: %s", strerror(errno));
336                 return NULL;
337         }
338
339         return c->val[_AI_FILE];
340 }
341
342 struct _cbinfo {
343         appinfo_iter_callback cb;
344         void *cb_data;
345 };
346
347 static void _iter_cb(gpointer key, gpointer value, gpointer user_data)
348 {
349         struct _cbinfo *cbi = user_data;
350
351         assert(cbi);
352
353         cbi->cb(cbi->cb_data, key, value);
354 }
355
356 void appinfo_foreach(struct appinfomgr *cf, appinfo_iter_callback cb, void *user_data)
357 {
358         struct _cbinfo cbi;
359
360         if (!cf || !cb) {
361                 errno = EINVAL;
362                 _E("appinfo foreach: %s", strerror(errno));
363                 return;
364         }
365
366         cbi.cb = cb;
367         cbi.cb_data = user_data;
368
369         g_hash_table_foreach(cf->tbl, _iter_cb, &cbi);
370 }
371
372 int appinfo_get_boolean(const struct appinfo *c, enum appinfo_type type)
373 {
374         const char *v;
375
376         v = appinfo_get_value(c, type);
377         if (!v)
378                 return -1;
379
380         if (!strcmp(v, "1") || !strcasecmp(v, "true"))
381                 return 1;
382
383         if (!strcmp(v, "0") || !strcasecmp(v, "false"))
384                 return 0;
385
386         errno = EFAULT;
387
388         return -1;
389 }
390