+static void
+empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->dispose = tp_chat_dispose;
+ object_class->finalize = tp_chat_finalize;
+ object_class->constructor = tp_chat_constructor;
+ object_class->get_property = tp_chat_get_property;
+ object_class->set_property = tp_chat_set_property;
+
+ g_object_class_install_property (object_class,
+ PROP_CHANNEL,
+ g_param_spec_object ("channel",
+ "telepathy channel",
+ "The text channel for the chat",
+ TP_TYPE_CHANNEL,
+ G_PARAM_READWRITE |
+ G_PARAM_CONSTRUCT_ONLY));
+
+ g_object_class_install_property (object_class,
+ PROP_REMOTE_CONTACT,
+ g_param_spec_object ("remote-contact",
+ "The remote contact",
+ "The remote contact if there is no group iface on the channel",
+ EMPATHY_TYPE_CONTACT,
+ G_PARAM_READABLE));
+
+ g_object_class_install_property (object_class,
+ PROP_READY,
+ g_param_spec_boolean ("ready",
+ "Is the object ready",
+ "This object can't be used until this becomes true",
+ FALSE,
+ G_PARAM_READABLE));
+
+ /* Signals */
+ signals[MESSAGE_RECEIVED] =
+ g_signal_new ("message-received",
+ G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST,
+ 0,
+ NULL, NULL,
+ g_cclosure_marshal_VOID__OBJECT,
+ G_TYPE_NONE,
+ 1, EMPATHY_TYPE_MESSAGE);
+
+ signals[SEND_ERROR] =
+ g_signal_new ("send-error",
+ G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST,
+ 0,
+ NULL, NULL,
+ _empathy_marshal_VOID__OBJECT_UINT,
+ G_TYPE_NONE,
+ 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
+
+ signals[CHAT_STATE_CHANGED] =
+ g_signal_new ("chat-state-changed",
+ G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST,
+ 0,
+ NULL, NULL,
+ _empathy_marshal_VOID__OBJECT_UINT,
+ G_TYPE_NONE,
+ 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
+
+ signals[PROPERTY_CHANGED] =
+ g_signal_new ("property-changed",
+ G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST,
+ 0,
+ NULL, NULL,
+ _empathy_marshal_VOID__STRING_BOXED,
+ G_TYPE_NONE,
+ 2, G_TYPE_STRING, G_TYPE_VALUE);
+
+ signals[DESTROY] =
+ g_signal_new ("destroy",
+ G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST,
+ 0,
+ NULL, NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+
+ g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
+}
+
+static void
+empathy_tp_chat_init (EmpathyTpChat *chat)
+{
+ EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
+ EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
+
+ chat->priv = priv;
+ priv->contact_monitor = NULL;
+ priv->messages_queue = g_queue_new ();
+ priv->pending_messages_queue = g_queue_new ();
+}
+
+static void
+tp_chat_iface_init (EmpathyContactListIface *iface)
+{
+ iface->add = tp_chat_add;
+ iface->remove = tp_chat_remove;
+ iface->get_members = tp_chat_get_members;
+ iface->get_monitor = tp_chat_get_monitor;
+}
+
+EmpathyTpChat *
+empathy_tp_chat_new (TpChannel *channel)
+{
+ return g_object_new (EMPATHY_TYPE_TP_CHAT,
+ "channel", channel,
+ NULL);
+}
+
+void
+empathy_tp_chat_close (EmpathyTpChat *chat) {
+ EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+ /* If there are still messages left, it'll come back..
+ We loose the ordering of sent messages though */
+ g_signal_handlers_disconnect_by_func (priv->channel,
+ tp_chat_invalidated_cb, chat);
+
+ tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
+ "closing channel", NULL, NULL);
+
+ g_object_unref (priv->channel);
+ priv->channel = NULL;
+
+ g_signal_emit (chat, signals[DESTROY], 0);
+}
+
+const gchar *
+empathy_tp_chat_get_id (EmpathyTpChat *chat)
+{
+ EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+ g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
+
+ return tp_channel_get_identifier (priv->channel);
+}
+
+EmpathyContact *
+empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
+{
+ EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+ g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
+ g_return_val_if_fail (priv->ready, NULL);
+
+ return priv->remote_contact;
+}
+
+TpChannel *
+empathy_tp_chat_get_channel (EmpathyTpChat *chat)
+{
+ EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+ g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
+
+ return priv->channel;
+}
+
+TpConnection *
+empathy_tp_chat_get_connection (EmpathyTpChat *chat)
+{
+ EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+ g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
+
+ return tp_channel_borrow_connection (priv->channel);
+}
+
+gboolean
+empathy_tp_chat_is_ready (EmpathyTpChat *chat)
+{
+ EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+ g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
+
+ return priv->ready;
+}
+
+void
+empathy_tp_chat_send (EmpathyTpChat *chat,
+ EmpathyMessage *message)
+{
+ EmpathyTpChatPriv *priv = GET_PRIV (chat);
+ const gchar *message_body;
+ TpChannelTextMessageType message_type;
+
+ g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
+ g_return_if_fail (EMPATHY_IS_MESSAGE (message));
+ g_return_if_fail (priv->ready);
+
+ message_body = empathy_message_get_body (message);
+ message_type = empathy_message_get_tptype (message);
+
+ DEBUG ("Sending message: %s", message_body);
+ tp_cli_channel_type_text_call_send (priv->channel, -1,
+ message_type,
+ message_body,
+ tp_chat_send_cb,
+ g_object_ref (message),
+ (GDestroyNotify) g_object_unref,
+ G_OBJECT (chat));
+}
+
+void
+empathy_tp_chat_set_state (EmpathyTpChat *chat,
+ TpChannelChatState state)
+{
+ EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+ g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
+ g_return_if_fail (priv->ready);
+
+ DEBUG ("Set state: %d", state);
+ tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
+ state,
+ tp_chat_async_cb,
+ "setting chat state",
+ NULL,
+ G_OBJECT (chat));
+}
+
+
+const GList *
+empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
+{
+ EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+ g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
+ g_return_val_if_fail (priv->ready, NULL);
+
+ return priv->pending_messages_queue->head;
+}
+
+static void
+acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
+ EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+ tp_cli_channel_type_text_call_acknowledge_pending_messages (
+ priv->channel, -1, ids, tp_chat_async_cb,
+ "acknowledging received message", NULL, G_OBJECT (chat));
+}
+
+void
+empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
+ EmpathyMessage *message) {
+ EmpathyTpChatPriv *priv = GET_PRIV (chat);
+ GArray *message_ids;
+ GList *m;
+ guint id;
+
+ g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
+ g_return_if_fail (priv->ready);
+
+ if (empathy_message_get_sender (message) == priv->user)
+ goto out;
+
+ message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
+
+ id = empathy_message_get_id (message);
+ g_array_append_val (message_ids, id);
+ acknowledge_messages (chat, message_ids);
+ g_array_free (message_ids, TRUE);
+
+out:
+ m = g_queue_find (priv->pending_messages_queue, message);
+ g_assert (m != NULL);
+ g_queue_delete_link (priv->pending_messages_queue, m);