]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-chat.c
Do not use mission_control_request_channel as we don't use MC's dispatcher anymore...
[empathy.git] / libempathy-gtk / empathy-chat.c
index 0328c4ee90a4d10488e135560c95609c55f068c9..48aa793d5f8f1bfbe09e412a4d7b4ead5f5b1b17 100644 (file)
@@ -39,7 +39,6 @@
 
 #include <libempathy/empathy-log-manager.h>
 #include <libempathy/empathy-contact-list.h>
-#include <libempathy/empathy-debug.h>
 #include <libempathy/empathy-utils.h>
 
 #include "empathy-chat.h"
 #include "empathy-spell-dialog.h"
 #include "empathy-contact-list-store.h"
 #include "empathy-contact-list-view.h"
+#include "empathy-contact-menu.h"
 #include "empathy-ui-utils.h"
 
-#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CHAT, EmpathyChatPriv))
-
-#define DEBUG_DOMAIN "Chat"
+#define DEBUG_FLAG EMPATHY_DEBUG_CHAT
+#include <libempathy/empathy-debug.h>
 
 #define CHAT_DIR_CREATE_MODE  (S_IRUSR | S_IWUSR | S_IXUSR)
 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
 #define MAX_INPUT_HEIGHT 150
 #define COMPOSING_STOP_TIMEOUT 5
 
-struct _EmpathyChatPriv {
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
+typedef struct {
        EmpathyTpChat     *tp_chat;
        McAccount         *account;
        gchar             *id;
        gchar             *name;
        gchar             *subject;
+       EmpathyContact    *remote_contact;
 
        EmpathyLogManager *log_manager;
        MissionControl    *mc;
        GSList            *sent_messages;
        gint               sent_messages_index;
        GList             *compositors;
-       GList             *backlog_messages;
        GCompletion       *completion;
        guint              composing_stop_timeout_id;
        guint              block_events_timeout_id;
        TpHandleType       handle_type;
+       gpointer           token;
+       gint               contacts_width;
+       gboolean           has_input_vscroll;
 
        GtkWidget         *widget;
        GtkWidget         *hpaned;
@@ -86,21 +89,8 @@ struct _EmpathyChatPriv {
        GtkWidget         *scrolled_window_contacts;
        GtkWidget         *hbox_topic;
        GtkWidget         *label_topic;
-       EmpathyContactListView *view;
-       EmpathyContactListStore *store;
-
-       /* Used to automatically shrink a window that has temporarily
-        * grown due to long input. 
-        */
-       gint               padding_height;
-       gint               default_window_height;
-       gint               last_input_height;
-       gboolean           vscroll_visible;
-       gboolean           is_first_char;
-};
-
-static void empathy_chat_class_init (EmpathyChatClass *klass);
-static void empathy_chat_init       (EmpathyChat      *chat);
+       GtkWidget         *contact_list_view;
+} EmpathyChatPriv;
 
 enum {
        COMPOSING,
@@ -115,6 +105,7 @@ enum {
        PROP_ID,
        PROP_NAME,
        PROP_SUBJECT,
+       PROP_REMOTE_CONTACT,
 };
 
 static guint signals[LAST_SIGNAL] = { 0 };
@@ -145,6 +136,9 @@ chat_get_property (GObject    *object,
        case PROP_SUBJECT:
                g_value_set_string (value, priv->subject);
                break;
+       case PROP_REMOTE_CONTACT:
+               g_value_set_object (value, priv->remote_contact);
+               break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
                break;
@@ -184,15 +178,18 @@ chat_status_changed_cb (MissionControl           *mc,
 
        if (status == TP_CONNECTION_STATUS_CONNECTED && !priv->tp_chat &&
            empathy_account_equal (account, priv->account) &&
-           priv->handle_type != 0) {
-               empathy_debug (DEBUG_DOMAIN,
-                              "Account reconnected, request a new Text channel");
-               mission_control_request_channel_with_string_handle (mc,
-                                                                   priv->account,
-                                                                   TP_IFACE_CHANNEL_TYPE_TEXT,
-                                                                   priv->id,
-                                                                   priv->handle_type,
-                                                                   NULL, NULL);
+           priv->handle_type != TP_HANDLE_TYPE_NONE) {
+               TpConnection *connection;
+
+               DEBUG ("Account reconnected, request a new Text channel");
+               connection = mission_control_get_tpconnection (mc, account, NULL);
+               tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
+               empathy_connection_request_channel (connection, -1,
+                                                   TP_IFACE_CHANNEL_TYPE_TEXT,
+                                                   priv->handle_type,
+                                                   priv->id, TRUE,
+                                                   NULL, NULL, NULL, NULL);
+               g_object_unref (connection);
        }
 
        g_object_unref (account);
@@ -258,23 +255,6 @@ chat_composing_stop (EmpathyChat *chat)
                                   TP_CHANNEL_CHAT_STATE_ACTIVE);
 }
 
-static void
-chat_destroy_cb (EmpathyTpChat *tp_chat,
-                EmpathyChat    *chat)
-{
-       EmpathyChatPriv *priv;
-
-       priv = GET_PRIV (chat);
-
-       if (priv->tp_chat) {
-               g_object_unref (priv->tp_chat);
-               priv->tp_chat = NULL;
-       }
-
-       empathy_chat_view_append_event (chat->view, _("Disconnected"));
-       gtk_widget_set_sensitive (chat->input_text_view, FALSE);
-}
-
 static void 
 chat_sent_message_add (EmpathyChat  *chat,
                       const gchar *str)
@@ -322,8 +302,7 @@ chat_sent_message_get_next (EmpathyChat *chat)
        priv = GET_PRIV (chat);
 
        if (!priv->sent_messages) {
-               empathy_debug (DEBUG_DOMAIN, 
-                             "No sent messages, next message is NULL");
+               DEBUG ("No sent messages, next message is NULL");
                return NULL;
        }
 
@@ -333,9 +312,7 @@ chat_sent_message_get_next (EmpathyChat *chat)
                priv->sent_messages_index++;
        }
        
-       empathy_debug (DEBUG_DOMAIN, 
-                     "Returning next message index:%d",
-                     priv->sent_messages_index);
+       DEBUG ("Returning next message index:%d", priv->sent_messages_index);
 
        return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
 }
