Add aul_widget_instance_get_content()
[platform/core/appfw/aul-1.git] / src / widget.c
1 /*
2  * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #define _GNU_SOURCE
18 #include <stdlib.h>
19 #include <glib.h>
20 #include <bundle.h>
21 #include "aul.h"
22 #include "aul_util.h"
23 #include "aul_sock.h"
24 #include "aul_cmd.h"
25 #include "aul_error.h"
26 #include "launch.h"
27
28 static const char *__to_appid(const char *widget_id)
29 {
30         const char *appid;
31         appid = g_strstr_len(widget_id, strlen(widget_id), "@") + 1;
32         if (appid != (const char *)1) {
33                 if (appid > widget_id + (sizeof(char) * strlen(widget_id)))
34                         appid = (char *)widget_id;
35         } else {
36                 appid = (char *)widget_id;
37         }
38
39         return appid;
40 }
41
42 API int aul_widget_instance_add(const char *widget_id, const char *instance_id)
43 {
44         int ret;
45         bundle *kb;
46
47         if (widget_id == NULL || instance_id == NULL)
48                 return AUL_R_EINVAL;
49
50         kb = bundle_create();
51         if (kb == NULL) {
52                 _E("out of memory");
53                 return AUL_R_ERROR;
54         }
55
56         bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
57         bundle_add_str(kb, AUL_K_WIDGET_INSTANCE_ID, instance_id);
58
59         ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), WIDGET_ADD, kb,
60                 AUL_SOCK_NONE);
61
62         bundle_free(kb);
63         if (ret < 0)
64                 return aul_error_convert(ret);
65
66         return AUL_R_OK;
67 }
68
69 API int aul_widget_instance_del(const char *widget_id, const char *instance_id)
70 {
71         int ret;
72         bundle *kb;
73
74         if (widget_id == NULL || instance_id == NULL)
75                 return AUL_R_EINVAL;
76
77         kb = bundle_create();
78         if (kb == NULL) {
79                 _E("out of memory");
80                 return AUL_R_ERROR;
81         }
82
83         bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
84         bundle_add_str(kb, AUL_K_WIDGET_INSTANCE_ID, instance_id);
85
86         ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), WIDGET_DEL, kb,
87                 AUL_SOCK_NONE);
88
89         bundle_free(kb);
90         if (ret < 0)
91                 return aul_error_convert(ret);
92
93         return AUL_R_OK;
94 }
95
96 struct __cb_data {
97         aul_widget_instance_foreach_cb cb;
98         void *data;
99 };
100
101 static void __foreach_cb(const char *key, const int type,
102                 const bundle_keyval_t *kv, void *user_data)
103 {
104         struct __cb_data *cb_data = (struct __cb_data *)user_data;
105
106         cb_data->cb(key, cb_data->data);
107 }
108
109 API int aul_widget_instance_foreach(const char *widget_id,
110                 aul_widget_instance_foreach_cb cb, void *data)
111 {
112         int ret;
113         int fd;
114         bundle *kb;
115         app_pkt_t *pkt = NULL;
116         bundle *list_kb = NULL;
117         struct __cb_data cb_data;
118
119         if (widget_id == NULL)
120                 return AUL_R_EINVAL;
121
122         kb = bundle_create();
123         if (kb == NULL) {
124                 _E("out of memory");
125                 return AUL_R_ERROR;
126         }
127
128         bundle_add_str(kb, AUL_K_APPID, __to_appid(widget_id));
129         bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
130
131         fd = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), WIDGET_LIST, kb,
132                 AUL_SOCK_ASYNC);
133
134         if (fd > 0) {
135                 ret = aul_sock_recv_reply_pkt(fd, &pkt);
136                 if (ret < 0 || pkt == NULL) {
137                         _E("failed to get instance list of %s", widget_id);
138                 } else {
139                         list_kb = bundle_decode(pkt->data, pkt->len);
140                         if (list_kb) {
141                                 cb_data.cb = cb;
142                                 cb_data.data = data;
143                                 bundle_foreach(list_kb, __foreach_cb, &cb_data);
144                         }
145                 }
146         } else {
147                 ret = fd;
148         }
149
150         if (pkt)
151                 free(pkt);
152
153         bundle_free(kb);
154
155         if (ret < 0)
156                 return aul_error_convert(ret);
157
158         return AUL_R_OK;
159 }
160
161 API int aul_widget_instance_update(const char *widget_id,
162                 const char *instance_id, bundle *param)
163 {
164         int ret;
165         bundle *kb = param;
166         const char *appid;
167
168         if (widget_id == NULL)
169                 return AUL_R_EINVAL;
170
171         if (kb == NULL)
172                 kb = bundle_create();
173
174         if (kb == NULL) {
175                 _E("out of memory");
176                 return AUL_R_ERROR;
177         }
178
179         appid = __to_appid(widget_id);
180
181         bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
182
183         if (instance_id)
184                 bundle_add_str(kb, AUL_K_WIDGET_INSTANCE_ID, instance_id);
185
186         ret = app_request_to_launchpad_for_uid(WIDGET_UPDATE, appid, kb,
187                         getuid());
188
189         if (param == NULL)
190                 bundle_free(kb);
191
192         return ret;
193 }
194
195 API int aul_widget_instance_get_content(const char *widget_id,
196                 const char *instance_id, char **content)
197 {
198         int ret;
199         bundle *kb;
200         int fd = 0;
201         app_pkt_t *pkt = NULL;
202
203         if (widget_id == NULL || instance_id == NULL || content == NULL)
204                 return AUL_R_EINVAL;
205
206         kb = bundle_create();
207         if (kb == NULL) {
208                 _E("out of memory");
209                 return AUL_R_ERROR;
210         }
211
212         bundle_add_str(kb, AUL_K_APPID, __to_appid(widget_id));
213         bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
214         bundle_add_str(kb, AUL_K_WIDGET_INSTANCE_ID, instance_id);
215
216         ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), WIDGET_GET_CONTENT,
217                         kb, AUL_SOCK_ASYNC);
218         if (ret > 0) {
219                 ret = aul_sock_recv_reply_sock_fd(ret, &fd, 1);
220                 if (ret == 0) {
221                         ret = aul_sock_recv_reply_pkt(fd, &pkt);
222                         if (ret == 0 && pkt && pkt->cmd == 0) {
223                                 *content = strdup((const char *)pkt->data);
224                                 _D("recieved content: %s", *content);
225                         } else {
226                                 if (pkt)
227                                         ret = pkt->cmd;
228
229                                 _E("failed to get content");
230                         }
231                 } else {
232                         _E("failed to get socket fd:%d", ret);
233                 }
234         }
235
236         bundle_free(kb);
237
238         if (ret < 0)
239                 ret = aul_error_convert(ret);
240
241         return ret;
242 }
243
244 API int aul_widget_instance_count(const char *widget_id)
245 {
246         int ret;
247         bundle *kb;
248
249         if (widget_id == NULL)
250                 return AUL_R_EINVAL;
251
252         kb = bundle_create();
253         if (kb == NULL) {
254                 _E("out of memory");
255                 return AUL_R_ERROR;
256         }
257
258         bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
259         ret = app_send_cmd(AUL_UTIL_PID, WIDGET_COUNT, kb);
260         bundle_free(kb);
261
262         return ret;
263 }
264