]> git.0d.be Git - empathy.git/blob - libempathy/empathy-filter.c
Do not load avatar from cache if token is empty. Fixes bug #517098.
[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 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 <dbus/dbus-glib.h>
25
26 #include <libtelepathy/tp-helpers.h>
27 #include <libtelepathy/tp-conn.h>
28
29 #include "empathy-filter.h"
30 #include "empathy-debug.h"
31 #include "empathy-utils.h"
32 #include "empathy-marshal.h"
33
34 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
35                        EMPATHY_TYPE_FILTER, EmpathyFilterPriv))
36
37 #define DEBUG_DOMAIN "EmpathyFilter"
38
39 struct _EmpathyFilterPriv {
40         GHashTable *table;
41 };
42
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,
50                                                const gchar        *channel,
51                                                guint               handle_type,
52                                                guint               handle,
53                                                guint               id,
54                                                GError            **error);
55
56 #include "empathy-filter-glue.h"
57
58 enum {
59         PROCESS,
60         NEW_CHANNEL,
61         LAST_SIGNAL
62 };
63
64 static guint signals[LAST_SIGNAL];
65
66 G_DEFINE_TYPE (EmpathyFilter, empathy_filter, G_TYPE_OBJECT)
67
68 static void
69 empathy_filter_class_init (EmpathyFilterClass *klass)
70 {
71         GObjectClass *object_class = G_OBJECT_CLASS (klass);
72
73         object_class->finalize = filter_finalize;
74
75         signals[NEW_CHANNEL] =
76                 g_signal_new ("new-channel",
77                               G_OBJECT_CLASS_TYPE (klass),
78                               G_SIGNAL_RUN_LAST,
79                               0,
80                               NULL, NULL,
81                               _empathy_marshal_VOID__OBJECT_OBJECT,
82                               G_TYPE_NONE,
83                               2, TELEPATHY_CONN_TYPE, TELEPATHY_CHAN_TYPE);
84
85         signals[PROCESS] =
86                 g_signal_new ("process",
87                               G_OBJECT_CLASS_TYPE (klass),
88                               G_SIGNAL_RUN_LAST,
89                               0,
90                               NULL, NULL,
91                               _empathy_marshal_VOID__UINT_BOOLEAN,
92                               G_TYPE_NONE,
93                               2, G_TYPE_UINT, G_TYPE_BOOLEAN);
94
95         g_type_class_add_private (object_class, sizeof (EmpathyFilterPriv));
96 }
97
98 static void
99 empathy_filter_init (EmpathyFilter *filter)
100 {
101         EmpathyFilterPriv *priv;
102
103         priv = GET_PRIV (filter);
104
105         priv->table = g_hash_table_new_full (g_direct_hash, g_direct_equal,
106                                              (GDestroyNotify) g_object_unref,
107                                              NULL);
108 }
109
110 static void
111 filter_finalize (GObject *object)
112 {
113         EmpathyFilterPriv *priv;
114
115         priv = GET_PRIV (object);
116
117         g_hash_table_destroy (priv->table);
118 }
119
120 EmpathyFilter *
121 empathy_filter_new (const gchar *bus_name,
122                     const gchar *object_path,
123                     const gchar *channel_type,
124                     guint        priority,
125                     guint        flags)
126 {
127         static gboolean  initialized = FALSE;
128         MissionControl  *mc;
129         EmpathyFilter   *filter;
130         DBusGProxy      *proxy;
131         guint            result;
132         GError          *error = NULL;
133
134         if (!initialized) {
135                 dbus_g_object_type_install_info (EMPATHY_TYPE_FILTER,
136                                                  &dbus_glib_empathy_filter_object_info);
137                 initialized = TRUE;
138         }
139
140         proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
141                                            DBUS_SERVICE_DBUS,
142                                            DBUS_PATH_DBUS,
143                                            DBUS_INTERFACE_DBUS);
144
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,
148                                 G_TYPE_INVALID,
149                                 G_TYPE_UINT, &result,
150                                 G_TYPE_INVALID)) {
151                 empathy_debug (DEBUG_DOMAIN,
152                                "Failed to request name: %s",
153                                error ? error->message : "No error given");
154                 g_clear_error (&error);
155
156                 return NULL;
157         }
158         g_object_unref (proxy);
159
160         filter = g_object_new (EMPATHY_TYPE_FILTER, NULL);
161         dbus_g_connection_register_g_object (tp_get_bus (),
162                                              object_path,
163                                              G_OBJECT (filter));
164
165         mc = empathy_mission_control_new ();
166         mission_control_register_filter (mc,
167                                          bus_name,
168                                          object_path,
169                                          channel_type,
170                                          priority,
171                                          flags,
172                                          NULL);
173         g_object_unref (mc);
174
175         return filter;
176 }
177
178 void
179 empathy_filter_process (EmpathyFilter *filter,
180                         TpChan        *tp_chan,
181                         gboolean       process)
182 {
183         EmpathyFilterPriv *priv;
184         guint              id;
185
186         g_return_if_fail (EMPATHY_IS_FILTER (filter));
187         g_return_if_fail (TELEPATHY_IS_CHAN (tp_chan));
188
189         priv = GET_PRIV (filter);
190
191         id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->table, tp_chan));
192         g_return_if_fail (id != 0);
193
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);
198 }
199
200 static gboolean
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,
206                                guint           handle_type,
207                                guint           handle,
208                                guint           id,
209                                GError        **error)
210 {
211         EmpathyFilterPriv *priv;
212         TpChan            *tp_chan;
213         TpConn            *tp_conn;
214
215         priv = GET_PRIV (filter);
216
217         tp_conn = tp_conn_new_without_connect (tp_get_bus (),
218                                                bus_name,
219                                                connection,
220                                                NULL,
221                                                error);
222         if (!tp_conn) {
223                 return FALSE;
224         }
225
226
227         tp_chan = tp_chan_new (tp_get_bus(),
228                                bus_name,
229                                channel,
230                                channel_type,
231                                handle_type,
232                                handle);
233
234         g_hash_table_insert (priv->table, tp_chan, GUINT_TO_POINTER (id));
235
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);
240
241         g_object_unref (tp_conn);
242
243         return TRUE;
244 }
245