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