tizen 2.3 release
[framework/appfw/slp-pkgmgr.git] / comm / comm_client.c
1 /*
2  * slp-pkgmgr
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 "comm_config.h"
28 #include "comm_client.h"
29 #include "comm_pkg_mgr_client_dbus_bindings.h"
30 #include "comm_status_broadcast_client_dbus_bindings.h"
31 #include "comm_status_broadcast_signal_marshaller.h"
32 #include <stdlib.h>
33 #include <string.h>
34
35 struct comm_client {
36         /* Resources to be freed */
37         DBusGConnection *conn;
38         GError *err;
39         DBusGProxy *request_proxy;
40         DBusGProxy *signal_proxy;
41         char *pkgid;
42
43         status_cb signal_cb;
44         void *signal_cb_data;
45 };
46
47 comm_client *comm_client_new(void)
48 {
49         comm_client *cc = NULL;
50
51         cc = calloc(1, sizeof(comm_client));
52         if (NULL == cc)
53                 return NULL;
54
55         cc->conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &(cc->err));
56         if (NULL == cc->conn) {
57                 g_printerr("Failed to open connection to dbus: %s\n",
58                            cc->err->message);
59                 g_error_free(cc->err);
60                 cc->err = NULL;
61                 comm_client_free(cc);
62                 return NULL;
63         }
64
65         cc->request_proxy = dbus_g_proxy_new_for_name(cc->conn,
66                                                 COMM_PKG_MGR_DBUS_SERVICE,
67                                 /* name : written in service file */
68                                                 COMM_PKG_MGR_DBUS_PATH,
69                                 /* path : written as a node in xml */
70                                                 COMM_PKG_MGR_DBUS_INTERFACE
71                         /* interface : written as an interface in xml */
72             );
73
74         return cc;
75 }
76
77 int comm_client_free(comm_client *cc)
78 {
79         if (NULL == cc)
80                 return -1;
81
82         if (cc->err)
83                 g_error_free(cc->err);
84         if (cc->conn)
85                 dbus_g_connection_unref(cc->conn);
86         if (cc->request_proxy)
87                 g_object_unref(cc->request_proxy);
88         if (cc->signal_proxy)
89                 g_object_unref(cc->signal_proxy);
90         if (cc->pkgid)
91                 free(cc->pkgid);
92
93         free(cc);
94
95         return 0;
96 }
97
98 static void
99 status_signal_handler(DBusGProxy *proxy,
100                       const char *req_id,
101                       const char *pkg_type,
102                       const char *pkgid,
103                       const char *key, const char *val, gpointer data)
104 {
105         comm_client *cc = (comm_client *) data;
106
107         SECURE_LOGD("Got signal: %s/%s/%s/%s/%s", req_id, pkg_type,
108                                  pkgid, key, val);
109         if (cc->signal_cb) {
110                 if (cc->pkgid && pkgid &&
111                         0 == strncmp(cc->pkgid, pkgid,
112                                      strlen(cc->pkgid))) {
113                         dbg("Run signal handler");
114                         cc->signal_cb(cc->signal_cb_data, req_id, pkg_type,
115                                       pkgid, key, val);
116                 } else {
117                         dbg("pkgid is different. (My pkgid:%s)"
118                         " Though pass signal to user callback.", cc->pkgid);
119                         cc->signal_cb(cc->signal_cb_data, req_id, pkg_type,
120                                       pkgid, key, val);
121                 }
122         } else {
123                 dbg("No signal handler is set. Do nothing.");
124         }
125 }
126
127 int
128 comm_client_request(comm_client *cc, const char *req_id, const int req_type,
129                     const char *pkg_type, const char *pkgid,
130                     const char *args, const char *cookie)
131 {
132         gboolean r;
133         gint ret = COMM_RET_ERROR;
134
135         SECURE_LOGD("got request:%s/%d/%s/%s/%s/%s\n", req_id, req_type, pkg_type,
136             pkgid, args, cookie);
137
138         if (!pkgid)
139                 pkgid = "";     /* NULL check */
140
141         r = com_samsung_slp_pkgmgr_request(cc->request_proxy, req_id, req_type,
142                                            pkg_type, pkgid, args, cookie,
143                                            &ret, &(cc->err));
144         if (TRUE == r) {
145                 //ret = COMM_RET_OK;
146         } else {
147                 g_printerr("Failed to send request via dbus: %s\n",
148                            cc->err->message);
149                 if (cc->err) {
150                         g_error_free(cc->err);
151                         cc->err = NULL;
152                 }
153                 return ret;
154         }
155         dbg("Request ok");
156
157         if (cc->pkgid) {
158                 dbg("freeing pkgid");
159                 free(cc->pkgid);
160                 dbg("freed pkgid");
161         }
162         cc->pkgid = strdup(pkgid);
163
164         dbg("ret:%d", ret);
165
166         return ret;
167 }
168
169 int
170 comm_client_set_status_callback(comm_client *cc, status_cb cb, void *cb_data)
171 {
172         /* set callback */
173         if (!cc->signal_proxy) {
174                 dbg("signal_proxy is NULL. Try to create a proxy for signal.");
175                 cc->signal_proxy = dbus_g_proxy_new_for_name(cc->conn,
176                                      COMM_STATUS_BROADCAST_DBUS_SERVICE_PREFIX,
177                                      COMM_STATUS_BROADCAST_DBUS_PATH,
178                                      COMM_STATUS_BROADCAST_DBUS_INTERFACE);
179                 if (NULL == cc->signal_proxy) {
180                         g_printerr("Failed to create proxy for signal\n", NULL);
181                         return COMM_RET_ERROR;
182                 } else {
183                 }
184         } else {
185                 /* Proxy is existing. Do nothing. */
186         }
187
188         cc->signal_cb = cb;
189         cc->signal_cb_data = cb_data;
190
191         dbg("Register signal-type marshaller.");
192         dbus_g_object_register_marshaller(
193         g_cclosure_user_marshal_VOID__STRING_STRING_STRING_STRING_STRING,
194                 /* marshaller */
195         G_TYPE_NONE, /* return type */
196         G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
197         G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);  /* termination flag */
198
199         dbg("Add signal to proxy.");
200         dbus_g_proxy_add_signal(cc->signal_proxy,
201                                 COMM_STATUS_BROADCAST_SIGNAL_STATUS,
202                                 G_TYPE_STRING,
203                                 G_TYPE_STRING,
204                                 G_TYPE_STRING,
205                                 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
206
207         dbg("Connect signal to proxy.");
208
209         dbus_g_proxy_connect_signal(cc->signal_proxy,
210                                     COMM_STATUS_BROADCAST_SIGNAL_STATUS,
211                                     G_CALLBACK(status_signal_handler),
212                                     cc, NULL);
213
214         return 0;
215 }
216