@@ -350,8 +327,7 @@ chat_sent_message_get_last (EmpathyChat *chat)
        priv = GET_PRIV (chat);
        
        if (!priv->sent_messages) {
-               empathy_debug (DEBUG_DOMAIN, 
-                             "No sent messages, last message is NULL");
+               DEBUG ("No sent messages, last message is NULL");
                return NULL;
        }
 
@@ -359,9 +335,7 @@ chat_sent_message_get_last (EmpathyChat *chat)
                priv->sent_messages_index--;
        }
 
-       empathy_debug (DEBUG_DOMAIN, 
-                     "Returning last message index:%d",
-                     priv->sent_messages_index);
+       DEBUG ("Returning last message index:%d", priv->sent_messages_index);
 
        return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
 }
@@ -413,8 +387,6 @@ chat_input_text_view_send (EmpathyChat *chat)
 
        chat_send (chat, msg);
        g_free (msg);
-
-       priv->is_first_char = TRUE;
 }
 
 static void
@@ -466,9 +438,9 @@ chat_state_changed_cb (EmpathyTpChat      *tp_chat,
                g_assert_not_reached ();
        }
 
-       empathy_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
-                     was_composing ? "yes" : "no",
-                     priv->compositors ? "yes" : "no");
+       DEBUG ("Was composing: %s now composing: %s",
+               was_composing ? "yes" : "no",
+               priv->compositors ? "yes" : "no");
 
        if ((was_composing && !priv->compositors) ||
            (!was_composing && priv->compositors)) {
@@ -483,48 +455,23 @@ chat_message_received_cb (EmpathyTpChat  *tp_chat,
                          EmpathyMessage *message,
                          EmpathyChat    *chat)
 {
-       EmpathyChatPriv *priv;
+       EmpathyChatPriv *priv = GET_PRIV (chat);
        EmpathyContact  *sender;
-       const gchar     *body;
-
-       priv = GET_PRIV (chat);
 
        sender = empathy_message_get_sender (message);
-       body = empathy_message_get_body (message);
-       while (priv->backlog_messages) {
-               EmpathyMessage *log_message;
-               EmpathyContact *log_sender;
-               const gchar    *log_body;
-
-               log_message = priv->backlog_messages->data;
-               log_sender = empathy_message_get_sender (log_message);
-               log_body = empathy_message_get_body (log_message);
-
-               priv->backlog_messages = g_list_remove (priv->backlog_messages,
-                                                       log_message);
-
-               if (empathy_contact_equal (sender, log_sender) &&
-                   !tp_strdiff (body, log_body)) {
-                       /* The message we received is already displayed because
-                        * some jabber chatrooms sends us back logs and we
-                        * already displayed it from localy logged messages. */
-                       empathy_debug (DEBUG_DOMAIN, "Skipping message because "
-                                      "it is already displayed from logged "
-                                      "messages");
-                       g_object_unref (log_message);
-                       return;
-               }
-               g_object_unref (log_message);
-       }
 
-       empathy_debug (DEBUG_DOMAIN, "Appending new message from %s (%d)",
-                      empathy_contact_get_name (sender),
-                      empathy_contact_get_handle (sender));
+       DEBUG ("Appending new message from %s (%d)",
+               empathy_contact_get_name (sender),
+               empathy_contact_get_handle (sender));
 
-       empathy_log_manager_add_message (priv->log_manager,
-                                        empathy_chat_get_id (chat),
-                                        FALSE,
-                                        message);
+       if (priv->id) {
+               gboolean is_chatroom;
+
+               is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
+               empathy_log_manager_add_message (priv->log_manager,
+                                                priv->id, is_chatroom,
+                                                message);
+       }
 
        empathy_chat_view_append_message (chat->view, message);
 
@@ -586,7 +533,12 @@ chat_property_changed_cb (EmpathyTpChat *tp_chat,
                priv->subject = g_value_dup_string (value);
                g_object_notify (G_OBJECT (chat), "subject");
 
-               gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
+               if (G_STR_EMPTY (priv->subject)) {
+                       gtk_widget_hide (priv->hbox_topic);
+               } else {
+                       gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
+                       gtk_widget_show (priv->hbox_topic);
+               }
                if (priv->block_events_timeout_id == 0) {
                        gchar *str;
 
@@ -649,27 +601,6 @@ chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
                              EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
                              &spell_checker);
 
-       if (priv->is_first_char) {
-               GtkRequisition  req;
-               gint            window_height;
-               GtkWindow      *dialog;
-               GtkAllocation  *allocation;
-
-               /* Save the window's size */
-               dialog = empathy_get_toplevel_window (GTK_WIDGET (chat));
-               if (dialog) {
-                       gtk_window_get_size (GTK_WINDOW (dialog), NULL, &window_height);
-                       gtk_widget_size_request (chat->input_text_view, &req);
-                       allocation = &GTK_WIDGET (chat->view)->allocation;
-
-                       priv->default_window_height = window_height;
-                       priv->last_input_height = req.height;
-                       priv->padding_height = window_height - req.height - allocation->height;
-               }
-
-               priv->is_first_char = FALSE;
-       }
-
        gtk_text_buffer_get_start_iter (buffer, &start);
 
        if (!spell_checker) {
@@ -892,121 +823,45 @@ chat_text_view_focus_in_event_cb (GtkWidget  *widget,
        return TRUE;
 }
 
-typedef struct {
-       GtkWindow *window;
-       gint       width;
-       gint       height;
-} ChangeSizeData;
-
 static gboolean
-chat_change_size_in_idle_cb (ChangeSizeData *data)
+chat_input_set_size_request_idle (gpointer sw)
 {
-       gtk_window_resize (data->window, data->width, data->height);
+       gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
 
        return FALSE;
 }
 
 static void
-chat_text_view_scroll_hide_cb (GtkWidget  *widget,
-                              EmpathyChat *chat)
-{
-       EmpathyChatPriv *priv;
-       GtkWidget      *sw;
-
-       priv = GET_PRIV (chat);
-
-       priv->vscroll_visible = FALSE;
-       g_signal_handlers_disconnect_by_func (widget,
-                                             chat_text_view_scroll_hide_cb,
-                                             chat);
-
-       sw = gtk_widget_get_parent (chat->input_text_view);
-       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
-                                       GTK_POLICY_NEVER,
-                                       GTK_POLICY_NEVER);
-       g_object_set (sw, "height-request", -1, NULL);
-}
-
-static void
-chat_text_view_size_allocate_cb (GtkWidget     *widget,
-                                GtkAllocation *allocation,
-                                EmpathyChat    *chat)
+chat_input_size_request_cb (GtkWidget      *widget,
+                           GtkRequisition *requisition,
+                           EmpathyChat    *chat)
 {
-       EmpathyChatPriv *priv;
-       gint            width;
-       GtkWindow      *dialog;
-       ChangeSizeData *data;
-       gint            window_height;
-       gint            new_height;
-       GtkAllocation  *view_allocation;
-       gint            current_height;
-       gint            diff;
-       GtkWidget      *sw;
-
-       priv = GET_PRIV (chat);
-
-       if (priv->default_window_height <= 0) {
-               return;
-       }
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       GtkWidget       *sw;
 
        sw = gtk_widget_get_parent (widget);
-       if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
-               GtkWidget *vscroll;
-
-               priv->vscroll_visible = TRUE;
-               gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
+       if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
+               g_idle_add (chat_input_set_size_request_idle, sw);
                gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
                                                GTK_POLICY_NEVER,
-                                               GTK_POLICY_AUTOMATIC);
-               vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
-               g_signal_connect (vscroll, "hide",
-                                 G_CALLBACK (chat_text_view_scroll_hide_cb),
-                                 chat);
-       }
-
-       if (priv->last_input_height <= allocation->height) {
-               priv->last_input_height = allocation->height;
-               return;
-       }
-
-       diff = priv->last_input_height - allocation->height;
-       priv->last_input_height = allocation->height;
-
-       view_allocation = &GTK_WIDGET (chat->view)->allocation;
-
-       dialog = empathy_get_toplevel_window (GTK_WIDGET (widget));
-       gtk_window_get_size (dialog, NULL, &current_height);
-
-       new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
-
-       if (new_height <= priv->default_window_height) {
-               window_height = priv->default_window_height;
-       } else {
-               window_height = new_height;
+                                               GTK_POLICY_ALWAYS);
+               priv->has_input_vscroll = TRUE;
        }
 
-       if (current_height <= window_height) {
-               return;
+       if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
+               gtk_widget_set_size_request (sw, -1, -1);
+               gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
+                                               GTK_POLICY_NEVER,
+                                               GTK_POLICY_NEVER);
+               priv->has_input_vscroll = FALSE;
        }
