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