]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-chat.c
Merge branch 'gnome-3-8'
[empathy.git] / libempathy / empathy-tp-chat.c
1 /*
2  * Copyright (C) 2007-2012 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  */
20
21 #include "config.h"
22 #include "empathy-tp-chat.h"
23
24 #include <tp-account-widgets/tpaw-utils.h>
25
26 #include "empathy-request-util.h"
27 #include "empathy-utils.h"
28
29 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
30 #include "empathy-debug.h"
31
32 struct _EmpathyTpChatPrivate
33 {
34   TpAccount *account;
35   EmpathyContact *user;
36   EmpathyContact *remote_contact;
37   GList *members;
38   /* Queue of messages signalled but not acked yet */
39   GQueue *pending_messages_queue;
40
41   /* Subject */
42   gboolean supports_subject;
43   gboolean can_set_subject;
44   gchar *subject;
45   gchar *subject_actor;
46
47   /* Room config (for now, we only track the title and don't support
48    * setting it) */
49   gchar *title;
50
51   gboolean can_upgrade_to_muc;
52
53   GHashTable *messages_being_sent;
54
55   /* GSimpleAsyncResult used when preparing EMPATHY_TP_CHAT_FEATURE_CORE */
56   GSimpleAsyncResult *ready_result;
57   gboolean preparing_password;
58 };
59
60 enum
61 {
62   PROP_0,
63   PROP_ACCOUNT,
64   PROP_SELF_CONTACT,
65   PROP_REMOTE_CONTACT,
66   PROP_N_MESSAGES_SENDING,
67   PROP_TITLE,
68   PROP_SUBJECT,
69 };
70
71 enum
72 {
73   MESSAGE_RECEIVED,
74   SEND_ERROR,
75   MESSAGE_ACKNOWLEDGED,
76   SIG_MEMBER_RENAMED,
77   SIG_MEMBERS_CHANGED,
78   LAST_SIGNAL
79 };
80
81 static guint signals[LAST_SIGNAL];
82
83 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, TP_TYPE_TEXT_CHANNEL)
84
85 static void
86 tp_chat_set_delivery_status (EmpathyTpChat *self,
87     const gchar *token,
88     EmpathyDeliveryStatus delivery_status)
89 {
90   TpDeliveryReportingSupportFlags flags =
91     tp_text_channel_get_delivery_reporting_support (
92       TP_TEXT_CHANNEL (self));
93
94   /* channel must support receiving failures and successes */
95   if (!tp_str_empty (token) &&
96       flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
97       flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES)
98     {
99       DEBUG ("Delivery status (%s) = %u", token, delivery_status);
100
101       switch (delivery_status)
102         {
103           case EMPATHY_DELIVERY_STATUS_NONE:
104             g_hash_table_remove (self->priv->messages_being_sent,
105               token);
106             break;
107
108           default:
109             g_hash_table_insert (self->priv->messages_being_sent,
110               g_strdup (token),
111               GUINT_TO_POINTER (delivery_status));
112             break;
113         }
114
115     g_object_notify (G_OBJECT (self), "n-messages-sending");
116   }
117 }
118
119 static void tp_chat_prepare_ready_async (TpProxy *proxy,
120   const TpProxyFeature *feature,
121   GAsyncReadyCallback callback,
122   gpointer user_data);
123
124 static void
125 tp_chat_async_cb (TpChannel *proxy,
126     const GError *error,
127     gpointer user_data,
128     GObject *weak_object)
129 {
130   if (error != NULL)
131     DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
132 }
133
134 static void
135 update_config_cb (TpChannel *proxy,
136     const GError *error,
137     gpointer user_data,
138     GObject *weak_object)
139 {
140   if (error != NULL)
141     DEBUG ("Failed to change config of the room: %s", error->message);
142 }
143
144 static void
145 create_conference_cb (GObject *source,
146     GAsyncResult *result,
147     gpointer user_data)
148 {
149   TpChannel *channel;
150   GError *error = NULL;
151   GHashTable *props;
152
153   channel = tp_account_channel_request_create_and_observe_channel_finish (
154       TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
155   if (channel == NULL)
156     {
157       DEBUG ("Failed to create conference channel: %s", error->message);
158       g_error_free (error);
159       return;
160     }
161
162   /* Make the channel more confidential as only people invited are supposed to
163    * join it. */
164   props = tp_asv_new (
165       "Private", G_TYPE_BOOLEAN, TRUE,
166       "InviteOnly", G_TYPE_BOOLEAN, TRUE,
167       NULL);
168
169   tp_cli_channel_interface_room_config_call_update_configuration (channel, -1,
170       props, update_config_cb, NULL, NULL, NULL);
171
172   g_object_unref (channel);
173   g_hash_table_unref (props);
174 }
175
176 void
177 empathy_tp_chat_add (EmpathyTpChat *self,
178     EmpathyContact *contact,
179     const gchar *message)
180 {
181   TpChannel *channel = (TpChannel *) self;
182
183   if (tp_proxy_has_interface_by_id (self,
184     TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
185     {
186       TpHandle handle;
187       GArray handles = {(gchar *) &handle, 1};
188
189       g_return_if_fail (EMPATHY_IS_CONTACT (contact));
190
191       handle = empathy_contact_get_handle (contact);
192       tp_cli_channel_interface_group_call_add_members (channel,
193         -1, &handles, NULL, NULL, NULL, NULL, NULL);
194     }
195   else if (self->priv->can_upgrade_to_muc)
196     {
197       TpAccountChannelRequest *req;
198       GHashTable *props;
199       const char *object_path;
200       GPtrArray channels = { (gpointer *) &object_path, 1 };
201       const char *invitees[2] = { NULL, };
202       TpAccount *account;
203
204       invitees[0] = empathy_contact_get_id (contact);
205       object_path = tp_proxy_get_object_path (self);
206
207       props = tp_asv_new (
208           TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
209               TP_IFACE_CHANNEL_TYPE_TEXT,
210           TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
211               TP_HANDLE_TYPE_NONE,
212           TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
213               TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
214           TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
215               G_TYPE_STRV, invitees,
216           /* FIXME: InvitationMessage ? */
217           NULL);
218
219       account = empathy_tp_chat_get_account (self);
220
221       req = tp_account_channel_request_new (account, props,
222         TP_USER_ACTION_TIME_NOT_USER_ACTION);
223
224       /* Although this is a MUC, it's anonymous, so CreateChannel is
225        * valid. */
226       tp_account_channel_request_create_and_observe_channel_async (req,
227           EMPATHY_CHAT_BUS_NAME, NULL, create_conference_cb, NULL);
228
229       g_object_unref (req);
230       g_hash_table_unref (props);
231     }
232   else
233     {
234       g_warning ("Cannot add to this channel");
235     }
236 }
237
238 GList *
239 empathy_tp_chat_get_members (EmpathyTpChat *self)
240 {
241   GList *members = NULL;
242
243   if (self->priv->members)
244     {
245       members = g_list_copy (self->priv->members);
246       g_list_foreach (members, (GFunc) g_object_ref, NULL);
247     }
248   else
249     {
250       members = g_list_prepend (members, g_object_ref (self->priv->user));
251
252       if (self->priv->remote_contact != NULL)
253         members = g_list_prepend (members,
254             g_object_ref (self->priv->remote_contact));
255     }
256
257   return members;
258 }
259
260 static void
261 check_ready (EmpathyTpChat *self)
262 {
263   if (self->priv->ready_result == NULL)
264     return;
265
266   DEBUG ("Ready");
267
268   g_simple_async_result_complete_in_idle (self->priv->ready_result);
269   tp_clear_object (&self->priv->ready_result);
270 }
271
272 static void
273 tp_chat_build_message (EmpathyTpChat *self,
274     TpMessage *msg,
275     gboolean incoming)
276 {
277   EmpathyMessage *message;
278   TpContact *sender;
279
280   message = empathy_message_new_from_tp_message (msg, incoming);
281   /* FIXME: this is actually a lie for incoming messages. */
282   empathy_message_set_receiver (message, self->priv->user);
283
284   sender = tp_signalled_message_get_sender (msg);
285   g_assert (sender != NULL);
286
287   if (tp_contact_get_handle (sender) == 0)
288     {
289       empathy_message_set_sender (message, self->priv->user);
290     }
291   else
292     {
293       EmpathyContact *contact;
294
295       contact = empathy_contact_dup_from_tp_contact (sender);
296
297       empathy_message_set_sender (message, contact);
298
299       g_object_unref (contact);
300     }
301
302   g_queue_push_tail (self->priv->pending_messages_queue, message);
303   g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
304 }
305
306 static void
307 handle_delivery_report (EmpathyTpChat *self,
308     TpMessage *message)
309 {
310   TpDeliveryStatus delivery_status;
311   const GHashTable *header;
312   TpChannelTextSendError delivery_error;
313   gboolean valid;
314   GPtrArray *echo;
315   const gchar *message_body = NULL;
316   const gchar *delivery_dbus_error;
317   const gchar *delivery_token = NULL;
318
319   header = tp_message_peek (message, 0);
320   if (header == NULL)
321     goto out;
322
323   delivery_token = tp_asv_get_string (header, "delivery-token");
324   delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
325
326   if (!valid)
327     {
328       goto out;
329     }
330   else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED)
331     {
332       DEBUG ("Accepted %s", delivery_token);
333       tp_chat_set_delivery_status (self, delivery_token,
334         EMPATHY_DELIVERY_STATUS_ACCEPTED);
335       goto out;
336     }
337   else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED)
338     {
339       DEBUG ("Delivered %s", delivery_token);
340       tp_chat_set_delivery_status (self, delivery_token,
341         EMPATHY_DELIVERY_STATUS_NONE);
342       goto out;
343     }
344   else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED &&
345        delivery_status != TP_DELIVERY_STATUS_TEMPORARILY_FAILED)
346     {
347       goto out;
348     }
349
350   delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
351   if (!valid)
352     delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
353
354   delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
355
356   /* TODO: ideally we should use tp-glib API giving us the echoed message as a
357    * TpMessage. (fdo #35884) */
358   echo = tp_asv_get_boxed (header, "delivery-echo",
359     TP_ARRAY_TYPE_MESSAGE_PART_LIST);
360   if (echo != NULL && echo->len >= 2)
361     {
362       const GHashTable *echo_body;
363
364       echo_body = g_ptr_array_index (echo, 1);
365       if (echo_body != NULL)
366         message_body = tp_asv_get_string (echo_body, "content");
367     }
368
369   tp_chat_set_delivery_status (self, delivery_token,
370       EMPATHY_DELIVERY_STATUS_NONE);
371   g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
372       delivery_error, delivery_dbus_error);
373
374 out:
375   tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
376     message, NULL, NULL);
377 }
378
379 static void
380 handle_incoming_message (EmpathyTpChat *self,
381     TpMessage *message,
382     gboolean pending)
383 {
384   gchar *message_body;
385
386   if (tp_message_is_delivery_report (message))
387     {
388       handle_delivery_report (self, message);
389       return;
390     }
391
392   message_body = tp_message_to_text (message, NULL);
393
394   DEBUG ("Message %s (channel %s): %s",
395     pending ? "pending" : "received",
396     tp_proxy_get_object_path (self), message_body);
397
398   if (message_body == NULL)
399     {
400       DEBUG ("Empty message with NonTextContent, ignoring and acking.");
401
402       tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
403         message, NULL, NULL);
404       return;
405     }
406
407   tp_chat_build_message (self, message, TRUE);
408
409   g_free (message_body);
410 }
411
412 static void
413 message_received_cb (TpTextChannel *channel,
414     TpMessage *message,
415     EmpathyTpChat *self)
416 {
417   handle_incoming_message (self, message, FALSE);
418 }
419
420 static gboolean
421 find_pending_message_func (gconstpointer a,
422     gconstpointer b)
423 {
424   EmpathyMessage *msg = (EmpathyMessage *) a;
425   TpMessage *message = (TpMessage *) b;
426
427   if (empathy_message_get_tp_message (msg) == message)
428     return 0;
429
430   return -1;
431 }
432
433 static void
434 pending_message_removed_cb (TpTextChannel   *channel,
435     TpMessage *message,
436     EmpathyTpChat *self)
437 {
438   GList *m;
439
440   m = g_queue_find_custom (self->priv->pending_messages_queue, message,
441       find_pending_message_func);
442
443   if (m == NULL)
444     return;
445
446   g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
447
448   g_object_unref (m->data);
449   g_queue_delete_link (self->priv->pending_messages_queue, m);
450 }
451
452 static void
453 message_sent_cb (TpTextChannel *channel,
454     TpMessage *message,
455     TpMessageSendingFlags flags,
456     gchar *token,
457     EmpathyTpChat *self)
458 {
459   gchar *message_body;
460
461   message_body = tp_message_to_text (message, NULL);
462
463   DEBUG ("Message sent: %s", message_body);
464
465   tp_chat_build_message (self, message, FALSE);
466
467   g_free (message_body);
468 }
469
470 static TpChannelTextSendError
471 error_to_text_send_error (GError *error)
472 {
473   if (error->domain != TP_ERROR)
474     return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
475
476   switch (error->code)
477     {
478       case TP_ERROR_OFFLINE:
479         return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
480       case TP_ERROR_INVALID_HANDLE:
481         return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
482       case TP_ERROR_PERMISSION_DENIED:
483         return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
484       case TP_ERROR_NOT_IMPLEMENTED:
485         return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
486     }
487
488   return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
489 }
490
491 static void
492 message_send_cb (GObject *source,
493      GAsyncResult *result,
494      gpointer user_data)
495 {
496   EmpathyTpChat *self = user_data;
497   TpTextChannel *channel = (TpTextChannel *) source;
498   gchar *token = NULL;
499   GError *error = NULL;
500
501   if (!tp_text_channel_send_message_finish (channel, result, &token, &error))
502     {
503       DEBUG ("Error: %s", error->message);
504
505       /* FIXME: we should use the body of the message as first argument of the
506        * signal but can't easily get it as we just get a user_data pointer. Once
507        * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
508        * to use the user_data pointer to pass the message and fix this. */
509       g_signal_emit (self, signals[SEND_ERROR], 0,
510                NULL, error_to_text_send_error (error), NULL);
511
512       g_error_free (error);
513     }
514
515   tp_chat_set_delivery_status (self, token,
516     EMPATHY_DELIVERY_STATUS_SENDING);
517   g_free (token);
518 }
519
520 static void
521 list_pending_messages (EmpathyTpChat *self)
522 {
523   GList *messages, *l;
524
525   messages = tp_text_channel_dup_pending_messages (TP_TEXT_CHANNEL (self));
526
527   for (l = messages; l != NULL; l = g_list_next (l))
528     {
529       TpMessage *message = l->data;
530
531       handle_incoming_message (self, message, FALSE);
532     }
533
534   g_list_free_full (messages, g_object_unref);
535 }
536
537 static void
538 update_subject (EmpathyTpChat *self,
539     GHashTable *properties)
540 {
541   gboolean can_set, valid;
542   const gchar *subject;
543
544   can_set = tp_asv_get_boolean (properties, "CanSet", &valid);
545   if (valid)
546     self->priv->can_set_subject = can_set;
547
548   subject = tp_asv_get_string (properties, "Subject");
549   if (subject != NULL)
550     {
551       const gchar *actor;
552
553       g_free (self->priv->subject);
554       self->priv->subject = g_strdup (subject);
555
556       /* If the actor is included with this update, use it;
557        * otherwise, clear it to avoid showing stale information.
558        * Why might it not be included? When you join an IRC channel,
559        * you get a pair of messages: first, the current topic; next,
560        * who set it, and when. Idle reports these in two separate
561        * signals.
562        */
563       actor = tp_asv_get_string (properties, "Actor");
564       g_free (self->priv->subject_actor);
565       self->priv->subject_actor = g_strdup (actor);
566
567       g_object_notify (G_OBJECT (self), "subject");
568     }
569
570   /* TODO: track Timestamp. */
571 }
572
573 static void
574 tp_chat_get_all_subject_cb (TpProxy *proxy,
575     GHashTable *properties,
576     const GError *error,
577     gpointer user_data G_GNUC_UNUSED,
578     GObject *chat)
579 {
580   EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
581
582   if (error != NULL)
583     {
584       DEBUG ("Error fetching subject: %s", error->message);
585       return;
586     }
587
588   self->priv->supports_subject = TRUE;
589   update_subject (self, properties);
590 }
591
592 static void
593 update_title (EmpathyTpChat *self,
594     GHashTable *properties)
595 {
596   const gchar *title = tp_asv_get_string (properties, "Title");
597
598   if (title != NULL)
599     {
600       if (tp_str_empty (title))
601         title = NULL;
602
603       g_free (self->priv->title);
604       self->priv->title = g_strdup (title);
605       g_object_notify (G_OBJECT (self), "title");
606     }
607 }
608
609 static void
610 tp_chat_get_all_room_config_cb (TpProxy *proxy,
611     GHashTable *properties,
612     const GError *error,
613     gpointer user_data G_GNUC_UNUSED,
614     GObject *chat)
615 {
616   EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
617
618   if (error)
619     {
620       DEBUG ("Error fetching room config: %s", error->message);
621       return;
622     }
623
624   update_title (self, properties);
625 }
626
627 static void
628 tp_chat_dbus_properties_changed_cb (TpProxy *proxy,
629     const gchar *interface_name,
630     GHashTable *changed,
631     const gchar **invalidated,
632     gpointer user_data,
633     GObject *chat)
634 {
635   EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
636
637   if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_SUBJECT))
638     update_subject (self, changed);
639
640   if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG))
641     update_title (self, changed);
642 }
643
644 void
645 empathy_tp_chat_set_subject (EmpathyTpChat *self,
646     const gchar *subject)
647 {
648   tp_cli_channel_interface_subject_call_set_subject (TP_CHANNEL (self), -1,
649       subject, tp_chat_async_cb, "while setting subject", NULL,
650       G_OBJECT (self));
651 }
652
653 const gchar *
654 empathy_tp_chat_get_title (EmpathyTpChat *self)
655 {
656   return self->priv->title;
657 }
658
659 gboolean
660 empathy_tp_chat_supports_subject (EmpathyTpChat *self)
661 {
662   return self->priv->supports_subject;
663 }
664
665 gboolean
666 empathy_tp_chat_can_set_subject (EmpathyTpChat *self)
667 {
668   return self->priv->can_set_subject;
669 }
670
671 const gchar *
672 empathy_tp_chat_get_subject (EmpathyTpChat *self)
673 {
674   return self->priv->subject;
675 }
676
677 const gchar *
678 empathy_tp_chat_get_subject_actor (EmpathyTpChat *self)
679 {
680   return self->priv->subject_actor;
681 }
682
683 static void
684 tp_chat_dispose (GObject *object)
685 {
686   EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
687
688   tp_clear_object (&self->priv->remote_contact);
689   tp_clear_object (&self->priv->user);
690
691   g_queue_foreach (self->priv->pending_messages_queue,
692     (GFunc) g_object_unref, NULL);
693   g_queue_clear (self->priv->pending_messages_queue);
694
695   tp_clear_object (&self->priv->ready_result);
696
697   if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
698     G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
699 }
700
701 static void
702 tp_chat_finalize (GObject *object)
703 {
704   EmpathyTpChat *self = (EmpathyTpChat *) object;
705
706   DEBUG ("Finalize: %p", object);
707
708   g_queue_free (self->priv->pending_messages_queue);
709   g_hash_table_unref (self->priv->messages_being_sent);
710
711   g_free (self->priv->title);
712   g_free (self->priv->subject);
713   g_free (self->priv->subject_actor);
714
715   G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
716 }
717
718 static void
719 check_almost_ready (EmpathyTpChat *self)
720 {
721   TpChannel *channel = (TpChannel *) self;
722
723   if (self->priv->ready_result == NULL)
724     return;
725
726   if (self->priv->user == NULL)
727     return;
728
729   if (self->priv->preparing_password)
730     return;
731
732   /* We need either the members (room) or the remote contact (private chat).
733    * If the chat is protected by a password we can't get these information so
734    * consider the chat as ready so it can be presented to the user. */
735   if (!tp_channel_password_needed (channel) && self->priv->members == NULL &&
736       self->priv->remote_contact == NULL)
737     return;
738
739   g_assert (tp_proxy_is_prepared (self,
740     TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
741
742   tp_g_signal_connect_object (self, "message-received",
743     G_CALLBACK (message_received_cb), self, 0);
744   tp_g_signal_connect_object (self, "pending-message-removed",
745     G_CALLBACK (pending_message_removed_cb), self, 0);
746
747   list_pending_messages (self);
748
749   tp_g_signal_connect_object (self, "message-sent",
750     G_CALLBACK (message_sent_cb), self, 0);
751
752   check_ready (self);
753 }
754
755 static void
756 add_members_contact (EmpathyTpChat *self,
757     GPtrArray *contacts)
758 {
759   guint i;
760
761   for (i = 0; i < contacts->len; i++)
762     {
763       EmpathyContact *contact;
764
765       contact = empathy_contact_dup_from_tp_contact (g_ptr_array_index (
766             contacts, i));
767
768       self->priv->members = g_list_prepend (self->priv->members, contact);
769
770       g_signal_emit (self, signals[SIG_MEMBERS_CHANGED], 0,
771                  contact, NULL, 0, NULL, TRUE);
772     }
773
774   check_almost_ready (self);
775 }
776
777 static void
778 remove_member (EmpathyTpChat *self,
779     EmpathyContact *contact)
780 {
781   GList *l;
782
783   for (l = self->priv->members; l; l = l->next)
784     {
785       EmpathyContact *c = l->data;
786
787       if (contact == c)
788         {
789           self->priv->members = g_list_delete_link (self->priv->members, l);
790           g_object_unref (c);
791           break;
792         }
793     }
794 }
795
796 static void
797 contact_renamed (EmpathyTpChat *self,
798     TpContact *old_contact,
799     TpContact *new_contact,
800     TpChannelGroupChangeReason reason,
801     const gchar *message)
802 {
803   EmpathyContact *old = NULL, *new = NULL;
804
805   old = empathy_contact_dup_from_tp_contact (old_contact);
806   new = empathy_contact_dup_from_tp_contact (new_contact);
807
808   self->priv->members = g_list_prepend (self->priv->members, new);
809
810   if (old != NULL)
811     {
812       remove_member (self, old);
813
814       g_signal_emit (self, signals[SIG_MEMBER_RENAMED], 0, old, new,
815           reason, message);
816       g_object_unref (old);
817     }
818
819   if (self->priv->user == old)
820     {
821       /* We change our nick */
822       tp_clear_object (&self->priv->user);
823       self->priv->user = g_object_ref (new);
824       g_object_notify (G_OBJECT (self), "self-contact");
825     }
826
827   check_almost_ready (self);
828 }
829
830 static void
831 tp_chat_group_contacts_changed_cb (TpChannel *channel,
832     GPtrArray *added,
833     GPtrArray *removed,
834     GPtrArray *local_pending,
835     GPtrArray *remote_pending,
836     TpContact *actor,
837     GHashTable *details,
838     EmpathyTpChat *self)
839 {
840   EmpathyContact *actor_contact = NULL;
841   guint i;
842   TpChannelGroupChangeReason reason;
843   const gchar *message;
844
845   reason = tp_asv_get_uint32 (details, "change-reason", NULL);
846   message = tp_asv_get_string (details, "message");
847
848   /* Contact renamed */
849   if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED)
850     {
851       /* there can only be a single 'added' and a single 'removed' handle */
852       if (removed->len != 1 || added->len != 1)
853         {
854           g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
855             added->len, removed->len);
856           return;
857         }
858
859       contact_renamed (self, g_ptr_array_index (removed, 0),
860           g_ptr_array_index (added, 0), reason, message);
861       return;
862     }
863
864   if (actor != NULL)
865     {
866       actor_contact = empathy_contact_dup_from_tp_contact (actor);
867
868       if (actor_contact == NULL)
869         {
870           /* FIXME: handle this a tad more gracefully: perhaps
871            * the actor was a server op. We could use the
872            * contact-ids detail of MembersChangedDetailed.
873            */
874           DEBUG ("actor %s not a channel member",
875               tp_contact_get_identifier (actor));
876         }
877     }
878
879   /* Remove contacts that are not members anymore */
880   for (i = 0; i < removed->len; i++)
881     {
882       TpContact *tp_contact = g_ptr_array_index (removed, i);
883       EmpathyContact *contact;
884
885       contact = empathy_contact_dup_from_tp_contact (tp_contact);
886
887       if (contact != NULL)
888         {
889           remove_member (self, contact);
890
891           g_signal_emit (self, signals[SIG_MEMBERS_CHANGED], 0,
892                      contact, actor_contact, reason, message, FALSE);
893           g_object_unref (contact);
894         }
895     }
896
897   if (added->len > 0)
898     {
899       add_members_contact (self, added);
900     }
901
902   if (actor_contact != NULL)
903     g_object_unref (actor_contact);
904 }
905
906 static void
907 create_remote_contact (EmpathyTpChat *self,
908     TpContact *contact)
909 {
910   self->priv->remote_contact = empathy_contact_dup_from_tp_contact (contact);
911   g_object_notify (G_OBJECT (self), "remote-contact");
912
913   check_almost_ready (self);
914 }
915
916 static void
917 create_self_contact (EmpathyTpChat *self,
918     TpContact *contact)
919 {
920   self->priv->user = empathy_contact_dup_from_tp_contact (contact);
921   empathy_contact_set_is_user (self->priv->user, TRUE);
922   g_object_notify (G_OBJECT (self), "self-contact");
923   check_almost_ready (self);
924 }
925
926 static void
927 tp_chat_get_property (GObject *object,
928     guint param_id,
929     GValue *value,
930     GParamSpec *pspec)
931 {
932   EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
933
934   switch (param_id)
935     {
936       case PROP_SELF_CONTACT:
937         g_value_set_object (value, self->priv->user);
938         break;
939       case PROP_REMOTE_CONTACT:
940         g_value_set_object (value, self->priv->remote_contact);
941         break;
942       case PROP_N_MESSAGES_SENDING:
943         g_value_set_uint (value,
944           g_hash_table_size (self->priv->messages_being_sent));
945         break;
946       case PROP_TITLE:
947         g_value_set_string (value,
948           empathy_tp_chat_get_title (self));
949         break;
950       case PROP_SUBJECT:
951         g_value_set_string (value,
952           empathy_tp_chat_get_subject (self));
953         break;
954       default:
955         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
956         break;
957     };
958 }
959
960 enum {
961   FEAT_READY,
962   N_FEAT
963 };
964
965 static const TpProxyFeature *
966 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
967 {
968   static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
969   static GQuark need[2] = {0, 0};
970
971   if (G_LIKELY (features[0].name != 0))
972     return features;
973
974   features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
975   need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
976   features[FEAT_READY].depends_on = need;
977   features[FEAT_READY].prepare_async =
978     tp_chat_prepare_ready_async;
979
980   /* assert that the terminator at the end is there */
981   g_assert (features[N_FEAT].name == 0);
982
983   return features;
984 }
985
986 static void
987 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
988 {
989   GObjectClass *object_class = G_OBJECT_CLASS (klass);
990   TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
991
992   object_class->dispose = tp_chat_dispose;
993   object_class->finalize = tp_chat_finalize;
994   object_class->get_property = tp_chat_get_property;
995
996   proxy_class->list_features = tp_chat_list_features;
997
998   /**
999    * EmpathyTpChat:self-contact:
1000    *
1001    * Not to be confused with TpChannel:group-self-contact.
1002    */
1003   g_object_class_install_property (object_class, PROP_SELF_CONTACT,
1004       g_param_spec_object ("self-contact", "The local contact",
1005         "The EmpathyContact for the local user on this channel",
1006         EMPATHY_TYPE_CONTACT,
1007         G_PARAM_READABLE));
1008
1009   g_object_class_install_property (object_class, PROP_REMOTE_CONTACT,
1010       g_param_spec_object ("remote-contact", "The remote contact",
1011         "The remote contact if there is no group iface on the channel",
1012         EMPATHY_TYPE_CONTACT,
1013         G_PARAM_READABLE));
1014
1015   g_object_class_install_property (object_class, PROP_N_MESSAGES_SENDING,
1016       g_param_spec_uint ("n-messages-sending", "Num Messages Sending",
1017         "The number of messages being sent",
1018         0, G_MAXUINT, 0,
1019         G_PARAM_READABLE));
1020
1021   g_object_class_install_property (object_class, PROP_TITLE,
1022       g_param_spec_string ("title", "Title",
1023         "A human-readable name for the room, if any",
1024         NULL,
1025         G_PARAM_READABLE |
1026         G_PARAM_STATIC_STRINGS));
1027
1028   g_object_class_install_property (object_class, PROP_SUBJECT,
1029       g_param_spec_string ("subject", "Subject",
1030         "The room's current subject, if any",
1031         NULL,
1032         G_PARAM_READABLE |
1033         G_PARAM_STATIC_STRINGS));
1034
1035   /* Signals */
1036   signals[MESSAGE_RECEIVED] = g_signal_new ("message-received-empathy",
1037       G_TYPE_FROM_CLASS (klass),
1038       G_SIGNAL_RUN_LAST,
1039       0,
1040       NULL, NULL,
1041       g_cclosure_marshal_generic,
1042       G_TYPE_NONE,
1043       1, EMPATHY_TYPE_MESSAGE);
1044
1045   signals[SEND_ERROR] = g_signal_new ("send-error",
1046       G_TYPE_FROM_CLASS (klass),
1047       G_SIGNAL_RUN_LAST,
1048       0,
1049       NULL, NULL,
1050       g_cclosure_marshal_generic,
1051       G_TYPE_NONE,
1052       3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1053
1054   signals[MESSAGE_ACKNOWLEDGED] = g_signal_new ("message-acknowledged",
1055       G_TYPE_FROM_CLASS (klass),
1056       G_SIGNAL_RUN_LAST,
1057       0,
1058       NULL, NULL,
1059       g_cclosure_marshal_generic,
1060       G_TYPE_NONE,
1061       1, EMPATHY_TYPE_MESSAGE);
1062
1063   signals[SIG_MEMBER_RENAMED] = g_signal_new ("member-renamed",
1064       G_OBJECT_CLASS_TYPE (klass),
1065       G_SIGNAL_RUN_LAST,
1066       0, NULL, NULL, NULL,
1067       G_TYPE_NONE,
1068       4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT,
1069       G_TYPE_UINT, G_TYPE_STRING);
1070
1071   signals[SIG_MEMBERS_CHANGED] = g_signal_new ("members-changed",
1072       G_OBJECT_CLASS_TYPE (klass),
1073       G_SIGNAL_RUN_LAST,
1074       0, NULL, NULL, NULL,
1075       G_TYPE_NONE,
1076       5, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT,
1077       G_TYPE_UINT, G_TYPE_STRING, G_TYPE_BOOLEAN);
1078
1079   g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1080 }
1081
1082 static void
1083 empathy_tp_chat_init (EmpathyTpChat *self)
1084 {
1085   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_TP_CHAT,
1086       EmpathyTpChatPrivate);
1087
1088   self->priv->pending_messages_queue = g_queue_new ();
1089   self->priv->messages_being_sent = g_hash_table_new_full (
1090       g_str_hash, g_str_equal, g_free, NULL);
1091 }
1092
1093 EmpathyTpChat *
1094 empathy_tp_chat_new (TpSimpleClientFactory *factory,
1095     TpConnection *conn,
1096     const gchar *object_path,
1097     const GHashTable *immutable_properties)
1098 {
1099   g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1100   g_return_val_if_fail (immutable_properties != NULL, NULL);
1101
1102   return g_object_new (EMPATHY_TYPE_TP_CHAT,
1103       "factory", factory,
1104        "connection", conn,
1105        "dbus-daemon", tp_proxy_get_dbus_daemon (conn),
1106        "bus-name", tp_proxy_get_bus_name (conn),
1107        "object-path", object_path,
1108        "channel-properties", immutable_properties,
1109        NULL);
1110 }
1111
1112 const gchar *
1113 empathy_tp_chat_get_id (EmpathyTpChat *self)
1114 {
1115   const gchar *id;
1116
1117   g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1118
1119   id = tp_channel_get_identifier ((TpChannel *) self);
1120   if (!TPAW_STR_EMPTY (id))
1121     return id;
1122   else if (self->priv->remote_contact)
1123     return empathy_contact_get_id (self->priv->remote_contact);
1124   else
1125     return NULL;
1126
1127 }
1128
1129 EmpathyContact *
1130 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1131 {
1132   g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1133
1134   return self->priv->remote_contact;
1135 }
1136
1137 TpAccount *
1138 empathy_tp_chat_get_account (EmpathyTpChat *self)
1139 {
1140   TpConnection *connection;
1141
1142   g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1143
1144   connection = tp_channel_get_connection (TP_CHANNEL (self));
1145
1146   return tp_connection_get_account (connection);
1147 }
1148
1149 void
1150 empathy_tp_chat_send (EmpathyTpChat *self,
1151           TpMessage *message)
1152 {
1153   gchar *message_body;
1154
1155   g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1156   g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1157
1158   message_body = tp_message_to_text (message, NULL);
1159
1160   DEBUG ("Sending message: %s", message_body);
1161
1162   tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1163     message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1164     message_send_cb, self);
1165
1166   g_free (message_body);
1167 }
1168
1169 const GList *
1170 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1171 {
1172   g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1173
1174   return self->priv->pending_messages_queue->head;
1175 }
1176
1177 void
1178 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1179     EmpathyMessage *message)
1180 {
1181   TpMessage *tp_msg;
1182
1183   g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1184
1185   if (!empathy_message_is_incoming (message))
1186     return;
1187
1188   tp_msg = empathy_message_get_tp_message (message);
1189   tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1190              tp_msg, NULL, NULL);
1191 }
1192
1193 /**
1194  * empathy_tp_chat_can_add_contact:
1195  *
1196  * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1197  * That is if this chat is a 1-to-1 channel that can be upgraded to
1198  * a MUC using the Conference interface or if the channel is a MUC.
1199  */
1200 gboolean
1201 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1202 {
1203   g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1204
1205   return self->priv->can_upgrade_to_muc ||
1206     tp_proxy_has_interface_by_id (self,
1207       TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1208 }
1209
1210 static void
1211 tp_channel_leave_async_cb (GObject *source_object,
1212         GAsyncResult *res,
1213         gpointer user_data)
1214 {
1215   GError *error = NULL;
1216
1217   if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error))
1218     {
1219       DEBUG ("Could not leave channel properly: (%s); closing the channel",
1220         error->message);
1221       g_error_free (error);
1222     }
1223 }
1224
1225 void
1226 empathy_tp_chat_leave (EmpathyTpChat *self,
1227     const gchar *message)
1228 {
1229   TpChannel *channel = (TpChannel *) self;
1230
1231   DEBUG ("Leaving channel %s with message \"%s\"",
1232     tp_channel_get_identifier (channel), message);
1233
1234   tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1235     message, tp_channel_leave_async_cb, self);
1236 }
1237
1238 gboolean
1239 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1240     TpContact **inviter)
1241 {
1242   TpContact *self_contact;
1243   TpChannel *channel = TP_CHANNEL (self);
1244
1245   if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1246     return FALSE;
1247
1248   self_contact = tp_channel_group_get_self_contact (channel);
1249   if (self_contact == NULL)
1250     return FALSE;
1251
1252   return tp_channel_group_get_local_pending_contact_info (channel,
1253       self_contact, inviter, NULL, NULL);
1254 }
1255
1256 TpChannelChatState
1257 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1258     EmpathyContact *contact)
1259 {
1260   return tp_text_channel_get_chat_state ((TpTextChannel *) self,
1261     empathy_contact_get_tp_contact (contact));
1262 }
1263
1264 EmpathyContact *
1265 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1266 {
1267   g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1268
1269   return self->priv->user;
1270 }
1271
1272 GQuark
1273 empathy_tp_chat_get_feature_ready (void)
1274 {
1275   return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1276 }
1277
1278 static void
1279 password_feature_prepare_cb (GObject *source,
1280     GAsyncResult *result,
1281     gpointer user_data)
1282 {
1283   EmpathyTpChat *self = user_data;
1284   GError *error = NULL;
1285
1286   if (!tp_proxy_prepare_finish (source, result, &error))
1287     {
1288       DEBUG ("Failed to prepare Password: %s", error->message);
1289       g_error_free (error);
1290     }
1291
1292   self->priv->preparing_password = FALSE;
1293
1294   check_almost_ready (self);
1295 }
1296
1297 static void
1298 continue_preparing (EmpathyTpChat *self)
1299 {
1300   TpChannel *channel = (TpChannel *) self;
1301   TpConnection *connection;
1302   gboolean listen_for_dbus_properties_changed = FALSE;
1303
1304   connection = tp_channel_get_connection (channel);
1305
1306   if (tp_proxy_has_interface_by_id (self,
1307         TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD))
1308     {
1309       /* The password feature can't be a hard dep on our own feature has we
1310        * depend on it only if the channel implements the
1311        * Password interface.
1312        */
1313       GQuark features[] = { TP_CHANNEL_FEATURE_PASSWORD , 0 };
1314
1315       self->priv->preparing_password = TRUE;
1316
1317       tp_proxy_prepare_async (self, features, password_feature_prepare_cb,
1318           self);
1319     }
1320
1321   if (tp_proxy_has_interface_by_id (self,
1322             TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
1323     {
1324       GPtrArray *contacts;
1325       TpContact *contact;
1326
1327       /* Get self contact from the group's self handle */
1328       contact = tp_channel_group_get_self_contact (channel);
1329       create_self_contact (self, contact);
1330
1331       /* Get initial member contacts */
1332       contacts = tp_channel_group_dup_members_contacts (channel);
1333       add_members_contact (self, contacts);
1334       g_ptr_array_unref (contacts);
1335
1336       self->priv->can_upgrade_to_muc = FALSE;
1337
1338       tp_g_signal_connect_object (self, "group-contacts-changed",
1339         G_CALLBACK (tp_chat_group_contacts_changed_cb), self, 0);
1340     }
1341   else
1342     {
1343       TpCapabilities *caps;
1344       GVariant *classes, *class;
1345       GVariantIter iter;
1346       TpContact *contact;
1347
1348       /* Get the self contact from the connection's self handle */
1349       contact = tp_connection_get_self_contact (connection);
1350       create_self_contact (self, contact);
1351
1352       /* Get the remote contact */
1353       contact = tp_channel_get_target_contact (channel);
1354       create_remote_contact (self, contact);
1355
1356       caps = tp_connection_get_capabilities (connection);
1357       g_assert (caps != NULL);
1358
1359       classes = tp_capabilities_dup_channel_classes_variant (caps);
1360
1361       g_variant_iter_init (&iter, classes);
1362       while ((class = g_variant_iter_next_value (&iter)))
1363         {
1364           GVariant *fixed, *allowed;
1365           const gchar *chan_type = NULL;
1366
1367           fixed = g_variant_get_child_value (class, 0);
1368           allowed = g_variant_get_child_value (class, 1);
1369
1370           g_variant_lookup (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE, "&s",
1371               &chan_type);
1372           if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
1373             {
1374               const gchar **oprops;
1375
1376               oprops = g_variant_get_strv (allowed, NULL);
1377
1378               if (tp_strv_contains (oprops,
1379                     TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS))
1380                 {
1381                   self->priv->can_upgrade_to_muc = TRUE;
1382                 }
1383
1384               g_free (oprops);
1385             }
1386
1387           g_variant_unref (class);
1388           g_variant_unref (fixed);
1389           g_variant_unref (allowed);
1390
1391           if (self->priv->can_upgrade_to_muc)
1392             break;
1393         }
1394
1395       g_variant_unref (classes);
1396     }
1397
1398   if (tp_proxy_has_interface_by_id (self,
1399             TP_IFACE_QUARK_CHANNEL_INTERFACE_SUBJECT))
1400     {
1401       tp_cli_dbus_properties_call_get_all (channel, -1,
1402                    TP_IFACE_CHANNEL_INTERFACE_SUBJECT,
1403                    tp_chat_get_all_subject_cb,
1404                    NULL, NULL,
1405                    G_OBJECT (self));
1406       listen_for_dbus_properties_changed = TRUE;
1407     }
1408
1409   if (tp_proxy_has_interface_by_id (self,
1410             TP_IFACE_QUARK_CHANNEL_INTERFACE_ROOM_CONFIG))
1411     {
1412       tp_cli_dbus_properties_call_get_all (channel, -1,
1413                    TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG,
1414                    tp_chat_get_all_room_config_cb,
1415                    NULL, NULL,
1416                    G_OBJECT (self));
1417       listen_for_dbus_properties_changed = TRUE;
1418     }
1419
1420   if (listen_for_dbus_properties_changed)
1421     {
1422       tp_cli_dbus_properties_connect_to_properties_changed (channel,
1423                         tp_chat_dbus_properties_changed_cb,
1424                         NULL, NULL,
1425                         G_OBJECT (self), NULL);
1426     }
1427 }
1428
1429 static void
1430 conn_connected_cb (GObject *source,
1431     GAsyncResult *result,
1432     gpointer user_data)
1433 {
1434   EmpathyTpChat *self = user_data;
1435   GError *error = NULL;
1436
1437   if (!tp_proxy_prepare_finish (source, result, &error))
1438     {
1439       DEBUG ("Failed to prepare Connected: %s", error->message);
1440       g_simple_async_result_take_error (self->priv->ready_result, error);
1441       g_simple_async_result_complete (self->priv->ready_result);
1442       tp_clear_object (&self->priv->ready_result);
1443       return;
1444     }
1445
1446   continue_preparing (self);
1447 }
1448
1449 static void
1450 tp_chat_prepare_ready_async (TpProxy *proxy,
1451   const TpProxyFeature *feature,
1452   GAsyncReadyCallback callback,
1453   gpointer user_data)
1454 {
1455   EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1456   TpChannel *channel = (TpChannel *) proxy;
1457   TpConnection *connection;
1458   GQuark features[] = { TP_CONNECTION_FEATURE_CONNECTED, 0 };
1459
1460   g_assert (self->priv->ready_result == NULL);
1461
1462   self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1463     callback, user_data, tp_chat_prepare_ready_async);
1464
1465   connection = tp_channel_get_connection (channel);
1466
1467   /* First we have to make sure that TP_CONNECTION_FEATURE_CONNECTED is
1468    * prepared as we rely on TpConnection::self-contact
1469    * in continue_preparing().
1470    *
1471    * It would be nice if tp-glib could do this for us: fdo#59126 */
1472   tp_proxy_prepare_async (connection, features, conn_connected_cb, proxy);
1473 }