-
-       /* Restore the window's size */
-       gtk_window_get_size (dialog, &width, NULL);
-
-       data = g_new0 (ChangeSizeData, 1);
-       data->window = dialog;
-       data->width  = width;
-       data->height = window_height;
-
-       g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
-                        (GSourceFunc) chat_change_size_in_idle_cb,
-                        data, g_free);
 }
 
 static void
-chat_text_view_realize_cb (GtkWidget  *widget,
-                          EmpathyChat *chat)
+chat_input_realize_cb (GtkWidget   *widget,
+                      EmpathyChat *chat)
 {
-       empathy_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
+       DEBUG ("Setting focus to the input text view");
        gtk_widget_grab_focus (widget);
 }
 
@@ -1068,15 +923,22 @@ chat_text_check_word_spelling_cb (GtkMenuItem     *menuitem,
                                  EmpathyChatSpell *chat_spell)
 {
        empathy_spell_dialog_show (chat_spell->chat,
-                                 chat_spell->start,
-                                 chat_spell->end,
+                                 &chat_spell->start,
+                                 &chat_spell->end,
                                  chat_spell->word);
 }
 
 static void
-chat_text_populate_popup_cb (GtkTextView *view,
-                            GtkMenu     *menu,
-                            EmpathyChat  *chat)
+chat_text_send_cb (GtkMenuItem *menuitem,
+                  EmpathyChat *chat)
+{
+       chat_input_text_view_send (chat);
+}
+
+static void
+chat_input_populate_popup_cb (GtkTextView *view,
+                             GtkMenu     *menu,
+                             EmpathyChat *chat)
 {
        EmpathyChatPriv  *priv;
        GtkTextBuffer   *buffer;
@@ -1088,15 +950,20 @@ chat_text_populate_popup_cb (GtkTextView *view,
        gchar           *str = NULL;
        EmpathyChatSpell *chat_spell;
        GtkWidget       *smiley_menu;
+       GtkWidget       *image;
 
        priv = GET_PRIV (chat);
+       buffer = gtk_text_view_get_buffer (view);
 
        /* Add the emoticon menu. */
        item = gtk_separator_menu_item_new ();
        gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
        gtk_widget_show (item);
 
-       item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
+       item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
+       image = gtk_image_new_from_icon_name ("face-smile",
+                                             GTK_ICON_SIZE_MENU);
+       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
        gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
        gtk_widget_show (item);
 
@@ -1105,71 +972,79 @@ chat_text_populate_popup_cb (GtkTextView *view,
                chat);
        gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
 
+       /* Add the Send menu item. */
+       gtk_text_buffer_get_bounds (buffer, &start, &end);
+       str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
+       if (!G_STR_EMPTY (str)) {
+               item = gtk_menu_item_new_with_mnemonic (_("_Send"));
+               g_signal_connect (G_OBJECT (item), "activate",
+                                 G_CALLBACK (chat_text_send_cb), chat);
+               gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
+               gtk_widget_show (item);
+       }
+       str = NULL;
+
        /* Add the spell check menu item. */
-       buffer = gtk_text_view_get_buffer (view);
        table = gtk_text_buffer_get_tag_table (buffer);
-
        tag = gtk_text_tag_table_lookup (table, "misspelled");
-
        gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
-
        gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
                                               GTK_TEXT_WINDOW_WIDGET,
                                               x, y,
                                               &x, &y);
-
        gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
-
        start = end = iter;
-
        if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
            gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
 
                str = gtk_text_buffer_get_text (buffer,
                                                &start, &end, FALSE);
        }
