tizen 2.3.1 release
[apps/home/installer.git] / src / installer_package_manager.c
1 #include <errno.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5
6 #include <aul.h>
7 #include <bundle.h>
8
9 #include <pkgmgr-info.h>
10 #include <package-manager.h>
11
12 #include <appsvc.h>
13 #include <notification.h>
14
15 #include "installer_util.h"
16 #include "installer_package_manager.h"
17
18 #define KEY_PACKAGE_LABEL               "__KEY_PACKAGE_LABEL__"
19 #define KEY_PACKAGE_PATH                "__KEY_PACKAGE_PATH__"
20 #define KEY_PACKAGE_ICON_PATH   "__KEY_PACKAGE_ICON_PATH__"
21
22
23 pkgmgr_client *__pc;
24 int __priv_id;
25
26
27 bool is_package_installed(const char *pkgid)
28 {
29         if (pkgid == NULL)
30         {
31                 LOGE("%s pkgid is empty.", __func__);
32         }
33
34         int result = 0;
35         pkgmgrinfo_pkginfo_h pPackageInfoHandle = NULL;
36
37         result = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pPackageInfoHandle);
38         if (result != 0)
39         {
40                 LOGD("%s pkgmgrinfo_pkginfo_get_pkginfo() failed. result=[%d], package=[%s]", __func__, result, pkgid);
41                 return false;
42         }
43
44         if (pPackageInfoHandle)
45         {
46                 pkgmgrinfo_pkginfo_destroy_pkginfo(pPackageInfoHandle);
47         }
48
49         SECURE_LOGD("%s pkgid = [%s] is installed. ", __func__, pkgid);
50         return true;
51 }
52
53 int get_package_version(const char *pkgid, char **version)
54 {
55         if (pkgid == NULL)
56         {
57                 LOGE("%s pkgid is empty.", __func__);
58                 return -1;
59         }
60
61         int result = 0;
62         char *ver_temp = NULL;
63         pkgmgrinfo_pkginfo_h handle = NULL;
64
65         result = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
66         if (result != 0)
67                 return -1;
68
69         result = pkgmgrinfo_pkginfo_get_version(handle, &ver_temp);
70         if (result != 0)
71         {
72                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
73                 return -1;
74         }
75
76         *version = strdup(ver_temp);
77         return 0;
78 }
79
80 int launch_install_service(const char *file_path, const package_manager_pkg_detail_info_t *pkg_info)
81 {
82         bundle *b = NULL;
83         b = bundle_create();
84         SECURE_LOGD("%s file_path(%s), label(%s), icon_path(%s)", __func__, file_path, pkg_info->label, pkg_info->icon_buf);
85
86         bundle_add(b, KEY_PACKAGE_PATH, file_path);
87         bundle_add(b, KEY_PACKAGE_LABEL, pkg_info->label);
88         bundle_add(b, KEY_PACKAGE_ICON_PATH, pkg_info->icon_buf);
89
90         if(!aul_app_is_running("org.tizen.installer-service"))
91         {
92                 int res = aul_launch_app("org.tizen.installer-service", b);
93                 if (res < 0)
94                 {
95                         LOGE("%s aul_launch_app() failed.(%d)", __func__, res);
96                 }
97
98                 ui_app_exit();
99         }
100
101         if (b)
102         {
103                 bundle_free(b);
104         }
105
106         return 0;
107 }
108
109
110 package_manager_pkg_detail_info_t* get_package_info_from_file(const char *file_path)
111 {
112         int ret = 0;
113
114         if (file_path == NULL)
115         {
116                 LOGE("%s file_path is empty.", __func__);
117                 return NULL;
118         }
119         LOGD("%s packagePath = [%s]", __func__, file_path);
120
121         ret = access(file_path, F_OK);
122         if (ret < 0)
123         {
124                 switch (errno)
125                 {
126                 case ENOENT:
127                         LOGE("%s access() failed. file_path is not existed. (%s)", __func__, file_path);
128                         return NULL;
129
130                 default:
131                         LOGE("%s access() failed. path: %s errno: %d (%s)", __func__, file_path, errno, strerror(errno));
132                         return NULL;
133                 }
134         }
135
136         char *extension = NULL;
137         extension = get_file_extension(file_path);
138         SECURE_LOGD("%s pacagePath = [%s], extension = [%s]", __func__, file_path, extension);
139
140         package_manager_pkg_detail_info_t* pkg_info = NULL;
141         pkg_info = pkgmgr_client_check_pkginfo_from_file(file_path);
142
143         if (pkg_info == NULL)
144         {
145                 LOGE("%s pkgmgr_client_check_pkginfo_from_file(%s) is failed.", __func__, file_path);
146                 return NULL;
147         }
148
149         return pkg_info;
150 }