tizen 2.3.1 release
[framework/appfw/pkgmgr-info.git] / src / pkgmgrinfo_pkginfo.c
1 /*
2  * pkgmgr-info
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include "pkgmgrinfo_private.h"
24
25 #ifdef LOG_TAG
26 #undef LOG_TAG
27 #endif
28 #define LOG_TAG "PKGMGR_INFO"
29
30 #define FILTER_QUERY_LIST_PACKAGE       "select * from package_info LEFT OUTER JOIN package_localized_info " \
31                                 "ON package_info.package=package_localized_info.package " \
32                                 "and package_localized_info.package_locale IN ('%s', '%s') where "
33
34
35 typedef struct _pkgmgr_cert_x {
36         char *pkgid;
37         int cert_id;
38 } pkgmgr_cert_x;
39
40 static void __destroy_each_node(gpointer data, gpointer user_data)
41 {
42         ret_if(data == NULL);
43         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)data;
44         FREE_AND_NULL(node->value);
45         FREE_AND_NULL(node->key);
46         FREE_AND_NULL(node);
47 }
48
49 static void __get_pkginfo_from_db(char *colname, char *coltxt, manifest_x *manifest_info)
50 {
51         if (colname == NULL || manifest_info == NULL)
52                 return;
53
54         if (strcmp(colname, "package") == 0) {
55                 if (manifest_info->package)
56                         return;
57
58                 if (coltxt)
59                         manifest_info->package = strdup(coltxt);
60         } else if (strcmp(colname, "package_type") == 0) {
61                 if (coltxt)
62                         manifest_info->type = strdup(coltxt);
63                 else
64                         manifest_info->type = NULL;
65         } else if (strcmp(colname, "package_version") == 0) {
66                 if (coltxt)
67                         manifest_info->version = strdup(coltxt);
68                 else
69                         manifest_info->version = NULL;
70         } else if (strcmp(colname, "install_location") == 0) {
71                 if (coltxt)
72                         manifest_info->installlocation = strdup(coltxt);
73                 else
74                         manifest_info->installlocation = NULL;
75         } else if (strcmp(colname, "package_size") == 0) {
76                 if (coltxt)
77                         manifest_info->package_size = strdup(coltxt);
78                 else
79                         manifest_info->package_size = NULL;
80         } else if (strcmp(colname, "package_removable") == 0 ){
81                 if (coltxt)
82                         manifest_info->removable = strdup(coltxt);
83                 else
84                         manifest_info->removable = NULL;
85         } else if (strcmp(colname, "package_preload") == 0 ){
86                 if (coltxt)
87                         manifest_info->preload = strdup(coltxt);
88                 else
89                         manifest_info->preload = NULL;
90         } else if (strcmp(colname, "package_readonly") == 0 ){
91                 if (coltxt)
92                         manifest_info->readonly = strdup(coltxt);
93                 else
94                         manifest_info->readonly = NULL;
95         } else if (strcmp(colname, "package_update") == 0 ){
96                 if (coltxt)
97                         manifest_info->update= strdup(coltxt);
98                 else
99                         manifest_info->update = NULL;
100         } else if (strcmp(colname, "package_system") == 0 ){
101                 if (coltxt)
102                         manifest_info->system= strdup(coltxt);
103                 else
104                         manifest_info->system = NULL;
105         } else if (strcmp(colname, "package_appsetting") == 0 ){
106                 if (coltxt)
107                         manifest_info->appsetting = strdup(coltxt);
108                 else
109                         manifest_info->appsetting = NULL;
110         } else if (strcmp(colname, "installed_time") == 0 ){
111                 if (coltxt)
112                         manifest_info->installed_time = strdup(coltxt);
113                 else
114                         manifest_info->installed_time = NULL;
115         } else if (strcmp(colname, "installed_storage") == 0 ){
116                 if (coltxt)
117                         manifest_info->installed_storage = strdup(coltxt);
118                 else
119                         manifest_info->installed_storage = NULL;
120         } else if (strcmp(colname, "mainapp_id") == 0 ){
121                 if (coltxt)
122                         manifest_info->mainapp_id = strdup(coltxt);
123                 else
124                         manifest_info->mainapp_id = NULL;
125         } else if (strcmp(colname, "storeclient_id") == 0 ){
126                 if (coltxt)
127                         manifest_info->storeclient_id = strdup(coltxt);
128                 else
129                         manifest_info->storeclient_id = NULL;
130         } else if (strcmp(colname, "package_url") == 0 ){
131                 if (coltxt)
132                         manifest_info->package_url = strdup(coltxt);
133                 else
134                         manifest_info->package_url = NULL;
135         } else if (strcmp(colname, "root_path") == 0 ){
136                 if (coltxt)
137                         manifest_info->root_path = strdup(coltxt);
138                 else
139                         manifest_info->root_path = NULL;
140         } else if (strcmp(colname, "csc_path") == 0 ){
141                 if (coltxt)
142                         manifest_info->csc_path = strdup(coltxt);
143                 else
144                         manifest_info->csc_path = NULL;
145         } else if (strcmp(colname, "package_support_disable") == 0 ){
146                 if (coltxt)
147                         manifest_info->support_disable = strdup(coltxt);
148                 else
149                         manifest_info->support_disable = NULL;
150         } else if (strcmp(colname, "package_mother_package") == 0 ){
151                 if (coltxt)
152                         manifest_info->mother_package = strdup(coltxt);
153                 else
154                         manifest_info->mother_package = NULL;
155         } else if (strcmp(colname, "package_support_mode") == 0 ){
156                 if (coltxt)
157                         manifest_info->support_mode = strdup(coltxt);
158                 else
159                         manifest_info->support_mode = NULL;
160         } else if (strcmp(colname, "package_reserve1") == 0 ){
161                 if (coltxt)
162                         manifest_info->support_reset = strdup(coltxt);
163                 else
164                         manifest_info->support_reset = NULL;
165         } else if (strcmp(colname, "package_reserve2") == 0 ){
166                 if (coltxt)
167                         manifest_info->use_reset = strdup(coltxt);
168                 else
169                         manifest_info->use_reset = NULL;
170         } else if (strcmp(colname, "package_hash") == 0 ){
171                 if (coltxt)
172                         manifest_info->hash = strdup(coltxt);
173                 else
174                         manifest_info->hash = NULL;
175         } else if (strcmp(colname, "package_reserve3") == 0 ){
176                 if (coltxt)
177                         manifest_info->groupid = strdup(coltxt);
178                 else
179                         manifest_info->groupid = NULL;
180
181         /*end package_info table*/
182
183         } else if (strcmp(colname, "author_email") == 0 ){
184                 if (coltxt)
185                         manifest_info->author->email = strdup(coltxt);
186                 else
187                         manifest_info->author->email = NULL;
188         } else if (strcmp(colname, "author_href") == 0 ){
189                 if (coltxt)
190                         manifest_info->author->href = strdup(coltxt);
191                 else
192                         manifest_info->author->href = NULL;
193         } else if (strcmp(colname, "package_label") == 0 ){
194                 if (coltxt)
195                         manifest_info->label->text = strdup(coltxt);
196                 else
197                         manifest_info->label->text = NULL;
198         } else if (strcmp(colname, "package_icon") == 0 ){
199                 if (coltxt)
200                         manifest_info->icon->text = strdup(coltxt);
201                 else
202                         manifest_info->icon->text = NULL;
203         } else if (strcmp(colname, "package_description") == 0 ){
204                 if (coltxt)
205                         manifest_info->description->text = strdup(coltxt);
206                 else
207                         manifest_info->description->text = NULL;
208         } else if (strcmp(colname, "package_author") == 0 ){
209                 if (coltxt)
210                         manifest_info->author->text = strdup(coltxt);
211                 else
212                         manifest_info->author->text = NULL;
213         } else if (strcmp(colname, "package_locale") == 0 ){
214                 if (coltxt) {
215                         manifest_info->author->lang = strdup(coltxt);
216                         manifest_info->icon->lang = strdup(coltxt);
217                         manifest_info->label->lang = strdup(coltxt);
218                         manifest_info->description->lang = strdup(coltxt);
219                 }
220                 else {
221                         manifest_info->author->lang = NULL;
222                         manifest_info->icon->lang = NULL;
223                         manifest_info->label->lang = NULL;
224                         manifest_info->description->lang = NULL;
225                 }
226         /*end package_localized_info table*/
227
228         } else if (strcmp(colname, "privilege") == 0 ){
229                 if (coltxt)
230                         manifest_info->privileges->privilege->text = strdup(coltxt);
231                 else
232                         manifest_info->privileges->privilege->text = NULL;
233
234         }
235         /*end package_privilege_info table*/
236
237 }
238
239 static void __get_pkginfo_for_list(sqlite3_stmt *stmt, pkgmgr_pkginfo_x *udata)
240 {
241         int i = 0;
242         int ncols = 0;
243         char *colname = NULL;
244         char *coltxt = NULL;
245         pkgmgr_pkginfo_x *info = NULL;
246
247         info = calloc(1, sizeof(pkgmgr_pkginfo_x));
248         if (info == NULL) {
249                 _LOGE("out of memory");
250                 return;
251         }
252
253         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
254         if (info->manifest_info == NULL) {
255                 _LOGE("out of memory");
256                 free(info);
257                 return;
258         }
259
260         info->manifest_info->icon= (icon_x *)calloc(1, sizeof(icon_x));
261         if (info->manifest_info->icon == NULL) {
262                 _LOGE("out of memory");
263                 free(info->manifest_info);
264                 free(info);
265                 return;
266         }
267
268         info->manifest_info->label= (label_x *)calloc(1, sizeof(label_x));
269         if (info->manifest_info->label == NULL) {
270                 _LOGE("out of memory");
271                 free(info->manifest_info->icon);
272                 free(info->manifest_info);
273                 free(info);
274                 return;
275         }
276
277         info->manifest_info->author= (author_x *)calloc(1, sizeof(author_x));
278         if (info->manifest_info->author == NULL) {
279                 _LOGE("out of memory");
280                 free(info->manifest_info->label);
281                 free(info->manifest_info->icon);
282                 free(info->manifest_info);
283                 free(info);
284                 return;
285         }
286
287         info->manifest_info->description= (description_x *)calloc(1, sizeof(description_x));
288         if (info->manifest_info->description == NULL) {
289                 _LOGE("out of memory");
290                 free(info->manifest_info->author);
291                 free(info->manifest_info->label);
292                 free(info->manifest_info->icon);
293                 free(info->manifest_info);
294                 free(info);
295                 return;
296         }
297
298         ncols = sqlite3_column_count(stmt);
299
300         LISTADD(udata, info);
301
302         for(i = 0; i < ncols; i++)
303         {
304                 colname = (char *)sqlite3_column_name(stmt, i);
305                 coltxt = (char *)sqlite3_column_text(stmt, i);
306
307 //              _LOGE("field value  :: %s = %s \n", colname, coltxt);
308                 __get_pkginfo_from_db(colname, coltxt, info->manifest_info);
309         }
310 }
311
312 static void __update_localed_label_for_list(sqlite3_stmt *stmt, pkgmgr_pkginfo_x *list_pkginfo)
313 {
314         int i = 0;
315         int ncols = 0;
316         char *colname = NULL;
317         char *coltxt = NULL;
318
319         pkgmgr_pkginfo_x *node = NULL;
320         pkgmgr_pkginfo_x *pkginfo = NULL;
321
322         LISTHEAD(list_pkginfo, node);
323         pkginfo = node->next;
324
325         ncols = sqlite3_column_count(stmt);
326
327         for(i = 0; i < ncols; i++)
328         {
329                 colname = (char *)sqlite3_column_name(stmt, i);
330                 if (colname && strcmp(colname, "package_label") == 0 ){
331                         coltxt = (char *)sqlite3_column_text(stmt, i);
332                         FREE_AND_STRDUP(coltxt, pkginfo->manifest_info->label->text);
333                 }
334         }
335 }
336
337 int __pkginfo_cb(void *data, int ncols, char **coltxt, char **colname)
338 {
339         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)data;
340         int i = 0;
341         author_x *author = NULL;
342         icon_x *icon = NULL;
343         label_x *label = NULL;
344         description_x *description = NULL;
345         privilege_x *privilege = NULL;
346
347         author = calloc(1, sizeof(author_x));
348         retvm_if(author == NULL, PMINFO_R_ERROR, "out of memory");
349         LISTADD(info->manifest_info->author, author);
350         icon = calloc(1, sizeof(icon_x));
351         retvm_if(icon == NULL, PMINFO_R_ERROR, "out of memory");
352         LISTADD(info->manifest_info->icon, icon);
353         label = calloc(1, sizeof(label_x));
354         retvm_if(label == NULL, PMINFO_R_ERROR, "out of memory");
355         LISTADD(info->manifest_info->label, label);
356         description = calloc(1, sizeof(description_x));
357         retvm_if(description == NULL, PMINFO_R_ERROR, "out of memory");
358         LISTADD(info->manifest_info->description, description);
359         privilege = calloc(1, sizeof(privilege_x));
360         retvm_if(privilege == NULL, PMINFO_R_ERROR, "out of memory");
361         LISTADD(info->manifest_info->privileges->privilege, privilege);
362
363         for(i = 0; i < ncols; i++)
364         {
365                 __get_pkginfo_from_db(colname[i], coltxt[i], info->manifest_info);
366         }
367         return 0;
368 }
369
370 static int __cert_cb(void *data, int ncols, char **coltxt, char **colname)
371 {
372         pkgmgr_cert_x *info = (pkgmgr_cert_x *)data;
373         int i = 0;
374
375         for(i = 0; i < ncols; i++)
376         {
377                 if (strcmp(colname[i], "author_signer_cert") == 0) {
378                         if (coltxt[i])
379                                 info->cert_id = atoi(coltxt[i]);
380                         else
381                                 info->cert_id = 0;
382                 } else if (strcmp(colname[i], "package") == 0) {
383                         if (coltxt[i] && info->pkgid == NULL) {
384                                 info->pkgid= strdup(coltxt[i]);
385                         } else {
386                                 if (info->pkgid)
387                                         free(info->pkgid);
388
389                                 info->pkgid = NULL;
390                         }
391                 } else
392                         continue;
393         }
394         return 0;
395 }
396
397 API int pkgmgrinfo_pkginfo_get_list(pkgmgrinfo_pkg_list_cb pkg_list_cb, void *user_data)
398 {
399         retvm_if(pkg_list_cb == NULL, PMINFO_R_EINVAL, "callback function is NULL\n");
400
401         int ret = PMINFO_R_OK;
402         char *locale = NULL;
403         char *query = NULL;
404
405         char pkgid[MAX_QUERY_LEN] = {0,};
406         char pre_pkgid[MAX_QUERY_LEN] = {0,};
407
408         sqlite3 *pkginfo_db = NULL;
409         sqlite3_stmt *stmt = NULL;
410
411         pkgmgr_pkginfo_x *node = NULL;
412         pkgmgr_pkginfo_x *pkginfo = NULL;
413         pkgmgr_pkginfo_x *list_pkginfo = NULL;
414
415         /*open db*/
416         ret = db_util_open(MANIFEST_DB, &pkginfo_db, 0);
417         retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "db_util_open[%s] failed!", MANIFEST_DB);
418
419         /*Start constructing query*/
420         locale = __convert_system_locale_to_manifest_locale();
421         retvm_if(locale == NULL, PMINFO_R_ERROR, "locale is NULL");
422         query = sqlite3_mprintf("select * from package_info LEFT OUTER JOIN package_localized_info " \
423                                                                 "ON package_info.package=package_localized_info.package "\
424                                                                 "where package_info.package_disable='false' and package_localized_info.package_locale IN (%Q, %Q)", DEFAULT_LOCALE, locale);
425
426         /*prepare query*/
427         ret = sqlite3_prepare_v2(pkginfo_db, query, strlen(query), &stmt, NULL);
428         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
429
430         /*calloc pkginfo_x*/
431         list_pkginfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
432         tryvm_if(list_pkginfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
433
434         /*step query*/
435         while(1) {
436                 ret = sqlite3_step(stmt);
437                 if(ret == SQLITE_ROW) {
438
439                         memset(pkgid, 0, MAX_QUERY_LEN);
440                         strncpy(pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
441
442                         if (strlen(pre_pkgid) != 0) {
443                                 if (strcmp(pre_pkgid, pkgid) == 0) {
444                                         __update_localed_label_for_list(stmt, list_pkginfo);
445
446                                         memset(pre_pkgid, 0, MAX_QUERY_LEN);
447                                         strncpy(pre_pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
448
449                                         continue;
450                                 } else {
451                                         memset(pre_pkgid, 0, MAX_QUERY_LEN);
452                                         strncpy(pre_pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
453                                 }
454                         } else {
455                                 strncpy(pre_pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
456                         }
457
458                         __get_pkginfo_for_list(stmt, list_pkginfo);
459
460                 } else {
461                         break;
462                 }
463         }
464
465         LISTHEAD(list_pkginfo, node);
466
467         for(node = node->next; node ; node = node->next) {
468                 pkginfo = node;
469                 pkginfo->locale = strdup(locale);
470
471                 ret = __pkginfo_check_installed_storage(pkginfo);
472                 if(ret < 0)
473                         continue;
474
475                 ret = pkg_list_cb( (void *)pkginfo, user_data);
476                 if(ret < 0)
477                         break;
478         }
479
480 catch:
481         sqlite3_free(query);
482         sqlite3_finalize(stmt);
483         sqlite3_close(pkginfo_db);
484         FREE_AND_NULL(locale);
485
486         __cleanup_list_pkginfo(list_pkginfo, node);
487
488         return ret;
489 }
490
491 API int pkgmgrinfo_pkginfo_get_mounted_list(pkgmgrinfo_pkg_list_cb pkg_list_cb, void *user_data)
492 {
493         retvm_if(pkg_list_cb == NULL, PMINFO_R_EINVAL, "callback function is NULL\n");
494
495         int ret = PMINFO_R_OK;
496         char *locale = NULL;
497         char *query = NULL;
498
499         char pkgid[MAX_QUERY_LEN] = {0,};
500         char pre_pkgid[MAX_QUERY_LEN] = {0,};
501
502         sqlite3 *pkginfo_db = NULL;
503         sqlite3_stmt *stmt = NULL;
504
505         pkgmgr_pkginfo_x *node = NULL;
506         pkgmgr_pkginfo_x *pkginfo = NULL;
507         pkgmgr_pkginfo_x *list_pkginfo = NULL;
508
509         /*open db*/
510         ret = db_util_open(MANIFEST_DB, &pkginfo_db, 0);
511         retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "db_util_open[%s] failed!", MANIFEST_DB);
512
513         /*Start constructing query*/
514         locale = __convert_system_locale_to_manifest_locale();
515         retvm_if(locale == NULL, PMINFO_R_ERROR, "locale is NULL");
516         query = sqlite3_mprintf("select * from package_info LEFT OUTER JOIN package_localized_info " \
517                                                                 "ON package_info.package=package_localized_info.package "\
518                                                                 "where installed_storage='installed_external' and package_info.package_disable='false' and package_localized_info.package_locale IN (%Q, %Q)", DEFAULT_LOCALE, locale);
519
520         /*prepare query*/
521         ret = sqlite3_prepare_v2(pkginfo_db, query, strlen(query), &stmt, NULL);
522         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
523
524         /*calloc pkginfo_x*/
525         list_pkginfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
526         tryvm_if(list_pkginfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
527
528         /*step query*/
529         while(1) {
530                 ret = sqlite3_step(stmt);
531                 if(ret == SQLITE_ROW) {
532
533                         memset(pkgid, 0, MAX_QUERY_LEN);
534                         strncpy(pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
535
536                         if (strlen(pre_pkgid) != 0) {
537                                 if (strcmp(pre_pkgid, pkgid) == 0) {
538                                         __update_localed_label_for_list(stmt, list_pkginfo);
539
540                                         memset(pre_pkgid, 0, MAX_QUERY_LEN);
541                                         strncpy(pre_pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
542                                         continue;
543                                 } else {
544                                         memset(pre_pkgid, 0, MAX_QUERY_LEN);
545                                         strncpy(pre_pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
546                                 }
547                         } else {
548                                 strncpy(pre_pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
549                         }
550
551                         __get_pkginfo_for_list(stmt, list_pkginfo);
552
553                 } else {
554                         break;
555                 }
556         }
557
558         LISTHEAD(list_pkginfo, node);
559
560         for(node = node->next; node ; node = node->next) {
561                 pkginfo = node;
562                 pkginfo->locale = strdup(locale);
563
564                 ret = __pkginfo_check_installed_storage(pkginfo);
565                 if(ret < 0)
566                         continue;
567
568                 ret = pkg_list_cb( (void *)pkginfo, user_data);
569                 if(ret < 0)
570                         break;
571         }
572
573 catch:
574         sqlite3_free(query);
575         sqlite3_finalize(stmt);
576         sqlite3_close(pkginfo_db);
577         FREE_AND_NULL(locale);
578
579         __cleanup_list_pkginfo(list_pkginfo, node);
580
581         return ret;
582 }
583
584
585 API int pkgmgrinfo_pkginfo_get_unmounted_list(pkgmgrinfo_pkg_list_cb pkg_list_cb, void *user_data)
586 {
587         retvm_if(pkg_list_cb == NULL, PMINFO_R_EINVAL, "callback function is NULL\n");
588
589         int ret = PMINFO_R_OK;
590         char *locale = NULL;
591         char *query = NULL;
592
593         char pkgid[MAX_QUERY_LEN] = {0,};
594         char pre_pkgid[MAX_QUERY_LEN] = {0,};
595
596         sqlite3 *pkginfo_db = NULL;
597         sqlite3_stmt *stmt = NULL;
598
599         pkgmgr_pkginfo_x *node = NULL;
600         pkgmgr_pkginfo_x *pkginfo = NULL;
601         pkgmgr_pkginfo_x *list_pkginfo = NULL;
602
603         /*open db*/
604         ret = db_util_open(MANIFEST_DB, &pkginfo_db, 0);
605         retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "db_util_open[%s] failed!", MANIFEST_DB);
606
607         /*Start constructing query*/
608         locale = __convert_system_locale_to_manifest_locale();
609         retvm_if(locale == NULL, PMINFO_R_ERROR, "locale is NULL");
610         query = sqlite3_mprintf("select * from package_info LEFT OUTER JOIN package_localized_info " \
611                                                                 "ON package_info.package=package_localized_info.package "\
612                                                                 "where installed_storage='installed_external' and package_info.package_disable='false' and package_localized_info.package_locale IN (%Q, %Q)", DEFAULT_LOCALE, locale);
613
614         /*prepare query*/
615         ret = sqlite3_prepare_v2(pkginfo_db, query, strlen(query), &stmt, NULL);
616         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
617
618         /*calloc pkginfo_x*/
619         list_pkginfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
620         tryvm_if(list_pkginfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
621
622         /*step query*/
623         while(1) {
624                 ret = sqlite3_step(stmt);
625                 if(ret == SQLITE_ROW) {
626
627                         memset(pkgid, 0, MAX_QUERY_LEN);
628                         strncpy(pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
629
630                         if (strlen(pre_pkgid) != 0) {
631                                 if (strcmp(pre_pkgid, pkgid) == 0) {
632                                         __update_localed_label_for_list(stmt, list_pkginfo);
633
634                                         memset(pre_pkgid, 0, MAX_QUERY_LEN);
635                                         strncpy(pre_pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
636
637                                         continue;
638                                 } else {
639                                         memset(pre_pkgid, 0, MAX_QUERY_LEN);
640                                         strncpy(pre_pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
641                                 }
642                         } else {
643                                 strncpy(pre_pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
644                         }
645
646                         __get_pkginfo_for_list(stmt, list_pkginfo);
647
648                 } else {
649                         break;
650                 }
651         }
652
653         LISTHEAD(list_pkginfo, node);
654
655         for(node = node->next; node ; node = node->next) {
656                 pkginfo = node;
657                 pkginfo->locale = strdup(locale);
658
659                 ret = pkg_list_cb( (void *)pkginfo, user_data);
660                 if(ret < 0)
661                         break;
662         }
663
664 catch:
665         sqlite3_free(query);
666         sqlite3_finalize(stmt);
667         sqlite3_close(pkginfo_db);
668         FREE_AND_NULL(locale);
669
670         __cleanup_list_pkginfo(list_pkginfo, node);
671
672         return ret;
673 }
674
675 API int pkgmgrinfo_pkginfo_get_unmounted_pkginfo(const char *pkgid, pkgmgrinfo_pkginfo_h *handle)
676 {
677         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
678         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
679         pkgmgr_pkginfo_x *pkginfo = NULL;
680         int ret = PMINFO_R_OK;
681         char *query = NULL;
682         char *locale = NULL;
683         int exist = 0;
684         label_x *tmp1 = NULL;
685         icon_x *tmp2 = NULL;
686         description_x *tmp3 = NULL;
687         author_x *tmp4 = NULL;
688         privilege_x *tmp5 = NULL;
689         sqlite3 *pkginfo_db = NULL;
690
691         /*validate pkgid*/
692         ret = db_util_open(MANIFEST_DB, &pkginfo_db, 0);
693         retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
694
695         /*check pkgid exist on db*/
696         query= sqlite3_mprintf("select exists(select * from package_info where package=%Q and package_disable='false')", pkgid);
697         ret = __exec_db_query(pkginfo_db, query, _pkgmgrinfo_validate_cb, (void *)&exist);
698         sqlite3_free(query);
699         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "sqlite3_exec[%s] fail", pkgid);
700         tryvm_if(exist == 0, ret = PMINFO_R_ERROR, "pkgid[%s] not found in DB", pkgid);
701
702         /*get system locale*/
703         locale = __convert_system_locale_to_manifest_locale();
704         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL");
705
706         pkginfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
707         tryvm_if(pkginfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for pkginfo");
708
709         pkginfo->locale = strdup(locale);
710
711         pkginfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
712         tryvm_if(pkginfo->manifest_info == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for manifest info");
713
714         pkginfo->manifest_info->package = strdup(pkgid);
715         pkginfo->manifest_info->privileges = (privileges_x *)calloc(1, sizeof(privileges_x));
716         tryvm_if(pkginfo->manifest_info->privileges == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for privileges info");
717
718         /*populate manifest_info from DB*/
719         query= sqlite3_mprintf("select * from package_info where package=%Q and package_disable='false'", pkgid);
720         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
721         sqlite3_free(query);
722         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
723
724         /*populate privilege_info from DB*/
725         query= sqlite3_mprintf("select * from package_privilege_info where package=%Q ", pkgid);
726         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
727         sqlite3_free(query);
728         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Privilege Info DB Information retrieval failed");
729
730         query= sqlite3_mprintf("select * from package_localized_info where package=%Q and package_locale=%Q", pkgid, locale);
731         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
732         sqlite3_free(query);
733         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
734
735         /*Also store the values corresponding to default locales*/
736         query= sqlite3_mprintf("select * from package_localized_info where package=%Q and package_locale=%Q", pkgid, DEFAULT_LOCALE);
737         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
738         sqlite3_free(query);
739         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
740
741         SAFE_LISTHEAD(pkginfo->manifest_info->label, tmp1);
742         SAFE_LISTHEAD(pkginfo->manifest_info->icon, tmp2);
743         SAFE_LISTHEAD(pkginfo->manifest_info->description, tmp3);
744         SAFE_LISTHEAD(pkginfo->manifest_info->author, tmp4);
745         SAFE_LISTHEAD(pkginfo->manifest_info->privileges->privilege, tmp5);
746
747 catch:
748         if (ret == PMINFO_R_OK)
749                 *handle = (void*)pkginfo;
750         else {
751                 *handle = NULL;
752                 __cleanup_pkginfo(pkginfo);
753         }
754         sqlite3_close(pkginfo_db);
755
756         FREE_AND_NULL(locale);
757         return ret;
758 }
759
760 API int pkgmgrinfo_pkginfo_get_pkginfo(const char *pkgid, pkgmgrinfo_pkginfo_h *handle)
761 {
762         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
763         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
764         pkgmgr_pkginfo_x *pkginfo = NULL;
765         int ret = PMINFO_R_OK;
766         char *query = NULL;
767         char *locale = NULL;
768         int exist = 0;
769         label_x *tmp1 = NULL;
770         icon_x *tmp2 = NULL;
771         description_x *tmp3 = NULL;
772         author_x *tmp4 = NULL;
773         privilege_x *tmp5 = NULL;
774         sqlite3 *pkginfo_db = NULL;
775
776         /*validate pkgid*/
777         ret = db_util_open(MANIFEST_DB, &pkginfo_db, 0);
778         retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
779
780         /*check pkgid exist on db*/
781         query= sqlite3_mprintf("select exists(select * from package_info where package=%Q and package_disable='false')", pkgid);
782         ret = __exec_db_query(pkginfo_db, query, _pkgmgrinfo_validate_cb, (void *)&exist);
783         sqlite3_free(query);
784         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "sqlite3_exec[%s] fail", pkgid);
785         if (exist == 0) {
786                 _LOGS("pkgid[%s] not found in DB", pkgid);
787                 ret = PMINFO_R_ERROR;
788                 goto catch;
789         }
790
791         /*get system locale*/
792         locale = __convert_system_locale_to_manifest_locale();
793         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL");
794
795         pkginfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
796         tryvm_if(pkginfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for pkginfo");
797
798         pkginfo->locale = strdup(locale);
799
800         pkginfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
801         tryvm_if(pkginfo->manifest_info == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for manifest info");
802
803         pkginfo->manifest_info->package = strdup(pkgid);
804         pkginfo->manifest_info->privileges = (privileges_x *)calloc(1, sizeof(privileges_x));
805         tryvm_if(pkginfo->manifest_info->privileges == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for privileges info");
806
807         /*populate manifest_info from DB*/
808         query= sqlite3_mprintf("select * from package_info where package=%Q and package_disable='false'", pkgid);
809         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
810         sqlite3_free(query);
811         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
812
813         /*populate privilege_info from DB*/
814         query= sqlite3_mprintf("select * from package_privilege_info where package=%Q ", pkgid);
815         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
816         sqlite3_free(query);
817         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Privilege Info DB Information retrieval failed");
818
819         query= sqlite3_mprintf("select * from package_localized_info where package=%Q and package_locale=%Q", pkgid, locale);
820         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
821         sqlite3_free(query);
822         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
823
824         /*Also store the values corresponding to default locales*/
825         query= sqlite3_mprintf("select * from package_localized_info where package=%Q and package_locale=%Q", pkgid, DEFAULT_LOCALE);
826         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
827         sqlite3_free(query);
828         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
829
830         SAFE_LISTHEAD(pkginfo->manifest_info->label, tmp1);
831         SAFE_LISTHEAD(pkginfo->manifest_info->icon, tmp2);
832         SAFE_LISTHEAD(pkginfo->manifest_info->description, tmp3);
833         SAFE_LISTHEAD(pkginfo->manifest_info->author, tmp4);
834         SAFE_LISTHEAD(pkginfo->manifest_info->privileges->privilege, tmp5);
835
836         ret = __pkginfo_check_installed_storage(pkginfo);
837         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "[%s] is installed external, but is not in mmc", pkgid);
838
839 catch:
840         if (ret == PMINFO_R_OK)
841                 *handle = (void*)pkginfo;
842         else {
843                 *handle = NULL;
844                 __cleanup_pkginfo(pkginfo);
845         }
846         sqlite3_close(pkginfo_db);
847
848         FREE_AND_NULL(locale);
849         return ret;
850 }
851
852
853 API int pkgmgrinfo_pkginfo_get_pkgname(pkgmgrinfo_pkginfo_h handle, char **pkg_name)
854 {
855         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
856         retvm_if(pkg_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
857         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
858         if (info->manifest_info->package)
859                 *pkg_name = (char *)info->manifest_info->package;
860         else
861                 return PMINFO_R_ERROR;
862
863         return PMINFO_R_OK;
864 }
865
866 API int pkgmgrinfo_pkginfo_get_pkgid(pkgmgrinfo_pkginfo_h handle, char **pkgid)
867 {
868         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
869         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
870         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
871         if (info->manifest_info->package)
872                 *pkgid = (char *)info->manifest_info->package;
873         else
874                 return PMINFO_R_ERROR;
875
876         return PMINFO_R_OK;
877 }
878
879 API int pkgmgrinfo_pkginfo_get_type(pkgmgrinfo_pkginfo_h handle, char **type)
880 {
881         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
882         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
883         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
884         if (info->manifest_info->type)
885                 *type = (char *)info->manifest_info->type;
886         else
887                 *type = "rpm";
888         return PMINFO_R_OK;
889 }
890
891 API int pkgmgrinfo_pkginfo_get_version(pkgmgrinfo_pkginfo_h handle, char **version)
892 {
893         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
894         retvm_if(version == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
895         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
896         *version = (char *)info->manifest_info->version;
897         return PMINFO_R_OK;
898 }
899
900 API int pkgmgrinfo_pkginfo_get_install_location(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_install_location *location)
901 {
902         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
903         retvm_if(location == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
904         char *val = NULL;
905         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
906         val = (char *)info->manifest_info->installlocation;
907         if (val) {
908                 if (strcmp(val, "internal-only") == 0)
909                         *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
910                 else if (strcmp(val, "prefer-external") == 0)
911                         *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
912                 else
913                         *location = PMINFO_INSTALL_LOCATION_AUTO;
914         }
915         return PMINFO_R_OK;
916 }
917
918 API int pkgmgrinfo_pkginfo_get_icon(pkgmgrinfo_pkginfo_h handle, char **icon)
919 {
920         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
921         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
922         int ret = PMINFO_R_OK;
923         char *locale = NULL;
924         icon_x *ptr = NULL;
925         *icon = NULL;
926
927         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
928
929         locale = info->locale;
930         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
931
932         for(ptr = info->manifest_info->icon; ptr != NULL; ptr = ptr->next)
933         {
934                 if (ptr->lang) {
935                         if (strcmp(ptr->lang, locale) == 0) {
936                                 if (ptr->text) {
937                                         *icon = (char *)ptr->text;
938                                         if (strcasecmp(*icon, PKGMGR_PARSER_EMPTY_STR) == 0) {
939                                                 locale = DEFAULT_LOCALE;
940                                                 continue;
941                                         } else
942                                                 break;
943                                 } else {
944                                         locale = DEFAULT_LOCALE;
945                                         continue;
946                                 }
947                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
948                                 *icon = (char *)ptr->text;
949                                 break;
950                         }
951                 }
952         }
953
954         return ret;
955 }
956
957 API int pkgmgrinfo_pkginfo_get_label(pkgmgrinfo_pkginfo_h handle, char **label)
958 {
959         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
960         retvm_if(label == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
961         int ret = PMINFO_R_OK;
962         char *locale = NULL;
963         label_x *ptr = NULL;
964         *label = NULL;
965
966         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
967         locale = info->locale;
968         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
969
970         for(ptr = info->manifest_info->label; ptr != NULL; ptr = ptr->next)
971         {
972                 if (ptr->lang) {
973                         if (strcmp(ptr->lang, locale) == 0) {
974                                 if (ptr->text) {
975                                         *label = (char *)ptr->text;
976                                         if (strcasecmp(*label, PKGMGR_PARSER_EMPTY_STR) == 0) {
977                                                 locale = DEFAULT_LOCALE;
978                                                 continue;
979                                         } else
980                                                 break;
981                                 } else {
982                                         locale = DEFAULT_LOCALE;
983                                         continue;
984                                 }
985                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
986                                 *label = (char *)ptr->text;
987                                 break;
988                         }
989                 }
990         }
991
992         return ret;
993 }
994
995 API int pkgmgrinfo_pkginfo_get_description(pkgmgrinfo_pkginfo_h handle, char **description)
996 {
997         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
998         retvm_if(description == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
999         char *locale = NULL;
1000         description_x *ptr = NULL;
1001         *description = NULL;
1002
1003         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1004         locale = info->locale;
1005         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
1006
1007         for(ptr = info->manifest_info->description; ptr != NULL; ptr = ptr->next)
1008         {
1009                 if (ptr->lang) {
1010                         if (strcmp(ptr->lang, locale) == 0) {
1011                                 *description = (char *)ptr->text;
1012                                 if (strcasecmp(*description, PKGMGR_PARSER_EMPTY_STR) == 0) {
1013                                         locale = DEFAULT_LOCALE;
1014                                         continue;
1015                                 } else
1016                                         break;
1017                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
1018                                 *description = (char *)ptr->text;
1019                                 break;
1020                         }
1021                 }
1022         }
1023         return PMINFO_R_OK;
1024 }
1025
1026 API int pkgmgrinfo_pkginfo_get_author_name(pkgmgrinfo_pkginfo_h handle, char **author_name)
1027 {
1028         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1029         retvm_if(author_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1030         char *locale = NULL;
1031         author_x *ptr = NULL;
1032         *author_name = NULL;
1033
1034         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1035         locale = info->locale;
1036         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
1037
1038         for(ptr = info->manifest_info->author; ptr != NULL; ptr = ptr->next)
1039         {
1040                 if (ptr->lang) {
1041                         if (strcmp(ptr->lang, locale) == 0) {
1042                                 *author_name = (char *)ptr->text;
1043                                 if (strcasecmp(*author_name, PKGMGR_PARSER_EMPTY_STR) == 0) {
1044                                         locale = DEFAULT_LOCALE;
1045                                         continue;
1046                                 } else
1047                                         break;
1048                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
1049                                 *author_name = (char *)ptr->text;
1050                                 break;
1051                         }
1052                 }
1053         }
1054         return PMINFO_R_OK;
1055 }
1056
1057 API int pkgmgrinfo_pkginfo_get_author_email(pkgmgrinfo_pkginfo_h handle, char **author_email)
1058 {
1059         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1060         retvm_if(author_email == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1061         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1062         *author_email = (char *)info->manifest_info->author->email;
1063         return PMINFO_R_OK;
1064 }
1065
1066 API int pkgmgrinfo_pkginfo_get_author_href(pkgmgrinfo_pkginfo_h handle, char **author_href)
1067 {
1068         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1069         retvm_if(author_href == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1070         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1071         *author_href = (char *)info->manifest_info->author->href;
1072         return PMINFO_R_OK;
1073 }
1074
1075 API int pkgmgrinfo_pkginfo_get_installed_storage(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_installed_storage *storage)
1076 {
1077         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1078         retvm_if(storage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1079
1080         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1081
1082          if (strcmp(info->manifest_info->installed_storage,"installed_internal") == 0)
1083                 *storage = PMINFO_INTERNAL_STORAGE;
1084          else if (strcmp(info->manifest_info->installed_storage,"installed_external") == 0)
1085                  *storage = PMINFO_EXTERNAL_STORAGE;
1086          else
1087                  return PMINFO_R_ERROR;
1088
1089         return PMINFO_R_OK;
1090 }
1091
1092 API int pkgmgrinfo_pkginfo_get_installed_time(pkgmgrinfo_pkginfo_h handle, int *installed_time)
1093 {
1094         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1095         retvm_if(installed_time == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1096         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1097         if (info->manifest_info->installed_time)
1098                 *installed_time = atoi(info->manifest_info->installed_time);
1099         else
1100                 return PMINFO_R_ERROR;
1101
1102         return PMINFO_R_OK;
1103 }
1104
1105 API int pkgmgrinfo_pkginfo_get_storeclientid(pkgmgrinfo_pkginfo_h handle, char **storeclientid)
1106 {
1107         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1108         retvm_if(storeclientid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1109         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1110         *storeclientid = (char *)info->manifest_info->storeclient_id;
1111         return PMINFO_R_OK;
1112 }
1113
1114 API int pkgmgrinfo_pkginfo_get_mainappid(pkgmgrinfo_pkginfo_h handle, char **mainappid)
1115 {
1116         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1117         retvm_if(mainappid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1118         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1119         *mainappid = (char *)info->manifest_info->mainapp_id;
1120         return PMINFO_R_OK;
1121 }
1122
1123 API int pkgmgrinfo_pkginfo_get_url(pkgmgrinfo_pkginfo_h handle, char **url)
1124 {
1125         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1126         retvm_if(url == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1127         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1128         *url = (char *)info->manifest_info->package_url;
1129         return PMINFO_R_OK;
1130 }
1131
1132 API int pkgmgrinfo_pkginfo_get_root_path(pkgmgrinfo_pkginfo_h handle, char **path)
1133 {
1134         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1135         retvm_if(path == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1136
1137         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1138         if (info->manifest_info->root_path)
1139                 *path = (char *)info->manifest_info->root_path;
1140         else
1141                 return PMINFO_R_ERROR;
1142
1143         return PMINFO_R_OK;
1144 }
1145
1146 API int pkgmgrinfo_pkginfo_get_csc_path(pkgmgrinfo_pkginfo_h handle, char **path)
1147 {
1148         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1149         retvm_if(path == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1150
1151         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1152         if (info->manifest_info->csc_path)
1153                 *path = (char *)info->manifest_info->csc_path;
1154         else
1155                 return PMINFO_R_ERROR;
1156
1157         return PMINFO_R_OK;
1158 }
1159
1160 API int pkgmgrinfo_pkginfo_get_support_mode(pkgmgrinfo_pkginfo_h handle, int *support_mode)
1161 {
1162         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1163         retvm_if(support_mode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1164
1165         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1166         if (info->manifest_info->support_mode)
1167                 *support_mode = atoi(info->manifest_info->support_mode);
1168         else
1169                 *support_mode = 0;
1170
1171         return PMINFO_R_OK;
1172 }
1173
1174 API int pkgmgrinfo_pkginfo_get_hash(pkgmgrinfo_pkginfo_h handle, char **hash)
1175 {
1176         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1177         retvm_if(hash == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1178
1179         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1180         if (info->manifest_info->hash)
1181                 *hash = (char *)info->manifest_info->hash;
1182         else
1183                 *hash = NULL;
1184
1185         return PMINFO_R_OK;
1186
1187 }
1188
1189 API int pkgmgrinfo_pkginfo_get_groupid(pkgmgrinfo_pkginfo_h handle, char **groupid)
1190 {
1191         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1192         retvm_if(groupid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1193
1194         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1195         if (info->manifest_info->groupid)
1196                 *groupid = (char *)info->manifest_info->groupid;
1197         else
1198                 return PMINFO_R_ERROR;
1199
1200         return PMINFO_R_OK;
1201 }
1202
1203 API int pkgmgrinfo_pkginfo_get_support_reset(pkgmgrinfo_pkginfo_h handle, char **support_reset)
1204 {
1205         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1206         retvm_if(support_reset == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1207
1208         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1209         if (info->manifest_info->support_reset)
1210                 *support_reset = (char *)info->manifest_info->support_reset;
1211         else
1212                 return PMINFO_R_ERROR;
1213
1214         return PMINFO_R_OK;
1215 }
1216
1217 API int pkgmgrinfo_pkginfo_foreach_privilege(pkgmgrinfo_pkginfo_h handle,
1218                         pkgmgrinfo_pkg_privilege_list_cb privilege_func, void *user_data)
1219 {
1220         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
1221         retvm_if(privilege_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1222         int ret = -1;
1223         privilege_x *ptr = NULL;
1224         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1225         ptr = info->manifest_info->privileges->privilege;
1226         for (; ptr; ptr = ptr->next) {
1227                 if (ptr->text){
1228                         ret = privilege_func(ptr->text, user_data);
1229                         if (ret < 0)
1230                                 break;
1231                 }
1232         }
1233         return PMINFO_R_OK;
1234 }
1235
1236 API int pkgmgrinfo_pkginfo_compare_pkg_cert_info(const char *lhs_package_id, const char *rhs_package_id, pkgmgrinfo_cert_compare_result_type_e *compare_result)
1237 {
1238         retvm_if(lhs_package_id == NULL, PMINFO_R_EINVAL, "lhs package ID is NULL");
1239         retvm_if(rhs_package_id == NULL, PMINFO_R_EINVAL, "rhs package ID is NULL");
1240         retvm_if(compare_result == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1241
1242         int ret = PMINFO_R_OK;
1243         char *query = NULL;
1244         char *error_message = NULL;
1245         pkgmgr_cert_x *info= NULL;
1246         int lcert = 0;
1247         int rcert = 0;
1248         int exist = -1;
1249         sqlite3 *pkgmgr_cert_db = NULL;
1250
1251         *compare_result = PMINFO_CERT_COMPARE_ERROR;
1252         info = (pkgmgr_cert_x *)calloc(1, sizeof(pkgmgr_cert_x));
1253         retvm_if(info == NULL, PMINFO_R_ERROR, "Out of Memory!!!");
1254
1255         ret = db_util_open_with_options(CERT_DB, &pkgmgr_cert_db, SQLITE_OPEN_READONLY, NULL);
1256         if (ret != SQLITE_OK) {
1257                 _LOGE("connect db [%s] failed!\n", CERT_DB);
1258                 ret = PMINFO_R_ERROR;
1259                 goto err;
1260         }
1261
1262         query = sqlite3_mprintf("select exists(select * from package_cert_info where package=%Q)", lhs_package_id);
1263         if (SQLITE_OK !=
1264             sqlite3_exec(pkgmgr_cert_db, query, _pkgmgrinfo_validate_cb, (void *)&exist, &error_message)) {
1265                 _LOGE("Don't execute query = %s error message = %s\n", query,
1266                        error_message);
1267                 ret = PMINFO_R_ERROR;
1268                 sqlite3_free(query);
1269                 goto err;
1270         }
1271
1272         if (exist == 0) {
1273                 lcert = 0;
1274         } else {
1275                 sqlite3_free(query);
1276                 query = sqlite3_mprintf("select author_signer_cert from package_cert_info where package=%Q", lhs_package_id);
1277                 if (SQLITE_OK !=
1278                         sqlite3_exec(pkgmgr_cert_db, query, __cert_cb, (void *)info, &error_message)) {
1279                         _LOGE("Don't execute query = %s error message = %s\n", query,
1280                                    error_message);
1281                         ret = PMINFO_R_ERROR;
1282                         sqlite3_free(query);
1283                         goto err;
1284                 }
1285                 lcert = info->cert_id;
1286         }
1287
1288         sqlite3_free(query);
1289         query = sqlite3_mprintf("select exists(select * from package_cert_info where package=%Q)", rhs_package_id);
1290         if (SQLITE_OK !=
1291                 sqlite3_exec(pkgmgr_cert_db, query, _pkgmgrinfo_validate_cb, (void *)&exist, &error_message)) {
1292                 _LOGE("Don't execute query = %s error message = %s\n", query,
1293                            error_message);
1294                 ret = PMINFO_R_ERROR;
1295                 sqlite3_free(query);
1296                 goto err;
1297         }
1298
1299         if (exist == 0) {
1300                 rcert = 0;
1301         } else {
1302                 sqlite3_free(query);
1303                 query = sqlite3_mprintf("select author_signer_cert from package_cert_info where package=%Q", rhs_package_id);
1304                 if (SQLITE_OK !=
1305                         sqlite3_exec(pkgmgr_cert_db, query, __cert_cb, (void *)info, &error_message)) {
1306                         _LOGE("Don't execute query = %s error message = %s\n", query,
1307                                    error_message);
1308                         ret = PMINFO_R_ERROR;
1309                         sqlite3_free(query);
1310                         goto err;
1311                 }
1312                 rcert = info->cert_id;
1313                 sqlite3_free(query);
1314         }
1315
1316         if ((lcert == 0) || (rcert == 0))
1317         {
1318                 if ((lcert == 0) && (rcert == 0))
1319                         *compare_result = PMINFO_CERT_COMPARE_BOTH_NO_CERT;
1320                 else if (lcert == 0)
1321                         *compare_result = PMINFO_CERT_COMPARE_LHS_NO_CERT;
1322                 else if (rcert == 0)
1323                         *compare_result = PMINFO_CERT_COMPARE_RHS_NO_CERT;
1324         } else {
1325                 if (lcert == rcert)
1326                         *compare_result = PMINFO_CERT_COMPARE_MATCH;
1327                 else
1328                         *compare_result = PMINFO_CERT_COMPARE_MISMATCH;
1329         }
1330
1331 err:
1332         sqlite3_free(error_message);
1333         sqlite3_close(pkgmgr_cert_db);
1334         if (info) {
1335                 FREE_AND_NULL(info->pkgid);
1336                 FREE_AND_NULL(info);
1337         }
1338         return ret;
1339 }
1340
1341
1342 API int pkgmgrinfo_pkginfo_compare_app_cert_info(const char *lhs_app_id, const char *rhs_app_id, pkgmgrinfo_cert_compare_result_type_e *compare_result)
1343 {
1344         retvm_if(lhs_app_id == NULL, PMINFO_R_EINVAL, "lhs app ID is NULL");
1345         retvm_if(rhs_app_id == NULL, PMINFO_R_EINVAL, "rhs app ID is NULL");
1346         retvm_if(compare_result == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1347
1348         int ret = PMINFO_R_OK;
1349         char *query = NULL;
1350         char *error_message = NULL;
1351         pkgmgr_cert_x *info= NULL;
1352         int exist = -1;
1353         char *lpkgid = NULL;
1354         char *rpkgid = NULL;
1355         sqlite3 *pkginfo_db = NULL;
1356
1357         info = (pkgmgr_cert_x *)calloc(1, sizeof(pkgmgr_cert_x));
1358         retvm_if(info == NULL, PMINFO_R_ERROR, "Out of Memory!!!");
1359
1360         ret = db_util_open(MANIFEST_DB, &pkginfo_db, 0);
1361         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
1362
1363         query = sqlite3_mprintf("select exists(select * from package_app_info where app_id=%Q and app_disable='false')", lhs_app_id);
1364         if (SQLITE_OK !=
1365             sqlite3_exec(pkginfo_db, query, _pkgmgrinfo_validate_cb, (void *)&exist, &error_message)) {
1366                 _LOGE("Don't execute query = %s error message = %s\n", query,
1367                        error_message);
1368                 ret = PMINFO_R_ERROR;
1369                 sqlite3_free(query);
1370                 goto catch;
1371         }
1372
1373         if (exist == 0) {
1374                 lpkgid = NULL;
1375         } else {
1376                 sqlite3_free(query);
1377                 query = sqlite3_mprintf("select package from package_app_info where app_id=%Q and app_disable='false'", lhs_app_id);
1378                 if (SQLITE_OK !=
1379                         sqlite3_exec(pkginfo_db, query, __cert_cb, (void *)info, &error_message)) {
1380                         _LOGE("Don't execute query = %s error message = %s\n", query,
1381                                    error_message);
1382                         ret = PMINFO_R_ERROR;
1383                         sqlite3_free(query);
1384                         goto catch;
1385                 }
1386                 lpkgid = strdup(info->pkgid);
1387                 if (lpkgid == NULL) {
1388                         _LOGE("Out of Memory\n");
1389                         ret = PMINFO_R_ERROR;
1390                         sqlite3_free(query);
1391                         goto catch;
1392                 }
1393                 free(info->pkgid);
1394                 info->pkgid = NULL;
1395         }
1396
1397         sqlite3_free(query);
1398         query = sqlite3_mprintf("select exists(select * from package_app_info where app_id=%Q and app_disable='false')", rhs_app_id);
1399         if (SQLITE_OK !=
1400             sqlite3_exec(pkginfo_db, query, _pkgmgrinfo_validate_cb, (void *)&exist, &error_message)) {
1401                 _LOGE("Don't execute query = %s error message = %s\n", query,
1402                        error_message);
1403                 ret = PMINFO_R_ERROR;
1404                 sqlite3_free(query);
1405                 goto catch;
1406         }
1407
1408         if (exist == 0) {
1409                 rpkgid = NULL;
1410         } else {
1411                 sqlite3_free(query);
1412                 query = sqlite3_mprintf("select package from package_app_info where app_id=%Q and app_disable='false'", rhs_app_id);
1413                 if (SQLITE_OK !=
1414                         sqlite3_exec(pkginfo_db, query, __cert_cb, (void *)info, &error_message)) {
1415                         _LOGE("Don't execute query = %s error message = %s\n", query,
1416                                    error_message);
1417                         ret = PMINFO_R_ERROR;
1418                         sqlite3_free(query);
1419                         goto catch;
1420                 }
1421                 rpkgid = strdup(info->pkgid);
1422                 if (rpkgid == NULL) {
1423                         _LOGE("Out of Memory\n");
1424                         ret = PMINFO_R_ERROR;
1425                         sqlite3_free(query);
1426                         goto catch;
1427                 }
1428                 sqlite3_free(query);
1429                 FREE_AND_NULL(info->pkgid);
1430         }
1431         ret = pkgmgrinfo_pkginfo_compare_pkg_cert_info(lpkgid, rpkgid, compare_result);
1432
1433  catch:
1434         sqlite3_free(error_message);
1435         sqlite3_close(pkginfo_db);
1436         if (info) {
1437                 FREE_AND_NULL(info->pkgid);
1438                 FREE_AND_NULL(info);
1439         }
1440         FREE_AND_NULL(lpkgid);
1441         FREE_AND_NULL(rpkgid);
1442         return ret;
1443 }
1444
1445 API int pkgmgrinfo_pkginfo_is_accessible(pkgmgrinfo_pkginfo_h handle, bool *accessible)
1446 {
1447         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1448         retvm_if(accessible == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1449
1450         *accessible = 1;
1451         return PMINFO_R_OK;
1452 }
1453
1454 API int pkgmgrinfo_pkginfo_is_removable(pkgmgrinfo_pkginfo_h handle, bool *removable)
1455 {
1456         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1457         retvm_if(removable == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1458         char *val = NULL;
1459         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1460         val = (char *)info->manifest_info->removable;
1461         if (val) {
1462                 if (strcasecmp(val, "true") == 0)
1463                         *removable = 1;
1464                 else if (strcasecmp(val, "false") == 0)
1465                         *removable = 0;
1466                 else
1467                         *removable = 1;
1468         }
1469         return PMINFO_R_OK;
1470 }
1471
1472 API int pkgmgrinfo_pkginfo_is_movable(pkgmgrinfo_pkginfo_h handle, bool *movable)
1473 {
1474         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1475         retvm_if(movable == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1476
1477         char *val = NULL;
1478         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1479
1480         val = (char *)info->manifest_info->installlocation;
1481         if (val) {
1482                 if (strcmp(val, "internal-only") == 0)
1483                         *movable = 0;
1484                 else if (strcmp(val, "prefer-external") == 0)
1485                         *movable = 1;
1486                 else
1487                         *movable = 1;
1488         }
1489
1490         return PMINFO_R_OK;
1491 }
1492
1493 API int pkgmgrinfo_pkginfo_is_preload(pkgmgrinfo_pkginfo_h handle, bool *preload)
1494 {
1495         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1496         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1497         char *val = NULL;
1498         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1499         val = (char *)info->manifest_info->preload;
1500         if (val) {
1501                 if (strcasecmp(val, "true") == 0)
1502                         *preload = 1;
1503                 else if (strcasecmp(val, "false") == 0)
1504                         *preload = 0;
1505                 else
1506                         *preload = 0;
1507         }
1508         return PMINFO_R_OK;
1509 }
1510
1511 API int pkgmgrinfo_pkginfo_is_system(pkgmgrinfo_pkginfo_h handle, bool *system)
1512 {
1513         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1514         retvm_if(system == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1515
1516         char *val = NULL;
1517         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1518         val = (char *)info->manifest_info->system;
1519         if (val) {
1520                 if (strcasecmp(val, "true") == 0)
1521                         *system = 1;
1522                 else if (strcasecmp(val, "false") == 0)
1523                         *system = 0;
1524                 else
1525                         *system = 0;
1526         }
1527
1528         return PMINFO_R_OK;
1529 }
1530
1531 API int pkgmgrinfo_pkginfo_is_readonly(pkgmgrinfo_pkginfo_h handle, bool *readonly)
1532 {
1533         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1534         retvm_if(readonly == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1535         char *val = NULL;
1536         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1537         val = (char *)info->manifest_info->readonly;
1538         if (val) {
1539                 if (strcasecmp(val, "true") == 0)
1540                         *readonly = 1;
1541                 else if (strcasecmp(val, "false") == 0)
1542                         *readonly = 0;
1543                 else
1544                         *readonly = 0;
1545         }
1546         return PMINFO_R_OK;
1547 }
1548
1549 API int pkgmgrinfo_pkginfo_is_update(pkgmgrinfo_pkginfo_h handle, bool *update)
1550 {
1551         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1552         retvm_if(update == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1553
1554         char *val = NULL;
1555         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1556         val = (char *)info->manifest_info->update;
1557         if (val) {
1558                 if (strcasecmp(val, "true") == 0)
1559                         *update = 1;
1560                 else if (strcasecmp(val, "false") == 0)
1561                         *update = 0;
1562                 else
1563                         *update = 1;
1564         }
1565         return PMINFO_R_OK;
1566 }
1567
1568 API int pkgmgrinfo_pkginfo_is_support_disable(pkgmgrinfo_pkginfo_h handle, bool *support_disable)
1569 {
1570         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1571         retvm_if(support_disable == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1572
1573         char *val = NULL;
1574         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1575         val = (char *)info->manifest_info->support_disable;
1576         if (val) {
1577                 if (strcasecmp(val, "true") == 0)
1578                         *support_disable = 1;
1579                 else if (strcasecmp(val, "false") == 0)
1580                         *support_disable = 0;
1581                 else
1582                         *support_disable = 1;
1583         }
1584         return PMINFO_R_OK;
1585 }
1586
1587 API int pkgmgrinfo_pkginfo_is_mother_package(pkgmgrinfo_pkginfo_h handle, bool *mother_package)
1588 {
1589         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1590         retvm_if(mother_package == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1591
1592         char *val = NULL;
1593         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1594         val = (char *)info->manifest_info->mother_package;
1595         if (val) {
1596                 if (strcasecmp(val, "true") == 0)
1597                         *mother_package = 1;
1598                 else if (strcasecmp(val, "false") == 0)
1599                         *mother_package = 0;
1600                 else
1601                         *mother_package = 1;
1602         }
1603         return PMINFO_R_OK;
1604 }
1605
1606 API int pkgmgrinfo_pkginfo_destroy_pkginfo(pkgmgrinfo_pkginfo_h handle)
1607 {
1608         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1609         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1610         __cleanup_pkginfo(info);
1611         return PMINFO_R_OK;
1612 }
1613
1614 API int pkgmgrinfo_pkginfo_filter_create(pkgmgrinfo_pkginfo_filter_h *handle)
1615 {
1616         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle output parameter is NULL\n");
1617         *handle = NULL;
1618         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)calloc(1, sizeof(pkgmgrinfo_filter_x));
1619         if (filter == NULL) {
1620                 _LOGE("Out of Memory!!!");
1621                 return PMINFO_R_ERROR;
1622         }
1623         *handle = filter;
1624         return PMINFO_R_OK;
1625 }
1626
1627 API int pkgmgrinfo_pkginfo_filter_destroy(pkgmgrinfo_pkginfo_filter_h handle)
1628 {
1629         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1630         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
1631         if (filter->list){
1632                 g_slist_foreach(filter->list, __destroy_each_node, NULL);
1633                 g_slist_free(filter->list);
1634         }
1635         FREE_AND_NULL(filter);
1636
1637         return PMINFO_R_OK;
1638 }
1639
1640 API int pkgmgrinfo_pkginfo_filter_add_int(pkgmgrinfo_pkginfo_filter_h handle,
1641                                 const char *property, const int value)
1642 {
1643         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1644         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1645         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
1646         char *val = NULL;
1647         GSList *link = NULL;
1648         int prop = -1;
1649         prop = _pminfo_pkginfo_convert_to_prop_int(property);
1650         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_INT ||
1651                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_INT) {
1652                 _LOGE("Invalid Integer Property\n");
1653                 return PMINFO_R_EINVAL;
1654         }
1655         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
1656         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
1657         if (node == NULL) {
1658                 _LOGE("Out of Memory!!!\n");
1659                 return PMINFO_R_ERROR;
1660         }
1661         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
1662         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
1663         if (val == NULL) {
1664                 _LOGE("Out of Memory\n");
1665                 FREE_AND_NULL(node);
1666                 return PMINFO_R_ERROR;
1667         }
1668         node->prop = prop;
1669         node->value = val;
1670         /*If API is called multiple times for same property, we should override the previous values.
1671         Last value set will be used for filtering.*/
1672         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1673         if (link)
1674                 filter->list = g_slist_delete_link(filter->list, link);
1675         filter->list = g_slist_append(filter->list, (gpointer)node);
1676         return PMINFO_R_OK;
1677
1678 }
1679
1680 API int pkgmgrinfo_pkginfo_filter_add_bool(pkgmgrinfo_pkginfo_filter_h handle,
1681                                 const char *property, const bool value)
1682 {
1683         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1684         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1685         char *val = NULL;
1686         GSList *link = NULL;
1687         int prop = -1;
1688         prop = _pminfo_pkginfo_convert_to_prop_bool(property);
1689         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_BOOL ||
1690                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_BOOL) {
1691                 _LOGE("Invalid Boolean Property\n");
1692                 return PMINFO_R_EINVAL;
1693         }
1694         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
1695         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
1696         if (node == NULL) {
1697                 _LOGE("Out of Memory!!!\n");
1698                 return PMINFO_R_ERROR;
1699         }
1700         if (value)
1701                 val = strndup("('true','True')", 15);
1702         else
1703                 val = strndup("('false','False')", 17);
1704         if (val == NULL) {
1705                 _LOGE("Out of Memory\n");
1706                 FREE_AND_NULL(node);
1707                 return PMINFO_R_ERROR;
1708         }
1709         node->prop = prop;
1710         node->value = val;
1711         /*If API is called multiple times for same property, we should override the previous values.
1712         Last value set will be used for filtering.*/
1713         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1714         if (link)
1715                 filter->list = g_slist_delete_link(filter->list, link);
1716         filter->list = g_slist_append(filter->list, (gpointer)node);
1717         return PMINFO_R_OK;
1718
1719 }
1720
1721 API int pkgmgrinfo_pkginfo_filter_add_string(pkgmgrinfo_pkginfo_filter_h handle,
1722                                 const char *property, const char *value)
1723 {
1724         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1725         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1726         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1727         char *val = NULL;
1728         GSList *link = NULL;
1729         int prop = -1;
1730         prop = _pminfo_pkginfo_convert_to_prop_str(property);
1731         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_STR ||
1732                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_STR) {
1733                 _LOGE("Invalid String Property\n");
1734                 return PMINFO_R_EINVAL;
1735         }
1736         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
1737         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
1738         if (node == NULL) {
1739                 _LOGE("Out of Memory!!!\n");
1740                 return PMINFO_R_ERROR;
1741         }
1742         if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_AUTO) == 0)
1743                 val = strndup("auto", PKG_STRING_LEN_MAX - 1);
1744         else if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_INTERNAL) == 0)
1745                 val = strndup("internal-only", PKG_STRING_LEN_MAX - 1);
1746         else if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_EXTERNAL) == 0)
1747                 val = strndup("prefer-external", PKG_STRING_LEN_MAX - 1);
1748         else if (strcmp(value, "installed_internal") == 0)
1749                 val = strndup("installed_internal", PKG_STRING_LEN_MAX - 1);
1750         else if (strcmp(value, "installed_external") == 0)
1751                 val = strndup("installed_external", PKG_STRING_LEN_MAX - 1);
1752         else
1753                 val = strndup(value, PKG_STRING_LEN_MAX - 1);
1754         if (val == NULL) {
1755                 _LOGE("Out of Memory\n");
1756                 FREE_AND_NULL(node);
1757                 return PMINFO_R_ERROR;
1758         }
1759         node->prop = prop;
1760         node->value = val;
1761         /*If API is called multiple times for same property, we should override the previous values.
1762         Last value set will be used for filtering.*/
1763         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1764         if (link)
1765                 filter->list = g_slist_delete_link(filter->list, link);
1766         filter->list = g_slist_append(filter->list, (gpointer)node);
1767         return PMINFO_R_OK;
1768
1769 }
1770
1771 API int pkgmgrinfo_pkginfo_filter_count(pkgmgrinfo_pkginfo_filter_h handle, int *count)
1772 {
1773         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1774         retvm_if(count == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1775
1776         int ret = 0;
1777         int filter_count = 0;
1778
1779         char pkgid[MAX_QUERY_LEN] = {0,};
1780         char pre_pkgid[MAX_QUERY_LEN] = {0,};
1781
1782         char *locale = NULL;
1783         char *condition = NULL;
1784         char query[MAX_QUERY_LEN] = {'\0'};
1785         char where[MAX_QUERY_LEN] = {'\0'};
1786         GSList *list;
1787
1788         pkgmgr_pkginfo_x *node = NULL;
1789         pkgmgr_pkginfo_x *pkginfo = NULL;
1790         pkgmgr_pkginfo_x *list_pkginfo = NULL;
1791         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
1792
1793         sqlite3 *pkginfo_db = NULL;
1794         sqlite3_stmt *stmt = NULL;
1795
1796
1797         /*open db*/
1798         ret = db_util_open(MANIFEST_DB, &pkginfo_db, 0);
1799         retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "db_util_open[%s] failed!", MANIFEST_DB);
1800
1801         /*Start constructing query*/
1802         locale = __convert_system_locale_to_manifest_locale();
1803         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_LIST_PACKAGE, DEFAULT_LOCALE, locale);
1804
1805         /*Get where clause*/
1806         for (list = filter->list; list; list = g_slist_next(list)) {
1807                 __get_filter_condition(list->data, &condition);
1808                 if (condition) {
1809                         strncat(where, condition, sizeof(where) - strlen(where) -1);
1810                         where[sizeof(where) - 1] = '\0';
1811                         FREE_AND_NULL(condition);
1812                 }
1813                 if (g_slist_next(list)) {
1814                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
1815                         where[sizeof(where) - 1] = '\0';
1816                 }
1817         }
1818
1819         if (strstr(where, "package_info.package_disable") == NULL) {
1820                 if (strlen(where) > 0) {
1821                         strncat(where, " and package_info.package_disable IN ('false','False')", sizeof(where) - strlen(where) - 1);
1822                         where[sizeof(where) - 1] = '\0';
1823                 }
1824         }
1825
1826         _LOGE("where = %s\n", where);
1827         if (strlen(where) > 0) {
1828                 strncat(query, where, sizeof(query) - strlen(query) - 1);
1829                 query[sizeof(query) - 1] = '\0';
1830         }
1831         _LOGE("query = %s\n", query);
1832
1833         list_pkginfo = calloc(1, sizeof(pkgmgr_pkginfo_x));
1834         tryvm_if(list_pkginfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
1835
1836         ret = sqlite3_prepare_v2(pkginfo_db, query, strlen(query), &stmt, NULL);
1837         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
1838
1839         while(1) {
1840                 ret = sqlite3_step(stmt);
1841                 if(ret == SQLITE_ROW) {
1842
1843                         memset(pkgid, 0, MAX_QUERY_LEN);
1844                         strncpy(pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
1845
1846                         if (strlen(pre_pkgid) != 0) {
1847                                 if (strcmp(pre_pkgid, pkgid) == 0) {
1848                                         __update_localed_label_for_list(stmt, list_pkginfo);
1849
1850                                         memset(pre_pkgid, 0, MAX_QUERY_LEN);
1851                                         strncpy(pre_pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
1852
1853                                         continue;
1854                                 } else {
1855                                         memset(pre_pkgid, 0, MAX_QUERY_LEN);
1856                                         strncpy(pre_pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
1857                                 }
1858                         } else {
1859                                 strncpy(pre_pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
1860                         }
1861
1862                         __get_pkginfo_for_list(stmt, list_pkginfo);
1863                 } else {
1864                         break;
1865                 }
1866         }
1867
1868         LISTHEAD(list_pkginfo, node);
1869
1870         for(node = node->next ; node ; node = node->next) {
1871                 pkginfo = node;
1872                 ret = __pkginfo_check_installed_storage(pkginfo);
1873                 if(ret < 0)
1874                         continue;
1875                 filter_count++;
1876         }
1877
1878         *count = filter_count;
1879         ret = PMINFO_R_OK;
1880
1881 catch:
1882         FREE_AND_NULL(locale);
1883         sqlite3_finalize(stmt);
1884         sqlite3_close(pkginfo_db);
1885
1886         __cleanup_list_pkginfo(list_pkginfo, node);
1887
1888         return ret;
1889 }
1890
1891 API int pkgmgrinfo_pkginfo_filter_foreach_pkginfo(pkgmgrinfo_pkginfo_filter_h handle,
1892                                 pkgmgrinfo_pkg_list_cb pkg_cb, void *user_data)
1893 {
1894         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1895         retvm_if(pkg_cb == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1896         char *locale = NULL;
1897         char *condition = NULL;
1898         char query[MAX_QUERY_LEN] = {'\0'};
1899         char where[MAX_QUERY_LEN] = {'\0'};
1900         GSList *list;
1901         int ret = 0;
1902
1903         char pkgid[MAX_QUERY_LEN] = {0,};
1904         char pre_pkgid[MAX_QUERY_LEN] = {0,};
1905
1906         pkgmgr_pkginfo_x *node = NULL;
1907         pkgmgr_pkginfo_x *pkginfo = NULL;
1908         pkgmgr_pkginfo_x *list_pkginfo = NULL;
1909         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
1910
1911         sqlite3 *pkginfo_db = NULL;
1912         sqlite3_stmt *stmt = NULL;
1913
1914         /*open db*/
1915         ret = db_util_open(MANIFEST_DB, &pkginfo_db, 0);
1916         retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "db_util_open[%s] failed!", MANIFEST_DB);
1917
1918         /*Start constructing query*/
1919         locale = __convert_system_locale_to_manifest_locale();
1920         retvm_if(locale == NULL, PMINFO_R_ERROR, "locale is NULL");
1921         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_LIST_PACKAGE, DEFAULT_LOCALE, locale);
1922
1923         /*Get where clause*/
1924         for (list = filter->list; list; list = g_slist_next(list)) {
1925                 __get_filter_condition(list->data, &condition);
1926                 if (condition) {
1927                         strncat(where, condition, sizeof(where) - strlen(where) -1);
1928                         where[sizeof(where) - 1] = '\0';
1929
1930                         FREE_AND_NULL(condition);
1931                 }
1932                 if (g_slist_next(list)) {
1933                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
1934                         where[sizeof(where) - 1] = '\0';
1935                 }
1936         }
1937
1938         if (strstr(where, "package_info.package_disable") == NULL) {
1939                 if (strlen(where) > 0) {
1940                         strncat(where, " and package_info.package_disable IN ('false','False')", sizeof(where) - strlen(where) - 1);
1941                         where[sizeof(where) - 1] = '\0';
1942                 }
1943         }
1944
1945         _LOGE("where = %s\n", where);
1946
1947         if (strlen(where) > 0) {
1948                 strncat(query, where, sizeof(query) - strlen(query) - 1);
1949                 query[sizeof(query) - 1] = '\0';
1950         }
1951         _LOGE("query = %s\n", query);
1952
1953         list_pkginfo = calloc(1, sizeof(pkgmgr_pkginfo_x));
1954         tryvm_if(list_pkginfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
1955
1956         ret = sqlite3_prepare_v2(pkginfo_db, query, strlen(query), &stmt, NULL);
1957         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
1958
1959         while(1) {
1960                 ret = sqlite3_step(stmt);
1961                 if(ret == SQLITE_ROW) {
1962
1963                         memset(pkgid, 0, MAX_QUERY_LEN);
1964                         strncpy(pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
1965
1966                         if (strlen(pre_pkgid) != 0) {
1967                                 if (strcmp(pre_pkgid, pkgid) == 0) {
1968                                         __update_localed_label_for_list(stmt, list_pkginfo);
1969
1970                                         memset(pre_pkgid, 0, MAX_QUERY_LEN);
1971                                         strncpy(pre_pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
1972
1973                                         continue;
1974                                 } else {
1975                                         memset(pre_pkgid, 0, MAX_QUERY_LEN);
1976                                         strncpy(pre_pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
1977                                 }
1978                         } else {
1979                                 strncpy(pre_pkgid, (const char *)sqlite3_column_text(stmt, 0), MAX_QUERY_LEN - 1);
1980                         }
1981
1982                         __get_pkginfo_for_list(stmt, list_pkginfo);
1983                 } else {
1984                         break;
1985                 }
1986         }
1987
1988         LISTHEAD(list_pkginfo, node);
1989
1990         for(node = node->next ; node ; node = node->next) {
1991                 pkginfo = node;
1992                 pkginfo->locale = strdup(locale);
1993
1994                 ret = __pkginfo_check_installed_storage(pkginfo);
1995                 if(ret < 0)
1996                         continue;
1997
1998                 ret = pkg_cb( (void *)pkginfo, user_data);
1999                 if(ret < 0)
2000                         break;
2001         }
2002         ret = PMINFO_R_OK;
2003
2004 catch:
2005         FREE_AND_NULL(locale);
2006         sqlite3_finalize(stmt);
2007         sqlite3_close(pkginfo_db);
2008         __cleanup_list_pkginfo(list_pkginfo, node);
2009
2010         return ret;
2011 }
2012
2013 API int pkgmgrinfo_pkginfo_privilege_filter_foreach(const char *privilege,
2014                         pkgmgrinfo_pkg_list_cb pkg_list_cb, void *user_data)
2015 {
2016         retvm_if(privilege == NULL, PMINFO_R_EINVAL, "privilege is NULL\n");
2017         retvm_if(pkg_list_cb == NULL, PMINFO_R_EINVAL, "callback function is NULL\n");
2018
2019         int ret = PMINFO_R_OK;
2020         char *query = NULL;
2021
2022         sqlite3 *pkginfo_db = NULL;
2023         sqlite3_stmt *stmt = NULL;
2024
2025         pkgmgr_pkginfo_x *node = NULL;
2026         pkgmgr_pkginfo_x *pkginfo = NULL;
2027         pkgmgr_pkginfo_x *list_pkginfo = NULL;
2028
2029         /*open db*/
2030         ret = db_util_open(MANIFEST_DB, &pkginfo_db, 0);
2031         retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "db_util_open[%s] failed!", MANIFEST_DB);
2032
2033         /*Start constructing query*/
2034         query = sqlite3_mprintf("select package_info.* from package_info LEFT OUTER JOIN package_privilege_info " \
2035                         "ON package_privilege_info.package=package_info.package " \
2036                         "where package_privilege_info.privilege=%Q and package_info.package_disable='false'", privilege);
2037
2038         /*prepare query*/
2039         ret = sqlite3_prepare_v2(pkginfo_db, query, strlen(query), &stmt, NULL);
2040         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
2041
2042         /*calloc pkginfo_x*/
2043         list_pkginfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
2044         tryvm_if(list_pkginfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
2045
2046         /*step query*/
2047         while(1) {
2048                 ret = sqlite3_step(stmt);
2049                 if(ret == SQLITE_ROW) {
2050                         __get_pkginfo_for_list(stmt, list_pkginfo);
2051                 } else {
2052                         break;
2053                 }
2054         }
2055
2056         LISTHEAD(list_pkginfo, node);
2057
2058         for(node = node->next; node ; node = node->next) {
2059                 pkginfo = node;
2060                 ret = pkg_list_cb( (void *)pkginfo, user_data);
2061                 if(ret < 0)
2062                         break;
2063         }
2064         ret = PMINFO_R_OK;
2065
2066 catch:
2067         sqlite3_free(query);
2068         sqlite3_finalize(stmt);
2069         sqlite3_close(pkginfo_db);
2070
2071         __cleanup_list_pkginfo(list_pkginfo, node);
2072
2073         return ret;
2074 }