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;
54 static void empathy_handler_client_handler_iface_init (gpointer g_iface,
55 gpointer g_iface_data);
57 G_DEFINE_TYPE_WITH_CODE (EmpathyHandler,
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);
67 static const gchar *empathy_handler_interfaces[] = {
68 TP_IFACE_CLIENT_HANDLER,
82 handler_constructor (GType type,
83 guint n_construct_params,
84 GObjectConstructParam *construct_params)
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);
95 priv = GET_PRIV (handler);
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",
101 dbus = tp_dbus_daemon_dup (NULL);
103 g_assert (tp_dbus_daemon_request_name (dbus,
104 busname, TRUE, NULL));
105 dbus_g_connection_register_g_object (tp_get_bus (),
108 DEBUG ("Registered at '%s'", object_path);
111 g_free (object_path);
112 g_object_unref (dbus);
114 return G_OBJECT (handler);
118 handler_finalize (GObject *object)
120 EmpathyHandlerPriv *priv = GET_PRIV (object);
122 if (priv->filters != NULL)
123 g_boxed_free (TP_ARRAY_TYPE_CHANNEL_CLASS_LIST, priv->filters);
125 if (priv->capabilities != NULL)
126 g_boxed_free (G_TYPE_STRV, priv->capabilities);
132 handler_set_property (GObject *object,
137 EmpathyHandler *handler = EMPATHY_HANDLER (object);
138 EmpathyHandlerPriv *priv = GET_PRIV (handler);
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 ();
147 case PROP_CAPABILITIES:
148 priv->capabilities = g_value_dup_boxed (value);
151 priv->name = g_value_dup_string (value);
152 if (EMP_STR_EMPTY (priv->name))
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);
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
169 handler_get_property (GObject *object,
174 EmpathyHandler *self = EMPATHY_HANDLER (object);
175 EmpathyHandlerPriv *priv = GET_PRIV (self);
179 case PROP_INTERFACES:
180 g_value_set_boxed (value, empathy_handler_interfaces);
182 case PROP_CHANNEL_FILTER:
183 g_value_set_boxed (value, priv->filters);
185 case PROP_CAPABILITIES:
186 g_value_set_boxed (value, priv->capabilities);
189 g_value_set_string (value, priv->name);
193 GList *l, *channels = NULL;
194 GPtrArray *array = g_ptr_array_new ();
196 if (priv->channels != NULL)
197 channels = priv->channels (self, priv->channels_user_data);
199 for (l = channels ; l != NULL; l = g_list_next (l))
201 TpProxy *channel = TP_PROXY (l->data);
202 g_ptr_array_add (array,
203 (gpointer) tp_proxy_get_object_path (channel));
205 g_value_set_boxed (value, array);
206 g_ptr_array_free (array, TRUE);
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
216 empathy_handler_class_init (EmpathyHandlerClass *klass)
218 GObjectClass *object_class = G_OBJECT_CLASS (klass);
219 GParamSpec *param_spec;
221 static TpDBusPropertiesMixinPropImpl client_props[] = {
222 { "Interfaces", "interfaces", NULL },
225 static TpDBusPropertiesMixinPropImpl client_handler_props[] = {
226 { "HandlerChannelFilter", "channel-filter", NULL },
227 { "HandledChannels", "channels", NULL },
228 { "Capabilities", "capabilities", NULL },
231 static TpDBusPropertiesMixinIfaceImpl prop_interfaces[] = {
233 tp_dbus_properties_mixin_getter_gobject_properties,
237 { TP_IFACE_CLIENT_HANDLER,
238 tp_dbus_properties_mixin_getter_gobject_properties,
245 object_class->finalize = handler_finalize;
246 object_class->constructor = handler_constructor;
248 object_class->get_property = handler_get_property;
249 object_class->set_property = handler_set_property;
251 param_spec = g_param_spec_boxed ("interfaces", "interfaces",
252 "Available D-Bus interfaces",
254 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
255 g_object_class_install_property (object_class, PROP_INTERFACES, param_spec);
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);
264 param_spec = g_param_spec_boxed ("capabilities", "capabilities",
265 "Filter for channels this handles",
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);
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);
278 param_spec = g_param_spec_string ("name", "name",
279 "The local name of the handler",
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);
285 g_type_class_add_private (object_class, sizeof (EmpathyHandlerPriv));
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));
293 empathy_handler_init (EmpathyHandler *handler)
295 EmpathyHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (handler,
296 EMPATHY_TYPE_HANDLER, EmpathyHandlerPriv);
298 handler->priv = priv;
302 empathy_handler_new (const gchar *name,
306 return EMPATHY_HANDLER (
307 g_object_new (EMPATHY_TYPE_HANDLER,
309 "channel-filter", filters,
310 "capabilities", capabilities,
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,
321 GHashTable *handler_info,
322 DBusGMethodInvocation *context)
324 EmpathyHandler *handler = EMPATHY_HANDLER (self);
325 EmpathyHandlerPriv *priv = GET_PRIV (handler);
326 GError *error = NULL;
328 if (!priv->handle_channels)
330 error = g_error_new_literal (TP_ERRORS,
331 TP_ERROR_NOT_AVAILABLE,
332 "No handler function setup");
336 if (!priv->handle_channels (handler, account_path, connection_path,
337 channels, requests_satisfied, timestamp, handler_info,
338 priv->handle_channels_user_data, &error))
341 tp_svc_client_handler_return_from_handle_channels (context);
345 dbus_g_method_return_error (context, error);
346 g_error_free (error);
350 empathy_handler_client_handler_iface_init (gpointer g_iface,
351 gpointer g_iface_data)
353 TpSvcClientHandlerClass *klass = (TpSvcClientHandlerClass *) g_iface;
355 tp_svc_client_handler_implement_handle_channels (klass,
356 empathy_handler_handle_channels);
360 empathy_handler_set_handle_channels_func (EmpathyHandler *handler,
361 EmpathyHandlerHandleChannelsFunc *func,
364 EmpathyHandlerPriv *priv = GET_PRIV (handler);
366 priv->handle_channels = func;
367 priv->handle_channels_user_data = user_data;
371 empathy_handler_set_channels_func (EmpathyHandler *handler,
372 EmpathyHandlerChannelsFunc *func,
375 EmpathyHandlerPriv *priv = GET_PRIV (handler);
377 priv->channels = func;
378 priv->channels_user_data = user_data;