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