Add filter prop for package privilege
[platform/core/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 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <ctype.h>
29 #include <sys/smack.h>
30 #include <linux/limits.h>
31 #include <libgen.h>
32 #include <sys/stat.h>
33
34 #include <libxml/parser.h>
35 #include <libxml/xmlreader.h>
36 #include <libxml/xmlschemas.h>
37 #include <sqlite3.h>
38 #include <glib.h>
39
40 #include "pkgmgr_parser.h"
41 #include "pkgmgrinfo_basic.h"
42 #include "pkgmgrinfo_private.h"
43 #include "pkgmgrinfo_debug.h"
44 #include "pkgmgr-info.h"
45 #include "pkgmgr_parser_db.h"
46 #include "pkgmgr_parser_internal.h"
47
48 #define FILTER_QUERY_COUNT_PACKAGE      "select count(DISTINCT package_info.package) " \
49                                 "from package_info LEFT OUTER JOIN package_localized_info " \
50                                 "ON package_info.package=package_localized_info.package " \
51                                 "and package_localized_info.package_locale='%s' where "
52
53
54 static int _pkginfo_get_pkg(const char *pkgid, const char *locale,
55                 pkgmgr_pkginfo_x **pkginfo);
56 static char *_get_filtered_query(const char *query_raw,
57                 pkgmgrinfo_filter_x *filter);
58
59 static gint __compare_func(gconstpointer data1, gconstpointer data2)
60 {
61         pkgmgrinfo_node_x *node1 = (pkgmgrinfo_node_x*)data1;
62         pkgmgrinfo_node_x *node2 = (pkgmgrinfo_node_x*)data2;
63         if (node1->prop == node2->prop)
64                 return 0;
65         else if (node1->prop > node2->prop)
66                 return 1;
67         else
68                 return -1;
69 }
70
71 static int __count_cb(void *data, int ncols, char **coltxt, char **colname)
72 {
73         int *p = (int*)data;
74         *p = atoi(coltxt[0]);
75         _LOGE("count value is %d\n", *p);
76         return 0;
77 }
78
79 static void __destroy_each_node(gpointer data, gpointer user_data)
80 {
81         ret_if(data == NULL);
82         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)data;
83         if (node->value) {
84                 free(node->value);
85                 node->value = NULL;
86         }
87         if (node->key) {
88                 free(node->key);
89                 node->key = NULL;
90         }
91         free(node);
92         node = NULL;
93 }
94
95 static void __cleanup_pkginfo(pkgmgr_pkginfo_x *data)
96 {
97         ret_if(data == NULL);
98         if (data->locale){
99                 free((void *)data->locale);
100                 data->locale = NULL;
101         }
102
103         pkgmgrinfo_basic_free_package(data->pkg_info);
104         free((void *)data);
105         data = NULL;
106         return;
107 }
108
109 static int __child_element(xmlTextReaderPtr reader, int depth)
110 {
111         int ret = xmlTextReaderRead(reader);
112         int cur = xmlTextReaderDepth(reader);
113         while (ret == 1) {
114
115                 switch (xmlTextReaderNodeType(reader)) {
116                 case XML_READER_TYPE_ELEMENT:
117                         if (cur == depth + 1)
118                                 return 1;
119                         break;
120                 case XML_READER_TYPE_TEXT:
121                         /*text is handled by each function separately*/
122                         if (cur == depth + 1)
123                                 return 0;
124                         break;
125                 case XML_READER_TYPE_END_ELEMENT:
126                         if (cur == depth)
127                                 return 0;
128                         break;
129                 default:
130                         if (cur <= depth)
131                                 return 0;
132                         break;
133                 }
134                 ret = xmlTextReaderRead(reader);
135                 cur = xmlTextReaderDepth(reader);
136         }
137         return ret;
138 }
139
140 long long _pkgmgr_calculate_dir_size(char *dirname)
141 {
142         long long total = 0;
143         long long ret = 0;
144         int q = 0; /*quotient*/
145         int r = 0; /*remainder*/
146         DIR *dp = NULL;
147         struct dirent *ep = NULL;
148         struct stat fileinfo;
149         char abs_filename[FILENAME_MAX] = { 0, };
150         retvm_if(dirname == NULL, PMINFO_R_ERROR, "dirname is NULL");
151
152         dp = opendir(dirname);
153         if (dp != NULL) {
154                 while ((ep = readdir(dp)) != NULL) {
155                         if (!strcmp(ep->d_name, ".") ||
156                                 !strcmp(ep->d_name, "..")) {
157                                 continue;
158                         }
159                         snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname,
160                                  ep->d_name);
161                         if (lstat(abs_filename, &fileinfo) < 0)
162                                 perror(abs_filename);
163                         else {
164                                 if (S_ISDIR(fileinfo.st_mode)) {
165                                         total += fileinfo.st_size;
166                                         if (strcmp(ep->d_name, ".")
167                                             && strcmp(ep->d_name, "..")) {
168                                                 ret = _pkgmgr_calculate_dir_size
169                                                     (abs_filename);
170                                                 total = total + ret;
171                                         }
172                                 } else if (S_ISLNK(fileinfo.st_mode)) {
173                                         continue;
174                                 } else {
175                                         /*It is a file. Calculate the actual
176                                         size occupied (in terms of 4096 blocks)*/
177                                 q = (fileinfo.st_size / BLOCK_SIZE);
178                                 r = (fileinfo.st_size % BLOCK_SIZE);
179                                 if (r) {
180                                         q = q + 1;
181                                 }
182                                 total += q * BLOCK_SIZE;
183                                 }
184                         }
185                 }
186                 (void)closedir(dp);
187         } else {
188                 _LOGE("Couldn't open the directory\n");
189                 return -1;
190         }
191         return total;
192
193 }
194
195 static GSList *_pkginfo_get_filtered_list(const char *locale,
196                 pkgmgrinfo_filter_x *filter)
197 {
198         static const char query_raw[] =
199                 "SELECT DISTINCT package_info.package FROM package_info"
200                 " LEFT OUTER JOIN package_localized_info"
201                 "  ON package_info.package=package_localized_info.package"
202                 "  AND package_localized_info.package_locale=%Q "
203                 " LEFT OUTER JOIN package_privilege_info"
204                 "  ON package_info.package=package_privilege_info.package";
205         int ret;
206         char *query;
207         char *query_localized;
208         sqlite3_stmt *stmt;
209         GSList *list = NULL;
210         char *pkgid;
211
212         query = _get_filtered_query(query_raw, filter);
213         if (query == NULL)
214                 return NULL;
215         query_localized = sqlite3_mprintf(query, locale);
216         free(query);
217         if (query_localized == NULL)
218                 return NULL;
219
220         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query_localized,
221                         strlen(query_localized), &stmt, NULL);
222         sqlite3_free(query_localized);
223         if (ret != SQLITE_OK) {
224                 LOGE("prepare failed: %s", sqlite3_errmsg(GET_DB(manifest_db)));
225                 return NULL;
226         }
227
228         while (sqlite3_step(stmt) == SQLITE_ROW) {
229                 _save_column_str(stmt, 0, (const char **)&pkgid);
230                 list = g_slist_append(list, pkgid);
231         }
232
233         sqlite3_finalize(stmt);
234
235         return list;
236 }
237
238 static int _pkginfo_get_filtered_foreach_pkginfo(pkgmgrinfo_filter_x *filter,
239                 pkgmgrinfo_pkg_list_cb pkg_list_cb, void *user_data, uid_t uid)
240 {
241         pkgmgr_pkginfo_x *info;
242         GSList *list;
243         GSList *tmp;
244         char *pkgid;
245         char *locale;
246         int stop = 0;
247
248         if (__open_manifest_db(uid) < 0)
249                 return PMINFO_R_ERROR;
250
251         locale = _get_system_locale();
252         if (locale == NULL) {
253                 __close_manifest_db();
254                 return PMINFO_R_ERROR;
255         }
256
257         list = _pkginfo_get_filtered_list(locale, filter);
258         if (list == NULL) {
259                 free(locale);
260                 __close_manifest_db();
261                 return PMINFO_R_OK;
262         }
263
264         for (tmp = list; tmp; tmp = tmp->next) {
265                 pkgid = (char *)tmp->data;
266                 if (stop == 0) {
267                         if (_pkginfo_get_pkg(pkgid, locale, &info)) {
268                                 free(pkgid);
269                                 continue;
270                         }
271                         info->uid = uid;
272                         if (pkg_list_cb(info, user_data) < 0)
273                                 stop = 1;
274                         pkgmgrinfo_pkginfo_destroy_pkginfo(info);
275                 }
276                 free(pkgid);
277         }
278
279         free(locale);
280         g_slist_free(list);
281         __close_manifest_db();
282
283         return PMINFO_R_OK;
284 }
285
286 API int pkgmgrinfo_pkginfo_get_usr_list(pkgmgrinfo_pkg_list_cb pkg_list_cb,
287                 void *user_data, uid_t uid)
288 {
289         if (pkg_list_cb == NULL) {
290                 LOGE("invalid parameter");
291                 return PMINFO_R_EINVAL;
292         }
293
294         return _pkginfo_get_filtered_foreach_pkginfo(NULL, pkg_list_cb,
295                         user_data, uid);
296 }
297
298 API int pkgmgrinfo_pkginfo_get_list(pkgmgrinfo_pkg_list_cb pkg_list_cb, void *user_data)
299 {
300         return pkgmgrinfo_pkginfo_get_usr_list(pkg_list_cb, user_data, GLOBAL_USER);
301 }
302
303 static int _pkginfo_get_author(const char *pkgid, author_x **author)
304 {
305         static const char query_raw[] =
306                 "SELECT author_name, author_email, author_href "
307                 "FROM package_info WHERE package=%Q";
308         int ret;
309         char *query;
310         sqlite3_stmt *stmt;
311         int idx = 0;
312         author_x *info;
313
314         query = sqlite3_mprintf(query_raw, pkgid);
315         if (query == NULL) {
316                 LOGE("out of memory");
317                 return PMINFO_R_ERROR;
318         }
319
320         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query),
321                         &stmt, NULL);
322         sqlite3_free(query);
323         if (ret != SQLITE_OK) {
324                 LOGE("prepare failed: %s", sqlite3_errmsg(GET_DB(manifest_db)));
325                 return PMINFO_R_ERROR;
326         }
327
328         if (sqlite3_step(stmt) == SQLITE_ERROR) {
329                 LOGE("step error: %s", sqlite3_errmsg(GET_DB(manifest_db)));
330                 sqlite3_finalize(stmt);
331                 return PMINFO_R_ERROR;
332         }
333
334         /* one author per one package */
335         info = calloc(1, sizeof(author_x));
336         if (info == NULL) {
337                 LOGE("out of memory");
338                 sqlite3_finalize(stmt);
339                 return PMINFO_R_ERROR;
340         }
341
342         _save_column_str(stmt, idx++, &info->text);
343         _save_column_str(stmt, idx++, &info->email);
344         _save_column_str(stmt, idx++, &info->href);
345
346         *author = info;
347
348         sqlite3_finalize(stmt);
349
350         return PMINFO_R_OK;
351 }
352
353 static int _pkginfo_get_label(const char *pkgid, const char *locale,
354                 label_x **label)
355 {
356         static const char query_raw[] =
357                 "SELECT package_label, package_locale "
358                 "FROM package_localized_info "
359                 "WHERE package=%Q AND package_locale IN (%Q, %Q)";
360         int ret;
361         char *query;
362         sqlite3_stmt *stmt;
363         int idx;
364         label_x *info;
365
366         query = sqlite3_mprintf(query_raw, pkgid, locale, DEFAULT_LOCALE);
367         if (query == NULL) {
368                 LOGE("out of memory");
369                 return PMINFO_R_ERROR;
370         }
371
372         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query),
373                         &stmt, NULL);
374         sqlite3_free(query);
375         if (ret != SQLITE_OK) {
376                 LOGE("prepare failed: %s", sqlite3_errmsg(GET_DB(manifest_db)));
377                 return PMINFO_R_ERROR;
378         }
379
380         while (sqlite3_step(stmt) == SQLITE_ROW) {
381                 info = calloc(1, sizeof(label_x));
382                 if (info == NULL) {
383                         LOGE("out of memory");
384                         sqlite3_finalize(stmt);
385                         if (*label) {
386                                 LISTHEAD(*label, info);
387                                 *label = info;
388                         }
389                         return PMINFO_R_ERROR;
390                 }
391                 idx = 0;
392                 _save_column_str(stmt, idx++, &info->text);
393                 _save_column_str(stmt, idx++, &info->lang);
394                 LISTADD(*label, info);
395         }
396
397         if (*label) {
398                 LISTHEAD(*label, info);
399                 *label = info;
400         }
401
402         sqlite3_finalize(stmt);
403
404         return PMINFO_R_OK;
405 }
406
407 static int _pkginfo_get_icon(const char *pkgid, const char *locale,
408                 icon_x **icon)
409 {
410         static const char query_raw[] =
411                 "SELECT package_icon, package_locale "
412                 "FROM package_localized_info "
413                 "WHERE package=%Q AND package_locale IN (%Q, %Q)";
414         int ret;
415         char *query;
416         sqlite3_stmt *stmt;
417         int idx;
418         icon_x *info;
419
420         query = sqlite3_mprintf(query_raw, pkgid, locale, DEFAULT_LOCALE);
421         if (query == NULL) {
422                 LOGE("out of memory");
423                 return PMINFO_R_ERROR;
424         }
425
426         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query),
427                         &stmt, NULL);
428         sqlite3_free(query);
429         if (ret != SQLITE_OK) {
430                 LOGE("prepare failed: %s", sqlite3_errmsg(GET_DB(manifest_db)));
431                 return PMINFO_R_ERROR;
432         }
433
434         while (sqlite3_step(stmt) == SQLITE_ROW) {
435                 info = calloc(1, sizeof(icon_x));
436                 if (info == NULL) {
437                         LOGE("out of memory");
438                         sqlite3_finalize(stmt);
439                         if (*icon) {
440                                 LISTHEAD(*icon, info);
441                                 *icon = info;
442                         }
443                         return PMINFO_R_ERROR;
444                 }
445                 idx = 0;
446                 _save_column_str(stmt, idx++, &info->text);
447                 _save_column_str(stmt, idx++, &info->lang);
448                 LISTADD(*icon, info);
449         }
450
451         if (*icon) {
452                 LISTHEAD(*icon, info);
453                 *icon = info;
454         }
455
456         sqlite3_finalize(stmt);
457
458         return PMINFO_R_OK;
459 }
460
461 static int _pkginfo_get_description(const char *pkgid, const char *locale,
462                 description_x **description)
463 {
464         static const char query_raw[] =
465                 "SELECT package_description, package_locale "
466                 "FROM package_localized_info "
467                 "WHERE package=%Q AND package_locale IN (%Q, %Q)";
468         int ret;
469         char *query;
470         sqlite3_stmt *stmt;
471         int idx;
472         description_x *info;
473
474         query = sqlite3_mprintf(query_raw, pkgid, locale, DEFAULT_LOCALE);
475         if (query == NULL) {
476                 LOGE("out of memory");
477                 return PMINFO_R_ERROR;
478         }
479
480         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query),
481                         &stmt, NULL);
482         sqlite3_free(query);
483         if (ret != SQLITE_OK) {
484                 LOGE("prepare failed: %s", sqlite3_errmsg(GET_DB(manifest_db)));
485                 return PMINFO_R_ERROR;
486         }
487
488         while (sqlite3_step(stmt) == SQLITE_ROW) {
489                 info = calloc(1, sizeof(description_x));
490                 if (info == NULL) {
491                         LOGE("out of memory");
492                         sqlite3_finalize(stmt);
493                         if (*description) {
494                                 LISTHEAD(*description, info);
495                                 *description = info;
496                         }
497                         return PMINFO_R_ERROR;
498                 }
499                 idx = 0;
500                 _save_column_str(stmt, idx++, &info->text);
501                 _save_column_str(stmt, idx++, &info->lang);
502                 LISTADD(*description, info);
503         }
504
505         if (*description) {
506                 LISTHEAD(*description, info);
507                 *description = info;
508         }
509
510         sqlite3_finalize(stmt);
511
512         return PMINFO_R_OK;
513 }
514
515 static int _pkginfo_get_privilege(const char *pkgid, privileges_x **privileges)
516 {
517         static const char query_raw[] =
518                 "SELECT privilege FROM package_privilege_info WHERE package=%Q";
519         int ret;
520         char *query;
521         sqlite3_stmt *stmt;
522         privileges_x *p;
523         privilege_x *info;
524
525         /* privilege list should stored in privileges_x... */
526         p = calloc(1, sizeof(privileges_x));
527         if (p == NULL) {
528                 LOGE("out of memory");
529                 return PMINFO_R_ERROR;
530         }
531         *privileges = p;
532
533         query = sqlite3_mprintf(query_raw, pkgid);
534         if (query == NULL) {
535                 LOGE("out of memory");
536                 free(p);
537                 return PMINFO_R_ERROR;
538         }
539
540         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query),
541                         &stmt, NULL);
542         sqlite3_free(query);
543         if (ret != SQLITE_OK) {
544                 LOGE("prepare failed: %s", sqlite3_errmsg(GET_DB(manifest_db)));
545                 free(p);
546                 return PMINFO_R_ERROR;
547         }
548
549         while (sqlite3_step(stmt) == SQLITE_ROW) {
550                 info = calloc(1, sizeof(privilege_x));
551                 if (info == NULL) {
552                         LOGE("out of memory");
553                         sqlite3_finalize(stmt);
554                         if (p->privilege) {
555                                 LISTHEAD(p->privilege, info);
556                                 p->privilege = info;
557                         }
558                         return PMINFO_R_ERROR;
559                 }
560                 _save_column_str(stmt, 0, &info->text);
561                 LISTADD(p->privilege, info);
562         }
563
564         if (p->privilege) {
565                 LISTHEAD(p->privilege, info);
566                 p->privilege = info;
567         }
568
569         sqlite3_finalize(stmt);
570
571         return PMINFO_R_OK;
572 }
573
574 static char *_get_filtered_query(const char *query_raw,
575                 pkgmgrinfo_filter_x *filter)
576 {
577         char buf[MAX_QUERY_LEN] = { 0, };
578         char *condition;
579         size_t len;
580         GSList *list;
581         GSList *head = NULL;
582
583         if (filter)
584                 head = filter->list;
585
586         strncat(buf, query_raw, MAX_QUERY_LEN - 1);
587         len = strlen(buf);
588         for (list = head; list; list = list->next) {
589                 /* TODO: revise condition getter function */
590                 __get_filter_condition(list->data, &condition);
591                 if (condition == NULL)
592                         continue;
593                 if (buf[strlen(query_raw)] == '\0') {
594                         len += strlen(" WHERE ");
595                         strncat(buf, " WHERE ", MAX_QUERY_LEN - len - 1);
596                 } else {
597                         len += strlen(" AND ");
598                         strncat(buf, " AND ", MAX_QUERY_LEN -len - 1);
599                 }
600                 len += strlen(condition);
601                 strncat(buf, condition, sizeof(buf) - len - 1);
602                 free(condition);
603                 condition = NULL;
604         }
605
606         return strdup(buf);
607 }
608
609 static int _pkginfo_get_pkg(const char *pkgid, const char *locale,
610                 pkgmgr_pkginfo_x **pkginfo)
611 {
612         static const char query_raw[] =
613                 "SELECT for_all_users, package, package_version, "
614                 "install_location, package_removable, package_preload, "
615                 "package_readonly, package_update, package_appsetting, "
616                 "package_system, package_type, package_size, installed_time, "
617                 "installed_storage, storeclient_id, mainapp_id, package_url, "
618                 "root_path, csc_path, package_nodisplay, package_api_version "
619                 "FROM package_info WHERE package=%Q";
620         int ret;
621         char *query;
622         sqlite3_stmt *stmt;
623         int idx;
624         pkgmgr_pkginfo_x *info;
625         package_x *pkg;
626
627         query = sqlite3_mprintf(query_raw, pkgid);
628         if (query == NULL) {
629                 LOGE("out of memory");
630                 return PMINFO_R_ERROR;
631         }
632
633         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query),
634                         &stmt, NULL);
635         sqlite3_free(query);
636         if (ret != SQLITE_OK) {
637                 LOGE("prepare failed: %s", sqlite3_errmsg(GET_DB(manifest_db)));
638                 return PMINFO_R_ERROR;
639         }
640
641         ret = sqlite3_step(stmt);
642         if (ret == SQLITE_DONE) {
643                 LOGE("cannot find pkg");
644                 sqlite3_finalize(stmt);
645                 return PMINFO_R_ENOENT;
646         } else if (ret != SQLITE_ROW) {
647                 LOGE("step failed: %s", sqlite3_errmsg(GET_DB(manifest_db)));
648                 sqlite3_finalize(stmt);
649                 return PMINFO_R_ERROR;
650         }
651
652         pkg = calloc(1, sizeof(package_x));
653         if (pkg == NULL) {
654                 LOGE("out of memory");
655                 sqlite3_finalize(stmt);
656                 return PMINFO_R_ERROR;
657         }
658         idx = 0;
659         _save_column_str(stmt, idx++, &pkg->for_all_users);
660         _save_column_str(stmt, idx++, &pkg->package);
661         _save_column_str(stmt, idx++, &pkg->version);
662         _save_column_str(stmt, idx++, &pkg->installlocation);
663         _save_column_str(stmt, idx++, &pkg->removable);
664         _save_column_str(stmt, idx++, &pkg->preload);
665         _save_column_str(stmt, idx++, &pkg->readonly);
666         _save_column_str(stmt, idx++, &pkg->update);
667         _save_column_str(stmt, idx++, &pkg->appsetting);
668         _save_column_str(stmt, idx++, &pkg->system);
669         _save_column_str(stmt, idx++, &pkg->type);
670         _save_column_str(stmt, idx++, &pkg->package_size);
671         _save_column_str(stmt, idx++, &pkg->installed_time);
672         _save_column_str(stmt, idx++, &pkg->installed_storage);
673         _save_column_str(stmt, idx++, &pkg->storeclient_id);
674         _save_column_str(stmt, idx++, &pkg->mainapp_id);
675         _save_column_str(stmt, idx++, &pkg->package_url);
676         _save_column_str(stmt, idx++, &pkg->root_path);
677         _save_column_str(stmt, idx++, &pkg->csc_path);
678         _save_column_str(stmt, idx++, &pkg->nodisplay_setting);
679         _save_column_str(stmt, idx++, &pkg->api_version);
680
681         if (_pkginfo_get_author(pkg->package, &pkg->author)) {
682                 pkgmgrinfo_basic_free_package(pkg);
683                 sqlite3_finalize(stmt);
684                 return PMINFO_R_ERROR;
685         }
686
687         if (_pkginfo_get_label(pkg->package, locale, &pkg->label)) {
688                 pkgmgrinfo_basic_free_package(pkg);
689                 sqlite3_finalize(stmt);
690                 return PMINFO_R_ERROR;
691         }
692
693         if (_pkginfo_get_icon(pkg->package, locale, &pkg->icon)) {
694                 pkgmgrinfo_basic_free_package(pkg);
695                 sqlite3_finalize(stmt);
696                 return PMINFO_R_ERROR;
697         }
698
699         if (_pkginfo_get_description(pkg->package, locale,
700                                 &pkg->description)) {
701                 pkgmgrinfo_basic_free_package(pkg);
702                 sqlite3_finalize(stmt);
703                 return PMINFO_R_ERROR;
704         }
705
706         if (_pkginfo_get_privilege(pkg->package, &pkg->privileges)) {
707                 pkgmgrinfo_basic_free_package(pkg);
708                 sqlite3_finalize(stmt);
709                         return PMINFO_R_ERROR;
710         }
711
712         info = calloc(1, sizeof(pkgmgr_pkginfo_x));
713         if (info == NULL) {
714                 LOGE("out of memory");
715                 pkgmgrinfo_basic_free_package(pkg);
716                 sqlite3_finalize(stmt);
717                 return PMINFO_R_ERROR;
718         }
719
720         info->pkg_info = pkg;
721         info->locale = strdup(locale);
722         *pkginfo = info;
723
724         sqlite3_finalize(stmt);
725
726         return PMINFO_R_OK;
727 }
728
729 API int pkgmgrinfo_pkginfo_get_usr_pkginfo(const char *pkgid, uid_t uid,
730                 pkgmgrinfo_pkginfo_h *handle)
731 {
732         pkgmgr_pkginfo_x *pkginfo = NULL;
733         char *locale;
734
735         if (pkgid == NULL || handle == NULL) {
736                 LOGE("invalid parameter");
737                 return PMINFO_R_EINVAL;
738         }
739
740         if (__open_manifest_db(uid) < 0)
741                 return PMINFO_R_ERROR;
742
743
744         locale = _get_system_locale();
745         if (locale == NULL) {
746                 __close_manifest_db();
747                 return PMINFO_R_ERROR;
748         }
749
750         if (_pkginfo_get_pkg(pkgid, locale, &pkginfo)) {
751                 LOGE("failed to get pkginfo of %s for user %d", pkgid, uid);
752                 free(locale);
753                 __close_manifest_db();
754                 return PMINFO_R_ERROR;
755         }
756
757         free(locale);
758         pkginfo->uid = uid;
759         *handle = pkginfo;
760
761         __close_manifest_db();
762
763         return PMINFO_R_OK;
764 }
765
766 API int pkgmgrinfo_pkginfo_get_pkginfo(const char *pkgid, pkgmgrinfo_pkginfo_h *handle)
767 {
768         return pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, GLOBAL_USER, handle);
769 }
770
771 API int pkgmgrinfo_pkginfo_get_pkgname(pkgmgrinfo_pkginfo_h handle, char **pkg_name)
772 {
773         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
774
775         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
776         retvm_if(pkg_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
777
778         if (info->pkg_info == NULL || info->pkg_info->package == NULL)
779                 return PMINFO_R_ERROR;
780
781         *pkg_name = (char *)info->pkg_info->package;
782
783         return PMINFO_R_OK;
784 }
785
786 API int pkgmgrinfo_pkginfo_get_pkgid(pkgmgrinfo_pkginfo_h handle, char **pkgid)
787 {
788         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
789
790         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
791         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
792
793         if (info->pkg_info == NULL || info->pkg_info->package == NULL)
794                 return PMINFO_R_ERROR;
795
796         *pkgid = (char *)info->pkg_info->package;
797
798         return PMINFO_R_OK;
799 }
800
801 API int pkgmgrinfo_pkginfo_get_type(pkgmgrinfo_pkginfo_h handle, char **type)
802 {
803         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
804
805         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
806         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
807
808         if (info->pkg_info == NULL || info->pkg_info->type == NULL)
809                 return PMINFO_R_ERROR;
810
811         *type = (char *)info->pkg_info->type;
812
813         return PMINFO_R_OK;
814 }
815
816 API int pkgmgrinfo_pkginfo_get_version(pkgmgrinfo_pkginfo_h handle, char **version)
817 {
818         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
819
820         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
821         retvm_if(version == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
822
823         if (info->pkg_info == NULL || info->pkg_info->version == NULL)
824                 return PMINFO_R_ERROR;
825
826         *version = (char *)info->pkg_info->version;
827
828         return PMINFO_R_OK;
829 }
830
831 API int pkgmgrinfo_pkginfo_get_install_location(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_install_location *location)
832 {
833         char *val;
834         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
835
836         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
837         retvm_if(location == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
838
839         if (info->pkg_info == NULL || info->pkg_info->installlocation == NULL)
840                 return PMINFO_R_ERROR;
841
842         val = (char *)info->pkg_info->installlocation;
843         if (strcmp(val, "internal-only") == 0)
844                 *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
845         else if (strcmp(val, "prefer-external") == 0)
846                 *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
847         else
848                 *location = PMINFO_INSTALL_LOCATION_AUTO;
849
850         return PMINFO_R_OK;
851 }
852
853 API int pkgmgrinfo_pkginfo_get_package_size(pkgmgrinfo_pkginfo_h handle, int *size)
854 {
855         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
856
857         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
858         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
859
860         if (info->pkg_info == NULL || info->pkg_info->package_size == NULL)
861                 return PMINFO_R_ERROR;
862
863         *size = atoi((char *)info->pkg_info->package_size);
864
865         return PMINFO_R_OK;
866 }
867
868 API int pkgmgrinfo_pkginfo_get_total_size(pkgmgrinfo_pkginfo_h handle, int *size)
869 {
870         char *pkgid;
871         char device_path[PKG_STRING_LEN_MAX] = { '\0', };
872         long long rw_size = 0;
873         long long ro_size = 0;
874         long long tmp_size = 0;
875         long long total_size = 0;
876         struct stat fileinfo;
877         int ret;
878
879         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
880         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
881
882         ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
883         if (ret < 0)
884                 return PMINFO_R_ERROR;
885
886         /* RW area */
887         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/bin", PKG_RW_PATH, pkgid);
888         if (lstat(device_path, &fileinfo) == 0) {
889                 if (!S_ISLNK(fileinfo.st_mode)) {
890                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
891                         if (tmp_size > 0)
892                                 rw_size += tmp_size;
893                 }
894         }
895
896         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/info", PKG_RW_PATH, pkgid);
897         if (lstat(device_path, &fileinfo) == 0) {
898                 if (!S_ISLNK(fileinfo.st_mode)) {
899                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
900                         if (tmp_size > 0)
901                         rw_size += tmp_size;
902                 }
903         }
904
905         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/res", PKG_RW_PATH, pkgid);
906         if (lstat(device_path, &fileinfo) == 0) {
907                 if (!S_ISLNK(fileinfo.st_mode)) {
908                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
909                         if (tmp_size > 0)
910                         rw_size += tmp_size;
911                 }
912         }
913
914         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RW_PATH, pkgid);
915         if (lstat(device_path, &fileinfo) == 0) {
916                 if (!S_ISLNK(fileinfo.st_mode)) {
917                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
918                         if (tmp_size > 0)
919                                 rw_size += tmp_size;
920                 }
921         }
922
923         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/shared", PKG_RW_PATH, pkgid);
924         if (lstat(device_path, &fileinfo) == 0) {
925                 if (!S_ISLNK(fileinfo.st_mode)) {
926                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
927                         if (tmp_size > 0)
928                                 rw_size += tmp_size;
929         }
930         }
931
932         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/setting", PKG_RW_PATH, pkgid);
933         if (lstat(device_path, &fileinfo) == 0) {
934                 if (!S_ISLNK(fileinfo.st_mode)) {
935                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
936                         if (tmp_size > 0)
937                                 rw_size += tmp_size;
938                 }
939         }
940
941         /* RO area */
942         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/bin", PKG_RO_PATH, pkgid);
943         if (lstat(device_path, &fileinfo) == 0) {
944                 if (!S_ISLNK(fileinfo.st_mode)) {
945                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
946                         if (tmp_size > 0)
947                                 ro_size += tmp_size;
948                 }
949         }
950
951         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/info", PKG_RO_PATH, pkgid);
952         if (lstat(device_path, &fileinfo) == 0) {
953                 if (!S_ISLNK(fileinfo.st_mode)) {
954                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
955                         if (tmp_size > 0)
956                                 ro_size += tmp_size;
957                 }
958         }
959
960         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/res", PKG_RO_PATH, pkgid);
961         if (lstat(device_path, &fileinfo) == 0) {
962                 if (!S_ISLNK(fileinfo.st_mode)) {
963                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
964                         if (tmp_size > 0)
965                                 ro_size += tmp_size;
966                 }
967         }
968
969         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RO_PATH, pkgid);
970         if (lstat(device_path, &fileinfo) == 0) {
971                 if (!S_ISLNK(fileinfo.st_mode)) {
972                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
973                         if (tmp_size > 0)
974                                 ro_size += tmp_size;
975                 }
976         }
977
978         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/shared", PKG_RO_PATH, pkgid);
979         if (lstat(device_path, &fileinfo) == 0) {
980                 if (!S_ISLNK(fileinfo.st_mode)) {
981                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
982                         if (tmp_size > 0)
983                                 ro_size += tmp_size;
984                 }
985         }
986
987         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/setting", PKG_RO_PATH, pkgid);
988         if (lstat(device_path, &fileinfo) == 0) {
989                 if (!S_ISLNK(fileinfo.st_mode)) {
990                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
991                         if (tmp_size > 0)
992                                 ro_size += tmp_size;
993                 }
994         }
995
996         /* Total size */
997         total_size = rw_size + ro_size;
998         *size = (int)total_size;
999
1000         return PMINFO_R_OK;
1001 }
1002
1003 API int pkgmgrinfo_pkginfo_get_data_size(pkgmgrinfo_pkginfo_h handle, int *size)
1004 {
1005         char *pkgid;
1006         char device_path[PKG_STRING_LEN_MAX] = { '\0', };
1007         long long total_size = 0;
1008
1009         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1010         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1011
1012         if (pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid) < 0)
1013                 return PMINFO_R_ERROR;
1014
1015         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RW_PATH, pkgid);
1016         if (access(device_path, R_OK) == 0)
1017                 total_size = _pkgmgr_calculate_dir_size(device_path);
1018         if (total_size < 0)
1019                 return PMINFO_R_ERROR;
1020
1021         *size = (int)total_size;
1022
1023         return PMINFO_R_OK;
1024 }
1025
1026 API int pkgmgrinfo_pkginfo_get_icon(pkgmgrinfo_pkginfo_h handle, char **icon)
1027 {
1028         char *locale;
1029         icon_x *ptr;
1030         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1031
1032         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
1033         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1034
1035         locale = info->locale;
1036         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
1037
1038         for (ptr = info->pkg_info->icon; ptr != NULL; ptr = ptr->next) {
1039                 if (ptr->lang == NULL)
1040                         continue;
1041
1042                 if (strcmp(ptr->lang, locale) == 0) {
1043                         *icon = (char *)ptr->text;
1044                         if (strcasecmp(*icon, "(null)") == 0) {
1045                                 locale = DEFAULT_LOCALE;
1046                                 continue;
1047                         } else {
1048                                 return PMINFO_R_OK;
1049                         }
1050                 } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
1051                         *icon = (char *)ptr->text;
1052                         return PMINFO_R_OK;
1053                 }
1054         }
1055
1056         return PMINFO_R_ERROR;
1057 }
1058
1059 API int pkgmgrinfo_pkginfo_get_label(pkgmgrinfo_pkginfo_h handle, char **label)
1060 {
1061         char *locale;
1062         label_x *ptr;
1063         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1064
1065         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
1066         retvm_if(label == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1067
1068         locale = info->locale;
1069         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
1070
1071         for (ptr = info->pkg_info->label; ptr != NULL; ptr = ptr->next) {
1072                 if (ptr->lang == NULL)
1073                         continue;
1074
1075                 if (strcmp(ptr->lang, locale) == 0) {
1076                         *label = (char *)ptr->text;
1077                         if (strcasecmp(*label, "(null)") == 0) {
1078                                 locale = DEFAULT_LOCALE;
1079                                 continue;
1080                         } else {
1081                                 return PMINFO_R_OK;
1082                         }
1083                 } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
1084                         *label = (char *)ptr->text;
1085                         return PMINFO_R_OK;
1086                 }
1087         }
1088
1089         return PMINFO_R_ERROR;
1090 }
1091
1092 API int pkgmgrinfo_pkginfo_get_description(pkgmgrinfo_pkginfo_h handle, char **description)
1093 {
1094         char *locale;
1095         description_x *ptr;
1096         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1097
1098         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1099         retvm_if(description == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1100
1101         locale = info->locale;
1102         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
1103
1104         for (ptr = info->pkg_info->description; ptr != NULL; ptr = ptr->next) {
1105                 if (ptr->lang == NULL)
1106                         continue;
1107
1108                 if (strcmp(ptr->lang, locale) == 0) {
1109                         *description = (char *)ptr->text;
1110                         if (strcasecmp(*description, PKGMGR_PARSER_EMPTY_STR) == 0) {
1111                                 locale = DEFAULT_LOCALE;
1112                                 continue;
1113                         } else {
1114                                 return PMINFO_R_OK;
1115                         }
1116                 } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
1117                         *description = (char *)ptr->text;
1118                         return PMINFO_R_OK;
1119                 }
1120         }
1121
1122         return PMINFO_R_ERROR;
1123 }
1124
1125 API int pkgmgrinfo_pkginfo_get_author_name(pkgmgrinfo_pkginfo_h handle, char **author_name)
1126 {
1127         char *locale;
1128         author_x *ptr;
1129         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1130
1131         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1132         retvm_if(author_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1133
1134         locale = info->locale;
1135         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
1136
1137         for (ptr = info->pkg_info->author; ptr != NULL; ptr = ptr->next) {
1138                 if (ptr->lang == NULL)
1139                         continue;
1140
1141                 if (strcmp(ptr->lang, locale) == 0) {
1142                         *author_name = (char *)ptr->text;
1143                         if (strcasecmp(*author_name, PKGMGR_PARSER_EMPTY_STR) == 0) {
1144                                 locale = DEFAULT_LOCALE;
1145                                 continue;
1146                         } else {
1147                                 return PMINFO_R_OK;
1148                         }
1149                 } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
1150                         *author_name = (char *)ptr->text;
1151                         return PMINFO_R_OK;
1152                 }
1153         }
1154
1155         return PMINFO_R_ERROR;
1156 }
1157
1158 API int pkgmgrinfo_pkginfo_get_author_email(pkgmgrinfo_pkginfo_h handle, char **author_email)
1159 {
1160         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1161
1162         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1163         retvm_if(author_email == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1164
1165         if (info->pkg_info == NULL || info->pkg_info->author == NULL ||
1166                         info->pkg_info->author->email == NULL)
1167                 return PMINFO_R_ERROR;
1168
1169         *author_email = (char *)info->pkg_info->author->email;
1170
1171         return PMINFO_R_OK;
1172 }
1173
1174 API int pkgmgrinfo_pkginfo_get_author_href(pkgmgrinfo_pkginfo_h handle, char **author_href)
1175 {
1176         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1177
1178         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1179         retvm_if(author_href == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1180
1181         if (info->pkg_info == NULL || info->pkg_info->author == NULL ||
1182                         info->pkg_info->author->href == NULL)
1183                 return PMINFO_R_ERROR;
1184
1185         *author_href = (char *)info->pkg_info->author->href;
1186
1187         return PMINFO_R_OK;
1188 }
1189
1190 API int pkgmgrinfo_pkginfo_get_installed_storage(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_installed_storage *storage)
1191 {
1192         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1193
1194         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1195         retvm_if(storage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1196
1197         if (info->pkg_info == NULL || info->pkg_info->installed_storage == NULL)
1198                 return PMINFO_R_ERROR;
1199
1200         if (strcmp(info->pkg_info->installed_storage,"installed_internal") == 0)
1201                 *storage = PMINFO_INTERNAL_STORAGE;
1202         else if (strcmp(info->pkg_info->installed_storage,"installed_external") == 0)
1203                 *storage = PMINFO_EXTERNAL_STORAGE;
1204         else
1205                 return PMINFO_R_ERROR;
1206
1207         return PMINFO_R_OK;
1208 }
1209
1210 API int pkgmgrinfo_pkginfo_get_installed_time(pkgmgrinfo_pkginfo_h handle, int *installed_time)
1211 {
1212         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1213
1214         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1215         retvm_if(installed_time == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1216
1217         if (info->pkg_info == NULL || info->pkg_info->installed_time == NULL)
1218                 return PMINFO_R_ERROR;
1219
1220         *installed_time = atoi(info->pkg_info->installed_time);
1221
1222         return PMINFO_R_OK;
1223 }
1224
1225 API int pkgmgrinfo_pkginfo_get_storeclientid(pkgmgrinfo_pkginfo_h handle, char **storeclientid)
1226 {
1227         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1228
1229         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1230         retvm_if(storeclientid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1231
1232         if (info->pkg_info == NULL || info->pkg_info->storeclient_id == NULL)
1233                 return PMINFO_R_ERROR;
1234
1235         *storeclientid = (char *)info->pkg_info->storeclient_id;
1236
1237         return PMINFO_R_OK;
1238 }
1239
1240 API int pkgmgrinfo_pkginfo_get_mainappid(pkgmgrinfo_pkginfo_h handle, char **mainappid)
1241 {
1242         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1243
1244         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1245         retvm_if(mainappid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1246
1247         if (info->pkg_info == NULL || info->pkg_info->mainapp_id == NULL)
1248                 return PMINFO_R_ERROR;
1249
1250         *mainappid = (char *)info->pkg_info->mainapp_id;
1251
1252         return PMINFO_R_OK;
1253 }
1254
1255 API int pkgmgrinfo_pkginfo_get_url(pkgmgrinfo_pkginfo_h handle, char **url)
1256 {
1257         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1258
1259         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1260         retvm_if(url == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1261
1262         if (info->pkg_info == NULL || info->pkg_info->package_url == NULL)
1263                 return PMINFO_R_ERROR;
1264
1265         *url = (char *)info->pkg_info->package_url;
1266
1267         return PMINFO_R_OK;
1268 }
1269
1270 API int pkgmgrinfo_pkginfo_get_size_from_xml(const char *manifest, int *size)
1271 {
1272         const char *val = NULL;
1273         const xmlChar *node;
1274         xmlTextReaderPtr reader;
1275         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "Input argument is NULL\n");
1276         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1277
1278         xmlInitParser();
1279         reader = xmlReaderForFile(manifest, NULL, 0);
1280
1281         if (reader){
1282                 if (__child_element(reader, -1)) {
1283                         node = xmlTextReaderConstName(reader);
1284                         if (!node) {
1285                                 _LOGE("xmlTextReaderConstName value is NULL\n");
1286                                 xmlFreeTextReader(reader);
1287                                 xmlCleanupParser();
1288                                 return PMINFO_R_ERROR;
1289                         }
1290
1291                         if (!strcmp(ASC_CHAR(node), "manifest")) {
1292                                 if (xmlTextReaderGetAttribute(reader, XML_CHAR("size")))
1293                                         val = ASC_CHAR(xmlTextReaderGetAttribute(reader, XML_CHAR("size")));
1294
1295                                 if (val) {
1296                                         *size = atoi(val);
1297                                 } else {
1298                                         *size = 0;
1299                                         _LOGE("package size is not specified\n");
1300                                         xmlFreeTextReader(reader);
1301                                         xmlCleanupParser();
1302                                         return PMINFO_R_ERROR;
1303                                 }
1304                         } else {
1305                                 _LOGE("Unable to create xml reader\n");
1306                                 xmlFreeTextReader(reader);
1307                                 xmlCleanupParser();
1308                                 return PMINFO_R_ERROR;
1309                         }
1310                 }
1311         } else {
1312                 _LOGE("xmlReaderForFile value is NULL\n");
1313                 xmlCleanupParser();
1314                 return PMINFO_R_ERROR;
1315         }
1316
1317         xmlFreeTextReader(reader);
1318         xmlCleanupParser();
1319
1320         return PMINFO_R_OK;
1321 }
1322
1323 API int pkgmgrinfo_pkginfo_get_location_from_xml(const char *manifest, pkgmgrinfo_install_location *location)
1324 {
1325         const char *val = NULL;
1326         const xmlChar *node;
1327         xmlTextReaderPtr reader;
1328         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "Input argument is NULL\n");
1329         retvm_if(location == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1330
1331         xmlInitParser();
1332         reader = xmlReaderForFile(manifest, NULL, 0);
1333
1334         if (reader) {
1335                 if ( __child_element(reader, -1)) {
1336                         node = xmlTextReaderConstName(reader);
1337                         if (!node) {
1338                                 _LOGE("xmlTextReaderConstName value is NULL\n");
1339                                 xmlFreeTextReader(reader);
1340                                 xmlCleanupParser();
1341                                 return PMINFO_R_ERROR;
1342                         }
1343
1344                         if (!strcmp(ASC_CHAR(node), "manifest")) {
1345                                 if (xmlTextReaderGetAttribute(reader, XML_CHAR("install-location")))
1346                                         val = ASC_CHAR(xmlTextReaderGetAttribute(reader, XML_CHAR("install-location")));
1347
1348                                 if (val) {
1349                                         if (strcmp(val, "internal-only") == 0)
1350                                                 *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
1351                                         else if (strcmp(val, "prefer-external") == 0)
1352                                                 *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
1353                                         else
1354                                                 *location = PMINFO_INSTALL_LOCATION_AUTO;
1355                                 }
1356                         } else {
1357                                 _LOGE("Unable to create xml reader\n");
1358                                 xmlFreeTextReader(reader);
1359                                 xmlCleanupParser();
1360                                 return PMINFO_R_ERROR;
1361                         }
1362                 }
1363         } else {
1364                 _LOGE("xmlReaderForFile value is NULL\n");
1365                 xmlCleanupParser();
1366                 return PMINFO_R_ERROR;
1367         }
1368
1369         xmlFreeTextReader(reader);
1370         xmlCleanupParser();
1371
1372         return PMINFO_R_OK;
1373 }
1374
1375
1376 API int pkgmgrinfo_pkginfo_get_root_path(pkgmgrinfo_pkginfo_h handle, char **path)
1377 {
1378         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1379
1380         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1381         retvm_if(path == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1382
1383         if (info->pkg_info == NULL || info->pkg_info->root_path == NULL)
1384                 return PMINFO_R_ERROR;
1385
1386         *path = (char *)info->pkg_info->root_path;
1387
1388         return PMINFO_R_OK;
1389 }
1390
1391 API int pkgmgrinfo_pkginfo_get_csc_path(pkgmgrinfo_pkginfo_h handle, char **path)
1392 {
1393         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1394
1395         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1396         retvm_if(path == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1397
1398         if (info->pkg_info == NULL || info->pkg_info->csc_path == NULL)
1399                 return PMINFO_R_ERROR;
1400
1401         *path = (char *)info->pkg_info->csc_path;
1402
1403         return PMINFO_R_OK;
1404 }
1405
1406
1407 API int pkgmgrinfo_pkginfo_is_accessible(pkgmgrinfo_pkginfo_h handle, bool *accessible)
1408 {
1409         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1410         retvm_if(accessible == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1411
1412 #if 0 //smack issue occured, check later
1413         char *pkgid = NULL;
1414         pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
1415         if (pkgid == NULL){
1416                  _LOGD("invalid func parameters\n");
1417                  return PMINFO_R_ERROR;
1418         }
1419          _LOGD("pkgmgr_get_pkg_external_validation() called\n");
1420
1421         FILE *fp = NULL;
1422         char app_mmc_path[FILENAME_MAX] = { 0, };
1423         char app_dir_path[FILENAME_MAX] = { 0, };
1424         char app_mmc_internal_path[FILENAME_MAX] = { 0, };
1425         snprintf(app_dir_path, FILENAME_MAX,"%s%s", PKG_INSTALLATION_PATH, pkgid);
1426         snprintf(app_mmc_path, FILENAME_MAX,"%s%s", PKG_SD_PATH, pkgid);
1427         snprintf(app_mmc_internal_path, FILENAME_MAX,"%s%s/.mmc", PKG_INSTALLATION_PATH, pkgid);
1428
1429         /*check whether application is in external memory or not */
1430         fp = fopen(app_mmc_path, "r");
1431         if (fp == NULL){
1432                 _LOGD(" app path in external memory not accesible\n");
1433         } else {
1434                 fclose(fp);
1435                 fp = NULL;
1436                 *accessible = 1;
1437                 _LOGD("pkgmgr_get_pkg_external_validation() : SD_CARD \n");
1438                 return PMINFO_R_OK;
1439         }
1440
1441         /*check whether application is in internal or not */
1442         fp = fopen(app_dir_path, "r");
1443         if (fp == NULL) {
1444                 _LOGD(" app path in internal memory not accesible\n");
1445                 *accessible = 0;
1446                 return PMINFO_R_ERROR;
1447         } else {
1448                 fclose(fp);
1449                 /*check whether the application is installed in SD card
1450                 but SD card is not present*/
1451                 fp = fopen(app_mmc_internal_path, "r");
1452                 if (fp == NULL){
1453                         *accessible = 1;
1454                         _LOGD("pkgmgr_get_pkg_external_validation() : INTERNAL_MEM \n");
1455                         return PMINFO_R_OK;
1456                 }
1457                 else{
1458                         *accessible = 0;
1459                         _LOGD("pkgmgr_get_pkg_external_validation() : ERROR_MMC_STATUS \n");
1460                 }
1461                 fclose(fp);
1462         }
1463
1464         _LOGD("pkgmgr_get_pkg_external_validation() end\n");
1465 #endif
1466
1467         *accessible = 1;
1468         return PMINFO_R_OK;
1469 }
1470
1471 API int pkgmgrinfo_pkginfo_is_removable(pkgmgrinfo_pkginfo_h handle, bool *removable)
1472 {
1473         char *val;
1474         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1475
1476         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1477         retvm_if(removable == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1478
1479         if (info->pkg_info == NULL || info->pkg_info->removable == NULL)
1480                 return PMINFO_R_ERROR;
1481
1482         val = (char *)info->pkg_info->removable;
1483         if (strcasecmp(val, "true") == 0)
1484                 *removable = 1;
1485         else if (strcasecmp(val, "false") == 0)
1486                 *removable = 0;
1487         else
1488                 *removable = 1;
1489
1490         return PMINFO_R_OK;
1491 }
1492
1493 API int pkgmgrinfo_pkginfo_is_movable(pkgmgrinfo_pkginfo_h handle, bool *movable)
1494 {
1495         char *val;
1496         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1497
1498         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1499         retvm_if(movable == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1500
1501         if (info->pkg_info == NULL || info->pkg_info->installlocation == NULL)
1502                 return PMINFO_R_ERROR;
1503
1504         val = (char *)info->pkg_info->installlocation;
1505         if (strcmp(val, "internal-only") == 0)
1506                 *movable = 0;
1507         else if (strcmp(val, "prefer-external") == 0)
1508                 *movable = 1;
1509         else
1510                 *movable = 1;
1511
1512         return PMINFO_R_OK;
1513 }
1514
1515 API int pkgmgrinfo_pkginfo_is_preload(pkgmgrinfo_pkginfo_h handle, bool *preload)
1516 {
1517         char *val;
1518         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1519
1520         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1521         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1522
1523         if (info->pkg_info == NULL || info->pkg_info->preload == NULL)
1524                 return PMINFO_R_ERROR;
1525
1526         val = (char *)info->pkg_info->preload;
1527         if (strcasecmp(val, "true") == 0)
1528                 *preload = 1;
1529         else if (strcasecmp(val, "false") == 0)
1530                 *preload = 0;
1531         else
1532                 *preload = 0;
1533
1534         return PMINFO_R_OK;
1535 }
1536
1537 API int pkgmgrinfo_pkginfo_is_system(pkgmgrinfo_pkginfo_h handle, bool *system)
1538 {
1539         char *val;
1540         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1541
1542         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1543         retvm_if(system == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1544
1545         if (info->pkg_info == NULL || info->pkg_info->system == NULL)
1546                 return PMINFO_R_ERROR;
1547
1548         val = (char *)info->pkg_info->system;
1549         if (strcasecmp(val, "true") == 0)
1550                 *system = 1;
1551         else if (strcasecmp(val, "false") == 0)
1552                 *system = 0;
1553         else
1554                 *system = 0;
1555
1556         return PMINFO_R_OK;
1557 }
1558
1559 API int pkgmgrinfo_pkginfo_is_readonly(pkgmgrinfo_pkginfo_h handle, bool *readonly)
1560 {
1561         char *val;
1562         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1563
1564         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1565         retvm_if(readonly == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1566
1567         if (info->pkg_info == NULL || info->pkg_info->readonly == NULL)
1568                 return PMINFO_R_ERROR;
1569
1570         val = (char *)info->pkg_info->readonly;
1571         if (strcasecmp(val, "true") == 0)
1572                 *readonly = 1;
1573         else if (strcasecmp(val, "false") == 0)
1574                 *readonly = 0;
1575         else
1576                 *readonly = 0;
1577
1578         return PMINFO_R_OK;
1579 }
1580
1581 API int pkgmgrinfo_pkginfo_is_update(pkgmgrinfo_pkginfo_h handle, bool *update)
1582 {
1583         char *val;
1584         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1585
1586         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1587         retvm_if(update == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1588
1589         if (info->pkg_info == NULL || info->pkg_info->update == NULL)
1590                 return PMINFO_R_ERROR;
1591
1592         val = (char *)info->pkg_info->update;
1593         if (strcasecmp(val, "true") == 0)
1594                 *update = 1;
1595         else if (strcasecmp(val, "false") == 0)
1596                 *update = 0;
1597         else
1598                 *update = 1;
1599
1600         return PMINFO_R_OK;
1601 }
1602
1603 API int pkgmgrinfo_pkginfo_is_for_all_users(pkgmgrinfo_pkginfo_h handle, bool *for_all_users)
1604 {
1605         char *val;
1606         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1607
1608         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1609         retvm_if(for_all_users == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1610
1611         if (info->pkg_info == NULL || info->pkg_info->for_all_users == NULL)
1612                 return PMINFO_R_ERROR;
1613
1614         val = (char *)info->pkg_info->for_all_users;
1615         if (strcasecmp(val, "1") == 0)
1616                 *for_all_users = 1;
1617         else if (strcasecmp(val, "0") == 0)
1618                 *for_all_users = 0;
1619         else
1620                 *for_all_users = 1;
1621
1622         return PMINFO_R_OK;
1623 }
1624
1625
1626 API int pkgmgrinfo_pkginfo_destroy_pkginfo(pkgmgrinfo_pkginfo_h handle)
1627 {
1628         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1629
1630         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1631
1632         __cleanup_pkginfo(info);
1633
1634         return PMINFO_R_OK;
1635 }
1636
1637 API int pkgmgrinfo_pkginfo_filter_create(pkgmgrinfo_pkginfo_filter_h *handle)
1638 {
1639         pkgmgrinfo_filter_x *filter;
1640
1641         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle output parameter is NULL\n");
1642
1643         filter = (pkgmgrinfo_filter_x*)calloc(1, sizeof(pkgmgrinfo_filter_x));
1644         if (filter == NULL) {
1645                 _LOGE("Out of Memory!!!");
1646                 return PMINFO_R_ERROR;
1647         }
1648
1649         *handle = filter;
1650
1651         return PMINFO_R_OK;
1652 }
1653
1654 API int pkgmgrinfo_pkginfo_filter_destroy(pkgmgrinfo_pkginfo_filter_h handle)
1655 {
1656         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
1657
1658         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1659
1660         if (filter->list) {
1661                 g_slist_foreach(filter->list, __destroy_each_node, NULL);
1662                 g_slist_free(filter->list);
1663         }
1664
1665         free(filter);
1666
1667         return PMINFO_R_OK;
1668 }
1669
1670 API int pkgmgrinfo_pkginfo_filter_add_int(pkgmgrinfo_pkginfo_filter_h handle,
1671                                 const char *property, const int value)
1672 {
1673         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
1674         char *val;
1675         GSList *link;
1676         int prop;
1677         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
1678         pkgmgrinfo_node_x *node;
1679
1680         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1681         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1682
1683         prop = _pminfo_pkginfo_convert_to_prop_int(property);
1684         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_INT ||
1685                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_INT) {
1686                 _LOGE("Invalid Integer Property\n");
1687                 return PMINFO_R_EINVAL;
1688         }
1689         node = (pkgmgrinfo_node_x *)calloc(1, sizeof(pkgmgrinfo_node_x));
1690         if (node == NULL) {
1691                 _LOGE("Out of Memory!!!\n");
1692                 return PMINFO_R_ERROR;
1693         }
1694         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
1695         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
1696         if (val == NULL) {
1697                 _LOGE("Out of Memory\n");
1698                 free(node);
1699                 return PMINFO_R_ERROR;
1700         }
1701         node->prop = prop;
1702         node->value = val;
1703         /*If API is called multiple times for same property, we should override the previous values.
1704         Last value set will be used for filtering.*/
1705         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1706         if (link)
1707                 filter->list = g_slist_delete_link(filter->list, link);
1708         filter->list = g_slist_append(filter->list, (gpointer)node);
1709         return PMINFO_R_OK;
1710
1711 }
1712
1713 API int pkgmgrinfo_pkginfo_filter_add_bool(pkgmgrinfo_pkginfo_filter_h handle,
1714                                 const char *property, const bool value)
1715 {
1716         char *val;
1717         GSList *link;
1718         int prop;
1719         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
1720         pkgmgrinfo_node_x *node;
1721
1722         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1723         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1724
1725         prop = _pminfo_pkginfo_convert_to_prop_bool(property);
1726         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_BOOL ||
1727                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_BOOL) {
1728                 _LOGE("Invalid Boolean Property\n");
1729                 return PMINFO_R_EINVAL;
1730         }
1731         node = (pkgmgrinfo_node_x *)calloc(1, sizeof(pkgmgrinfo_node_x));
1732         if (node == NULL) {
1733                 _LOGE("Out of Memory!!!\n");
1734                 return PMINFO_R_ERROR;
1735         }
1736         if (value)
1737                 val = strndup("('true','True')", 15);
1738         else
1739                 val = strndup("('false','False')", 17);
1740         if (val == NULL) {
1741                 _LOGE("Out of Memory\n");
1742                 free(node);
1743                 return PMINFO_R_ERROR;
1744         }
1745         node->prop = prop;
1746         node->value = val;
1747         /*If API is called multiple times for same property, we should override the previous values.
1748         Last value set will be used for filtering.*/
1749         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1750         if (link)
1751                 filter->list = g_slist_delete_link(filter->list, link);
1752         filter->list = g_slist_append(filter->list, (gpointer)node);
1753         return PMINFO_R_OK;
1754
1755 }
1756
1757 API int pkgmgrinfo_pkginfo_filter_add_string(pkgmgrinfo_pkginfo_filter_h handle,
1758                                 const char *property, const char *value)
1759 {
1760         char *val;
1761         GSList *link;
1762         int prop;
1763         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
1764         pkgmgrinfo_node_x *node;
1765
1766         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1767         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1768         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1769
1770         prop = _pminfo_pkginfo_convert_to_prop_str(property);
1771         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_STR ||
1772                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_STR) {
1773                 _LOGE("Invalid String Property\n");
1774                 return PMINFO_R_EINVAL;
1775         }
1776         node = (pkgmgrinfo_node_x *)calloc(1, sizeof(pkgmgrinfo_node_x));
1777         if (node == NULL) {
1778                 _LOGE("Out of Memory!!!\n");
1779                 return PMINFO_R_ERROR;
1780         }
1781         if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_AUTO) == 0)
1782                 val = strndup("auto", PKG_STRING_LEN_MAX - 1);
1783         else if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_INTERNAL) == 0)
1784                 val = strndup("internal-only", PKG_STRING_LEN_MAX - 1);
1785         else if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_EXTERNAL) == 0)
1786                 val = strndup("prefer-external", PKG_STRING_LEN_MAX - 1);
1787         else if (strcmp(value, "installed_internal") == 0)
1788                 val = strndup("installed_internal", PKG_STRING_LEN_MAX - 1);
1789         else if (strcmp(value, "installed_external") == 0)
1790                 val = strndup("installed_external", PKG_STRING_LEN_MAX - 1);
1791         else
1792                 val = strndup(value, PKG_STRING_LEN_MAX - 1);
1793         if (val == NULL) {
1794                 _LOGE("Out of Memory\n");
1795                 free(node);
1796                 return PMINFO_R_ERROR;
1797         }
1798         node->prop = prop;
1799         node->value = val;
1800         /*If API is called multiple times for same property, we should override the previous values.
1801         Last value set will be used for filtering.*/
1802         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1803         if (link)
1804                 filter->list = g_slist_delete_link(filter->list, link);
1805         filter->list = g_slist_append(filter->list, (gpointer)node);
1806         return PMINFO_R_OK;
1807
1808 }
1809
1810 API int pkgmgrinfo_pkginfo_usr_filter_count(pkgmgrinfo_pkginfo_filter_h handle, int *count, uid_t uid)
1811 {
1812         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1813         retvm_if(count == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1814         char *locale = NULL;
1815         char *condition = NULL;
1816         char *error_message = NULL;
1817         char query[MAX_QUERY_LEN] = {'\0'};
1818         char where[MAX_QUERY_LEN] = {'\0'};
1819         GSList *list;
1820         int ret = 0;
1821
1822         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
1823         filter->uid = uid;
1824         /*Get current locale*/
1825         locale = _get_system_locale();
1826         if (locale == NULL) {
1827                 _LOGE("manifest locale is NULL\n");
1828                 return PMINFO_R_ERROR;
1829         }
1830
1831         ret = __open_manifest_db(uid);
1832         if (ret == -1) {
1833                 _LOGE("Fail to open manifest DB\n");
1834                 free(locale);
1835                 return PMINFO_R_ERROR;
1836         }
1837
1838         /*Start constructing query*/
1839         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_COUNT_PACKAGE, locale);
1840
1841         /*Get where clause*/
1842         for (list = filter->list; list; list = g_slist_next(list)) {
1843                 __get_filter_condition(list->data, &condition);
1844                 if (condition) {
1845                         strncat(where, condition, sizeof(where) - strlen(where) -1);
1846                         where[sizeof(where) - 1] = '\0';
1847                         free(condition);
1848                         condition = NULL;
1849                 }
1850                 if (g_slist_next(list)) {
1851                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
1852                         where[sizeof(where) - 1] = '\0';
1853                 }
1854         }
1855         if (strlen(where) > 0) {
1856                 strncat(query, where, sizeof(query) - strlen(query) - 1);
1857                 query[sizeof(query) - 1] = '\0';
1858         }
1859
1860         /*Execute Query*/
1861         if (SQLITE_OK !=
1862             sqlite3_exec(GET_DB(manifest_db), query, __count_cb, (void *)count, &error_message)) {
1863                 _LOGE("Don't execute query = %s error message = %s\n", query,
1864                        error_message);
1865                 sqlite3_free(error_message);
1866                 ret = PMINFO_R_ERROR;
1867                 *count = 0;
1868                 goto err;
1869         }
1870         ret = PMINFO_R_OK;
1871 err:
1872         if (locale) {
1873                 free(locale);
1874                 locale = NULL;
1875         }
1876         __close_manifest_db();
1877         return ret;
1878 }
1879
1880 API int pkgmgrinfo_pkginfo_filter_count(pkgmgrinfo_pkginfo_filter_h handle, int *count)
1881 {
1882         return pkgmgrinfo_pkginfo_usr_filter_count(handle, count, GLOBAL_USER);
1883 }
1884
1885 API int pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(
1886                 pkgmgrinfo_pkginfo_filter_h handle,
1887                 pkgmgrinfo_pkg_list_cb pkg_cb, void *user_data, uid_t uid)
1888 {
1889         if (handle == NULL || pkg_cb == NULL) {
1890                 LOGE("invalid parameter");
1891                 return PMINFO_R_EINVAL;
1892         }
1893
1894         return _pkginfo_get_filtered_foreach_pkginfo(handle, pkg_cb, user_data,
1895                         uid);
1896 }
1897
1898 API int pkgmgrinfo_pkginfo_filter_foreach_pkginfo(pkgmgrinfo_pkginfo_filter_h handle,
1899                                 pkgmgrinfo_pkg_list_cb pkg_cb, void *user_data)
1900 {
1901         return pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(handle, pkg_cb, user_data, GLOBAL_USER);
1902 }
1903
1904 API int pkgmgrinfo_pkginfo_foreach_privilege(pkgmgrinfo_pkginfo_h handle,
1905                         pkgmgrinfo_pkg_privilege_list_cb privilege_func, void *user_data)
1906 {
1907         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
1908         retvm_if(privilege_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1909         int ret = -1;
1910         privilege_x *ptr = NULL;
1911         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1912         ptr = info->pkg_info->privileges->privilege;
1913         for (; ptr; ptr = ptr->next) {
1914                 if (ptr->text){
1915                         ret = privilege_func(ptr->text, user_data);
1916                         if (ret < 0)
1917                                 break;
1918                 }
1919         }
1920         return PMINFO_R_OK;
1921 }
1922
1923 API int pkgmgrinfo_create_pkgusrdbinfo(const char *pkgid, uid_t uid, pkgmgrinfo_pkgdbinfo_h *handle)
1924 {
1925         retvm_if(!pkgid, PMINFO_R_EINVAL, "pkgid is NULL");
1926         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
1927
1928         char *manifest = NULL;
1929         manifest_x *mfx = NULL;
1930         *handle = NULL;
1931         manifest = pkgmgr_parser_get_usr_manifest_file(pkgid, uid);
1932         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "pkg[%s] dont have manifest file", pkgid);
1933
1934         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
1935         if (manifest) {
1936                 free(manifest);
1937                 manifest = NULL;
1938         }
1939         retvm_if(mfx == NULL, PMINFO_R_EINVAL, "pkg[%s] parsing fail", pkgid);
1940
1941         *handle = (void *)mfx;
1942
1943         return PMINFO_R_OK;
1944 }
1945
1946 API int pkgmgrinfo_create_pkgdbinfo(const char *pkgid, pkgmgrinfo_pkgdbinfo_h *handle)
1947 {
1948         retvm_if(!pkgid, PMINFO_R_EINVAL, "pkgid is NULL");
1949         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
1950
1951         char *manifest = NULL;
1952         manifest_x *mfx = NULL;
1953         *handle = NULL;
1954         manifest = pkgmgr_parser_get_manifest_file(pkgid);
1955         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "pkg[%s] dont have manifest file", pkgid);
1956
1957         mfx = pkgmgr_parser_process_manifest_xml(manifest);
1958         if (manifest) {
1959                 free(manifest);
1960                 manifest = NULL;
1961         }
1962         retvm_if(mfx == NULL, PMINFO_R_EINVAL, "pkg[%s] parsing fail", pkgid);
1963
1964         *handle = (void *)mfx;
1965
1966         return PMINFO_R_OK;
1967 }
1968
1969 API int pkgmgrinfo_set_type_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *type)
1970 {
1971         int len;
1972         manifest_x *mfx = (manifest_x *)handle;
1973
1974         retvm_if(!type, PMINFO_R_EINVAL, "Argument supplied is NULL");
1975         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
1976
1977         len = strlen(type);
1978         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
1979
1980         if (mfx->type)
1981                 free((void *)mfx->type);
1982
1983         mfx->type = strndup(type, PKG_TYPE_STRING_LEN_MAX);
1984
1985         return PMINFO_R_OK;
1986 }
1987
1988 API int pkgmgrinfo_set_version_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *version)
1989 {
1990         int len;
1991         manifest_x *mfx = (manifest_x *)handle;
1992
1993         retvm_if(!version, PMINFO_R_EINVAL, "Argument supplied is NULL");
1994         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
1995
1996         len = strlen(version);
1997         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
1998
1999         if (mfx->version)
2000                 free((void *)mfx->version);
2001
2002         mfx->version = strndup(version, PKG_VERSION_STRING_LEN_MAX);
2003         return PMINFO_R_OK;
2004 }
2005
2006 API int pkgmgrinfo_set_install_location_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, INSTALL_LOCATION location)
2007 {
2008         manifest_x *mfx = (manifest_x *)handle;
2009
2010         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2011         retvm_if((location < 0) || (location > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
2012
2013         if (mfx->installlocation)
2014                 free((void *)mfx->installlocation);
2015
2016         if (location == INSTALL_INTERNAL)
2017                 mfx->installlocation = strdup("internal-only");
2018         else if (location == INSTALL_EXTERNAL)
2019                 mfx->installlocation = strdup("prefer-external");
2020
2021         return PMINFO_R_OK;
2022 }
2023
2024 API int pkgmgrinfo_set_size_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *size)
2025 {
2026         manifest_x *mfx = (manifest_x *)handle;
2027
2028         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2029         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL");
2030
2031         if (mfx->package_size)
2032                 free((void *)mfx->package_size);
2033
2034         mfx->package_size = strdup(size);
2035
2036         return PMINFO_R_OK;
2037 }
2038
2039 API int pkgmgrinfo_set_label_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *label_txt, const char *locale)
2040 {
2041         int len;
2042         manifest_x *mfx = (manifest_x *)handle;
2043         label_x *label;
2044
2045         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2046         retvm_if(!label_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
2047
2048         len = strlen(label_txt);
2049         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
2050
2051         label = calloc(1, sizeof(label_x));
2052         retvm_if(label == NULL, PMINFO_R_EINVAL, "Malloc Failed");
2053
2054         LISTADD(mfx->label, label);
2055         if (locale)
2056                 mfx->label->lang = strdup(locale);
2057         else
2058                 mfx->label->lang = strdup(DEFAULT_LOCALE);
2059         mfx->label->text = strdup(label_txt);
2060
2061         return PMINFO_R_OK;
2062 }
2063
2064 API int pkgmgrinfo_set_icon_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *icon_txt, const char *locale)
2065 {
2066         int len;
2067         manifest_x *mfx = (manifest_x *)handle;
2068         icon_x *icon;
2069
2070         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2071         retvm_if(!icon_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
2072
2073         len = strlen(icon_txt);
2074         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
2075
2076         icon = calloc(1, sizeof(icon_x));
2077         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Malloc Failed");
2078
2079         LISTADD(mfx->icon, icon);
2080         if (locale)
2081                 mfx->icon->lang = strdup(locale);
2082         else
2083                 mfx->icon->lang = strdup(DEFAULT_LOCALE);
2084         mfx->icon->text = strdup(icon_txt);
2085
2086         return PMINFO_R_OK;
2087 }
2088
2089 API int pkgmgrinfo_set_description_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *desc_txt, const char *locale)
2090 {
2091         int len = strlen(desc_txt);
2092         manifest_x *mfx = (manifest_x *)handle;
2093         description_x *description;
2094
2095         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2096         retvm_if(!desc_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
2097
2098         len = strlen(desc_txt);
2099         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
2100
2101         description = calloc(1, sizeof(description_x));
2102         retvm_if(description == NULL, PMINFO_R_EINVAL, "Malloc Failed");
2103
2104         LISTADD(mfx->description, description);
2105         if (locale)
2106                 mfx->description->lang = strdup(locale);
2107         else
2108                 mfx->description->lang = strdup(DEFAULT_LOCALE);
2109         mfx->description->text = strdup(desc_txt);
2110
2111         return PMINFO_R_OK;
2112 }
2113
2114 API int pkgmgrinfo_set_author_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *author_name,
2115                 const char *author_email, const char *author_href, const char *locale)
2116 {
2117         manifest_x *mfx = (manifest_x *)handle;
2118         author_x *author;
2119
2120         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2121
2122         author = calloc(1, sizeof(author_x));
2123         retvm_if(author == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL");
2124
2125         LISTADD(mfx->author, author);
2126         if (author_name)
2127                 mfx->author->text = strdup(author_name);
2128         if (author_email)
2129                 mfx->author->email = strdup(author_email);
2130         if (author_href)
2131                 mfx->author->href = strdup(author_href);
2132         if (locale)
2133                 mfx->author->lang = strdup(locale);
2134         else
2135                 mfx->author->lang = strdup(DEFAULT_LOCALE);
2136         return PMINFO_R_OK;
2137 }
2138
2139 API int pkgmgrinfo_set_removable_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, int removable)
2140 {
2141         manifest_x *mfx = (manifest_x *)handle;
2142
2143         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2144         retvm_if((removable < 0) || (removable > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
2145
2146         if (mfx->removable)
2147                 free((void *)mfx->removable);
2148
2149         if (removable == 0)
2150                 mfx->removable = strdup("false");
2151         else if (removable == 1)
2152                 mfx->removable = strdup("true");
2153
2154         return PMINFO_R_OK;
2155 }
2156
2157 API int pkgmgrinfo_set_preload_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, int preload)
2158 {
2159         manifest_x *mfx = (manifest_x *)handle;
2160
2161         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2162         retvm_if((preload < 0) || (preload > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
2163
2164         if (mfx->preload)
2165                 free((void *)mfx->preload);
2166
2167         if (preload == 0)
2168                 mfx->preload = strdup("false");
2169         else if (preload == 1)
2170                 mfx->preload = strdup("true");
2171
2172         return PMINFO_R_OK;
2173 }
2174
2175 API int pkgmgrinfo_set_installed_storage_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, INSTALL_LOCATION location)
2176 {
2177         manifest_x *mfx = (manifest_x *)handle;
2178
2179         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2180         retvm_if((location < 0) || (location > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
2181
2182         if (mfx->installed_storage)
2183                 free((void *)mfx->installed_storage);
2184
2185         if (location == INSTALL_INTERNAL)
2186                 mfx->installed_storage = strdup("installed_internal");
2187         else if (location == INSTALL_EXTERNAL)
2188                 mfx->installed_storage = strdup("installed_external");
2189
2190         return PMINFO_R_OK;
2191 }
2192
2193 API int pkgmgrinfo_save_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle)
2194 {
2195         int ret;
2196         manifest_x *mfx = (manifest_x *)handle;
2197         mfx = (manifest_x *)handle;
2198
2199         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2200
2201         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
2202         if (ret == 0) {
2203                 _LOGE("Successfully stored info in DB\n");
2204                 return PMINFO_R_OK;
2205         } else {
2206                 _LOGE("Failed to store info in DB\n");
2207                 return PMINFO_R_ERROR;
2208         }
2209 }
2210
2211 API int pkgmgrinfo_save_pkgusrdbinfo(pkgmgrinfo_pkgdbinfo_h handle, uid_t uid)
2212 {
2213         int ret;
2214         manifest_x *mfx = (manifest_x *)handle;
2215
2216         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2217
2218         ret = pkgmgr_parser_update_manifest_info_in_usr_db(mfx, uid);
2219         if (ret == 0) {
2220                 _LOGE("Successfully stored info in DB\n");
2221                 return PMINFO_R_OK;
2222         } else {
2223                 _LOGE("Failed to store info in DB\n");
2224                 return PMINFO_R_ERROR;
2225         }
2226 }
2227
2228 API int pkgmgrinfo_destroy_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle)
2229 {
2230         manifest_x *mfx = (manifest_x *)handle;
2231
2232         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2233
2234         pkgmgrinfo_basic_free_package(mfx);
2235
2236         return PMINFO_R_OK;
2237 }