]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-chat.c
add myself to AUTHORS
[empathy.git] / libempathy-gtk / empathy-chat.c
index cfeb0073b62cca0440d5d17a9b8d17f8d9fbd1f7..dec4757dbb2f0bec5d001dd8849f06a658dd4853 100644 (file)
@@ -34,9 +34,9 @@
 #include <glib/gi18n-lib.h>
 #include <gtk/gtk.h>
 
+#include <telepathy-glib/account-manager.h>
 #include <telepathy-glib/util.h>
 
-#include <libempathy/empathy-account-manager.h>
 #include <libempathy/empathy-log-manager.h>
 #include <libempathy/empathy-contact-list.h>
 #include <libempathy/empathy-utils.h>
@@ -64,7 +64,7 @@
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
 typedef struct {
        EmpathyTpChat     *tp_chat;
-       EmpathyAccount    *account;
+       TpAccount         *account;
        gchar             *id;
        gchar             *name;
        gchar             *subject;
@@ -72,9 +72,9 @@ typedef struct {
        gboolean           show_contacts;
 
        EmpathyLogManager *log_manager;
-       EmpathyAccountManager *account_manager;
-       GSList            *sent_messages;
-       gint               sent_messages_index;
+       TpAccountManager  *account_manager;
+       GList             *input_history;
+       GList             *input_history_current;
        GList             *compositors;
        GCompletion       *completion;
        guint              composing_stop_timeout_id;
@@ -92,8 +92,21 @@ typedef struct {
        GtkWidget         *hbox_topic;
        GtkWidget         *label_topic;
        GtkWidget         *contact_list_view;
+       GtkWidget         *info_bar_vbox;
+
+       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;
 } 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 +209,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 +332,280 @@ 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;
+
+       if (list == NULL) {
+               DEBUG ("No input history");
+               return;
+       }
 
-       /* Save the sent message in our repeat buffer */
-       list = priv->sent_messages;
+       /* 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;
+       }
 
-       /* 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);
+       /* 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);
+               }
        }
 
-       /* 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);
+       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;
+
+       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 message */
-       list = g_slist_prepend (list, g_strdup (str));
 
-       /* Set list and reset the index */
-       priv->sent_messages = list;
-       priv->sent_messages_index = -1;
+
+       /* 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);
 
-       return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
+       DEBUG ("Returning next entry: '%s'", msg);
+
+       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->sent_messages_index >= 0) {
-               priv->sent_messages_index--;
+       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;
        }
 
-       DEBUG ("Returning last message index:%d", priv->sent_messages_index);
+       /* 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);
+       }
 
-       return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
+       g_free (text);
 }
 
 static void
@@ -508,6 +740,26 @@ chat_command_msg (EmpathyChat *chat,
        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)
@@ -564,6 +816,9 @@ static ChatCommandItem commands[] = {
        {"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 current server")},
+
        {"me", 2, 2, chat_command_me,
         N_("/me <message>, send an ACTION message to the current conversation")},
 
@@ -610,6 +865,9 @@ chat_command_help (EmpathyChat *chat,
                        return;
                }
        }
+
+       empathy_chat_view_append_event (chat->view,
+               _("Unknown command"));
 }
 
 static GStrv
@@ -680,7 +938,7 @@ chat_send (EmpathyChat  *chat,
 
        priv = GET_PRIV (chat);
 
-       chat_sent_message_add (chat, msg);
+       chat_input_history_add (chat, msg, FALSE);
 
        if (msg[0] == '/') {
                gboolean second_slash = FALSE;
@@ -689,9 +947,13 @@ chat_send (EmpathyChat  *chat,
                for (i = 0; i < G_N_ELEMENTS (commands); i++) {
                        GStrv strv;
                        guint strv_len;
+                       gchar c;
 
-                       if (!has_prefix_case (msg + 1, commands[i].prefix) ||
-                           !g_ascii_isspace (msg + 1 + strlen (commands[i].prefix))) {
+                       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;
                        }
 
@@ -754,6 +1016,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);
@@ -839,6 +1103,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);
 }
 
@@ -1025,11 +1290,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,
@@ -1209,7 +1475,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
@@ -1557,6 +1825,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;
 
@@ -1571,6 +1841,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)
 {
@@ -1580,17 +1874,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;
@@ -1601,7 +1898,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);
 
@@ -1640,7 +1938,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");
 }
@@ -1670,7 +1968,7 @@ chat_remote_contact_changed_cb (EmpathyChat *chat)
                g_object_get (channel, "handle-type", &priv->handle_type, NULL);
        }
 
-       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");
@@ -1695,9 +1993,22 @@ 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);
+
+       chat_update_contacts_visibility (chat, FALSE);
 }
 
+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
 show_pending_messages (EmpathyChat *chat) {
        EmpathyChatPriv *priv = GET_PRIV (chat);
@@ -1706,6 +2017,9 @@ show_pending_messages (EmpathyChat *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)) {
@@ -1723,6 +2037,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");
@@ -1735,11 +2050,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);
@@ -1782,6 +2104,17 @@ chat_create_ui (EmpathyChat *chat)
        /* 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);
        gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
@@ -1814,7 +2147,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;
 
@@ -1833,11 +2166,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));
@@ -1859,17 +2192,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);
 
@@ -1914,8 +2244,11 @@ static void
 chat_constructed (GObject *object)
 {
        EmpathyChat *chat = EMPATHY_CHAT (object);
+       EmpathyChatPriv *priv = GET_PRIV (chat);
 
-       chat_add_logs (chat);
+       if (priv->handle_type != TP_HANDLE_TYPE_ROOM)
+               chat_add_logs (chat);
+       priv->can_show_pending = TRUE;
        show_pending_messages (chat);
 }
 
@@ -1947,7 +2280,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,
@@ -2024,6 +2357,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)
 {
@@ -2033,14 +2394,12 @@ empathy_chat_init (EmpathyChat *chat)
        chat->priv = priv;
        priv->log_manager = empathy_log_manager_dup_singleton ();
        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,
@@ -2074,6 +2433,162 @@ 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)
@@ -2096,10 +2611,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),
@@ -2119,9 +2631,15 @@ 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);
@@ -2159,9 +2677,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);
@@ -2349,3 +2870,23 @@ 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), FALSE);
+
+       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));
+
+       priv->unread_messages = 0;
+}