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