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