]> git.0d.be Git - empathy.git/blob - src/empathy-chat-window.c
Port EmpathyChatWindow to new 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 <glib/gi18n.h>
35 #include <libnotify/notification.h>
36
37 #include <telepathy-glib/util.h>
38 #include <libmissioncontrol/mission-control.h>
39
40 #include <libempathy/empathy-contact-factory.h>
41 #include <libempathy/empathy-contact.h>
42 #include <libempathy/empathy-message.h>
43 #include <libempathy/empathy-dispatcher.h>
44 #include <libempathy/empathy-chatroom-manager.h>
45 #include <libempathy/empathy-utils.h>
46
47 #include <libempathy-gtk/empathy-images.h>
48 #include <libempathy-gtk/empathy-conf.h>
49 #include <libempathy-gtk/empathy-contact-dialogs.h>
50 #include <libempathy-gtk/empathy-log-window.h>
51 #include <libempathy-gtk/empathy-geometry.h>
52 #include <libempathy-gtk/empathy-smiley-manager.h>
53 #include <libempathy-gtk/empathy-ui-utils.h>
54
55 #include "empathy-chat-window.h"
56 #include "empathy-about-dialog.h"
57 #include "empathy-misc.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 typedef struct {
839         EmpathyChatWindow *window;
840         EmpathyChat *chat;
841 } NotificationData;
842
843 static void
844 chat_window_notification_closed_cb (NotifyNotification *notify,
845                                     NotificationData *cb_data)
846 {
847         EmpathyNotificationClosedReason reason = 0;
848         EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
849
850 #ifdef notify_notification_get_closed_reason
851         reason = notify_notification_get_closed_reason (notify);
852 #endif
853         if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
854                 empathy_chat_window_present_chat (cb_data->chat);
855         }
856
857         g_object_unref (notify);
858         priv->notification = NULL;
859         g_object_unref (cb_data->chat);
860         g_slice_free (NotificationData, cb_data);
861 }
862
863 static void
864 chat_window_show_or_update_notification (EmpathyChatWindow *window,
865                                          EmpathyMessage *message,
866                                          EmpathyChat *chat)
867 {
868         EmpathyContact *sender;
869         char *header, *escaped;
870         const char *body;
871         GdkPixbuf *pixbuf;
872         NotificationData *cb_data;
873         EmpathyChatWindowPriv *priv = GET_PRIV (window);
874         gboolean res;
875
876         if (!empathy_notification_is_enabled ()) {
877                 return;
878         } else {
879                 empathy_conf_get_bool (empathy_conf_get (),
880                                        EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
881                 if (!res) {
882                         return;
883                 }
884         }
885
886         cb_data = g_slice_new0 (NotificationData);
887         cb_data->chat = g_object_ref (chat);
888         cb_data->window = window;
889
890         sender = empathy_message_get_sender (message);
891         header = g_strdup_printf (_("New message from %s"),
892                                   empathy_contact_get_name (sender));
893         body = empathy_message_get_body (message);
894         escaped = g_markup_escape_text (body, -1);
895
896         pixbuf = empathy_misc_get_pixbuf_for_notification (sender, EMPATHY_IMAGE_NEW_MESSAGE);
897
898         if (priv->notification != NULL) {
899                 notify_notification_update (priv->notification,
900                                             header, escaped, NULL);
901                 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
902         } else {
903                 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
904                 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
905                 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
906
907                 g_signal_connect (priv->notification, "closed",
908                                   G_CALLBACK (chat_window_notification_closed_cb), cb_data);
909         }
910
911         notify_notification_show (priv->notification, NULL);
912
913         g_object_unref (pixbuf);
914         g_free (header);
915         g_free (escaped);
916 }
917
918 static void
919 chat_window_new_message_cb (EmpathyChat       *chat,
920                             EmpathyMessage    *message,
921                             EmpathyChatWindow *window)
922 {
923         EmpathyChatWindowPriv *priv;
924         gboolean              has_focus;
925         gboolean              needs_urgency;
926         EmpathyContact        *sender;
927
928         priv = GET_PRIV (window);
929
930         has_focus = empathy_chat_window_has_focus (window);
931
932         /* - if we're the sender, we play the sound if it's specified in the
933          *   preferences and we're not away.
934          * - if we receive a message, we play the sound if it's specified in the
935          *   prefereces and the window does not have focus on the chat receiving
936          *   the message.
937          */
938
939         sender = empathy_message_get_sender (message);
940
941         if (empathy_contact_is_user (sender) != FALSE) {
942                 empathy_sound_play (GTK_WIDGET (priv->dialog),
943                                     EMPATHY_SOUND_MESSAGE_OUTGOING);
944         } else {
945                 if ((!has_focus || priv->current_chat != chat)) {
946                         empathy_sound_play (GTK_WIDGET (priv->dialog),
947                                             EMPATHY_SOUND_MESSAGE_INCOMING);
948                 }
949         }
950
951         if (!has_focus) {
952                 chat_window_show_or_update_notification (window, message, chat);
953         }
954
955         if (has_focus && priv->current_chat == chat) {
956                 return;
957         }
958         
959         if (empathy_chat_get_members_count (chat) > 2) {
960                 needs_urgency = empathy_message_should_highlight (message);
961         } else {
962                 needs_urgency = TRUE;
963         }
964
965         if (needs_urgency && !has_focus) {
966                 chat_window_set_urgency_hint (window, TRUE);
967         }
968
969         if (!g_list_find (priv->chats_new_msg, chat)) {
970                 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
971                 chat_window_update_chat_tab (chat);
972         }
973 }
974
975 static GtkNotebook *
976 chat_window_detach_hook (GtkNotebook *source,
977                          GtkWidget   *page,
978                          gint         x,
979                          gint         y,
980                          gpointer     user_data)
981 {
982         EmpathyChatWindowPriv *priv;
983         EmpathyChatWindow     *window, *new_window;
984         EmpathyChat           *chat;
985
986         chat = EMPATHY_CHAT (page);
987         window = chat_window_find_chat (chat);
988
989         new_window = empathy_chat_window_new ();
990         priv = GET_PRIV (new_window);
991
992         DEBUG ("Detach hook called");
993
994         empathy_chat_window_move_chat (window, new_window, chat);
995
996         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
997         gtk_widget_show (priv->dialog);
998
999         return NULL;
1000 }
1001
1002 static void
1003 chat_window_page_switched_cb (GtkNotebook      *notebook,
1004                               GtkNotebookPage  *page,
1005                               gint              page_num,
1006                               EmpathyChatWindow *window)
1007 {
1008         EmpathyChatWindowPriv *priv;
1009         EmpathyChat           *chat;
1010         GtkWidget            *child;
1011
1012         DEBUG ("Page switched");
1013
1014         priv = GET_PRIV (window);
1015
1016         child = gtk_notebook_get_nth_page (notebook, page_num);
1017         chat = EMPATHY_CHAT (child);
1018
1019         if (priv->page_added) {
1020                 priv->page_added = FALSE;
1021                 empathy_chat_scroll_down (chat);
1022         }
1023         else if (priv->current_chat == chat) {
1024                 return;
1025         }
1026
1027         priv->current_chat = chat;
1028         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1029
1030         chat_window_update_chat_tab (chat);
1031 }
1032
1033 static void
1034 chat_window_page_added_cb (GtkNotebook      *notebook,
1035                            GtkWidget        *child,
1036                            guint             page_num,
1037                            EmpathyChatWindow *window)
1038 {
1039         EmpathyChatWindowPriv *priv;
1040         EmpathyChat           *chat;
1041
1042         priv = GET_PRIV (window);
1043
1044         /* If we just received DND to the same window, we don't want
1045          * to do anything here like removing the tab and then readding
1046          * it, so we return here and in "page-added".
1047          */
1048         if (priv->dnd_same_window) {
1049                 DEBUG ("Page added (back to the same window)");
1050                 priv->dnd_same_window = FALSE;
1051                 return;
1052         }
1053
1054         DEBUG ("Page added");
1055
1056         /* Get chat object */
1057         chat = EMPATHY_CHAT (child);
1058
1059         /* Connect chat signals for this window */
1060         g_signal_connect (chat, "composing",
1061                           G_CALLBACK (chat_window_composing_cb),
1062                           window);
1063         g_signal_connect (chat, "new-message",
1064                           G_CALLBACK (chat_window_new_message_cb),
1065                           window);
1066
1067         /* Set flag so we know to perform some special operations on
1068          * switch page due to the new page being added.
1069          */
1070         priv->page_added = TRUE;
1071
1072         /* Get list of chats up to date */
1073         priv->chats = g_list_append (priv->chats, chat);
1074
1075         chat_window_update_chat_tab (chat);
1076 }
1077
1078 static void
1079 chat_window_page_removed_cb (GtkNotebook      *notebook,
1080                              GtkWidget        *child,
1081                              guint             page_num,
1082                              EmpathyChatWindow *window)
1083 {
1084         EmpathyChatWindowPriv *priv;
1085         EmpathyChat           *chat;
1086
1087         priv = GET_PRIV (window);
1088
1089         /* If we just received DND to the same window, we don't want
1090          * to do anything here like removing the tab and then readding
1091          * it, so we return here and in "page-added".
1092          */
1093         if (priv->dnd_same_window) {
1094                 DEBUG ("Page removed (and will be readded to same window)");
1095                 return;
1096         }
1097
1098         DEBUG ("Page removed");
1099
1100         /* Get chat object */
1101         chat = EMPATHY_CHAT (child);
1102
1103         /* Disconnect all signal handlers for this chat and this window */
1104         g_signal_handlers_disconnect_by_func (chat,
1105                                               G_CALLBACK (chat_window_composing_cb),
1106                                               window);
1107         g_signal_handlers_disconnect_by_func (chat,
1108                                               G_CALLBACK (chat_window_new_message_cb),
1109                                               window);
1110
1111         /* Keep list of chats up to date */
1112         priv->chats = g_list_remove (priv->chats, chat);
1113         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1114         priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1115
1116         if (priv->chats == NULL) {
1117                 g_object_unref (window);
1118         } else {
1119                 chat_window_update (window);
1120         }
1121 }
1122
1123 static gboolean
1124 chat_window_focus_in_event_cb (GtkWidget        *widget,
1125                                GdkEvent         *event,
1126                                EmpathyChatWindow *window)
1127 {
1128         EmpathyChatWindowPriv *priv;
1129
1130         DEBUG ("Focus in event, updating title");
1131
1132         priv = GET_PRIV (window);
1133
1134         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1135
1136         chat_window_set_urgency_hint (window, FALSE);
1137         
1138         /* Update the title, since we now mark all unread messages as read. */
1139         chat_window_update_chat_tab (priv->current_chat);
1140
1141         return FALSE;
1142 }
1143
1144 static void
1145 chat_window_drag_data_received (GtkWidget        *widget,
1146                                 GdkDragContext   *context,
1147                                 int               x,
1148                                 int               y,
1149                                 GtkSelectionData *selection,
1150                                 guint             info,
1151                                 guint             time,
1152                                 EmpathyChatWindow *window)
1153 {
1154         if (info == DND_DRAG_TYPE_CONTACT_ID) {
1155                 EmpathyChat           *chat;
1156                 EmpathyChatWindow     *old_window;
1157                 McAccount             *account;
1158                 const gchar           *id;
1159                 gchar                **strv;
1160
1161                 id = (const gchar*) selection->data;
1162
1163                 DEBUG ("DND contact from roster with id:'%s'", id);
1164                 
1165                 strv = g_strsplit (id, "/", 2);
1166                 account = mc_account_lookup (strv[0]);
1167                 chat = empathy_chat_window_find_chat (account, strv[1]);
1168
1169                 if (!chat) {
1170                         empathy_dispatcher_chat_with_contact_id (account, strv[2], NULL, NULL);
1171                         g_object_unref (account);
1172                         g_strfreev (strv);
1173                         return;
1174                 }
1175                 g_object_unref (account);
1176                 g_strfreev (strv);
1177
1178                 old_window = chat_window_find_chat (chat);              
1179                 if (old_window) {
1180                         if (old_window == window) {
1181                                 gtk_drag_finish (context, TRUE, FALSE, time);
1182                                 return;
1183                         }
1184                         
1185                         empathy_chat_window_move_chat (old_window, window, chat);
1186                 } else {
1187                         empathy_chat_window_add_chat (window, chat);
1188                 }
1189                 
1190                 /* Added to take care of any outstanding chat events */
1191                 empathy_chat_window_present_chat (chat);
1192
1193                 /* We should return TRUE to remove the data when doing
1194                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1195                  * weird consequences, and we handle that internally
1196                  * anyway with add_chat() and remove_chat().
1197                  */
1198                 gtk_drag_finish (context, TRUE, FALSE, time);
1199         }
1200         else if (info == DND_DRAG_TYPE_TAB) {
1201                 EmpathyChat        **chat;
1202                 EmpathyChatWindow   *old_window = NULL;
1203
1204                 DEBUG ("DND tab");
1205
1206                 chat = (void*) selection->data;
1207                 old_window = chat_window_find_chat (*chat);
1208
1209                 if (old_window) {
1210                         EmpathyChatWindowPriv *priv;
1211
1212                         priv = GET_PRIV (window);
1213
1214                         if (old_window == window) {
1215                                 DEBUG ("DND tab (within same window)");
1216                                 priv->dnd_same_window = TRUE;
1217                                 gtk_drag_finish (context, TRUE, FALSE, time);
1218                                 return;
1219                         }
1220                         
1221                         priv->dnd_same_window = FALSE;
1222                 }
1223
1224                 /* We should return TRUE to remove the data when doing
1225                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1226                  * weird consequences, and we handle that internally
1227                  * anyway with add_chat() and remove_chat().
1228                  */
1229                 gtk_drag_finish (context, TRUE, FALSE, time);
1230         } else {
1231                 DEBUG ("DND from unknown source");
1232                 gtk_drag_finish (context, FALSE, FALSE, time);
1233         }
1234 }
1235
1236 static void
1237 chat_window_finalize (GObject *object)
1238 {
1239         EmpathyChatWindow     *window;
1240         EmpathyChatWindowPriv *priv;
1241
1242         window = EMPATHY_CHAT_WINDOW (object);
1243         priv = GET_PRIV (window);
1244
1245         DEBUG ("Finalized: %p", object);
1246
1247         g_object_unref (priv->chatroom_manager);
1248         if (priv->save_geometry_id != 0) {
1249                 g_source_remove (priv->save_geometry_id);
1250         }
1251
1252         if (priv->notification != NULL) {
1253                 notify_notification_close (priv->notification, NULL);
1254                 g_object_unref (priv->notification);
1255                 priv->notification = NULL;
1256         }
1257
1258         chat_windows = g_list_remove (chat_windows, window);
1259         gtk_widget_destroy (priv->dialog);
1260
1261         G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1262 }
1263
1264 static void
1265 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1266 {
1267         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1268
1269         object_class->finalize = chat_window_finalize;
1270
1271         g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1272
1273         /* Set up a style for the close button with no focus padding. */
1274         gtk_rc_parse_string (
1275                 "style \"empathy-close-button-style\"\n"
1276                 "{\n"
1277                 "  GtkWidget::focus-padding = 0\n"
1278                 "  xthickness = 0\n"
1279                 "  ythickness = 0\n"
1280                 "}\n"
1281                 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1282
1283         gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1284 }
1285
1286 static void
1287 empathy_chat_window_init (EmpathyChatWindow *window)
1288 {
1289         GtkBuilder            *gui;
1290         GtkAccelGroup         *accel_group;
1291         GClosure              *closure;
1292         GtkWidget             *menu_conv;
1293         GtkWidget             *menu;
1294         gint                   i;
1295         GtkWidget             *chat_vbox;
1296         gchar                 *filename;
1297         EmpathySmileyManager  *smiley_manager;
1298         EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1299                 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1300
1301         window->priv = priv;
1302         filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1303         gui = empathy_builder_get_file (filename,
1304                                        "chat_window", &priv->dialog,
1305                                        "chat_vbox", &chat_vbox,
1306                                        "menu_conv", &menu_conv,
1307                                        "menu_conv_clear", &priv->menu_conv_clear,
1308                                        "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1309                                        "menu_conv_contact", &priv->menu_conv_contact,
1310                                        "menu_conv_favorite", &priv->menu_conv_favorite,
1311                                        "menu_conv_close", &priv->menu_conv_close,
1312                                        "menu_edit_cut", &priv->menu_edit_cut,
1313                                        "menu_edit_copy", &priv->menu_edit_copy,
1314                                        "menu_edit_paste", &priv->menu_edit_paste,
1315                                        "menu_tabs_next", &priv->menu_tabs_next,
1316                                        "menu_tabs_prev", &priv->menu_tabs_prev,
1317                                        "menu_tabs_left", &priv->menu_tabs_left,
1318                                        "menu_tabs_right", &priv->menu_tabs_right,
1319                                        "menu_tabs_detach", &priv->menu_tabs_detach,
1320                                        "menu_help_contents", &priv->menu_help_contents,
1321                                        "menu_help_about", &priv->menu_help_about,
1322                                        NULL);
1323         g_free (filename);
1324
1325         empathy_builder_connect (gui, window,
1326                               "chat_window", "configure-event", chat_window_configure_event_cb,
1327                               "menu_conv", "activate", chat_window_conv_activate_cb,
1328                               "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1329                               "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1330                               "menu_conv_close", "activate", chat_window_close_activate_cb,
1331                               "menu_edit", "activate", chat_window_edit_activate_cb,
1332                               "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1333                               "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1334                               "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1335                               "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1336                               "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1337                               "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1338                               "menu_help_contents", "activate", chat_window_help_contents_cb,
1339                               "menu_help_about", "activate", chat_window_help_about_cb,
1340                               NULL);
1341
1342         g_object_unref (gui);
1343
1344         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1345
1346         priv->notebook = gtk_notebook_new ();
1347         gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow"); 
1348         gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1349         gtk_widget_show (priv->notebook);
1350
1351         /* Set up accels */
1352         accel_group = gtk_accel_group_new ();
1353         gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1354
1355         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1356                 closure =  g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1357                                            window,
1358                                            NULL);
1359                 gtk_accel_group_connect (accel_group,
1360                                          tab_accel_keys[i],
1361                                          GDK_MOD1_MASK,
1362                                          0,
1363                                          closure);
1364         }
1365
1366         g_object_unref (accel_group);
1367
1368         /* Set up smiley menu */
1369         smiley_manager = empathy_smiley_manager_dup_singleton ();
1370         menu = empathy_smiley_menu_new (smiley_manager,
1371                                         chat_window_insert_smiley_activate_cb,
1372                                         window);
1373         gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->menu_conv_insert_smiley),
1374                                    menu);
1375         g_object_unref (smiley_manager);
1376
1377         /* Set up signals we can't do with ui file since we may need to
1378          * block/unblock them at some later stage.
1379          */
1380
1381         g_signal_connect (priv->dialog,
1382                           "delete_event",
1383                           G_CALLBACK (chat_window_delete_event_cb),
1384                           window);
1385
1386         g_signal_connect_swapped (priv->menu_tabs_prev,
1387                                   "activate",
1388                                   G_CALLBACK (gtk_notebook_prev_page),
1389                                   priv->notebook);
1390         g_signal_connect_swapped (priv->menu_tabs_next,
1391                                   "activate",
1392                                   G_CALLBACK (gtk_notebook_next_page),
1393                                   priv->notebook);
1394
1395         g_signal_connect (priv->dialog,
1396                           "focus_in_event",
1397                           G_CALLBACK (chat_window_focus_in_event_cb),
1398                           window);
1399         g_signal_connect_after (priv->notebook,
1400                                 "switch_page",
1401                                 G_CALLBACK (chat_window_page_switched_cb),
1402                                 window);
1403         g_signal_connect (priv->notebook,
1404                           "page_added",
1405                           G_CALLBACK (chat_window_page_added_cb),
1406                           window);
1407         g_signal_connect (priv->notebook,
1408                           "page_removed",
1409                           G_CALLBACK (chat_window_page_removed_cb),
1410                           window);
1411
1412         /* Set up drag and drop */
1413         gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1414                            GTK_DEST_DEFAULT_ALL,
1415                            drag_types_dest,
1416                            G_N_ELEMENTS (drag_types_dest),
1417                            GDK_ACTION_MOVE);
1418
1419         g_signal_connect (priv->notebook,
1420                           "drag-data-received",
1421                           G_CALLBACK (chat_window_drag_data_received),
1422                           window);
1423
1424         chat_windows = g_list_prepend (chat_windows, window);
1425
1426         /* Set up private details */
1427         priv->chats = NULL;
1428         priv->chats_new_msg = NULL;
1429         priv->chats_composing = NULL;
1430         priv->current_chat = NULL;
1431 }
1432
1433 EmpathyChatWindow *
1434 empathy_chat_window_new (void)
1435 {
1436         return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1437 }
1438
1439 /* Returns the window to open a new tab in if there is only one window
1440  * visble, otherwise, returns NULL indicating that a new window should
1441  * be added.
1442  */
1443 EmpathyChatWindow *
1444 empathy_chat_window_get_default (void)
1445 {
1446         GList    *l;
1447         gboolean  separate_windows = TRUE;
1448
1449         empathy_conf_get_bool (empathy_conf_get (),
1450                               EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1451                               &separate_windows);
1452
1453         if (separate_windows) {
1454                 /* Always create a new window */
1455                 return NULL;
1456         }
1457
1458         for (l = chat_windows; l; l = l->next) {
1459                 EmpathyChatWindow *chat_window;
1460                 GtkWidget         *dialog;
1461
1462                 chat_window = l->data;
1463
1464                 dialog = empathy_chat_window_get_dialog (chat_window);
1465                 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1466                         /* Found a visible window on this desktop */
1467                         return chat_window;
1468                 }
1469         }
1470
1471         return NULL;
1472 }
1473
1474 GtkWidget *
1475 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1476 {
1477         EmpathyChatWindowPriv *priv;
1478
1479         g_return_val_if_fail (window != NULL, NULL);
1480
1481         priv = GET_PRIV (window);
1482
1483         return priv->dialog;
1484 }
1485
1486 void
1487 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1488                               EmpathyChat       *chat)
1489 {
1490         EmpathyChatWindowPriv *priv;
1491         GtkWidget             *label;
1492         GtkWidget             *child;
1493         gint                   x, y, w, h;
1494
1495         g_return_if_fail (window != NULL);
1496         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1497
1498         priv = GET_PRIV (window);
1499
1500         /* Reference the chat object */
1501         g_object_ref (chat);
1502
1503         /* If this window has just been created, position it */
1504         if (priv->chats == NULL) {
1505                 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1506                 
1507                 if (x >= 0 && y >= 0) {
1508                         /* Let the window manager position it if we don't have
1509                          * good x, y coordinates.
1510                          */
1511                         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1512                 }
1513                 
1514                 if (w > 0 && h > 0) {
1515                         /* Use the defaults from the ui file if we don't have
1516                          * good w, h geometry.
1517                          */
1518                         gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1519                 }
1520         }
1521
1522         child = GTK_WIDGET (chat);
1523         label = chat_window_create_label (window, chat); 
1524         gtk_widget_show (child);
1525
1526         g_signal_connect (chat, "notify::name",
1527                           G_CALLBACK (chat_window_chat_notify_cb),
1528                           NULL);
1529         g_signal_connect (chat, "notify::subject",
1530                           G_CALLBACK (chat_window_chat_notify_cb),
1531                           NULL);
1532         g_signal_connect (chat, "notify::remote-contact",
1533                           G_CALLBACK (chat_window_chat_notify_cb),
1534                           NULL);
1535         chat_window_chat_notify_cb (chat);
1536
1537         gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), child, label);
1538         gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1539         gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1540         gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1541                                             TRUE, TRUE, GTK_PACK_START); 
1542
1543         DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1544 }
1545
1546 void
1547 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1548                                  EmpathyChat       *chat)
1549 {
1550         EmpathyChatWindowPriv *priv;
1551         gint                   position;
1552         EmpathyContact        *remote_contact;
1553
1554         g_return_if_fail (window != NULL);
1555         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1556
1557         priv = GET_PRIV (window);
1558
1559         g_signal_handlers_disconnect_by_func (chat,
1560                                               chat_window_chat_notify_cb,
1561                                               NULL);
1562         remote_contact = g_object_get_data (G_OBJECT (chat),
1563                                             "chat-window-remote-contact");
1564         if (remote_contact) {
1565                 g_signal_handlers_disconnect_by_func (remote_contact,
1566                                                       chat_window_update_chat_tab,
1567                                                       chat);
1568         }
1569
1570         position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1571                                           GTK_WIDGET (chat));
1572         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1573
1574         DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1575
1576         g_object_unref (chat);
1577 }
1578
1579 void
1580 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1581                                EmpathyChatWindow *new_window,
1582                                EmpathyChat       *chat)
1583 {
1584         GtkWidget *widget;
1585
1586         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1587         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1588         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1589
1590         widget = GTK_WIDGET (chat);
1591
1592         DEBUG ("Chat moving with widget:%p (%d references)", widget,
1593                 G_OBJECT (widget)->ref_count);
1594
1595         /* We reference here to make sure we don't loose the widget
1596          * and the EmpathyChat object during the move.
1597          */
1598         g_object_ref (chat);
1599         g_object_ref (widget);
1600
1601         empathy_chat_window_remove_chat (old_window, chat);
1602         empathy_chat_window_add_chat (new_window, chat);
1603
1604         g_object_unref (widget);
1605         g_object_unref (chat);
1606 }
1607
1608 void
1609 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1610                                     EmpathyChat       *chat)
1611 {
1612         EmpathyChatWindowPriv *priv;
1613         gint                  page_num;
1614
1615         g_return_if_fail (window != NULL);
1616         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1617
1618         priv = GET_PRIV (window);
1619
1620         page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1621                                           GTK_WIDGET (chat));
1622         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1623                                        page_num);
1624 }
1625
1626 gboolean
1627 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1628 {
1629         EmpathyChatWindowPriv *priv;
1630         gboolean              has_focus;
1631
1632         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1633
1634         priv = GET_PRIV (window);
1635
1636         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1637
1638         return has_focus;
1639 }
1640
1641 EmpathyChat *
1642 empathy_chat_window_find_chat (McAccount   *account,
1643                                const gchar *id)
1644 {
1645         GList *l;
1646
1647         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1648         g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1649
1650         for (l = chat_windows; l; l = l->next) {
1651                 EmpathyChatWindowPriv *priv;
1652                 EmpathyChatWindow     *window;
1653                 GList                *ll;
1654
1655                 window = l->data;
1656                 priv = GET_PRIV (window);
1657
1658                 for (ll = priv->chats; ll; ll = ll->next) {
1659                         EmpathyChat *chat;
1660
1661                         chat = ll->data;
1662
1663                         if (empathy_account_equal (account, empathy_chat_get_account (chat)) &&
1664                             !tp_strdiff (id, empathy_chat_get_id (chat))) {
1665                                 return chat;
1666                         }
1667                 }
1668         }
1669
1670         return NULL;
1671 }
1672
1673 void
1674 empathy_chat_window_present_chat (EmpathyChat *chat)
1675 {
1676         EmpathyChatWindow     *window;
1677         EmpathyChatWindowPriv *priv;
1678
1679         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1680
1681         window = chat_window_find_chat (chat);
1682
1683         /* If the chat has no window, create one */
1684         if (window == NULL) {
1685                 window = empathy_chat_window_get_default ();
1686                 if (!window) {
1687                         window = empathy_chat_window_new ();
1688                 }
1689
1690                 empathy_chat_window_add_chat (window, chat);
1691         }
1692
1693         priv = GET_PRIV (window);
1694         empathy_chat_window_switch_to_chat (window, chat);
1695         empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1696
1697         gtk_widget_grab_focus (chat->input_text_view); 
1698 }
1699
1700 #if 0
1701 static gboolean
1702 chat_window_should_play_sound (EmpathyChatWindow *window)
1703 {
1704         EmpathyChatWindowPriv *priv = GET_PRIV (window);
1705         gboolean               has_focus = FALSE;
1706
1707         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1708
1709         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1710
1711         return !has_focus;
1712 }
1713 #endif