-
-       if (G_STR_EMPTY (str)) {
-               return;
+       if (!G_STR_EMPTY (str)) {
+               chat_spell = chat_spell_new (chat, str, start, end);
+               g_object_set_data_full (G_OBJECT (menu),
+                                       "chat_spell", chat_spell,
+                                       (GDestroyNotify) chat_spell_free);
+
+               item = gtk_separator_menu_item_new ();
+               gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
+               gtk_widget_show (item);
+
+               item = gtk_image_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
+               image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
+                                                     GTK_ICON_SIZE_MENU);
+               gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
+               g_signal_connect (item,
+                                 "activate",
+                                 G_CALLBACK (chat_text_check_word_spelling_cb),
+                                 chat_spell);
+               gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
+               gtk_widget_show (item);
        }
-
-       chat_spell = chat_spell_new (chat, str, start, end);
-
-       g_object_set_data_full (G_OBJECT (menu),
-                               "chat_spell", chat_spell,
-                               (GDestroyNotify) chat_spell_free);
-
-       item = gtk_separator_menu_item_new ();
-       gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
-       gtk_widget_show (item);
-
-       item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
-       g_signal_connect (item,
-                         "activate",
-                         G_CALLBACK (chat_text_check_word_spelling_cb),
-                         chat_spell);
-       gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
-       gtk_widget_show (item);
 }
 
 static void
 chat_add_logs (EmpathyChat *chat)
 {
-       EmpathyChatPriv *priv;
-       GList          *messages, *l;
-       guint           num_messages;
-       guint           i;
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       gboolean         is_chatroom;
+       GList           *messages, *l;
+       guint            num_messages;
+       guint            i;
 
-       priv = GET_PRIV (chat);
+       if (!priv->id) {
+               return;
+       }
 
        /* Turn off scrolling temporarily */
        empathy_chat_view_scroll (chat->view, FALSE);
 
        /* Add messages from last conversation */
+       is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
        messages = empathy_log_manager_get_last_messages (priv->log_manager,
                                                          priv->account,
-                                                         empathy_chat_get_id (chat),
-                                                         FALSE);
+                                                         priv->id,
+                                                         is_chatroom);
        num_messages  = g_list_length (messages);
 
        /* Only keep the 10 last messages */
