]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-roomlist.c
Fix reviewer comments
[empathy.git] / libempathy / empathy-tp-roomlist.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 <string.h>
25
26 #include <telepathy-glib/channel.h>
27 #include <telepathy-glib/dbus.h>
28 #include <telepathy-glib/util.h>
29
30 #include <libmissioncontrol/mission-control.h>
31
32 #include "empathy-tp-roomlist.h"
33 #include "empathy-chatroom.h"
34 #include "empathy-utils.h"
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_TP
37 #include "empathy-debug.h"
38
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpRoomlist)
40 typedef struct {
41         TpConnection *connection;
42         TpChannel    *channel;
43         McAccount    *account;
44         gboolean      is_listing;
45 } EmpathyTpRoomlistPriv;
46
47 enum {
48         NEW_ROOM,
49         DESTROY,
50         LAST_SIGNAL
51 };
52
53 enum {
54         PROP_0,
55         PROP_CONNECTION,
56         PROP_IS_LISTING,
57 };
58
59 static guint signals[LAST_SIGNAL];
60
61 G_DEFINE_TYPE (EmpathyTpRoomlist, empathy_tp_roomlist, G_TYPE_OBJECT);
62
63 static void
64 tp_roomlist_listing_cb (TpChannel *channel,
65                         gboolean   listing,
66                         gpointer   user_data,
67                         GObject   *list)
68 {
69         EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
70
71         DEBUG ("Listing: %s", listing ? "Yes" : "No");
72         priv->is_listing = listing;
73         g_object_notify (list, "is-listing");
74 }
75
76 static void
77 tp_roomlist_chatrooms_free (gpointer data)
78 {
79         GSList *chatrooms = data;
80
81         g_slist_foreach (chatrooms, (GFunc) g_object_unref, NULL);
82         g_slist_free (chatrooms);
83 }
84
85 static void
86 tp_roomlist_inspect_handles_cb (TpConnection *connection,
87                                 const gchar **names,
88                                 const GError *error,
89                                 gpointer      user_data,
90                                 GObject      *list)
91 {
92         GSList *chatrooms = user_data;
93
94         while (*names) {
95                 EmpathyChatroom *chatroom = chatrooms->data;
96
97                 empathy_chatroom_set_room (chatroom, *names);
98                 g_signal_emit (list, signals[NEW_ROOM], 0, chatroom);
99
100                 names++;
101                 chatrooms = chatrooms->next;
102         }
103 }
104
105 static void
106 tp_roomlist_got_rooms_cb (TpChannel       *channel,
107                           const GPtrArray *rooms,
108                           gpointer         user_data,
109                           GObject         *list)
110 {
111         EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
112         EmpathyChatroom       *chatroom;
113         guint                  i;
114         GArray                *handles = NULL;
115         GSList                *chatrooms = NULL;
116
117         for (i = 0; i < rooms->len; i++) {
118                 const GValue *room_name_value;
119                 const GValue *handle_name_value;
120                 GValueArray  *room_struct;
121                 guint         handle;
122                 const gchar  *channel_type;
123                 GHashTable   *info;
124
125                 /* Get information */
126                 room_struct = g_ptr_array_index (rooms, i);
127                 handle = g_value_get_uint (g_value_array_get_nth (room_struct, 0));
128                 channel_type = g_value_get_string (g_value_array_get_nth (room_struct, 1));
129                 info = g_value_get_boxed (g_value_array_get_nth (room_struct, 2));
130                 room_name_value = g_hash_table_lookup (info, "name");
131                 handle_name_value = g_hash_table_lookup (info, "handle-name");
132
133                 if (tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)) {
134                         continue;
135                 }
136
137                 chatroom = empathy_chatroom_new (priv->account);
138
139                 if (room_name_value != NULL) {
140                         empathy_chatroom_set_name (chatroom,
141                                                    g_value_get_string (room_name_value));
142                 }
143
144                 if (handle_name_value != NULL) {
145                         empathy_chatroom_set_room (chatroom,
146                                                    g_value_get_string (handle_name_value));
147
148                         /* We have the room ID, we can directly emit it */
149                         g_signal_emit (list, signals[NEW_ROOM], 0, chatroom);
150                         g_object_unref (chatroom);
151                 } else {
152                         /* We don't have the room ID, we'll inspect all handles
153                          * at once and then emit rooms */
154                         if (handles == NULL) {
155                                 handles = g_array_new (FALSE, FALSE, sizeof (guint));
156                         }
157
158                         g_array_append_val (handles, handle);
159                         chatrooms = g_slist_prepend (chatrooms, chatroom);
160                 }
161         }
162
163         if (handles != NULL) {
164                 chatrooms = g_slist_reverse (chatrooms);
165                 tp_cli_connection_call_inspect_handles (priv->connection, -1,
166                                                        TP_HANDLE_TYPE_ROOM,
167                                                        handles,
168                                                        tp_roomlist_inspect_handles_cb,
169                                                        chatrooms,
170                                                        tp_roomlist_chatrooms_free,
171                                                        list);
172         }
173
174         g_array_free (handles, TRUE);
175 }
176
177 static void
178 tp_roomlist_get_listing_rooms_cb (TpChannel    *channel,
179                                   gboolean      is_listing,
180                                   const GError *error,
181                                   gpointer      user_data,
182                                   GObject      *list)
183 {
184         EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
185
186         if (error) {
187                 DEBUG ("Error geting listing rooms: %s", error->message);
188                 return;
189         }
190
191         priv->is_listing = is_listing;
192         g_object_notify (list, "is-listing");
193 }
194
195 static void
196 tp_roomlist_invalidated_cb (TpChannel         *channel,
197                             guint              domain,
198                             gint               code,
199                             gchar             *message,
200                             EmpathyTpRoomlist *list)
201 {
202         DEBUG ("Channel invalidated: %s", message);
203         g_signal_emit (list, signals[DESTROY], 0);
204 }
205
206 static void
207 tp_roomlist_request_channel_cb (TpConnection *connection,
208                                 const gchar  *object_path,
209                                 const GError *error,
210                                 gpointer      user_data,
211                                 GObject      *list)
212 {
213         EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
214
215         if (error) {
216                 DEBUG ("Error requesting channel: %s", error->message);
217                 return;
218         }
219
220         priv->channel = tp_channel_new (priv->connection, object_path,
221                                         TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
222                                         TP_HANDLE_TYPE_NONE,
223                                         0, NULL);
224         tp_channel_run_until_ready (priv->channel, NULL, NULL);
225
226         g_signal_connect (priv->channel, "invalidated",
227                           G_CALLBACK (tp_roomlist_invalidated_cb),
228                           list);
229
230         tp_cli_channel_type_room_list_connect_to_listing_rooms (priv->channel,
231                                                                 tp_roomlist_listing_cb,
232                                                                 NULL, NULL,
233                                                                 G_OBJECT (list),
234                                                                 NULL);
235         tp_cli_channel_type_room_list_connect_to_got_rooms (priv->channel,
236                                                             tp_roomlist_got_rooms_cb,
237                                                             NULL, NULL,
238                                                             G_OBJECT (list),
239                                                             NULL);
240
241         tp_cli_channel_type_room_list_call_get_listing_rooms (priv->channel, -1,
242                                                               tp_roomlist_get_listing_rooms_cb,
243                                                               NULL, NULL,
244                                                               G_OBJECT (list));
245 }
246
247 static void
248 tp_roomlist_finalize (GObject *object)
249 {
250         EmpathyTpRoomlistPriv *priv = GET_PRIV (object);
251
252         if (priv->channel) {
253                 DEBUG ("Closing channel...");
254                 g_signal_handlers_disconnect_by_func (priv->channel,
255                                                       tp_roomlist_invalidated_cb,
256                                                       object);
257                 tp_cli_channel_call_close (priv->channel, -1,
258                                            NULL, NULL, NULL, NULL);
259                 g_object_unref (priv->channel);
260         }
261
262         if (priv->account) {
263                 g_object_unref (priv->account);
264         }
265         if (priv->connection) {
266                 g_object_unref (priv->connection);
267         }
268
269         G_OBJECT_CLASS (empathy_tp_roomlist_parent_class)->finalize (object);
270 }
271
272 static void
273 tp_roomlist_constructed (GObject *list)
274 {
275         EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
276         MissionControl        *mc;
277
278         mc = empathy_mission_control_new ();
279         priv->account = mission_control_get_account_for_tpconnection (mc,
280                                                                       priv->connection,
281                                                                       NULL);
282         g_object_unref (mc);
283
284         tp_cli_connection_call_request_channel (priv->connection, -1,
285                                                 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
286                                                 TP_HANDLE_TYPE_NONE,
287                                                 0,
288                                                 TRUE,
289                                                 tp_roomlist_request_channel_cb,
290                                                 NULL, NULL,
291                                                 list);
292 }
293
294 static void
295 tp_roomlist_get_property (GObject    *object,
296                           guint       param_id,
297                           GValue     *value,
298                           GParamSpec *pspec)
299 {
300         EmpathyTpRoomlistPriv *priv = GET_PRIV (object);
301
302         switch (param_id) {
303         case PROP_CONNECTION:
304                 g_value_set_object (value, priv->connection);
305                 break;
306         case PROP_IS_LISTING:
307                 g_value_set_boolean (value, priv->is_listing);
308                 break;
309         default:
310                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
311                 break;
312         };
313 }
314
315 static void
316 tp_roomlist_set_property (GObject      *object,
317                           guint         param_id,
318                           const GValue *value,
319                           GParamSpec   *pspec)
320 {
321         EmpathyTpRoomlistPriv *priv = GET_PRIV (object);
322
323         switch (param_id) {
324         case PROP_CONNECTION:
325                 priv->connection = g_object_ref (g_value_get_object (value));
326                 break;
327         default:
328                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
329                 break;
330         };
331 }
332
333 static void
334 empathy_tp_roomlist_class_init (EmpathyTpRoomlistClass *klass)
335 {
336         GObjectClass *object_class = G_OBJECT_CLASS (klass);
337
338         object_class->finalize = tp_roomlist_finalize;
339         object_class->constructed = tp_roomlist_constructed;
340         object_class->get_property = tp_roomlist_get_property;
341         object_class->set_property = tp_roomlist_set_property;
342
343         g_object_class_install_property (object_class,
344                                          PROP_CONNECTION,
345                                          g_param_spec_object ("connection",
346                                                               "The Connection",
347                                                               "The connection on which it lists rooms",
348                                                               TP_TYPE_CONNECTION,
349                                                               G_PARAM_READWRITE |
350                                                               G_PARAM_CONSTRUCT_ONLY));
351         g_object_class_install_property (object_class,
352                                          PROP_IS_LISTING,
353                                          g_param_spec_boolean ("is-listing",
354                                                                "Is listing",
355                                                                "Are we listing rooms",
356                                                                FALSE,
357                                                                G_PARAM_READABLE));
358
359         signals[NEW_ROOM] =
360                 g_signal_new ("new-room",
361                               G_TYPE_FROM_CLASS (klass),
362                               G_SIGNAL_RUN_LAST,
363                               0,
364                               NULL, NULL,
365                               g_cclosure_marshal_VOID__OBJECT,
366                               G_TYPE_NONE,
367                               1, EMPATHY_TYPE_CHATROOM);
368
369         signals[DESTROY] =
370                 g_signal_new ("destroy",
371                               G_TYPE_FROM_CLASS (klass),
372                               G_SIGNAL_RUN_LAST,
373                               0,
374                               NULL, NULL,
375                               g_cclosure_marshal_VOID__VOID,
376                               G_TYPE_NONE,
377                               0);
378
379         g_type_class_add_private (object_class, sizeof (EmpathyTpRoomlistPriv));
380 }
381
382 static void
383 empathy_tp_roomlist_init (EmpathyTpRoomlist *list)
384 {
385         EmpathyTpRoomlistPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
386                 EMPATHY_TYPE_TP_ROOMLIST, EmpathyTpRoomlistPriv);
387
388         list->priv = priv;
389 }
390
391 EmpathyTpRoomlist *
392 empathy_tp_roomlist_new (McAccount *account)
393 {
394         EmpathyTpRoomlist *list;
395         MissionControl    *mc;
396         TpConnection      *connection;
397
398         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
399
400         mc = empathy_mission_control_new ();
401         connection = mission_control_get_tpconnection (mc, account, NULL);
402
403         list = g_object_new (EMPATHY_TYPE_TP_ROOMLIST,
404                              "connection", connection,
405                              NULL);
406
407         g_object_unref (mc);
408         g_object_unref (connection);
409
410         return list;
411 }
412
413 gboolean
414 empathy_tp_roomlist_is_listing (EmpathyTpRoomlist *list)
415 {
416         EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
417
418         g_return_val_if_fail (EMPATHY_IS_TP_ROOMLIST (list), FALSE);
419
420         return priv->is_listing;
421 }
422
423 void
424 empathy_tp_roomlist_start (EmpathyTpRoomlist *list)
425 {
426         EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
427
428         g_return_if_fail (EMPATHY_IS_TP_ROOMLIST (list));
429         g_return_if_fail (TP_IS_CHANNEL (priv->channel));
430
431         tp_cli_channel_type_room_list_call_list_rooms (priv->channel, -1,
432                                                        NULL, NULL, NULL, NULL);
433 }
434
435 void
436 empathy_tp_roomlist_stop (EmpathyTpRoomlist *list)
437 {
438         EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
439
440         g_return_if_fail (EMPATHY_IS_TP_ROOMLIST (list));
441         g_return_if_fail (TP_IS_CHANNEL (priv->channel));
442
443         tp_cli_channel_type_room_list_call_stop_listing (priv->channel, -1,
444                                                          NULL, NULL, NULL, NULL);
445 }
446