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