Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / utils / manifest_util.cc
1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/utils/manifest_util.h"
6
7 #include <manifest_parser/utils/logging.h>
8 #include <pkgmgr/pkgmgr_parser.h>
9 #include <pkgmgr-info.h>
10 #include <tzplatform_config.h>
11
12 #include <filesystem>
13 #include <string>
14
15 #include "common/utils/pkgmgr_query.h"
16
17 namespace fs = std::filesystem;
18
19 namespace {
20
21 int PkgmgrAppInfoCallback(const pkgmgrinfo_appinfo_h handle,
22                           void *user_data) {
23   manifest_x* mfx = static_cast<manifest_x*>(user_data);
24
25   char* app_id = nullptr;
26   if (pkgmgrinfo_appinfo_get_appid(handle, &app_id))
27     return PMINFO_R_ERROR;
28
29   char* icon_text = nullptr;
30   if (pkgmgrinfo_appinfo_get_icon(handle, &icon_text))
31     LOG(INFO) << "No Icon for " << app_id;
32
33   if (!mfx->root_path) {
34     char *root_path = nullptr;
35     if (pkgmgrinfo_appinfo_get_root_path(handle, &root_path))
36       return PMINFO_R_ERROR;
37     mfx->root_path = strdup(root_path);
38   }
39
40   application_x* application =
41       static_cast<application_x*>(calloc(1, sizeof(application_x)));
42   if (!application)
43     return PMINFO_R_ERROR;
44
45   if (icon_text) {
46     icon_x* icon = static_cast<icon_x*>(calloc(1, sizeof(icon_x)));
47     if (!icon) {
48       free(application);
49       return PMINFO_R_ERROR;
50     }
51     icon->text = strdup(icon_text);
52     application->icon = g_list_append(application->icon, icon);
53   }
54   application->appid = strdup(app_id);
55   mfx->application = g_list_append(mfx->application, application);
56
57   return PMINFO_R_OK;
58 }
59
60 }  // namespace
61
62 namespace common_installer {
63
64 fs::path GetManifestLocation(const std::string& pkgid,
65                              uid_t uid,
66                              bool is_readonly) {
67   PkgQueryInterface pkg_query(pkgid, uid);
68   if (pkg_query.IsGlobalPackage()) {
69     uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
70     is_readonly = pkg_query.IsReadonlyPackage();
71   }
72   fs::path xml_path = fs::path(getUserManifestPath(uid, is_readonly))
73                       / fs::path(pkgid.c_str());
74   xml_path += ".xml";
75
76   return xml_path;
77 }
78
79 manifest_x* PkgmgrGenerateManifestInfoFromDB(const std::string &pkgid,
80                                              uid_t uid) {
81   pkgmgrinfo_appinfo_filter_h filter;
82   if (pkgmgrinfo_appinfo_filter_create(&filter))
83     return nullptr;
84
85   if (pkgmgrinfo_appinfo_filter_add_string(filter,
86                           PMINFO_APPINFO_PROP_APP_PACKAGE, pkgid.c_str())) {
87     pkgmgrinfo_appinfo_filter_destroy(filter);
88     return nullptr;
89   }
90
91   manifest_x* mfx = static_cast<manifest_x*>(calloc(1, sizeof(manifest_x)));
92   if (!mfx) {
93     pkgmgrinfo_appinfo_filter_destroy(filter);
94     LOG(ERROR) << "Out of memory";
95     return nullptr;
96   }
97   if (pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(filter,
98                                                     PkgmgrAppInfoCallback,
99                                                     mfx, uid)) {
100     pkgmgr_parser_free_manifest_xml(mfx);
101     pkgmgrinfo_appinfo_filter_destroy(filter);
102     return nullptr;
103   }
104   mfx->package = strdup(pkgid.c_str());
105
106   pkgmgrinfo_appinfo_filter_destroy(filter);
107
108   return mfx;
109 }
110
111 }  // namespace common_installer