1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 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 <dbus/dbus-glib.h>
26 #include <libtelepathy/tp-helpers.h>
27 #include <libtelepathy/tp-conn.h>
29 #include "empathy-filter.h"
30 #include "empathy-debug.h"
31 #include "empathy-utils.h"
32 #include "empathy-marshal.h"
34 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
35 EMPATHY_TYPE_FILTER, EmpathyFilterPriv))
37 #define DEBUG_DOMAIN "EmpathyFilter"
39 struct _EmpathyFilterPriv {
43 static void empathy_filter_class_init (EmpathyFilterClass *klass);
44 static void empathy_filter_init (EmpathyFilter *filter);
45 static void filter_finalize (GObject *object);
46 static gboolean empathy_filter_filter_channel (EmpathyFilter *filter,
47 const gchar *bus_name,
48 const gchar *connection,
49 const gchar *channel_type,
56 #include "empathy-filter-glue.h"
64 static guint signals[LAST_SIGNAL];
66 G_DEFINE_TYPE (EmpathyFilter, empathy_filter, G_TYPE_OBJECT)
69 empathy_filter_class_init (EmpathyFilterClass *klass)
71 GObjectClass *object_class = G_OBJECT_CLASS (klass);
73 object_class->finalize = filter_finalize;
75 signals[NEW_CHANNEL] =
76 g_signal_new ("new-channel",
77 G_OBJECT_CLASS_TYPE (klass),
81 _empathy_marshal_VOID__OBJECT_OBJECT,
83 2, TELEPATHY_CONN_TYPE, TELEPATHY_CHAN_TYPE);
86 g_signal_new ("process",
87 G_OBJECT_CLASS_TYPE (klass),
91 _empathy_marshal_VOID__UINT_BOOLEAN,
93 2, G_TYPE_UINT, G_TYPE_BOOLEAN);
95 g_type_class_add_private (object_class, sizeof (EmpathyFilterPriv));
99 empathy_filter_init (EmpathyFilter *filter)
101 EmpathyFilterPriv *priv;
103 priv = GET_PRIV (filter);
105 priv->table = g_hash_table_new_full (g_direct_hash, g_direct_equal,
106 (GDestroyNotify) g_object_unref,
111 filter_finalize (GObject *object)
113 EmpathyFilterPriv *priv;
115 priv = GET_PRIV (object);
117 g_hash_table_destroy (priv->table);
121 empathy_filter_new (const gchar *bus_name,
122 const gchar *object_path,
123 const gchar *channel_type,
127 static gboolean initialized = FALSE;
129 EmpathyFilter *filter;
132 GError *error = NULL;
135 dbus_g_object_type_install_info (EMPATHY_TYPE_FILTER,
136 &dbus_glib_empathy_filter_object_info);
140 proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
143 DBUS_INTERFACE_DBUS);
145 if (!dbus_g_proxy_call (proxy, "RequestName", &error,
146 G_TYPE_STRING, bus_name,
147 G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
149 G_TYPE_UINT, &result,
151 empathy_debug (DEBUG_DOMAIN,
152 "Failed to request name: %s",
153 error ? error->message : "No error given");
154 g_clear_error (&error);
158 g_object_unref (proxy);
160 filter = g_object_new (EMPATHY_TYPE_FILTER, NULL);
161 dbus_g_connection_register_g_object (tp_get_bus (),
165 mc = empathy_mission_control_new ();
166 mission_control_register_filter (mc,
179 empathy_filter_process (EmpathyFilter *filter,
183 EmpathyFilterPriv *priv;
186 g_return_if_fail (EMPATHY_IS_FILTER (filter));
187 g_return_if_fail (TELEPATHY_IS_CHAN (tp_chan));
189 priv = GET_PRIV (filter);
191 id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->table, tp_chan));
192 g_return_if_fail (id != 0);
194 empathy_debug (DEBUG_DOMAIN, "Processing channel id %d: %s",
195 id, process ? "Yes" : "No");
196 g_signal_emit (filter, signals[PROCESS], 0, id, process);
197 g_hash_table_remove (priv->table, tp_chan);
201 empathy_filter_filter_channel (EmpathyFilter *filter,
202 const gchar *bus_name,
203 const gchar *connection,
204 const gchar *channel_type,
205 const gchar *channel,
211 EmpathyFilterPriv *priv;
215 priv = GET_PRIV (filter);
217 tp_conn = tp_conn_new_without_connect (tp_get_bus (),
227 tp_chan = tp_chan_new (tp_get_bus(),
234 g_hash_table_insert (priv->table, tp_chan, GUINT_TO_POINTER (id));
236 empathy_debug (DEBUG_DOMAIN, "New channel to be filtred: "
237 "type=%s handle=%d id=%d",
238 channel_type, handle, id);
239 g_signal_emit (filter, signals[NEW_CHANNEL], 0, tp_conn, tp_chan);
241 g_object_unref (tp_conn);