1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
24 #include <telepathy-glib/dbus.h>
25 #include <telepathy-glib/connection.h>
27 #include <extensions/extensions.h>
28 #include "empathy-filter.h"
29 #include "empathy-debug.h"
30 #include "empathy-utils.h"
32 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
33 EMPATHY_TYPE_FILTER, EmpathyFilterPriv))
35 #define DEBUG_DOMAIN "EmpathyFilter"
37 struct _EmpathyFilterPriv {
41 static void empathy_filter_class_init (EmpathyFilterClass *klass);
42 static void empathy_filter_init (EmpathyFilter *filter);
43 static void filter_iface_init (EmpSvcFilterClass *klass);
50 static guint signals[LAST_SIGNAL];
52 G_DEFINE_TYPE_WITH_CODE (EmpathyFilter, empathy_filter, G_TYPE_OBJECT,
53 G_IMPLEMENT_INTERFACE (EMP_TYPE_SVC_FILTER,
57 EmpathyFilter *filter;
68 filter_channel_idle_cb (gpointer data)
70 IdleData *idle_data = data;
71 EmpathyFilterPriv *priv = GET_PRIV (idle_data->filter);
74 static TpDBusDaemon *daemon = NULL;
77 daemon = tp_dbus_daemon_new (tp_get_bus ());
80 conn = tp_connection_new (daemon, idle_data->bus_name,
81 idle_data->connection, NULL);
82 tp_connection_run_until_ready (conn, FALSE, NULL, NULL);
83 chan = tp_channel_new (conn, idle_data->channel, idle_data->channel_type,
84 idle_data->handle_type, idle_data->handle, NULL);
85 tp_channel_run_until_ready (chan, NULL, NULL);
87 g_hash_table_insert (priv->table, chan, GUINT_TO_POINTER (idle_data->id));
89 empathy_debug (DEBUG_DOMAIN, "New channel to be filtred: "
90 "type=%s handle=%d id=%d",
91 idle_data->channel_type, idle_data->handle,
94 g_signal_emit (idle_data->filter, signals[NEW_CHANNEL], 0, chan);
96 g_object_unref (conn);
97 g_free (idle_data->bus_name);
98 g_free (idle_data->connection);
99 g_free (idle_data->channel_type);
100 g_free (idle_data->channel);
101 g_slice_free (IdleData, idle_data);
107 my_filter_channel (EmpSvcFilter *self,
108 const gchar *bus_name,
109 const gchar *connection,
110 const gchar *channel_type,
111 const gchar *channel,
115 DBusGMethodInvocation *context)
117 EmpathyFilter *filter = EMPATHY_FILTER (self);
120 data = g_slice_new (IdleData);
121 data->filter = filter;
122 data->bus_name = g_strdup (bus_name);
123 data->connection = g_strdup (connection);
124 data->channel_type = g_strdup (channel_type);
125 data->channel = g_strdup (channel);
126 data->handle_type = handle_type;
127 data->handle = handle;
129 g_idle_add_full (G_PRIORITY_HIGH,
130 filter_channel_idle_cb,
133 emp_svc_filter_return_from_filter_channel (context);
137 filter_finalize (GObject *object)
139 EmpathyFilterPriv *priv;
141 priv = GET_PRIV (object);
143 g_hash_table_destroy (priv->table);
147 empathy_filter_class_init (EmpathyFilterClass *klass)
149 GObjectClass *object_class = G_OBJECT_CLASS (klass);
151 object_class->finalize = filter_finalize;
153 signals[NEW_CHANNEL] =
154 g_signal_new ("new-channel",
155 G_OBJECT_CLASS_TYPE (klass),
159 g_cclosure_marshal_VOID__OBJECT,
163 g_type_class_add_private (object_class, sizeof (EmpathyFilterPriv));
167 filter_iface_init (EmpSvcFilterClass *klass)
169 #define IMPLEMENT(x) emp_svc_filter_implement_##x \
171 IMPLEMENT (filter_channel);
176 empathy_filter_init (EmpathyFilter *filter)
178 EmpathyFilterPriv *priv;
180 priv = GET_PRIV (filter);
182 priv->table = g_hash_table_new_full (g_direct_hash, g_direct_equal,
183 (GDestroyNotify) g_object_unref,
188 empathy_filter_new (const gchar *bus_name,
189 const gchar *object_path,
190 const gchar *channel_type,
195 EmpathyFilter *filter;
198 GError *error = NULL;
200 proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
203 DBUS_INTERFACE_DBUS);
205 if (!dbus_g_proxy_call (proxy, "RequestName", &error,
206 G_TYPE_STRING, bus_name,
207 G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
209 G_TYPE_UINT, &result,
211 empathy_debug (DEBUG_DOMAIN,
212 "Failed to request name: %s",
213 error ? error->message : "No error given");
214 g_clear_error (&error);
218 g_object_unref (proxy);
220 filter = g_object_new (EMPATHY_TYPE_FILTER, NULL);
221 dbus_g_connection_register_g_object (tp_get_bus (),
225 mc = empathy_mission_control_new ();
227 mission_control_register_filter (mc,
240 empathy_filter_process (EmpathyFilter *filter,
244 EmpathyFilterPriv *priv;
247 g_return_if_fail (EMPATHY_IS_FILTER (filter));
248 g_return_if_fail (TP_IS_CHANNEL (channel));
250 priv = GET_PRIV (filter);
252 id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->table, channel));
253 g_return_if_fail (id != 0);
255 empathy_debug (DEBUG_DOMAIN, "Processing channel id %d: %s",
256 id, process ? "Yes" : "No");
258 emp_svc_filter_emit_process (filter, id, process);
260 g_hash_table_remove (priv->table, channel);