]> git.0d.be Git - empathy.git/blob - libempathy/empathy-filter.c
Fix disconnection of AccountStatusChanged signal.
[empathy.git] / libempathy / empathy-filter.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
18  * 
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <telepathy-glib/dbus.h>
25 #include <telepathy-glib/connection.h>
26
27 #include <extensions/extensions.h>
28 #include "empathy-filter.h"
29 #include "empathy-debug.h"
30 #include "empathy-utils.h"
31
32 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
33                        EMPATHY_TYPE_FILTER, EmpathyFilterPriv))
34
35 #define DEBUG_DOMAIN "EmpathyFilter"
36
37 struct _EmpathyFilterPriv {
38         GHashTable *table;
39 };
40
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);
44
45 enum {
46         NEW_CHANNEL,
47         LAST_SIGNAL
48 };
49
50 static guint signals[LAST_SIGNAL];
51
52 G_DEFINE_TYPE_WITH_CODE (EmpathyFilter, empathy_filter, G_TYPE_OBJECT,
53                          G_IMPLEMENT_INTERFACE (EMP_TYPE_SVC_FILTER,
54                                                 filter_iface_init));
55
56 typedef struct {
57         EmpathyFilter *filter;
58         gchar         *bus_name;
59         gchar         *connection;
60         gchar         *channel_type;
61         gchar         *channel;
62         guint          handle_type;
63         guint          handle;
64         guint          id;
65 } IdleData;
66
67 static gboolean
68 filter_channel_idle_cb (gpointer data)
69 {
70         IdleData            *idle_data = data;
71         EmpathyFilterPriv   *priv = GET_PRIV (idle_data->filter);
72         TpChannel           *chan;
73         TpConnection        *conn;
74         static TpDBusDaemon *daemon = NULL;
75
76         if (!daemon) {
77                 daemon = tp_dbus_daemon_new (tp_get_bus ());
78         }
79
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);
86
87         g_hash_table_insert (priv->table, chan, GUINT_TO_POINTER (idle_data->id));
88
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,
92                                      idle_data->id);
93
94         g_signal_emit (idle_data->filter, signals[NEW_CHANNEL], 0, chan);
95
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);
102
103         return FALSE;
104 }
105
106 static void
107 my_filter_channel (EmpSvcFilter          *self,
108                    const gchar           *bus_name,
109                    const gchar           *connection,
110                    const gchar           *channel_type,
111                    const gchar           *channel,
112                    guint                  handle_type,
113                    guint                  handle,
114                    guint                  id,
115                    DBusGMethodInvocation *context)
116 {
117         EmpathyFilter *filter = EMPATHY_FILTER (self);
118         IdleData      *data;
119
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;
128         data->id = id;
129         g_idle_add_full (G_PRIORITY_HIGH,
130                          filter_channel_idle_cb,
131                          data, NULL);
132
133         emp_svc_filter_return_from_filter_channel (context);
134 }
135
136 static void
137 filter_finalize (GObject *object)
138 {
139         EmpathyFilterPriv *priv;
140
141         priv = GET_PRIV (object);
142
143         g_hash_table_destroy (priv->table);
144 }
145
146 static void
147 empathy_filter_class_init (EmpathyFilterClass *klass)
148 {
149         GObjectClass *object_class = G_OBJECT_CLASS (klass);
150
151         object_class->finalize = filter_finalize;
152
153         signals[NEW_CHANNEL] =
154                 g_signal_new ("new-channel",
155                               G_OBJECT_CLASS_TYPE (klass),
156                               G_SIGNAL_RUN_LAST,
157                               0,
158                               NULL, NULL,
159                               g_cclosure_marshal_VOID__OBJECT,
160                               G_TYPE_NONE,
161                               1, TP_TYPE_CHANNEL);
162
163         g_type_class_add_private (object_class, sizeof (EmpathyFilterPriv));
164 }
165
166 static void
167 filter_iface_init (EmpSvcFilterClass *klass)
168 {
169 #define IMPLEMENT(x) emp_svc_filter_implement_##x \
170     (klass, my_##x)
171   IMPLEMENT (filter_channel);
172 #undef IMPLEMENT
173 }
174
175 static void
176 empathy_filter_init (EmpathyFilter *filter)
177 {
178         EmpathyFilterPriv *priv;
179
180         priv = GET_PRIV (filter);
181
182         priv->table = g_hash_table_new_full (g_direct_hash, g_direct_equal,
183                                              (GDestroyNotify) g_object_unref,
184                                              NULL);
185 }
186
187 EmpathyFilter *
188 empathy_filter_new (const gchar *bus_name,
189                     const gchar *object_path,
190                     const gchar *channel_type,
191                     guint        priority,
192                     guint        flags)
193 {
194         MissionControl  *mc;
195         EmpathyFilter   *filter;
196         DBusGProxy      *proxy;
197         guint            result;
198         GError          *error = NULL;
199
200         proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
201                                            DBUS_SERVICE_DBUS,
202                                            DBUS_PATH_DBUS,
203                                            DBUS_INTERFACE_DBUS);
204
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,
208                                 G_TYPE_INVALID,
209                                 G_TYPE_UINT, &result,
210                                 G_TYPE_INVALID)) {
211                 empathy_debug (DEBUG_DOMAIN,
212                                "Failed to request name: %s",
213                                error ? error->message : "No error given");
214                 g_clear_error (&error);
215
216                 return NULL;
217         }
218         g_object_unref (proxy);
219
220         filter = g_object_new (EMPATHY_TYPE_FILTER, NULL);
221         dbus_g_connection_register_g_object (tp_get_bus (),
222                                              object_path,
223                                              G_OBJECT (filter));
224
225         mc = empathy_mission_control_new ();
226
227         mission_control_register_filter (mc,
228                                          bus_name,
229                                          object_path,
230                                          channel_type,
231                                          priority,
232                                          flags,
233                                          NULL);
234         g_object_unref (mc);
235
236         return filter;
237 }
238
239 void
240 empathy_filter_process (EmpathyFilter *filter,
241                         TpChannel     *channel,
242                         gboolean       process)
243 {
244         EmpathyFilterPriv *priv;
245         guint              id;
246
247         g_return_if_fail (EMPATHY_IS_FILTER (filter));
248         g_return_if_fail (TP_IS_CHANNEL (channel));
249
250         priv = GET_PRIV (filter);
251
252         id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->table, channel));
253         g_return_if_fail (id != 0);
254
255         empathy_debug (DEBUG_DOMAIN, "Processing channel id %d: %s",
256                        id, process ? "Yes" : "No");
257
258         emp_svc_filter_emit_process (filter, id, process);
259
260         g_hash_table_remove (priv->table, channel);
261 }
262