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