@@ -1183,8 +1058,9 @@ chat_add_logs (EmpathyChat *chat)
 
        for (l = messages; l; l = l->next) {
                empathy_chat_view_append_message (chat->view, l->data);
+               g_object_unref (l->data);
        }
-       priv->backlog_messages = messages;
+       g_list_free (messages);
 
        /* Turn back on scrolling */
        empathy_chat_view_scroll (chat->view, TRUE);
@@ -1198,6 +1074,13 @@ chat_contacts_completion_func (const gchar *s1,
        gchar *tmp, *nick1, *nick2;
        gint   ret;
 
+       if (s1 == s2) {
+               return 0;
+       }
+       if (!s1 || !s2) {
+               return s1 ? -1 : +1;
+       }
+
        tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
        nick1 = g_utf8_casefold (tmp, -1);
        g_free (tmp);
@@ -1244,6 +1127,117 @@ chat_members_changed_cb (EmpathyTpChat  *tp_chat,
        }
 }
 
+static gboolean
+chat_reset_size_request (gpointer widget)
+{
+       gtk_widget_set_size_request (widget, -1, -1);
+
+       return FALSE;
+}
+
+static void
+chat_set_show_contacts (EmpathyChat *chat, gboolean show)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+
+       if (!priv->scrolled_window_contacts) {
+               return;
+       }
+
+       if (show) {
+               EmpathyContactListStore *store;
+               gint                     min_width;
+
+               /* We are adding the contact list to the chat, we don't want the
+                * chat view to become too small. If the chat view is already
+                * smaller than 250 make sure that size won't change. If the
+                * 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_set_size_request (priv->vbox_left, min_width, -1);
+               g_idle_add (chat_reset_size_request, priv->vbox_left);
+
+               if (priv->contacts_width > 0) {
+                       gtk_paned_set_position (GTK_PANED (priv->hpaned),
+                                               priv->contacts_width);
+               }
+
+               store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
+               priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
+                       EMPATHY_CONTACT_LIST_FEATURE_NONE,
+                       EMPATHY_CONTACT_FEATURE_CHAT |
+                       EMPATHY_CONTACT_FEATURE_CALL |
+                       EMPATHY_CONTACT_FEATURE_LOG |
+                       EMPATHY_CONTACT_FEATURE_INFO));
+               gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
+                                  priv->contact_list_view);
+               gtk_widget_show (priv->contact_list_view);
+               gtk_widget_show (priv->scrolled_window_contacts);
+               g_object_unref (store);
+       } else {
+               priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
+               gtk_widget_hide (priv->scrolled_window_contacts);
+               if (priv->contact_list_view) {
+                       gtk_widget_destroy (priv->contact_list_view);
+                       priv->contact_list_view = NULL;
+               }
+       }
+}
+
+static void
+chat_remote_contact_changed_cb (EmpathyChat *chat)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+
+       if (priv->remote_contact) {
+               g_object_unref (priv->remote_contact);
+               priv->remote_contact = NULL;
+       }
+
+       priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
+       if (priv->remote_contact) {
+               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) {
+               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_set_show_contacts (chat, priv->remote_contact == NULL);
+
+       g_object_notify (G_OBJECT (chat), "remote-contact");
+       g_object_notify (G_OBJECT (chat), "id");
+}
+
+static void
+chat_destroy_cb (EmpathyTpChat *tp_chat,
+                EmpathyChat   *chat)
+{
+       EmpathyChatPriv *priv;
+
+       priv = GET_PRIV (chat);
+
+       if (!priv->tp_chat) {
+               return;
+       }
+
+       g_object_unref (priv->tp_chat);
+       priv->tp_chat = NULL;
+       g_object_notify (G_OBJECT (chat), "tp-chat");
+
+       empathy_chat_view_append_event (chat->view, _("Disconnected"));
+       gtk_widget_set_sensitive (chat->input_text_view, FALSE);
+       chat_set_show_contacts (chat, FALSE);
+}
+
 static void
 chat_create_ui (EmpathyChat *chat)
 {
@@ -1288,17 +1282,17 @@ chat_create_ui (EmpathyChat *chat)
                                              "left-margin", 2,
                                              "wrap-mode", GTK_WRAP_WORD_CHAR,
                                              NULL);
-       g_signal_connect (chat->input_text_view, "key_press_event",
+       g_signal_connect (chat->input_text_view, "key-press-event",
                          G_CALLBACK (chat_input_key_press_event_cb),
                          chat);
-       g_signal_connect (chat->input_text_view, "size_allocate",
-                         G_CALLBACK (chat_text_view_size_allocate_cb),
+       g_signal_connect (chat->input_text_view, "size-request",
+                         G_CALLBACK (chat_input_size_request_cb),
                          chat);
        g_signal_connect (chat->input_text_view, "realize",
-                         G_CALLBACK (chat_text_view_realize_cb),
+                         G_CALLBACK (chat_input_realize_cb),
                          chat);
-       g_signal_connect (chat->input_text_view, "populate_popup",
-                         G_CALLBACK (chat_text_populate_popup_cb),
+       g_signal_connect (chat->input_text_view, "populate-popup",
+                         G_CALLBACK (chat_input_populate_popup_cb),
                          chat);
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
        g_signal_connect (buffer, "changed",
@@ -1312,17 +1306,10 @@ chat_create_ui (EmpathyChat *chat)
        gtk_widget_show (chat->input_text_view);
 
        /* Create contact list */
-       priv->store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
-       priv->view = empathy_contact_list_view_new (priv->store,
-                                                   EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CHAT |
-                                                   EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CALL |
-                                                   EMPATHY_CONTACT_LIST_FEATURE_CONTACT_LOG |
-                                                   EMPATHY_CONTACT_LIST_FEATURE_CONTACT_FT |
-                                                   EMPATHY_CONTACT_LIST_FEATURE_CONTACT_INVITE |
-                                                   EMPATHY_CONTACT_LIST_FEATURE_CONTACT_INFO);
-       gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
-                          GTK_WIDGET (priv->view));
-       gtk_widget_show (GTK_WIDGET (priv->view));
+       chat_set_show_contacts (chat, priv->remote_contact == NULL);
+
+       /* Initialy hide the topic, will be shown if not empty */
+       gtk_widget_hide (priv->hbox_topic);
 
        /* Set widget focus order */
        list = g_list_append (NULL, priv->scrolled_window_input);
