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);
145 case PROP_CAPABILITIES:
146 priv->capabilities = g_value_dup_boxed (value);
149 priv->name = g_value_dup_string (value);
152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
158 handler_get_property (GObject *object,
163 EmpathyHandler *self = EMPATHY_HANDLER (object);
164 EmpathyHandlerPriv *priv = GET_PRIV (self);
168 case PROP_INTERFACES:
169 g_value_set_boxed (value, empathy_handler_interfaces);
171 case PROP_CHANNEL_FILTER:
172 g_value_set_boxed (value, priv->filters);
174 case PROP_CAPABILITIES:
175 g_value_set_boxed (value, priv->capabilities);
178 g_value_set_string (value, priv->name);
182 GList *l, *channels = NULL;
183 GPtrArray *array = g_ptr_array_new ();
185 if (priv->channels != NULL)
186 channels = priv->channels (self, priv->channels_user_data);
188 for (l = channels ; l != NULL; l = g_list_next (l))
190 TpProxy *channel = TP_PROXY (l->data);
191 g_ptr_array_add (array,
192 (gpointer) tp_proxy_get_object_path (channel));
194 g_value_set_boxed (value, array);
195 g_ptr_array_free (array, TRUE);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
205 empathy_handler_class_init (EmpathyHandlerClass *klass)
207 GObjectClass *object_class = G_OBJECT_CLASS (klass);
208 GParamSpec *param_spec;
210 static TpDBusPropertiesMixinPropImpl client_props[] = {
211 { "Interfaces", "interfaces", NULL },
214 static TpDBusPropertiesMixinPropImpl client_handler_props[] = {
215 { "HandlerChannelFilter", "channel-filter", NULL },
216 { "HandledChannels", "channels", NULL },
217 { "Capabilities", "capabilities", NULL },
220 static TpDBusPropertiesMixinIfaceImpl prop_interfaces[] = {
222 tp_dbus_properties_mixin_getter_gobject_properties,
226 { TP_IFACE_CLIENT_HANDLER,
227 tp_dbus_properties_mixin_getter_gobject_properties,
234 object_class->finalize = handler_finalize;
235 object_class->constructor = handler_constructor;
237 object_class->get_property = handler_get_property;
238 object_class->set_property = handler_set_property;
240 param_spec = g_param_spec_boxed ("interfaces", "interfaces",
241 "Available D-Bus interfaces",
243 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
244 g_object_class_install_property (object_class, PROP_INTERFACES, param_spec);
246 param_spec = g_param_spec_boxed ("channel-filter", "channel-filter",
247 "Filter for channels this handles",
248 TP_ARRAY_TYPE_CHANNEL_CLASS_LIST,
249 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
250 g_object_class_install_property (object_class,
251 PROP_CHANNEL_FILTER, param_spec);
253 param_spec = g_param_spec_boxed ("capabilities", "capabilities",
254 "Filter for channels this handles",
256 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
257 g_object_class_install_property (object_class,
258 PROP_CAPABILITIES, param_spec);
260 param_spec = g_param_spec_boxed ("channels", "channels",
261 "List of channels we're handling",
262 EMPATHY_ARRAY_TYPE_OBJECT,
263 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
264 g_object_class_install_property (object_class,
265 PROP_CHANNELS, param_spec);
267 param_spec = g_param_spec_string ("name", "name",
268 "The local name of the handler",
270 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
271 g_object_class_install_property (object_class,
272 PROP_NAME, param_spec);
274 g_type_class_add_private (object_class, sizeof (EmpathyHandlerPriv));
276 klass->dbus_props_class.interfaces = prop_interfaces;
277 tp_dbus_properties_mixin_class_init (object_class,
278 G_STRUCT_OFFSET (EmpathyHandlerClass, dbus_props_class));
282 empathy_handler_init (EmpathyHandler *handler)
284 EmpathyHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (handler,
285 EMPATHY_TYPE_HANDLER, EmpathyHandlerPriv);
287 handler->priv = priv;
291 empathy_handler_new (const gchar *name,
295 return EMPATHY_HANDLER (
296 g_object_new (EMPATHY_TYPE_HANDLER,
298 "channel-filter", filters,
299 "capabilities", capabilities,
304 empathy_handler_handle_channels (TpSvcClientHandler *self,
305 const gchar *account_path,
306 const gchar *connection_path,
307 const GPtrArray *channels,
308 const GPtrArray *requests_satisfied,
310 GHashTable *handler_info,
311 DBusGMethodInvocation *context)
313 EmpathyHandler *handler = EMPATHY_HANDLER (self);
314 EmpathyHandlerPriv *priv = GET_PRIV (handler);
315 GError *error = NULL;
317 if (!priv->handle_channels)
319 error = g_error_new_literal (TP_ERRORS,
320 TP_ERROR_NOT_AVAILABLE,
321 "No handler function setup");
325 if (!priv->handle_channels (handler, account_path, connection_path,
326 channels, requests_satisfied, timestamp, handler_info,
327 priv->handle_channels_user_data, &error))
330 tp_svc_client_handler_return_from_handle_channels (context);
334 dbus_g_method_return_error (context, error);
335 g_error_free (error);
339 empathy_handler_client_handler_iface_init (gpointer g_iface,
340 gpointer g_iface_data)
342 TpSvcClientHandlerClass *klass = (TpSvcClientHandlerClass *) g_iface;
344 tp_svc_client_handler_implement_handle_channels (klass,
345 empathy_handler_handle_channels);
349 empathy_handler_set_handle_channels_func (EmpathyHandler *handler,
350 EmpathyHandlerHandleChannelsFunc *func,
353 EmpathyHandlerPriv *priv = GET_PRIV (handler);
355 priv->handle_channels = func;
356 priv->handle_channels_user_data = user_data;
360 empathy_handler_set_channels_func (EmpathyHandler *handler,
361 EmpathyHandlerChannelsFunc *func,
364 EmpathyHandlerPriv *priv = GET_PRIV (handler);
366 priv->channels = func;
367 priv->channels_user_data = user_data;