Update source from tizen 2.3
[platform/core/base/rpm-installer.git] / backend / src / vconf / rpm-vconf-intf.c
1 /*
2  * rpm-installer
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 #include <pkgmgr_installer.h>
24 #include <vconf.h>
25 #include <errno.h>
26
27 #include "rpm-installer-util.h"
28 #include "rpm-installer.h"
29
30 #define VCONF_LOCATION                  "db"
31 #define VCONF_RPM_INSTALLER             VCONF_LOCATION"/private/rpm-installer"
32
33 #define VCONF_RPM_INSTALLER_BACKEND_STATE \
34         VCONF_RPM_INSTALLER"/state"
35 #define VCONF_RPM_INSTALLER_BACKEND_STATEINFO \
36         VCONF_RPM_INSTALLER"/stateinfo"
37
38 #define VCONF_RPM_INSTALLER_LAST_REQUESTINFO_COMMAND \
39         VCONF_RPM_INSTALLER"/requestinfo/command"
40 #define VCONF_RPM_INSTALLER_LAST_REQUESTINFO_PKGNAME \
41         VCONF_RPM_INSTALLER"/requestinfo/pkgname"
42 #define VCONF_RPM_INSTALLER_LAST_REQUESTINFO_OPTIONS \
43         VCONF_RPM_INSTALLER"/requestinfo/options"
44
45 #define ERR_RETURN_LEN                  256
46
47 extern pkgmgr_installer *pi;
48 extern char *gpkgname;
49 extern int broadcast_disable;
50
51 int _ri_get_backend_state()
52 {
53         int ret = -1;
54         int state = -1;
55
56 //      _LOGD("_ri_get_backend_state\n");
57         ret = vconf_get_int(VCONF_RPM_INSTALLER_BACKEND_STATE, &state);
58         if (ret == -1) {
59                 _LOGE(
60                        "_ri_get_backend_state: vconf_get_int FAIL\n");
61         } else {
62                 ret = state;
63         }
64         return ret;
65 }
66
67 int _ri_set_backend_state(int state)
68 {
69         int ret = -1;
70
71         if (state == 0) {
72                 vconf_unset_recursive(VCONF_RPM_INSTALLER);
73         }
74
75 //      _LOGD("_ri_set_backend_state\n");
76         ret = vconf_set_int(VCONF_RPM_INSTALLER_BACKEND_STATE, state);
77         if (ret == -1) {
78                 _LOGE(
79                        "_ri_set_backend_state: vconf_set_int FAIL\n");
80         }
81
82         return ret;
83 }
84
85 int _ri_get_backend_state_info()
86 {
87         int ret = -1;
88         int state = -1;
89         ret = vconf_get_int(VCONF_RPM_INSTALLER_BACKEND_STATEINFO, &state);
90         if (ret == -1) {
91                 _LOGE(
92                        "_ri_get_backend_state_info: vconf_get_int FAIL\n");
93         } else {
94                 ret = state;
95         /*      _LOGD("_ri_get_backend_state_info state[%d]\n", state);*/
96         }
97         return ret;
98 }
99
100 int _ri_set_backend_state_info(int state)
101 {
102         int ret = -1;
103 //      _LOGD("_ri_set_backend_state_info %d\n", state);
104         ret = vconf_set_int(VCONF_RPM_INSTALLER_BACKEND_STATEINFO, state);
105         if (ret == -1)
106                 _LOGE(
107                        "_ri_set_backend_state_info: vconf_set_int FAIL\n");
108
109         return ret;
110 }
111
112 int _ri_get_last_input_info(char **pkgid, int *preqcommand, int *poptions)
113 {
114         int ret = -1;
115         if (!pkgid || !preqcommand || !poptions)
116                 return -1;
117         ret = vconf_get_int(VCONF_RPM_INSTALLER_LAST_REQUESTINFO_COMMAND,
118                             preqcommand);
119         if (ret == -1)
120                 _LOGE(
121                        "_ri_get_last_input_info: VCONF_RPM_INSTALLER_LAST_REQUESTINFO_COMMAND: vconf_get_int FAIL\n");
122
123         ret = vconf_get_int(VCONF_RPM_INSTALLER_LAST_REQUESTINFO_OPTIONS,
124                             poptions);
125         if (ret == -1)
126                 _LOGE(
127                        "_ri_get_last_input_info: VCONF_RPM_INSTALLER_LAST_REQUESTINFO_OPTIONS: vconf_get_int FAIL\n");
128
129         *pkgid = vconf_get_str(VCONF_RPM_INSTALLER_LAST_REQUESTINFO_PKGNAME);
130         return 0;
131 }
132
133 void _ri_save_last_input_info(char *pkgid, int reqcommand, int options)
134 {
135         keylist_t *kl = NULL;
136         kl = vconf_keylist_new();
137         int ret = -1;
138
139         ret = vconf_keylist_add_int(kl,
140                             VCONF_RPM_INSTALLER_LAST_REQUESTINFO_COMMAND,
141                             reqcommand);
142         if (ret == -1)
143                 _LOGE("vconf_keylist_add_int FAIL\n");
144         ret = vconf_keylist_add_str(kl,
145                             VCONF_RPM_INSTALLER_LAST_REQUESTINFO_PKGNAME,
146                             pkgid);
147         if (ret == -1)
148                 _LOGE("vconf_keylist_add_str FAIL\n");
149         ret = vconf_keylist_add_int(kl,
150                             VCONF_RPM_INSTALLER_LAST_REQUESTINFO_OPTIONS,
151                             options);
152         if (ret == -1)
153                 _LOGE("vconf_keylist_add_int FAIL\n");
154
155         if (vconf_set(kl))
156                 _LOGE(
157                        "_ri_save_last_input_info: Failure in writing vconf\n");
158
159         ret = vconf_keylist_free(kl);
160         if (ret == -1)
161                 _LOGE("vconf_keylist_free FAIL\n");
162 }
163
164 void _ri_broadcast_status_notification(const char *pkgid, char *pkg_type, char *key, char *val)
165 {
166         const char *pkgid_tmp = pkgid;
167         char buf[ERR_RETURN_LEN] = {'\0'};
168         int ret_val = 0;
169
170         if (broadcast_disable)
171                 return;
172
173 #if 0
174         if (gpkgname != NULL)
175                 pkgid_tmp = gpkgname;
176         else
177                 pkgid_tmp = pkgid;
178 #endif
179
180         if (pi == NULL) {
181                 _LOGE("Failure in sending broadcast message\n");
182                 return;
183         }
184
185         if (strcmp(key,PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR) == 0) {
186                 ret_val = atoi(val);
187
188                 _LOGD("pkgid=[%s], key=[%s], val=[%s]\n", pkgid_tmp, key, val);
189
190                 snprintf(buf, ERR_RETURN_LEN - 1, "%d", ret_val);
191                 pkgmgr_installer_send_signal(pi, pkg_type, pkgid_tmp, key, buf);
192                 return;
193         } else {
194                 ret_val = _ri_string_to_error_no(val);
195
196                 _LOGD( "pkgid=[%s], key=[%s], val=[%s]\n", pkgid_tmp, key, val);
197
198                 if (ret_val == RPM_INSTALLER_ERR_UNKNOWN){
199                         pkgmgr_installer_send_signal(pi, pkg_type, pkgid_tmp, key, val);
200                 }
201                 else{
202                         snprintf(buf, ERR_RETURN_LEN - 1, "%d:%s", ret_val, val);
203                         pkgmgr_installer_send_signal(pi, pkg_type, pkgid_tmp, key, buf);
204                 }
205         }
206 }