]> git.0d.be Git - empathy.git/blob - libempathy/empathy-handler.c
add myself to AUTHORS
[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   gchar *busname;
50
51   GPtrArray *filters;
52   GStrv *capabilities;
53
54   gboolean dispose_run;
55 } EmpathyHandlerPriv;
56
57 static void empathy_handler_client_handler_iface_init (gpointer g_iface,
58     gpointer g_iface_data);
59
60 G_DEFINE_TYPE_WITH_CODE (EmpathyHandler,
61     empathy_handler,
62     G_TYPE_OBJECT,
63     G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_DBUS_PROPERTIES,
64       tp_dbus_properties_mixin_iface_init);
65     G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CLIENT, NULL);
66     G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CLIENT_HANDLER,
67       empathy_handler_client_handler_iface_init);
68   );
69
70 static const gchar *empathy_handler_interfaces[] = {
71   TP_IFACE_CLIENT_HANDLER,
72   NULL
73 };
74
75 enum
76 {
77   PROP_INTERFACES = 1,
78   PROP_CHANNEL_FILTER,
79   PROP_CHANNELS,
80   PROP_CAPABILITIES,
81   PROP_NAME,
82 };
83
84 static GObject *
85 handler_constructor (GType type,
86     guint n_construct_params,
87     GObjectConstructParam *construct_params)
88 {
89   GObject *obj =
90     G_OBJECT_CLASS (empathy_handler_parent_class)->constructor
91       (type, n_construct_params, construct_params);
92   EmpathyHandler *handler = EMPATHY_HANDLER (obj);
93   EmpathyHandlerPriv *priv = GET_PRIV (handler);
94   TpDBusDaemon *dbus;
95   gchar *object_path;
96
97   priv = GET_PRIV (handler);
98
99   priv->busname = g_strdup_printf (TP_CLIENT_BUS_NAME_BASE"%s", priv->name);
100   object_path = g_strdup_printf (TP_CLIENT_OBJECT_PATH_BASE"%s",
101     priv->name);
102
103   dbus = tp_dbus_daemon_dup (NULL);
104
105   g_assert (tp_dbus_daemon_request_name (dbus,
106     priv->busname, TRUE, NULL));
107   dbus_g_connection_register_g_object (tp_get_bus (),
108     object_path, obj);
109
110   DEBUG ("Registered at '%s'", object_path);
111
112   g_free (object_path);
113   g_object_unref (dbus);
114
115   return G_OBJECT (handler);
116 }
117
118 static void
119 handler_dispose (GObject *object)
120 {
121   EmpathyHandlerPriv *priv = GET_PRIV (object);
122   TpDBusDaemon *dbus;
123
124   if (priv->dispose_run)
125     return;
126
127   priv->dispose_run = TRUE;
128
129   dbus = tp_dbus_daemon_dup (NULL);
130
131   tp_dbus_daemon_release_name (dbus, priv->busname, NULL);
132
133   g_object_unref (dbus);
134
135   if (G_OBJECT_CLASS (empathy_handler_parent_class)->dispose != NULL)
136     G_OBJECT_CLASS (empathy_handler_parent_class)->dispose (object);
137 }
138
139 static void
140 handler_finalize (GObject *object)
141 {
142   EmpathyHandlerPriv *priv = GET_PRIV (object);
143
144   if (priv->filters != NULL)
145     g_boxed_free (TP_ARRAY_TYPE_CHANNEL_CLASS_LIST, priv->filters);
146
147   if (priv->capabilities != NULL)
148     g_boxed_free (G_TYPE_STRV, priv->capabilities);
149
150   g_free (priv->name);
151   g_free (priv->busname);
152
153   if (G_OBJECT_CLASS (empathy_handler_parent_class)->finalize != NULL)
154     G_OBJECT_CLASS (empathy_handler_parent_class)->finalize (object);
155 }
156
157 static void
158 handler_set_property (GObject *object,
159     guint property_id,
160     const GValue *value,
161     GParamSpec *pspec)
162 {
163   EmpathyHandler *handler = EMPATHY_HANDLER (object);
164   EmpathyHandlerPriv *priv = GET_PRIV (handler);
165
166   switch (property_id)
167     {
168       case PROP_CHANNEL_FILTER:
169         priv->filters = g_value_dup_boxed (value);
170         if (priv->filters == NULL)
171           priv->filters = g_ptr_array_new ();
172         break;
173       case PROP_CAPABILITIES:
174         priv->capabilities = g_value_dup_boxed (value);
175         break;
176       case PROP_NAME:
177         priv->name = g_value_dup_string (value);
178         if (EMP_STR_EMPTY (priv->name))
179           {
180             TpDBusDaemon *bus;
181
182             bus = tp_dbus_daemon_dup (NULL);
183             priv->name = g_strdup_printf ("badger_%s_%p",
184                 tp_dbus_daemon_get_unique_name (bus), object);
185             g_strdelimit (priv->name, ":.", '_');
186             g_object_unref (bus);
187           }
188         break;
189       default:
190         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
191         break;
192     }
193 }
194
195 static void
196 handler_get_property (GObject *object,
197     guint property_id,
198     GValue *value,
199     GParamSpec *pspec)
200 {
201   EmpathyHandler *self = EMPATHY_HANDLER (object);
202   EmpathyHandlerPriv *priv = GET_PRIV (self);
203
204   switch (property_id)
205     {
206       case PROP_INTERFACES:
207         g_value_set_boxed (value, empathy_handler_interfaces);
208         break;
209       case PROP_CHANNEL_FILTER:
210         g_value_set_boxed (value, priv->filters);
211         break;
212       case PROP_CAPABILITIES:
213         g_value_set_boxed (value, priv->capabilities);
214         break;
215       case PROP_NAME:
216         g_value_set_string (value, priv->name);
217         break;
218       case PROP_CHANNELS:
219         {
220           GList *l, *channels = NULL;
221           GPtrArray *array = g_ptr_array_new ();
222
223           if (priv->channels != NULL)
224             channels =  priv->channels (self, priv->channels_user_data);
225
226           for (l = channels ; l != NULL; l = g_list_next (l))
227             {
228               TpProxy *channel = TP_PROXY (l->data);
229               g_ptr_array_add (array,
230                 (gpointer) tp_proxy_get_object_path (channel));
231             }
232           g_value_set_boxed (value, array);
233           g_ptr_array_free (array, TRUE);
234           break;
235         }
236       default:
237         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
238         break;
239     }
240 }
241
242 static void
243 empathy_handler_class_init (EmpathyHandlerClass *klass)
244 {
245   GObjectClass *object_class = G_OBJECT_CLASS (klass);
246   GParamSpec *param_spec;
247
248   static TpDBusPropertiesMixinPropImpl client_props[] = {
249     { "Interfaces", "interfaces", NULL },
250     { NULL }
251   };
252   static TpDBusPropertiesMixinPropImpl client_handler_props[] = {
253     { "HandlerChannelFilter", "channel-filter", NULL },
254     { "HandledChannels", "channels", NULL },
255     { "Capabilities", "capabilities", NULL },
256     { NULL }
257   };
258   static TpDBusPropertiesMixinIfaceImpl prop_interfaces[] = {
259     { TP_IFACE_CLIENT,
260       tp_dbus_properties_mixin_getter_gobject_properties,
261       NULL,
262       client_props
263     },
264     { TP_IFACE_CLIENT_HANDLER,
265       tp_dbus_properties_mixin_getter_gobject_properties,
266       NULL,
267       client_handler_props
268     },
269     { NULL }
270   };
271
272   object_class->finalize = handler_finalize;
273   object_class->dispose = handler_dispose;
274   object_class->constructor = handler_constructor;
275
276   object_class->get_property = handler_get_property;
277   object_class->set_property = handler_set_property;
278
279   param_spec = g_param_spec_boxed ("interfaces", "interfaces",
280     "Available D-Bus interfaces",
281     G_TYPE_STRV,
282     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
283   g_object_class_install_property (object_class, PROP_INTERFACES, param_spec);
284
285   param_spec = g_param_spec_boxed ("channel-filter", "channel-filter",
286     "Filter for channels this handles",
287     TP_ARRAY_TYPE_CHANNEL_CLASS_LIST,
288     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
289   g_object_class_install_property (object_class,
290     PROP_CHANNEL_FILTER, param_spec);
291
292   param_spec = g_param_spec_boxed ("capabilities", "capabilities",
293     "Filter for channels this handles",
294     G_TYPE_STRV,
295     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
296   g_object_class_install_property (object_class,
297     PROP_CAPABILITIES, param_spec);
298
299   param_spec = g_param_spec_boxed ("channels", "channels",
300     "List of channels we're handling",
301     EMPATHY_ARRAY_TYPE_OBJECT,
302     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
303   g_object_class_install_property (object_class,
304     PROP_CHANNELS, param_spec);
305
306   param_spec = g_param_spec_string ("name", "name",
307     "The local name of the handler",
308     NULL,
309     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
310   g_object_class_install_property (object_class,
311     PROP_NAME, param_spec);
312
313   g_type_class_add_private (object_class, sizeof (EmpathyHandlerPriv));
314
315   klass->dbus_props_class.interfaces = prop_interfaces;
316   tp_dbus_properties_mixin_class_init (object_class,
317     G_STRUCT_OFFSET (EmpathyHandlerClass, dbus_props_class));
318 }
319
320 static void
321 empathy_handler_init (EmpathyHandler *handler)
322 {
323   EmpathyHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (handler,
324     EMPATHY_TYPE_HANDLER, EmpathyHandlerPriv);
325
326   handler->priv = priv;
327 }
328
329 EmpathyHandler *
330 empathy_handler_new (const gchar *name,
331     GPtrArray *filters,
332     GStrv capabilities)
333 {
334   return EMPATHY_HANDLER (
335     g_object_new (EMPATHY_TYPE_HANDLER,
336       "name", name,
337       "channel-filter", filters,
338       "capabilities", capabilities,
339       NULL));
340 }
341
342 static void
343 empathy_handler_handle_channels (TpSvcClientHandler *self,
344     const gchar *account_path,
345     const gchar *connection_path,
346     const GPtrArray *channels,
347     const GPtrArray *requests_satisfied,
348     guint64 timestamp,
349     GHashTable *handler_info,
350     DBusGMethodInvocation *context)
351 {
352   EmpathyHandler *handler = EMPATHY_HANDLER (self);
353   EmpathyHandlerPriv *priv = GET_PRIV (handler);
354   GError *error = NULL;
355
356   if (!priv->handle_channels)
357     {
358       error = g_error_new_literal (TP_ERRORS,
359         TP_ERROR_NOT_AVAILABLE,
360         "No handler function setup");
361       goto error;
362     }
363
364   if (!priv->handle_channels (handler, account_path, connection_path,
365       channels, requests_satisfied, timestamp, handler_info,
366       priv->handle_channels_user_data, &error))
367     goto error;
368
369   tp_svc_client_handler_return_from_handle_channels (context);
370   return;
371
372 error:
373   dbus_g_method_return_error (context, error);
374   g_error_free (error);
375 }
376
377 static void
378 empathy_handler_client_handler_iface_init (gpointer g_iface,
379     gpointer g_iface_data)
380 {
381   TpSvcClientHandlerClass *klass = (TpSvcClientHandlerClass *) g_iface;
382
383   tp_svc_client_handler_implement_handle_channels (klass,
384     empathy_handler_handle_channels);
385 }
386
387 void
388 empathy_handler_set_handle_channels_func (EmpathyHandler *handler,
389     EmpathyHandlerHandleChannelsFunc *func,
390     gpointer user_data)
391 {
392   EmpathyHandlerPriv *priv = GET_PRIV (handler);
393
394   priv->handle_channels = func;
395   priv->handle_channels_user_data = user_data;
396 }
397
398 void
399 empathy_handler_set_channels_func (EmpathyHandler *handler,
400     EmpathyHandlerChannelsFunc *func,
401     gpointer user_data)
402 {
403   EmpathyHandlerPriv *priv = GET_PRIV (handler);
404
405   priv->channels = func;
406   priv->channels_user_data = user_data;
407 }
408