tizen 2.3.1 release
[framework/appfw/pkgmgr-info.git] / src / pkgmgrinfo_feature.c
1 /*
2  * pkgmgrinfo-feature
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Junsuk Oh <junsuk77.oh@samsung.com>,
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include "pkgmgrinfo_private.h"
23
24 #ifdef LOG_TAG
25 #undef LOG_TAG
26 #endif
27 #define LOG_TAG "PKGMGR_INFO"
28
29 typedef int (*pkgmgr_handler)(int req_id, const char *pkg_type,
30                                 const char *pkgid, const char *key,
31                                 const char *val, const void *pmsg, void *data);
32
33 typedef void pkgmgr_client;
34 typedef void pkgmgr_info;
35
36 typedef enum {
37         PM_REQUEST_CSC = 0,
38         PM_REQUEST_MOVE = 1,
39         PM_REQUEST_GET_SIZE = 2,
40         PM_REQUEST_KILL_APP = 3,
41         PM_REQUEST_CHECK_APP = 4,
42         PM_REQUEST_MAX
43 }pkgmgr_request_service_type;
44
45 typedef enum {
46         PM_GET_TOTAL_SIZE= 0,
47         PM_GET_DATA_SIZE = 1,
48         PM_GET_ALL_PKGS = 2,
49         PM_GET_SIZE_INFO = 3,
50         PM_GET_TOTAL_AND_DATA = 4,
51         PM_GET_SIZE_FILE = 5,
52         PM_GET_MAX
53 }pkgmgr_getsize_type;
54
55 typedef enum {
56         PC_REQUEST = 0,
57         PC_LISTENING,
58         PC_BROADCAST,
59 }client_type;
60
61 static int __pkg_list_cb(void *data, int ncols, char **coltxt, char **colname)
62 {
63         pkgmgr_pkginfo_x *udata = (pkgmgr_pkginfo_x *)data;
64         int i = 0;
65         pkgmgr_pkginfo_x *info = NULL;
66         info = calloc(1, sizeof(pkgmgr_pkginfo_x));
67         retvm_if(info == NULL, PMINFO_R_ERROR, "our of memory");
68         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
69         if (info->manifest_info == NULL) {
70                 _LOGE("out of memory");
71                 free(info);
72                 return PMINFO_R_ERROR;
73         }
74
75         LISTADD(udata, info);
76         for(i = 0; i < ncols; i++)
77         {
78                 if (strcmp(colname[i], "package") == 0) {
79                         if (coltxt[i])
80                                 info->manifest_info->package = strdup(coltxt[i]);
81                         else
82                                 info->manifest_info->package = NULL;
83                 } else
84                         continue;
85         }
86
87         return 0;
88 }
89
90 API int pkgmgrinfo_pkginfo_get_disabled_pkginfo(const char *pkgid, pkgmgrinfo_pkginfo_h *handle)
91 {
92         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
93         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
94         pkgmgr_pkginfo_x *pkginfo = NULL;
95         int ret = PMINFO_R_OK;
96         char *query = NULL;
97         char *locale = NULL;
98         label_x *tmp1 = NULL;
99         icon_x *tmp2 = NULL;
100         description_x *tmp3 = NULL;
101         author_x *tmp4 = NULL;
102         privilege_x *tmp5 = NULL;
103         sqlite3 *pkginfo_db = NULL;
104
105         /*validate pkgid*/
106         ret = db_util_open(MANIFEST_DB, &pkginfo_db, 0);
107         retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
108
109         /*get system locale*/
110         locale = __convert_system_locale_to_manifest_locale();
111         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL");
112
113         pkginfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
114         tryvm_if(pkginfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for pkginfo");
115
116         pkginfo->locale = strdup(locale);
117
118         pkginfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
119         tryvm_if(pkginfo->manifest_info == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for manifest info");
120
121         pkginfo->manifest_info->package = strdup(pkgid);
122         pkginfo->manifest_info->privileges = (privileges_x *)calloc(1, sizeof(privileges_x));
123         tryvm_if(pkginfo->manifest_info->privileges == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for privileges info");
124
125         /*populate manifest_info from DB*/
126         query= sqlite3_mprintf("select * from disabled_package_info where package=%Q", pkgid);
127         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
128         sqlite3_free(query);
129         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
130
131         query= sqlite3_mprintf("select * from disabled_package_localized_info where package=%Q and package_locale=%Q", pkgid, locale);
132         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
133         sqlite3_free(query);
134         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
135
136         /*Also store the values corresponding to default locales*/
137         query= sqlite3_mprintf("select * from disabled_package_localized_info where package=%Q and package_locale=%Q", pkgid, DEFAULT_LOCALE);
138         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
139         sqlite3_free(query);
140         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
141
142         SAFE_LISTHEAD(pkginfo->manifest_info->label, tmp1);
143         SAFE_LISTHEAD(pkginfo->manifest_info->icon, tmp2);
144         SAFE_LISTHEAD(pkginfo->manifest_info->description, tmp3);
145         SAFE_LISTHEAD(pkginfo->manifest_info->author, tmp4);
146         SAFE_LISTHEAD(pkginfo->manifest_info->privileges->privilege, tmp5);
147
148 catch:
149         if (ret == PMINFO_R_OK)
150                 *handle = (void*)pkginfo;
151         else {
152                 *handle = NULL;
153                 __cleanup_pkginfo(pkginfo);
154         }
155         sqlite3_close(pkginfo_db);
156
157         if (locale) {
158                 free(locale);
159                 locale = NULL;
160         }
161         return ret;
162 }
163
164 API int pkgmgrinfo_pkginfo_get_disabled_list(pkgmgrinfo_pkg_list_cb pkg_list_cb, void *user_data)
165 {
166         retvm_if(pkg_list_cb == NULL, PMINFO_R_EINVAL, "callback function is NULL\n");
167         int ret = PMINFO_R_OK;
168         char query[MAX_QUERY_LEN] = {'\0'};
169         char *locale = NULL;
170         pkgmgr_pkginfo_x *pkginfo = NULL;
171         label_x *tmp1 = NULL;
172         icon_x *tmp2 = NULL;
173         description_x *tmp3 = NULL;
174         author_x *tmp4 = NULL;
175         sqlite3 *pkginfo_db = NULL;
176         pkgmgr_pkginfo_x *tmphead = NULL;
177         pkgmgr_pkginfo_x *node = NULL;
178         pkgmgr_pkginfo_x *temp_node = NULL;
179
180         /*open db*/
181         ret = db_util_open(MANIFEST_DB, &pkginfo_db, 0);
182         retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
183
184         /*get system locale*/
185         locale = __convert_system_locale_to_manifest_locale();
186         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL");
187
188         tmphead = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
189         if(tmphead == NULL){
190                 _LOGE("@calloc failed!!");
191                 FREE_AND_NULL(locale);
192                 return PMINFO_R_ERROR;
193         }
194
195         snprintf(query, MAX_QUERY_LEN, "select * from disabled_package_info");
196         ret = __exec_db_query(pkginfo_db, query, __pkg_list_cb, (void *)tmphead);
197         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
198
199         LISTHEAD(tmphead, node);
200
201         for(node = node->next; node ; node = node->next) {
202                 pkginfo = node;
203                 pkginfo->locale = strdup(locale);
204                 pkginfo->manifest_info->privileges = (privileges_x *)calloc(1, sizeof(privileges_x));
205                 tryvm_if(pkginfo->manifest_info->privileges == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for privileges info\n");
206
207                 /*populate manifest_info from DB*/
208                 snprintf(query, MAX_QUERY_LEN, "select * from disabled_package_info where package='%s' ", pkginfo->manifest_info->package);
209                 ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
210                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
211
212                 memset(query, '\0', MAX_QUERY_LEN);
213                 snprintf(query, MAX_QUERY_LEN, "select * from disabled_package_localized_info where" \
214                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, locale);
215                 ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
216                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
217
218                 /*Also store the values corresponding to default locales*/
219                 memset(query, '\0', MAX_QUERY_LEN);
220                 snprintf(query, MAX_QUERY_LEN, "select * from disabled_package_localized_info where" \
221                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, DEFAULT_LOCALE);
222                 ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
223                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
224
225                 SAFE_LISTHEAD(pkginfo->manifest_info->label, tmp1);
226                 SAFE_LISTHEAD(pkginfo->manifest_info->icon, tmp2);
227                 SAFE_LISTHEAD(pkginfo->manifest_info->description, tmp3);
228                 SAFE_LISTHEAD(pkginfo->manifest_info->author, tmp4);
229         }
230
231         LISTHEAD(tmphead, node);
232
233         for(node = node->next; node ; node = node->next) {
234                 pkginfo = node;
235
236                 ret = pkg_list_cb( (void *)pkginfo, user_data);
237                 if(ret < 0)
238                         break;
239         }
240
241         ret = PMINFO_R_OK;
242
243 catch:
244         sqlite3_close(pkginfo_db);
245         if (locale) {
246                 free(locale);
247                 locale = NULL;
248         }
249         if (tmphead) {
250                 LISTHEAD(tmphead, node);
251                 temp_node = node->next;
252                 node = temp_node;
253                 while (node) {
254                         temp_node = node->next;
255                         __cleanup_pkginfo(node);
256                         node = temp_node;
257                 }
258                 __cleanup_pkginfo(tmphead);
259         }
260         return ret;
261 }
262
263 API int pkgmgrinfo_appinfo_get_disabled_appinfo(const char *appid, pkgmgrinfo_appinfo_h *handle)
264 {
265         retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL");
266         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
267
268         pkgmgr_appinfo_x *appinfo = NULL;
269         char *locale = NULL;
270         int ret = -1;
271         int exist = 0;
272         label_x *tmp1 = NULL;
273         icon_x *tmp2 = NULL;
274         category_x *tmp3 = NULL;
275         metadata_x *tmp4 = NULL;
276
277         char *query = NULL;
278         sqlite3 *appinfo_db = NULL;
279
280         /*open db*/
281         ret = db_util_open(MANIFEST_DB, &appinfo_db, 0);
282         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
283
284         /*check appid exist on db*/
285         query = sqlite3_mprintf("select exists(select * from disabled_package_app_info where app_id=%Q)", appid);
286         ret = __exec_db_query(appinfo_db, query, _pkgmgrinfo_validate_cb, (void *)&exist);
287         sqlite3_free(query);
288         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "sqlite3_exec fail");
289         tryvm_if(exist == 0, ret = PMINFO_R_ERROR, "Appid[%s] not found in DB", appid);
290
291         /*get system locale*/
292         locale = __convert_system_locale_to_manifest_locale();
293         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL");
294
295         /*calloc appinfo*/
296         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
297         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for appinfo");
298
299         /*calloc app_component*/
300         appinfo->uiapp_info = (uiapplication_x *)calloc(1, sizeof(uiapplication_x));
301         tryvm_if(appinfo->uiapp_info == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for uiapp info");
302
303         appinfo->locale = strdup(locale);
304
305         /*populate app_info from DB*/
306         query = sqlite3_mprintf("select * from disabled_package_app_info where app_id=%Q ", appid);
307         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
308         sqlite3_free(query);
309         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
310
311         query = sqlite3_mprintf("select * from disabled_package_app_localized_info where app_id=%Q and app_locale=%Q", appid, locale);
312         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
313         sqlite3_free(query);
314         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
315
316         /*Also store the values corresponding to default locales*/
317         query = sqlite3_mprintf("select * from disabled_package_app_localized_info where app_id=%Q and app_locale=%Q", appid, DEFAULT_LOCALE);
318         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
319         sqlite3_free(query);
320         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
321
322         /*Populate app category*/
323         query = sqlite3_mprintf("select * from disabled_package_app_app_category where app_id=%Q", appid);
324         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
325         sqlite3_free(query);
326         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Category Info DB Information retrieval failed");
327
328         /*Populate app metadata*/
329         query = sqlite3_mprintf("select * from disabled_package_app_app_metadata where app_id=%Q", appid);
330         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
331         sqlite3_free(query);
332         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Metadata Info DB Information retrieval failed");
333
334         SAFE_LISTHEAD(appinfo->uiapp_info->label, tmp1);
335         SAFE_LISTHEAD(appinfo->uiapp_info->icon, tmp2);
336         SAFE_LISTHEAD(appinfo->uiapp_info->category, tmp3);
337         SAFE_LISTHEAD(appinfo->uiapp_info->metadata, tmp4);
338
339         ret = PMINFO_R_OK;
340
341 catch:
342         if (ret == PMINFO_R_OK)
343                 *handle = (void*)appinfo;
344         else {
345                 *handle = NULL;
346                 __cleanup_appinfo(appinfo);
347         }
348
349         sqlite3_close(appinfo_db);
350         if (locale) {
351                 free(locale);
352                 locale = NULL;
353         }
354         return ret;
355 }
356
357 API int pkgmgrinfo_appinfo_get_disabled_list(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_app_component component,
358                                                 pkgmgrinfo_app_list_cb app_func, void *user_data)
359 {
360         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
361         retvm_if(app_func == NULL, PMINFO_R_EINVAL, "callback pointer is NULL");
362         retvm_if((component != PMINFO_UI_APP) && (component != PMINFO_SVC_APP) && (component != PMINFO_ALL_APP), PMINFO_R_EINVAL, "Invalid App Component Type");
363
364         char *locale = NULL;
365         int ret = -1;
366         char query[MAX_QUERY_LEN] = {'\0'};
367         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
368         pkgmgr_pkginfo_x *allinfo = NULL;
369         pkgmgr_appinfo_x *appinfo = NULL;
370         icon_x *ptr1 = NULL;
371         label_x *ptr2 = NULL;
372         category_x *ptr3 = NULL;
373         metadata_x *ptr4 = NULL;
374         permission_x *ptr5 = NULL;
375         image_x *ptr6 = NULL;
376         sqlite3 *appinfo_db = NULL;
377
378         /*check installed storage*/
379         ret = __pkginfo_check_installed_storage(info);
380         retvm_if(ret < 0, PMINFO_R_EINVAL, "[%s] is installed external, but is not in mmc", info->manifest_info->package);
381
382         /*get system locale*/
383         locale = __convert_system_locale_to_manifest_locale();
384         tryvm_if(locale == NULL, ret = PMINFO_R_EINVAL, "manifest locale is NULL");
385
386         /*calloc allinfo*/
387         allinfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
388         tryvm_if(allinfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for appinfo");
389
390         /*calloc manifest_info*/
391         allinfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
392         tryvm_if(allinfo->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
393
394         /*calloc appinfo*/
395         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
396         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for appinfo");
397
398         /*open db */
399         ret = db_util_open(MANIFEST_DB, &appinfo_db, 0);
400         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
401
402         snprintf(query, MAX_QUERY_LEN, "select DISTINCT * from disabled_package_app_info where package='%s'", info->manifest_info->package);
403
404                 /*Populate ui app info */
405                 ret = __exec_db_query(appinfo_db, query, __uiapp_list_cb, (void *)info);
406                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info list retrieval failed");
407
408                 uiapplication_x *tmp = NULL;
409                 SAFE_LISTHEAD(info->manifest_info->uiapplication, tmp);
410
411                 /*Populate localized info for default locales and call callback*/
412                 /*If the callback func return < 0 we break and no more call back is called*/
413                 while(tmp != NULL)
414                 {
415                         if (locale)
416                                 appinfo->locale = strdup(locale);
417                         appinfo->uiapp_info = tmp;
418                         if (strcmp(appinfo->uiapp_info->type,"c++app") == 0){
419                                 if (locale) {
420                                         free(locale);
421                                 }
422                                 locale = __get_app_locale_by_fallback(appinfo_db, appinfo->uiapp_info->appid);
423                         }
424
425                         memset(query, '\0', MAX_QUERY_LEN);
426                         snprintf(query, MAX_QUERY_LEN, "select * from disabled_package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->uiapp_info->appid, locale);
427                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
428                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
429
430                         memset(query, '\0', MAX_QUERY_LEN);
431                         snprintf(query, MAX_QUERY_LEN, "select * from disabled_package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->uiapp_info->appid, DEFAULT_LOCALE);
432                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
433                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
434
435                         /*Populate app category*/
436                         memset(query, '\0', MAX_QUERY_LEN);
437                         snprintf(query, MAX_QUERY_LEN, "select * from disabled_package_app_app_category where app_id=='%s'", appinfo->uiapp_info->appid);
438                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
439                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Category Info DB Information retrieval failed");
440
441                         /*Populate app metadata*/
442                         memset(query, '\0', MAX_QUERY_LEN);
443                         snprintf(query, MAX_QUERY_LEN, "select * from disabled_package_app_app_metadata where app_id='%s'", appinfo->uiapp_info->appid);
444                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
445                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Metadata Info DB Information retrieval failed");
446
447                         SAFE_LISTHEAD(appinfo->uiapp_info->icon, ptr1);
448                         SAFE_LISTHEAD(appinfo->uiapp_info->label, ptr2);
449                         SAFE_LISTHEAD(appinfo->uiapp_info->category, ptr3);
450                         SAFE_LISTHEAD(appinfo->uiapp_info->metadata, ptr4);
451                         SAFE_LISTHEAD(appinfo->uiapp_info->permission, ptr5);
452                         SAFE_LISTHEAD(appinfo->uiapp_info->image, ptr6);
453
454                         ret = app_func((void *)appinfo, user_data);
455                         if (ret < 0)
456                                 break;
457                         tmp = tmp->next;
458                 }
459
460         ret = PMINFO_R_OK;
461 catch:
462         if (locale) {
463                 free(locale);
464                 locale = NULL;
465         }
466         if (appinfo) {
467                 free(appinfo);
468                 appinfo = NULL;
469         }
470         __cleanup_pkginfo(allinfo);
471
472         sqlite3_close(appinfo_db);
473         return ret;
474 }