]> git.0d.be Git - empathy.git/blobdiff - src/empathy-chat-window.c
Remove useless include of empathy-contact-factory.h
[empathy.git] / src / empathy-chat-window.c
index 6dd55cbae8b39ab2fe0554f5323fa340deff0ba4..a6ca638b88d1b91d47e23dc700da138358213902 100644 (file)
 
 #include <gtk/gtk.h>
 #include <gdk/gdkkeysyms.h>
-#include <glade/glade.h>
 #include <glib/gi18n.h>
+#include <libnotify/notification.h>
 
+#include <telepathy-glib/util.h>
 #include <libmissioncontrol/mission-control.h>
 
-#include <libempathy/empathy-contact-factory.h>
 #include <libempathy/empathy-contact.h>
 #include <libempathy/empathy-message.h>
-#include <libempathy/empathy-debug.h>
+#include <libempathy/empathy-dispatcher.h>
+#include <libempathy/empathy-chatroom-manager.h>
 #include <libempathy/empathy-utils.h>
 
 #include <libempathy-gtk/empathy-images.h>
 #include <libempathy-gtk/empathy-contact-dialogs.h>
 #include <libempathy-gtk/empathy-log-window.h>
 #include <libempathy-gtk/empathy-geometry.h>
+#include <libempathy-gtk/empathy-smiley-manager.h>
 #include <libempathy-gtk/empathy-ui-utils.h>
 
 #include "empathy-chat-window.h"
 #include "empathy-about-dialog.h"
+#include "empathy-misc.h"
 
-#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv))
-
-#define DEBUG_DOMAIN "ChatWindow"
+#define DEBUG_FLAG EMPATHY_DEBUG_CHAT
+#include <libempathy/empathy-debug.h>
 
-struct _EmpathyChatWindowPriv {
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatWindow)
+typedef struct {
        EmpathyChat *current_chat;
        GList       *chats;
        GList       *chats_new_msg;
@@ -64,32 +67,27 @@ struct _EmpathyChatWindowPriv {
        gboolean     page_added;
        gboolean     dnd_same_window;
        guint        save_geometry_id;
+       EmpathyChatroomManager *chatroom_manager;
        GtkWidget   *dialog;
        GtkWidget   *notebook;
+       NotifyNotification *notification;
 
        /* Menu items. */
-       GtkWidget   *menu_conv_clear;
-       GtkWidget   *menu_conv_insert_smiley;
-       GtkWidget   *menu_conv_contact;
-       GtkWidget   *menu_conv_close;
-
-       GtkWidget   *menu_edit_cut;
-       GtkWidget   *menu_edit_copy;
-       GtkWidget   *menu_edit_paste;
-
-       GtkWidget   *menu_tabs_next;
-       GtkWidget   *menu_tabs_prev;
-       GtkWidget   *menu_tabs_left;
-       GtkWidget   *menu_tabs_right;
-       GtkWidget   *menu_tabs_detach;
-       
-       GtkWidget   *menu_help_contents;
-       GtkWidget   *menu_help_about;
-};
-
-static void       empathy_chat_window_class_init (EmpathyChatWindowClass *klass);
-static void       empathy_chat_window_init       (EmpathyChatWindow      *window);
-static void       chat_window_finalize           (GObject                *object);
+       GtkUIManager *ui_manager;
+       GtkAction   *menu_conv_insert_smiley;
+       GtkAction   *menu_conv_contact;
+       GtkAction   *menu_conv_favorite;
+
+       GtkAction   *menu_edit_cut;
+       GtkAction   *menu_edit_copy;
+       GtkAction   *menu_edit_paste;
+
+       GtkAction   *menu_tabs_next;
+       GtkAction   *menu_tabs_prev;
+       GtkAction   *menu_tabs_left;
+       GtkAction   *menu_tabs_right;
+       GtkAction   *menu_tabs_detach;
+} EmpathyChatWindowPriv;
 
 static GList *chat_windows = NULL;
 
@@ -153,7 +151,7 @@ chat_window_find_chat (EmpathyChat *chat)
 }
 
 static void
