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