]> git.0d.be Git - empathy.git/blob - src/empathy-chat-window.c
Fixes cycling tab bug in chat window (#589263)
[empathy.git] / src / empathy-chat-window.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2003-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Mikael Hallendal <micke@imendio.com>
22  *          Richard Hult <richard@imendio.com>
23  *          Martyn Russell <martyn@imendio.com>
24  *          Geert-Jan Van den Bogaerde <geertjan@gnome.org>
25  *          Xavier Claessens <xclaesse@gmail.com>
26  *          RĂ´mulo Fernandes Machado <romulo@castorgroup.net>
27  */
28
29 #include <config.h>
30
31 #include <string.h>
32
33 #include <gtk/gtk.h>
34 #include <gdk/gdkkeysyms.h>
35 #include <glib/gi18n.h>
36 #include <libnotify/notification.h>
37
38 #include <telepathy-glib/util.h>
39
40 #include <libempathy/empathy-contact.h>
41 #include <libempathy/empathy-message.h>
42 #include <libempathy/empathy-dispatcher.h>
43 #include <libempathy/empathy-chatroom-manager.h>
44 #include <libempathy/empathy-account-manager.h>
45 #include <libempathy/empathy-utils.h>
46
47 #include <libempathy-gtk/empathy-images.h>
48 #include <libempathy-gtk/empathy-conf.h>
49 #include <libempathy-gtk/empathy-contact-dialogs.h>
50 #include <libempathy-gtk/empathy-log-window.h>
51 #include <libempathy-gtk/empathy-geometry.h>
52 #include <libempathy-gtk/empathy-smiley-manager.h>
53 #include <libempathy-gtk/empathy-sound.h>
54 #include <libempathy-gtk/empathy-ui-utils.h>
55
56 #include "empathy-chat-window.h"
57 #include "empathy-about-dialog.h"
58 #include "empathy-misc.h"
59
60 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
61 #include <libempathy/empathy-debug.h>
62
63 typedef struct {
64         EmpathyChatWindow *window;
65         EmpathyChat *chat;
66 } NotificationData;
67
68 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatWindow)
69 typedef struct {
70         EmpathyChat *current_chat;
71         GList       *chats;
72         GList       *chats_new_msg;
73         GList       *chats_composing;
74         gboolean     page_added;
75         gboolean     dnd_same_window;
76         guint        save_geometry_id;
77         EmpathyChatroomManager *chatroom_manager;
78         GtkWidget   *dialog;
79         GtkWidget   *notebook;
80         NotifyNotification *notification;
81         NotificationData *notification_data;
82
83         /* Menu items. */
84         GtkUIManager *ui_manager;
85         GtkAction   *menu_conv_insert_smiley;
86         GtkAction   *menu_conv_favorite;
87         GtkAction   *menu_conv_toggle_contacts;
88
89         GtkAction   *menu_edit_cut;
90         GtkAction   *menu_edit_copy;
91         GtkAction   *menu_edit_paste;
92
93         GtkAction   *menu_tabs_next;
94         GtkAction   *menu_tabs_prev;
95         GtkAction   *menu_tabs_left;
96         GtkAction   *menu_tabs_right;
97         GtkAction   *menu_tabs_detach;
98 } EmpathyChatWindowPriv;
99
100 static GList *chat_windows = NULL;
101
102 static const guint tab_accel_keys[] = {
103         GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
104         GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
105 };
106
107 typedef enum {
108         DND_DRAG_TYPE_CONTACT_ID,
109         DND_DRAG_TYPE_TAB
110 } DndDragType;
111
112 static const GtkTargetEntry drag_types_dest[] = {
113         { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
114         { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
115 };
116
117 static void chat_window_update (EmpathyChatWindow *window);
118
119 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
120
121 static void
122 chat_window_accel_cb (GtkAccelGroup    *accelgroup,
123                       GObject          *object,
124                       guint             key,
125                       GdkModifierType   mod,
126                       EmpathyChatWindow *window)
127 {
128         EmpathyChatWindowPriv *priv;
129         gint                  num = -1;
130         guint                 i;
131
132         priv = GET_PRIV (window);
133
134         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
135                 if (tab_accel_keys[i] == key) {
136                         num = i;
137                         break;
138                 }
139         }
140
141         if (num != -1) {
142                 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
143         }
144 }
145
146 static EmpathyChatWindow *
147 chat_window_find_chat (EmpathyChat *chat)
148 {
149         EmpathyChatWindowPriv *priv;
150         GList                 *l, *ll;
151
152         for (l = chat_windows; l; l = l->next) {
153                 priv = GET_PRIV (l->data);
154                 ll = g_list_find (priv->chats, chat);
155                 if (ll) {
156                         return l->data;
157                 }
158         }
159
160         return NULL;
161 }
162
163 static void
164 chat_window_close_clicked_cb (GtkAction   *action,
165                               EmpathyChat *chat)
166 {
167         EmpathyChatWindow *window;
168
169         window = chat_window_find_chat (chat);
170         empathy_chat_window_remove_chat (window, chat);
171 }
172
173 static void
174 chat_tab_style_set_cb (GtkWidget *hbox,
175                                        GtkStyle  *previous_style,
176                                        gpointer   user_data)
177 {
178         GtkWidget *button;
179         int char_width, h, w;
180         PangoContext *context;
181         PangoFontMetrics *metrics;
182
183         button = g_object_get_data (G_OBJECT (user_data),
184                 "chat-window-tab-close-button");
185         context = gtk_widget_get_pango_context (hbox);
186
187         metrics = pango_context_get_metrics (context, gtk_widget_get_style (hbox)->font_desc,
188                 pango_context_get_language (context));
189         char_width = pango_font_metrics_get_approximate_char_width (metrics);
190         pango_font_metrics_unref (metrics);
191
192         gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
193                                            GTK_ICON_SIZE_MENU, &w, &h);
194
195         /* Request at least about 12 chars width plus at least space for the status
196          * image and the close button */
197         gtk_widget_set_size_request (hbox,
198                 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
199
200         gtk_widget_set_size_request (button, w, h);
201 }
202
203 static GtkWidget *
204 chat_window_create_label (EmpathyChatWindow *window,
205                           EmpathyChat       *chat,
206                           gboolean           is_tab_label)
207 {
208         EmpathyChatWindowPriv *priv;
209         GtkWidget            *hbox;
210         GtkWidget            *name_label;
211         GtkWidget            *status_image;
212         GtkWidget            *close_button;
213         GtkWidget            *close_image;
214         GtkWidget            *event_box;
215         GtkWidget            *event_box_hbox;
216         PangoAttrList        *attr_list;
217         PangoAttribute       *attr;
218
219         priv = GET_PRIV (window);
220
221         /* The spacing between the button and the label. */
222         hbox = gtk_hbox_new (FALSE, 0);
223
224         event_box = gtk_event_box_new ();
225         gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
226
227         name_label = gtk_label_new (NULL);
228         if (is_tab_label)
229                 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
230
231         attr_list = pango_attr_list_new ();
232         attr = pango_attr_scale_new (1/1.2);
233         attr->start_index = 0;
234         attr->end_index = -1;
235         pango_attr_list_insert (attr_list, attr);
236         gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
237         pango_attr_list_unref (attr_list);
238
239         gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
240         gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
241         g_object_set_data (G_OBJECT (chat),
242                 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
243                 name_label);
244
245         status_image = gtk_image_new ();
246
247         /* Spacing between the icon and label. */
248         event_box_hbox = gtk_hbox_new (FALSE, 0);
249
250         gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
251         gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
252
253         g_object_set_data (G_OBJECT (chat),
254                 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
255                 status_image);
256         g_object_set_data (G_OBJECT (chat),
257                 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
258                 event_box);
259
260         gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
261         gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
262
263         if (is_tab_label) {
264                 close_button = gtk_button_new ();
265                 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
266                 g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
267
268                 /* We don't want focus/keynav for the button to avoid clutter, and
269                  * Ctrl-W works anyway.
270                  */
271                 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_FOCUS);
272                 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_DEFAULT);
273
274                 /* Set the name to make the special rc style match. */
275                 gtk_widget_set_name (close_button, "empathy-close-button");
276
277                 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
278
279                 gtk_container_add (GTK_CONTAINER (close_button), close_image);
280
281                 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
282
283                 g_signal_connect (close_button,
284                                   "clicked",
285                                   G_CALLBACK (chat_window_close_clicked_cb),
286                                   chat);
287
288                 /* React to theme changes and also setup the size correctly.  */
289                 g_signal_connect (hbox,
290                                   "style-set",
291                                   G_CALLBACK (chat_tab_style_set_cb),
292                                   chat);
293         }
294
295         gtk_widget_show_all (hbox);
296
297         return hbox;
298 }
299
300 static void
301 _submenu_notify_visible_changed_cb (GObject    *object,
302                                     GParamSpec *pspec,
303                                     gpointer    userdata)
304 {
305         g_signal_handlers_disconnect_by_func (object,
306                                               _submenu_notify_visible_changed_cb,
307                                               userdata);
308         chat_window_update (EMPATHY_CHAT_WINDOW (userdata));
309 }
310
311 static void
312 chat_window_update (EmpathyChatWindow *window)
313 {
314         EmpathyChatWindowPriv *priv = GET_PRIV (window);
315         gboolean               first_page;
316         gboolean               last_page;
317         gboolean               is_connected;
318         gint                   num_pages;
319         gint                   page_num;
320         gint                   i;
321         const gchar           *name;
322         guint                  n_chats;
323         GdkPixbuf             *icon;
324         EmpathyContact        *remote_contact;
325         gboolean               avatar_in_icon;
326         GtkWidget             *chat;
327         GtkWidget             *chat_close_button;
328         GtkWidget             *menu, *submenu, *orig_submenu;
329
330         /* Get information */
331         page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
332         num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
333         first_page = (page_num == 0);
334         last_page = (page_num == (num_pages - 1));
335         is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
336         name = empathy_chat_get_name (priv->current_chat);
337         n_chats = g_list_length (priv->chats);
338
339         DEBUG ("Update window");
340
341         /* Update menu */
342         gtk_action_set_sensitive (priv->menu_tabs_next, TRUE);
343         gtk_action_set_sensitive (priv->menu_tabs_prev, TRUE);
344         gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
345         gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
346         gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
347         gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
348
349         /* Update Contact menu */
350         menu = gtk_ui_manager_get_widget (priv->ui_manager,
351                 "/chats_menubar/menu_contact");
352         orig_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menu));
353         if (orig_submenu == NULL || !GTK_WIDGET_VISIBLE (orig_submenu)) {
354                 submenu = empathy_chat_get_contact_menu (priv->current_chat);
355                 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
356                 gtk_widget_show (menu);
357         } else {
358                 empathy_signal_connect_weak (orig_submenu,
359                                              "notify::visible",
360                                              (GCallback)_submenu_notify_visible_changed_cb,
361                                              G_OBJECT (window));
362         }
363
364         /* Update window title */
365         if (n_chats == 1) {
366                 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
367         } else {
368                 gchar *title;
369
370                 title = g_strdup_printf (_("Conversations (%d)"), n_chats);
371                 gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
372                 g_free (title);
373         }
374
375         /* Update window icon */
376         if (priv->chats_new_msg) {
377                 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
378                                           EMPATHY_IMAGE_MESSAGE);
379         } else {
380                 empathy_conf_get_bool (empathy_conf_get (),
381                                        EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
382                                        &avatar_in_icon);
383
384                 if (n_chats == 1 && avatar_in_icon) {
385                         remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
386                         icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
387                         gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
388
389                         if (icon != NULL) {
390                                 g_object_unref (icon);
391                         }
392                 } else {
393                         gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
394                 }
395         }
396
397         if (num_pages == 1) {
398                 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
399                 chat_close_button = g_object_get_data (G_OBJECT (chat),
400                                 "chat-window-tab-close-button");
401                 gtk_widget_hide (chat_close_button);
402         } else {
403                 for (i=0; i<num_pages; i++) {
404                         chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
405                         chat_close_button = g_object_get_data (G_OBJECT (chat),
406                                         "chat-window-tab-close-button");
407                         gtk_widget_show (chat_close_button);
408                 }
409         }
410 }
411
412 static void
413 append_markup_printf (GString    *string,
414                       const char *format,
415                       ...)
416 {
417         gchar *tmp;
418         va_list args;
419
420         va_start (args, format);
421
422         tmp = g_markup_vprintf_escaped (format, args);
423         g_string_append (string, tmp);
424         g_free (tmp);
425
426         va_end (args);
427 }
428
429 static void
430 chat_window_update_chat_tab (EmpathyChat *chat)
431 {
432         EmpathyChatWindow     *window;
433         EmpathyChatWindowPriv *priv;
434         EmpathyContact        *remote_contact;
435         const gchar           *name;
436         const gchar           *id;
437         EmpathyAccount        *account;
438         const gchar           *subject;
439         const gchar           *status = NULL;
440         GtkWidget             *widget;
441         GString               *tooltip;
442         gchar                 *markup;
443         const gchar           *icon_name;
444         GtkWidget             *tab_image;
445         GtkWidget             *menu_image;
446
447         window = chat_window_find_chat (chat);
448         if (!window) {
449                 return;
450         }
451         priv = GET_PRIV (window);
452
453         /* Get information */
454         name = empathy_chat_get_name (chat);
455         account = empathy_chat_get_account (chat);
456         subject = empathy_chat_get_subject (chat);
457         remote_contact = empathy_chat_get_remote_contact (chat);
458
459         DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
460                 name, empathy_account_get_unique_name (account), subject, remote_contact);
461
462         /* Update tab image */
463         if (empathy_chat_get_tp_chat (chat) == NULL) {
464                 /* No TpChat, we are disconnected */
465                 icon_name = NULL;
466         }
467         else if (g_list_find (priv->chats_new_msg, chat)) {
468                 icon_name = EMPATHY_IMAGE_MESSAGE;
469         }
470         else if (g_list_find (priv->chats_composing, chat)) {
471                 icon_name = EMPATHY_IMAGE_TYPING;
472         }
473         else if (remote_contact) {
474                 icon_name = empathy_icon_name_for_contact (remote_contact);
475         } else {
476                 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
477         }
478
479         tab_image = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
480         menu_image = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
481         if (icon_name != NULL) {
482                 gtk_image_set_from_icon_name (GTK_IMAGE (tab_image), icon_name, GTK_ICON_SIZE_MENU);
483                 gtk_widget_show (tab_image);
484                 gtk_image_set_from_icon_name (GTK_IMAGE (menu_image), icon_name, GTK_ICON_SIZE_MENU);
485                 gtk_widget_show (menu_image);
486         } else {
487                 gtk_widget_hide (tab_image);
488                 gtk_widget_hide (menu_image);
489         }
490
491         /* Update tab tooltip */
492         tooltip = g_string_new (NULL);
493
494         if (remote_contact) {
495                 id = empathy_contact_get_id (remote_contact);
496                 status = empathy_contact_get_presence_message (remote_contact);
497         } else {
498                 id = name;
499         }
500
501         append_markup_printf (tooltip,
502                               "<b>%s</b><small> (%s)</small>",
503                               id,
504                               empathy_account_get_display_name (account));
505
506         if (!EMP_STR_EMPTY (status)) {
507                 append_markup_printf (tooltip, "\n<i>%s</i>", status);
508         }
509
510         if (subject) {
511                 append_markup_printf (tooltip, "\n<b>%s</b> %s",
512                                       _("Topic:"), subject);
513         }
514
515         if (g_list_find (priv->chats_composing, chat)) {
516                 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
517         }
518
519         markup = g_string_free (tooltip, FALSE);
520         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
521         gtk_widget_set_tooltip_markup (widget, markup);
522         widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
523         gtk_widget_set_tooltip_markup (widget, markup);
524         g_free (markup);
525
526         /* Update tab and menu label */
527         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
528         gtk_label_set_text (GTK_LABEL (widget), name);
529         widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
530         gtk_label_set_text (GTK_LABEL (widget), name);
531
532         /* Update the window if it's the current chat */
533         if (priv->current_chat == chat) {
534                 chat_window_update (window);
535         }
536 }
537
538 static void
539 chat_window_chat_notify_cb (EmpathyChat *chat)
540 {
541         EmpathyContact *old_remote_contact;
542         EmpathyContact *remote_contact = NULL;
543
544         old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
545         remote_contact = empathy_chat_get_remote_contact (chat);
546
547         if (old_remote_contact != remote_contact) {
548                 /* The remote-contact associated with the chat changed, we need
549                  * to keep track of any change of that contact and update the
550                  * window each time. */
551                 if (remote_contact) {
552                         g_signal_connect_swapped (remote_contact, "notify",
553                                                   G_CALLBACK (chat_window_update_chat_tab),
554                                                   chat);
555                 }
556                 if (old_remote_contact) {
557                         g_signal_handlers_disconnect_by_func (old_remote_contact,
558                                                               chat_window_update_chat_tab,
559                                                               chat);
560                 }
561
562                 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
563                                    remote_contact);
564         }
565
566         chat_window_update_chat_tab (chat);
567 }
568
569 static void
570 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
571                                        EmpathySmiley        *smiley,
572                                        gpointer              window)
573 {
574         EmpathyChatWindowPriv *priv = GET_PRIV (window);
575         EmpathyChat           *chat;
576         GtkTextBuffer         *buffer;
577         GtkTextIter            iter;
578
579         chat = priv->current_chat;
580
581         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
582         gtk_text_buffer_get_end_iter (buffer, &iter);
583         gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
584 }
585
586 static void
587 chat_window_conv_activate_cb (GtkAction         *action,
588                               EmpathyChatWindow *window)
589 {
590         EmpathyChatWindowPriv *priv = GET_PRIV (window);
591         gboolean               is_room;
592         gboolean               active;
593         EmpathyContact        *remote_contact = NULL;
594
595         /* Favorite room menu */
596         is_room = empathy_chat_is_room (priv->current_chat);
597         if (is_room) {
598                 const gchar *room;
599                 EmpathyAccount   *account;
600                 gboolean     found = FALSE;
601                 EmpathyChatroom *chatroom;
602
603                 room = empathy_chat_get_id (priv->current_chat);
604                 account = empathy_chat_get_account (priv->current_chat);
605                 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
606                                                        account, room);
607                 if (chatroom != NULL)
608                         found = empathy_chatroom_is_favorite (chatroom);
609
610                 DEBUG ("This room %s favorite", found ? "is" : "is not");
611                 gtk_toggle_action_set_active (
612                         GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
613         }
614         gtk_action_set_visible (priv->menu_conv_favorite, is_room);
615
616         /* Show contacts menu */
617         g_object_get (priv->current_chat,
618                       "remote-contact", &remote_contact,
619                       "show-contacts", &active,
620                       NULL);
621         if (remote_contact == NULL) {
622                 gtk_toggle_action_set_active (
623                         GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
624                                            active);
625         }
626         gtk_action_set_visible (priv->menu_conv_toggle_contacts,
627                                 (remote_contact == NULL));
628         if (remote_contact != NULL) {
629                 g_object_unref (remote_contact);
630         }
631 }
632
633 static void
634 chat_window_clear_activate_cb (GtkAction         *action,
635                                EmpathyChatWindow *window)
636 {
637         EmpathyChatWindowPriv *priv = GET_PRIV (window);
638
639         empathy_chat_clear (priv->current_chat);
640 }
641
642 static void
643 chat_window_favorite_toggled_cb (GtkToggleAction   *toggle_action,
644                                  EmpathyChatWindow *window)
645 {
646         EmpathyChatWindowPriv *priv = GET_PRIV (window);
647         gboolean               active;
648         EmpathyAccount        *account;
649         const gchar           *room;
650         EmpathyChatroom       *chatroom;
651
652         active = gtk_toggle_action_get_active (toggle_action);
653         account = empathy_chat_get_account (priv->current_chat);
654         room = empathy_chat_get_id (priv->current_chat);
655
656         chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
657                                                   account, room);
658
659         if (chatroom == NULL) {
660                 const gchar *name;
661
662                 name = empathy_chat_get_name (priv->current_chat);
663                 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
664                 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
665                 g_object_unref (chatroom);
666         }
667
668         empathy_chatroom_set_favorite (chatroom, active);
669 }
670
671 static void
672 chat_window_contacts_toggled_cb (GtkToggleAction   *toggle_action,
673                                  EmpathyChatWindow *window)
674 {
675         EmpathyChatWindowPriv *priv = GET_PRIV (window);
676         gboolean               active;
677
678         active = gtk_toggle_action_get_active (toggle_action);
679
680         empathy_chat_set_show_contacts (priv->current_chat, active);
681 }
682
683 static const gchar *
684 chat_get_window_id_for_geometry (EmpathyChat *chat)
685 {
686         const gchar *res = NULL;
687         gboolean     separate_windows;
688
689         empathy_conf_get_bool (empathy_conf_get (),
690                                EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
691                                &separate_windows);
692
693         if (separate_windows) {
694                 res = empathy_chat_get_id (chat);
695         }
696
697         return res ? res : "chat-window";
698 }
699
700 static gboolean
701 chat_window_save_geometry_timeout_cb (EmpathyChatWindow *window)
702 {
703         EmpathyChatWindowPriv *priv;
704         gint                  x, y, w, h;
705
706         priv = GET_PRIV (window);
707
708         gtk_window_get_size (GTK_WINDOW (priv->dialog), &w, &h);
709         gtk_window_get_position (GTK_WINDOW (priv->dialog), &x, &y);
710
711         empathy_geometry_save (chat_get_window_id_for_geometry (priv->current_chat),
712                                x, y, w, h);
713
714         priv->save_geometry_id = 0;
715
716         return FALSE;
717 }
718
719 static gboolean
720 chat_window_configure_event_cb (GtkWidget         *widget,
721                                 GdkEventConfigure *event,
722                                 EmpathyChatWindow  *window)
723 {
724         EmpathyChatWindowPriv *priv;
725
726         priv = GET_PRIV (window);
727
728         if (priv->save_geometry_id != 0) {
729                 g_source_remove (priv->save_geometry_id);
730         }
731
732         priv->save_geometry_id =
733                 g_timeout_add_seconds (1,
734                                        (GSourceFunc) chat_window_save_geometry_timeout_cb,
735                                        window);
736
737         return FALSE;
738 }
739
740 static void
741 chat_window_close_activate_cb (GtkAction         *action,
742                                EmpathyChatWindow *window)
743 {
744         EmpathyChatWindowPriv *priv;
745
746         priv = GET_PRIV (window);
747
748         g_return_if_fail (priv->current_chat != NULL);
749
750         empathy_chat_window_remove_chat (window, priv->current_chat);
751 }
752
753 static void
754 chat_window_edit_activate_cb (GtkAction         *action,
755                               EmpathyChatWindow *window)
756 {
757         EmpathyChatWindowPriv *priv;
758         GtkClipboard         *clipboard;
759         GtkTextBuffer        *buffer;
760         gboolean              text_available;
761
762         priv = GET_PRIV (window);
763
764         g_return_if_fail (priv->current_chat != NULL);
765
766         if (!empathy_chat_get_tp_chat (priv->current_chat)) {
767                 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
768                 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
769                 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
770                 return;
771         }
772
773         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
774         if (gtk_text_buffer_get_has_selection (buffer)) {
775                 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
776                 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
777         } else {
778                 gboolean selection;
779
780                 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
781
782                 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
783                 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
784         }
785
786         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
787         text_available = gtk_clipboard_wait_is_text_available (clipboard);
788         gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
789 }
790
791 static void
792 chat_window_cut_activate_cb (GtkAction         *action,
793                              EmpathyChatWindow *window)
794 {
795         EmpathyChatWindowPriv *priv;
796
797         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
798
799         priv = GET_PRIV (window);
800
801         empathy_chat_cut (priv->current_chat);
802 }
803
804 static void
805 chat_window_copy_activate_cb (GtkAction         *action,
806                               EmpathyChatWindow *window)
807 {
808         EmpathyChatWindowPriv *priv;
809
810         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
811
812         priv = GET_PRIV (window);
813
814         empathy_chat_copy (priv->current_chat);
815 }
816
817 static void
818 chat_window_paste_activate_cb (GtkAction         *action,
819                                EmpathyChatWindow *window)
820 {
821         EmpathyChatWindowPriv *priv;
822
823         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
824
825         priv = GET_PRIV (window);
826
827         empathy_chat_paste (priv->current_chat);
828 }
829
830 static void
831 chat_window_tabs_next_activate_cb (GtkAction         *action,
832                                    EmpathyChatWindow *window)
833 {
834         EmpathyChatWindowPriv *priv;
835         EmpathyChat           *chat;
836         gint                  index_, numPages;
837
838         priv = GET_PRIV (window);
839
840         chat = priv->current_chat;
841         index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
842         numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
843
844         if (index_ == (numPages - 1)) {
845                 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), 0);
846                 return;
847         }
848
849         gtk_notebook_next_page (GTK_NOTEBOOK (priv->notebook));
850 }
851
852 static void
853 chat_window_tabs_previous_activate_cb (GtkAction         *action,
854                                    EmpathyChatWindow *window)
855 {
856         EmpathyChatWindowPriv *priv;
857         EmpathyChat           *chat;
858         gint                  index_, numPages;
859
860         priv = GET_PRIV (window);
861
862         chat = priv->current_chat;
863         index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
864         numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
865
866         if (index_ <= 0) {
867                 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), numPages - 1);
868                 return;
869         }
870
871         gtk_notebook_prev_page (GTK_NOTEBOOK (priv->notebook));
872 }
873
874 static void
875 chat_window_tabs_left_activate_cb (GtkAction         *action,
876                                    EmpathyChatWindow *window)
877 {
878         EmpathyChatWindowPriv *priv;
879         EmpathyChat           *chat;
880         gint                  index_;
881
882         priv = GET_PRIV (window);
883
884         chat = priv->current_chat;
885         index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
886         if (index_ <= 0) {
887                 return;
888         }
889
890         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
891                                     GTK_WIDGET (chat),
892                                     index_ - 1);
893 }
894
895 static void
896 chat_window_tabs_right_activate_cb (GtkAction         *action,
897                                     EmpathyChatWindow *window)
898 {
899         EmpathyChatWindowPriv *priv;
900         EmpathyChat           *chat;
901         gint                  index_;
902
903         priv = GET_PRIV (window);
904
905         chat = priv->current_chat;
906         index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
907
908         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
909                                     GTK_WIDGET (chat),
910                                     index_ + 1);
911 }
912
913 static void
914 chat_window_detach_activate_cb (GtkAction         *action,
915                                 EmpathyChatWindow *window)
916 {
917         EmpathyChatWindowPriv *priv;
918         EmpathyChatWindow     *new_window;
919         EmpathyChat           *chat;
920
921         priv = GET_PRIV (window);
922
923         chat = priv->current_chat;
924         new_window = empathy_chat_window_new ();
925
926         empathy_chat_window_move_chat (window, new_window, chat);
927
928         priv = GET_PRIV (new_window);
929         gtk_widget_show (priv->dialog);
930 }
931
932 static void
933 chat_window_help_contents_activate_cb (GtkAction         *action,
934                                        EmpathyChatWindow *window)
935 {
936         EmpathyChatWindowPriv *priv = GET_PRIV (window);
937
938         empathy_url_show (priv->dialog, "ghelp:empathy");
939 }
940
941 static void
942 chat_window_help_about_activate_cb (GtkAction         *action,
943                                     EmpathyChatWindow *window)
944 {
945         EmpathyChatWindowPriv *priv = GET_PRIV (window);
946
947         empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
948 }
949
950 static gboolean
951 chat_window_delete_event_cb (GtkWidget        *dialog,
952                              GdkEvent         *event,
953                              EmpathyChatWindow *window)
954 {
955         EmpathyChatWindowPriv *priv = GET_PRIV (window);
956
957         DEBUG ("Delete event received");
958
959         g_object_ref (window);
960         while (priv->chats) {
961                 empathy_chat_window_remove_chat (window, priv->chats->data);
962         }
963         g_object_unref (window);
964
965         return TRUE;
966 }
967
968 static void
969 chat_window_composing_cb (EmpathyChat       *chat,
970                           gboolean          is_composing,
971                           EmpathyChatWindow *window)
972 {
973         EmpathyChatWindowPriv *priv;
974
975         priv = GET_PRIV (window);
976
977         if (is_composing && !g_list_find (priv->chats_composing, chat)) {
978                 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
979         } else {
980                 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
981         }
982
983         chat_window_update_chat_tab (chat);
984 }
985
986 static void
987 chat_window_set_urgency_hint (EmpathyChatWindow *window,
988                               gboolean          urgent)
989 {
990         EmpathyChatWindowPriv *priv;
991
992         priv = GET_PRIV (window);
993
994         DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
995         gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
996 }
997
998 static void
999 free_notification_data (NotificationData *data)
1000 {
1001         g_object_unref (data->chat);
1002         g_slice_free (NotificationData, data);
1003 }
1004
1005 static void
1006 chat_window_notification_closed_cb (NotifyNotification *notify,
1007                                     NotificationData *cb_data)
1008 {
1009         EmpathyNotificationClosedReason reason = 0;
1010         EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
1011
1012 #ifdef notify_notification_get_closed_reason
1013         reason = notify_notification_get_closed_reason (notify);
1014 #endif
1015         if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
1016                 empathy_chat_window_present_chat (cb_data->chat);
1017         }
1018
1019         g_object_unref (notify);
1020         priv->notification = NULL;
1021         free_notification_data (cb_data);
1022         priv->notification_data = NULL;
1023 }
1024
1025 static void
1026 chat_window_show_or_update_notification (EmpathyChatWindow *window,
1027                                          EmpathyMessage *message,
1028                                          EmpathyChat *chat)
1029 {
1030         EmpathyContact *sender;
1031         const gchar *header;
1032         char *escaped;
1033         const char *body;
1034         GdkPixbuf *pixbuf;
1035         EmpathyChatWindowPriv *priv = GET_PRIV (window);
1036         gboolean res;
1037
1038         if (!empathy_notification_is_enabled ()) {
1039                 return;
1040         } else {
1041                 empathy_conf_get_bool (empathy_conf_get (),
1042                                        EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
1043                 if (!res) {
1044                         return;
1045                 }
1046         }
1047
1048         sender = empathy_message_get_sender (message);
1049         header = empathy_contact_get_name (sender);
1050         body = empathy_message_get_body (message);
1051         escaped = g_markup_escape_text (body, -1);
1052
1053         if (priv->notification != NULL) {
1054                 notify_notification_update (priv->notification,
1055                                             header, escaped, NULL);
1056         } else {
1057                 NotificationData *cb_data = cb_data = g_slice_new0 (NotificationData);
1058
1059                 cb_data->chat = g_object_ref (chat);
1060                 cb_data->window = window;
1061
1062                 priv->notification_data = cb_data;
1063                 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
1064                 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
1065
1066                 g_signal_connect (priv->notification, "closed",
1067                                   G_CALLBACK (chat_window_notification_closed_cb), cb_data);
1068         }
1069
1070         pixbuf = empathy_misc_get_pixbuf_for_notification (sender, EMPATHY_IMAGE_NEW_MESSAGE);
1071
1072         if (pixbuf != NULL) {
1073                 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
1074                 g_object_unref (pixbuf);
1075         }
1076
1077         notify_notification_show (priv->notification, NULL);
1078
1079         g_free (escaped);
1080 }
1081
1082 static void
1083 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1084 {
1085         gchar *markup;
1086         GtkWidget *widget;
1087
1088         if (!empathy_chat_is_room (chat))
1089                 return;
1090
1091         markup = g_markup_printf_escaped (
1092                 "<span color=\"red\" weight=\"bold\">%s</span>",
1093                 empathy_chat_get_name (chat));
1094
1095         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1096         gtk_label_set_markup (GTK_LABEL (widget), markup);
1097         g_free (markup);
1098 }
1099
1100 static void
1101 chat_window_new_message_cb (EmpathyChat       *chat,
1102                             EmpathyMessage    *message,
1103                             EmpathyChatWindow *window)
1104 {
1105         EmpathyChatWindowPriv *priv;
1106         gboolean              has_focus;
1107         gboolean              needs_urgency;
1108         EmpathyContact        *sender;
1109
1110         priv = GET_PRIV (window);
1111
1112         has_focus = empathy_chat_window_has_focus (window);
1113
1114         /* - if we're the sender, we play the sound if it's specified in the
1115          *   preferences and we're not away.
1116          * - if we receive a message, we play the sound if it's specified in the
1117          *   preferences and the window does not have focus on the chat receiving
1118          *   the message.
1119          */
1120
1121         sender = empathy_message_get_sender (message);
1122
1123         if (empathy_contact_is_user (sender)) {
1124                 empathy_sound_play (GTK_WIDGET (priv->dialog),
1125                                     EMPATHY_SOUND_MESSAGE_OUTGOING);
1126         }
1127
1128         if (has_focus && priv->current_chat == chat) {
1129                 return;
1130         }
1131
1132         if (!g_list_find (priv->chats_new_msg, chat)) {
1133                 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1134                 chat_window_update_chat_tab (chat);
1135         }
1136
1137         /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1138          * If empathy_chat_get_remote_contact () returns NULL, that means it's
1139          * an unamed MUC (msn-like).
1140          * In case of a MUC, we set urgency only if the message contains our
1141          * alias. */
1142         if (empathy_chat_is_room (chat) ||
1143             empathy_chat_get_remote_contact (chat) == NULL) {
1144                 needs_urgency = empathy_message_should_highlight (message);
1145         } else {
1146                 needs_urgency = TRUE;
1147         }
1148
1149         if (needs_urgency) {
1150                 if (!has_focus) {
1151                         chat_window_set_urgency_hint (window, TRUE);
1152                         chat_window_set_highlight_room_tab_label (chat);
1153                 }
1154
1155                 empathy_sound_play (GTK_WIDGET (priv->dialog),
1156                     EMPATHY_SOUND_MESSAGE_INCOMING);
1157                 chat_window_show_or_update_notification (window, message, chat);
1158         }
1159 }
1160
1161 static GtkNotebook *
1162 chat_window_detach_hook (GtkNotebook *source,
1163                          GtkWidget   *page,
1164                          gint         x,
1165                          gint         y,
1166                          gpointer     user_data)
1167 {
1168         EmpathyChatWindowPriv *priv;
1169         EmpathyChatWindow     *window, *new_window;
1170         EmpathyChat           *chat;
1171
1172         chat = EMPATHY_CHAT (page);
1173         window = chat_window_find_chat (chat);
1174
1175         new_window = empathy_chat_window_new ();
1176         priv = GET_PRIV (new_window);
1177
1178         DEBUG ("Detach hook called");
1179
1180         empathy_chat_window_move_chat (window, new_window, chat);
1181
1182         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1183         gtk_widget_show (priv->dialog);
1184
1185         return NULL;
1186 }
1187
1188 static void
1189 chat_window_page_switched_cb (GtkNotebook      *notebook,
1190                               GtkNotebookPage  *page,
1191                               gint              page_num,
1192                               EmpathyChatWindow *window)
1193 {
1194         EmpathyChatWindowPriv *priv;
1195         EmpathyChat           *chat;
1196         GtkWidget            *child;
1197
1198         DEBUG ("Page switched");
1199
1200         priv = GET_PRIV (window);
1201
1202         child = gtk_notebook_get_nth_page (notebook, page_num);
1203         chat = EMPATHY_CHAT (child);
1204
1205         if (priv->page_added) {
1206                 priv->page_added = FALSE;
1207                 empathy_chat_scroll_down (chat);
1208         }
1209         else if (priv->current_chat == chat) {
1210                 return;
1211         }
1212
1213         priv->current_chat = chat;
1214         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1215
1216         chat_window_update_chat_tab (chat);
1217 }
1218
1219 static void
1220 chat_window_page_added_cb (GtkNotebook      *notebook,
1221                            GtkWidget        *child,
1222                            guint             page_num,
1223                            EmpathyChatWindow *window)
1224 {
1225         EmpathyChatWindowPriv *priv;
1226         EmpathyChat           *chat;
1227
1228         priv = GET_PRIV (window);
1229
1230         /* If we just received DND to the same window, we don't want
1231          * to do anything here like removing the tab and then readding
1232          * it, so we return here and in "page-added".
1233          */
1234         if (priv->dnd_same_window) {
1235                 DEBUG ("Page added (back to the same window)");
1236                 priv->dnd_same_window = FALSE;
1237                 return;
1238         }
1239
1240         DEBUG ("Page added");
1241
1242         /* Get chat object */
1243         chat = EMPATHY_CHAT (child);
1244
1245         /* Connect chat signals for this window */
1246         g_signal_connect (chat, "composing",
1247                           G_CALLBACK (chat_window_composing_cb),
1248                           window);
1249         g_signal_connect (chat, "new-message",
1250                           G_CALLBACK (chat_window_new_message_cb),
1251                           window);
1252         g_signal_connect (chat, "notify::tp-chat",
1253                           G_CALLBACK (chat_window_update_chat_tab),
1254                           window);
1255
1256         /* Set flag so we know to perform some special operations on
1257          * switch page due to the new page being added.
1258          */
1259         priv->page_added = TRUE;
1260
1261         /* Get list of chats up to date */
1262         priv->chats = g_list_append (priv->chats, chat);
1263
1264         chat_window_update_chat_tab (chat);
1265 }
1266
1267 static void
1268 chat_window_page_removed_cb (GtkNotebook      *notebook,
1269                              GtkWidget        *child,
1270                              guint             page_num,
1271                              EmpathyChatWindow *window)
1272 {
1273         EmpathyChatWindowPriv *priv;
1274         EmpathyChat           *chat;
1275
1276         priv = GET_PRIV (window);
1277
1278         /* If we just received DND to the same window, we don't want
1279          * to do anything here like removing the tab and then readding
1280          * it, so we return here and in "page-added".
1281          */
1282         if (priv->dnd_same_window) {
1283                 DEBUG ("Page removed (and will be readded to same window)");
1284                 return;
1285         }
1286
1287         DEBUG ("Page removed");
1288
1289         /* Get chat object */
1290         chat = EMPATHY_CHAT (child);
1291
1292         /* Disconnect all signal handlers for this chat and this window */
1293         g_signal_handlers_disconnect_by_func (chat,
1294                                               G_CALLBACK (chat_window_composing_cb),
1295                                               window);
1296         g_signal_handlers_disconnect_by_func (chat,
1297                                               G_CALLBACK (chat_window_new_message_cb),
1298                                               window);
1299         g_signal_handlers_disconnect_by_func (chat,
1300                                               G_CALLBACK (chat_window_update_chat_tab),
1301                                               window);
1302
1303         /* Keep list of chats up to date */
1304         priv->chats = g_list_remove (priv->chats, chat);
1305         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1306         priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1307
1308         if (priv->chats == NULL) {
1309                 g_object_unref (window);
1310         } else {
1311                 chat_window_update (window);
1312         }
1313 }
1314
1315 static gboolean
1316 chat_window_focus_in_event_cb (GtkWidget        *widget,
1317                                GdkEvent         *event,
1318                                EmpathyChatWindow *window)
1319 {
1320         EmpathyChatWindowPriv *priv;
1321
1322         DEBUG ("Focus in event, updating title");
1323
1324         priv = GET_PRIV (window);
1325
1326         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1327
1328         chat_window_set_urgency_hint (window, FALSE);
1329
1330         /* Update the title, since we now mark all unread messages as read. */
1331         chat_window_update_chat_tab (priv->current_chat);
1332
1333         return FALSE;
1334 }
1335
1336 static void
1337 chat_window_drag_data_received (GtkWidget        *widget,
1338                                 GdkDragContext   *context,
1339                                 int               x,
1340                                 int               y,
1341                                 GtkSelectionData *selection,
1342                                 guint             info,
1343                                 guint             time_,
1344                                 EmpathyChatWindow *window)
1345 {
1346         if (info == DND_DRAG_TYPE_CONTACT_ID) {
1347                 EmpathyChat           *chat = NULL;
1348                 EmpathyChatWindow     *old_window;
1349                 EmpathyAccount        *account = NULL;
1350                 EmpathyAccountManager *account_manager;
1351                 const gchar           *id;
1352                 gchar                **strv;
1353                 const gchar           *account_id;
1354                 const gchar           *contact_id;
1355
1356                 id = (const gchar*) gtk_selection_data_get_data (selection);
1357                 account_manager = empathy_account_manager_dup_singleton ();
1358
1359                 DEBUG ("DND contact from roster with id:'%s'", id);
1360
1361                 strv = g_strsplit (id, ":", 2);
1362                 if (g_strv_length (strv) == 2) {
1363                         account_id = strv[0];
1364                         contact_id = strv[1];
1365                         account =
1366                                 empathy_account_manager_get_account (account_manager, account_id);
1367                         if (account != NULL)
1368                                 chat = empathy_chat_window_find_chat (account, contact_id);
1369                 }
1370
1371                 if (account == NULL) {
1372                         g_strfreev (strv);
1373                         gtk_drag_finish (context, FALSE, FALSE, time_);
1374                         return;
1375                 }
1376
1377                 if (!chat) {
1378                         TpConnection *connection;
1379
1380                         connection = empathy_account_get_connection (account);
1381
1382                         if (connection) {
1383                                 empathy_dispatcher_chat_with_contact_id (
1384                                         connection, contact_id, NULL, NULL);
1385                         }
1386
1387                         g_strfreev (strv);
1388                         return;
1389                 }
1390                 g_object_unref (account_manager);
1391                 g_strfreev (strv);
1392
1393                 old_window = chat_window_find_chat (chat);
1394                 if (old_window) {
1395                         if (old_window == window) {
1396                                 gtk_drag_finish (context, TRUE, FALSE, time_);
1397                                 return;
1398                         }
1399
1400                         empathy_chat_window_move_chat (old_window, window, chat);
1401                 } else {
1402                         empathy_chat_window_add_chat (window, chat);
1403                 }
1404
1405                 /* Added to take care of any outstanding chat events */
1406                 empathy_chat_window_present_chat (chat);
1407
1408                 /* We should return TRUE to remove the data when doing
1409                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1410                  * weird consequences, and we handle that internally
1411                  * anyway with add_chat () and remove_chat ().
1412                  */
1413                 gtk_drag_finish (context, TRUE, FALSE, time_);
1414         }
1415         else if (info == DND_DRAG_TYPE_TAB) {
1416                 EmpathyChat        **chat;
1417                 EmpathyChatWindow   *old_window = NULL;
1418
1419                 DEBUG ("DND tab");
1420
1421                 chat = (void *) gtk_selection_data_get_data (selection);
1422                 old_window = chat_window_find_chat (*chat);
1423
1424                 if (old_window) {
1425                         EmpathyChatWindowPriv *priv;
1426
1427                         priv = GET_PRIV (window);
1428
1429                         if (old_window == window) {
1430                                 DEBUG ("DND tab (within same window)");
1431                                 priv->dnd_same_window = TRUE;
1432                                 gtk_drag_finish (context, TRUE, FALSE, time_);
1433                                 return;
1434                         }
1435
1436                         priv->dnd_same_window = FALSE;
1437                 }
1438
1439                 /* We should return TRUE to remove the data when doing
1440                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1441                  * weird consequences, and we handle that internally
1442                  * anyway with add_chat () and remove_chat ().
1443                  */
1444                 gtk_drag_finish (context, TRUE, FALSE, time_);
1445         } else {
1446                 DEBUG ("DND from unknown source");
1447                 gtk_drag_finish (context, FALSE, FALSE, time_);
1448         }
1449 }
1450
1451 static void
1452 chat_window_finalize (GObject *object)
1453 {
1454         EmpathyChatWindow     *window;
1455         EmpathyChatWindowPriv *priv;
1456
1457         window = EMPATHY_CHAT_WINDOW (object);
1458         priv = GET_PRIV (window);
1459
1460         DEBUG ("Finalized: %p", object);
1461
1462         g_object_unref (priv->ui_manager);
1463         g_object_unref (priv->chatroom_manager);
1464         if (priv->save_geometry_id != 0) {
1465                 g_source_remove (priv->save_geometry_id);
1466         }
1467
1468         if (priv->notification != NULL) {
1469                 notify_notification_close (priv->notification, NULL);
1470                 g_object_unref (priv->notification);
1471                 priv->notification = NULL;
1472                 if (priv->notification_data != NULL)
1473                         {
1474                                 free_notification_data (priv->notification_data);
1475                                 priv->notification_data = NULL;
1476                         }
1477         }
1478
1479         chat_windows = g_list_remove (chat_windows, window);
1480         gtk_widget_destroy (priv->dialog);
1481
1482         G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1483 }
1484
1485 static void
1486 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1487 {
1488         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1489
1490         object_class->finalize = chat_window_finalize;
1491
1492         g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1493
1494         /* Set up a style for the close button with no focus padding. */
1495         gtk_rc_parse_string (
1496                 "style \"empathy-close-button-style\"\n"
1497                 "{\n"
1498                 "  GtkWidget::focus-padding = 0\n"
1499                 "  xthickness = 0\n"
1500                 "  ythickness = 0\n"
1501                 "}\n"
1502                 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1503
1504         gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1505 }
1506
1507 static void
1508 empathy_chat_window_init (EmpathyChatWindow *window)
1509 {
1510         GtkBuilder            *gui;
1511         GtkAccelGroup         *accel_group;
1512         GClosure              *closure;
1513         GtkWidget             *menu;
1514         GtkWidget             *submenu;
1515         guint                  i;
1516         GtkWidget             *chat_vbox;
1517         gchar                 *filename;
1518         EmpathySmileyManager  *smiley_manager;
1519         EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1520                 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1521
1522         window->priv = priv;
1523         filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1524         gui = empathy_builder_get_file (filename,
1525                                        "chat_window", &priv->dialog,
1526                                        "chat_vbox", &chat_vbox,
1527                                        "ui_manager", &priv->ui_manager,
1528                                        "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1529                                        "menu_conv_favorite", &priv->menu_conv_favorite,
1530                                        "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1531                                        "menu_edit_cut", &priv->menu_edit_cut,
1532                                        "menu_edit_copy", &priv->menu_edit_copy,
1533                                        "menu_edit_paste", &priv->menu_edit_paste,
1534                                        "menu_tabs_next", &priv->menu_tabs_next,
1535                                        "menu_tabs_prev", &priv->menu_tabs_prev,
1536                                        "menu_tabs_left", &priv->menu_tabs_left,
1537                                        "menu_tabs_right", &priv->menu_tabs_right,
1538                                        "menu_tabs_detach", &priv->menu_tabs_detach,
1539                                        NULL);
1540         g_free (filename);
1541
1542         empathy_builder_connect (gui, window,
1543                               "chat_window", "configure-event", chat_window_configure_event_cb,
1544                               "menu_conv", "activate", chat_window_conv_activate_cb,
1545                               "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1546                               "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1547                               "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1548                               "menu_conv_close", "activate", chat_window_close_activate_cb,
1549                               "menu_edit", "activate", chat_window_edit_activate_cb,
1550                               "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1551                               "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1552                               "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1553                               "menu_tabs_next", "activate", chat_window_tabs_next_activate_cb,
1554                               "menu_tabs_prev", "activate", chat_window_tabs_previous_activate_cb,
1555                               "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1556                               "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1557                               "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1558                               "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1559                               "menu_help_about", "activate", chat_window_help_about_activate_cb,
1560                               NULL);
1561
1562         g_object_ref (priv->ui_manager);
1563         g_object_unref (gui);
1564
1565         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1566
1567         priv->notebook = gtk_notebook_new ();
1568         gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1569         gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1570         gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1571         gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1572         gtk_widget_show (priv->notebook);
1573
1574         /* Set up accels */
1575         accel_group = gtk_accel_group_new ();
1576         gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1577
1578         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1579                 closure =  g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1580                                            window,
1581                                            NULL);
1582                 gtk_accel_group_connect (accel_group,
1583                                          tab_accel_keys[i],
1584                                          GDK_MOD1_MASK,
1585                                          0,
1586                                          closure);
1587         }
1588
1589         g_object_unref (accel_group);
1590
1591         /* Set up smiley menu */
1592         smiley_manager = empathy_smiley_manager_dup_singleton ();
1593         submenu = empathy_smiley_menu_new (smiley_manager,
1594                                            chat_window_insert_smiley_activate_cb,
1595                                            window);
1596         menu = gtk_ui_manager_get_widget (priv->ui_manager,
1597                 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1598         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1599         g_object_unref (smiley_manager);
1600
1601         /* Set up signals we can't do with ui file since we may need to
1602          * block/unblock them at some later stage.
1603          */
1604
1605         g_signal_connect (priv->dialog,
1606                           "delete_event",
1607                           G_CALLBACK (chat_window_delete_event_cb),
1608                           window);
1609         g_signal_connect (priv->dialog,
1610                           "focus_in_event",
1611                           G_CALLBACK (chat_window_focus_in_event_cb),
1612                           window);
1613         g_signal_connect_after (priv->notebook,
1614                                 "switch_page",
1615                                 G_CALLBACK (chat_window_page_switched_cb),
1616                                 window);
1617         g_signal_connect (priv->notebook,
1618                           "page_added",
1619                           G_CALLBACK (chat_window_page_added_cb),
1620                           window);
1621         g_signal_connect (priv->notebook,
1622                           "page_removed",
1623                           G_CALLBACK (chat_window_page_removed_cb),
1624                           window);
1625
1626         /* Set up drag and drop */
1627         gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1628                            GTK_DEST_DEFAULT_ALL,
1629                            drag_types_dest,
1630                            G_N_ELEMENTS (drag_types_dest),
1631                            GDK_ACTION_MOVE);
1632
1633         g_signal_connect (priv->notebook,
1634                           "drag-data-received",
1635                           G_CALLBACK (chat_window_drag_data_received),
1636                           window);
1637
1638         chat_windows = g_list_prepend (chat_windows, window);
1639
1640         /* Set up private details */
1641         priv->chats = NULL;
1642         priv->chats_new_msg = NULL;
1643         priv->chats_composing = NULL;
1644         priv->current_chat = NULL;
1645 }
1646
1647 EmpathyChatWindow *
1648 empathy_chat_window_new (void)
1649 {
1650         return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1651 }
1652
1653 /* Returns the window to open a new tab in if there is only one window
1654  * visble, otherwise, returns NULL indicating that a new window should
1655  * be added.
1656  */
1657 EmpathyChatWindow *
1658 empathy_chat_window_get_default (void)
1659 {
1660         GList    *l;
1661         gboolean  separate_windows = TRUE;
1662
1663         empathy_conf_get_bool (empathy_conf_get (),
1664                               EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1665                               &separate_windows);
1666
1667         if (separate_windows) {
1668                 /* Always create a new window */
1669                 return NULL;
1670         }
1671
1672         for (l = chat_windows; l; l = l->next) {
1673                 EmpathyChatWindow *chat_window;
1674                 GtkWidget         *dialog;
1675
1676                 chat_window = l->data;
1677
1678                 dialog = empathy_chat_window_get_dialog (chat_window);
1679                 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1680                         /* Found a visible window on this desktop */
1681                         return chat_window;
1682                 }
1683         }
1684
1685         return NULL;
1686 }
1687
1688 GtkWidget *
1689 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1690 {
1691         EmpathyChatWindowPriv *priv;
1692
1693         g_return_val_if_fail (window != NULL, NULL);
1694
1695         priv = GET_PRIV (window);
1696
1697         return priv->dialog;
1698 }
1699
1700 void
1701 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1702                               EmpathyChat       *chat)
1703 {
1704         EmpathyChatWindowPriv *priv;
1705         GtkWidget             *label;
1706         GtkWidget             *popup_label;
1707         GtkWidget             *child;
1708         gint                   x, y, w, h;
1709
1710         g_return_if_fail (window != NULL);
1711         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1712
1713         priv = GET_PRIV (window);
1714
1715         /* Reference the chat object */
1716         g_object_ref (chat);
1717
1718         /* If this window has just been created, position it */
1719         if (priv->chats == NULL) {
1720                 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1721
1722                 if (x >= 0 && y >= 0) {
1723                         /* Let the window manager position it if we don't have
1724                          * good x, y coordinates.
1725                          */
1726                         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1727                 }
1728
1729                 if (w > 0 && h > 0) {
1730                         /* Use the defaults from the ui file if we don't have
1731                          * good w, h geometry.
1732                          */
1733                         gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1734                 }
1735         }
1736
1737         child = GTK_WIDGET (chat);
1738         label = chat_window_create_label (window, chat, TRUE);
1739         popup_label = chat_window_create_label (window, chat, FALSE);
1740         gtk_widget_show (child);
1741
1742         g_signal_connect (chat, "notify::name",
1743                           G_CALLBACK (chat_window_chat_notify_cb),
1744                           NULL);
1745         g_signal_connect (chat, "notify::subject",
1746                           G_CALLBACK (chat_window_chat_notify_cb),
1747                           NULL);
1748         g_signal_connect (chat, "notify::remote-contact",
1749                           G_CALLBACK (chat_window_chat_notify_cb),
1750                           NULL);
1751         chat_window_chat_notify_cb (chat);
1752
1753         gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1754         gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1755         gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1756         gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1757                                             TRUE, TRUE, GTK_PACK_START);
1758
1759         DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1760 }
1761
1762 void
1763 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1764                                  EmpathyChat       *chat)
1765 {
1766         EmpathyChatWindowPriv *priv;
1767         gint                   position;
1768         EmpathyContact        *remote_contact;
1769
1770         g_return_if_fail (window != NULL);
1771         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1772
1773         priv = GET_PRIV (window);
1774
1775         g_signal_handlers_disconnect_by_func (chat,
1776                                               chat_window_chat_notify_cb,
1777                                               NULL);
1778         remote_contact = g_object_get_data (G_OBJECT (chat),
1779                                             "chat-window-remote-contact");
1780         if (remote_contact) {
1781                 g_signal_handlers_disconnect_by_func (remote_contact,
1782                                                       chat_window_update_chat_tab,
1783                                                       chat);
1784         }
1785
1786         position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1787                                           GTK_WIDGET (chat));
1788         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1789
1790         DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1791
1792         g_object_unref (chat);
1793 }
1794
1795 void
1796 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1797                                EmpathyChatWindow *new_window,
1798                                EmpathyChat       *chat)
1799 {
1800         GtkWidget *widget;
1801
1802         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1803         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1804         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1805
1806         widget = GTK_WIDGET (chat);
1807
1808         DEBUG ("Chat moving with widget:%p (%d references)", widget,
1809                 G_OBJECT (widget)->ref_count);
1810
1811         /* We reference here to make sure we don't loose the widget
1812          * and the EmpathyChat object during the move.
1813          */
1814         g_object_ref (chat);
1815         g_object_ref (widget);
1816
1817         empathy_chat_window_remove_chat (old_window, chat);
1818         empathy_chat_window_add_chat (new_window, chat);
1819
1820         g_object_unref (widget);
1821         g_object_unref (chat);
1822 }
1823
1824 void
1825 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1826                                     EmpathyChat       *chat)
1827 {
1828         EmpathyChatWindowPriv *priv;
1829         gint                  page_num;
1830
1831         g_return_if_fail (window != NULL);
1832         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1833
1834         priv = GET_PRIV (window);
1835
1836         page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1837                                           GTK_WIDGET (chat));
1838         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1839                                        page_num);
1840 }
1841
1842 gboolean
1843 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1844 {
1845         EmpathyChatWindowPriv *priv;
1846         gboolean              has_focus;
1847
1848         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1849
1850         priv = GET_PRIV (window);
1851
1852         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1853
1854         return has_focus;
1855 }
1856
1857 EmpathyChat *
1858 empathy_chat_window_find_chat (EmpathyAccount   *account,
1859                                const gchar *id)
1860 {
1861         GList *l;
1862
1863         g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1864
1865         for (l = chat_windows; l; l = l->next) {
1866                 EmpathyChatWindowPriv *priv;
1867                 EmpathyChatWindow     *window;
1868                 GList                *ll;
1869
1870                 window = l->data;
1871                 priv = GET_PRIV (window);
1872
1873                 for (ll = priv->chats; ll; ll = ll->next) {
1874                         EmpathyChat *chat;
1875
1876                         chat = ll->data;
1877
1878                         if (account == empathy_chat_get_account (chat) &&
1879                             !tp_strdiff (id, empathy_chat_get_id (chat))) {
1880                                 return chat;
1881                         }
1882                 }
1883         }
1884
1885         return NULL;
1886 }
1887
1888 void
1889 empathy_chat_window_present_chat (EmpathyChat *chat)
1890 {
1891         EmpathyChatWindow     *window;
1892         EmpathyChatWindowPriv *priv;
1893
1894         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1895
1896         window = chat_window_find_chat (chat);
1897
1898         /* If the chat has no window, create one */
1899         if (window == NULL) {
1900                 window = empathy_chat_window_get_default ();
1901                 if (!window) {
1902                         window = empathy_chat_window_new ();
1903                 }
1904
1905                 empathy_chat_window_add_chat (window, chat);
1906         }
1907
1908         priv = GET_PRIV (window);
1909         empathy_chat_window_switch_to_chat (window, chat);
1910         empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1911
1912         gtk_widget_grab_focus (chat->input_text_view);
1913 }
1914