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