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