1 #include "msgport-service.h"
2 #include "msgport-utils.h"
3 #include "common/dbus-service-glue.h"
4 #include "common/log.h"
12 MsgPortDbusGlueService *proxy;
13 guint on_messge_signal_id;
14 messageport_message_cb client_cb;
17 G_DEFINE_TYPE(MsgPortService, msgport_service, G_TYPE_OBJECT)
20 _service_dispose (GObject *self)
22 MsgPortService *service = MSGPORT_SERVICE (self);
24 g_clear_object (&service->proxy);
26 G_OBJECT_CLASS(msgport_service_parent_class)->dispose (self);
30 msgport_service_class_init (MsgPortServiceClass *klass)
32 GObjectClass *g_klass = G_OBJECT_CLASS(klass);
34 g_klass->dispose = _service_dispose;
38 msgport_service_init (MsgPortService *service)
40 service->proxy = NULL;
41 service->client_cb = NULL;
42 service->on_messge_signal_id = 0;
46 _on_got_message (MsgPortService *service, GVariant *data, const gchar *remote_app_id, const gchar *remote_port, gboolean remote_is_trusted, gpointer userdata)
49 gchar *str_data = g_variant_print (data, TRUE);
50 DBG ("Message received : %s(%"G_GSIZE_FORMAT")", str_data, g_variant_n_children (data));
53 bundle *b = bundle_from_variant_map (data);
55 service->client_cb (msgport_dbus_glue_service_get_id (service->proxy), remote_app_id, remote_port, remote_is_trusted, b);
61 msgport_service_new (GDBusConnection *connection, const gchar *path, messageport_message_cb message_cb)
63 //GVariant *v_prop = NULL;
66 MsgPortService *service = g_object_new (MSGPORT_TYPE_SERVICE, NULL);
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,
81 v_prop = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (service->proxy), "Id");
83 service->id = g_variant_get_uint32 (v_prop);
84 g_variant_unref (v_prop);
86 WARN ("Could not load property name 'Id' of service '%s'", path);
89 v_prop = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (service->proxy), "PortName");
91 service->name = g_strdup (g_variant_get_string (v_prop, NULL));
92 g_variant_unref (v_prop);
94 WARN ("Could not load property name 'PortName' of service '%s'", path);
97 v_prop = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (service->proxy), "IsTrusted");
99 service->is_trusted = g_variant_get_boolean (v_prop);
100 g_object_unref (v_prop);
102 WARN ("Could not load property name 'IsTrusted' of service '%s'", path);
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);
112 msgport_service_id (MsgPortService *service)
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);
117 return msgport_dbus_glue_service_get_id (service->proxy);
121 msgport_service_name (MsgPortService *service)
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);
126 return msgport_dbus_glue_service_get_port_name (service->proxy);
130 msgport_service_is_trusted (MsgPortService *service)
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);
135 return msgport_dbus_glue_service_get_is_trusted (service->proxy);
139 msgport_service_unregister (MsgPortService *service)
141 g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), FALSE);
142 g_return_val_if_fail (service->proxy, FALSE);
144 /* fire and forget */
145 return msgport_dbus_glue_service_call_unregister_sync (service->proxy, NULL, NULL);
149 msgport_service_send_message (MsgPortService *service, guint remote_service_id, GVariant *message)
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);
156 msgport_dbus_glue_service_call_send_message_sync (service->proxy, remote_service_id, message, NULL, &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;
164 return MESSAGEPORT_ERROR_NONE;