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>
29 #include <libmissioncontrol/mission-control.h>
31 #include "empathy-tp-roomlist.h"
32 #include "empathy-chatroom.h"
33 #include "empathy-utils.h"
34 #include "empathy-debug.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 } EmpathyTpRoomlistPriv;
59 static guint signals[LAST_SIGNAL];
61 G_DEFINE_TYPE (EmpathyTpRoomlist, empathy_tp_roomlist, G_TYPE_OBJECT);
64 tp_roomlist_listing_cb (TpChannel *channel,
69 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
71 DEBUG ("Listing: %s", listing ? "Yes" : "No");
72 priv->is_listing = listing;
73 g_object_notify (list, "is-listing");
77 tp_roomlist_got_rooms_cb (TpChannel *channel,
78 const GPtrArray *rooms,
82 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
88 names = g_new0 (const gchar*, rooms->len + 1);
89 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), rooms->len);
90 for (i = 0; i < rooms->len; i++) {
91 const GValue *room_name_value;
92 GValueArray *room_struct;
97 room_struct = g_ptr_array_index (rooms, i);
98 handle = g_value_get_uint (g_value_array_get_nth (room_struct, 0));
99 info = g_value_get_boxed (g_value_array_get_nth (room_struct, 2));
100 room_name_value = g_hash_table_lookup (info, "name");
102 names[i] = g_value_get_string (room_name_value);
103 g_array_append_val (handles, handle);
106 tp_cli_connection_run_inspect_handles (priv->connection, -1,
111 for (i = 0; i < handles->len; i++) {
112 EmpathyChatroom *chatroom;
114 chatroom = empathy_chatroom_new_full (priv->account,
118 g_signal_emit (list, signals[NEW_ROOM], 0, chatroom);
119 g_object_unref (chatroom);
120 g_free (room_ids[i]);
125 g_array_free (handles, TRUE);
129 tp_roomlist_get_listing_rooms_cb (TpChannel *channel,
135 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
138 DEBUG ("Error geting listing rooms: %s", error->message);
142 priv->is_listing = is_listing;
143 g_object_notify (list, "is-listing");
147 tp_roomlist_invalidated_cb (TpChannel *channel,
151 EmpathyTpRoomlist *list)
153 DEBUG ("Channel invalidated: %s", message);
154 g_signal_emit (list, signals[DESTROY], 0);
158 tp_roomlist_request_channel_cb (TpConnection *connection,
159 const gchar *object_path,
164 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
167 DEBUG ("Error requesting channel: %s", error->message);
171 priv->channel = tp_channel_new (priv->connection, object_path,
172 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
175 tp_channel_run_until_ready (priv->channel, NULL, NULL);
177 g_signal_connect (priv->channel, "invalidated",
178 G_CALLBACK (tp_roomlist_invalidated_cb),
181 tp_cli_channel_type_room_list_connect_to_listing_rooms (priv->channel,
182 tp_roomlist_listing_cb,
186 tp_cli_channel_type_room_list_connect_to_got_rooms (priv->channel,
187 tp_roomlist_got_rooms_cb,
192 tp_cli_channel_type_room_list_call_get_listing_rooms (priv->channel, -1,
193 tp_roomlist_get_listing_rooms_cb,
199 tp_roomlist_finalize (GObject *object)
201 EmpathyTpRoomlistPriv *priv = GET_PRIV (object);
204 DEBUG ("Closing channel...");
205 g_signal_handlers_disconnect_by_func (priv->channel,
206 tp_roomlist_invalidated_cb,
208 tp_cli_channel_call_close (priv->channel, -1,
209 NULL, NULL, NULL, NULL);
210 g_object_unref (priv->channel);
214 g_object_unref (priv->account);
216 if (priv->connection) {
217 g_object_unref (priv->connection);
220 G_OBJECT_CLASS (empathy_tp_roomlist_parent_class)->finalize (object);
224 tp_roomlist_constructed (GObject *list)
226 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
229 mc = empathy_mission_control_new ();
230 priv->account = mission_control_get_account_for_tpconnection (mc,
235 tp_cli_connection_call_request_channel (priv->connection, -1,
236 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
240 tp_roomlist_request_channel_cb,
246 tp_roomlist_get_property (GObject *object,
251 EmpathyTpRoomlistPriv *priv = GET_PRIV (object);
254 case PROP_CONNECTION:
255 g_value_set_object (value, priv->connection);
257 case PROP_IS_LISTING:
258 g_value_set_boolean (value, priv->is_listing);
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
267 tp_roomlist_set_property (GObject *object,
272 EmpathyTpRoomlistPriv *priv = GET_PRIV (object);
275 case PROP_CONNECTION:
276 priv->connection = g_object_ref (g_value_get_object (value));
279 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
285 empathy_tp_roomlist_class_init (EmpathyTpRoomlistClass *klass)
287 GObjectClass *object_class = G_OBJECT_CLASS (klass);
289 object_class->finalize = tp_roomlist_finalize;
290 object_class->constructed = tp_roomlist_constructed;
291 object_class->get_property = tp_roomlist_get_property;
292 object_class->set_property = tp_roomlist_set_property;
294 g_object_class_install_property (object_class,
296 g_param_spec_object ("connection",
298 "The connection on which it lists rooms",
301 G_PARAM_CONSTRUCT_ONLY));
302 g_object_class_install_property (object_class,
304 g_param_spec_boolean ("is-listing",
306 "Are we listing rooms",
311 g_signal_new ("new-room",
312 G_TYPE_FROM_CLASS (klass),
316 g_cclosure_marshal_VOID__OBJECT,
318 1, EMPATHY_TYPE_CHATROOM);
321 g_signal_new ("destroy",
322 G_TYPE_FROM_CLASS (klass),
326 g_cclosure_marshal_VOID__VOID,
330 g_type_class_add_private (object_class, sizeof (EmpathyTpRoomlistPriv));
334 empathy_tp_roomlist_init (EmpathyTpRoomlist *list)
336 EmpathyTpRoomlistPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
337 EMPATHY_TYPE_TP_ROOMLIST, EmpathyTpRoomlistPriv);
343 empathy_tp_roomlist_new (McAccount *account)
345 EmpathyTpRoomlist *list;
347 TpConnection *connection;
349 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
351 mc = empathy_mission_control_new ();
352 connection = mission_control_get_tpconnection (mc, account, NULL);
354 list = g_object_new (EMPATHY_TYPE_TP_ROOMLIST,
355 "connection", connection,
359 g_object_unref (connection);
365 empathy_tp_roomlist_is_listing (EmpathyTpRoomlist *list)
367 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
369 g_return_val_if_fail (EMPATHY_IS_TP_ROOMLIST (list), FALSE);
371 return priv->is_listing;
375 empathy_tp_roomlist_start (EmpathyTpRoomlist *list)
377 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
379 g_return_if_fail (EMPATHY_IS_TP_ROOMLIST (list));
380 g_return_if_fail (TP_IS_CHANNEL (priv->channel));
382 tp_cli_channel_type_room_list_call_list_rooms (priv->channel, -1,
388 empathy_tp_roomlist_stop (EmpathyTpRoomlist *list)
390 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
392 g_return_if_fail (EMPATHY_IS_TP_ROOMLIST (list));
393 g_return_if_fail (TP_IS_CHANNEL (priv->channel));
395 tp_cli_channel_type_room_list_call_stop_listing (priv->channel, -1,