]> git.0d.be Git - empathy.git/blob - libempathy/empathy-handler.c
Don't set empty values for the default EmpathyHandler
[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         if (priv->filters == NULL)
145           priv->filters = g_ptr_array_new ();
146         break;
147       case PROP_CAPABILITIES:
148         priv->capabilities = g_value_dup_boxed (value);
149         break;
150       case PROP_NAME:
151         priv->name = g_value_dup_string (value);
152         if (EMP_STR_EMPTY (priv->name))
153           {
154             TpDBusDaemon *bus;
155
156             bus = tp_dbus_daemon_dup (NULL);
157             priv->name = g_strdup_printf ("%s%p",
158                 tp_dbus_daemon_get_unique_name (bus), object);
159             g_object_unref (bus);
160           }
161         break;
162       default:
163         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
164         break;
165     }
166 }
167
168 static void
169 handler_get_property (GObject *object,
170     guint property_id,
171     GValue *value,
172     GParamSpec *pspec)
173 {
174   EmpathyHandler *self = EMPATHY_HANDLER (object);
175   EmpathyHandlerPriv *priv = GET_PRIV (self);
176
177   switch (property_id)
178     {
179       case PROP_INTERFACES:
180         g_value_set_boxed (value, empathy_handler_interfaces);
181         break;
182       case PROP_CHANNEL_FILTER:
183         g_value_set_boxed (value, priv->filters);
184         break;
185       case PROP_CAPABILITIES:
186         g_value_set_boxed (value, priv->capabilities);
187         break;
188       case PROP_NAME:
189         g_value_set_string (value, priv->name);
190         break;
191       case PROP_CHANNELS:
192         {
193           GList *l, *channels = NULL;
194           GPtrArray *array = g_ptr_array_new ();
195
196           if (priv->channels != NULL)
197             channels =  priv->channels (self, priv->channels_user_data);
198
199           for (l = channels ; l != NULL; l = g_list_next (l))
200             {
201               TpProxy *channel = TP_PROXY (l->data);
202               g_ptr_array_add (array,
203                 (gpointer) tp_proxy_get_object_path (channel));
204             }
205           g_value_set_boxed (value, array);
206           g_ptr_array_free (array, TRUE);
207           break;
208         }
209       default:
210         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
211         break;
212     }
213 }
214
215 static void
216 empathy_handler_class_init (EmpathyHandlerClass *klass)
217 {
218   GObjectClass *object_class = G_OBJECT_CLASS (klass);
219   GParamSpec *param_spec;
220
221   static TpDBusPropertiesMixinPropImpl client_props[] = {
222     { "Interfaces", "interfaces", NULL },
223     { NULL }
224   };
225   static TpDBusPropertiesMixinPropImpl client_handler_props[] = {
226     { "HandlerChannelFilter", "channel-filter", NULL },
227     { "HandledChannels", "channels", NULL },
228     { "Capabilities", "capabilities", NULL },
229     { NULL }
230   };
231   static TpDBusPropertiesMixinIfaceImpl prop_interfaces[] = {
232     { TP_IFACE_CLIENT,
233       tp_dbus_properties_mixin_getter_gobject_properties,
234       NULL,
235       client_props
236     },
237     { TP_IFACE_CLIENT_HANDLER,
238       tp_dbus_properties_mixin_getter_gobject_properties,
239       NULL,
240       client_handler_props
241     },
242     { NULL }
243   };
244
245   object_class->finalize = handler_finalize;
246   object_class->constructor = handler_constructor;
247
248   object_class->get_property = handler_get_property;
249   object_class->set_property = handler_set_property;
250
251   param_spec = g_param_spec_boxed ("interfaces", "interfaces",
252     "Available D-Bus interfaces",
253     G_TYPE_STRV,
254     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
255   g_object_class_install_property (object_class, PROP_INTERFACES, param_spec);
256
257   param_spec = g_param_spec_boxed ("channel-filter", "channel-filter",
258     "Filter for channels this handles",
259     TP_ARRAY_TYPE_CHANNEL_CLASS_LIST,
260     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
261   g_object_class_install_property (object_class,
262     PROP_CHANNEL_FILTER, param_spec);
263
264   param_spec = g_param_spec_boxed ("capabilities", "capabilities",
265     "Filter for channels this handles",
266     G_TYPE_STRV,
267     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
268   g_object_class_install_property (object_class,
269     PROP_CAPABILITIES, param_spec);
270
271   param_spec = g_param_spec_boxed ("channels", "channels",
272     "List of channels we're handling",
273     EMPATHY_ARRAY_TYPE_OBJECT,
274     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
275   g_object_class_install_property (object_class,
276     PROP_CHANNELS, param_spec);
277
278   param_spec = g_param_spec_string ("name", "name",
279     "The local name of the handler",
280     NULL,
281     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
282   g_object_class_install_property (object_class,
283     PROP_NAME, param_spec);
284
285   g_type_class_add_private (object_class, sizeof (EmpathyHandlerPriv));
286
287   klass->dbus_props_class.interfaces = prop_interfaces;
288   tp_dbus_properties_mixin_class_init (object_class,
289     G_STRUCT_OFFSET (EmpathyHandlerClass, dbus_props_class));
290 }
291
292 static void
293 empathy_handler_init (EmpathyHandler *handler)
294 {
295   EmpathyHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (handler,
296     EMPATHY_TYPE_HANDLER, EmpathyHandlerPriv);
297
298   handler->priv = priv;
299 }
300
301 EmpathyHandler *
302 empathy_handler_new (const gchar *name,
303     GPtrArray *filters,
304     GStrv capabilities)
305 {
306   return EMPATHY_HANDLER (
307     g_object_new (EMPATHY_TYPE_HANDLER,
308       "name", name,
309       "channel-filter", filters,
310       "capabilities", capabilities,
311       NULL));
312 }
313
314 static void
315 empathy_handler_handle_channels (TpSvcClientHandler *self,
316     const gchar *account_path,
317     const gchar *connection_path,
318     const GPtrArray *channels,
319     const GPtrArray *requests_satisfied,
320     guint64 timestamp,
321     GHashTable *handler_info,
322     DBusGMethodInvocation *context)
323 {
324   EmpathyHandler *handler = EMPATHY_HANDLER (self);
325   EmpathyHandlerPriv *priv = GET_PRIV (handler);
326   GError *error = NULL;
327
328   if (!priv->handle_channels)
329     {
330       error = g_error_new_literal (TP_ERRORS,
331         TP_ERROR_NOT_AVAILABLE,
332         "No handler function setup");
333       goto error;
334     }
335
336   if (!priv->handle_channels (handler, account_path, connection_path,
337       channels, requests_satisfied, timestamp, handler_info,
338       priv->handle_channels_user_data, &error))
339     goto error;
340
341   tp_svc_client_handler_return_from_handle_channels (context);
342   return;
343
344 error:
345   dbus_g_method_return_error (context, error);
346   g_error_free (error);
347 }
348
349 static void
350 empathy_handler_client_handler_iface_init (gpointer g_iface,
351     gpointer g_iface_data)
352 {
353   TpSvcClientHandlerClass *klass = (TpSvcClientHandlerClass *) g_iface;
354
355   tp_svc_client_handler_implement_handle_channels (klass,
356     empathy_handler_handle_channels);
357 }
358
359 void
360 empathy_handler_set_handle_channels_func (EmpathyHandler *handler,
361     EmpathyHandlerHandleChannelsFunc *func,
362     gpointer user_data)
363 {
364   EmpathyHandlerPriv *priv = GET_PRIV (handler);
365
366   priv->handle_channels = func;
367   priv->handle_channels_user_data = user_data;
368 }
369
370 void
371 empathy_handler_set_channels_func (EmpathyHandler *handler,
372     EmpathyHandlerChannelsFunc *func,
373     gpointer user_data)
374 {
375   EmpathyHandlerPriv *priv = GET_PRIV (handler);
376
377   priv->channels = func;
378   priv->channels_user_data = user_data;
379 }
380