Bump release : v1.0.1
[profile/ivi/message-port.git] / lib / msgport-service.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of message-port.
5  *
6  * Copyright (C) 2013 Intel Corporation.
7  *
8  * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  */
25
26 #include "msgport-service.h"
27 #include "msgport-utils.h"
28 #include "common/dbus-service-glue.h"
29 #include "common/log.h"
30 #include <bundle.h>
31
32
33 struct _MsgPortService
34 {
35     GObject parent;
36
37     MsgPortDbusGlueService *proxy;
38     guint                   on_messge_signal_id;
39     messageport_message_cb  client_cb;
40 };
41
42 G_DEFINE_TYPE(MsgPortService, msgport_service, G_TYPE_OBJECT)
43
44 static void
45 _service_dispose (GObject *self)
46 {
47     MsgPortService *service = MSGPORT_SERVICE (self);
48
49     g_clear_object (&service->proxy);
50
51     G_OBJECT_CLASS(msgport_service_parent_class)->dispose (self);
52 }
53
54 static void
55 msgport_service_class_init (MsgPortServiceClass *klass)
56 {
57     GObjectClass *g_klass = G_OBJECT_CLASS(klass);
58
59     g_klass->dispose = _service_dispose;
60 }
61
62 static void
63 msgport_service_init (MsgPortService *service)
64 {
65     service->proxy = NULL;
66     service->client_cb = NULL;
67     service->on_messge_signal_id = 0;
68 }
69
70 static void
71 _on_got_message (MsgPortService *service, GVariant *data, const gchar *remote_app_id, const gchar *remote_port, gboolean remote_is_trusted, gpointer userdata)
72 {
73 #ifdef ENABLE_DEBUG
74     gchar *str_data = g_variant_print (data, TRUE);
75     DBG ("Message received : '%s' from '%s':'%s':%d",
76             str_data, remote_app_id, remote_port, remote_is_trusted);
77     g_free (str_data);
78 #endif
79     bundle *b = bundle_from_variant_map (data);
80
81     /*
82      * NOTE: wrt plugin cannot handle empty strings for port_id and app_id.
83      * It is expecting NULL in this case, But we get empty stirng from Dbus.
84      * So check for this case:
85      */
86     if (remote_app_id && !remote_app_id[0]) remote_app_id = NULL;
87     if (remote_port   && !remote_port[0])   remote_port = NULL;
88
89     service->client_cb (msgport_dbus_glue_service_get_id (service->proxy), remote_app_id, remote_port, remote_is_trusted, b);
90 }
91
92 MsgPortService *
93 msgport_service_new (GDBusConnection *connection, const gchar *path, messageport_message_cb message_cb)
94 {
95     GError *error = NULL;
96
97     MsgPortService *service = g_object_new (MSGPORT_TYPE_SERVICE, NULL);
98     if (!service) {
99         return NULL;
100     }
101
102     service->proxy = msgport_dbus_glue_service_proxy_new_sync (connection,
103                 G_DBUS_PROXY_FLAGS_NONE, NULL, path, NULL, &error);
104     if (!service->proxy) {
105         g_object_unref (service);
106         WARN ("failed create servie proxy for path '%s' : %s", path, error->message);
107         g_error_free (error);
108         return NULL;
109     }
110
111     service->client_cb = message_cb;
112     service->on_messge_signal_id = g_signal_connect_swapped (service->proxy, "on-message", G_CALLBACK (_on_got_message), service);
113
114     return service;
115 }
116
117 guint
118 msgport_service_id (MsgPortService *service)
119 {
120     g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), 0);
121     g_return_val_if_fail (service->proxy && MSGPORT_DBUS_GLUE_IS_SERVICE (service->proxy), 0);
122
123     return msgport_dbus_glue_service_get_id (service->proxy);
124 }
125
126 const gchar *
127 msgport_service_name (MsgPortService *service)
128 {
129     g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), NULL);
130     g_return_val_if_fail (service->proxy && MSGPORT_DBUS_GLUE_IS_SERVICE (service->proxy), NULL);
131
132     return msgport_dbus_glue_service_get_port_name (service->proxy);
133 }
134
135 gboolean
136 msgport_service_is_trusted (MsgPortService *service)
137 {
138     g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), FALSE);
139     g_return_val_if_fail (service->proxy && MSGPORT_DBUS_GLUE_IS_SERVICE (service->proxy), FALSE);
140
141     return msgport_dbus_glue_service_get_is_trusted (service->proxy);
142 }
143
144 void
145 msgport_service_set_message_handler (MsgPortService *service, messageport_message_cb handler)
146 {
147     g_return_if_fail (service && MSGPORT_IS_SERVICE (service));
148
149     service->client_cb = handler;
150 }
151
152 gboolean
153 msgport_service_unregister (MsgPortService *service)
154 {
155     g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), FALSE);
156     g_return_val_if_fail (service->proxy, FALSE);
157
158     /* fire and forget */
159     return msgport_dbus_glue_service_call_unregister_sync (service->proxy, NULL, NULL);
160 }
161
162 messageport_error_e
163 msgport_service_send_message (MsgPortService *service, guint remote_service_id, GVariant *message)
164 {
165     GError *error = NULL;
166     g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), MESSAGEPORT_ERROR_IO_ERROR);
167     g_return_val_if_fail (service->proxy, MESSAGEPORT_ERROR_IO_ERROR);
168     g_return_val_if_fail (message, MESSAGEPORT_ERROR_INVALID_PARAMETER);
169
170     msgport_dbus_glue_service_call_send_message_sync (service->proxy, remote_service_id, message, NULL, &error);
171
172     if (error) {
173         messageport_error_e err = msgport_daemon_error_to_error (error);
174         WARN ("Fail to send message on service %p to %d : %s", service, remote_service_id, error->message);
175         g_error_free (error);
176         return err;
177     }
178
179     return MESSAGEPORT_ERROR_NONE;
180 }