]> git.0d.be Git - empathy.git/blob - src/empathy-chat-window.c
Code cleanup
[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         needs_urgency = empathy_message_should_highlight (message);
766
767         if (needs_urgency && !has_focus) {
768                 chat_window_set_urgency_hint (window, TRUE);
769         }
770
771         if (!g_list_find (priv->chats_new_msg, chat)) {
772                 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
773                 chat_window_update_chat_tab (chat);
774         }
775 }
776
777 static GtkNotebook *
778 chat_window_detach_hook (GtkNotebook *source,
779                          GtkWidget   *page,
780                          gint         x,
781                          gint         y,
782                          gpointer     user_data)
783 {
784         EmpathyChatWindowPriv *priv;
785         EmpathyChatWindow     *window, *new_window;
786         EmpathyChat           *chat;
787
788         chat = EMPATHY_CHAT (page);
789         window = chat_window_find_chat (chat);
790
791         new_window = empathy_chat_window_new ();
792         priv = GET_PRIV (new_window);
793
794         empathy_debug (DEBUG_DOMAIN, "Detach hook called");
795
796         empathy_chat_window_move_chat (window, new_window, chat);
797
798         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
799         gtk_widget_show (priv->dialog);
800
801         return NULL;
802 }
803
804 static void
805 chat_window_page_switched_cb (GtkNotebook      *notebook,
806                               GtkNotebookPage  *page,
807                               gint              page_num,
808                               EmpathyChatWindow *window)
809 {
810         EmpathyChatWindowPriv *priv;
811         EmpathyChat           *chat;
812         GtkWidget            *child;
813
814         empathy_debug (DEBUG_DOMAIN, "Page switched");
815
816         priv = GET_PRIV (window);
817
818         child = gtk_notebook_get_nth_page (notebook, page_num);
819         chat = EMPATHY_CHAT (child);
820
821         if (priv->page_added) {
822                 priv->page_added = FALSE;
823                 empathy_chat_scroll_down (chat);
824         }
825         else if (priv->current_chat == chat) {
826                 return;
827         }
828
829         priv->current_chat = chat;
830         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
831
832         chat_window_update_chat_tab (chat);
833 }
834
835 static void
836 chat_window_page_added_cb (GtkNotebook      *notebook,
837                            GtkWidget        *child,
838                            guint             page_num,
839                            EmpathyChatWindow *window)
840 {
841         EmpathyChatWindowPriv *priv;
842         EmpathyChat           *chat;
843
844         priv = GET_PRIV (window);
845
846         /* If we just received DND to the same window, we don't want
847          * to do anything here like removing the tab and then readding
848          * it, so we return here and in "page-added".
849          */
850         if (priv->dnd_same_window) {
851                 empathy_debug (DEBUG_DOMAIN, "Page added (back to the same window)");
852                 priv->dnd_same_window = FALSE;
853                 return;
854         }
855
856         empathy_debug (DEBUG_DOMAIN, "Page added");
857
858         /* Get chat object */
859         chat = EMPATHY_CHAT (child);
860
861         /* Connect chat signals for this window */
862         g_signal_connect (chat, "composing",
863                           G_CALLBACK (chat_window_composing_cb),
864                           window);
865         g_signal_connect (chat, "new-message",
866                           G_CALLBACK (chat_window_new_message_cb),
867                           window);
868
869         /* Set flag so we know to perform some special operations on
870          * switch page due to the new page being added.
871          */
872         priv->page_added = TRUE;
873
874         /* Get list of chats up to date */
875         priv->chats = g_list_append (priv->chats, chat);
876
877         chat_window_update_chat_tab (chat);
878 }
879
880 static void
881 chat_window_page_removed_cb (GtkNotebook      *notebook,
882                              GtkWidget        *child,
883                              guint             page_num,
884                              EmpathyChatWindow *window)
885 {
886         EmpathyChatWindowPriv *priv;
887         EmpathyChat           *chat;
888
889         priv = GET_PRIV (window);
890
891         /* If we just received DND to the same window, we don't want
892          * to do anything here like removing the tab and then readding
893          * it, so we return here and in "page-added".
894          */
895         if (priv->dnd_same_window) {
896                 empathy_debug (DEBUG_DOMAIN, "Page removed (and will be readded to same window)");
897                 return;
898         }
899
900         empathy_debug (DEBUG_DOMAIN, "Page removed");
901
902         /* Get chat object */
903         chat = EMPATHY_CHAT (child);
904
905         /* Disconnect all signal handlers for this chat and this window */
906         g_signal_handlers_disconnect_by_func (chat,
907                                               G_CALLBACK (chat_window_composing_cb),
908                                               window);
909         g_signal_handlers_disconnect_by_func (chat,
910                                               G_CALLBACK (chat_window_new_message_cb),
911                                               window);
912
913         /* Keep list of chats up to date */
914         priv->chats = g_list_remove (priv->chats, chat);
915         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
916         priv->chats_composing = g_list_remove (priv->chats_composing, chat);
917
918         if (priv->chats == NULL) {
919                 g_object_unref (window);
920         } else {
921                 chat_window_update (window);
922         }
923 }
924
925 static gboolean
926 chat_window_focus_in_event_cb (GtkWidget        *widget,
927                                GdkEvent         *event,
928                                EmpathyChatWindow *window)
929 {
930         EmpathyChatWindowPriv *priv;
931
932         empathy_debug (DEBUG_DOMAIN, "Focus in event, updating title");
933
934         priv = GET_PRIV (window);
935
936         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
937
938         chat_window_set_urgency_hint (window, FALSE);
939         
940         /* Update the title, since we now mark all unread messages as read. */
941         chat_window_update_chat_tab (priv->current_chat);
942
943         return FALSE;
944 }
945
946 static void
947 chat_window_drag_data_received (GtkWidget        *widget,
948                                 GdkDragContext   *context,
949                                 int               x,
950                                 int               y,
951                                 GtkSelectionData *selection,
952                                 guint             info,
953                                 guint             time,
954                                 EmpathyChatWindow *window)
955 {
956         if (info == DND_DRAG_TYPE_CONTACT_ID) {
957                 EmpathyChat           *chat;
958                 EmpathyChatWindow     *old_window;
959                 McAccount             *account;
960                 const gchar           *id = NULL;
961                 gchar                **strv;
962
963                 if (selection) {
964                         id = (const gchar*) selection->data;
965                 }
966
967                 empathy_debug (DEBUG_DOMAIN, "DND contact from roster with id:'%s'", id);
968                 
969                 strv = g_strsplit (id, "/", 2);
970                 account = mc_account_lookup (strv[0]);
971                 chat = empathy_chat_window_find_chat (account, strv[1]);
972
973                 if (!chat) {
974                         empathy_chat_with_contact_id (account, strv[2]);
975                         g_object_unref (account);
976                         g_strfreev (strv);
977                         return;
978                 }
979                 g_object_unref (account);
980                 g_strfreev (strv);
981
982                 old_window = chat_window_find_chat (chat);              
983                 if (old_window) {
984                         if (old_window == window) {
985                                 gtk_drag_finish (context, TRUE, FALSE, time);
986                                 return;
987                         }
988                         
989                         empathy_chat_window_move_chat (old_window, window, chat);
990                 } else {
991                         empathy_chat_window_add_chat (window, chat);
992                 }
993                 
994                 /* Added to take care of any outstanding chat events */
995                 empathy_chat_window_present_chat (chat);
996
997                 /* We should return TRUE to remove the data when doing
998                  * GDK_ACTION_MOVE, but we don't here otherwise it has
999                  * weird consequences, and we handle that internally
1000                  * anyway with add_chat() and remove_chat().
1001                  */
1002                 gtk_drag_finish (context, TRUE, FALSE, time);
1003         }
1004         else if (info == DND_DRAG_TYPE_TAB) {
1005                 EmpathyChat        *chat = NULL;
1006                 EmpathyChatWindow  *old_window;
1007                 GtkWidget        **child = NULL;
1008
1009                 empathy_debug (DEBUG_DOMAIN, "DND tab");
1010
1011                 if (selection) {
1012                         child = (void*) selection->data;
1013                 }
1014
1015                 if (child) {
1016                         chat = EMPATHY_CHAT (child);
1017                 }
1018
1019                 old_window = chat_window_find_chat (chat);
1020                 if (old_window) {
1021                         EmpathyChatWindowPriv *priv;
1022
1023                         priv = GET_PRIV (window);
1024
1025                         if (old_window == window) {
1026                                 empathy_debug (DEBUG_DOMAIN, "DND tab (within same window)");
1027                                 priv->dnd_same_window = TRUE;
1028                                 gtk_drag_finish (context, TRUE, FALSE, time);
1029                                 return;
1030                         }
1031                         
1032                         priv->dnd_same_window = FALSE;
1033                 }
1034
1035                 /* We should return TRUE to remove the data when doing
1036                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1037                  * weird consequences, and we handle that internally
1038                  * anyway with add_chat() and remove_chat().
1039                  */
1040                 gtk_drag_finish (context, TRUE, FALSE, time);
1041         } else {
1042                 empathy_debug (DEBUG_DOMAIN, "DND from unknown source");
1043                 gtk_drag_finish (context, FALSE, FALSE, time);
1044         }
1045 }
1046
1047 static void
1048 chat_window_finalize (GObject *object)
1049 {
1050         EmpathyChatWindow     *window;
1051         EmpathyChatWindowPriv *priv;
1052
1053         window = EMPATHY_CHAT_WINDOW (object);
1054         priv = GET_PRIV (window);
1055
1056         empathy_debug (DEBUG_DOMAIN, "Finalized: %p", object);
1057
1058         if (priv->save_geometry_id != 0) {
1059                 g_source_remove (priv->save_geometry_id);
1060         }
1061
1062         chat_windows = g_list_remove (chat_windows, window);
1063         gtk_widget_destroy (priv->dialog);
1064
1065         G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1066 }
1067
1068 static void
1069 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1070 {
1071         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1072
1073         object_class->finalize = chat_window_finalize;
1074
1075         g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1076
1077         /* Set up a style for the close button with no focus padding. */
1078         gtk_rc_parse_string (
1079                 "style \"empathy-close-button-style\"\n"
1080                 "{\n"
1081                 "  GtkWidget::focus-padding = 0\n"
1082                 "  xthickness = 0\n"
1083                 "  ythickness = 0\n"
1084                 "}\n"
1085                 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1086
1087         gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1088 }
1089
1090 static void
1091 empathy_chat_window_init (EmpathyChatWindow *window)
1092 {
1093         EmpathyChatWindowPriv *priv;
1094         GladeXML             *glade;
1095         GtkAccelGroup        *accel_group;
1096         GClosure             *closure;
1097         GtkWidget            *menu_conv;
1098         GtkWidget            *menu;
1099         gint                  i;
1100         GtkWidget            *chat_vbox;
1101         gchar                *filename;
1102
1103         priv = GET_PRIV (window);
1104
1105         filename = empathy_file_lookup ("empathy-chat-window.glade", "src");
1106         glade = empathy_glade_get_file (filename,
1107                                        "chat_window",
1108                                        NULL,
1109                                        "chat_window", &priv->dialog,
1110                                        "chat_vbox", &chat_vbox,
1111                                        "menu_conv", &menu_conv,
1112                                        "menu_conv_clear", &priv->menu_conv_clear,
1113                                        "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1114                                        "menu_conv_contact", &priv->menu_conv_contact,
1115                                        "menu_conv_close", &priv->menu_conv_close,
1116                                        "menu_edit_cut", &priv->menu_edit_cut,
1117                                        "menu_edit_copy", &priv->menu_edit_copy,
1118                                        "menu_edit_paste", &priv->menu_edit_paste,
1119                                        "menu_tabs_next", &priv->menu_tabs_next,
1120                                        "menu_tabs_prev", &priv->menu_tabs_prev,
1121                                        "menu_tabs_left", &priv->menu_tabs_left,
1122                                        "menu_tabs_right", &priv->menu_tabs_right,
1123                                        "menu_tabs_detach", &priv->menu_tabs_detach,
1124                                        "menu_help_contents", &priv->menu_help_contents,
1125                                        "menu_help_about", &priv->menu_help_about,
1126                                        NULL);
1127         g_free (filename);
1128
1129         empathy_glade_connect (glade,
1130                               window,
1131                               "chat_window", "configure-event", chat_window_configure_event_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         empathy_debug (DEBUG_DOMAIN, 
1339                       "Chat added (%d references)",
1340                       G_OBJECT (chat)->ref_count);
1341 }
1342
1343 void
1344 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1345                                  EmpathyChat       *chat)
1346 {
1347         EmpathyChatWindowPriv *priv;
1348         gint                   position;
1349         EmpathyContact        *remote_contact;
1350
1351         g_return_if_fail (window != NULL);
1352         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1353
1354         priv = GET_PRIV (window);
1355
1356         g_signal_handlers_disconnect_by_func (chat,
1357                                               chat_window_chat_notify_cb,
1358                                               NULL);
1359         remote_contact = g_object_get_data (G_OBJECT (chat),
1360                                             "chat-window-remote-contact");
1361         if (remote_contact) {
1362                 g_signal_handlers_disconnect_by_func (remote_contact,
1363                                                       chat_window_update_chat_tab,
1364                                                       chat);
1365         }
1366
1367         position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1368                                           GTK_WIDGET (chat));
1369         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1370
1371         empathy_debug (DEBUG_DOMAIN, 
1372                       "Chat removed (%d references)", 
1373                       G_OBJECT (chat)->ref_count - 1);
1374
1375         g_object_unref (chat);
1376 }
1377
1378 void
1379 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1380                                EmpathyChatWindow *new_window,
1381                                EmpathyChat       *chat)
1382 {
1383         GtkWidget *widget;
1384
1385         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1386         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1387         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1388
1389         widget = GTK_WIDGET (chat);
1390
1391         empathy_debug (DEBUG_DOMAIN,
1392                       "Chat moving with widget:%p (%d references)", 
1393                       widget,
1394                       G_OBJECT (widget)->ref_count);
1395
1396         /* We reference here to make sure we don't loose the widget
1397          * and the EmpathyChat object during the move.
1398          */
1399         g_object_ref (chat);
1400         g_object_ref (widget);
1401
1402         empathy_chat_window_remove_chat (old_window, chat);
1403         empathy_chat_window_add_chat (new_window, chat);
1404
1405         g_object_unref (widget);
1406         g_object_unref (chat);
1407 }
1408
1409 void
1410 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1411                                     EmpathyChat       *chat)
1412 {
1413         EmpathyChatWindowPriv *priv;
1414         gint                  page_num;
1415
1416         g_return_if_fail (window != NULL);
1417         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1418
1419         priv = GET_PRIV (window);
1420
1421         page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1422                                           GTK_WIDGET (chat));
1423         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1424                                        page_num);
1425 }
1426
1427 gboolean
1428 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1429 {
1430         EmpathyChatWindowPriv *priv;
1431         gboolean              has_focus;
1432
1433         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1434
1435         priv = GET_PRIV (window);
1436
1437         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1438
1439         return has_focus;
1440 }
1441
1442 EmpathyChat *
1443 empathy_chat_window_find_chat (McAccount   *account,
1444                                const gchar *id)
1445 {
1446         GList *l;
1447
1448         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1449         g_return_val_if_fail (!G_STR_EMPTY (id), NULL);
1450
1451         for (l = chat_windows; l; l = l->next) {
1452                 EmpathyChatWindowPriv *priv;
1453                 EmpathyChatWindow     *window;
1454                 GList                *ll;
1455
1456                 window = l->data;
1457                 priv = GET_PRIV (window);
1458
1459                 for (ll = priv->chats; ll; ll = ll->next) {
1460                         EmpathyChat *chat;
1461
1462                         chat = ll->data;
1463
1464                         if (empathy_account_equal (account, empathy_chat_get_account (chat)) &&
1465                             strcmp (id, empathy_chat_get_id (chat)) == 0) {
1466                                 return chat;
1467                         }
1468                 }
1469         }
1470
1471         return NULL;
1472 }
1473
1474 void
1475 empathy_chat_window_present_chat (EmpathyChat *chat)
1476 {
1477         EmpathyChatWindow     *window;
1478         EmpathyChatWindowPriv *priv;
1479
1480         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1481
1482         window = chat_window_find_chat (chat);
1483
1484         /* If the chat has no window, create one */
1485         if (window == NULL) {
1486                 window = empathy_chat_window_get_default ();
1487                 if (!window) {
1488                         window = empathy_chat_window_new ();
1489                 }
1490
1491                 empathy_chat_window_add_chat (window, chat);
1492         }
1493
1494         priv = GET_PRIV (window);
1495         empathy_chat_window_switch_to_chat (window, chat);
1496         empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1497
1498         gtk_widget_grab_focus (chat->input_text_view); 
1499 }
1500
1501 #if 0
1502 static gboolean
1503 chat_window_should_play_sound (EmpathyChatWindow *window)
1504 {
1505         EmpathyChatWindowPriv *priv = GET_PRIV (window);
1506         gboolean               has_focus = FALSE;
1507
1508         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1509
1510         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1511
1512         return !has_focus;
1513 }
1514 #endif