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