1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 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>
26 #include <telepathy-glib/channel.h>
27 #include <telepathy-glib/dbus.h>
28 #include <telepathy-glib/util.h>
30 #include <libmissioncontrol/mission-control.h>
32 #include "empathy-tp-roomlist.h"
33 #include "empathy-chatroom.h"
34 #include "empathy-utils.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_TP
37 #include "empathy-debug.h"
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpRoomlist)
41 TpConnection *connection;
45 gboolean start_requested;
46 } EmpathyTpRoomlistPriv;
61 static guint signals[LAST_SIGNAL];
63 G_DEFINE_TYPE (EmpathyTpRoomlist, empathy_tp_roomlist, G_TYPE_OBJECT);
66 tp_roomlist_listing_cb (TpChannel *channel,
71 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
73 DEBUG ("Listing: %s", listing ? "Yes" : "No");
74 priv->is_listing = listing;
75 g_object_notify (list, "is-listing");
79 tp_roomlist_chatrooms_free (gpointer data)
81 GSList *chatrooms = data;
83 g_slist_foreach (chatrooms, (GFunc) g_object_unref, NULL);
84 g_slist_free (chatrooms);
88 tp_roomlist_inspect_handles_cb (TpConnection *connection,
94 GSList *chatrooms = user_data;
97 DEBUG ("Error: %s", error->message);
101 while (*names != NULL) {
102 EmpathyChatroom *chatroom = chatrooms->data;
104 empathy_chatroom_set_room (chatroom, *names);
105 g_signal_emit (list, signals[NEW_ROOM], 0, chatroom);
108 chatrooms = chatrooms->next;
113 tp_roomlist_got_rooms_cb (TpChannel *channel,
114 const GPtrArray *rooms,
118 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
119 EmpathyChatroom *chatroom;
121 GArray *handles = NULL;
122 GSList *chatrooms = NULL;
124 for (i = 0; i < rooms->len; i++) {
125 const GValue *room_name_value;
126 const GValue *handle_name_value;
127 const GValue *room_members_value;
128 const GValue *room_subject_value;
129 const GValue *room_invite_value;
130 const GValue *room_password_value;
131 GValueArray *room_struct;
133 const gchar *channel_type;
136 /* Get information */
137 room_struct = g_ptr_array_index (rooms, i);
138 handle = g_value_get_uint (g_value_array_get_nth (room_struct, 0));
139 channel_type = g_value_get_string (g_value_array_get_nth (room_struct, 1));
140 info = g_value_get_boxed (g_value_array_get_nth (room_struct, 2));
141 room_name_value = g_hash_table_lookup (info, "name");
142 handle_name_value = g_hash_table_lookup (info, "handle-name");
143 room_subject_value = g_hash_table_lookup (info, "subject");
144 room_members_value = g_hash_table_lookup (info, "members");
145 room_invite_value = g_hash_table_lookup (info, "invite-only");
146 room_password_value = g_hash_table_lookup (info, "password");
148 if (tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)) {
152 chatroom = empathy_chatroom_new (priv->account);
154 if (room_name_value != NULL) {
155 empathy_chatroom_set_name (chatroom,
156 g_value_get_string (room_name_value));
159 if (room_members_value != NULL) {
160 empathy_chatroom_set_members_count (chatroom,
161 g_value_get_uint (room_members_value));
164 if (room_subject_value != NULL) {
165 empathy_chatroom_set_subject (chatroom,
166 g_value_get_string (room_subject_value));
169 if (room_invite_value != NULL) {
170 empathy_chatroom_set_invite_only (chatroom,
171 g_value_get_boolean (room_invite_value));
174 if (room_password_value != NULL) {
175 empathy_chatroom_set_need_password (chatroom,
176 g_value_get_boolean (room_password_value));
179 if (handle_name_value != NULL) {
180 empathy_chatroom_set_room (chatroom,
181 g_value_get_string (handle_name_value));
183 /* We have the room ID, we can directly emit it */
184 g_signal_emit (list, signals[NEW_ROOM], 0, chatroom);
185 g_object_unref (chatroom);
187 /* We don't have the room ID, we'll inspect all handles
188 * at once and then emit rooms */
189 if (handles == NULL) {
190 handles = g_array_new (FALSE, FALSE, sizeof (guint));
193 g_array_append_val (handles, handle);
194 chatrooms = g_slist_prepend (chatrooms, chatroom);
198 if (handles != NULL) {
199 chatrooms = g_slist_reverse (chatrooms);
200 tp_cli_connection_call_inspect_handles (priv->connection, -1,
203 tp_roomlist_inspect_handles_cb,
205 tp_roomlist_chatrooms_free,
207 g_array_free (handles, TRUE);
212 tp_roomlist_get_listing_rooms_cb (TpChannel *channel,
218 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
221 DEBUG ("Error geting listing rooms: %s", error->message);
225 priv->is_listing = is_listing;
226 g_object_notify (list, "is-listing");
230 tp_roomlist_invalidated_cb (TpChannel *channel,
234 EmpathyTpRoomlist *list)
236 DEBUG ("Channel invalidated: %s", message);
237 g_signal_emit (list, signals[DESTROY], 0);
241 call_list_rooms_cb (TpChannel *proxy,
244 GObject *weak_object)
247 DEBUG ("Error listing rooms: %s", error->message);
248 g_signal_emit_by_name (list, "error::start", error);
253 stop_listing_cb (TpChannel *proxy,
256 GObject *weak_object)
259 DEBUG ("Error on stop listing: %s", error->message);
260 g_signal_emit_by_name (list, "error::stop", error);
265 channel_ready_cb (TpChannel *channel,
269 EmpathyTpRoomlist *list = EMPATHY_TP_ROOMLIST (user_data);
270 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
273 DEBUG ("Channel invalidated: %s", error->message);
274 g_signal_emit (list, signals[DESTROY], 0);
278 g_signal_connect (priv->channel, "invalidated",
279 G_CALLBACK (tp_roomlist_invalidated_cb),
282 tp_cli_channel_type_room_list_connect_to_listing_rooms (priv->channel,
283 tp_roomlist_listing_cb,
287 tp_cli_channel_type_room_list_connect_to_got_rooms (priv->channel,
288 tp_roomlist_got_rooms_cb,
293 tp_cli_channel_type_room_list_call_get_listing_rooms (priv->channel, -1,
294 tp_roomlist_get_listing_rooms_cb,
298 if (priv->start_requested == TRUE) {
299 tp_cli_channel_type_room_list_call_list_rooms (priv->channel, -1,
300 call_list_rooms_cb, list, NULL, NULL);
301 priv->start_requested = FALSE;
306 tp_roomlist_request_channel_cb (TpConnection *connection,
307 const gchar *object_path,
312 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
315 DEBUG ("Error requesting channel: %s", error->message);
319 priv->channel = tp_channel_new (priv->connection, object_path,
320 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
323 tp_channel_call_when_ready (priv->channel, channel_ready_cb, list);
327 tp_roomlist_finalize (GObject *object)
329 EmpathyTpRoomlistPriv *priv = GET_PRIV (object);
332 DEBUG ("Closing channel...");
333 g_signal_handlers_disconnect_by_func (priv->channel,
334 tp_roomlist_invalidated_cb,
336 tp_cli_channel_call_close (priv->channel, -1,
337 NULL, NULL, NULL, NULL);
338 g_object_unref (priv->channel);
342 g_object_unref (priv->account);
344 if (priv->connection) {
345 g_object_unref (priv->connection);
348 G_OBJECT_CLASS (empathy_tp_roomlist_parent_class)->finalize (object);
352 tp_roomlist_constructed (GObject *list)
354 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
357 mc = empathy_mission_control_dup_singleton ();
358 priv->account = mission_control_get_account_for_tpconnection (mc,
363 tp_cli_connection_call_request_channel (priv->connection, -1,
364 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
368 tp_roomlist_request_channel_cb,
374 tp_roomlist_get_property (GObject *object,
379 EmpathyTpRoomlistPriv *priv = GET_PRIV (object);
382 case PROP_CONNECTION:
383 g_value_set_object (value, priv->connection);
385 case PROP_IS_LISTING:
386 g_value_set_boolean (value, priv->is_listing);
389 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
395 tp_roomlist_set_property (GObject *object,
400 EmpathyTpRoomlistPriv *priv = GET_PRIV (object);
403 case PROP_CONNECTION:
404 priv->connection = g_object_ref (g_value_get_object (value));
407 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
413 empathy_tp_roomlist_class_init (EmpathyTpRoomlistClass *klass)
415 GObjectClass *object_class = G_OBJECT_CLASS (klass);
417 object_class->finalize = tp_roomlist_finalize;
418 object_class->constructed = tp_roomlist_constructed;
419 object_class->get_property = tp_roomlist_get_property;
420 object_class->set_property = tp_roomlist_set_property;
422 g_object_class_install_property (object_class,
424 g_param_spec_object ("connection",
426 "The connection on which it lists rooms",
429 G_PARAM_CONSTRUCT_ONLY));
430 g_object_class_install_property (object_class,
432 g_param_spec_boolean ("is-listing",
434 "Are we listing rooms",
439 g_signal_new ("new-room",
440 G_TYPE_FROM_CLASS (klass),
444 g_cclosure_marshal_VOID__OBJECT,
446 1, EMPATHY_TYPE_CHATROOM);
449 g_signal_new ("destroy",
450 G_TYPE_FROM_CLASS (klass),
454 g_cclosure_marshal_VOID__VOID,
459 g_signal_new ("error",
460 G_TYPE_FROM_CLASS (klass),
461 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
464 g_cclosure_marshal_VOID__POINTER,
468 g_type_class_add_private (object_class, sizeof (EmpathyTpRoomlistPriv));
472 empathy_tp_roomlist_init (EmpathyTpRoomlist *list)
474 EmpathyTpRoomlistPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
475 EMPATHY_TYPE_TP_ROOMLIST, EmpathyTpRoomlistPriv);
478 priv->start_requested = FALSE;
479 priv->is_listing = FALSE;
483 empathy_tp_roomlist_new (McAccount *account)
485 EmpathyTpRoomlist *list;
487 TpConnection *connection;
489 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
491 mc = empathy_mission_control_dup_singleton ();
492 connection = mission_control_get_tpconnection (mc, account, NULL);
494 list = g_object_new (EMPATHY_TYPE_TP_ROOMLIST,
495 "connection", connection,
499 g_object_unref (connection);
505 empathy_tp_roomlist_is_listing (EmpathyTpRoomlist *list)
507 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
509 g_return_val_if_fail (EMPATHY_IS_TP_ROOMLIST (list), FALSE);
511 return priv->is_listing;
515 empathy_tp_roomlist_start (EmpathyTpRoomlist *list)
517 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
519 g_return_if_fail (EMPATHY_IS_TP_ROOMLIST (list));
520 if (priv->channel != NULL) {
521 tp_cli_channel_type_room_list_call_list_rooms (priv->channel, -1,
522 call_list_rooms_cb, list, NULL, NULL);
524 priv->start_requested = TRUE;
529 empathy_tp_roomlist_stop (EmpathyTpRoomlist *list)
531 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
533 g_return_if_fail (EMPATHY_IS_TP_ROOMLIST (list));
534 g_return_if_fail (TP_IS_CHANNEL (priv->channel));
536 tp_cli_channel_type_room_list_call_stop_listing (priv->channel, -1,
537 stop_listing_cb, list, NULL, NULL);