-chat_window_close_clicked_cb (GtkWidget  *button,
+chat_window_close_clicked_cb (GtkAction   *action,
                              EmpathyChat *chat)
 {
        EmpathyChatWindow *window;
@@ -226,6 +224,7 @@ chat_window_create_label (EmpathyChatWindow *window,
 
        close_button = gtk_button_new ();
        gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
+       g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
 
        /* We don't want focus/keynav for the button to avoid clutter, and
         * Ctrl-W works anyway.
@@ -263,65 +262,43 @@ chat_window_create_label (EmpathyChatWindow *window,
 }
 
 static void
-chat_window_update_menu (EmpathyChatWindow *window)
+chat_window_update (EmpathyChatWindow *window)
 {
-       EmpathyChatWindowPriv *priv;
-       gboolean              first_page;
-       gboolean              last_page;
-       gboolean              is_connected;
-       gint                  num_pages;
-       gint                  page_num;
-
-       priv = GET_PRIV (window);
+       EmpathyChatWindowPriv *priv = GET_PRIV (window);
+       gboolean               first_page;
+       gboolean               last_page;
+       gboolean               is_connected;
+       gint                   num_pages;
+       gint                   page_num;
+       gint                   i;
+       const gchar           *name;
+       guint                  n_chats;
+       GdkPixbuf             *icon;
+       EmpathyContact        *remote_contact;
+       gboolean               avatar_in_icon;
+       GtkWidget             *chat;
+       GtkWidget             *chat_close_button;
 
-       /* Notebook pages */
+       /* Get information */
        page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
        num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
        first_page = (page_num == 0);
        last_page = (page_num == (num_pages - 1));
-
-       gtk_widget_set_sensitive (priv->menu_tabs_next, !last_page);
-       gtk_widget_set_sensitive (priv->menu_tabs_prev, !first_page);
-       gtk_widget_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
-       gtk_widget_set_sensitive (priv->menu_tabs_left, !first_page);
-       gtk_widget_set_sensitive (priv->menu_tabs_right, !last_page);
-
        is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
-       gtk_widget_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
-}
-
-static const gchar *
-chat_window_get_chat_name (EmpathyChat *chat)
-{
-       EmpathyTpChat  *tp_chat;
-       EmpathyContact *remote_contact = NULL;
-       const gchar    *name = NULL;
-
-       name = empathy_chat_get_name (chat);
-       if (!name) {
-               tp_chat = empathy_chat_get_tp_chat (chat);
-               if (tp_chat) {
-                       remote_contact = empathy_tp_chat_get_remote_contact (tp_chat);
-               }
-               if (remote_contact) {
-                       name = empathy_contact_get_name (remote_contact);
-               } else {
-                       name = _("Conversation");
-               }
-       }
+       name = empathy_chat_get_name (priv->current_chat);
+       n_chats = g_list_length (priv->chats);
 
-       return name;
-}
+       DEBUG ("Update window");
 
-static void
-chat_window_update_title (EmpathyChatWindow *window)
-{
-       EmpathyChatWindowPriv *priv = GET_PRIV (window);
-       const gchar           *name;
-       guint                  n_chats;
+       /* Update menu */
+       gtk_action_set_sensitive (priv->menu_tabs_next, !last_page);
+       gtk_action_set_sensitive (priv->menu_tabs_prev, !first_page);
+       gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
+       gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
+       gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
+       gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
 
-       name = chat_window_get_chat_name (priv->current_chat);
-       n_chats = g_list_length (priv->chats);
+       /* Update window title */
        if (n_chats == 1) {
                gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
        } else {
@@ -331,38 +308,72 @@ chat_window_update_title (EmpathyChatWindow *window)
                gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
                g_free (title);
        }
+
+       /* Update window icon */
        if (priv->chats_new_msg) {
                gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
                                          EMPATHY_IMAGE_MESSAGE);
        } else {
-               gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
+               empathy_conf_get_bool (empathy_conf_get (),
+                                      EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
+                                      &avatar_in_icon);
+
+               if (n_chats == 1 && avatar_in_icon) {
+                       remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
+                       icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
+                       gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
+
+                       if (icon != NULL) {
+                               g_object_unref (icon);
+                       }
+               } else {
+                       gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
+               }
+       }
+
+       if (num_pages == 1) {
+               chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
+               chat_close_button = g_object_get_data (G_OBJECT (chat),
+                               "chat-window-tab-close-button");
+               gtk_widget_hide (chat_close_button);
+       } else {
+               for (i=0; i<num_pages; i++) {
+                       chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
+                       chat_close_button = g_object_get_data (G_OBJECT (chat),
+                                       "chat-window-tab-close-button");
+                       gtk_widget_show (chat_close_button);
+               }
        }
 }
 
 static void
-chat_window_update_chat (EmpathyChat *chat)
+chat_window_update_chat_tab (EmpathyChat *chat)
 {
        EmpathyChatWindow     *window;
        EmpathyChatWindowPriv *priv;
-       EmpathyTpChat         *tp_chat;
-       EmpathyContact        *remote_contact = NULL;
+       EmpathyContact        *remote_contact;
        const gchar           *name;
+       McAccount             *account;
        const gchar           *subject;
        GtkWidget             *widget;
        GString               *tooltip;
-       gchar                 *str;
+       gchar                 *markup;
        const gchar           *icon_name;
 
        window = chat_window_find_chat (chat);
+       if (!window) {
+               return;
+       }
        priv = GET_PRIV (window);
 
        /* Get information */
-       name = chat_window_get_chat_name (chat);
+       name = empathy_chat_get_name (chat);
+       account = empathy_chat_get_account (chat);
        subject = empathy_chat_get_subject (chat);
-       tp_chat = empathy_chat_get_tp_chat (chat);
-       if (tp_chat) {
-               remote_contact = empathy_tp_chat_get_remote_contact (tp_chat);
-       }
+       remote_contact = empathy_chat_get_remote_contact (chat);
+
+       DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
+               name, mc_account_get_unique_name (account), subject, remote_contact);
 
        /* Update tab image */
        if (g_list_find (priv->chats_new_msg, chat)) {
@@ -381,47 +392,56 @@ chat_window_update_chat (EmpathyChat *chat)
 
        /* Update tab tooltip */
        tooltip = g_string_new (NULL);
+
        if (remote_contact) {
-               g_string_append_printf (tooltip, "%s\n%s",
-                                       empathy_contact_get_id (remote_contact),
-                                       empathy_contact_get_status (remote_contact));
+               markup = g_markup_printf_escaped ("<b>%s</b><small> (%s)</small>\n%s",
+                                                 empathy_contact_get_id (remote_contact),
+                                                 mc_account_get_display_name (account),
+                                                 empathy_contact_get_status (remote_contact));
+               g_string_append (tooltip, markup);
+               g_free (markup);
        }
        else {
-               g_string_append (tooltip, name);
+               markup = g_markup_printf_escaped ("<b>%s</b><small>  (%s)</small>", name,
+                                                 mc_account_get_display_name (account));
+               g_string_append (tooltip, markup);
+               g_free (markup);
        }
+
        if (subject) {
-               g_string_append_printf (tooltip, "\n%s %s", _("Topic:"), subject);
+               markup = g_markup_printf_escaped ("\n<b>%s</b> %s", _("Topic:"), subject);
+               g_string_append (tooltip, markup);
+               g_free (markup);
        }
        if (g_list_find (priv->chats_composing, chat)) {
-               g_string_append_printf (tooltip, "\n%s", _("Typing a message."));
+               markup = g_markup_printf_escaped ("\n%s", _("Typing a message."));
+               g_string_append (tooltip, markup);
+               g_free (markup);
        }
-       str = g_string_free (tooltip, FALSE);
+
+       markup = g_string_free (tooltip, FALSE);
        widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
-       gtk_widget_set_tooltip_text (widget, str);
-       g_free (str);
+       gtk_widget_set_tooltip_markup (widget, markup);
+       g_free (markup);
 
        /* Update tab label */
        widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
        gtk_label_set_text (GTK_LABEL (widget), name);
 
-       /* Update window title? */
+       /* Update the window if it's the current chat */
        if (priv->current_chat == chat) {
-               chat_window_update_title (window);
+               chat_window_update (window);
        }
 }
 
 static void
-chat_window_remote_contact_notify_cb (EmpathyChat *chat)
+chat_window_chat_notify_cb (EmpathyChat *chat)
 {
-       EmpathyTpChat  *tp_chat;
        EmpathyContact *old_remote_contact;
        EmpathyContact *remote_contact = NULL;
 
        old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
-       tp_chat = empathy_chat_get_tp_chat (chat);
-       if (tp_chat) {
-               remote_contact = empathy_tp_chat_get_remote_contact (tp_chat);
-       }
+       remote_contact = empathy_chat_get_remote_contact (chat);
 
        if (old_remote_contact != remote_contact) {
                /* The remote-contact associated with the chat changed, we need
@@ -429,128 +449,133 @@ chat_window_remote_contact_notify_cb (EmpathyChat *chat)
                 * window each time. */
                if (remote_contact) {
                        g_signal_connect_swapped (remote_contact, "notify",
-                                                 G_CALLBACK (chat_window_update_chat),
+                                                 G_CALLBACK (chat_window_update_chat_tab),
                                                  chat);
                }
                if (old_remote_contact) {
                        g_signal_handlers_disconnect_by_func (old_remote_contact,
-                                                             chat_window_update_chat,
+                                                             chat_window_update_chat_tab,
                                                              chat);
                }
 
                g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
                                   remote_contact);
-
-               chat_window_update_chat (chat);
        }
+
+       chat_window_update_chat_tab (chat);
 }
 
 static void
