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 ("%s%p",
184 tp_dbus_daemon_get_unique_name (bus), object);
185 g_object_unref (bus);
189 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
195 handler_get_property (GObject *object,
200 EmpathyHandler *self = EMPATHY_HANDLER (object);
201 EmpathyHandlerPriv *priv = GET_PRIV (self);
205 case PROP_INTERFACES:
206 g_value_set_boxed (value, empathy_handler_interfaces);
208 case PROP_CHANNEL_FILTER:
209 g_value_set_boxed (value, priv->filters);
211 case PROP_CAPABILITIES:
212 g_value_set_boxed (value, priv->capabilities);
215 g_value_set_string (value, priv->name);
219 GList *l, *channels = NULL;
220 GPtrArray *array = g_ptr_array_new ();
222 if (priv->channels != NULL)
223 channels = priv->channels (self, priv->channels_user_data);
225 for (l = channels ; l != NULL; l = g_list_next (l))
227 TpProxy *channel = TP_PROXY (l->data);
228 g_ptr_array_add (array,
229 (gpointer) tp_proxy_get_object_path (channel));
231 g_value_set_boxed (value, array);
232 g_ptr_array_free (array, TRUE);
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
242 empathy_handler_class_init (EmpathyHandlerClass *klass)
244 GObjectClass *object_class = G_OBJECT_CLASS (klass);
245 GParamSpec *param_spec;
247 static TpDBusPropertiesMixinPropImpl client_props[] = {
248 { "Interfaces", "interfaces", NULL },
251 static TpDBusPropertiesMixinPropImpl client_handler_props[] = {
252 { "HandlerChannelFilter", "channel-filter", NULL },
253 { "HandledChannels", "channels", NULL },
254 { "Capabilities", "capabilities", NULL },
257 static TpDBusPropertiesMixinIfaceImpl prop_interfaces[] = {
259 tp_dbus_properties_mixin_getter_gobject_properties,
263 { TP_IFACE_CLIENT_HANDLER,
264 tp_dbus_properties_mixin_getter_gobject_properties,
271 object_class->finalize = handler_finalize;
272 object_class->dispose = handler_dispose;
273 object_class->constructor = handler_constructor;
275 object_class->get_property = handler_get_property;
276 object_class->set_property = handler_set_property;
278 param_spec = g_param_spec_boxed ("interfaces", "interfaces",
279 "Available D-Bus interfaces",
281 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
282 g_object_class_install_property (object_class, PROP_INTERFACES, param_spec);
284 param_spec = g_param_spec_boxed ("channel-filter", "channel-filter",
285 "Filter for channels this handles",
286 TP_ARRAY_TYPE_CHANNEL_CLASS_LIST,
287 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
288 g_object_class_install_property (object_class,
289 PROP_CHANNEL_FILTER, param_spec);
291 param_spec = g_param_spec_boxed ("capabilities", "capabilities",
292 "Filter for channels this handles",
294 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
295 g_object_class_install_property (object_class,
296 PROP_CAPABILITIES, param_spec);
298 param_spec = g_param_spec_boxed ("channels", "channels",
299 "List of channels we're handling",
300 EMPATHY_ARRAY_TYPE_OBJECT,
301 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
302 g_object_class_install_property (object_class,
303 PROP_CHANNELS, param_spec);
305 param_spec = g_param_spec_string ("name", "name",
306 "The local name of the handler",
308 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
309 g_object_class_install_property (object_class,
310 PROP_NAME, param_spec);
312 g_type_class_add_private (object_class, sizeof (EmpathyHandlerPriv));
314 klass->dbus_props_class.interfaces = prop_interfaces;
315 tp_dbus_properties_mixin_class_init (object_class,
316 G_STRUCT_OFFSET (EmpathyHandlerClass, dbus_props_class));
320 empathy_handler_init (EmpathyHandler *handler)
322 EmpathyHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (handler,
323 EMPATHY_TYPE_HANDLER, EmpathyHandlerPriv);
325 handler->priv = priv;
329 empathy_handler_new (const gchar *name,
333 return EMPATHY_HANDLER (
334 g_object_new (EMPATHY_TYPE_HANDLER,
336 "channel-filter", filters,
337 "capabilities", capabilities,
342 empathy_handler_handle_channels (TpSvcClientHandler *self,
343 const gchar *account_path,
344 const gchar *connection_path,
345 const GPtrArray *channels,
346 const GPtrArray *requests_satisfied,
348 GHashTable *handler_info,
349 DBusGMethodInvocation *context)
351 EmpathyHandler *handler = EMPATHY_HANDLER (self);
352 EmpathyHandlerPriv *priv = GET_PRIV (handler);
353 GError *error = NULL;
355 if (!priv->handle_channels)
357 error = g_error_new_literal (TP_ERRORS,
358 TP_ERROR_NOT_AVAILABLE,
359 "No handler function setup");
363 if (!priv->handle_channels (handler, account_path, connection_path,
364 channels, requests_satisfied, timestamp, handler_info,
365 priv->handle_channels_user_data, &error))
368 tp_svc_client_handler_return_from_handle_channels (context);
372 dbus_g_method_return_error (context, error);
373 g_error_free (error);
377 empathy_handler_client_handler_iface_init (gpointer g_iface,
378 gpointer g_iface_data)
380 TpSvcClientHandlerClass *klass = (TpSvcClientHandlerClass *) g_iface;
382 tp_svc_client_handler_implement_handle_channels (klass,
383 empathy_handler_handle_channels);
387 empathy_handler_set_handle_channels_func (EmpathyHandler *handler,
388 EmpathyHandlerHandleChannelsFunc *func,
391 EmpathyHandlerPriv *priv = GET_PRIV (handler);
393 priv->handle_channels = func;
394 priv->handle_channels_user_data = user_data;
398 empathy_handler_set_channels_func (EmpathyHandler *handler,
399 EmpathyHandlerChannelsFunc *func,
402 EmpathyHandlerPriv *priv = GET_PRIV (handler);
404 priv->channels = func;
405 priv->channels_user_data = user_data;