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