]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-chat.c
Updated Polish translation
[empathy.git] / libempathy-gtk / empathy-chat.c
index a979c9e76203349645d5d30f4f460fef86f595c2..4db9e455c4a31a98c4b7e3c38a658588021ba124 100644 (file)
 #include <glib/gi18n-lib.h>
 #include <gtk/gtk.h>
 
+#include <telepathy-glib/account-manager.h>
 #include <telepathy-glib/util.h>
+#ifdef ENABLE_TPL
+#include <telepathy-logger/log-manager.h>
+#else
 
-#include <libempathy/empathy-account-manager.h>
 #include <libempathy/empathy-log-manager.h>
+#endif /* ENABLE_TPL */
 #include <libempathy/empathy-contact-list.h>
 #include <libempathy/empathy-utils.h>
 #include <libempathy/empathy-dispatcher.h>
 #include "empathy-contact-list-store.h"
 #include "empathy-contact-list-view.h"
 #include "empathy-contact-menu.h"
+#include "empathy-search-bar.h"
 #include "empathy-theme-manager.h"
 #include "empathy-smiley-manager.h"
 #include "empathy-ui-utils.h"
+#include "empathy-string-parser.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
 #include <libempathy/empathy-debug.h>
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
 typedef struct {
        EmpathyTpChat     *tp_chat;
-       EmpathyAccount    *account;
+       TpAccount         *account;
        gchar             *id;
        gchar             *name;
        gchar             *subject;
        EmpathyContact    *remote_contact;
        gboolean           show_contacts;
 
+#ifdef ENABLE_TPL
+       TplLogManager     *log_manager;
+#else
        EmpathyLogManager *log_manager;
-       EmpathyAccountManager *account_manager;
-       GSList            *sent_messages;
-       gint               sent_messages_index;
+#endif /* ENABLE_TPL */
+       TpAccountManager  *account_manager;
+       GList             *input_history;
+       GList             *input_history_current;
        GList             *compositors;
        GCompletion       *completion;
        guint              composing_stop_timeout_id;
@@ -92,8 +102,45 @@ typedef struct {
        GtkWidget         *hbox_topic;
        GtkWidget         *label_topic;
        GtkWidget         *contact_list_view;
+       GtkWidget         *info_bar_vbox;
+       GtkWidget         *search_bar;
+
+       guint              unread_messages;
+       /* TRUE if the pending messages can be displayed. This is to avoid to show
+        * pending messages *before* messages from logs. (#603980) */
+       gboolean           can_show_pending;
+
+       /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
+        * be differently handled since it introduces another race condition, which
+        * is really hard to occur, but still possible.
+        *
+        * With the current workaround (which has the race above), we need to be
+        * sure to ACK any pending messages only when the retrieval of backlogs is
+        * finished, that's why using retrieving_backlogs flag.
+        * empathy_chat_messages_read () will check this variable and not ACK
+        * anything when TRUE. It will be set TRUE at chat_constructed () and set
+        * back to FALSE when the backlog has been retrieved and the pending
+        * messages actually showed to the user.
+        *
+        * Race condition introduced with this workaround:
+        * Scenario: a message is pending, the user is notified and selects the tab.
+        * the tab with a pending message is focused before the messages are properly
+        * shown (since the preparation of the window is slower AND async WRT the
+        * tab showing), which means the user won't see any new messages (rare but
+        * possible), if he/she will change tab focus before the messages are
+        * properly shown, the tab will be set as 'seen' and the user won't be
+        * notified again about the already notified pending messages when the
+        * messages in tab will be properly shown */
+       gboolean           retrieving_backlogs;
 } EmpathyChatPriv;
 
+typedef struct {
+       gchar *text; /* Original message that was specified
+                     * upon entry creation. */
+       gchar *modified_text; /* Message that was modified by user.
+                              * When no modifications were made, it is NULL */
+} InputHistoryEntry;
+
 enum {
        COMPOSING,
        NEW_MESSAGE,
@@ -196,39 +243,67 @@ chat_connect_channel_reconnected (EmpathyDispatchOperation *dispatch,
 }
 
 static void
-chat_new_connection_cb (EmpathyAccountManager *manager,
-                       TpConnection *connection,
-                       EmpathyChat *chat)
+reconnected_connection_ready_cb (TpConnection *connection,
+                       const GError *error,
+                       gpointer user_data)
 {
+       EmpathyChat *chat = user_data;
        EmpathyChatPriv *priv = GET_PRIV (chat);
-       EmpathyAccount *account;
-
-       account = empathy_account_manager_get_account_for_connection (manager,
-               connection);
-       if (!priv->tp_chat && account == priv->account &&
-           priv->handle_type != TP_HANDLE_TYPE_NONE &&
-           !EMP_STR_EMPTY (priv->id)) {
-
-               DEBUG ("Account reconnected, request a new Text channel");
-
-               switch (priv->handle_type) {
-                       case TP_HANDLE_TYPE_CONTACT:
-                               empathy_dispatcher_chat_with_contact_id (
-                                       connection, priv->id,
-                                       chat_connect_channel_reconnected,
-                                       chat);
-                               break;
-                       case TP_HANDLE_TYPE_ROOM:
-                               empathy_dispatcher_join_muc (connection,
-                                       priv->id,
-                                       chat_connect_channel_reconnected,
-                                       chat);
-                               break;
-                       default:
-                               g_assert_not_reached ();
-                               break;
-               }
+
+       if (error != NULL) {
+               DEBUG ("connection is not ready: %s", error->message);
+               goto out;
+       }
+
+       DEBUG ("Account reconnected, request a new Text channel");
+
+       switch (priv->handle_type) {
+               case TP_HANDLE_TYPE_CONTACT:
+                       empathy_dispatcher_chat_with_contact_id (
+                               connection, priv->id,
+                               chat_connect_channel_reconnected,
+                               chat);
+                       break;
+               case TP_HANDLE_TYPE_ROOM:
+                       empathy_dispatcher_join_muc (connection,
+                               priv->id,
+                               chat_connect_channel_reconnected,
+                               chat);
+                       break;
+               default:
+                       g_assert_not_reached ();
+                       break;
        }
+
+out:
+       g_object_unref (chat);
+}
+
+static void
+chat_new_connection_cb (TpAccount   *account,
+                       guint        old_status,
+                       guint        new_status,
+                       guint        reason,
+                       gchar       *dbus_error_name,
+                       GHashTable  *details,
+                       EmpathyChat *chat)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       TpConnection *connection;
+
+       if (new_status != TP_CONNECTION_STATUS_CONNECTED)
+               return;
+
+       connection = tp_account_get_connection (account);
+
+       if (priv->tp_chat != NULL || account != priv->account ||
+           priv->handle_type == TP_HANDLE_TYPE_NONE ||
+           EMP_STR_EMPTY (priv->id))
+               return;
+
+       g_object_ref (chat);
+       tp_connection_call_when_ready (connection, reconnected_connection_ready_cb,
+                                  chat);
 }
 
 static void
@@ -291,89 +366,607 @@ chat_composing_stop (EmpathyChat *chat)
                                   TP_CHANNEL_CHAT_STATE_ACTIVE);
 }
 
+static gint
+chat_input_history_entry_cmp (InputHistoryEntry *entry,
+                              const gchar *text)
+{
+       if (!tp_strdiff (entry->text, text)) {
+               if (entry->modified_text != NULL) {
+                       /* Modified entry and single string cannot be equal. */
+                       return 1;
+               }
+               return 0;
+       }
+       return 1;
+}
+
+static InputHistoryEntry *
+chat_input_history_entry_new_with_text (const gchar *text)
+{
+       InputHistoryEntry *entry;
+       entry = g_slice_new0 (InputHistoryEntry);
+       entry->text = g_strdup (text);
+
+       return entry;
+}
+
 static void
