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