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