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