diff options
Diffstat (limited to 'libempathy/empathy-tp-chat.c')
-rw-r--r-- | libempathy/empathy-tp-chat.c | 174 |
1 files changed, 167 insertions, 7 deletions
diff --git a/libempathy/empathy-tp-chat.c b/libempathy/empathy-tp-chat.c index a56c08a32..72c6bebf0 100644 --- a/libempathy/empathy-tp-chat.c +++ b/libempathy/empathy-tp-chat.c @@ -59,6 +59,10 @@ typedef struct { gboolean got_password_flags; gboolean ready; gboolean can_upgrade_to_muc; + gboolean got_sms_channel; + gboolean sms_channel; + + GHashTable *messages_being_sent; } EmpathyTpChatPriv; static void tp_chat_iface_init (EmpathyContactListIface *iface); @@ -70,6 +74,8 @@ enum { PROP_REMOTE_CONTACT, PROP_PASSWORD_NEEDED, PROP_READY, + PROP_SMS_CHANNEL, + PROP_N_MESSAGES_SENDING, }; enum { @@ -90,6 +96,40 @@ G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT, static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids); static void +tp_chat_set_delivery_status (EmpathyTpChat *self, + const gchar *token, + EmpathyDeliveryStatus delivery_status) +{ + EmpathyTpChatPriv *priv = GET_PRIV (self); + TpDeliveryReportingSupportFlags flags = + tp_text_channel_get_delivery_reporting_support ( + TP_TEXT_CHANNEL (priv->channel)); + + /* channel must support receiving failures and successes */ + if (!tp_str_empty (token) && + flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES && + flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES) { + + DEBUG ("Delivery status (%s) = %u", token, delivery_status); + + switch (delivery_status) { + case EMPATHY_DELIVERY_STATUS_NONE: + g_hash_table_remove (priv->messages_being_sent, + token); + break; + + default: + g_hash_table_insert (priv->messages_being_sent, + g_strdup (token), + GUINT_TO_POINTER (delivery_status)); + break; + } + + g_object_notify (G_OBJECT (self), "n-messages-sending"); + } +} + +static void tp_chat_invalidated_cb (TpProxy *proxy, guint domain, gint code, @@ -319,19 +359,38 @@ handle_delivery_report (EmpathyTpChat *self, gboolean valid; GPtrArray *echo; const gchar *message_body = NULL; + const gchar *delivery_dbus_error; + const gchar *delivery_token = NULL; header = tp_message_peek (message, 0); if (header == NULL) goto out; + delivery_token = tp_asv_get_string (header, "delivery-token"); delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid); - if (!valid || delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED) + + if (!valid) { + goto out; + } else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED) { + DEBUG ("Accepted %s", delivery_token); + tp_chat_set_delivery_status (self, delivery_token, + EMPATHY_DELIVERY_STATUS_ACCEPTED); goto out; + } else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED) { + DEBUG ("Delivered %s", delivery_token); + tp_chat_set_delivery_status (self, delivery_token, + EMPATHY_DELIVERY_STATUS_NONE); + goto out; + } else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED) { + goto out; + } delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid); if (!valid) delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN; + delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error"); + /* TODO: ideally we should use tp-glib API giving us the echoed message as a * TpMessage. (fdo #35884) */ echo = tp_asv_get_boxed (header, "delivery-echo", @@ -344,7 +403,10 @@ handle_delivery_report (EmpathyTpChat *self, message_body = tp_asv_get_string (echo_body, "content"); } - g_signal_emit (self, signals[SEND_ERROR], 0, message_body, delivery_error); + tp_chat_set_delivery_status (self, delivery_token, + EMPATHY_DELIVERY_STATUS_NONE); + g_signal_emit (self, signals[SEND_ERROR], 0, message_body, + delivery_error, delivery_dbus_error); out: tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (priv->channel), @@ -436,9 +498,10 @@ message_send_cb (GObject *source, { EmpathyTpChat *chat = user_data; TpTextChannel *channel = (TpTextChannel *) source; + gchar *token = NULL; GError *error = NULL; - if (!tp_text_channel_send_message_finish (channel, result, NULL, &error)) { + if (!tp_text_channel_send_message_finish (channel, result, &token, &error)) { DEBUG ("Error: %s", error->message); /* FIXME: we should use the body of the message as first argument of the @@ -446,10 +509,14 @@ message_send_cb (GObject *source, * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able * to use the user_data pointer to pass the message and fix this. */ g_signal_emit (chat, signals[SEND_ERROR], 0, - NULL, error_to_text_send_error (error)); + NULL, error_to_text_send_error (error), NULL); g_error_free (error); } + + tp_chat_set_delivery_status (chat, token, + EMPATHY_DELIVERY_STATUS_SENDING); + g_free (token); } typedef struct { @@ -809,6 +876,7 @@ tp_chat_finalize (GObject *object) g_queue_free (priv->messages_queue); g_queue_free (priv->pending_messages_queue); + g_hash_table_destroy (priv->messages_being_sent); G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object); } @@ -827,6 +895,9 @@ check_almost_ready (EmpathyTpChat *chat) if (!priv->got_password_flags) return; + if (!priv->got_sms_channel) + return; + /* We need either the members (room) or the remote contact (private chat). * If the chat is protected by a password we can't get these information so * consider the chat as ready so it can be presented to the user. */ @@ -1218,6 +1289,41 @@ got_password_flags_cb (TpChannel *proxy, check_almost_ready (EMPATHY_TP_CHAT (self)); } +static void +sms_channel_changed_cb (TpChannel *channel, + gboolean sms_channel, + gpointer user_data, + GObject *chat) +{ + EmpathyTpChatPriv *priv = GET_PRIV (chat); + + priv->sms_channel = sms_channel; + + g_object_notify (G_OBJECT (chat), "sms-channel"); +} + +static void +get_sms_channel_cb (TpProxy *channel, + const GValue *value, + const GError *in_error, + gpointer user_data, + GObject *chat) +{ + EmpathyTpChatPriv *priv = GET_PRIV (chat); + + if (in_error != NULL) { + DEBUG ("Failed to get SMSChannel: %s", in_error->message); + return; + } + + g_return_if_fail (G_VALUE_HOLDS_BOOLEAN (value)); + + priv->sms_channel = g_value_get_boolean (value); + priv->got_sms_channel = TRUE; + + check_almost_ready (EMPATHY_TP_CHAT (chat)); +} + static GObject * tp_chat_constructor (GType type, guint n_props, @@ -1327,6 +1433,22 @@ tp_chat_constructor (GType type, priv->got_password_flags = TRUE; } + /* Check if the chat is for SMS */ + if (tp_proxy_has_interface_by_id (priv->channel, + TP_IFACE_QUARK_CHANNEL_INTERFACE_SMS)) { + tp_cli_channel_interface_sms_connect_to_sms_channel_changed ( + priv->channel, + sms_channel_changed_cb, chat, NULL, G_OBJECT (chat), + NULL); + + tp_cli_dbus_properties_call_get (priv->channel, -1, + TP_IFACE_CHANNEL_INTERFACE_SMS, "SMSChannel", + get_sms_channel_cb, chat, NULL, G_OBJECT (chat)); + } else { + /* if there's no SMS support, then we're not waiting for it */ + priv->got_sms_channel = TRUE; + } + return chat; } @@ -1355,6 +1477,13 @@ tp_chat_get_property (GObject *object, case PROP_PASSWORD_NEEDED: g_value_set_boolean (value, empathy_tp_chat_password_needed (self)); break; + case PROP_SMS_CHANNEL: + g_value_set_boolean (value, priv->sms_channel); + break; + case PROP_N_MESSAGES_SENDING: + g_value_set_uint (value, + g_hash_table_size (priv->messages_being_sent)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); break; @@ -1436,6 +1565,22 @@ empathy_tp_chat_class_init (EmpathyTpChatClass *klass) FALSE, G_PARAM_READABLE)); + g_object_class_install_property (object_class, + PROP_SMS_CHANNEL, + g_param_spec_boolean ("sms-channel", + "SMS Channel", + "TRUE if channel is for sending SMSes", + FALSE, + G_PARAM_READABLE)); + + g_object_class_install_property (object_class, + PROP_N_MESSAGES_SENDING, + g_param_spec_uint ("n-messages-sending", + "Num Messages Sending", + "The number of messages being sent", + 0, G_MAXUINT, 0, + G_PARAM_READABLE)); + /* Signals */ signals[MESSAGE_RECEIVED] = g_signal_new ("message-received", @@ -1453,9 +1598,9 @@ empathy_tp_chat_class_init (EmpathyTpChatClass *klass) G_SIGNAL_RUN_LAST, 0, NULL, NULL, - _empathy_marshal_VOID__STRING_UINT, + _empathy_marshal_VOID__STRING_UINT_STRING, G_TYPE_NONE, - 2, G_TYPE_STRING, G_TYPE_UINT); + 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING); signals[CHAT_STATE_CHANGED] = g_signal_new ("chat-state-changed", @@ -1499,6 +1644,8 @@ empathy_tp_chat_init (EmpathyTpChat *chat) chat->priv = priv; priv->messages_queue = g_queue_new (); priv->pending_messages_queue = g_queue_new (); + priv->messages_being_sent = g_hash_table_new_full ( + g_str_hash, g_str_equal, g_free, NULL); } static void @@ -1607,7 +1754,8 @@ empathy_tp_chat_send (EmpathyTpChat *chat, DEBUG ("Sending message: %s", message_body); tp_text_channel_send_message_async (TP_TEXT_CHANNEL (priv->channel), - message, 0, message_send_cb, chat); + message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY, + message_send_cb, chat); g_free (message_body); } @@ -1909,3 +2057,15 @@ empathy_tp_chat_get_self_contact (EmpathyTpChat *self) return priv->user; } + +gboolean +empathy_tp_chat_is_sms_channel (EmpathyTpChat *self) +{ + EmpathyTpChatPriv *priv; + + g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE); + + priv = GET_PRIV (self); + + return priv->sms_channel; +} |