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