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) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
40 EMPATHY_TYPE_TP_ROOMLIST, EmpathyTpRoomlistPriv))
42 struct _EmpathyTpRoomlistPriv {
43 TpConnection *connection;
49 static void empathy_tp_roomlist_class_init (EmpathyTpRoomlistClass *klass);
50 static void empathy_tp_roomlist_init (EmpathyTpRoomlist *chat);
64 static guint signals[LAST_SIGNAL];
66 G_DEFINE_TYPE (EmpathyTpRoomlist, empathy_tp_roomlist, G_TYPE_OBJECT);
69 tp_roomlist_listing_cb (TpChannel *channel,
74 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
76 DEBUG ("Listing: %s", listing ? "Yes" : "No");
77 priv->is_listing = listing;
78 g_object_notify (list, "is-listing");
82 tp_roomlist_got_rooms_cb (TpChannel *channel,
83 const GPtrArray *rooms,
87 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
93 names = g_new0 (const gchar*, rooms->len + 1);
94 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), rooms->len);
95 for (i = 0; i < rooms->len; i++) {
96 const GValue *room_name_value;
97 GValueArray *room_struct;
101 /* Get information */
102 room_struct = g_ptr_array_index (rooms, i);
103 handle = g_value_get_uint (g_value_array_get_nth (room_struct, 0));
104 info = g_value_get_boxed (g_value_array_get_nth (room_struct, 2));
105 room_name_value = g_hash_table_lookup (info, "name");
107 names[i] = g_value_get_string (room_name_value);
108 g_array_append_val (handles, handle);
111 tp_cli_connection_run_inspect_handles (priv->connection, -1,
116 for (i = 0; i < handles->len; i++) {
117 EmpathyChatroom *chatroom;
119 chatroom = empathy_chatroom_new_full (priv->account,
123 g_signal_emit (list, signals[NEW_ROOM], 0, chatroom);
124 g_object_unref (chatroom);
125 g_free (room_ids[i]);
130 g_array_free (handles, TRUE);
134 tp_roomlist_get_listing_rooms_cb (TpChannel *channel,
140 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
143 DEBUG ("Error geting listing rooms: %s", error->message);
147 priv->is_listing = is_listing;
148 g_object_notify (list, "is-listing");
152 tp_roomlist_invalidated_cb (TpChannel *channel,
156 EmpathyTpRoomlist *list)
158 DEBUG ("Channel invalidated: %s", message);
159 g_signal_emit (list, signals[DESTROY], 0);
163 tp_roomlist_request_channel_cb (TpConnection *connection,
164 const gchar *object_path,
169 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
172 DEBUG ("Error requesting channel: %s", error->message);
176 priv->channel = tp_channel_new (priv->connection, object_path,
177 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
180 tp_channel_run_until_ready (priv->channel, NULL, NULL);
182 g_signal_connect (priv->channel, "invalidated",
183 G_CALLBACK (tp_roomlist_invalidated_cb),
186 tp_cli_channel_type_room_list_connect_to_listing_rooms (priv->channel,
187 tp_roomlist_listing_cb,
191 tp_cli_channel_type_room_list_connect_to_got_rooms (priv->channel,
192 tp_roomlist_got_rooms_cb,
197 tp_cli_channel_type_room_list_call_get_listing_rooms (priv->channel, -1,
198 tp_roomlist_get_listing_rooms_cb,
204 tp_roomlist_finalize (GObject *object)
206 EmpathyTpRoomlistPriv *priv = GET_PRIV (object);
209 DEBUG ("Closing channel...");
210 g_signal_handlers_disconnect_by_func (priv->channel,
211 tp_roomlist_invalidated_cb,
213 tp_cli_channel_call_close (priv->channel, -1,
214 NULL, NULL, NULL, NULL);
215 g_object_unref (priv->channel);
219 g_object_unref (priv->account);
221 if (priv->connection) {
222 g_object_unref (priv->connection);
225 G_OBJECT_CLASS (empathy_tp_roomlist_parent_class)->finalize (object);
229 tp_roomlist_constructed (GObject *list)
231 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
234 mc = empathy_mission_control_new ();
235 priv->account = mission_control_get_account_for_tpconnection (mc,
240 tp_cli_connection_call_request_channel (priv->connection, -1,
241 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
245 tp_roomlist_request_channel_cb,
251 tp_roomlist_get_property (GObject *object,
256 EmpathyTpRoomlistPriv *priv = GET_PRIV (object);
259 case PROP_CONNECTION:
260 g_value_set_object (value, priv->connection);
262 case PROP_IS_LISTING:
263 g_value_set_boolean (value, priv->is_listing);
266 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
272 tp_roomlist_set_property (GObject *object,
277 EmpathyTpRoomlistPriv *priv = GET_PRIV (object);
280 case PROP_CONNECTION:
281 priv->connection = g_object_ref (g_value_get_object (value));
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
290 empathy_tp_roomlist_class_init (EmpathyTpRoomlistClass *klass)
292 GObjectClass *object_class = G_OBJECT_CLASS (klass);
294 object_class->finalize = tp_roomlist_finalize;
295 object_class->constructed = tp_roomlist_constructed;
296 object_class->get_property = tp_roomlist_get_property;
297 object_class->set_property = tp_roomlist_set_property;
299 g_object_class_install_property (object_class,
301 g_param_spec_object ("connection",
303 "The connection on which it lists rooms",
306 G_PARAM_CONSTRUCT_ONLY));
307 g_object_class_install_property (object_class,
309 g_param_spec_boolean ("is-listing",
311 "Are we listing rooms",
316 g_signal_new ("new-room",
317 G_TYPE_FROM_CLASS (klass),
321 g_cclosure_marshal_VOID__OBJECT,
323 1, EMPATHY_TYPE_CHATROOM);
326 g_signal_new ("destroy",
327 G_TYPE_FROM_CLASS (klass),
331 g_cclosure_marshal_VOID__VOID,
335 g_type_class_add_private (object_class, sizeof (EmpathyTpRoomlistPriv));
339 empathy_tp_roomlist_init (EmpathyTpRoomlist *list)
344 empathy_tp_roomlist_new (McAccount *account)
346 EmpathyTpRoomlist *list;
348 TpConnection *connection;
350 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
352 mc = empathy_mission_control_new ();
353 connection = mission_control_get_tpconnection (mc, account, NULL);
355 list = g_object_new (EMPATHY_TYPE_TP_ROOMLIST,
356 "connection", connection,
360 g_object_unref (connection);
366 empathy_tp_roomlist_is_listing (EmpathyTpRoomlist *list)
368 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
370 g_return_val_if_fail (EMPATHY_IS_TP_ROOMLIST (list), FALSE);
372 return priv->is_listing;
376 empathy_tp_roomlist_start (EmpathyTpRoomlist *list)
378 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
380 g_return_if_fail (EMPATHY_IS_TP_ROOMLIST (list));
381 g_return_if_fail (TP_IS_CHANNEL (priv->channel));
383 tp_cli_channel_type_room_list_call_list_rooms (priv->channel, -1,
389 empathy_tp_roomlist_stop (EmpathyTpRoomlist *list)
391 EmpathyTpRoomlistPriv *priv = GET_PRIV (list);
393 g_return_if_fail (EMPATHY_IS_TP_ROOMLIST (list));
394 g_return_if_fail (TP_IS_CHANNEL (priv->channel));
396 tp_cli_channel_type_room_list_call_stop_listing (priv->channel, -1,