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 "empathy-filter.h"
28 #include "empathy-debug.h"
29 #include "empathy-utils.h"
30 #include "empathy-marshal.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_finalize (GObject *object);
44 static gboolean empathy_filter_filter_channel (EmpathyFilter *filter,
45 const gchar *bus_name,
46 const gchar *connection,
47 const gchar *channel_type,
54 #include "empathy-filter-glue.h"
62 static guint signals[LAST_SIGNAL];
64 G_DEFINE_TYPE (EmpathyFilter, empathy_filter, G_TYPE_OBJECT)
67 empathy_filter_class_init (EmpathyFilterClass *klass)
69 GObjectClass *object_class = G_OBJECT_CLASS (klass);
71 object_class->finalize = filter_finalize;
73 signals[NEW_CHANNEL] =
74 g_signal_new ("new-channel",
75 G_OBJECT_CLASS_TYPE (klass),
79 g_cclosure_marshal_VOID__OBJECT,
84 g_signal_new ("process",
85 G_OBJECT_CLASS_TYPE (klass),
89 _empathy_marshal_VOID__UINT_BOOLEAN,
91 2, G_TYPE_UINT, G_TYPE_BOOLEAN);
93 g_type_class_add_private (object_class, sizeof (EmpathyFilterPriv));
97 empathy_filter_init (EmpathyFilter *filter)
99 EmpathyFilterPriv *priv;
101 priv = GET_PRIV (filter);
103 priv->table = g_hash_table_new_full (g_direct_hash, g_direct_equal,
104 (GDestroyNotify) g_object_unref,
109 filter_finalize (GObject *object)
111 EmpathyFilterPriv *priv;
113 priv = GET_PRIV (object);
115 g_hash_table_destroy (priv->table);
119 empathy_filter_new (const gchar *bus_name,
120 const gchar *object_path,
121 const gchar *channel_type,
125 static gboolean initialized = FALSE;
127 EmpathyFilter *filter;
130 GError *error = NULL;
133 dbus_g_object_type_install_info (EMPATHY_TYPE_FILTER,
134 &dbus_glib_empathy_filter_object_info);
138 proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
141 DBUS_INTERFACE_DBUS);
143 if (!dbus_g_proxy_call (proxy, "RequestName", &error,
144 G_TYPE_STRING, bus_name,
145 G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
147 G_TYPE_UINT, &result,
149 empathy_debug (DEBUG_DOMAIN,
150 "Failed to request name: %s",
151 error ? error->message : "No error given");
152 g_clear_error (&error);
156 g_object_unref (proxy);
158 filter = g_object_new (EMPATHY_TYPE_FILTER, NULL);
159 dbus_g_connection_register_g_object (tp_get_bus (),
163 mc = empathy_mission_control_new ();
164 mission_control_register_filter (mc,
177 empathy_filter_process (EmpathyFilter *filter,
181 EmpathyFilterPriv *priv;
184 g_return_if_fail (EMPATHY_IS_FILTER (filter));
185 g_return_if_fail (TP_IS_CHANNEL (channel));
187 priv = GET_PRIV (filter);
189 id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->table, channel));
190 g_return_if_fail (id != 0);
192 empathy_debug (DEBUG_DOMAIN, "Processing channel id %d: %s",
193 id, process ? "Yes" : "No");
194 g_signal_emit (filter, signals[PROCESS], 0, id, process);
195 g_hash_table_remove (priv->table, channel);
199 empathy_filter_filter_channel (EmpathyFilter *filter,
200 const gchar *bus_name,
201 const gchar *connection,
202 const gchar *channel_type,
203 const gchar *channel,
209 EmpathyFilterPriv *priv;
212 static TpDBusDaemon *daemon = NULL;
214 priv = GET_PRIV (filter);
217 daemon = tp_dbus_daemon_new (tp_get_bus ());
220 conn = tp_connection_new (daemon, bus_name, connection, NULL);
221 chan = tp_channel_new (conn, channel, channel_type, handle_type, handle, NULL);
222 tp_channel_run_until_ready (chan, NULL, NULL);
224 g_hash_table_insert (priv->table, chan, GUINT_TO_POINTER (id));
226 empathy_debug (DEBUG_DOMAIN, "New channel to be filtred: "
227 "type=%s handle=%d id=%d",
228 channel_type, handle, id);
230 g_signal_emit (filter, signals[NEW_CHANNEL], 0, chan);
232 g_object_unref (conn);