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