CallWindow: show 'On hold' in the statusbar when appropriate
[empathy-mirror.git] / libempathy / empathy-tp-chat.c
blob02bdc80e396eaf124204ac23f0e023ce11183ced
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
22 #include <config.h>
24 #include <string.h>
26 #include <telepathy-glib/telepathy-glib.h>
28 #include <extensions/extensions.h>
30 #include "empathy-tp-chat.h"
31 #include "empathy-tp-contact-factory.h"
32 #include "empathy-contact-list.h"
33 #include "empathy-marshal.h"
34 #include "empathy-request-util.h"
35 #include "empathy-time.h"
36 #include "empathy-utils.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
39 #include "empathy-debug.h"
41 struct _EmpathyTpChatPrivate {
42 TpAccount *account;
43 EmpathyContact *user;
44 EmpathyContact *remote_contact;
45 GList *members;
46 /* Queue of messages not signalled yet */
47 GQueue *messages_queue;
48 /* Queue of messages signalled but not acked yet */
49 GQueue *pending_messages_queue;
50 gboolean had_properties_list;
51 GPtrArray *properties;
52 TpChannelPasswordFlags password_flags;
53 /* TRUE if we fetched the password flag of the channel or if it's not needed
54 * (channel doesn't implement the Password interface) */
55 gboolean got_password_flags;
56 gboolean can_upgrade_to_muc;
57 gboolean got_sms_channel;
58 gboolean sms_channel;
60 GHashTable *messages_being_sent;
62 /* GSimpleAsyncResult used when preparing EMPATHY_TP_CHAT_FEATURE_CORE */
63 GSimpleAsyncResult *ready_result;
66 static void tp_chat_iface_init (EmpathyContactListIface *iface);
68 enum {
69 PROP_0,
70 PROP_ACCOUNT,
71 PROP_REMOTE_CONTACT,
72 PROP_PASSWORD_NEEDED,
73 PROP_SMS_CHANNEL,
74 PROP_N_MESSAGES_SENDING,
77 enum {
78 MESSAGE_RECEIVED,
79 SEND_ERROR,
80 CHAT_STATE_CHANGED,
81 PROPERTY_CHANGED,
82 MESSAGE_ACKNOWLEDGED,
83 LAST_SIGNAL
86 static guint signals[LAST_SIGNAL];
88 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, TP_TYPE_TEXT_CHANNEL,
89 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
90 tp_chat_iface_init));
92 static void
93 tp_chat_set_delivery_status (EmpathyTpChat *self,
94 const gchar *token,
95 EmpathyDeliveryStatus delivery_status)
97 TpDeliveryReportingSupportFlags flags =
98 tp_text_channel_get_delivery_reporting_support (
99 TP_TEXT_CHANNEL (self));
101 /* channel must support receiving failures and successes */
102 if (!tp_str_empty (token) &&
103 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
104 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES) {
106 DEBUG ("Delivery status (%s) = %u", token, delivery_status);
108 switch (delivery_status) {
109 case EMPATHY_DELIVERY_STATUS_NONE:
110 g_hash_table_remove (self->priv->messages_being_sent,
111 token);
112 break;
114 default:
115 g_hash_table_insert (self->priv->messages_being_sent,
116 g_strdup (token),
117 GUINT_TO_POINTER (delivery_status));
118 break;
121 g_object_notify (G_OBJECT (self), "n-messages-sending");
125 static void tp_chat_prepare_ready_async (TpProxy *proxy,
126 const TpProxyFeature *feature,
127 GAsyncReadyCallback callback,
128 gpointer user_data);
130 static void
131 tp_chat_async_cb (TpChannel *proxy,
132 const GError *error,
133 gpointer user_data,
134 GObject *weak_object)
136 if (error) {
137 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
141 static void
142 create_conference_cb (GObject *source,
143 GAsyncResult *result,
144 gpointer user_data)
146 GError *error = NULL;
148 if (!tp_account_channel_request_create_channel_finish (
149 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error)) {
150 DEBUG ("Failed to create conference channel: %s", error->message);
151 g_error_free (error);
155 static void
156 tp_chat_add (EmpathyContactList *list,
157 EmpathyContact *contact,
158 const gchar *message)
160 EmpathyTpChat *self = (EmpathyTpChat *) list;
161 TpChannel *channel = (TpChannel *) self;
163 if (tp_proxy_has_interface_by_id (self,
164 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
165 TpHandle handle;
166 GArray handles = {(gchar *) &handle, 1};
168 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
169 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
171 handle = empathy_contact_get_handle (contact);
172 tp_cli_channel_interface_group_call_add_members (channel,
173 -1, &handles, NULL, NULL, NULL, NULL, NULL);
174 } else if (self->priv->can_upgrade_to_muc) {
175 TpAccountChannelRequest *req;
176 GHashTable *props;
177 const char *object_path;
178 GPtrArray channels = { (gpointer *) &object_path, 1 };
179 const char *invitees[2] = { NULL, };
181 invitees[0] = empathy_contact_get_id (contact);
182 object_path = tp_proxy_get_object_path (self);
184 props = tp_asv_new (
185 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
186 TP_IFACE_CHANNEL_TYPE_TEXT,
187 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
188 TP_HANDLE_TYPE_NONE,
189 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
190 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
191 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
192 G_TYPE_STRV, invitees,
193 /* FIXME: InvitationMessage ? */
194 NULL);
196 req = tp_account_channel_request_new (self->priv->account, props,
197 TP_USER_ACTION_TIME_NOT_USER_ACTION);
199 /* Although this is a MUC, it's anonymous, so CreateChannel is
200 * valid. */
201 tp_account_channel_request_create_channel_async (req, EMPATHY_CHAT_BUS_NAME,
202 NULL, create_conference_cb, NULL);
204 g_object_unref (req);
205 g_hash_table_unref (props);
206 } else {
207 g_warning ("Cannot add to this channel");
211 static void
212 tp_chat_remove (EmpathyContactList *list,
213 EmpathyContact *contact,
214 const gchar *message)
216 EmpathyTpChat *self = (EmpathyTpChat *) list;
217 TpHandle handle;
218 GArray handles = {(gchar *) &handle, 1};
220 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
221 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
223 handle = empathy_contact_get_handle (contact);
224 tp_cli_channel_interface_group_call_remove_members ((TpChannel *) self, -1,
225 &handles, NULL,
226 NULL, NULL, NULL,
227 NULL);
230 static GList *
231 tp_chat_get_members (EmpathyContactList *list)
233 EmpathyTpChat *self = (EmpathyTpChat *) list;
234 GList *members = NULL;
236 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
238 if (self->priv->members) {
239 members = g_list_copy (self->priv->members);
240 g_list_foreach (members, (GFunc) g_object_ref, NULL);
241 } else {
242 members = g_list_prepend (members, g_object_ref (self->priv->user));
243 if (self->priv->remote_contact != NULL)
244 members = g_list_prepend (members, g_object_ref (self->priv->remote_contact));
247 return members;
250 static void
251 check_ready (EmpathyTpChat *self)
253 if (self->priv->ready_result == NULL)
254 return;
256 if (g_queue_get_length (self->priv->messages_queue) > 0)
257 return;
259 DEBUG ("Ready");
261 g_simple_async_result_complete (self->priv->ready_result);
262 tp_clear_object (&self->priv->ready_result);
265 static void
266 tp_chat_emit_queued_messages (EmpathyTpChat *self)
268 EmpathyMessage *message;
270 /* Check if we can now emit some queued messages */
271 while ((message = g_queue_peek_head (self->priv->messages_queue)) != NULL) {
272 if (empathy_message_get_sender (message) == NULL) {
273 break;
276 DEBUG ("Queued message ready");
277 g_queue_pop_head (self->priv->messages_queue);
278 g_queue_push_tail (self->priv->pending_messages_queue, message);
279 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
282 check_ready (self);
285 static void
286 tp_chat_got_sender_cb (TpConnection *connection,
287 EmpathyContact *contact,
288 const GError *error,
289 gpointer message,
290 GObject *chat)
292 EmpathyTpChat *self = (EmpathyTpChat *) chat;
294 if (error) {
295 DEBUG ("Error: %s", error->message);
296 /* Do not block the message queue, just drop this message */
297 g_queue_remove (self->priv->messages_queue, message);
298 } else {
299 empathy_message_set_sender (message, contact);
302 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (self));
305 static void
306 tp_chat_build_message (EmpathyTpChat *self,
307 TpMessage *msg,
308 gboolean incoming)
310 EmpathyMessage *message;
311 TpContact *sender;
313 message = empathy_message_new_from_tp_message (msg, incoming);
314 /* FIXME: this is actually a lie for incoming messages. */
315 empathy_message_set_receiver (message, self->priv->user);
317 g_queue_push_tail (self->priv->messages_queue, message);
319 sender = tp_signalled_message_get_sender (msg);
320 g_assert (sender != NULL);
322 if (tp_contact_get_handle (sender) == 0) {
323 empathy_message_set_sender (message, self->priv->user);
324 tp_chat_emit_queued_messages (self);
325 } else {
326 TpConnection *connection = tp_channel_borrow_connection (
327 (TpChannel *) self);
329 empathy_tp_contact_factory_get_from_handle (connection,
330 tp_contact_get_handle (sender),
331 tp_chat_got_sender_cb,
332 message, NULL, G_OBJECT (self));
336 static void
337 handle_delivery_report (EmpathyTpChat *self,
338 TpMessage *message)
340 TpDeliveryStatus delivery_status;
341 const GHashTable *header;
342 TpChannelTextSendError delivery_error;
343 gboolean valid;
344 GPtrArray *echo;
345 const gchar *message_body = NULL;
346 const gchar *delivery_dbus_error;
347 const gchar *delivery_token = NULL;
349 header = tp_message_peek (message, 0);
350 if (header == NULL)
351 goto out;
353 delivery_token = tp_asv_get_string (header, "delivery-token");
354 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
356 if (!valid) {
357 goto out;
358 } else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED) {
359 DEBUG ("Accepted %s", delivery_token);
360 tp_chat_set_delivery_status (self, delivery_token,
361 EMPATHY_DELIVERY_STATUS_ACCEPTED);
362 goto out;
363 } else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED) {
364 DEBUG ("Delivered %s", delivery_token);
365 tp_chat_set_delivery_status (self, delivery_token,
366 EMPATHY_DELIVERY_STATUS_NONE);
367 goto out;
368 } else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED) {
369 goto out;
372 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
373 if (!valid)
374 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
376 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
378 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
379 * TpMessage. (fdo #35884) */
380 echo = tp_asv_get_boxed (header, "delivery-echo",
381 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
382 if (echo != NULL && echo->len >= 1) {
383 const GHashTable *echo_body;
385 echo_body = g_ptr_array_index (echo, 1);
386 if (echo_body != NULL)
387 message_body = tp_asv_get_string (echo_body, "content");
390 tp_chat_set_delivery_status (self, delivery_token,
391 EMPATHY_DELIVERY_STATUS_NONE);
392 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
393 delivery_error, delivery_dbus_error);
395 out:
396 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
397 message, NULL, NULL);
400 static void
401 handle_incoming_message (EmpathyTpChat *self,
402 TpMessage *message,
403 gboolean pending)
405 gchar *message_body;
407 if (tp_message_is_delivery_report (message)) {
408 handle_delivery_report (self, message);
409 return;
412 message_body = tp_message_to_text (message, NULL);
414 DEBUG ("Message %s (channel %s): %s",
415 pending ? "pending" : "received",
416 tp_proxy_get_object_path (self), message_body);
418 if (message_body == NULL) {
419 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
421 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
422 message, NULL, NULL);
423 return;
426 tp_chat_build_message (self, message, TRUE);
428 g_free (message_body);
431 static void
432 message_received_cb (TpTextChannel *channel,
433 TpMessage *message,
434 EmpathyTpChat *self)
436 handle_incoming_message (self, message, FALSE);
439 static gboolean
440 find_pending_message_func (gconstpointer a,
441 gconstpointer b)
443 EmpathyMessage *msg = (EmpathyMessage *) a;
444 TpMessage *message = (TpMessage *) b;
446 if (empathy_message_get_tp_message (msg) == message)
447 return 0;
449 return -1;
452 static void
453 pending_message_removed_cb (TpTextChannel *channel,
454 TpMessage *message,
455 EmpathyTpChat *self)
457 GList *m;
459 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
460 find_pending_message_func);
462 if (m == NULL)
463 return;
465 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
467 g_object_unref (m->data);
468 g_queue_delete_link (self->priv->pending_messages_queue, m);
471 static void
472 message_sent_cb (TpTextChannel *channel,
473 TpMessage *message,
474 TpMessageSendingFlags flags,
475 gchar *token,
476 EmpathyTpChat *self)
478 gchar *message_body;
480 message_body = tp_message_to_text (message, NULL);
482 DEBUG ("Message sent: %s", message_body);
484 tp_chat_build_message (self, message, FALSE);
486 g_free (message_body);
489 static TpChannelTextSendError
490 error_to_text_send_error (GError *error)
492 if (error->domain != TP_ERRORS)
493 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
495 switch (error->code) {
496 case TP_ERROR_OFFLINE:
497 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
498 case TP_ERROR_INVALID_HANDLE:
499 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
500 case TP_ERROR_PERMISSION_DENIED:
501 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
502 case TP_ERROR_NOT_IMPLEMENTED:
503 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
506 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
509 static void
510 message_send_cb (GObject *source,
511 GAsyncResult *result,
512 gpointer user_data)
514 EmpathyTpChat *self = user_data;
515 TpTextChannel *channel = (TpTextChannel *) source;
516 gchar *token = NULL;
517 GError *error = NULL;
519 if (!tp_text_channel_send_message_finish (channel, result, &token, &error)) {
520 DEBUG ("Error: %s", error->message);
522 /* FIXME: we should use the body of the message as first argument of the
523 * signal but can't easily get it as we just get a user_data pointer. Once
524 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
525 * to use the user_data pointer to pass the message and fix this. */
526 g_signal_emit (self, signals[SEND_ERROR], 0,
527 NULL, error_to_text_send_error (error), NULL);
529 g_error_free (error);
532 tp_chat_set_delivery_status (self, token,
533 EMPATHY_DELIVERY_STATUS_SENDING);
534 g_free (token);
537 typedef struct {
538 EmpathyTpChat *chat;
539 TpChannelChatState state;
540 } StateChangedData;
542 static void
543 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
544 EmpathyContact *contact,
545 const GError *error,
546 gpointer user_data,
547 GObject *chat)
549 TpChannelChatState state;
551 if (error) {
552 DEBUG ("Error: %s", error->message);
553 return;
556 state = GPOINTER_TO_UINT (user_data);
557 DEBUG ("Chat state changed for %s (%d): %d",
558 empathy_contact_get_alias (contact),
559 empathy_contact_get_handle (contact), state);
561 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
564 static void
565 tp_chat_state_changed_cb (TpChannel *channel,
566 TpHandle handle,
567 TpChannelChatState state,
568 EmpathyTpChat *self)
570 TpConnection *connection = tp_channel_borrow_connection (
571 (TpChannel *) self);
573 empathy_tp_contact_factory_get_from_handle (connection, handle,
574 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
575 NULL, G_OBJECT (self));
578 static void
579 list_pending_messages (EmpathyTpChat *self)
581 GList *messages, *l;
583 messages = tp_text_channel_get_pending_messages (
584 TP_TEXT_CHANNEL (self));
586 for (l = messages; l != NULL; l = g_list_next (l)) {
587 TpMessage *message = l->data;
589 handle_incoming_message (self, message, FALSE);
592 g_list_free (messages);
595 static void
596 tp_chat_property_flags_changed_cb (TpProxy *proxy,
597 const GPtrArray *properties,
598 gpointer user_data,
599 GObject *chat)
601 EmpathyTpChat *self = (EmpathyTpChat *) chat;
602 guint i, j;
604 if (!self->priv->had_properties_list || !properties) {
605 return;
608 for (i = 0; i < properties->len; i++) {
609 GValueArray *prop_struct;
610 EmpathyTpChatProperty *property;
611 guint id;
612 guint flags;
614 prop_struct = g_ptr_array_index (properties, i);
615 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
616 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
618 for (j = 0; j < self->priv->properties->len; j++) {
619 property = g_ptr_array_index (self->priv->properties, j);
620 if (property->id == id) {
621 property->flags = flags;
622 DEBUG ("property %s flags changed: %d",
623 property->name, property->flags);
624 break;
630 static void
631 tp_chat_properties_changed_cb (TpProxy *proxy,
632 const GPtrArray *properties,
633 gpointer user_data,
634 GObject *chat)
636 EmpathyTpChat *self = (EmpathyTpChat *) chat;
637 guint i, j;
639 if (!self->priv->had_properties_list || !properties) {
640 return;
643 for (i = 0; i < properties->len; i++) {
644 GValueArray *prop_struct;
645 EmpathyTpChatProperty *property;
646 guint id;
647 GValue *src_value;
649 prop_struct = g_ptr_array_index (properties, i);
650 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
651 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
653 for (j = 0; j < self->priv->properties->len; j++) {
654 property = g_ptr_array_index (self->priv->properties, j);
655 if (property->id == id) {
656 if (property->value) {
657 g_value_copy (src_value, property->value);
658 } else {
659 property->value = tp_g_value_slice_dup (src_value);
662 DEBUG ("property %s changed", property->name);
663 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
664 property->name, property->value);
665 break;
671 static void
672 tp_chat_get_properties_cb (TpProxy *proxy,
673 const GPtrArray *properties,
674 const GError *error,
675 gpointer user_data,
676 GObject *chat)
678 if (error) {
679 DEBUG ("Error getting properties: %s", error->message);
680 return;
683 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
686 static void
687 tp_chat_list_properties_cb (TpProxy *proxy,
688 const GPtrArray *properties,
689 const GError *error,
690 gpointer user_data,
691 GObject *chat)
693 EmpathyTpChat *self = (EmpathyTpChat *) chat;
694 GArray *ids;
695 guint i;
697 self->priv->had_properties_list = TRUE;
699 if (error) {
700 DEBUG ("Error listing properties: %s", error->message);
701 return;
704 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
705 self->priv->properties = g_ptr_array_sized_new (properties->len);
706 for (i = 0; i < properties->len; i++) {
707 GValueArray *prop_struct;
708 EmpathyTpChatProperty *property;
710 prop_struct = g_ptr_array_index (properties, i);
711 property = g_slice_new0 (EmpathyTpChatProperty);
712 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
713 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
714 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
716 DEBUG ("Adding property name=%s id=%d flags=%d",
717 property->name, property->id, property->flags);
718 g_ptr_array_add (self->priv->properties, property);
719 if (property->flags & TP_PROPERTY_FLAG_READ) {
720 g_array_append_val (ids, property->id);
724 tp_cli_properties_interface_call_get_properties (proxy, -1,
725 ids,
726 tp_chat_get_properties_cb,
727 NULL, NULL,
728 chat);
730 g_array_free (ids, TRUE);
733 void
734 empathy_tp_chat_set_property (EmpathyTpChat *self,
735 const gchar *name,
736 const GValue *value)
738 EmpathyTpChatProperty *property;
739 guint i;
741 if (!self->priv->had_properties_list) {
742 return;
745 for (i = 0; i < self->priv->properties->len; i++) {
746 property = g_ptr_array_index (self->priv->properties, i);
747 if (!tp_strdiff (property->name, name)) {
748 GPtrArray *properties;
749 GValueArray *prop;
750 GValue id = {0, };
751 GValue dest_value = {0, };
753 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
754 break;
757 g_value_init (&id, G_TYPE_UINT);
758 g_value_init (&dest_value, G_TYPE_VALUE);
759 g_value_set_uint (&id, property->id);
760 g_value_set_boxed (&dest_value, value);
762 prop = g_value_array_new (2);
763 g_value_array_append (prop, &id);
764 g_value_array_append (prop, &dest_value);
766 properties = g_ptr_array_sized_new (1);
767 g_ptr_array_add (properties, prop);
769 DEBUG ("Set property %s", name);
770 tp_cli_properties_interface_call_set_properties (self, -1,
771 properties,
772 (tp_cli_properties_interface_callback_for_set_properties)
773 tp_chat_async_cb,
774 "Seting property", NULL,
775 G_OBJECT (self));
777 g_ptr_array_free (properties, TRUE);
778 g_value_array_free (prop);
780 break;
785 EmpathyTpChatProperty *
786 empathy_tp_chat_get_property (EmpathyTpChat *self,
787 const gchar *name)
789 EmpathyTpChatProperty *property;
790 guint i;
792 if (!self->priv->had_properties_list) {
793 return NULL;
796 for (i = 0; i < self->priv->properties->len; i++) {
797 property = g_ptr_array_index (self->priv->properties, i);
798 if (!tp_strdiff (property->name, name)) {
799 return property;
803 return NULL;
806 GPtrArray *
807 empathy_tp_chat_get_properties (EmpathyTpChat *self)
809 return self->priv->properties;
812 static void
813 tp_chat_dispose (GObject *object)
815 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
817 tp_clear_object (&self->priv->account);
818 tp_clear_object (&self->priv->remote_contact);
819 tp_clear_object (&self->priv->user);
821 g_queue_foreach (self->priv->messages_queue, (GFunc) g_object_unref, NULL);
822 g_queue_clear (self->priv->messages_queue);
824 g_queue_foreach (self->priv->pending_messages_queue,
825 (GFunc) g_object_unref, NULL);
826 g_queue_clear (self->priv->pending_messages_queue);
828 tp_clear_object (&self->priv->ready_result);
830 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
831 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
834 static void
835 tp_chat_finalize (GObject *object)
837 EmpathyTpChat *self = (EmpathyTpChat *) object;
838 guint i;
840 DEBUG ("Finalize: %p", object);
842 if (self->priv->properties) {
843 for (i = 0; i < self->priv->properties->len; i++) {
844 EmpathyTpChatProperty *property;
846 property = g_ptr_array_index (self->priv->properties, i);
847 g_free (property->name);
848 if (property->value) {
849 tp_g_value_slice_free (property->value);
851 g_slice_free (EmpathyTpChatProperty, property);
853 g_ptr_array_free (self->priv->properties, TRUE);
856 g_queue_free (self->priv->messages_queue);
857 g_queue_free (self->priv->pending_messages_queue);
858 g_hash_table_destroy (self->priv->messages_being_sent);
860 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
863 static void
864 check_almost_ready (EmpathyTpChat *self)
866 if (self->priv->ready_result == NULL)
867 return;
869 if (self->priv->user == NULL)
870 return;
872 if (!self->priv->got_password_flags)
873 return;
875 if (!self->priv->got_sms_channel)
876 return;
878 /* We need either the members (room) or the remote contact (private chat).
879 * If the chat is protected by a password we can't get these information so
880 * consider the chat as ready so it can be presented to the user. */
881 if (!empathy_tp_chat_password_needed (self) && self->priv->members == NULL &&
882 self->priv->remote_contact == NULL)
883 return;
885 g_assert (tp_proxy_is_prepared (self,
886 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
888 tp_g_signal_connect_object (self, "message-received",
889 G_CALLBACK (message_received_cb), self, 0);
890 tp_g_signal_connect_object (self, "pending-message-removed",
891 G_CALLBACK (pending_message_removed_cb), self, 0);
893 list_pending_messages (self);
895 tp_g_signal_connect_object (self, "message-sent",
896 G_CALLBACK (message_sent_cb), self, 0);
898 tp_g_signal_connect_object (self, "chat-state-changed",
899 G_CALLBACK (tp_chat_state_changed_cb), self, 0);
901 check_ready (self);
904 static void
905 tp_chat_update_remote_contact (EmpathyTpChat *self)
907 TpChannel *channel = (TpChannel *) self;
908 EmpathyContact *contact = NULL;
909 TpHandle self_handle;
910 TpHandleType handle_type;
911 GList *l;
913 /* If this is a named chatroom, never pretend it is a private chat */
914 tp_channel_get_handle (channel, &handle_type);
915 if (handle_type == TP_HANDLE_TYPE_ROOM) {
916 return;
919 /* This is an MSN chat, but it's the new style where 1-1 chats don't
920 * have the group interface. If it has the conference interface, then
921 * it is indeed a MUC. */
922 if (tp_proxy_has_interface_by_id (self,
923 TP_IFACE_QUARK_CHANNEL_INTERFACE_CONFERENCE)) {
924 return;
927 /* This is an MSN-like chat where anyone can join the chat at anytime.
928 * If there is only one non-self contact member, we are in a private
929 * chat and we set the "remote-contact" property to that contact. If
930 * there are more, set the "remote-contact" property to NULL and the
931 * UI will display a contact list. */
932 self_handle = tp_channel_group_get_self_handle (channel);
933 for (l = self->priv->members; l; l = l->next) {
934 /* Skip self contact if member */
935 if (empathy_contact_get_handle (l->data) == self_handle) {
936 continue;
939 /* We have more than one remote contact, break */
940 if (contact != NULL) {
941 contact = NULL;
942 break;
945 /* If we didn't find yet a remote contact, keep this one */
946 contact = l->data;
949 if (self->priv->remote_contact == contact) {
950 return;
953 DEBUG ("Changing remote contact from %p to %p",
954 self->priv->remote_contact, contact);
956 if (self->priv->remote_contact) {
957 g_object_unref (self->priv->remote_contact);
960 self->priv->remote_contact = contact ? g_object_ref (contact) : NULL;
961 g_object_notify (G_OBJECT (self), "remote-contact");
964 static void
965 tp_chat_got_added_contacts_cb (TpConnection *connection,
966 guint n_contacts,
967 EmpathyContact * const * contacts,
968 guint n_failed,
969 const TpHandle *failed,
970 const GError *error,
971 gpointer user_data,
972 GObject *chat)
974 EmpathyTpChat *self = (EmpathyTpChat *) chat;
975 guint i;
976 const TpIntSet *members;
977 TpHandle handle;
978 EmpathyContact *contact;
980 if (error) {
981 DEBUG ("Error: %s", error->message);
982 return;
985 members = tp_channel_group_get_members ((TpChannel *) self);
986 for (i = 0; i < n_contacts; i++) {
987 contact = contacts[i];
988 handle = empathy_contact_get_handle (contact);
990 /* Make sure the contact is still member */
991 if (tp_intset_is_member (members, handle)) {
992 self->priv->members = g_list_prepend (self->priv->members,
993 g_object_ref (contact));
994 g_signal_emit_by_name (chat, "members-changed",
995 contact, NULL, 0, NULL, TRUE);
999 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
1000 check_almost_ready (EMPATHY_TP_CHAT (chat));
1003 static EmpathyContact *
1004 chat_lookup_contact (EmpathyTpChat *self,
1005 TpHandle handle,
1006 gboolean remove_)
1008 GList *l;
1010 for (l = self->priv->members; l; l = l->next) {
1011 EmpathyContact *c = l->data;
1013 if (empathy_contact_get_handle (c) != handle) {
1014 continue;
1017 if (remove_) {
1018 /* Caller takes the reference. */
1019 self->priv->members = g_list_delete_link (self->priv->members, l);
1020 } else {
1021 g_object_ref (c);
1024 return c;
1027 return NULL;
1030 typedef struct
1032 TpHandle old_handle;
1033 guint reason;
1034 gchar *message;
1035 } ContactRenameData;
1037 static ContactRenameData *
1038 contact_rename_data_new (TpHandle handle,
1039 guint reason,
1040 const gchar* message)
1042 ContactRenameData *data = g_new (ContactRenameData, 1);
1043 data->old_handle = handle;
1044 data->reason = reason;
1045 data->message = g_strdup (message);
1047 return data;
1050 static void
1051 contact_rename_data_free (ContactRenameData* data)
1053 g_free (data->message);
1054 g_free (data);
1057 static void
1058 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
1059 guint n_contacts,
1060 EmpathyContact * const * contacts,
1061 guint n_failed,
1062 const TpHandle *failed,
1063 const GError *error,
1064 gpointer user_data,
1065 GObject *chat)
1067 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1068 const TpIntSet *members;
1069 TpHandle handle;
1070 EmpathyContact *old = NULL, *new = NULL;
1071 ContactRenameData *rename_data = (ContactRenameData *) user_data;
1073 if (error) {
1074 DEBUG ("Error: %s", error->message);
1075 return;
1078 /* renamed members can only be delivered one at a time */
1079 g_warn_if_fail (n_contacts == 1);
1081 new = contacts[0];
1083 members = tp_channel_group_get_members ((TpChannel *) self);
1084 handle = empathy_contact_get_handle (new);
1086 old = chat_lookup_contact (self, rename_data->old_handle, TRUE);
1088 /* Make sure the contact is still member */
1089 if (tp_intset_is_member (members, handle)) {
1090 self->priv->members = g_list_prepend (self->priv->members,
1091 g_object_ref (new));
1093 if (old != NULL) {
1094 g_signal_emit_by_name (self, "member-renamed",
1095 old, new, rename_data->reason,
1096 rename_data->message);
1097 g_object_unref (old);
1101 if (self->priv->user == old) {
1102 /* We change our nick */
1103 tp_clear_object (&self->priv->user);
1104 self->priv->user = g_object_ref (new);
1107 tp_chat_update_remote_contact (self);
1108 check_almost_ready (self);
1112 static void
1113 tp_chat_group_members_changed_cb (TpChannel *channel,
1114 gchar *message,
1115 GArray *added,
1116 GArray *removed,
1117 GArray *local_pending,
1118 GArray *remote_pending,
1119 guint actor,
1120 guint reason,
1121 EmpathyTpChat *self)
1123 EmpathyContact *contact;
1124 EmpathyContact *actor_contact = NULL;
1125 guint i;
1126 ContactRenameData *rename_data;
1127 TpHandle old_handle;
1128 TpConnection *connection = tp_channel_borrow_connection (
1129 (TpChannel *) self);
1131 /* Contact renamed */
1132 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED) {
1133 /* there can only be a single 'added' and a single 'removed' handle */
1134 if (removed->len != 1 || added->len != 1) {
1135 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
1136 added->len, removed->len);
1137 return;
1140 old_handle = g_array_index (removed, guint, 0);
1142 rename_data = contact_rename_data_new (old_handle, reason, message);
1143 empathy_tp_contact_factory_get_from_handles (connection,
1144 added->len, (TpHandle *) added->data,
1145 tp_chat_got_renamed_contacts_cb,
1146 rename_data, (GDestroyNotify) contact_rename_data_free,
1147 G_OBJECT (self));
1148 return;
1151 if (actor != 0) {
1152 actor_contact = chat_lookup_contact (self, actor, FALSE);
1153 if (actor_contact == NULL) {
1154 /* FIXME: handle this a tad more gracefully: perhaps
1155 * the actor was a server op. We could use the
1156 * contact-ids detail of MembersChangedDetailed.
1158 DEBUG ("actor %u not a channel member", actor);
1162 /* Remove contacts that are not members anymore */
1163 for (i = 0; i < removed->len; i++) {
1164 contact = chat_lookup_contact (self,
1165 g_array_index (removed, TpHandle, i), TRUE);
1167 if (contact != NULL) {
1168 g_signal_emit_by_name (self, "members-changed", contact,
1169 actor_contact, reason, message,
1170 FALSE);
1171 g_object_unref (contact);
1175 /* Request added contacts */
1176 if (added->len > 0) {
1177 empathy_tp_contact_factory_get_from_handles (connection,
1178 added->len, (TpHandle *) added->data,
1179 tp_chat_got_added_contacts_cb, NULL, NULL,
1180 G_OBJECT (self));
1183 tp_chat_update_remote_contact (self);
1185 if (actor_contact != NULL) {
1186 g_object_unref (actor_contact);
1190 static void
1191 tp_chat_got_remote_contact_cb (TpConnection *connection,
1192 EmpathyContact *contact,
1193 const GError *error,
1194 gpointer user_data,
1195 GObject *chat)
1197 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1199 if (error) {
1200 DEBUG ("Error: %s", error->message);
1201 empathy_tp_chat_leave (self, "");
1202 return;
1205 self->priv->remote_contact = g_object_ref (contact);
1206 g_object_notify (chat, "remote-contact");
1208 check_almost_ready (self);
1211 static void
1212 tp_chat_got_self_contact_cb (TpConnection *connection,
1213 EmpathyContact *contact,
1214 const GError *error,
1215 gpointer user_data,
1216 GObject *chat)
1218 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1220 if (error) {
1221 DEBUG ("Error: %s", error->message);
1222 empathy_tp_chat_leave (self, "");
1223 return;
1226 self->priv->user = g_object_ref (contact);
1227 empathy_contact_set_is_user (self->priv->user, TRUE);
1228 check_almost_ready (self);
1231 static void
1232 password_flags_changed_cb (TpChannel *channel,
1233 guint added,
1234 guint removed,
1235 gpointer user_data,
1236 GObject *weak_object)
1238 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1239 gboolean was_needed, needed;
1241 was_needed = empathy_tp_chat_password_needed (self);
1243 self->priv->password_flags |= added;
1244 self->priv->password_flags ^= removed;
1246 needed = empathy_tp_chat_password_needed (self);
1248 if (was_needed != needed)
1249 g_object_notify (G_OBJECT (self), "password-needed");
1252 static void
1253 got_password_flags_cb (TpChannel *proxy,
1254 guint password_flags,
1255 const GError *error,
1256 gpointer user_data,
1257 GObject *weak_object)
1259 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1261 self->priv->got_password_flags = TRUE;
1262 self->priv->password_flags = password_flags;
1264 check_almost_ready (EMPATHY_TP_CHAT (self));
1267 static void
1268 sms_channel_changed_cb (TpChannel *channel,
1269 gboolean sms_channel,
1270 gpointer user_data,
1271 GObject *chat)
1273 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1275 self->priv->sms_channel = sms_channel;
1277 g_object_notify (G_OBJECT (chat), "sms-channel");
1280 static void
1281 get_sms_channel_cb (TpProxy *channel,
1282 const GValue *value,
1283 const GError *in_error,
1284 gpointer user_data,
1285 GObject *chat)
1287 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1289 if (in_error != NULL) {
1290 DEBUG ("Failed to get SMSChannel: %s", in_error->message);
1291 return;
1294 g_return_if_fail (G_VALUE_HOLDS_BOOLEAN (value));
1296 self->priv->sms_channel = g_value_get_boolean (value);
1297 self->priv->got_sms_channel = TRUE;
1299 check_almost_ready (EMPATHY_TP_CHAT (chat));
1302 static void
1303 tp_chat_get_property (GObject *object,
1304 guint param_id,
1305 GValue *value,
1306 GParamSpec *pspec)
1308 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1310 switch (param_id) {
1311 case PROP_ACCOUNT:
1312 g_value_set_object (value, self->priv->account);
1313 break;
1314 case PROP_REMOTE_CONTACT:
1315 g_value_set_object (value, self->priv->remote_contact);
1316 break;
1317 case PROP_PASSWORD_NEEDED:
1318 g_value_set_boolean (value, empathy_tp_chat_password_needed (self));
1319 break;
1320 case PROP_SMS_CHANNEL:
1321 g_value_set_boolean (value, self->priv->sms_channel);
1322 break;
1323 case PROP_N_MESSAGES_SENDING:
1324 g_value_set_uint (value,
1325 g_hash_table_size (self->priv->messages_being_sent));
1326 break;
1327 default:
1328 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1329 break;
1333 static void
1334 tp_chat_set_property (GObject *object,
1335 guint param_id,
1336 const GValue *value,
1337 GParamSpec *pspec)
1339 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1341 switch (param_id) {
1342 case PROP_ACCOUNT:
1343 self->priv->account = g_value_dup_object (value);
1344 break;
1345 default:
1346 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1347 break;
1351 enum {
1352 FEAT_READY,
1353 N_FEAT
1356 static const TpProxyFeature *
1357 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
1359 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
1360 static GQuark need[2] = {0, 0};
1362 if (G_LIKELY (features[0].name != 0))
1363 return features;
1365 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
1366 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
1367 features[FEAT_READY].depends_on = need;
1368 features[FEAT_READY].prepare_async =
1369 tp_chat_prepare_ready_async;
1371 /* assert that the terminator at the end is there */
1372 g_assert (features[N_FEAT].name == 0);
1374 return features;
1377 static void
1378 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1380 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1381 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
1383 object_class->dispose = tp_chat_dispose;
1384 object_class->finalize = tp_chat_finalize;
1385 object_class->get_property = tp_chat_get_property;
1386 object_class->set_property = tp_chat_set_property;
1388 proxy_class->list_features = tp_chat_list_features;
1390 g_object_class_install_property (object_class,
1391 PROP_ACCOUNT,
1392 g_param_spec_object ("account",
1393 "TpAccount",
1394 "the account associated with the chat",
1395 TP_TYPE_ACCOUNT,
1396 G_PARAM_READWRITE |
1397 G_PARAM_CONSTRUCT_ONLY |
1398 G_PARAM_STATIC_STRINGS));
1400 g_object_class_install_property (object_class,
1401 PROP_REMOTE_CONTACT,
1402 g_param_spec_object ("remote-contact",
1403 "The remote contact",
1404 "The remote contact if there is no group iface on the channel",
1405 EMPATHY_TYPE_CONTACT,
1406 G_PARAM_READABLE));
1408 g_object_class_install_property (object_class,
1409 PROP_PASSWORD_NEEDED,
1410 g_param_spec_boolean ("password-needed",
1411 "password needed",
1412 "TRUE if a password is needed to join the channel",
1413 FALSE,
1414 G_PARAM_READABLE));
1416 g_object_class_install_property (object_class,
1417 PROP_SMS_CHANNEL,
1418 g_param_spec_boolean ("sms-channel",
1419 "SMS Channel",
1420 "TRUE if channel is for sending SMSes",
1421 FALSE,
1422 G_PARAM_READABLE));
1424 g_object_class_install_property (object_class,
1425 PROP_N_MESSAGES_SENDING,
1426 g_param_spec_uint ("n-messages-sending",
1427 "Num Messages Sending",
1428 "The number of messages being sent",
1429 0, G_MAXUINT, 0,
1430 G_PARAM_READABLE));
1432 /* Signals */
1433 signals[MESSAGE_RECEIVED] =
1434 g_signal_new ("message-received-empathy",
1435 G_TYPE_FROM_CLASS (klass),
1436 G_SIGNAL_RUN_LAST,
1438 NULL, NULL,
1439 g_cclosure_marshal_VOID__OBJECT,
1440 G_TYPE_NONE,
1441 1, EMPATHY_TYPE_MESSAGE);
1443 signals[SEND_ERROR] =
1444 g_signal_new ("send-error",
1445 G_TYPE_FROM_CLASS (klass),
1446 G_SIGNAL_RUN_LAST,
1448 NULL, NULL,
1449 _empathy_marshal_VOID__STRING_UINT_STRING,
1450 G_TYPE_NONE,
1451 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1453 signals[CHAT_STATE_CHANGED] =
1454 g_signal_new ("chat-state-changed-empathy",
1455 G_TYPE_FROM_CLASS (klass),
1456 G_SIGNAL_RUN_LAST,
1458 NULL, NULL,
1459 _empathy_marshal_VOID__OBJECT_UINT,
1460 G_TYPE_NONE,
1461 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1463 signals[PROPERTY_CHANGED] =
1464 g_signal_new ("property-changed",
1465 G_TYPE_FROM_CLASS (klass),
1466 G_SIGNAL_RUN_LAST,
1468 NULL, NULL,
1469 _empathy_marshal_VOID__STRING_BOXED,
1470 G_TYPE_NONE,
1471 2, G_TYPE_STRING, G_TYPE_VALUE);
1473 signals[MESSAGE_ACKNOWLEDGED] =
1474 g_signal_new ("message-acknowledged",
1475 G_TYPE_FROM_CLASS (klass),
1476 G_SIGNAL_RUN_LAST,
1478 NULL, NULL,
1479 g_cclosure_marshal_VOID__OBJECT,
1480 G_TYPE_NONE,
1481 1, EMPATHY_TYPE_MESSAGE);
1483 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1486 static void
1487 empathy_tp_chat_init (EmpathyTpChat *self)
1489 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1490 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPrivate);
1492 self->priv->messages_queue = g_queue_new ();
1493 self->priv->pending_messages_queue = g_queue_new ();
1494 self->priv->messages_being_sent = g_hash_table_new_full (
1495 g_str_hash, g_str_equal, g_free, NULL);
1498 static void
1499 tp_chat_iface_init (EmpathyContactListIface *iface)
1501 iface->add = tp_chat_add;
1502 iface->remove = tp_chat_remove;
1503 iface->get_members = tp_chat_get_members;
1506 EmpathyTpChat *
1507 empathy_tp_chat_new (TpAccount *account,
1508 TpConnection *conn,
1509 const gchar *object_path,
1510 const GHashTable *immutable_properties)
1512 TpProxy *conn_proxy = (TpProxy *) conn;
1514 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1515 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1516 g_return_val_if_fail (immutable_properties != NULL, NULL);
1518 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1519 "account", account,
1520 "connection", conn,
1521 "dbus-daemon", conn_proxy->dbus_daemon,
1522 "bus-name", conn_proxy->bus_name,
1523 "object-path", object_path,
1524 "channel-properties", immutable_properties,
1525 NULL);
1528 const gchar *
1529 empathy_tp_chat_get_id (EmpathyTpChat *self)
1531 const gchar *id;
1533 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1535 id = tp_channel_get_identifier ((TpChannel *) self);
1536 if (!EMP_STR_EMPTY (id))
1537 return id;
1538 else if (self->priv->remote_contact)
1539 return empathy_contact_get_id (self->priv->remote_contact);
1540 else
1541 return NULL;
1545 EmpathyContact *
1546 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1548 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1550 return self->priv->remote_contact;
1553 TpAccount *
1554 empathy_tp_chat_get_account (EmpathyTpChat *self)
1556 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1558 return self->priv->account;
1561 void
1562 empathy_tp_chat_send (EmpathyTpChat *self,
1563 TpMessage *message)
1565 gchar *message_body;
1567 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1568 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1570 message_body = tp_message_to_text (message, NULL);
1572 DEBUG ("Sending message: %s", message_body);
1574 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1575 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1576 message_send_cb, self);
1578 g_free (message_body);
1581 const GList *
1582 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1584 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1586 return self->priv->pending_messages_queue->head;
1589 void
1590 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1591 EmpathyMessage *message) {
1592 TpMessage *tp_msg;
1594 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1596 if (!empathy_message_is_incoming (message))
1597 return;
1599 tp_msg = empathy_message_get_tp_message (message);
1600 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1601 tp_msg, NULL, NULL);
1604 void
1605 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *self,
1606 const GSList *messages) {
1607 const GSList *l;
1608 GList *messages_to_ack = NULL;
1610 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1612 if (messages == NULL)
1613 return;
1615 for (l = messages; l != NULL; l = g_slist_next (l)) {
1616 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1618 if (empathy_message_is_incoming (message)) {
1619 TpMessage *tp_msg = empathy_message_get_tp_message (message);
1620 messages_to_ack = g_list_append (messages_to_ack, tp_msg);
1624 if (messages_to_ack != NULL) {
1625 tp_text_channel_ack_messages_async (TP_TEXT_CHANNEL (self),
1626 messages_to_ack, NULL, NULL);
1629 g_list_free (messages_to_ack);
1632 void
1633 empathy_tp_chat_acknowledge_all_messages (EmpathyTpChat *self)
1635 empathy_tp_chat_acknowledge_messages (self,
1636 (GSList *) empathy_tp_chat_get_pending_messages (self));
1639 gboolean
1640 empathy_tp_chat_password_needed (EmpathyTpChat *self)
1642 return self->priv->password_flags & TP_CHANNEL_PASSWORD_FLAG_PROVIDE;
1645 static void
1646 provide_password_cb (TpChannel *channel,
1647 gboolean correct,
1648 const GError *error,
1649 gpointer user_data,
1650 GObject *weak_object)
1652 GSimpleAsyncResult *result = user_data;
1654 if (error != NULL) {
1655 g_simple_async_result_set_from_error (result, error);
1657 else if (!correct) {
1658 /* The current D-Bus API is a bit weird so re-use the
1659 * AuthenticationFailed error */
1660 g_simple_async_result_set_error (result, TP_ERRORS,
1661 TP_ERROR_AUTHENTICATION_FAILED, "Wrong password");
1664 g_simple_async_result_complete (result);
1665 g_object_unref (result);
1668 void
1669 empathy_tp_chat_provide_password_async (EmpathyTpChat *self,
1670 const gchar *password,
1671 GAsyncReadyCallback callback,
1672 gpointer user_data)
1674 GSimpleAsyncResult *result;
1676 result = g_simple_async_result_new (G_OBJECT (self),
1677 callback, user_data,
1678 empathy_tp_chat_provide_password_finish);
1680 tp_cli_channel_interface_password_call_provide_password
1681 ((TpChannel *) self, -1, password, provide_password_cb, result,
1682 NULL, G_OBJECT (self));
1685 gboolean
1686 empathy_tp_chat_provide_password_finish (EmpathyTpChat *self,
1687 GAsyncResult *result,
1688 GError **error)
1690 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1691 error))
1692 return FALSE;
1694 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1695 G_OBJECT (self), empathy_tp_chat_provide_password_finish), FALSE);
1697 return TRUE;
1701 * empathy_tp_chat_can_add_contact:
1703 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1704 * That is if this chat is a 1-to-1 channel that can be upgraded to
1705 * a MUC using the Conference interface or if the channel is a MUC.
1707 gboolean
1708 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1710 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1712 return self->priv->can_upgrade_to_muc ||
1713 tp_proxy_has_interface_by_id (self,
1714 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1717 static void
1718 tp_channel_leave_async_cb (GObject *source_object,
1719 GAsyncResult *res,
1720 gpointer user_data)
1722 GError *error = NULL;
1724 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error)) {
1725 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1726 error->message);
1727 g_error_free (error);
1731 void
1732 empathy_tp_chat_leave (EmpathyTpChat *self,
1733 const gchar *message)
1735 TpChannel *channel = (TpChannel *) self;
1737 DEBUG ("Leaving channel %s with message \"%s\"",
1738 tp_channel_get_identifier (channel), message);
1740 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1741 message, tp_channel_leave_async_cb, self);
1744 static void
1745 add_members_cb (TpChannel *proxy,
1746 const GError *error,
1747 gpointer user_data,
1748 GObject *weak_object)
1750 EmpathyTpChat *self = (EmpathyTpChat *) weak_object;
1752 if (error != NULL) {
1753 DEBUG ("Failed to join chat (%s): %s",
1754 tp_channel_get_identifier ((TpChannel *) self), error->message);
1758 void
1759 empathy_tp_chat_join (EmpathyTpChat *self)
1761 TpHandle self_handle;
1762 GArray *members;
1764 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1766 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1767 g_array_append_val (members, self_handle);
1769 tp_cli_channel_interface_group_call_add_members ((TpChannel *) self, -1, members,
1770 "", add_members_cb, NULL, NULL, G_OBJECT (self));
1772 g_array_free (members, TRUE);
1775 gboolean
1776 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1777 TpHandle *inviter)
1779 TpHandle self_handle;
1781 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1782 return FALSE;
1784 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1785 if (self_handle == 0)
1786 return FALSE;
1788 return tp_channel_group_get_local_pending_info ((TpChannel *) self, self_handle,
1789 inviter, NULL, NULL);
1792 TpChannelChatState
1793 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1794 EmpathyContact *contact)
1796 return tp_channel_get_chat_state ((TpChannel *) self,
1797 empathy_contact_get_handle (contact));
1800 EmpathyContact *
1801 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1803 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1805 return self->priv->user;
1809 gboolean
1810 empathy_tp_chat_is_sms_channel (EmpathyTpChat *self)
1812 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1814 return self->priv->sms_channel;
1817 GQuark
1818 empathy_tp_chat_get_feature_ready (void)
1820 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1823 static void
1824 conn_prepared_cb (GObject *source,
1825 GAsyncResult *result,
1826 gpointer user_data)
1828 EmpathyTpChat *self = user_data;
1829 TpChannel *channel = (TpChannel *) self;
1830 GError *error = NULL;
1831 TpHandle handle;
1832 TpConnection *connection = (TpConnection *) source;
1834 if (!tp_proxy_prepare_finish (source, result, &error)) {
1835 g_simple_async_result_set_from_error (self->priv->ready_result, error);
1836 g_error_free (error);
1837 g_simple_async_result_complete (self->priv->ready_result);
1838 tp_clear_object (&self->priv->ready_result);
1839 return;
1842 if (tp_proxy_has_interface_by_id (self,
1843 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1844 const TpIntSet *members;
1845 GArray *handles;
1847 /* Get self contact from the group's self handle */
1848 handle = tp_channel_group_get_self_handle (channel);
1849 empathy_tp_contact_factory_get_from_handle (connection,
1850 handle, tp_chat_got_self_contact_cb,
1851 NULL, NULL, G_OBJECT (self));
1853 /* Get initial member contacts */
1854 members = tp_channel_group_get_members (channel);
1855 handles = tp_intset_to_array (members);
1856 empathy_tp_contact_factory_get_from_handles (connection,
1857 handles->len, (TpHandle *) handles->data,
1858 tp_chat_got_added_contacts_cb, NULL, NULL, G_OBJECT (self));
1860 self->priv->can_upgrade_to_muc = FALSE;
1862 tp_g_signal_connect_object (self, "group-members-changed",
1863 G_CALLBACK (tp_chat_group_members_changed_cb), self, 0);
1864 } else {
1865 TpCapabilities *caps;
1866 GPtrArray *classes;
1867 guint i;
1869 /* Get the self contact from the connection's self handle */
1870 handle = tp_connection_get_self_handle (connection);
1871 empathy_tp_contact_factory_get_from_handle (connection,
1872 handle, tp_chat_got_self_contact_cb,
1873 NULL, NULL, G_OBJECT (self));
1875 /* Get the remote contact */
1876 handle = tp_channel_get_handle (channel, NULL);
1877 empathy_tp_contact_factory_get_from_handle (connection,
1878 handle, tp_chat_got_remote_contact_cb,
1879 NULL, NULL, G_OBJECT (self));
1881 caps = tp_connection_get_capabilities (connection);
1882 g_assert (caps != NULL);
1884 classes = tp_capabilities_get_channel_classes (caps);
1886 for (i = 0; i < classes->len; i++) {
1887 GValueArray *array = g_ptr_array_index (classes, i);
1888 const char **oprops = g_value_get_boxed (
1889 g_value_array_get_nth (array, 1));
1891 if (tp_strv_contains (oprops, TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS)) {
1892 self->priv->can_upgrade_to_muc = TRUE;
1893 break;
1898 if (tp_proxy_has_interface_by_id (self,
1899 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1900 tp_cli_properties_interface_call_list_properties (channel, -1,
1901 tp_chat_list_properties_cb,
1902 NULL, NULL,
1903 G_OBJECT (self));
1904 tp_cli_properties_interface_connect_to_properties_changed (channel,
1905 tp_chat_properties_changed_cb,
1906 NULL, NULL,
1907 G_OBJECT (self), NULL);
1908 tp_cli_properties_interface_connect_to_property_flags_changed (channel,
1909 tp_chat_property_flags_changed_cb,
1910 NULL, NULL,
1911 G_OBJECT (self), NULL);
1914 /* Check if the chat is password protected */
1915 if (tp_proxy_has_interface_by_id (self,
1916 TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD)) {
1917 self->priv->got_password_flags = FALSE;
1919 tp_cli_channel_interface_password_connect_to_password_flags_changed
1920 (channel, password_flags_changed_cb, self, NULL,
1921 G_OBJECT (self), NULL);
1923 tp_cli_channel_interface_password_call_get_password_flags
1924 (channel, -1, got_password_flags_cb, self, NULL, G_OBJECT (self));
1925 } else {
1926 /* No Password interface, so no need to fetch the password flags */
1927 self->priv->got_password_flags = TRUE;
1930 /* Check if the chat is for SMS */
1931 if (tp_proxy_has_interface_by_id (channel,
1932 TP_IFACE_QUARK_CHANNEL_INTERFACE_SMS)) {
1933 tp_cli_channel_interface_sms_connect_to_sms_channel_changed (
1934 channel,
1935 sms_channel_changed_cb, self, NULL, G_OBJECT (self), NULL);
1937 tp_cli_dbus_properties_call_get (channel, -1,
1938 TP_IFACE_CHANNEL_INTERFACE_SMS, "SMSChannel",
1939 get_sms_channel_cb, self, NULL, G_OBJECT (self));
1940 } else {
1941 /* if there's no SMS support, then we're not waiting for it */
1942 self->priv->got_sms_channel = TRUE;
1946 static void
1947 tp_chat_prepare_ready_async (TpProxy *proxy,
1948 const TpProxyFeature *feature,
1949 GAsyncReadyCallback callback,
1950 gpointer user_data)
1952 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1953 TpChannel *channel = (TpChannel *) proxy;
1954 TpConnection *connection;
1955 GQuark conn_features[] = { TP_CONNECTION_FEATURE_CAPABILITIES, 0 };
1957 g_assert (self->priv->ready_result == NULL);
1958 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1959 callback, user_data, tp_chat_prepare_ready_async);
1961 connection = tp_channel_borrow_connection (channel);
1963 tp_proxy_prepare_async (connection, conn_features,
1964 conn_prepared_cb, self);