-chat_window_weak_ref_cb (gpointer  data,
-                        GObject  *chat)
+chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
+                                      EmpathySmiley        *smiley,
+                                      gpointer              window)
 {
-       EmpathyTpChat  *tp_chat;
-       EmpathyContact *remote_contact;
+       EmpathyChatWindowPriv *priv = GET_PRIV (window);
+       EmpathyChat           *chat;
+       GtkTextBuffer         *buffer;
+       GtkTextIter            iter;
 
-       tp_chat = g_object_get_data (chat, "chat-window-tp-chat");
-       if (tp_chat) {
-               g_signal_handlers_disconnect_by_func (tp_chat,
-                                                     chat_window_remote_contact_notify_cb,
-                                                     chat);
-       }
+       chat = priv->current_chat;
 
-       remote_contact = g_object_get_data (chat, "chat-window-remote-contact");
-       if (remote_contact) {
-               g_signal_handlers_disconnect_by_func (remote_contact,
-                                                     chat_window_update_chat,
-                                                     chat);
-       }
+       buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
+       gtk_text_buffer_get_end_iter (buffer, &iter);
+       gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
 }
 
 static void
-chat_window_chat_notify_cb (EmpathyChat *chat)
+chat_window_conv_activate_cb (GtkAction         *action,
+                             EmpathyChatWindow *window)
 {
-       EmpathyTpChat *tp_chat;
-       EmpathyTpChat *old_tp_chat;
-
-       old_tp_chat = g_object_get_data (G_OBJECT (chat), "chat-window-tp-chat");
-       tp_chat = empathy_chat_get_tp_chat (chat);
-
-       if (old_tp_chat != tp_chat) {
-               /* The TpChat associated with the chat has changed, we need to
-                * keep track of it's remote-contact if there is one. */
-               if (tp_chat) {
-                       g_signal_connect_swapped (tp_chat, "notify::remote-contact",
-                                                 G_CALLBACK (chat_window_remote_contact_notify_cb),
-                                                 chat);
-                       g_object_weak_ref (G_OBJECT (chat),
-                                          chat_window_weak_ref_cb,
-                                          NULL);
-               }
-               if (old_tp_chat) {
-                       g_signal_handlers_disconnect_by_func (old_tp_chat,
-                                                             chat_window_remote_contact_notify_cb,
-                                                             chat);
-               }
-               g_object_set_data (G_OBJECT (chat), "chat-window-tp-chat", tp_chat);
-
-               /* This will call chat_window_update_chat() if the remote-contact
-                * changed, so we don't have to call it again. That's why we
-                * return here. */
-               chat_window_remote_contact_notify_cb (chat);
-               return;
+       EmpathyChatWindowPriv *priv = GET_PRIV (window);
+       GtkWidget             *menu = NULL;
+       GtkWidget             *submenu = NULL;
+       gboolean               is_room;
+
+       /* Contact submenu */
+       submenu = empathy_chat_get_contact_menu (priv->current_chat);
+       if (submenu) {
+               menu = gtk_ui_manager_get_widget (priv->ui_manager,
+                       "/chats_menubar/menu_conv/menu_conv_contact");
+               gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
+               gtk_widget_show (submenu);
        }
-
-       chat_window_update_chat (chat);
+       gtk_action_set_visible (priv->menu_conv_contact, submenu != NULL);
+
+       /* Favorite room menu */
+       is_room = empathy_chat_is_room (priv->current_chat);
+       if (is_room) {
+               const gchar *room;
+               McAccount   *account;
+               gboolean     found;
+
+               room = empathy_chat_get_id (priv->current_chat);
+               account = empathy_chat_get_account (priv->current_chat);
+               found = empathy_chatroom_manager_find (priv->chatroom_manager,
+                                                      account, room) != NULL;
+
+               DEBUG ("This room %s favorite", found ? "is" : "is not");
+               gtk_toggle_action_set_active (
+                       GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
+       }
+       gtk_action_set_visible (priv->menu_conv_favorite, is_room);
 }
 
 static void
-chat_window_insert_smiley_activate_cb (GtkWidget         *menuitem,
-                                      EmpathyChatWindow *window)
+chat_window_clear_activate_cb (GtkAction         *action,
+                              EmpathyChatWindow *window)
 {
-       EmpathyChatWindowPriv *priv;
-       EmpathyChat           *chat;
-       GtkTextBuffer        *buffer;
-       GtkTextIter           iter;
-       const gchar          *smiley;
-
-       priv = GET_PRIV (window);
-
-       chat = priv->current_chat;
-
-       smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
+       EmpathyChatWindowPriv *priv = GET_PRIV (window);
 
-       buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
-       gtk_text_buffer_get_end_iter (buffer, &iter);
-       gtk_text_buffer_insert (buffer, &iter,
-                               smiley, -1);
+       empathy_chat_clear (priv->current_chat);
 }
 
 static void
-chat_window_clear_activate_cb (GtkWidget        *menuitem,
-                              EmpathyChatWindow *window)
+chat_window_favorite_toggled_cb (GtkToggleAction   *toggle_action,
+                                EmpathyChatWindow *window)
 {
-       EmpathyChatWindowPriv *priv;
+       EmpathyChatWindowPriv *priv = GET_PRIV (window);
+       gboolean               active;
+       McAccount             *account;
+       const gchar           *room;
+       EmpathyChatroom       *chatroom;
 
-       priv = GET_PRIV (window);
+       active = gtk_toggle_action_get_active (toggle_action);
+       account = empathy_chat_get_account (priv->current_chat);
+       room = empathy_chat_get_id (priv->current_chat);
 
-       empathy_chat_clear (priv->current_chat);
+       chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
+                                                 account, room);
+
+       if (active && !chatroom) {
+               const gchar *name;
+
+               name = empathy_chat_get_name (priv->current_chat);
+               chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
+               empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
+               g_object_unref (chatroom);
+               return;
+       }
+       
+       if (!active && chatroom) {
+               empathy_chatroom_manager_remove (priv->chatroom_manager, chatroom);
+       }
 }
 
 static const gchar *
 chat_get_window_id_for_geometry (EmpathyChat *chat)
 {
-       gboolean separate_windows;
+       const gchar *res = NULL;
+       gboolean     separate_windows;
 
        empathy_conf_get_bool (empathy_conf_get (),
                               EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
                               &separate_windows);
 
        if (separate_windows) {
-               return empathy_chat_get_id (chat);
-       } else {
-               return "chat-window";
+               res = empathy_chat_get_id (chat);
        }
+
+       return res ? res : "chat-window";
 }
 
 static gboolean
@@ -594,7 +619,7 @@ chat_window_configure_event_cb (GtkWidget         *widget,
 }
 
 static void
-chat_window_close_activate_cb (GtkWidget        *menuitem,
+chat_window_close_activate_cb (GtkAction         *action,
                               EmpathyChatWindow *window)
 {
        EmpathyChatWindowPriv *priv;
@@ -607,7 +632,7 @@ chat_window_close_activate_cb (GtkWidget        *menuitem,
 }
 
 static void
-chat_window_edit_activate_cb (GtkWidget        *menuitem,
+chat_window_edit_activate_cb (GtkAction         *action,
                              EmpathyChatWindow *window)
 {
        EmpathyChatWindowPriv *priv;
@@ -620,33 +645,32 @@ chat_window_edit_activate_cb (GtkWidget        *menuitem,
        g_return_if_fail (priv->current_chat != NULL);
 
        if (!empathy_chat_get_tp_chat (priv->current_chat)) {
-               gtk_widget_set_sensitive (priv->menu_edit_copy, FALSE);
-               gtk_widget_set_sensitive (priv->menu_edit_cut, FALSE);
-               gtk_widget_set_sensitive (priv->menu_edit_paste, FALSE);
+               gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
+               gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
+               gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
                return;
        }
 
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
-       if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
-               gtk_widget_set_sensitive (priv->menu_edit_copy, TRUE);
-               gtk_widget_set_sensitive (priv->menu_edit_cut, TRUE);
+       if (gtk_text_buffer_get_has_selection (buffer)) {
+               gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
+               gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
        } else {
                gboolean selection;
 
-               selection = empathy_chat_view_get_selection_bounds (priv->current_chat->view, 
-                                                                  NULL, NULL);
+               selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
 
-               gtk_widget_set_sensitive (priv->menu_edit_cut, FALSE);
-               gtk_widget_set_sensitive (priv->menu_edit_copy, selection);
+               gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
+               gtk_action_set_sensitive (priv->menu_edit_copy, selection);
        }
 
        clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
        text_available = gtk_clipboard_wait_is_text_available (clipboard);
-       gtk_widget_set_sensitive (priv->menu_edit_paste, text_available);
+       gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
 }
 
 static void
-chat_window_cut_activate_cb (GtkWidget        *menuitem,
+chat_window_cut_activate_cb (GtkAction         *action,
                             EmpathyChatWindow *window)
 {
        EmpathyChatWindowPriv *priv;
@@ -659,7 +683,7 @@ chat_window_cut_activate_cb (GtkWidget        *menuitem,
 }
 
 static void
-chat_window_copy_activate_cb (GtkWidget        *menuitem,
+chat_window_copy_activate_cb (GtkAction         *action,
                              EmpathyChatWindow *window)
 {
        EmpathyChatWindowPriv *priv;
@@ -672,7 +696,7 @@ chat_window_copy_activate_cb (GtkWidget        *menuitem,
 }
 
 static void
-chat_window_paste_activate_cb (GtkWidget        *menuitem,
+chat_window_paste_activate_cb (GtkAction         *action,
                               EmpathyChatWindow *window)
 {
        EmpathyChatWindowPriv *priv;
@@ -685,7 +709,7 @@ chat_window_paste_activate_cb (GtkWidget        *menuitem,
 }
 
 static void
-chat_window_tabs_left_activate_cb (GtkWidget        *menuitem,
+chat_window_tabs_left_activate_cb (GtkAction         *action,
                                   EmpathyChatWindow *window)
 {
        EmpathyChatWindowPriv *priv;
@@ -706,7 +730,7 @@ chat_window_tabs_left_activate_cb (GtkWidget        *menuitem,
 }
 
 static void
-chat_window_tabs_right_activate_cb (GtkWidget        *menuitem,
+chat_window_tabs_right_activate_cb (GtkAction         *action,
                                    EmpathyChatWindow *window)
 {
        EmpathyChatWindowPriv *priv;
@@ -724,7 +748,7 @@ chat_window_tabs_right_activate_cb (GtkWidget        *menuitem,
 }
 
 static void
-chat_window_detach_activate_cb (GtkWidget        *menuitem,
+chat_window_detach_activate_cb (GtkAction         *action,
                                EmpathyChatWindow *window)
 {
        EmpathyChatWindowPriv *priv;
@@ -743,15 +767,17 @@ chat_window_detach_activate_cb (GtkWidget        *menuitem,
 }
 
 static void
-chat_window_help_contents_cb (GtkWidget         *menuitem,
-                             EmpathyChatWindow *window)
+chat_window_help_contents_activate_cb (GtkAction         *action,
+                                      EmpathyChatWindow *window)
 {
-       //empathy_help_show ();
+       EmpathyChatWindowPriv *priv = GET_PRIV (window);
+
+       empathy_url_show (priv->dialog, "ghelp:empathy?chat");
 }
 
 static void
-chat_window_help_about_cb (GtkWidget         *menuitem,
-                          EmpathyChatWindow *window)
+chat_window_help_about_activate_cb (GtkAction         *action,
+                                   EmpathyChatWindow *window)
 {
        EmpathyChatWindowPriv *priv = GET_PRIV (window);
 
@@ -763,21 +789,15 @@ chat_window_delete_event_cb (GtkWidget        *dialog,
                             GdkEvent         *event,
                             EmpathyChatWindow *window)
 {
-       EmpathyChatWindowPriv *priv;
-       GList                *list;
-       GList                *l;
-
-       priv = GET_PRIV (window);
-
-       empathy_debug (DEBUG_DOMAIN, "Delete event received");
+       EmpathyChatWindowPriv *priv = GET_PRIV (window);
 
-       list = g_list_copy (priv->chats);
+       DEBUG ("Delete event received");
 
-       for (l = list; l; l = l->next) {
-               empathy_chat_window_remove_chat (window, l->data);
+       g_object_ref (window);
+       while (priv->chats) {
+               empathy_chat_window_remove_chat (window, priv->chats->data);
        }
-
-       g_list_free (list);
+       g_object_unref (window);
 
        return TRUE;
 }
@@ -797,7 +817,7 @@ chat_window_composing_cb (EmpathyChat       *chat,
                priv->chats_composing = g_list_remove (priv->chats_composing, chat);
        }
 
-       chat_window_update_chat (chat);
+       chat_window_update_chat_tab (chat);
 }
 
 static void
@@ -808,11 +828,90 @@ chat_window_set_urgency_hint (EmpathyChatWindow *window,
 
        priv = GET_PRIV (window);
 
-       empathy_debug (DEBUG_DOMAIN, "Turning %s urgency hint",
-                      urgent ? "on" : "off");
+       DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
        gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
 }
 
+typedef struct {
+       EmpathyChatWindow *window;
+       EmpathyChat *chat;
+} NotificationData;
+
+static void
+chat_window_notification_closed_cb (NotifyNotification *notify,
+                                   NotificationData *cb_data)
+{
+       EmpathyNotificationClosedReason reason = 0;
+       EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
+
+#ifdef notify_notification_get_closed_reason
+       reason = notify_notification_get_closed_reason (notify);
+#endif
+       if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
+               empathy_chat_window_present_chat (cb_data->chat);
+       }
+
+       g_object_unref (notify);
+       priv->notification = NULL;
+       g_object_unref (cb_data->chat);
+       g_slice_free (NotificationData, cb_data);
+}
+
+static void
+chat_window_show_or_update_notification (EmpathyChatWindow *window,
+                                        EmpathyMessage *message,
+                                        EmpathyChat *chat)
+{
+       EmpathyContact *sender;
+       char *header, *escaped;
+       const char *body;
+       GdkPixbuf *pixbuf;
+       NotificationData *cb_data;
+       EmpathyChatWindowPriv *priv = GET_PRIV (window);
+       gboolean res;
+
+       if (!empathy_notification_is_enabled ()) {
+               return;
+       } else {
+               empathy_conf_get_bool (empathy_conf_get (),
+                                      EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
+               if (!res) {
+                       return;
+               }
+       }
+
+       cb_data = g_slice_new0 (NotificationData);
+       cb_data->chat = g_object_ref (chat);
+       cb_data->window = window;
+
+       sender = empathy_message_get_sender (message);
+       header = g_strdup_printf (_("New message from %s"),
+                                 empathy_contact_get_name (sender));
+       body = empathy_message_get_body (message);
+       escaped = g_markup_escape_text (body, -1);
+
+       pixbuf = empathy_misc_get_pixbuf_for_notification (sender, EMPATHY_IMAGE_NEW_MESSAGE);
+
+       if (priv->notification != NULL) {
+               notify_notification_update (priv->notification,
+                                           header, escaped, NULL);
+               notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
+       } else {
+               priv->notification = notify_notification_new (header, escaped, NULL, NULL);
+               notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
+               notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
+
+               g_signal_connect (priv->notification, "closed",
+                                 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
+       }
+
+       notify_notification_show (priv->notification, NULL);
+
+       g_object_unref (pixbuf);
+       g_free (header);
+       g_free (escaped);
+}
+
 static void
 chat_window_new_message_cb (EmpathyChat       *chat,
                            EmpathyMessage    *message,
@@ -821,16 +920,50 @@ chat_window_new_message_cb (EmpathyChat       *chat,
        EmpathyChatWindowPriv *priv;
        gboolean              has_focus;
        gboolean              needs_urgency;
+       EmpathyContact        *sender;
 
        priv = GET_PRIV (window);
 
        has_focus = empathy_chat_window_has_focus (window);
-       
+
+       /* - if we're the sender, we play the sound if it's specified in the
+        *   preferences and we're not away.
+        * - if we receive a message, we play the sound if it's specified in the
+        *   prefereces and the window does not have focus on the chat receiving
+        *   the message.
+        */
+
+       sender = empathy_message_get_sender (message);
+
+       if (empathy_contact_is_user (sender) != FALSE) {
+               empathy_sound_play (GTK_WIDGET (priv->dialog),
+                                   EMPATHY_SOUND_MESSAGE_OUTGOING);
+       } else {
+               if ((!has_focus || priv->current_chat != chat)) {
+                       empathy_sound_play (GTK_WIDGET (priv->dialog),
+                                           EMPATHY_SOUND_MESSAGE_INCOMING);
+               }
+       }
+
+       if (!has_focus) {
+               chat_window_show_or_update_notification (window, message, chat);
+       }
+
        if (has_focus && priv->current_chat == chat) {
                return;
        }
-       
-       needs_urgency = empathy_message_should_highlight (message);
+
+       /* If empathy_chat_is_room() returns TRUE, that means it's a named MUC.
+        * If empathy_chat_get_remote_contact() returns NULL, that means it's
+        * an unamed MUC (msn-like).
+        * In case of a MUC, we set urgency only if the message contains our
+        * alias. */
+       if (empathy_chat_is_room (chat) ||
+           empathy_chat_get_remote_contact (chat) == NULL) {
+               needs_urgency = empathy_message_should_highlight (message);
+       } else {
+               needs_urgency = TRUE;
+       }
 
        if (needs_urgency && !has_focus) {
                chat_window_set_urgency_hint (window, TRUE);
@@ -838,7 +971,7 @@ chat_window_new_message_cb (EmpathyChat       *chat,
 
        if (!g_list_find (priv->chats_new_msg, chat)) {
                priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
-               chat_window_update_chat (chat);
+               chat_window_update_chat_tab (chat);
        }
 }
 
@@ -859,7 +992,7 @@ chat_window_detach_hook (GtkNotebook *source,
        new_window = empathy_chat_window_new ();
        priv = GET_PRIV (new_window);
 
-       empathy_debug (DEBUG_DOMAIN, "Detach hook called");
+       DEBUG ("Detach hook called");
 
        empathy_chat_window_move_chat (window, new_window, chat);
 
@@ -879,7 +1012,7 @@ chat_window_page_switched_cb (GtkNotebook      *notebook,
        EmpathyChat           *chat;
        GtkWidget            *child;
 
-       empathy_debug (DEBUG_DOMAIN, "Page switched");
+       DEBUG ("Page switched");
 
        priv = GET_PRIV (window);
 
@@ -897,20 +1030,7 @@ chat_window_page_switched_cb (GtkNotebook      *notebook,
        priv->current_chat = chat;
        priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
 
-       chat_window_update_menu (window);
-       chat_window_update_title (window);
-}
-
-static void
-chat_window_page_reordered_cb (GtkNotebook      *notebook,
-                              GtkWidget        *widget,
-                              guint             page_num,
-                              EmpathyChatWindow *window)
-{
-       empathy_debug (DEBUG_DOMAIN, "Page reordered");
-       
-       chat_window_update_menu (window);
-       chat_window_update_title (window);
+       chat_window_update_chat_tab (chat);
 }
 
 static void
@@ -929,12 +1049,12 @@ chat_window_page_added_cb (GtkNotebook      *notebook,
         * it, so we return here and in "page-added".
         */
        if (priv->dnd_same_window) {
-               empathy_debug (DEBUG_DOMAIN, "Page added (back to the same window)");
+               DEBUG ("Page added (back to the same window)");
                priv->dnd_same_window = FALSE;
                return;
        }
 
-       empathy_debug (DEBUG_DOMAIN, "Page added");
+       DEBUG ("Page added");
 
        /* Get chat object */
        chat = EMPATHY_CHAT (child);
@@ -954,6 +1074,8 @@ chat_window_page_added_cb (GtkNotebook      *notebook,
 
        /* Get list of chats up to date */
        priv->chats = g_list_append (priv->chats, chat);
+
+       chat_window_update_chat_tab (chat);
 }
 
 static void
@@ -972,11 +1094,11 @@ chat_window_page_removed_cb (GtkNotebook      *notebook,
         * it, so we return here and in "page-added".
         */
        if (priv->dnd_same_window) {
-               empathy_debug (DEBUG_DOMAIN, "Page removed (and will be readded to same window)");
+               DEBUG ("Page removed (and will be readded to same window)");
                return;
        }
 
-       empathy_debug (DEBUG_DOMAIN, "Page removed");
+       DEBUG ("Page removed");
 
        /* Get chat object */
        chat = EMPATHY_CHAT (child);
@@ -997,7 +1119,7 @@ chat_window_page_removed_cb (GtkNotebook      *notebook,
        if (priv->chats == NULL) {
                g_object_unref (window);
        } else {
-               chat_window_update_chat (chat);
+               chat_window_update (window);
        }
 }
 
@@ -1008,7 +1130,7 @@ chat_window_focus_in_event_cb (GtkWidget        *widget,
 {
        EmpathyChatWindowPriv *priv;
 
-       empathy_debug (DEBUG_DOMAIN, "Focus in event, updating title");
+       DEBUG ("Focus in event, updating title");
 
        priv = GET_PRIV (window);
 
@@ -1017,7 +1139,7 @@ chat_window_focus_in_event_cb (GtkWidget        *widget,
        chat_window_set_urgency_hint (window, FALSE);
        
        /* Update the title, since we now mark all unread messages as read. */
-       chat_window_update_chat (priv->current_chat);
+       chat_window_update_chat_tab (priv->current_chat);
 
        return FALSE;
 }
@@ -1036,21 +1158,19 @@ chat_window_drag_data_received (GtkWidget        *widget,
                EmpathyChat           *chat;
                EmpathyChatWindow     *old_window;
                McAccount             *account;
-               const gchar           *id = NULL;
+               const gchar           *id;
                gchar                **strv;
 
-               if (selection) {
-                       id = (const gchar*) selection->data;
-               }
+               id = (const gchar*) selection->data;
 
-               empathy_debug (DEBUG_DOMAIN, "DND contact from roster with id:'%s'", id);
+               DEBUG ("DND contact from roster with id:'%s'", id);
                
                strv = g_strsplit (id, "/", 2);
                account = mc_account_lookup (strv[0]);
                chat = empathy_chat_window_find_chat (account, strv[1]);
 
                if (!chat) {
-                       empathy_chat_with_contact_id (account, strv[2]);
+                       empathy_dispatcher_chat_with_contact_id (account, strv[2], NULL, NULL);
                        g_object_unref (account);
                        g_strfreev (strv);
                        return;
@@ -1081,28 +1201,21 @@ chat_window_drag_data_received (GtkWidget        *widget,
                gtk_drag_finish (context, TRUE, FALSE, time);
        }
        else if (info == DND_DRAG_TYPE_TAB) {
-               EmpathyChat        *chat = NULL;
-               EmpathyChatWindow  *old_window;
-               GtkWidget        **child = NULL;
+               EmpathyChat        **chat;
+               EmpathyChatWindow   *old_window = NULL;
 
-               empathy_debug (DEBUG_DOMAIN, "DND tab");
+               DEBUG ("DND tab");
 
-               if (selection) {
-                       child = (void*) selection->data;
-               }
+               chat = (void*) selection->data;
+               old_window = chat_window_find_chat (*chat);
 
-               if (child) {
-                       chat = EMPATHY_CHAT (child);
-               }
-
-               old_window = chat_window_find_chat (chat);
                if (old_window) {
                        EmpathyChatWindowPriv *priv;
 
                        priv = GET_PRIV (window);
 
                        if (old_window == window) {
-                               empathy_debug (DEBUG_DOMAIN, "DND tab (within same window)");
+                               DEBUG ("DND tab (within same window)");
                                priv->dnd_same_window = TRUE;
                                gtk_drag_finish (context, TRUE, FALSE, time);
                                return;
@@ -1118,7 +1231,7 @@ chat_window_drag_data_received (GtkWidget        *widget,
                 */
                gtk_drag_finish (context, TRUE, FALSE, time);
        } else {
-               empathy_debug (DEBUG_DOMAIN, "DND from unknown source");
+               DEBUG ("DND from unknown source");
                gtk_drag_finish (context, FALSE, FALSE, time);
        }
 }
@@ -1132,12 +1245,20 @@ chat_window_finalize (GObject *object)
        window = EMPATHY_CHAT_WINDOW (object);
        priv = GET_PRIV (window);
 
-       empathy_debug (DEBUG_DOMAIN, "Finalized: %p", object);
+       DEBUG ("Finalized: %p", object);
 
+       g_object_unref (priv->ui_manager);
+       g_object_unref (priv->chatroom_manager);
        if (priv->save_geometry_id != 0) {
                g_source_remove (priv->save_geometry_id);
        }
 
+       if (priv->notification != NULL) {
+               notify_notification_close (priv->notification, NULL);
+               g_object_unref (priv->notification);
+               priv->notification = NULL;
+       }
+
        chat_windows = g_list_remove (chat_windows, window);
        gtk_widget_destroy (priv->dialog);
 
@@ -1169,29 +1290,27 @@ empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
 static void
 empathy_chat_window_init (EmpathyChatWindow *window)
 {
-       EmpathyChatWindowPriv *priv;
-       GladeXML             *glade;
-       GtkAccelGroup        *accel_group;
-       GClosure             *closure;
-       GtkWidget            *menu_conv;
-       GtkWidget            *menu;
-       gint                  i;
-       GtkWidget            *chat_vbox;
-       gchar                *filename;
-
-       priv = GET_PRIV (window);
-
-       filename = empathy_file_lookup ("empathy-chat-window.glade", "src");
-       glade = empathy_glade_get_file (filename,
-                                      "chat_window",
-                                      NULL,
+       GtkBuilder            *gui;
+       GtkAccelGroup         *accel_group;
+       GClosure              *closure;
+       GtkWidget             *menu;
+       GtkWidget             *submenu;
+       gint                   i;
+       GtkWidget             *chat_vbox;
+       gchar                 *filename;
+       EmpathySmileyManager  *smiley_manager;
+       EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
+               EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
+
+       window->priv = priv;
+       filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
+       gui = empathy_builder_get_file (filename,
                                       "chat_window", &priv->dialog,
                                       "chat_vbox", &chat_vbox,
-                                      "menu_conv", &menu_conv,
-                                      "menu_conv_clear", &priv->menu_conv_clear,
+                                      "ui_manager", &priv->ui_manager,
                                       "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
                                       "menu_conv_contact", &priv->menu_conv_contact,
-                                      "menu_conv_close", &priv->menu_conv_close,
+                                      "menu_conv_favorite", &priv->menu_conv_favorite,
                                       "menu_edit_cut", &priv->menu_edit_cut,
                                       "menu_edit_copy", &priv->menu_edit_copy,
                                       "menu_edit_paste", &priv->menu_edit_paste,
@@ -1200,15 +1319,14 @@ empathy_chat_window_init (EmpathyChatWindow *window)
                                       "menu_tabs_left", &priv->menu_tabs_left,
                                       "menu_tabs_right", &priv->menu_tabs_right,
                                       "menu_tabs_detach", &priv->menu_tabs_detach,
-                                      "menu_help_contents", &priv->menu_help_contents,
-                                      "menu_help_about", &priv->menu_help_about,
                                       NULL);
        g_free (filename);
 
-       empathy_glade_connect (glade,
-                             window,
+       empathy_builder_connect (gui, window,
                              "chat_window", "configure-event", chat_window_configure_event_cb,
+                             "menu_conv", "activate", chat_window_conv_activate_cb,
                              "menu_conv_clear", "activate", chat_window_clear_activate_cb,
+                             "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
                              "menu_conv_close", "activate", chat_window_close_activate_cb,
                              "menu_edit", "activate", chat_window_edit_activate_cb,
                              "menu_edit_cut", "activate", chat_window_cut_activate_cb,
@@ -1217,11 +1335,14 @@ empathy_chat_window_init (EmpathyChatWindow *window)
                              "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
                              "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
                              "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
-                             "menu_help_contents", "activate", chat_window_help_contents_cb,
-                             "menu_help_about", "activate", chat_window_help_about_cb,
+                             "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
+                             "menu_help_about", "activate", chat_window_help_about_activate_cb,
                              NULL);
 
-       g_object_unref (glade);
+       g_object_ref (priv->ui_manager);
+       g_object_unref (gui);
+
+       priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
 
        priv->notebook = gtk_notebook_new ();
        gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow"); 
@@ -1246,12 +1367,16 @@ empathy_chat_window_init (EmpathyChatWindow *window)
        g_object_unref (accel_group);
 
        /* Set up smiley menu */
-       menu = empathy_chat_view_get_smiley_menu (
-               G_CALLBACK (chat_window_insert_smiley_activate_cb),
-               window);
-       gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->menu_conv_insert_smiley), menu);
-
-       /* Set up signals we can't do with glade since we may need to
+       smiley_manager = empathy_smiley_manager_dup_singleton ();
+       submenu = empathy_smiley_menu_new (smiley_manager,
+                                          chat_window_insert_smiley_activate_cb,
+                                          window);
+       menu = gtk_ui_manager_get_widget (priv->ui_manager,
+               "/chats_menubar/menu_conv/menu_conv_insert_smiley");
+       gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
+       g_object_unref (smiley_manager);
+
+       /* Set up signals we can't do with ui file since we may need to
         * block/unblock them at some later stage.
         */
 
@@ -1277,10 +1402,6 @@ empathy_chat_window_init (EmpathyChatWindow *window)
                                "switch_page",
                                G_CALLBACK (chat_window_page_switched_cb),
                                window);
-       g_signal_connect (priv->notebook,
-                         "page_reordered",
-                         G_CALLBACK (chat_window_page_reordered_cb),
-                         window);
        g_signal_connect (priv->notebook,
                          "page_added",
                          G_CALLBACK (chat_window_page_added_cb),
@@ -1343,7 +1464,7 @@ empathy_chat_window_get_default (void)
                chat_window = l->data;
 
                dialog = empathy_chat_window_get_dialog (chat_window);
-               if (empathy_window_get_is_visible (GTK_WINDOW (GTK_WINDOW (dialog)))) {
+               if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
                        /* Found a visible window on this desktop */
                        return chat_window;
                }
@@ -1381,24 +1502,28 @@ empathy_chat_window_add_chat (EmpathyChatWindow *window,
        /* Reference the chat object */
        g_object_ref (chat);
 
-       empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
-
-       if (x >= 0 && y >= 0) {
-               /* Let the window manager position it if we don't have
-                * good x, y coordinates.
-                */
-               gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
-       }
-
-       if (w > 0 && h > 0) {
-               /* Use the defaults from the glade file if we don't have
-                * good w, h geometry.
-                */
-               gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
+       /* If this window has just been created, position it */
+       if (priv->chats == NULL) {
+               empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
+               
+               if (x >= 0 && y >= 0) {
+                       /* Let the window manager position it if we don't have
+                        * good x, y coordinates.
+                        */
+                       gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
+               }
+               
+               if (w > 0 && h > 0) {
+                       /* Use the defaults from the ui file if we don't have
+                        * good w, h geometry.
+                        */
+                       gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
+               }
        }
 
        child = GTK_WIDGET (chat);
        label = chat_window_create_label (window, chat); 
+       gtk_widget_show (child);
 
        g_signal_connect (chat, "notify::name",
                          G_CALLBACK (chat_window_chat_notify_cb),
@@ -1406,7 +1531,7 @@ empathy_chat_window_add_chat (EmpathyChatWindow *window,
        g_signal_connect (chat, "notify::subject",
                          G_CALLBACK (chat_window_chat_notify_cb),
                          NULL);
-       g_signal_connect (chat, "notify::tp-chat",
+       g_signal_connect (chat, "notify::remote-contact",
                          G_CALLBACK (chat_window_chat_notify_cb),
                          NULL);
        chat_window_chat_notify_cb (chat);
@@ -1416,11 +1541,8 @@ empathy_chat_window_add_chat (EmpathyChatWindow *window,
        gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
        gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
                                            TRUE, TRUE, GTK_PACK_START); 
-       gtk_widget_show (child);
 
-       empathy_debug (DEBUG_DOMAIN, 
-                     "Chat added (%d references)",
-                     G_OBJECT (chat)->ref_count);
+       DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
 }
 
 void
@@ -1428,7 +1550,8 @@ empathy_chat_window_remove_chat (EmpathyChatWindow *window,
                                 EmpathyChat       *chat)
 {
        EmpathyChatWindowPriv *priv;
-       gint                  position;
+       gint                   position;
+       EmpathyContact        *remote_contact;
 
        g_return_if_fail (window != NULL);
        g_return_if_fail (EMPATHY_IS_CHAT (chat));
@@ -1438,14 +1561,19 @@ empathy_chat_window_remove_chat (EmpathyChatWindow *window,
        g_signal_handlers_disconnect_by_func (chat,
                                              chat_window_chat_notify_cb,
                                              NULL);
+       remote_contact = g_object_get_data (G_OBJECT (chat),
+                                           "chat-window-remote-contact");
+       if (remote_contact) {
+               g_signal_handlers_disconnect_by_func (remote_contact,
+                                                     chat_window_update_chat_tab,
+                                                     chat);
+       }
 
        position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
                                          GTK_WIDGET (chat));
        gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
 
-       empathy_debug (DEBUG_DOMAIN, 
-                     "Chat removed (%d references)", 
-                     G_OBJECT (chat)->ref_count - 1);
+       DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
 
        g_object_unref (chat);
 }
@@ -1463,10 +1591,8 @@ empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
 
        widget = GTK_WIDGET (chat);
 
-       empathy_debug (DEBUG_DOMAIN,
-                     "Chat moving with widget:%p (%d references)", 
-                     widget,
-                     G_OBJECT (widget)->ref_count);
+       DEBUG ("Chat moving with widget:%p (%d references)", widget,
+               G_OBJECT (widget)->ref_count);
 
        /* We reference here to make sure we don't loose the widget
         * and the EmpathyChat object during the move.
@@ -1521,7 +1647,7 @@ empathy_chat_window_find_chat (McAccount   *account,
        GList *l;
 
        g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
-       g_return_val_if_fail (!G_STR_EMPTY (id), NULL);
+       g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
 
        for (l = chat_windows; l; l = l->next) {
                EmpathyChatWindowPriv *priv;
@@ -1537,7 +1663,7 @@ empathy_chat_window_find_chat (McAccount   *account,
                        chat = ll->data;
 
                        if (empathy_account_equal (account, empathy_chat_get_account (chat)) &&
-                           strcmp (id, empathy_chat_get_id (chat)) == 0) {
+                           !tp_strdiff (id, empathy_chat_get_id (chat))) {
                                return chat;
                        }
                }
@@ -1573,17 +1699,3 @@ empathy_chat_window_present_chat (EmpathyChat *chat)
        gtk_widget_grab_focus (chat->input_text_view); 
 }
 
-#if 0
-static gboolean
-chat_window_should_play_sound (EmpathyChatWindow *window)
-{
-       EmpathyChatWindowPriv *priv = GET_PRIV (window);
-       gboolean               has_focus = FALSE;
-
-       g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
-
-       g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
-
-       return !has_focus;
-}
-#endif