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