-chat_sent_message_add (EmpathyChat  *chat,
-                      const gchar *str)
+chat_input_history_entry_free (InputHistoryEntry *entry)
 {
-       EmpathyChatPriv *priv;
-       GSList         *list;
-       GSList         *item;
+       g_free (entry->text);
+       g_free (entry->modified_text);
+       g_slice_free (InputHistoryEntry, entry);
+}
+
+static void
+chat_input_history_entry_revert (InputHistoryEntry *entry)
+{
+       g_free (entry->modified_text);
+       entry->modified_text = NULL;
+}
+
+static void
+chat_input_history_entry_update_text (InputHistoryEntry *entry,
+                                      const gchar *text)
+{
+       gchar *old;
+
+       if (!tp_strdiff (text, entry->text)) {
+               g_free (entry->modified_text);
+               entry->modified_text = NULL;
+               return;
+       }
+
+       old = entry->modified_text;
+       entry->modified_text = g_strdup (text);
+       g_free (old);
+}
+
+static const gchar *
+chat_input_history_entry_get_text (InputHistoryEntry *entry)
+{
+       if (entry == NULL) {
+               return NULL;
+       }
+
+       if (entry->modified_text != NULL) {
+               return entry->modified_text;
+       }
+       return entry->text;
+}
+
+static GList *
+chat_input_history_remove_item (GList *list,
+                                GList *item)
+{
+       list = g_list_remove_link (list, item);
+       chat_input_history_entry_free (item->data);
+       g_list_free_1 (item);
+       return list;
+}
+
+static void
+chat_input_history_revert (EmpathyChat *chat)
+{
+       EmpathyChatPriv   *priv;
+       GList             *list;
+       GList             *item1;
+       GList             *item2;
+       InputHistoryEntry *entry;
 
        priv = GET_PRIV (chat);
+       list = priv->input_history;
 
-       /* Save the sent message in our repeat buffer */
-       list = priv->sent_messages;
+       if (list == NULL) {
+               DEBUG ("No input history");
+               return;
+       }
 
-       /* Remove any other occurances of this msg */
-       while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
-               list = g_slist_remove_link (list, item);
-               g_free (item->data);
-               g_slist_free1 (item);
+       /* Delete temporary entry */
+       if (priv->input_history_current != NULL) {
+               item1 = list;
+               list = chat_input_history_remove_item (list, item1);
+               if (priv->input_history_current == item1) {
+                       /* Removed temporary entry was current entry */
+                       priv->input_history = list;
+                       priv->input_history_current = NULL;
+                       return;
+               }
+       }
+       else {
+               /* There is no entry to revert */
+               return;
        }
 
-       /* Trim the list to the last 10 items */
-       while (g_slist_length (list) > 10) {
-               item = g_slist_last (list);
-               if (item) {
-                       list = g_slist_remove_link (list, item);
-                       g_free (item->data);
-                       g_slist_free1 (item);
+       /* Restore the current history entry to original value */
+       item1 = priv->input_history_current;
+       entry = item1->data;
+       chat_input_history_entry_revert (entry);
+
+       /* Remove restored entry if there is other occurance before this entry */
+       item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
+                                   (GCompareFunc) chat_input_history_entry_cmp);
+       if (item2 != item1) {
+               list = chat_input_history_remove_item (list, item1);
+       }
+       else {
+               /* Remove other occurance of the restored entry */
+               item2 = g_list_find_custom (item1->next,
+                                           chat_input_history_entry_get_text (entry),
+                                           (GCompareFunc) chat_input_history_entry_cmp);
+               if (item2 != NULL) {
+                       list = chat_input_history_remove_item (list, item2);
                }
        }
 
-       /* Add new message */
-       list = g_slist_prepend (list, g_strdup (str));
+       priv->input_history_current = NULL;
+       priv->input_history = list;
+}
+
+static void
+chat_input_history_add (EmpathyChat  *chat,
+                        const gchar *str,
+                        gboolean temporary)
+{
+       EmpathyChatPriv   *priv;
+       GList             *list;
+       GList             *item;
+       InputHistoryEntry *entry;
 
-       /* Set list and reset the index */
-       priv->sent_messages = list;
-       priv->sent_messages_index = -1;
+       priv = GET_PRIV (chat);
+
+       list = priv->input_history;
+
+       /* Remove any other occurances of this entry, if not temporary */
+       if (!temporary) {
+               while ((item = g_list_find_custom (list, str,
+                   (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
+                       list = chat_input_history_remove_item (list, item);
+               }
+
+               /* Trim the list to the last 10 items */
+               while (g_list_length (list) > 10) {
+                       item = g_list_last (list);
+                       if (item != NULL) {
+                               list = chat_input_history_remove_item (list, item);
+                       }
+               }
+       }
+
+
+
+       /* Add new entry */
+       entry = chat_input_history_entry_new_with_text (str);
+       list = g_list_prepend (list, entry);
+
+       /* Set the list and the current item pointer */
+       priv->input_history = list;
+       if (temporary) {
+               priv->input_history_current = list;
+       }
+       else {
+               priv->input_history_current = NULL;
+       }
 }
 
 static const gchar *
-chat_sent_message_get_next (EmpathyChat *chat)
+chat_input_history_get_next (EmpathyChat *chat)
 {
        EmpathyChatPriv *priv;
-       gint            max;
+       GList           *item;
+       const gchar     *msg;
 
        priv = GET_PRIV (chat);
 
-       if (!priv->sent_messages) {
-               DEBUG ("No sent messages, next message is NULL");
+       if (priv->input_history == NULL) {
+               DEBUG ("No input history, next entry is NULL");
                return NULL;
        }
+       g_assert (priv->input_history_current != NULL);
 
-       max = g_slist_length (priv->sent_messages) - 1;
-
-       if (priv->sent_messages_index < max) {
-               priv->sent_messages_index++;
+       if ((item = g_list_next (priv->input_history_current)) == NULL)
+       {
+               item = priv->input_history_current;
        }
 
-       DEBUG ("Returning next message index:%d", priv->sent_messages_index);
+       msg = chat_input_history_entry_get_text (item->data);
+
+       DEBUG ("Returning next entry: '%s'", msg);
 
-       return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
+       priv->input_history_current = item;
+
+       return msg;
 }
 
 static const gchar *
-chat_sent_message_get_last (EmpathyChat *chat)
+chat_input_history_get_prev (EmpathyChat *chat)
 {
        EmpathyChatPriv *priv;
+       GList           *item;
+       const gchar     *msg;
 
        g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
 
        priv = GET_PRIV (chat);
 
-       if (!priv->sent_messages) {
-               DEBUG ("No sent messages, last message is NULL");
+       if (priv->input_history == NULL) {
+               DEBUG ("No input history, previous entry is NULL");
+               return NULL;
+       }
+
+       if (priv->input_history_current == NULL)
+       {
                return NULL;
        }
+       else if ((item = g_list_previous (priv->input_history_current)) == NULL)
+       {
+               item = priv->input_history_current;
+       }
+
+       msg = chat_input_history_entry_get_text (item->data);
+
+       DEBUG ("Returning previous entry: '%s'", msg);
+
+       priv->input_history_current = item;
+
+       return msg;
+}
+
+static void
+chat_input_history_update (EmpathyChat *chat,
+                           GtkTextBuffer *buffer)
+{
+       EmpathyChatPriv      *priv;
+       GtkTextIter           start, end;
+       gchar                *text;
+       InputHistoryEntry    *entry;
+
+       priv = GET_PRIV (chat);
+
+       gtk_text_buffer_get_bounds (buffer, &start, &end);
+       text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
+
+       if (priv->input_history_current == NULL) {
+               /* Add the current text temporarily to the history */
+               chat_input_history_add (chat, text, TRUE);
+               g_free (text);
+               return;
+       }
+
+       /* Save the changes in the history */
+       entry = priv->input_history_current->data;
+       if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
+               chat_input_history_entry_update_text (entry, text);
+       }
+
+       g_free (text);
+}
+
+static void
+chat_command_join_cb (EmpathyDispatchOperation *dispatch,
+                     const GError             *error,
+                     gpointer                  user_data)
+{
+       EmpathyChat *chat = user_data;
+
+       if (error != NULL) {
+               DEBUG ("Error: %s", error->message);
+               empathy_chat_view_append_event (chat->view,
+                       _("Failed to join chat room"));
+       }
+}
+
+typedef struct {
+       EmpathyChat *chat;
+       gchar *message;
+} ChatCommandMsgData;
+
+static void
+chat_command_msg_cb (EmpathyDispatchOperation *dispatch,
+                             const GError             *error,
+                             gpointer                  user_data)
+{
+       ChatCommandMsgData *data = user_data;
+
+       if (error != NULL) {
+               empathy_chat_view_append_event (data->chat->view,
+                       _("Failed to open private chat"));
+               goto OUT;
+       }
+
+       if (!EMP_STR_EMPTY (data->message)) {
+               EmpathyTpChat *tpchat;
+               EmpathyMessage *message;
+
+               tpchat = EMPATHY_TP_CHAT (
+                       empathy_dispatch_operation_get_channel_wrapper (dispatch));
+
+               message = empathy_message_new (data->message);
+               empathy_tp_chat_send (tpchat, message);
+               g_object_unref (message);
+       }
+
+OUT:
+       g_free (data->message);
+       g_slice_free (ChatCommandMsgData, data);
+}
+
+static void
+chat_command_clear (EmpathyChat *chat,
+                   GStrv        strv)
+{
+       empathy_chat_view_clear (chat->view);
+}
+
+static void
+chat_command_topic (EmpathyChat *chat,
+                   GStrv        strv)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       EmpathyTpChatProperty *property;
+       GValue value = {0, };
+
+       property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
+       if (property == NULL) {
+               empathy_chat_view_append_event (chat->view,
+                       _("Topic not supported on this conversation"));
+               return;
+       }
+
+       if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
+               empathy_chat_view_append_event (chat->view,
+                       _("You are not allowed to change the topic"));
+               return;
+       }
+
+       g_value_init (&value, G_TYPE_STRING);
+       g_value_set_string (&value, strv[1]);
+       empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
+       g_value_unset (&value);
+}
+
+static void
+chat_command_join (EmpathyChat *chat,
+                  GStrv        strv)
+{
+       guint i = 0;
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+
+       GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
+
+       while (rooms[i] != NULL) {
+               /* ignore empty strings */
+               if (!EMP_STR_EMPTY (rooms[i])) {
+                       TpConnection *connection;
+
+                       connection = empathy_tp_chat_get_connection (priv->tp_chat);
+                       empathy_dispatcher_join_muc (connection, rooms[i],
+                                                    chat_command_join_cb,
+                                                    chat);
+               }
+               i++;
+       }
+       g_strfreev (rooms);
+}
+
+static void
+chat_command_msg_internal (EmpathyChat *chat,
+                          const gchar *contact_id,
+                          const gchar *message)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       TpConnection *connection;
+       ChatCommandMsgData *data;
+
+       /* FIXME: We should probably search in members alias. But this
+        * is enough for IRC */
+       data = g_slice_new (ChatCommandMsgData);
+       data->chat = chat;
+       data->message = g_strdup (message);
+       connection = empathy_tp_chat_get_connection (priv->tp_chat);
+       empathy_dispatcher_chat_with_contact_id (connection, contact_id,
+                                                chat_command_msg_cb,
+                                                data);
+}
+
+static void
+chat_command_query (EmpathyChat *chat,
+                   GStrv        strv)
+{
+       /* If <message> part is not defined,
+        * strv[2] will be the terminal NULL */
+       chat_command_msg_internal (chat, strv[1], strv[2]);
+}
+
+static void
+chat_command_msg (EmpathyChat *chat,
+                 GStrv        strv)
+{
+       chat_command_msg_internal (chat, strv[1], strv[2]);
+}
+
+static void
+chat_command_nick (EmpathyChat *chat,
+                  GStrv        strv)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       TpConnection *connection;
+       GHashTable *new_alias;
+       TpHandle handle;
+
+       connection = tp_account_get_connection (priv->account);
+       handle = tp_connection_get_self_handle (connection);
+       new_alias = g_hash_table_new (g_direct_hash, g_direct_equal);
+       g_hash_table_insert (new_alias, GUINT_TO_POINTER (handle), strv[1]);
+
+       tp_cli_connection_interface_aliasing_call_set_aliases (connection, -1,
+               new_alias, NULL, NULL, NULL, NULL);
+
+       g_hash_table_destroy (new_alias);
+}
+
+static void
+chat_command_me (EmpathyChat *chat,
+                 GStrv        strv)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       EmpathyMessage *message;
+
+       message = empathy_message_new (strv[1]);
+       empathy_message_set_tptype (message, TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION);
+       empathy_tp_chat_send (priv->tp_chat, message);
+       g_object_unref (message);
+}
+
+static void
+chat_command_say (EmpathyChat *chat,
+                 GStrv        strv)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       EmpathyMessage *message;
+
+       message = empathy_message_new (strv[1]);
+       empathy_tp_chat_send (priv->tp_chat, message);
+       g_object_unref (message);
+}
+
+static void chat_command_help (EmpathyChat *chat, GStrv strv);
+
+typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
+
+typedef struct {
+       const gchar *prefix;
+       guint min_parts;
+       guint max_parts;
+       ChatCommandFunc func;
+       const gchar *help;
+} ChatCommandItem;
+
+static ChatCommandItem commands[] = {
+       {"clear", 1, 1, chat_command_clear,
+        N_("/clear: clear all messages from the current conversation")},
+
+       {"topic", 2, 2, chat_command_topic,
+        N_("/topic <topic>: set the topic of the current conversation")},
+
+       {"join", 2, 2, chat_command_join,
+        N_("/join <chat room ID>: join a new chat room")},
+
+       {"j", 2, 2, chat_command_join,
+        N_("/j <chat room ID>: join a new chat room")},
+
+       {"query", 2, 3, chat_command_query,
+        N_("/query <contact ID> [<message>]: open a private chat")},
+
+       {"msg", 3, 3, chat_command_msg,
+        N_("/msg <contact ID> <message>: open a private chat")},
+
+       {"nick", 2, 2, chat_command_nick,
+        N_("/nick <nickname>: change your nickname on the current server")},
+
+       {"me", 2, 2, chat_command_me,
+        N_("/me <message>: send an ACTION message to the current conversation")},
+
+       {"say", 2, 2, chat_command_say,
+        N_("/say <message>: send <message> to the current conversation. "
+           "This is used to send a message starting with a '/'. For example: "
+           "\"/say /join is used to join a new chat room\"")},
+
+       {"help", 1, 2, chat_command_help,
+        N_("/help [<command>]: show all supported commands. "
+           "If <command> is defined, show its usage.")},
+};
+
+static void
+chat_command_show_help (EmpathyChat     *chat,
+                       ChatCommandItem *item)
+{
+       gchar *str;
+
+       str = g_strdup_printf (_("Usage: %s"), _(item->help));
+       empathy_chat_view_append_event (chat->view, str);
+       g_free (str);
+}
+
+static void
+chat_command_help (EmpathyChat *chat,
+                  GStrv        strv)
+{
+       guint i;
+
+       /* If <command> part is not defined,
+        * strv[1] will be the terminal NULL */
+       if (strv[1] == NULL) {
+               for (i = 0; i < G_N_ELEMENTS (commands); i++) {
+                       empathy_chat_view_append_event (chat->view,
+                               _(commands[i].help));
+               }
+               return;
+       }
+
+       for (i = 0; i < G_N_ELEMENTS (commands); i++) {
+               if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
+                       chat_command_show_help (chat, &commands[i]);
+                       return;
+               }
+       }
+
+       empathy_chat_view_append_event (chat->view,
+               _("Unknown command"));
+}
+
+static GStrv
+chat_command_parse (const gchar *text, guint max_parts)
+{
+       GPtrArray *array;
+       gchar *item;
+
+       DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
+
+       array = g_ptr_array_sized_new (max_parts + 1);
+       while (max_parts > 1) {
+               const gchar *end;
+
+               /* Skip white spaces */
+               while (g_ascii_isspace (*text)) {
+                       text++;
+               }
+
+               /* Search the end of this part, until first space. */
+               for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
+                       /* Do nothing */;
+               if (*end == '\0') {
+                       break;
+               }
+
+               item = g_strndup (text, end - text);
+               g_ptr_array_add (array, item);
+               DEBUG ("\tITEM: \"%s\"", item);
+
+               text = end;
+               max_parts--;
+       }
 
-       if (priv->sent_messages_index >= 0) {
-               priv->sent_messages_index--;
+       /* Append last part if not empty */
+       item = g_strstrip (g_strdup (text));
+       if (!EMP_STR_EMPTY (item)) {
+               g_ptr_array_add (array, item);
+               DEBUG ("\tITEM: \"%s\"", item);
+       } else {
+               g_free (item);
        }
 
-       DEBUG ("Returning last message index:%d", priv->sent_messages_index);
+       /* Make the array NULL-terminated */
+       g_ptr_array_add (array, NULL);
+
+       return (GStrv) g_ptr_array_free (array, FALSE);
+}
 
-       return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
+static gboolean
+has_prefix_case (const gchar *s,
+                 const gchar *prefix)
+{
+       return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
 }
 
 static void
@@ -382,6 +975,7 @@ chat_send (EmpathyChat  *chat,
 {
        EmpathyChatPriv *priv;
        EmpathyMessage  *message;
+       guint            i;
 
        if (EMP_STR_EMPTY (msg)) {
                return;
@@ -389,22 +983,65 @@ chat_send (EmpathyChat  *chat,
 
        priv = GET_PRIV (chat);
 
-       chat_sent_message_add (chat, msg);
+       chat_input_history_add (chat, msg, FALSE);
 
-       if (strcmp (msg, "/clear") == 0) {
-               empathy_chat_view_clear (chat->view);
-               return;
-       }
+       if (msg[0] == '/') {
+               gboolean second_slash = FALSE;
+               const gchar *iter = msg + 1;
 
-       message = empathy_message_new_from_entry (msg);
+               for (i = 0; i < G_N_ELEMENTS (commands); i++) {
+                       GStrv strv;
+                       guint strv_len;
+                       gchar c;
 
-       if (message == NULL) {
-               empathy_chat_view_append_event (chat->view,
-                       _("Unsupported command"));
-       } else {
-               empathy_tp_chat_send (priv->tp_chat, message);
-               g_object_unref (message);
+                       if (!has_prefix_case (msg + 1, commands[i].prefix)) {
+                               continue;
+                       }
+                       c = *(msg + 1 + strlen (commands[i].prefix));
+                       if (c != '\0' && !g_ascii_isspace (c)) {
+                               continue;
+                       }
+
+                       /* We can't use g_strsplit here because it does
+                        * not deal correctly if we have more than one space
+                        * between args */
+                       strv = chat_command_parse (msg + 1, commands[i].max_parts);
+
+                       strv_len = g_strv_length (strv);
+                       if (strv_len < commands[i].min_parts ||
+                           strv_len > commands[i].max_parts) {
+                               chat_command_show_help (chat, &commands[i]);
+                               g_strfreev (strv);
+                               return;
+                       }
+
+                       commands[i].func (chat, strv);
+                       g_strfreev (strv);
+                       return;
+               }
+
+               /* Also allow messages with two slashes before the
+                * first space, so it is possible to send a /unix/path.
+                * This heuristic is kind of crap. */
+               while (*iter != '\0' && !g_ascii_isspace (*iter)) {
+                       if (*iter == '/') {
+                               second_slash = TRUE;
+                               break;
+                       }
+                       iter++;
+               }
+
+               if (!second_slash) {
+                       empathy_chat_view_append_event (chat->view,
+                               _("Unknown command; see /help for the available"
+                                 " commands"));
+                       return;
+               }
        }
+
+       message = empathy_message_new (msg);
+       empathy_tp_chat_send (priv->tp_chat, message);
+       g_object_unref (message);
 }
 
 static void
@@ -424,6 +1061,8 @@ chat_input_text_view_send (EmpathyChat *chat)
 
        /* clear the input field */
        gtk_text_buffer_set_text (buffer, "", -1);
+       /* delete input history modifications */
+       chat_input_history_revert (chat);
 
        chat_send (chat, msg);
        g_free (msg);
@@ -509,6 +1148,7 @@ chat_message_received (EmpathyChat *chat, EmpathyMessage *message)
                               TP_CHANNEL_CHAT_STATE_ACTIVE,
                               chat);
 
+       priv->unread_messages++;
        g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
 }
 
@@ -518,12 +1158,11 @@ chat_message_received_cb (EmpathyTpChat  *tp_chat,
                          EmpathyChat    *chat)
 {
        chat_message_received (chat, message);
-       empathy_tp_chat_acknowledge_message (tp_chat, message);
 }
 
 static void
 chat_send_error_cb (EmpathyTpChat          *tp_chat,
-                   EmpathyMessage         *message,
+                   const gchar            *message_body,
                    TpChannelTextSendError  error_code,
                    EmpathyChat            *chat)
 {
@@ -552,7 +1191,7 @@ chat_send_error_cb (EmpathyTpChat          *tp_chat,
        }
 
        str = g_strdup_printf (_("Error sending message '%s': %s"),
-                              empathy_message_get_body (message),
+                              message_body,
                               error);
        empathy_chat_view_append_event (chat->view, str);
        g_free (str);
@@ -574,7 +1213,12 @@ chat_property_changed_cb (EmpathyTpChat *tp_chat,
                if (EMP_STR_EMPTY (priv->subject)) {
                        gtk_widget_hide (priv->hbox_topic);
                } else {
-                       gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
+                       gchar *markup_text;
+
+                       markup_text = empathy_add_link_markup (priv->subject);
+                       gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
+                       g_free (markup_text);
+
                        gtk_widget_show (priv->hbox_topic);
                }
                if (priv->block_events_timeout_id == 0) {
@@ -675,6 +1319,13 @@ chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
        }
 }
 
+static gboolean
+empathy_isspace_cb (gunichar c,
+                gpointer data)
+{
+       return g_unichar_isspace (c);
+}
+
 static gboolean
 chat_input_key_press_event_cb (GtkWidget   *widget,
                               GdkEventKey *event,
@@ -695,11 +1346,12 @@ chat_input_key_press_event_cb (GtkWidget   *widget,
                const gchar   *str;
 
                buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
+               chat_input_history_update (chat, buffer);
 
                if (event->keyval == GDK_Up) {
-                       str = chat_sent_message_get_next (chat);
+                       str = chat_input_history_get_next (chat);
                } else {
-                       str = chat_sent_message_get_last (chat);
+                       str = chat_input_history_get_prev (chat);
                }
 
                g_signal_handlers_block_by_func (buffer,
@@ -755,6 +1407,9 @@ chat_input_key_press_event_cb (GtkWidget   *widget,
                gtk_adjustment_set_value (adj, val);
                return TRUE;
        }
+       if (event->keyval == GDK_Escape) {
+               empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
+       }
        if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
            event->keyval == GDK_Tab) {
                GtkTextBuffer *buffer;
@@ -768,7 +1423,9 @@ chat_input_key_press_event_cb (GtkWidget   *widget,
 
                /* Get the start of the nick to complete. */
                gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
-               gtk_text_iter_backward_word_start (&start);
+               if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
+                       gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
+               }
                is_start_of_buffer = gtk_text_iter_is_start (&start);
 
                list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
@@ -785,6 +1442,8 @@ chat_input_key_press_event_cb (GtkWidget   *widget,
                        guint        len;
                        const gchar *text;
                        gchar       *complete_char = NULL;
+                       GString     *message = NULL;
+                       GList       *l;
 
                        gtk_text_buffer_delete (buffer, &start, &current);
 
@@ -800,6 +1459,18 @@ chat_input_key_press_event_cb (GtkWidget   *widget,
                                text = empathy_contact_get_name (completed_list->data);
                        } else {
                                text = completed;
+
+                               /* Print all hits to the scrollback view, so the
+                                * user knows what possibilities he has.
+                                * Fixes #599779
+                                * */
+                                message = g_string_new ("");
+                                for (l = completed_list; l != NULL; l = l->next) {
+                                       g_string_append (message, empathy_contact_get_name (l->data));
+                                       g_string_append (message, " - ");
+                                }
+                                empathy_chat_view_append_event (chat->view, message->str);
+                                g_string_free (message, TRUE);
                        }
 
                        gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
@@ -879,7 +1550,9 @@ chat_input_realize_cb (GtkWidget   *widget,
                       EmpathyChat *chat)
 {
        DEBUG ("Setting focus to the input text view");
-       gtk_widget_grab_focus (widget);
+       if (gtk_widget_is_sensitive (widget)) {
+               gtk_widget_grab_focus (widget);
+       }
 }
 
 static void
@@ -1072,31 +1745,128 @@ chat_input_populate_popup_cb (GtkTextView *view,
        }
 }
 
+
+#ifdef ENABLE_TPL
+static gboolean
+chat_log_filter (TplLogEntry *log,
+                gpointer user_data)
+#else
 static gboolean
 chat_log_filter (EmpathyMessage *message,
                 gpointer user_data)
+#endif /* ENABLE_TPL */
 {
-       EmpathyChat *chat = (EmpathyChat *) user_data;
+       EmpathyChat *chat = user_data;
+#ifdef ENABLE_TPL
+       EmpathyMessage *message;
+#endif /* ENABLE_TPL */
        EmpathyChatPriv *priv = GET_PRIV (chat);
        const GList *pending;
 
+#ifdef ENABLE_TPL
+       g_return_val_if_fail (TPL_IS_LOG_ENTRY (log), FALSE);
+#else
+       g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
+#endif /* ENABLE_TPL */
+       g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
+
        pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
+#ifdef ENABLE_TPL
+       message = empathy_message_from_tpl_log_entry (log);
+#endif /* ENABLE_TPL */
 
        for (; pending; pending = g_list_next (pending)) {
                if (empathy_message_equal (message, pending->data)) {
                        return FALSE;
                }
        }
-
+#ifdef ENABLE_TPL
+       g_object_unref (message);
+#endif
        return TRUE;
 }
 
+
+static void
+show_pending_messages (EmpathyChat *chat) {
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       const GList *messages, *l;
+
+       g_return_if_fail (EMPATHY_IS_CHAT (chat));
+
+       if (chat->view == NULL || priv->tp_chat == NULL)
+               return;
+
+       if (!priv->can_show_pending)
+               return;
+
+       messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
+
+       for (l = messages; l != NULL ; l = g_list_next (l)) {
+               EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
+               chat_message_received (chat, message);
+       }
+}
+
+
+#ifdef ENABLE_TPL
+static void
+got_filtered_messages_cb (GObject *manager,
+               GAsyncResult *result,
+               gpointer user_data)
+{
+       GList *l;
+       GList *messages;
+       EmpathyChat *chat = EMPATHY_CHAT (user_data);
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       GError *error = NULL;
+
+       messages = tpl_log_manager_get_filtered_messages_async_finish (result, &error);
+
+       if (error != NULL) {
+               DEBUG ("%s. Aborting.", error->message);
+               empathy_chat_view_append_event (chat->view,
+                       _("Failed to retrieve recent logs"));
+               g_error_free (error);
+               goto out;
+       }
+
+       for (l = messages; l; l = g_list_next (l)) {
+               EmpathyMessage *message;
+               g_assert (TPL_IS_LOG_ENTRY (l->data));
+
+               message = empathy_message_from_tpl_log_entry (l->data);
+               g_object_unref (l->data);
+
+               empathy_chat_view_append_message (chat->view, message);
+               g_object_unref (message);
+       }
+       g_list_free (messages);
+
+out:
+       /* in case of TPL error, skip backlog and show pending messages */
+       priv->can_show_pending = TRUE;
+       show_pending_messages (chat);
+
+       /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
+        * about it in EmpathyChatPriv definition */
+       priv->retrieving_backlogs = FALSE;
+       empathy_chat_messages_read (chat);
+
+       /* Turn back on scrolling */
+       empathy_chat_view_scroll (chat->view, TRUE);
+}
+#endif /* ENABLE_TPL */
+
+
 static void
 chat_add_logs (EmpathyChat *chat)
 {
        EmpathyChatPriv *priv = GET_PRIV (chat);
        gboolean         is_chatroom;
+#ifndef ENABLE_TPL
        GList           *messages, *l;
+#endif /* ENABLE_TPL */
 
        if (!priv->id) {
                return;
@@ -1108,6 +1878,7 @@ chat_add_logs (EmpathyChat *chat)
        /* Add messages from last conversation */
        is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
 
+#ifndef ENABLE_TPL
        messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
                                                              priv->account,
                                                              priv->id,
@@ -1125,6 +1896,18 @@ chat_add_logs (EmpathyChat *chat)
 
        /* Turn back on scrolling */
        empathy_chat_view_scroll (chat->view, TRUE);
+#else
+       priv->retrieving_backlogs = TRUE;
+       tpl_log_manager_get_filtered_messages_async (priv->log_manager,
+                                                             priv->account,
+                                                             priv->id,
+                                                             is_chatroom,
+                                                             5,
+                                                             chat_log_filter,
+                                                             chat,
+                                                             got_filtered_messages_cb,
+                                                             (gpointer) chat);
+#endif /* ENABLE_TPL */
 }
 
 static gint
@@ -1227,6 +2010,8 @@ chat_members_changed_cb (EmpathyTpChat  *tp_chat,
        const gchar *name = empathy_contact_get_name (contact);
        gchar *str;
 
+       g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
+
        if (priv->block_events_timeout_id != 0)
                return;
 
@@ -1241,6 +2026,30 @@ chat_members_changed_cb (EmpathyTpChat  *tp_chat,
        g_free (str);
 }
 
+static void
+chat_member_renamed_cb (EmpathyTpChat  *tp_chat,
+                        EmpathyContact *old_contact,
+                        EmpathyContact *new_contact,
+                        guint           reason,
+                        gchar          *message,
+                        EmpathyChat    *chat)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+
+       g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
+
+       if (priv->block_events_timeout_id == 0) {
+               gchar *str;
+
+               str = g_strdup_printf (_("%s is now known as %s"),
+                                      empathy_contact_get_name (old_contact),
+                                      empathy_contact_get_name (new_contact));
+               empathy_chat_view_append_event (chat->view, str);
+               g_free (str);
+       }
+
+}
+
 static gboolean
 chat_reset_size_request (gpointer widget)
 {
@@ -1250,17 +2059,20 @@ chat_reset_size_request (gpointer widget)
 }
 
 static void
-chat_update_contacts_visibility (EmpathyChat *chat)
+chat_update_contacts_visibility (EmpathyChat *chat,
+                        gboolean show)
 {
        EmpathyChatPriv *priv = GET_PRIV (chat);
-       gboolean show;
-
-       show = priv->remote_contact == NULL && priv->show_contacts;
+       GtkAllocation allocation;
 
        if (!priv->scrolled_window_contacts) {
                return;
        }
 
+       if (priv->remote_contact != NULL) {
+               show = FALSE;
+       }
+
        if (show && priv->contact_list_view == NULL) {
                EmpathyContactListStore *store;
                gint                     min_width;
@@ -1271,7 +2083,8 @@ chat_update_contacts_visibility (EmpathyChat *chat)
                 * chat view is bigger the contact list will take some space on
                 * it but we make sure the chat view don't become smaller than
                 * 250. Relax the size request once the resize is done */
-               min_width = MIN (priv->vbox_left->allocation.width, 250);
+               gtk_widget_get_allocation (priv->vbox_left, &allocation);
+               min_width = MIN (allocation.width, 250);
                gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
                g_idle_add (chat_reset_size_request, priv->vbox_left);
 
@@ -1310,7 +2123,7 @@ empathy_chat_set_show_contacts (EmpathyChat *chat,
 
        priv->show_contacts = show;
 
-       chat_update_contacts_visibility (chat);
+       chat_update_contacts_visibility (chat, show);
 
        g_object_notify (G_OBJECT (chat), "show-contacts");
 }
@@ -1325,23 +2138,22 @@ chat_remote_contact_changed_cb (EmpathyChat *chat)
                priv->remote_contact = NULL;
        }
 
+       g_free (priv->id);
+
+       priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
        priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
        if (priv->remote_contact != NULL) {
                g_object_ref (priv->remote_contact);
                priv->handle_type = TP_HANDLE_TYPE_CONTACT;
-               g_free (priv->id);
-               priv->id = g_strdup (empathy_contact_get_id (priv->remote_contact));
        }
        else if (priv->tp_chat != NULL) {
                TpChannel *channel;
 
                channel = empathy_tp_chat_get_channel (priv->tp_chat);
                g_object_get (channel, "handle-type", &priv->handle_type, NULL);
-               g_free (priv->id);
-               priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
        }
 
-       chat_update_contacts_visibility (chat);
+       chat_update_contacts_visibility (chat, priv->show_contacts);
 
        g_object_notify (G_OBJECT (chat), "remote-contact");
        g_object_notify (G_OBJECT (chat), "id");
@@ -1366,24 +2178,19 @@ chat_destroy_cb (EmpathyTpChat *tp_chat,
 
        empathy_chat_view_append_event (chat->view, _("Disconnected"));
        gtk_widget_set_sensitive (chat->input_text_view, FALSE);
-       empathy_chat_set_show_contacts (chat, FALSE);
-}
-
-static void
-show_pending_messages (EmpathyChat *chat) {
-       EmpathyChatPriv *priv = GET_PRIV (chat);
-       const GList *messages, *l;
 
-       if (chat->view == NULL || priv->tp_chat == NULL)
-               return;
-
-       messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
+       chat_update_contacts_visibility (chat, FALSE);
+}
 
-       for (l = messages; l != NULL ; l = g_list_next (l)) {
-               EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
-               chat_message_received (chat, message);
-       }
-       empathy_tp_chat_acknowledge_messages (priv->tp_chat, messages);
+static gboolean
+chat_hpaned_pos_changed_cb (GtkWidget* hpaned, gpointer user_data)
+{
+       gint hpaned_pos;
+       hpaned_pos = gtk_paned_get_position (GTK_PANED(hpaned));
+       empathy_conf_set_int (empathy_conf_get (),
+                             EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
+                             hpaned_pos);
+       return TRUE;
 }
 
 static void
@@ -1394,6 +2201,7 @@ chat_create_ui (EmpathyChat *chat)
        GList           *list = NULL;
        gchar           *filename;
        GtkTextBuffer   *buffer;
+       gint              paned_pos;
 
        filename = empathy_file_lookup ("empathy-chat.ui",
                                        "libempathy-gtk");
@@ -1406,11 +2214,18 @@ chat_create_ui (EmpathyChat *chat)
                                        "hbox_topic", &priv->hbox_topic,
                                        "label_topic", &priv->label_topic,
                                        "scrolled_window_contacts", &priv->scrolled_window_contacts,
+                                       "info_bar_vbox", &priv->info_bar_vbox,
                                        NULL);
        g_free (filename);
 
        /* Add message view. */
        chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
+       /* If this is a GtkTextView, it's set as a drag destination for text/plain
+          and other types, even though it's non-editable and doesn't accept any
+          drags.  This steals drag motion for anything inside the scrollbars,
+          making drag destinations on chat windows far less useful.
+        */
+       gtk_drag_dest_unset (GTK_WIDGET (chat->view));
        g_signal_connect (chat->view, "focus_in_event",
                          G_CALLBACK (chat_text_view_focus_in_event_cb),
                          chat);
@@ -1450,14 +2265,30 @@ chat_create_ui (EmpathyChat *chat)
                           chat->input_text_view);
        gtk_widget_show (chat->input_text_view);
 
-       /* Create contact list */
-       chat_update_contacts_visibility (chat);
+       /* Add the (invisible) search bar */
+       priv->search_bar = empathy_search_bar_new (chat->view);
+       gtk_box_pack_start (GTK_BOX(priv->vbox_left),
+                           priv->search_bar,
+                           FALSE, FALSE, 0);
+       gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
 
        /* Initialy hide the topic, will be shown if not empty */
        gtk_widget_hide (priv->hbox_topic);
 
+       g_signal_connect (priv->hpaned, "notify::position",
+                         G_CALLBACK (chat_hpaned_pos_changed_cb),
+                         NULL);
+
+        /* Load the paned position */
+       if (empathy_conf_get_int (empathy_conf_get (),
+                                EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
+                                &paned_pos)
+               && paned_pos)
+               gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
+
        /* Set widget focus order */
-       list = g_list_append (NULL, priv->scrolled_window_input);
+       list = g_list_append (NULL, priv->search_bar);
+       list = g_list_append (list, priv->scrolled_window_input);
        gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
        g_list_free (list);
 
@@ -1488,7 +2319,7 @@ chat_size_request (GtkWidget      *widget,
 
   child = gtk_bin_get_child (bin);
 
-  if (child && GTK_WIDGET_VISIBLE (child))
+  if (child && gtk_widget_get_visible (child))
     {
       GtkRequisition child_requisition;
 
@@ -1507,11 +2338,11 @@ chat_size_allocate (GtkWidget     *widget,
   GtkAllocation child_allocation;
   GtkWidget *child;
 
-  widget->allocation = *allocation;
+  gtk_widget_set_allocation (widget, allocation);
 
   child = gtk_bin_get_child (bin);
 
-  if (child && GTK_WIDGET_VISIBLE (child))
+  if (child && gtk_widget_get_visible (child))
     {
       child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
       child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
@@ -1533,17 +2364,14 @@ chat_finalize (GObject *object)
 
        DEBUG ("Finalized: %p", object);
 
-       g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
-       g_slist_free (priv->sent_messages);
+       g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
+       g_list_free (priv->input_history);
 
        g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
        g_list_free (priv->compositors);
 
        chat_composing_remove_timeout (chat);
 
-       g_signal_handlers_disconnect_by_func (priv->account_manager,
-                                             chat_new_connection_cb, object);
-
        g_object_unref (priv->account_manager);
        g_object_unref (priv->log_manager);
 
@@ -1562,7 +2390,7 @@ chat_finalize (GObject *object)
                        chat_members_changed_cb, chat);
                g_signal_handlers_disconnect_by_func (priv->tp_chat,
                        chat_remote_contact_changed_cb, chat);
-               empathy_tp_chat_close (priv->tp_chat);
+               empathy_tp_chat_leave (priv->tp_chat);
                g_object_unref (priv->tp_chat);
        }
        if (priv->account) {
@@ -1588,10 +2416,16 @@ static void
 chat_constructed (GObject *object)
 {
        EmpathyChat *chat = EMPATHY_CHAT (object);
+       EmpathyChatPriv *priv = GET_PRIV (chat);
 
-       chat_create_ui (chat);
-       chat_add_logs (chat);
+       if (priv->handle_type != TP_HANDLE_TYPE_ROOM)
+               chat_add_logs (chat);
+#ifndef ENABLE_TPL
+       /* When async API are involved, pending message are shown at the end of the
+        * callbacks' chain fired by chat_add_logs */
+       priv->can_show_pending = TRUE;
        show_pending_messages (chat);
+#endif /* ENABLE_TPL */
 }
 
 static void
@@ -1622,7 +2456,7 @@ empathy_chat_class_init (EmpathyChatClass *klass)
                                         g_param_spec_object ("account",
                                                              "Account of the chat",
                                                              "The account of the chat",
-                                                             EMPATHY_TYPE_ACCOUNT,
+                                                             TP_TYPE_ACCOUNT,
                                                              G_PARAM_READABLE |
                                                              G_PARAM_STATIC_STRINGS));
        g_object_class_install_property (object_class,
@@ -1699,6 +2533,34 @@ chat_block_events_timeout_cb (gpointer data)
        return FALSE;
 }
 
+static void
+account_manager_prepared_cb (GObject *source_object,
+                            GAsyncResult *result,
+                            gpointer user_data)
+{
+       GList *accounts, *l;
+       TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
+       EmpathyChat *chat = user_data;
+       GError *error = NULL;
+
+       if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
+               DEBUG ("Failed to prepare the account manager: %s", error->message);
+               g_error_free (error);
+               return;
+       }
+
+       accounts = tp_account_manager_get_valid_accounts (account_manager);
+
+       for (l = accounts; l != NULL; l = l->next) {
+               TpAccount *account = l->data;
+               empathy_signal_connect_weak (account, "status-changed",
+                                            G_CALLBACK (chat_new_connection_cb),
+                                            G_OBJECT (chat));
+       }
+
+       g_list_free (accounts);
+}
+
 static void
 empathy_chat_init (EmpathyChat *chat)
 {
@@ -1706,16 +2568,18 @@ empathy_chat_init (EmpathyChat *chat)
                EMPATHY_TYPE_CHAT, EmpathyChatPriv);
 
        chat->priv = priv;
+#ifndef ENABLE_TPL
        priv->log_manager = empathy_log_manager_dup_singleton ();
+#else
+       priv->log_manager = tpl_log_manager_dup_singleton ();
+#endif /* ENABLE_TPL */
        priv->contacts_width = -1;
-       priv->sent_messages = NULL;
-       priv->sent_messages_index = -1;
-       priv->account_manager = empathy_account_manager_dup_singleton ();
+       priv->input_history = NULL;
+       priv->input_history_current = NULL;
+       priv->account_manager = tp_account_manager_dup ();
 
-       g_signal_connect (priv->account_manager,
-                         "new-connection",
-                         G_CALLBACK (chat_new_connection_cb),
-                         chat);
+       tp_account_manager_prepare_async (priv->account_manager, NULL,
+                                         account_manager_prepared_cb, chat);
 
        empathy_conf_get_bool (empathy_conf_get (),
                               EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS,
@@ -1729,6 +2593,8 @@ empathy_chat_init (EmpathyChat *chat)
        /* Add nick name completion */
        priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
        g_completion_set_compare (priv->completion, chat_contacts_completion_func);
+
+       chat_create_ui (chat);
 }
 
 EmpathyChat *
@@ -1747,12 +2613,169 @@ empathy_chat_get_tp_chat (EmpathyChat *chat)
        return priv->tp_chat;
 }
 
+static void display_password_info_bar (EmpathyChat *self,
+                                      gboolean retry);
+
+static void
+provide_password_cb (GObject *tp_chat,
+                    GAsyncResult *res,
+                    gpointer user_data)
+{
+       EmpathyChat *self = EMPATHY_CHAT (user_data);
+       EmpathyChatPriv *priv = GET_PRIV (self);
+       GError *error = NULL;
+
+       if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
+                                                     &error)) {
+               DEBUG ("error: %s", error->message);
+               /* FIXME: what should we do if that's another error? Close the channel?
+                * Display the raw D-Bus error to the user isn't very useful */
+               if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED))
+                       display_password_info_bar (self, TRUE);
+               g_error_free (error);
+               return;
+       }
+
+       /* Room joined */
+       gtk_widget_set_sensitive (priv->hpaned, TRUE);
+       gtk_widget_grab_focus (self->input_text_view);
+}
+
+static void
+password_infobar_response_cb (GtkWidget *info_bar,
+                             gint response_id,
+                             EmpathyChat *self)
+{
+       EmpathyChatPriv *priv = GET_PRIV (self);
+       GtkWidget *entry;
+       const gchar *password;
+
+       if (response_id != GTK_RESPONSE_OK)
+               goto out;
+
+       entry = g_object_get_data (G_OBJECT (info_bar), "password-entry");
+       g_assert (entry != NULL);
+
+       password = gtk_entry_get_text (GTK_ENTRY (entry));
+
+       empathy_tp_chat_provide_password_async (priv->tp_chat, password,
+                                               provide_password_cb, self);
+
+ out:
+       gtk_widget_destroy (info_bar);
+}
+
+static void
+password_entry_activate_cb (GtkWidget *entry,
+                         GtkWidget *info_bar)
+{
+       gtk_info_bar_response (GTK_INFO_BAR (info_bar), GTK_RESPONSE_OK);
+}
+
+static void
+passwd_join_button_cb (GtkButton *button,
+                         GtkWidget *info_bar)
+{
+       gtk_info_bar_response (GTK_INFO_BAR (info_bar), GTK_RESPONSE_OK);
+}
+
+static void
+display_password_info_bar (EmpathyChat *self,
+                          gboolean retry)
+{
+       EmpathyChatPriv *priv = GET_PRIV (self);
+       GtkWidget *info_bar;
+       GtkWidget *content_area;
+       GtkWidget *hbox;
+       GtkWidget *image;
+       GtkWidget *label;
+       GtkWidget *entry;
+       GtkWidget *alig;
+       GtkWidget *button;
+       GtkMessageType type;
+       const gchar *msg, *button_label;
+
+       if (retry) {
+               /* Previous password was wrong */
+               type = GTK_MESSAGE_ERROR;
+               msg = _("Wrong password; please try again:");
+               button_label = _("Retry");
+       }
+       else {
+               /* First time we're trying to join */
+               type = GTK_MESSAGE_QUESTION;
+               msg = _("This room is protected by a password:");
+               button_label = _("Join");
+       }
+
+       info_bar = gtk_info_bar_new ();
+       gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), type);
+
+       content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
+
+       hbox = gtk_hbox_new (FALSE, 3);
+       gtk_container_add (GTK_CONTAINER (content_area), hbox);
+
+       /* Add image */
+       image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
+                                         GTK_ICON_SIZE_DIALOG);
+       gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
+
+       /* Add message */
+       label = gtk_label_new (msg);
+       gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
+
+       /* Add password entry */
+       entry = gtk_entry_new ();
+       gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
+       gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
+
+       g_signal_connect (entry, "activate",
+                         G_CALLBACK (password_entry_activate_cb), info_bar);
+
+       /* Focus the password entry once it's realized */
+       g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
+
+       /* Add 'Join' button */
+       alig = gtk_alignment_new (0, 0.5, 0, 0);
+
+       button = gtk_button_new_with_label (button_label);
+       gtk_container_add (GTK_CONTAINER (alig), button);
+       gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
+
+       g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
+                         info_bar);
+
+       g_object_set_data (G_OBJECT (info_bar), "password-entry", entry);
+
+       gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
+                           FALSE, FALSE, 3);
+       gtk_widget_show_all (hbox);
+
+       g_signal_connect (info_bar, "response",
+                         G_CALLBACK (password_infobar_response_cb), self);
+
+       gtk_widget_show_all (info_bar);
+}
+
+static void
+chat_password_needed_changed_cb (EmpathyChat *self)
+{
+       EmpathyChatPriv *priv = GET_PRIV (self);
+
+       if (empathy_tp_chat_password_needed (priv->tp_chat)) {
+               display_password_info_bar (self, FALSE);
+               gtk_widget_set_sensitive (priv->hpaned, FALSE);
+       }
+}
+
 void
 empathy_chat_set_tp_chat (EmpathyChat   *chat,
                          EmpathyTpChat *tp_chat)
 {
        EmpathyChatPriv *priv = GET_PRIV (chat);
        TpConnection    *connection;
+       GPtrArray       *properties;
 
        g_return_if_fail (EMPATHY_IS_CHAT (chat));
        g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
@@ -1768,10 +2791,7 @@ empathy_chat_set_tp_chat (EmpathyChat   *chat,
 
        priv->tp_chat = g_object_ref (tp_chat);
        connection = empathy_tp_chat_get_connection (priv->tp_chat);
-       priv->account = empathy_account_manager_get_account_for_connection (
-                                                            priv->account_manager,
-                                                            connection);
-       g_object_ref (priv->account);
+       priv->account = g_object_ref (empathy_get_account_for_connection (connection));
 
        g_signal_connect (tp_chat, "destroy",
                          G_CALLBACK (chat_destroy_cb),
@@ -1791,9 +2811,34 @@ empathy_chat_set_tp_chat (EmpathyChat   *chat,
        g_signal_connect (tp_chat, "members-changed",
                          G_CALLBACK (chat_members_changed_cb),
                          chat);
+       g_signal_connect (tp_chat, "member-renamed",
+                         G_CALLBACK (chat_member_renamed_cb),
+                         chat);
        g_signal_connect_swapped (tp_chat, "notify::remote-contact",
                                  G_CALLBACK (chat_remote_contact_changed_cb),
                                  chat);
+       g_signal_connect_swapped (tp_chat, "notify::password-needed",
+                                 G_CALLBACK (chat_password_needed_changed_cb),
+                                 chat);
+
+       /* Get initial value of properties */
+       properties = empathy_tp_chat_get_properties (priv->tp_chat);
+       if (properties != NULL) {
+               guint i;
+
+               for (i = 0; i < properties->len; i++) {
+                       EmpathyTpChatProperty *property;
+
+                       property = g_ptr_array_index (properties, i);
+                       if (property->value == NULL)
+                               continue;
+
+                       chat_property_changed_cb (priv->tp_chat,
+                                                 property->name,
+                                                 property->value,
+                                                 chat);
+               }
+       }
 
        chat_remote_contact_changed_cb (chat);
 
@@ -1812,9 +2857,12 @@ empathy_chat_set_tp_chat (EmpathyChat   *chat,
         * the pending messages to be show when it's set on the object after it has
         * been created */
        show_pending_messages (chat);
+
+       /* check if a password is needed */
+       chat_password_needed_changed_cb (chat);
 }
 
-EmpathyAccount *
+TpAccount *
 empathy_chat_get_account (EmpathyChat *chat)
 {
        EmpathyChatPriv *priv = GET_PRIV (chat);
@@ -1957,15 +3005,34 @@ empathy_chat_paste (EmpathyChat *chat)
 {
        GtkTextBuffer *buffer;
        GtkClipboard  *clipboard;
+       EmpathyChatPriv *priv;
 
        g_return_if_fail (EMPATHY_IS_CHAT (chat));
 
+       priv = GET_PRIV (chat);
+
+       if (priv->tp_chat == NULL ||
+           !GTK_WIDGET_IS_SENSITIVE (chat->input_text_view))
+               return;
+
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
        clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
 
        gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
 }
 
+void
+empathy_chat_find (EmpathyChat *chat)
+{
+       EmpathyChatPriv *priv;
+
+       g_return_if_fail (EMPATHY_IS_CHAT (chat));
+
+       priv = GET_PRIV (chat);
+
+       empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
+}
+
 void
 empathy_chat_correct_word (EmpathyChat  *chat,
                          GtkTextIter *start,
@@ -1995,3 +3062,31 @@ empathy_chat_is_room (EmpathyChat *chat)
        return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
 }
 
+guint
+empathy_chat_get_nb_unread_messages (EmpathyChat *self)
+{
+       EmpathyChatPriv *priv = GET_PRIV (self);
+
+       g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
+
+       return priv->unread_messages;
+}
+
+/* called when the messages have been read by user */
+void
+empathy_chat_messages_read (EmpathyChat *self)
+{
+       EmpathyChatPriv *priv = GET_PRIV (self);
+
+       g_return_if_fail (EMPATHY_IS_CHAT (self));
+
+       /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
+        * definition. If we are still retrieving the backlogs, do not ACK */
+       if (priv->retrieving_backlogs)
+               return;
+
+       if (priv->tp_chat != NULL ) {
+                       empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
+       }
+       priv->unread_messages = 0;
+}