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