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