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