db698b3a17923faf977c6ebd96e606182d2db42c
[profile/ivi/message-port.git] / lib / msgport-service.c
1 #include "msgport-service.h"
2 #include "msgport-utils.h"
3 #include "common/dbus-service-glue.h"
4 #include "common/log.h"
5 #include <bundle.h>
6
7
8 struct _MsgPortService
9 {
10     GObject parent;
11
12     MsgPortDbusGlueService *proxy;
13     guint                   on_messge_signal_id;
14     messageport_message_cb  client_cb;
15 };
16
17 G_DEFINE_TYPE(MsgPortService, msgport_service, G_TYPE_OBJECT)
18
19 static void
20 _service_dispose (GObject *self)
21 {
22     MsgPortService *service = MSGPORT_SERVICE (self);
23
24     g_clear_object (&service->proxy);
25
26     G_OBJECT_CLASS(msgport_service_parent_class)->dispose (self);
27 }
28
29 static void
30 msgport_service_class_init (MsgPortServiceClass *klass)
31 {
32     GObjectClass *g_klass = G_OBJECT_CLASS(klass);
33
34     g_klass->dispose = _service_dispose;
35 }
36
37 static void
38 msgport_service_init (MsgPortService *service)
39 {
40     service->proxy = NULL;
41     service->client_cb = NULL;
42     service->on_messge_signal_id = 0;
43 }
44
45 static void
46 _on_got_message (MsgPortService *service, GVariant *data, const gchar *remote_app_id, const gchar *remote_port, gboolean remote_is_trusted, gpointer userdata)
47 {
48 #ifdef ENABLE_DEBUG
49     gchar *str_data = g_variant_print (data, TRUE);
50     DBG ("Message received : %s(%"G_GSIZE_FORMAT")", str_data, g_variant_n_children (data));
51     g_free (str_data);
52 #endif
53     bundle *b = bundle_from_variant_map (data);
54
55     service->client_cb (msgport_dbus_glue_service_get_id (service->proxy), remote_app_id, remote_port, remote_is_trusted, b);
56
57     bundle_free (b);
58 }
59
60 MsgPortService *
61 msgport_service_new (GDBusConnection *connection, const gchar *path, messageport_message_cb message_cb)
62 {
63     //GVariant *v_prop = NULL;
64     GError *error = NULL;
65
66     MsgPortService *service = g_object_new (MSGPORT_TYPE_SERVICE, NULL);
67     if (!service) {
68         return NULL;
69     }
70
71     service->proxy = msgport_dbus_glue_service_proxy_new_sync (connection,
72                 G_DBUS_PROXY_FLAGS_NONE, NULL, path, NULL, &error);
73     if (!service->proxy) {
74         g_object_unref (service);
75         WARN ("failed create servie proxy for path '%s' : %s", path,
76                 error->message);
77         g_error_free (error);
78         return NULL;
79     }
80 #if 0
81     v_prop = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (service->proxy), "Id");
82     if (v_prop) {
83         service->id = g_variant_get_uint32 (v_prop);
84         g_variant_unref (v_prop);
85     } else {
86         WARN ("Could not load property name 'Id' of service '%s'", path);
87     }
88
89     v_prop = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (service->proxy), "PortName");
90     if (v_prop) {
91         service->name = g_strdup (g_variant_get_string (v_prop, NULL));
92         g_variant_unref (v_prop);
93     } else {
94         WARN ("Could not load property name 'PortName' of service '%s'", path);
95     }
96
97     v_prop = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (service->proxy), "IsTrusted");
98     if (v_prop) {
99         service->is_trusted = g_variant_get_boolean (v_prop);
100         g_object_unref (v_prop);
101     } else {
102         WARN ("Could not load property name 'IsTrusted' of service '%s'", path);
103     }
104 #endif
105     service->client_cb = message_cb;
106     service->on_messge_signal_id = g_signal_connect_swapped (service->proxy, "on-message", G_CALLBACK (_on_got_message), service);
107
108     return service;
109 }
110
111 guint
112 msgport_service_id (MsgPortService *service)
113 {
114     g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), 0);
115     g_return_val_if_fail (service->proxy && MSGPORT_DBUS_GLUE_IS_SERVICE (service->proxy), 0);
116
117     return msgport_dbus_glue_service_get_id (service->proxy);
118 }
119
120 const gchar *
121 msgport_service_name (MsgPortService *service)
122 {
123     g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), NULL);
124     g_return_val_if_fail (service->proxy && MSGPORT_DBUS_GLUE_IS_SERVICE (service->proxy), NULL);
125
126     return msgport_dbus_glue_service_get_port_name (service->proxy);
127 }
128
129 gboolean
130 msgport_service_is_trusted (MsgPortService *service)
131 {
132     g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), FALSE);
133     g_return_val_if_fail (service->proxy && MSGPORT_DBUS_GLUE_IS_SERVICE (service->proxy), FALSE);
134
135     return msgport_dbus_glue_service_get_is_trusted (service->proxy);
136 }
137
138 gboolean
139 msgport_service_unregister (MsgPortService *service)
140 {
141     g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), FALSE);
142     g_return_val_if_fail (service->proxy, FALSE);
143
144     /* fire and forget */
145     return msgport_dbus_glue_service_call_unregister_sync (service->proxy, NULL, NULL);
146 }
147
148 messageport_error_e
149 msgport_service_send_message (MsgPortService *service, guint remote_service_id, GVariant *message)
150 {
151     GError *error = NULL;
152     g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), MESSAGEPORT_ERROR_IO_ERROR);
153     g_return_val_if_fail (service->proxy, MESSAGEPORT_ERROR_IO_ERROR);
154     g_return_val_if_fail (message, MESSAGEPORT_ERROR_INVALID_PARAMETER);
155
156     msgport_dbus_glue_service_call_send_message_sync (service->proxy, remote_service_id, message, NULL, &error);
157
158     if (error) {
159         WARN ("Fail to send message on service %p to %d : %s", service, remote_service_id, error->message);
160         g_error_free (error);
161         return MESSAGEPORT_ERROR_IO_ERROR;
162     }
163
164     return MESSAGEPORT_ERROR_NONE;
165 }
166