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