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