upload tizen1.0 source
[pkgs/n/native-installer.git] / backend / src / vconf / nativebackendvconfintf.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
24
25
26   
27 #include <pkgmgr_installer.h>
28 #include <vconf.h>
29 #include <errno.h>
30
31 #include "native_installer_util.h"
32
33 #define VCONF_LOCATION                                                  "db"
34 #define VCONF_NATIVE_INSTALLER          VCONF_LOCATION"/nativeinstaller"
35
36 #define VCONF_NATIVE_INSTALLER_BACKEND_STATE \
37         VCONF_NATIVE_INSTALLER"/state"
38 #define VCONF_NATIVE_INSTALLER_BACKEND_STATEINFO \
39         VCONF_NATIVE_INSTALLER"/stateinfo"
40
41 #define VCONF_NATIVE_INSTALLER_LAST_REQUESTINFO_COMMAND \
42         VCONF_NATIVE_INSTALLER"/requestinfo/command"
43 #define VCONF_NATIVE_INSTALLER_LAST_REQUESTINFO_PKGNAME \
44         VCONF_NATIVE_INSTALLER"/requestinfo/pkgname"
45 #define VCONF_NATIVE_INSTALLER_LAST_REQUESTINFO_OPTIONS \
46         VCONF_NATIVE_INSTALLER"/requestinfo/options"
47 #define VCONF_NATIVE_INSTALLER_APPSETTING       "db/app-settings"
48
49 extern char *gptrpkgname;
50 extern pkgmgr_installer *pi;
51
52 int _set_event_notification(int value)
53 {
54         int pid;
55         int ret = -1;
56         char buff[256] = { 0, };
57         char strval[8] = { 0, };
58         ret =
59             snprintf(buff, sizeof(buff) - 1,
60                      VCONF_NATIVE_INSTALLER_APPSETTING "/noti-enabled/%s",
61                      gptrpkgname);
62         if (ret < 0) {
63                 d_msg_backend(DEBUG_ERR,
64                               "_set_event_notification: snprintf FAIL\n");
65                 return ret;
66         }
67
68         ret = snprintf(strval, sizeof(strval) - 1, "%d", value);
69         if (ret < 0) {
70                 d_msg_backend(DEBUG_ERR,
71                               "_set_event_notification: snprintf FAIL\n");
72                 return ret;
73         }
74
75         if ((pid = fork()) < 0) {
76                 ret = -1;
77         } else if (pid == 0) {
78                 execl("/usr/bin/vconftool", "vconftool", "-g", "6514", "set",
79                       "-t", "int", buff, strval, (char *)0);
80                 exit(127);      /* execl error */
81         } else {
82                 while (waitpid(pid, &ret, 0) < 0) {
83                         if (errno != EINTR) {
84                                 ret = -1;       /* error other  than EINTR 
85                                                 from waitpid() */
86                                 break;
87                         }
88                 }
89         }
90
91         if (ret != 0) {
92                 d_msg_backend(DEBUG_ERR,
93                               "_set_event_notification: vconf_set_int FAIL\n");
94                 return ret;
95         } else
96                 return NATIVEINSTALLER_SUCCESS;
97 }
98
99 int _unset_event_notification()
100 {
101         int pid;
102         int ret = -1;
103         char buff[256] = { 0, };
104         ret = snprintf(buff, sizeof(buff) - 1,
105                        VCONF_NATIVE_INSTALLER_APPSETTING "/noti-enabled/%s",
106                        gptrpkgname);
107         if (ret < 0) {
108                 d_msg_backend(DEBUG_ERR,
109                               "_unset_event_notification: snprintf FAIL\n");
110                 return ret;
111         }
112
113         if ((pid = fork()) < 0) {
114                 ret = -1;
115         } else if (pid == 0) {
116                 execl("/usr/bin/vconftool", "vconftool", "unset", buff,
117                       (char *)0);
118                 exit(127);      /* execl error */
119         } else {
120                 while (waitpid(pid, &ret, 0) < 0) {
121                         if (errno != EINTR) {
122                                 ret = -1;       /* error other than EINTR
123                                                 from waitpid() */
124                                 break;
125                         }
126                 }
127         }
128
129         if (ret != 0) {
130                 d_msg_backend(DEBUG_ERR,
131                               "_unset_event_notification: vconf_unset FAIL\n");
132                 return ret;
133         } else {
134                 return NATIVEINSTALLER_SUCCESS;
135         }
136 }
137
138 int _get_backend_state()
139 {
140         int ret = -1;
141         int state = -1;
142         ret = vconf_get_int(VCONF_NATIVE_INSTALLER_BACKEND_STATE, &state);
143         if (ret == -1) {
144                 d_msg_backend(DEBUG_ERR,
145                               "_get_backend_state: vconf_get_int FAIL\n");
146         } else {
147                 ret = state;
148         }
149         return ret;
150 }
151
152 int _set_backend_state(int state)
153 {
154         int ret = -1;
155
156         if (state == 0) {
157                 vconf_unset_recursive(VCONF_NATIVE_INSTALLER);
158         }
159
160         ret = vconf_set_int(VCONF_NATIVE_INSTALLER_BACKEND_STATE, state);
161         if (ret == -1) {
162                 d_msg_backend(DEBUG_ERR,
163                               "_set_backend_state: vconf_set_int FAIL\n");
164         }
165
166         return ret;
167 }
168
169 int _get_backend_state_info()
170 {
171         int ret = -1;
172         int state = -1;
173         ret = vconf_get_int(VCONF_NATIVE_INSTALLER_BACKEND_STATEINFO, &state);
174         if (ret == -1) {
175                 d_msg_backend(DEBUG_ERR,
176                               "_get_backend_state_info: vconf_get_int FAIL\n");
177         } else {
178                 ret = state;
179         }
180         return ret;
181 }
182
183 int _set_backend_state_info(int state)
184 {
185         int ret = -1;
186         ret = vconf_set_int(VCONF_NATIVE_INSTALLER_BACKEND_STATEINFO, state);
187         if (ret == -1)
188                 d_msg_backend(DEBUG_ERR,
189                               "_set_backend_state_info: vconf_set_int FAIL\n");
190
191         return ret;
192 }
193
194 int _get_last_input_info(char **pkgname, int *preqcommand, int *poptions)
195 {
196         int ret = -1;
197         if (!pkgname || !preqcommand || !poptions)
198                 return -1;
199         ret = vconf_get_int(VCONF_NATIVE_INSTALLER_LAST_REQUESTINFO_COMMAND,
200                             preqcommand);
201         if (ret == -1)
202                 d_msg_backend(DEBUG_ERR,
203                               "_get_last_input_info: VCONF_NATIVE_INSTALLER_LAST_REQUESTINFO_COMMAND: vconf_get_int FAIL\n");
204
205         ret = vconf_get_int(VCONF_NATIVE_INSTALLER_LAST_REQUESTINFO_OPTIONS,
206                             poptions);
207         if (ret == -1)
208                 d_msg_backend(DEBUG_ERR,
209                               "_get_last_input_info: VCONF_NATIVE_INSTALLER_LAST_REQUESTINFO_OPTIONS: vconf_get_int FAIL\n");
210
211         *pkgname =
212             vconf_get_str(VCONF_NATIVE_INSTALLER_LAST_REQUESTINFO_PKGNAME);
213         return 0;
214 }
215
216 void _save_last_input_info(char *pkgname, int reqcommand, int options)
217 {
218         keylist_t *kl = NULL;
219         kl = vconf_keylist_new();
220
221         vconf_keylist_add_int(kl,
222                               VCONF_NATIVE_INSTALLER_LAST_REQUESTINFO_COMMAND,
223                               reqcommand);
224         vconf_keylist_add_str(kl,
225                               VCONF_NATIVE_INSTALLER_LAST_REQUESTINFO_PKGNAME,
226                               pkgname);
227         vconf_keylist_add_int(kl,
228                               VCONF_NATIVE_INSTALLER_LAST_REQUESTINFO_OPTIONS,
229                               options);
230
231         if (vconf_set(kl))
232                 d_msg_backend(DEBUG_ERR,
233                               "_save_last_input_info: Failure in writing vconf\n");
234
235         vconf_keylist_free(kl);
236 }
237
238
239
240 void _broadcast_status_notification(char *pkgname, char *key, char *val)
241 {
242         char *ppkgname = NULL;
243
244         if (gptrpkgname != NULL)
245                 ppkgname = gptrpkgname;
246         else
247                 ppkgname = pkgname;
248
249         d_msg_backend(DEBUG_INFO, "pkgname = %s, key = %s, val = %s\n",
250                       ppkgname, key, val);
251
252         
253         if (pi != NULL)
254                 pkgmgr_installer_send_signal(pi, PKGTYPE, ppkgname, key, val);
255         else
256                 d_msg_backend(DEBUG_ERR,
257                               "Failure in sending broadcast message\n");
258 }
259