]> git.0d.be Git - empathy.git/blob - libempathy/empathy-handler.c
Merge commit 'jtellier/confirm-lose-accounts-settings'
[empathy.git] / libempathy / empathy-handler.c
1 /*
2  * Copyright (C) 2007-2009 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  *          Sjoerd Simons <sjoerd.simons@collabora.co.uk>
20  *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
21  */
22
23 #include <config.h>
24
25 #include <telepathy-glib/dbus.h>
26 #include <telepathy-glib/proxy-subclass.h>
27 #include <telepathy-glib/gtypes.h>
28 #include <telepathy-glib/defs.h>
29 #include <telepathy-glib/svc-client.h>
30 #include <telepathy-glib/svc-generic.h>
31 #include <telepathy-glib/interfaces.h>
32
33 #include "empathy-handler.h"
34 #include "empathy-utils.h"
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
37 #include <libempathy/empathy-debug.h>
38
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyHandler)
40 typedef struct
41 {
42   EmpathyHandlerHandleChannelsFunc *handle_channels;
43   gpointer handle_channels_user_data;
44
45   EmpathyHandlerChannelsFunc *channels;
46   gpointer channels_user_data;
47
48   gchar *name;
49
50   GPtrArray *filters;
51   GStrv *capabilities;
52 } EmpathyHandlerPriv;
53
54 static void empathy_handler_client_handler_iface_init (gpointer g_iface,
55     gpointer g_iface_data);
56
57 G_DEFINE_TYPE_WITH_CODE (EmpathyHandler,
58     empathy_handler,
59     G_TYPE_OBJECT,
60     G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_DBUS_PROPERTIES,
61       tp_dbus_properties_mixin_iface_init);
62     G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CLIENT, NULL);
63     G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CLIENT_HANDLER,
64       empathy_handler_client_handler_iface_init);
65   );
66
67 static const gchar *empathy_handler_interfaces[] = {
68   TP_IFACE_CLIENT_HANDLER,
69   NULL
70 };
71
72 enum
73 {
74   PROP_INTERFACES = 1,
75   PROP_CHANNEL_FILTER,
76   PROP_CHANNELS,
77   PROP_CAPABILITIES,
78   PROP_NAME,
79 };
80
81 static GObject *
82 handler_constructor (GType type,
83     guint n_construct_params,
84     GObjectConstructParam *construct_params)
85 {
86   GObject *obj =
87     G_OBJECT_CLASS (empathy_handler_parent_class)->constructor
88       (type, n_construct_params, construct_params);
89   EmpathyHandler *handler = EMPATHY_HANDLER (obj);
90   EmpathyHandlerPriv *priv = GET_PRIV (handler);
91   TpDBusDaemon *dbus;
92   gchar *busname;
93   gchar *object_path;
94
95   priv = GET_PRIV (handler);
96
97   busname = g_strdup_printf (TP_CLIENT_BUS_NAME_BASE"%s", priv->name);
98   object_path = g_strdup_printf (TP_CLIENT_OBJECT_PATH_BASE"%s",
99     priv->name);
100
101   dbus = tp_dbus_daemon_dup (NULL);
102
103   g_assert (tp_dbus_daemon_request_name (dbus,
104     busname, TRUE, NULL));
105   dbus_g_connection_register_g_object (tp_get_bus (),
106     object_path, obj);
107
108   DEBUG ("Registered at '%s'", object_path);
109
110   g_free (busname);
111   g_free (object_path);
112   g_object_unref (dbus);
113
114   return G_OBJECT (handler);
115 }
116
117 static void
118 handler_finalize (GObject *object)
119 {
120   EmpathyHandlerPriv *priv = GET_PRIV (object);
121
122   if (priv->filters != NULL)
123     g_boxed_free (TP_ARRAY_TYPE_CHANNEL_CLASS_LIST, priv->filters);
124
125   if (priv->capabilities != NULL)
126     g_boxed_free (G_TYPE_STRV, priv->capabilities);
127
128   g_free (priv->name);
129 }
130
131 static void
132 handler_set_property (GObject *object,
133     guint property_id,
134     const GValue *value,
135     GParamSpec *pspec)
136 {
137   EmpathyHandler *handler = EMPATHY_HANDLER (object);
138   EmpathyHandlerPriv *priv = GET_PRIV (handler);
139
140   switch (property_id)
141     {
142       case PROP_CHANNEL_FILTER:
143         priv->filters = g_value_dup_boxed (value);
144         break;
145       case PROP_CAPABILITIES:
146         priv->capabilities = g_value_dup_boxed (value);
147         break;
148       case PROP_NAME:
149         priv->name = g_value_dup_string (value);
150         break;
151       default:
152         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
153         break;
154     }
155 }
156
157 static void
158 handler_get_property (GObject *object,
159     guint property_id,
160     GValue *value,
161     GParamSpec *pspec)
162 {
163   EmpathyHandler *self = EMPATHY_HANDLER (object);
164   EmpathyHandlerPriv *priv = GET_PRIV (self);
165
166   switch (property_id)
167     {
168       case PROP_INTERFACES:
169         g_value_set_boxed (value, empathy_handler_interfaces);
170         break;
171       case PROP_CHANNEL_FILTER:
172         g_value_set_boxed (value, priv->filters);
173         break;
174       case PROP_CAPABILITIES:
175         g_value_set_boxed (value, priv->capabilities);
176         break;
177       case PROP_NAME:
178         g_value_set_string (value, priv->name);
179         break;
180       case PROP_CHANNELS:
181         {
182           GList *l, *channels = NULL;
183           GPtrArray *array = g_ptr_array_new ();
184
185           if (priv->channels != NULL)
186             channels =  priv->channels (self, priv->channels_user_data);
187
188           for (l = channels ; l != NULL; l = g_list_next (l))
189             {
190               TpProxy *channel = TP_PROXY (l->data);
191               g_ptr_array_add (array,
192                 (gpointer) tp_proxy_get_object_path (channel));
193             }
194           g_value_set_boxed (value, array);
195           g_ptr_array_free (array, TRUE);
196           break;
197         }
198       default:
199         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
200         break;
201     }
202 }
203
204 static void
205 empathy_handler_class_init (EmpathyHandlerClass *klass)
206 {
207   GObjectClass *object_class = G_OBJECT_CLASS (klass);
208   GParamSpec *param_spec;
209
210   static TpDBusPropertiesMixinPropImpl client_props[] = {
211     { "Interfaces", "interfaces", NULL },
212     { NULL }
213   };
214   static TpDBusPropertiesMixinPropImpl client_handler_props[] = {
215     { "HandlerChannelFilter", "channel-filter", NULL },
216     { "HandledChannels", "channels", NULL },
217     { "Capabilities", "capabilities", NULL },
218     { NULL }
219   };
220   static TpDBusPropertiesMixinIfaceImpl prop_interfaces[] = {
221     { TP_IFACE_CLIENT,
222       tp_dbus_properties_mixin_getter_gobject_properties,
223       NULL,
224       client_props
225     },
226     { TP_IFACE_CLIENT_HANDLER,
227       tp_dbus_properties_mixin_getter_gobject_properties,
228       NULL,
229       client_handler_props
230     },
231     { NULL }
232   };
233
234   object_class->finalize = handler_finalize;
235   object_class->constructor = handler_constructor;
236
237   object_class->get_property = handler_get_property;
238   object_class->set_property = handler_set_property;
239
240   param_spec = g_param_spec_boxed ("interfaces", "interfaces",
241     "Available D-Bus interfaces",
242     G_TYPE_STRV,
243     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
244   g_object_class_install_property (object_class, PROP_INTERFACES, param_spec);
245
246   param_spec = g_param_spec_boxed ("channel-filter", "channel-filter",
247     "Filter for channels this handles",
248     TP_ARRAY_TYPE_CHANNEL_CLASS_LIST,
249     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
250   g_object_class_install_property (object_class,
251     PROP_CHANNEL_FILTER, param_spec);
252
253   param_spec = g_param_spec_boxed ("capabilities", "capabilities",
254     "Filter for channels this handles",
255     G_TYPE_STRV,
256     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
257   g_object_class_install_property (object_class,
258     PROP_CAPABILITIES, param_spec);
259
260   param_spec = g_param_spec_boxed ("channels", "channels",
261     "List of channels we're handling",
262     EMPATHY_ARRAY_TYPE_OBJECT,
263     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
264   g_object_class_install_property (object_class,
265     PROP_CHANNELS, param_spec);
266
267   param_spec = g_param_spec_string ("name", "name",
268     "The local name of the handler",
269     NULL,
270     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
271   g_object_class_install_property (object_class,
272     PROP_NAME, param_spec);
273
274   g_type_class_add_private (object_class, sizeof (EmpathyHandlerPriv));
275
276   klass->dbus_props_class.interfaces = prop_interfaces;
277   tp_dbus_properties_mixin_class_init (object_class,
278     G_STRUCT_OFFSET (EmpathyHandlerClass, dbus_props_class));
279 }
280
281 static void
282 empathy_handler_init (EmpathyHandler *handler)
283 {
284   EmpathyHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (handler,
285     EMPATHY_TYPE_HANDLER, EmpathyHandlerPriv);
286
287   handler->priv = priv;
288 }
289
290 EmpathyHandler *
291 empathy_handler_new (const gchar *name,
292     GPtrArray *filters,
293     GStrv capabilities)
294 {
295   return EMPATHY_HANDLER (
296     g_object_new (EMPATHY_TYPE_HANDLER,
297       "name", name,
298       "channel-filter", filters,
299       "capabilities", capabilities,
300       NULL));
301 }
302
303 static void
304 empathy_handler_handle_channels (TpSvcClientHandler *self,
305     const gchar *account_path,
306     const gchar *connection_path,
307     const GPtrArray *channels,
308     const GPtrArray *requests_satisfied,
309     guint64 timestamp,
310     GHashTable *handler_info,
311     DBusGMethodInvocation *context)
312 {
313   EmpathyHandler *handler = EMPATHY_HANDLER (self);
314   EmpathyHandlerPriv *priv = GET_PRIV (handler);
315   GError *error = NULL;
316
317   if (!priv->handle_channels)
318     {
319       error = g_error_new_literal (TP_ERRORS,
320         TP_ERROR_NOT_AVAILABLE,
321         "No handler function setup");
322       goto error;
323     }
324
325   if (!priv->handle_channels (handler, account_path, connection_path,
326       channels, requests_satisfied, timestamp, handler_info,
327       priv->handle_channels_user_data, &error))
328     goto error;
329
330   tp_svc_client_handler_return_from_handle_channels (context);
331   return;
332
333 error:
334   dbus_g_method_return_error (context, error);
335   g_error_free (error);
336 }
337
338 static void
339 empathy_handler_client_handler_iface_init (gpointer g_iface,
340     gpointer g_iface_data)
341 {
342   TpSvcClientHandlerClass *klass = (TpSvcClientHandlerClass *) g_iface;
343
344   tp_svc_client_handler_implement_handle_channels (klass,
345     empathy_handler_handle_channels);
346 }
347
348 void
349 empathy_handler_set_handle_channels_func (EmpathyHandler *handler,
350     EmpathyHandlerHandleChannelsFunc *func,
351     gpointer user_data)
352 {
353   EmpathyHandlerPriv *priv = GET_PRIV (handler);
354
355   priv->handle_channels = func;
356   priv->handle_channels_user_data = user_data;
357 }
358
359 void
360 empathy_handler_set_channels_func (EmpathyHandler *handler,
361     EmpathyHandlerChannelsFunc *func,
362     gpointer user_data)
363 {
364   EmpathyHandlerPriv *priv = GET_PRIV (handler);
365
366   priv->channels = func;
367   priv->channels_user_data = user_data;
368 }
369