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