]> git.0d.be Git - empathy.git/blob - src/empathy-chat-window.c
Use markup for chat-window tooltips
[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         const gchar           *subject;
356         GtkWidget             *widget;
357         GString               *tooltip;
358         gchar                 *markup;
359         const gchar           *icon_name;
360
361         window = chat_window_find_chat (chat);
362         if (!window) {
363                 return;
364         }
365         priv = GET_PRIV (window);
366
367         /* Get information */
368         name = empathy_chat_get_name (chat);
369         subject = empathy_chat_get_subject (chat);
370         remote_contact = empathy_chat_get_remote_contact (chat);
371
372         DEBUG ("Updating chat tab, name=%s, subject=%s, remote_contact=%p",
373                 name, subject, remote_contact);
374
375         /* Update tab image */
376         if (g_list_find (priv->chats_new_msg, chat)) {
377                 icon_name = EMPATHY_IMAGE_MESSAGE;
378         }
379         else if (g_list_find (priv->chats_composing, chat)) {
380                 icon_name = EMPATHY_IMAGE_TYPING;
381         }
382         else if (remote_contact) {
383                 icon_name = empathy_icon_name_for_contact (remote_contact);
384         } else {
385                 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
386         }
387         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
388         gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
389
390         /* Update tab tooltip */
391         tooltip = g_string_new (NULL);
392
393         if (remote_contact) {
394                 markup = g_markup_printf_escaped ("<b>%s</b>\n%s",
395                                                   empathy_contact_get_id (remote_contact),
396                                                   empathy_contact_get_status (remote_contact));
397                 g_string_append (tooltip, markup);
398                 g_free (markup);
399         }
400         else {
401                 markup = g_markup_printf_escaped ("<b>%s</b>", name);
402                 g_string_append (tooltip, markup);
403                 g_free (markup);
404         }
405
406         if (subject) {
407                 markup = g_markup_printf_escaped ("\n<b>%s</b> %s", _("Topic:"), subject);
408                 g_string_append (tooltip, markup);
409                 g_free (markup);
410         }
411         if (g_list_find (priv->chats_composing, chat)) {
412                 markup = g_markup_printf_escaped ("\n%s", _("Typing a message."));
413                 g_string_append (tooltip, markup);
414                 g_free (markup);
415         }
416
417         markup = g_string_free (tooltip, FALSE);
418         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
419         gtk_widget_set_tooltip_markup (widget, markup);
420         g_free (markup);
421
422         /* Update tab label */
423         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
424         gtk_label_set_text (GTK_LABEL (widget), name);
425
426         /* Update the window if it's the current chat */
427         if (priv->current_chat == chat) {
428                 chat_window_update (window);
429         }
430 }
431
432 static void
433 chat_window_chat_notify_cb (EmpathyChat *chat)
434 {
435         EmpathyContact *old_remote_contact;
436         EmpathyContact *remote_contact = NULL;
437
438         old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
439         remote_contact = empathy_chat_get_remote_contact (chat);
440
441         if (old_remote_contact != remote_contact) {
442                 /* The remote-contact associated with the chat changed, we need
443                  * to keep track of any change of that contact and update the
444                  * window each time. */
445                 if (remote_contact) {
446                         g_signal_connect_swapped (remote_contact, "notify",
447                                                   G_CALLBACK (chat_window_update_chat_tab),
448                                                   chat);
449                 }
450                 if (old_remote_contact) {
451                         g_signal_handlers_disconnect_by_func (old_remote_contact,
452                                                               chat_window_update_chat_tab,
453                                                               chat);
454                 }
455
456                 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
457                                    remote_contact);
458         }
459
460         chat_window_update_chat_tab (chat);
461 }
462
463 static void
464 chat_window_insert_smiley_activate_cb (GtkWidget         *menuitem,
465                                        EmpathyChatWindow *window)
466 {
467         EmpathyChatWindowPriv *priv;
468         EmpathyChat           *chat;
469         GtkTextBuffer        *buffer;
470         GtkTextIter           iter;
471         const gchar          *smiley;
472
473         priv = GET_PRIV (window);
474
475         chat = priv->current_chat;
476
477         smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
478
479         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
480         gtk_text_buffer_get_end_iter (buffer, &iter);
481         gtk_text_buffer_insert (buffer, &iter,
482                                 smiley, -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_selection_bounds (buffer, NULL, NULL)) {
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_selection_bounds (priv->current_chat->view, 
662                                                                    NULL, NULL);
663
664                 gtk_widget_set_sensitive (priv->menu_edit_cut, FALSE);
665                 gtk_widget_set_sensitive (priv->menu_edit_copy, selection);
666         }
667
668         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
669         text_available = gtk_clipboard_wait_is_text_available (clipboard);
670         gtk_widget_set_sensitive (priv->menu_edit_paste, text_available);
671 }
672
673 static void
674 chat_window_cut_activate_cb (GtkWidget        *menuitem,
675                              EmpathyChatWindow *window)
676 {
677         EmpathyChatWindowPriv *priv;
678
679         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
680
681         priv = GET_PRIV (window);
682
683         empathy_chat_cut (priv->current_chat);
684 }
685
686 static void
687 chat_window_copy_activate_cb (GtkWidget        *menuitem,
688                               EmpathyChatWindow *window)
689 {
690         EmpathyChatWindowPriv *priv;
691
692         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
693
694         priv = GET_PRIV (window);
695
696         empathy_chat_copy (priv->current_chat);
697 }
698
699 static void
700 chat_window_paste_activate_cb (GtkWidget        *menuitem,
701                                EmpathyChatWindow *window)
702 {
703         EmpathyChatWindowPriv *priv;
704
705         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
706
707         priv = GET_PRIV (window);
708
709         empathy_chat_paste (priv->current_chat);
710 }
711
712 static void
713 chat_window_tabs_left_activate_cb (GtkWidget        *menuitem,
714                                    EmpathyChatWindow *window)
715 {
716         EmpathyChatWindowPriv *priv;
717         EmpathyChat           *chat;
718         gint                  index;
719
720         priv = GET_PRIV (window);
721
722         chat = priv->current_chat;
723         index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
724         if (index <= 0) {
725                 return;
726         }
727
728         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
729                                     GTK_WIDGET (chat),
730                                     index - 1);
731 }
732
733 static void
734 chat_window_tabs_right_activate_cb (GtkWidget        *menuitem,
735                                     EmpathyChatWindow *window)
736 {
737         EmpathyChatWindowPriv *priv;
738         EmpathyChat           *chat;
739         gint                  index;
740
741         priv = GET_PRIV (window);
742
743         chat = priv->current_chat;
744         index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
745
746         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
747                                     GTK_WIDGET (chat),
748                                     index + 1);
749 }
750
751 static void
752 chat_window_detach_activate_cb (GtkWidget        *menuitem,
753                                 EmpathyChatWindow *window)
754 {
755         EmpathyChatWindowPriv *priv;
756         EmpathyChatWindow     *new_window;
757         EmpathyChat           *chat;
758
759         priv = GET_PRIV (window);
760
761         chat = priv->current_chat;
762         new_window = empathy_chat_window_new ();
763
764         empathy_chat_window_move_chat (window, new_window, chat);
765
766         priv = GET_PRIV (new_window);
767         gtk_widget_show (priv->dialog);
768 }
769
770 static void
771 chat_window_help_contents_cb (GtkWidget         *menuitem,
772                               EmpathyChatWindow *window)
773 {
774         empathy_url_show ("ghelp:empathy?chat");
775 }
776
777 static void
778 chat_window_help_about_cb (GtkWidget         *menuitem,
779                            EmpathyChatWindow *window)
780 {
781         EmpathyChatWindowPriv *priv = GET_PRIV (window);
782
783         empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
784 }
785
786 static gboolean
787 chat_window_delete_event_cb (GtkWidget        *dialog,
788                              GdkEvent         *event,
789                              EmpathyChatWindow *window)
790 {
791         EmpathyChatWindowPriv *priv = GET_PRIV (window);
792
793         DEBUG ("Delete event received");
794
795         g_object_ref (window);
796         while (priv->chats) {
797                 empathy_chat_window_remove_chat (window, priv->chats->data);
798         }
799         g_object_unref (window);
800
801         return TRUE;
802 }
803
804 static void
805 chat_window_composing_cb (EmpathyChat       *chat,
806                           gboolean          is_composing,
807                           EmpathyChatWindow *window)
808 {
809         EmpathyChatWindowPriv *priv;
810
811         priv = GET_PRIV (window);
812
813         if (is_composing && !g_list_find (priv->chats_composing, chat)) {
814                 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
815         } else {
816                 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
817         }
818
819         chat_window_update_chat_tab (chat);
820 }
821
822 static void
823 chat_window_set_urgency_hint (EmpathyChatWindow *window,
824                               gboolean          urgent)
825 {
826         EmpathyChatWindowPriv *priv;
827
828         priv = GET_PRIV (window);
829
830         DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
831         gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
832 }
833
834 static void
835 chat_window_new_message_cb (EmpathyChat       *chat,
836                             EmpathyMessage    *message,
837                             EmpathyChatWindow *window)
838 {
839         EmpathyChatWindowPriv *priv;
840         gboolean              has_focus;
841         gboolean              needs_urgency;
842
843         priv = GET_PRIV (window);
844
845         has_focus = empathy_chat_window_has_focus (window);
846
847         if (has_focus && priv->current_chat == chat) {
848                 return;
849         }
850         
851         if (empathy_chat_get_members_count (chat) > 2) {
852                 needs_urgency = empathy_message_should_highlight (message);
853         } else {
854                 needs_urgency = TRUE;
855         }
856
857         if (needs_urgency && !has_focus) {
858                 chat_window_set_urgency_hint (window, TRUE);
859         }
860
861         if (!g_list_find (priv->chats_new_msg, chat)) {
862                 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
863                 chat_window_update_chat_tab (chat);
864         }
865 }
866
867 static GtkNotebook *
868 chat_window_detach_hook (GtkNotebook *source,
869                          GtkWidget   *page,
870                          gint         x,
871                          gint         y,
872                          gpointer     user_data)
873 {
874         EmpathyChatWindowPriv *priv;
875         EmpathyChatWindow     *window, *new_window;
876         EmpathyChat           *chat;
877
878         chat = EMPATHY_CHAT (page);
879         window = chat_window_find_chat (chat);
880
881         new_window = empathy_chat_window_new ();
882         priv = GET_PRIV (new_window);
883
884         DEBUG ("Detach hook called");
885
886         empathy_chat_window_move_chat (window, new_window, chat);
887
888         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
889         gtk_widget_show (priv->dialog);
890
891         return NULL;
892 }
893
894 static void
895 chat_window_page_switched_cb (GtkNotebook      *notebook,
896                               GtkNotebookPage  *page,
897                               gint              page_num,
898                               EmpathyChatWindow *window)
899 {
900         EmpathyChatWindowPriv *priv;
901         EmpathyChat           *chat;
902         GtkWidget            *child;
903
904         DEBUG ("Page switched");
905
906         priv = GET_PRIV (window);
907
908         child = gtk_notebook_get_nth_page (notebook, page_num);
909         chat = EMPATHY_CHAT (child);
910
911         if (priv->page_added) {
912                 priv->page_added = FALSE;
913                 empathy_chat_scroll_down (chat);
914         }
915         else if (priv->current_chat == chat) {
916                 return;
917         }
918
919         priv->current_chat = chat;
920         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
921
922         chat_window_update_chat_tab (chat);
923 }
924
925 static void
926 chat_window_page_added_cb (GtkNotebook      *notebook,
927                            GtkWidget        *child,
928                            guint             page_num,
929                            EmpathyChatWindow *window)
930 {
931         EmpathyChatWindowPriv *priv;
932         EmpathyChat           *chat;
933
934         priv = GET_PRIV (window);
935
936         /* If we just received DND to the same window, we don't want
937          * to do anything here like removing the tab and then readding
938          * it, so we return here and in "page-added".
939          */
940         if (priv->dnd_same_window) {
941                 DEBUG ("Page added (back to the same window)");
942                 priv->dnd_same_window = FALSE;
943                 return;
944         }
945
946         DEBUG ("Page added");
947
948         /* Get chat object */
949         chat = EMPATHY_CHAT (child);
950
951         /* Connect chat signals for this window */
952         g_signal_connect (chat, "composing",
953                           G_CALLBACK (chat_window_composing_cb),
954                           window);
955         g_signal_connect (chat, "new-message",
956                           G_CALLBACK (chat_window_new_message_cb),
957                           window);
958
959         /* Set flag so we know to perform some special operations on
960          * switch page due to the new page being added.
961          */
962         priv->page_added = TRUE;
963
964         /* Get list of chats up to date */
965         priv->chats = g_list_append (priv->chats, chat);
966
967         chat_window_update_chat_tab (chat);
968 }
969
970 static void
971 chat_window_page_removed_cb (GtkNotebook      *notebook,
972                              GtkWidget        *child,
973                              guint             page_num,
974                              EmpathyChatWindow *window)
975 {
976         EmpathyChatWindowPriv *priv;
977         EmpathyChat           *chat;
978
979         priv = GET_PRIV (window);
980
981         /* If we just received DND to the same window, we don't want
982          * to do anything here like removing the tab and then readding
983          * it, so we return here and in "page-added".
984          */
985         if (priv->dnd_same_window) {
986                 DEBUG ("Page removed (and will be readded to same window)");
987                 return;
988         }
989
990         DEBUG ("Page removed");
991
992         /* Get chat object */
993         chat = EMPATHY_CHAT (child);
994
995         /* Disconnect all signal handlers for this chat and this window */
996         g_signal_handlers_disconnect_by_func (chat,
997                                               G_CALLBACK (chat_window_composing_cb),
998                                               window);
999         g_signal_handlers_disconnect_by_func (chat,
1000                                               G_CALLBACK (chat_window_new_message_cb),
1001                                               window);
1002
1003         /* Keep list of chats up to date */
1004         priv->chats = g_list_remove (priv->chats, chat);
1005         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1006         priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1007
1008         if (priv->chats == NULL) {
1009                 g_object_unref (window);
1010         } else {
1011                 chat_window_update (window);
1012         }
1013 }
1014
1015 static gboolean
1016 chat_window_focus_in_event_cb (GtkWidget        *widget,
1017                                GdkEvent         *event,
1018                                EmpathyChatWindow *window)
1019 {
1020         EmpathyChatWindowPriv *priv;
1021
1022         DEBUG ("Focus in event, updating title");
1023
1024         priv = GET_PRIV (window);
1025
1026         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1027
1028         chat_window_set_urgency_hint (window, FALSE);
1029         
1030         /* Update the title, since we now mark all unread messages as read. */
1031         chat_window_update_chat_tab (priv->current_chat);
1032
1033         return FALSE;
1034 }
1035
1036 static void
1037 chat_window_drag_data_received (GtkWidget        *widget,
1038                                 GdkDragContext   *context,
1039                                 int               x,
1040                                 int               y,
1041                                 GtkSelectionData *selection,
1042                                 guint             info,
1043                                 guint             time,
1044                                 EmpathyChatWindow *window)
1045 {
1046         if (info == DND_DRAG_TYPE_CONTACT_ID) {
1047                 EmpathyChat           *chat;
1048                 EmpathyChatWindow     *old_window;
1049                 McAccount             *account;
1050                 const gchar           *id;
1051                 gchar                **strv;
1052
1053                 id = (const gchar*) selection->data;
1054
1055                 DEBUG ("DND contact from roster with id:'%s'", id);
1056                 
1057                 strv = g_strsplit (id, "/", 2);
1058                 account = mc_account_lookup (strv[0]);
1059                 chat = empathy_chat_window_find_chat (account, strv[1]);
1060
1061                 if (!chat) {
1062                         empathy_dispatcher_chat_with_contact_id (account, strv[2]);
1063                         g_object_unref (account);
1064                         g_strfreev (strv);
1065                         return;
1066                 }
1067                 g_object_unref (account);
1068                 g_strfreev (strv);
1069
1070                 old_window = chat_window_find_chat (chat);              
1071                 if (old_window) {
1072                         if (old_window == window) {
1073                                 gtk_drag_finish (context, TRUE, FALSE, time);
1074                                 return;
1075                         }
1076                         
1077                         empathy_chat_window_move_chat (old_window, window, chat);
1078                 } else {
1079                         empathy_chat_window_add_chat (window, chat);
1080                 }
1081                 
1082                 /* Added to take care of any outstanding chat events */
1083                 empathy_chat_window_present_chat (chat);
1084
1085                 /* We should return TRUE to remove the data when doing
1086                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1087                  * weird consequences, and we handle that internally
1088                  * anyway with add_chat() and remove_chat().
1089                  */
1090                 gtk_drag_finish (context, TRUE, FALSE, time);
1091         }
1092         else if (info == DND_DRAG_TYPE_TAB) {
1093                 EmpathyChat        **chat;
1094                 EmpathyChatWindow   *old_window = NULL;
1095
1096                 DEBUG ("DND tab");
1097
1098                 chat = (void*) selection->data;
1099                 old_window = chat_window_find_chat (*chat);
1100
1101                 if (old_window) {
1102                         EmpathyChatWindowPriv *priv;
1103
1104                         priv = GET_PRIV (window);
1105
1106                         if (old_window == window) {
1107                                 DEBUG ("DND tab (within same window)");
1108                                 priv->dnd_same_window = TRUE;
1109                                 gtk_drag_finish (context, TRUE, FALSE, time);
1110                                 return;
1111                         }
1112                         
1113                         priv->dnd_same_window = FALSE;
1114                 }
1115
1116                 /* We should return TRUE to remove the data when doing
1117                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1118                  * weird consequences, and we handle that internally
1119                  * anyway with add_chat() and remove_chat().
1120                  */
1121                 gtk_drag_finish (context, TRUE, FALSE, time);
1122         } else {
1123                 DEBUG ("DND from unknown source");
1124                 gtk_drag_finish (context, FALSE, FALSE, time);
1125         }
1126 }
1127
1128 static void
1129 chat_window_finalize (GObject *object)
1130 {
1131         EmpathyChatWindow     *window;
1132         EmpathyChatWindowPriv *priv;
1133
1134         window = EMPATHY_CHAT_WINDOW (object);
1135         priv = GET_PRIV (window);
1136
1137         DEBUG ("Finalized: %p", object);
1138
1139         g_object_unref (priv->chatroom_manager);
1140         if (priv->save_geometry_id != 0) {
1141                 g_source_remove (priv->save_geometry_id);
1142         }
1143
1144         chat_windows = g_list_remove (chat_windows, window);
1145         gtk_widget_destroy (priv->dialog);
1146
1147         G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1148 }
1149
1150 static void
1151 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1152 {
1153         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1154
1155         object_class->finalize = chat_window_finalize;
1156
1157         g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1158
1159         /* Set up a style for the close button with no focus padding. */
1160         gtk_rc_parse_string (
1161                 "style \"empathy-close-button-style\"\n"
1162                 "{\n"
1163                 "  GtkWidget::focus-padding = 0\n"
1164                 "  xthickness = 0\n"
1165                 "  ythickness = 0\n"
1166                 "}\n"
1167                 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1168
1169         gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1170 }
1171
1172 static void
1173 empathy_chat_window_init (EmpathyChatWindow *window)
1174 {
1175         GladeXML              *glade;
1176         GtkAccelGroup         *accel_group;
1177         GClosure              *closure;
1178         GtkWidget             *menu_conv;
1179         GtkWidget             *menu;
1180         gint                   i;
1181         GtkWidget             *chat_vbox;
1182         gchar                 *filename;
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         menu = empathy_chat_view_get_smiley_menu (
1258                 G_CALLBACK (chat_window_insert_smiley_activate_cb),
1259                 window);
1260         gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->menu_conv_insert_smiley), menu);
1261
1262         /* Set up signals we can't do with glade since we may need to
1263          * block/unblock them at some later stage.
1264          */
1265
1266         g_signal_connect (priv->dialog,
1267                           "delete_event",
1268                           G_CALLBACK (chat_window_delete_event_cb),
1269                           window);
1270
1271         g_signal_connect_swapped (priv->menu_tabs_prev,
1272                                   "activate",
1273                                   G_CALLBACK (gtk_notebook_prev_page),
1274                                   priv->notebook);
1275         g_signal_connect_swapped (priv->menu_tabs_next,
1276                                   "activate",
1277                                   G_CALLBACK (gtk_notebook_next_page),
1278                                   priv->notebook);
1279
1280         g_signal_connect (priv->dialog,
1281                           "focus_in_event",
1282                           G_CALLBACK (chat_window_focus_in_event_cb),
1283                           window);
1284         g_signal_connect_after (priv->notebook,
1285                                 "switch_page",
1286                                 G_CALLBACK (chat_window_page_switched_cb),
1287                                 window);
1288         g_signal_connect (priv->notebook,
1289                           "page_added",
1290                           G_CALLBACK (chat_window_page_added_cb),
1291                           window);
1292         g_signal_connect (priv->notebook,
1293                           "page_removed",
1294                           G_CALLBACK (chat_window_page_removed_cb),
1295                           window);
1296
1297         /* Set up drag and drop */
1298         gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1299                            GTK_DEST_DEFAULT_ALL,
1300                            drag_types_dest,
1301                            G_N_ELEMENTS (drag_types_dest),
1302                            GDK_ACTION_MOVE);
1303
1304         g_signal_connect (priv->notebook,
1305                           "drag-data-received",
1306                           G_CALLBACK (chat_window_drag_data_received),
1307                           window);
1308
1309         chat_windows = g_list_prepend (chat_windows, window);
1310
1311         /* Set up private details */
1312         priv->chats = NULL;
1313         priv->chats_new_msg = NULL;
1314         priv->chats_composing = NULL;
1315         priv->current_chat = NULL;
1316 }
1317
1318 EmpathyChatWindow *
1319 empathy_chat_window_new (void)
1320 {
1321         return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1322 }
1323
1324 /* Returns the window to open a new tab in if there is only one window
1325  * visble, otherwise, returns NULL indicating that a new window should
1326  * be added.
1327  */
1328 EmpathyChatWindow *
1329 empathy_chat_window_get_default (void)
1330 {
1331         GList    *l;
1332         gboolean  separate_windows = TRUE;
1333
1334         empathy_conf_get_bool (empathy_conf_get (),
1335                               EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1336                               &separate_windows);
1337
1338         if (separate_windows) {
1339                 /* Always create a new window */
1340                 return NULL;
1341         }
1342
1343         for (l = chat_windows; l; l = l->next) {
1344                 EmpathyChatWindow *chat_window;
1345                 GtkWidget         *dialog;
1346
1347                 chat_window = l->data;
1348
1349                 dialog = empathy_chat_window_get_dialog (chat_window);
1350                 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1351                         /* Found a visible window on this desktop */
1352                         return chat_window;
1353                 }
1354         }
1355
1356         return NULL;
1357 }
1358
1359 GtkWidget *
1360 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1361 {
1362         EmpathyChatWindowPriv *priv;
1363
1364         g_return_val_if_fail (window != NULL, NULL);
1365
1366         priv = GET_PRIV (window);
1367
1368         return priv->dialog;
1369 }
1370
1371 void
1372 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1373                               EmpathyChat       *chat)
1374 {
1375         EmpathyChatWindowPriv *priv;
1376         GtkWidget             *label;
1377         GtkWidget             *child;
1378         gint                   x, y, w, h;
1379
1380         g_return_if_fail (window != NULL);
1381         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1382
1383         priv = GET_PRIV (window);
1384
1385         /* Reference the chat object */
1386         g_object_ref (chat);
1387
1388         /* If this window has just been created, position it */
1389         if (priv->chats == NULL) {
1390                 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1391                 
1392                 if (x >= 0 && y >= 0) {
1393                         /* Let the window manager position it if we don't have
1394                          * good x, y coordinates.
1395                          */
1396                         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1397                 }
1398                 
1399                 if (w > 0 && h > 0) {
1400                         /* Use the defaults from the glade file if we don't have
1401                          * good w, h geometry.
1402                          */
1403                         gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1404                 }
1405         }
1406
1407         child = GTK_WIDGET (chat);
1408         label = chat_window_create_label (window, chat); 
1409         gtk_widget_show (child);
1410
1411         g_signal_connect (chat, "notify::name",
1412                           G_CALLBACK (chat_window_chat_notify_cb),
1413                           NULL);
1414         g_signal_connect (chat, "notify::subject",
1415                           G_CALLBACK (chat_window_chat_notify_cb),
1416                           NULL);
1417         g_signal_connect (chat, "notify::remote-contact",
1418                           G_CALLBACK (chat_window_chat_notify_cb),
1419                           NULL);
1420         chat_window_chat_notify_cb (chat);
1421
1422         gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), child, label);
1423         gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1424         gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1425         gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1426                                             TRUE, TRUE, GTK_PACK_START); 
1427
1428         DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1429 }
1430
1431 void
1432 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1433                                  EmpathyChat       *chat)
1434 {
1435         EmpathyChatWindowPriv *priv;
1436         gint                   position;
1437         EmpathyContact        *remote_contact;
1438
1439         g_return_if_fail (window != NULL);
1440         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1441
1442         priv = GET_PRIV (window);
1443
1444         g_signal_handlers_disconnect_by_func (chat,
1445                                               chat_window_chat_notify_cb,
1446                                               NULL);
1447         remote_contact = g_object_get_data (G_OBJECT (chat),
1448                                             "chat-window-remote-contact");
1449         if (remote_contact) {
1450                 g_signal_handlers_disconnect_by_func (remote_contact,
1451                                                       chat_window_update_chat_tab,
1452                                                       chat);
1453         }
1454
1455         position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1456                                           GTK_WIDGET (chat));
1457         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1458
1459         DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1460
1461         g_object_unref (chat);
1462 }
1463
1464 void
1465 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1466                                EmpathyChatWindow *new_window,
1467                                EmpathyChat       *chat)
1468 {
1469         GtkWidget *widget;
1470
1471         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1472         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1473         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1474
1475         widget = GTK_WIDGET (chat);
1476
1477         DEBUG ("Chat moving with widget:%p (%d references)", widget,
1478                 G_OBJECT (widget)->ref_count);
1479
1480         /* We reference here to make sure we don't loose the widget
1481          * and the EmpathyChat object during the move.
1482          */
1483         g_object_ref (chat);
1484         g_object_ref (widget);
1485
1486         empathy_chat_window_remove_chat (old_window, chat);
1487         empathy_chat_window_add_chat (new_window, chat);
1488
1489         g_object_unref (widget);
1490         g_object_unref (chat);
1491 }
1492
1493 void
1494 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1495                                     EmpathyChat       *chat)
1496 {
1497         EmpathyChatWindowPriv *priv;
1498         gint                  page_num;
1499
1500         g_return_if_fail (window != NULL);
1501         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1502
1503         priv = GET_PRIV (window);
1504
1505         page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1506                                           GTK_WIDGET (chat));
1507         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1508                                        page_num);
1509 }
1510
1511 gboolean
1512 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1513 {
1514         EmpathyChatWindowPriv *priv;
1515         gboolean              has_focus;
1516
1517         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1518
1519         priv = GET_PRIV (window);
1520
1521         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1522
1523         return has_focus;
1524 }
1525
1526 EmpathyChat *
1527 empathy_chat_window_find_chat (McAccount   *account,
1528                                const gchar *id)
1529 {
1530         GList *l;
1531
1532         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1533         g_return_val_if_fail (!G_STR_EMPTY (id), NULL);
1534
1535         for (l = chat_windows; l; l = l->next) {
1536                 EmpathyChatWindowPriv *priv;
1537                 EmpathyChatWindow     *window;
1538                 GList                *ll;
1539
1540                 window = l->data;
1541                 priv = GET_PRIV (window);
1542
1543                 for (ll = priv->chats; ll; ll = ll->next) {
1544                         EmpathyChat *chat;
1545
1546                         chat = ll->data;
1547
1548                         if (empathy_account_equal (account, empathy_chat_get_account (chat)) &&
1549                             !tp_strdiff (id, empathy_chat_get_id (chat))) {
1550                                 return chat;
1551                         }
1552                 }
1553         }
1554
1555         return NULL;
1556 }
1557
1558 void
1559 empathy_chat_window_present_chat (EmpathyChat *chat)
1560 {
1561         EmpathyChatWindow     *window;
1562         EmpathyChatWindowPriv *priv;
1563
1564         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1565
1566         window = chat_window_find_chat (chat);
1567
1568         /* If the chat has no window, create one */
1569         if (window == NULL) {
1570                 window = empathy_chat_window_get_default ();
1571                 if (!window) {
1572                         window = empathy_chat_window_new ();
1573                 }
1574
1575                 empathy_chat_window_add_chat (window, chat);
1576         }
1577
1578         priv = GET_PRIV (window);
1579         empathy_chat_window_switch_to_chat (window, chat);
1580         empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1581
1582         gtk_widget_grab_focus (chat->input_text_view); 
1583 }
1584
1585 #if 0
1586 static gboolean
1587 chat_window_should_play_sound (EmpathyChatWindow *window)
1588 {
1589         EmpathyChatWindowPriv *priv = GET_PRIV (window);
1590         gboolean               has_focus = FALSE;
1591
1592         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1593
1594         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1595
1596         return !has_focus;
1597 }
1598 #endif