@@ -1392,7 +1379,7 @@ chat_finalize (GObject *object)
        chat = EMPATHY_CHAT (object);
        priv = GET_PRIV (chat);
 
-       empathy_debug (DEBUG_DOMAIN, "Finalized: %p", object);
+       DEBUG ("Finalized: %p", object);
 
        g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
        g_slist_free (priv->sent_messages);
@@ -1400,25 +1387,21 @@ chat_finalize (GObject *object)
        g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
        g_list_free (priv->compositors);
 
-       g_list_foreach (priv->backlog_messages, (GFunc) g_object_unref, NULL);
-       g_list_free (priv->backlog_messages);
-
        chat_composing_remove_timeout (chat);
 
-       dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
-                                       G_CALLBACK (chat_status_changed_cb),
-                                       chat);
+       empathy_disconnect_account_status_changed (priv->token);
        g_object_unref (priv->mc);
        g_object_unref (priv->log_manager);
-       g_object_unref (priv->store);
 
        if (priv->tp_chat) {
                g_object_unref (priv->tp_chat);
        }
-
        if (priv->account) {
                g_object_unref (priv->account);
        }
+       if (priv->remote_contact) {
+               g_object_unref (priv->remote_contact);
+       }
 
        if (priv->block_events_timeout_id) {
                g_source_remove (priv->block_events_timeout_id);
@@ -1490,6 +1473,13 @@ empathy_chat_class_init (EmpathyChatClass *klass)
                                                              "The subject or topic of the chat",
                                                              NULL,
                                                              G_PARAM_READABLE));
