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