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