+       g_object_class_install_property (object_class,
+                                        PROP_REMOTE_CONTACT,
+                                        g_param_spec_object ("remote-contact",
+                                                             "The remote contact",
+                                                             "The remote contact is any",
+                                                             EMPATHY_TYPE_CONTACT,
+                                                             G_PARAM_READABLE));
 
        signals[COMPOSING] =
                g_signal_new ("composing",
@@ -1527,17 +1517,17 @@ chat_block_events_timeout_cb (gpointer data)
 static void
 empathy_chat_init (EmpathyChat *chat)
 {
-       EmpathyChatPriv *priv = GET_PRIV (chat);
+       EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
+               EMPATHY_TYPE_CHAT, EmpathyChatPriv);
 
-       priv->is_first_char = TRUE;
+       chat->priv = priv;
        priv->log_manager = empathy_log_manager_new ();
-       priv->default_window_height = -1;
-       priv->vscroll_visible = FALSE;
+       priv->contacts_width = -1;
        priv->sent_messages = NULL;
        priv->sent_messages_index = -1;
        priv->mc = empathy_mission_control_new ();
 
-       empathy_connect_to_account_status_changed (priv->mc,
+       priv->token = empathy_connect_to_account_status_changed (priv->mc,
                                                   G_CALLBACK (chat_status_changed_cb),
                                                   chat, NULL);
 
@@ -1571,28 +1561,21 @@ void
 empathy_chat_set_tp_chat (EmpathyChat   *chat,
                          EmpathyTpChat *tp_chat)
 {
-       EmpathyChatPriv *priv;
+       EmpathyChatPriv *priv = GET_PRIV (chat);
 
        g_return_if_fail (EMPATHY_IS_CHAT (chat));
        g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
        g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
 
-       priv = GET_PRIV (chat);
-
-       if (tp_chat == priv->tp_chat) {
+       if (priv->tp_chat) {
                return;
        }
 
-       if (priv->tp_chat) {
-               g_object_unref (priv->tp_chat);
-       }
        if (priv->account) {
                g_object_unref (priv->account);
        }
-       g_free (priv->id);
 
        priv->tp_chat = g_object_ref (tp_chat);
-       priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
        priv->account = g_object_ref (empathy_tp_chat_get_account (tp_chat));
 
        g_signal_connect (tp_chat, "message-received",
@@ -1610,10 +1593,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_swapped (tp_chat, "notify::remote-contact",
+                                 G_CALLBACK (chat_remote_contact_changed_cb),
+                                 chat);
        g_signal_connect (tp_chat, "destroy",
                          G_CALLBACK (chat_destroy_cb),
                          chat);
 
+       chat_remote_contact_changed_cb (chat);
+
        if (chat->input_text_view) {
                gtk_widget_set_sensitive (chat->input_text_view, TRUE);
                if (priv->block_events_timeout_id == 0) {
@@ -1621,6 +1609,9 @@ empathy_chat_set_tp_chat (EmpathyChat   *chat,
                }
        }
 
+       empathy_tp_chat_set_acknowledge (priv->tp_chat, TRUE);
+       empathy_tp_chat_emit_pendings (priv->tp_chat);
+
        g_object_notify (G_OBJECT (chat), "tp-chat");
        g_object_notify (G_OBJECT (chat), "id");
        g_object_notify (G_OBJECT (chat), "account");
@@ -1666,6 +1657,54 @@ empathy_chat_get_subject (EmpathyChat *chat)
        return priv->subject;
 }
 
+EmpathyContact *
+empathy_chat_get_remote_contact (EmpathyChat *chat)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+
+       g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
+
+       return priv->remote_contact;
+}
+
+guint
+empathy_chat_get_members_count (EmpathyChat *chat)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+
+       g_return_val_if_fail (EMPATHY_IS_CHAT (chat), 0);
+
+       if (priv->tp_chat) {
+               return empathy_tp_chat_get_members_count (priv->tp_chat);
+       }
+
+       return 0;
+}
+
+GtkWidget *
+empathy_chat_get_contact_menu (EmpathyChat *chat)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       GtkWidget       *menu = NULL;
+
+       g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
+
+       if (priv->remote_contact) {
+               menu = empathy_contact_menu_new (priv->remote_contact,
+                                                EMPATHY_CONTACT_FEATURE_CALL |
+                                                EMPATHY_CONTACT_FEATURE_LOG |
+                                                EMPATHY_CONTACT_FEATURE_INFO);
+       }
+       else if (priv->contact_list_view) {
+               EmpathyContactListView *view;
+
+               view = EMPATHY_CONTACT_LIST_VIEW (priv->contact_list_view);
+               menu = empathy_contact_list_view_get_contact_menu (view);
+       }
+
+       return menu;
+}
+
 void
 empathy_chat_clear (EmpathyChat *chat)
 {
@@ -1737,8 +1776,8 @@ empathy_chat_paste (EmpathyChat *chat)
 
 void
 empathy_chat_correct_word (EmpathyChat  *chat,
-                         GtkTextIter  start,
-                         GtkTextIter  end,
+                         GtkTextIter *start,
+                         GtkTextIter *end,
                          const gchar *new_word)
 {
        GtkTextBuffer *buffer;
@@ -1748,8 +1787,8 @@ empathy_chat_correct_word (EmpathyChat  *chat,
 
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
 
-       gtk_text_buffer_delete (buffer, &start, &end);
-       gtk_text_buffer_insert (buffer, &start,
+       gtk_text_buffer_delete (buffer, start, end);
+       gtk_text_buffer_insert (buffer, start,
                                new_word,
                                -1);
 }