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