2 * Copyright (C) 2007-2009 Collabora Ltd.
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.
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.
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
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
19 * Sjoerd Simons <sjoerd.simons@collabora.co.uk>
20 * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
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>
33 #include "empathy-handler.h"
34 #include "empathy-utils.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
37 #include <libempathy/empathy-debug.h>
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyHandler)
42 EmpathyHandlerHandleChannelsFunc *handle_channels;
43 gpointer handle_channels_user_data;
45 EmpathyHandlerChannelsFunc *channels;
46 gpointer channels_user_data;
57 static void empathy_handler_client_handler_iface_init (gpointer g_iface,
58 gpointer g_iface_data);
60 G_DEFINE_TYPE_WITH_CODE (EmpathyHandler,
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);
70 static const gchar *empathy_handler_interfaces[] = {
71 TP_IFACE_CLIENT_HANDLER,
85 handler_constructor (GType type,
86 guint n_construct_params,
87 GObjectConstructParam *construct_params)
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);
97 priv = GET_PRIV (handler);
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",
103 dbus = tp_dbus_daemon_dup (NULL);
105 g_assert (tp_dbus_daemon_request_name (dbus,
106 priv->busname, TRUE, NULL));
107 dbus_g_connection_register_g_object (tp_get_bus (),
110 DEBUG ("Registered at '%s'", object_path);
112 g_free (object_path);
113 g_object_unref (dbus);
115 return G_OBJECT (handler);
119 handler_dispose (GObject *object)
121 EmpathyHandlerPriv *priv = GET_PRIV (object);
124 if (priv->dispose_run)
127 priv->dispose_run = TRUE;
129 dbus = tp_dbus_daemon_dup (NULL);
131 tp_dbus_daemon_release_name (dbus, priv->busname, NULL);
133 g_object_unref (dbus);
135 if (G_OBJECT_CLASS (empathy_handler_parent_class)->dispose != NULL)
136 G_OBJECT_CLASS (empathy_handler_parent_class)->dispose (object);
140 handler_finalize (GObject *object)
142 EmpathyHandlerPriv *priv = GET_PRIV (object);
144 if (priv->filters != NULL)
145 g_boxed_free (TP_ARRAY_TYPE_CHANNEL_CLASS_LIST, priv->filters);
147 if (priv->capabilities != NULL)
148 g_boxed_free (G_TYPE_STRV, priv->capabilities);
151 g_free (priv->busname);
153 if (G_OBJECT_CLASS (empathy_handler_parent_class)->finalize != NULL)
154 G_OBJECT_CLASS (empathy_handler_parent_class)->finalize (object);
158 handler_set_property (GObject *object,
163 EmpathyHandler *handler = EMPATHY_HANDLER (object);
164 EmpathyHandlerPriv *priv = GET_PRIV (handler);
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 ();
173 case PROP_CAPABILITIES:
174 priv->capabilities = g_value_dup_boxed (value);
177 priv->name = g_value_dup_string (value);
178 if (EMP_STR_EMPTY (priv->name))
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);
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
196 handler_get_property (GObject *object,
201 EmpathyHandler *self = EMPATHY_HANDLER (object);
202 EmpathyHandlerPriv *priv = GET_PRIV (self);
206 case PROP_INTERFACES:
207 g_value_set_boxed (value, empathy_handler_interfaces);
209 case PROP_CHANNEL_FILTER:
210 g_value_set_boxed (value, priv->filters);
212 case PROP_CAPABILITIES:
213 g_value_set_boxed (value, priv->capabilities);
216 g_value_set_string (value, priv->name);
220 GList *l, *channels = NULL;
221 GPtrArray *array = g_ptr_array_new ();
223 if (priv->channels != NULL)
224 channels = priv->channels (self, priv->channels_user_data);
226 for (l = channels ; l != NULL; l = g_list_next (l))
228 TpProxy *channel = TP_PROXY (l->data);
229 g_ptr_array_add (array,
230 (gpointer) tp_proxy_get_object_path (channel));
232 g_value_set_boxed (value, array);
233 g_ptr_array_free (array, TRUE);
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
243 empathy_handler_class_init (EmpathyHandlerClass *klass)
245 GObjectClass *object_class = G_OBJECT_CLASS (klass);
246 GParamSpec *param_spec;
248 static TpDBusPropertiesMixinPropImpl client_props[] = {
249 { "Interfaces", "interfaces", NULL },
252 static TpDBusPropertiesMixinPropImpl client_handler_props[] = {
253 { "HandlerChannelFilter", "channel-filter", NULL },
254 { "HandledChannels", "channels", NULL },
255 { "Capabilities", "capabilities", NULL },
258 static TpDBusPropertiesMixinIfaceImpl prop_interfaces[] = {
260 tp_dbus_properties_mixin_getter_gobject_properties,
264 { TP_IFACE_CLIENT_HANDLER,
265 tp_dbus_properties_mixin_getter_gobject_properties,
272 object_class->finalize = handler_finalize;
273 object_class->dispose = handler_dispose;
274 object_class->constructor = handler_constructor;
276 object_class->get_property = handler_get_property;
277 object_class->set_property = handler_set_property;
279 param_spec = g_param_spec_boxed ("interfaces", "interfaces",
280 "Available D-Bus interfaces",
282 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
283 g_object_class_install_property (object_class, PROP_INTERFACES, param_spec);
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);
292 param_spec = g_param_spec_boxed ("capabilities", "capabilities",
293 "Filter for channels this handles",
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);
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);
306 param_spec = g_param_spec_string ("name", "name",
307 "The local name of the handler",
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);
313 g_type_class_add_private (object_class, sizeof (EmpathyHandlerPriv));
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));
321 empathy_handler_init (EmpathyHandler *handler)
323 EmpathyHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (handler,
324 EMPATHY_TYPE_HANDLER, EmpathyHandlerPriv);
326 handler->priv = priv;
330 empathy_handler_new (const gchar *name,
334 return EMPATHY_HANDLER (
335 g_object_new (EMPATHY_TYPE_HANDLER,
337 "channel-filter", filters,
338 "capabilities", capabilities,
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,
349 GHashTable *handler_info,
350 DBusGMethodInvocation *context)
352 EmpathyHandler *handler = EMPATHY_HANDLER (self);
353 EmpathyHandlerPriv *priv = GET_PRIV (handler);
354 GError *error = NULL;
356 if (!priv->handle_channels)
358 error = g_error_new_literal (TP_ERRORS,
359 TP_ERROR_NOT_AVAILABLE,
360 "No handler function setup");
364 if (!priv->handle_channels (handler, account_path, connection_path,
365 channels, requests_satisfied, timestamp, handler_info,
366 priv->handle_channels_user_data, &error))
369 tp_svc_client_handler_return_from_handle_channels (context);
373 dbus_g_method_return_error (context, error);
374 g_error_free (error);
378 empathy_handler_client_handler_iface_init (gpointer g_iface,
379 gpointer g_iface_data)
381 TpSvcClientHandlerClass *klass = (TpSvcClientHandlerClass *) g_iface;
383 tp_svc_client_handler_implement_handle_channels (klass,
384 empathy_handler_handle_channels);
388 empathy_handler_set_handle_channels_func (EmpathyHandler *handler,
389 EmpathyHandlerHandleChannelsFunc *func,
392 EmpathyHandlerPriv *priv = GET_PRIV (handler);
394 priv->handle_channels = func;
395 priv->handle_channels_user_data = user_data;
399 empathy_handler_set_channels_func (EmpathyHandler *handler,
400 EmpathyHandlerChannelsFunc *func,
403 EmpathyHandlerPriv *priv = GET_PRIV (handler);
405 priv->channels = func;
406 priv->channels_user_data = user_data;