]> git.0d.be Git - empathy.git/blob - src/empathy-chat-window.c
display the number of unread message in the window title (#548701)
[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 others tab */
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 everyone)",
433                                 "%s (%d unread from everyone)",
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 than the message is
1199                  * read */
1200                 empathy_chat_messages_read (chat);
1201                 return;
1202         }
1203
1204         if (!g_list_find (priv->chats_new_msg, chat)) {
1205                 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1206                 chat_window_update_chat_tab (chat);
1207         }
1208
1209         /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1210          * If empathy_chat_get_remote_contact () returns NULL, that means it's
1211          * an unamed MUC (msn-like).
1212          * In case of a MUC, we set urgency only if the message contains our
1213          * alias. */
1214         if (empathy_chat_is_room (chat) ||
1215             empathy_chat_get_remote_contact (chat) == NULL) {
1216                 needs_urgency = empathy_message_should_highlight (message);
1217         } else {
1218                 needs_urgency = TRUE;
1219         }
1220
1221         if (needs_urgency) {
1222                 if (!has_focus) {
1223                         chat_window_set_urgency_hint (window, TRUE);
1224                         chat_window_set_highlight_room_tab_label (chat);
1225                 }
1226
1227                 empathy_sound_play (GTK_WIDGET (priv->dialog),
1228                     EMPATHY_SOUND_MESSAGE_INCOMING);
1229                 chat_window_show_or_update_notification (window, message, chat);
1230         }
1231 }
1232
1233 static GtkNotebook *
1234 chat_window_detach_hook (GtkNotebook *source,
1235                          GtkWidget   *page,
1236                          gint         x,
1237                          gint         y,
1238                          gpointer     user_data)
1239 {
1240         EmpathyChatWindowPriv *priv;
1241         EmpathyChatWindow     *window, *new_window;
1242         EmpathyChat           *chat;
1243
1244         chat = EMPATHY_CHAT (page);
1245         window = chat_window_find_chat (chat);
1246
1247         new_window = empathy_chat_window_new ();
1248         priv = GET_PRIV (new_window);
1249
1250         DEBUG ("Detach hook called");
1251
1252         empathy_chat_window_move_chat (window, new_window, chat);
1253
1254         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1255         gtk_widget_show (priv->dialog);
1256
1257         return NULL;
1258 }
1259
1260 static void
1261 chat_window_page_switched_cb (GtkNotebook      *notebook,
1262                               GtkNotebookPage  *page,
1263                               gint              page_num,
1264                               EmpathyChatWindow *window)
1265 {
1266         EmpathyChatWindowPriv *priv;
1267         EmpathyChat           *chat;
1268         GtkWidget            *child;
1269
1270         DEBUG ("Page switched");
1271
1272         priv = GET_PRIV (window);
1273
1274         child = gtk_notebook_get_nth_page (notebook, page_num);
1275         chat = EMPATHY_CHAT (child);
1276
1277         if (priv->page_added) {
1278                 priv->page_added = FALSE;
1279                 empathy_chat_scroll_down (chat);
1280         }
1281         else if (priv->current_chat == chat) {
1282                 return;
1283         }
1284
1285         priv->current_chat = chat;
1286         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1287         empathy_chat_messages_read (chat);
1288
1289         chat_window_update_chat_tab (chat);
1290 }
1291
1292 static void
1293 chat_window_page_added_cb (GtkNotebook      *notebook,
1294                            GtkWidget        *child,
1295                            guint             page_num,
1296                            EmpathyChatWindow *window)
1297 {
1298         EmpathyChatWindowPriv *priv;
1299         EmpathyChat           *chat;
1300
1301         priv = GET_PRIV (window);
1302
1303         /* If we just received DND to the same window, we don't want
1304          * to do anything here like removing the tab and then readding
1305          * it, so we return here and in "page-added".
1306          */
1307         if (priv->dnd_same_window) {
1308                 DEBUG ("Page added (back to the same window)");
1309                 priv->dnd_same_window = FALSE;
1310                 return;
1311         }
1312
1313         DEBUG ("Page added");
1314
1315         /* Get chat object */
1316         chat = EMPATHY_CHAT (child);
1317
1318         /* Connect chat signals for this window */
1319         g_signal_connect (chat, "composing",
1320                           G_CALLBACK (chat_window_composing_cb),
1321                           window);
1322         g_signal_connect (chat, "new-message",
1323                           G_CALLBACK (chat_window_new_message_cb),
1324                           window);
1325         g_signal_connect (chat, "notify::tp-chat",
1326                           G_CALLBACK (chat_window_update_chat_tab),
1327                           window);
1328
1329         /* Set flag so we know to perform some special operations on
1330          * switch page due to the new page being added.
1331          */
1332         priv->page_added = TRUE;
1333
1334         /* Get list of chats up to date */
1335         priv->chats = g_list_append (priv->chats, chat);
1336
1337         chat_window_update_chat_tab (chat);
1338 }
1339
1340 static void
1341 chat_window_page_removed_cb (GtkNotebook      *notebook,
1342                              GtkWidget        *child,
1343                              guint             page_num,
1344                              EmpathyChatWindow *window)
1345 {
1346         EmpathyChatWindowPriv *priv;
1347         EmpathyChat           *chat;
1348
1349         priv = GET_PRIV (window);
1350
1351         /* If we just received DND to the same window, we don't want
1352          * to do anything here like removing the tab and then readding
1353          * it, so we return here and in "page-added".
1354          */
1355         if (priv->dnd_same_window) {
1356                 DEBUG ("Page removed (and will be readded to same window)");
1357                 return;
1358         }
1359
1360         DEBUG ("Page removed");
1361
1362         /* Get chat object */
1363         chat = EMPATHY_CHAT (child);
1364
1365         /* Disconnect all signal handlers for this chat and this window */
1366         g_signal_handlers_disconnect_by_func (chat,
1367                                               G_CALLBACK (chat_window_composing_cb),
1368                                               window);
1369         g_signal_handlers_disconnect_by_func (chat,
1370                                               G_CALLBACK (chat_window_new_message_cb),
1371                                               window);
1372         g_signal_handlers_disconnect_by_func (chat,
1373                                               G_CALLBACK (chat_window_update_chat_tab),
1374                                               window);
1375
1376         /* Keep list of chats up to date */
1377         priv->chats = g_list_remove (priv->chats, chat);
1378         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1379         empathy_chat_messages_read (chat);
1380         priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1381
1382         if (priv->chats == NULL) {
1383                 g_object_unref (window);
1384         } else {
1385                 chat_window_update (window);
1386         }
1387 }
1388
1389 static gboolean
1390 chat_window_focus_in_event_cb (GtkWidget        *widget,
1391                                GdkEvent         *event,
1392                                EmpathyChatWindow *window)
1393 {
1394         EmpathyChatWindowPriv *priv;
1395
1396         DEBUG ("Focus in event, updating title");
1397
1398         priv = GET_PRIV (window);
1399
1400         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1401         empathy_chat_messages_read (priv->current_chat);
1402
1403         chat_window_set_urgency_hint (window, FALSE);
1404
1405         /* Update the title, since we now mark all unread messages as read. */
1406         chat_window_update_chat_tab (priv->current_chat);
1407
1408         return FALSE;
1409 }
1410
1411 static void
1412 chat_window_drag_data_received (GtkWidget        *widget,
1413                                 GdkDragContext   *context,
1414                                 int               x,
1415                                 int               y,
1416                                 GtkSelectionData *selection,
1417                                 guint             info,
1418                                 guint             time_,
1419                                 EmpathyChatWindow *window)
1420 {
1421         if (info == DND_DRAG_TYPE_CONTACT_ID) {
1422                 EmpathyChat           *chat = NULL;
1423                 EmpathyChatWindow     *old_window;
1424                 TpAccount             *account = NULL;
1425                 TpAccountManager      *account_manager;
1426                 const gchar           *id;
1427                 gchar                **strv;
1428                 const gchar           *account_id;
1429                 const gchar           *contact_id;
1430
1431                 id = (const gchar*) gtk_selection_data_get_data (selection);
1432
1433                 /* FIXME: Perhaps should be sure that the account manager is
1434                  * prepared before calling _ensure_account on it. */
1435                 account_manager = tp_account_manager_dup ();
1436
1437                 DEBUG ("DND contact from roster with id:'%s'", id);
1438
1439                 strv = g_strsplit (id, ":", 2);
1440                 if (g_strv_length (strv) == 2) {
1441                         account_id = strv[0];
1442                         contact_id = strv[1];
1443                         account =
1444                                 tp_account_manager_ensure_account (account_manager, account_id);
1445                         if (account != NULL)
1446                                 chat = empathy_chat_window_find_chat (account, contact_id);
1447                 }
1448
1449                 if (account == NULL) {
1450                         g_strfreev (strv);
1451                         gtk_drag_finish (context, FALSE, FALSE, time_);
1452                         return;
1453                 }
1454
1455                 if (!chat) {
1456                         TpConnection *connection;
1457
1458                         connection = tp_account_get_connection (account);
1459
1460                         if (connection) {
1461                                 empathy_dispatcher_chat_with_contact_id (
1462                                         connection, contact_id, NULL, NULL);
1463                         }
1464
1465                         g_strfreev (strv);
1466                         return;
1467                 }
1468                 g_object_unref (account_manager);
1469                 g_strfreev (strv);
1470
1471                 old_window = chat_window_find_chat (chat);
1472                 if (old_window) {
1473                         if (old_window == window) {
1474                                 gtk_drag_finish (context, TRUE, FALSE, time_);
1475                                 return;
1476                         }
1477
1478                         empathy_chat_window_move_chat (old_window, window, chat);
1479                 } else {
1480                         empathy_chat_window_add_chat (window, chat);
1481                 }
1482
1483                 /* Added to take care of any outstanding chat events */
1484                 empathy_chat_window_present_chat (chat);
1485
1486                 /* We should return TRUE to remove the data when doing
1487                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1488                  * weird consequences, and we handle that internally
1489                  * anyway with add_chat () and remove_chat ().
1490                  */
1491                 gtk_drag_finish (context, TRUE, FALSE, time_);
1492         }
1493         else if (info == DND_DRAG_TYPE_TAB) {
1494                 EmpathyChat        **chat;
1495                 EmpathyChatWindow   *old_window = NULL;
1496
1497                 DEBUG ("DND tab");
1498
1499                 chat = (void *) gtk_selection_data_get_data (selection);
1500                 old_window = chat_window_find_chat (*chat);
1501
1502                 if (old_window) {
1503                         EmpathyChatWindowPriv *priv;
1504
1505                         priv = GET_PRIV (window);
1506
1507                         if (old_window == window) {
1508                                 DEBUG ("DND tab (within same window)");
1509                                 priv->dnd_same_window = TRUE;
1510                                 gtk_drag_finish (context, TRUE, FALSE, time_);
1511                                 return;
1512                         }
1513
1514                         priv->dnd_same_window = FALSE;
1515                 }
1516
1517                 /* We should return TRUE to remove the data when doing
1518                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1519                  * weird consequences, and we handle that internally
1520                  * anyway with add_chat () and remove_chat ().
1521                  */
1522                 gtk_drag_finish (context, TRUE, FALSE, time_);
1523         } else {
1524                 DEBUG ("DND from unknown source");
1525                 gtk_drag_finish (context, FALSE, FALSE, time_);
1526         }
1527 }
1528
1529 static void
1530 chat_window_finalize (GObject *object)
1531 {
1532         EmpathyChatWindow     *window;
1533         EmpathyChatWindowPriv *priv;
1534
1535         window = EMPATHY_CHAT_WINDOW (object);
1536         priv = GET_PRIV (window);
1537
1538         DEBUG ("Finalized: %p", object);
1539
1540         g_object_unref (priv->ui_manager);
1541         g_object_unref (priv->chatroom_manager);
1542         g_object_unref (priv->notify_mgr);
1543
1544         if (priv->notification != NULL) {
1545                 notify_notification_close (priv->notification, NULL);
1546                 g_object_unref (priv->notification);
1547                 priv->notification = NULL;
1548                 if (priv->notification_data != NULL)
1549                         {
1550                                 free_notification_data (priv->notification_data);
1551                                 priv->notification_data = NULL;
1552                         }
1553         }
1554
1555         chat_windows = g_list_remove (chat_windows, window);
1556         gtk_widget_destroy (priv->dialog);
1557
1558         G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1559 }
1560
1561 static void
1562 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1563 {
1564         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1565
1566         object_class->finalize = chat_window_finalize;
1567
1568         g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1569
1570         /* Set up a style for the close button with no focus padding. */
1571         gtk_rc_parse_string (
1572                 "style \"empathy-close-button-style\"\n"
1573                 "{\n"
1574                 "  GtkWidget::focus-padding = 0\n"
1575                 "  xthickness = 0\n"
1576                 "  ythickness = 0\n"
1577                 "}\n"
1578                 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1579
1580         gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1581 }
1582
1583 static void
1584 empathy_chat_window_init (EmpathyChatWindow *window)
1585 {
1586         GtkBuilder            *gui;
1587         GtkAccelGroup         *accel_group;
1588         GClosure              *closure;
1589         GtkWidget             *menu;
1590         GtkWidget             *submenu;
1591         guint                  i;
1592         GtkWidget             *chat_vbox;
1593         gchar                 *filename;
1594         EmpathySmileyManager  *smiley_manager;
1595         EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1596                 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1597
1598         window->priv = priv;
1599         filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1600         gui = empathy_builder_get_file (filename,
1601                                        "chat_window", &priv->dialog,
1602                                        "chat_vbox", &chat_vbox,
1603                                        "ui_manager", &priv->ui_manager,
1604                                        "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1605                                        "menu_conv_favorite", &priv->menu_conv_favorite,
1606                                        "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1607                                        "menu_edit_cut", &priv->menu_edit_cut,
1608                                        "menu_edit_copy", &priv->menu_edit_copy,
1609                                        "menu_edit_paste", &priv->menu_edit_paste,
1610                                        "menu_tabs_next", &priv->menu_tabs_next,
1611                                        "menu_tabs_prev", &priv->menu_tabs_prev,
1612                                        "menu_tabs_left", &priv->menu_tabs_left,
1613                                        "menu_tabs_right", &priv->menu_tabs_right,
1614                                        "menu_tabs_detach", &priv->menu_tabs_detach,
1615                                        NULL);
1616         g_free (filename);
1617
1618         empathy_builder_connect (gui, window,
1619                               "menu_conv", "activate", chat_window_conv_activate_cb,
1620                               "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1621                               "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1622                               "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1623                               "menu_conv_close", "activate", chat_window_close_activate_cb,
1624                               "menu_edit", "activate", chat_window_edit_activate_cb,
1625                               "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1626                               "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1627                               "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1628                               "menu_tabs_next", "activate", chat_window_tabs_next_activate_cb,
1629                               "menu_tabs_prev", "activate", chat_window_tabs_previous_activate_cb,
1630                               "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1631                               "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1632                               "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1633                               "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1634                               "menu_help_about", "activate", chat_window_help_about_activate_cb,
1635                               NULL);
1636
1637         g_object_ref (priv->ui_manager);
1638         g_object_unref (gui);
1639
1640         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1641
1642         priv->notebook = gtk_notebook_new ();
1643         gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1644         gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1645         gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1646         gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1647         gtk_widget_show (priv->notebook);
1648
1649         /* Set up accels */
1650         accel_group = gtk_accel_group_new ();
1651         gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1652
1653         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1654                 closure =  g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1655                                            window,
1656                                            NULL);
1657                 gtk_accel_group_connect (accel_group,
1658                                          tab_accel_keys[i],
1659                                          GDK_MOD1_MASK,
1660                                          0,
1661                                          closure);
1662         }
1663
1664         g_object_unref (accel_group);
1665
1666         /* Set up smiley menu */
1667         smiley_manager = empathy_smiley_manager_dup_singleton ();
1668         submenu = empathy_smiley_menu_new (smiley_manager,
1669                                            chat_window_insert_smiley_activate_cb,
1670                                            window);
1671         menu = gtk_ui_manager_get_widget (priv->ui_manager,
1672                 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1673         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1674         g_object_unref (smiley_manager);
1675
1676         /* Set up signals we can't do with ui file since we may need to
1677          * block/unblock them at some later stage.
1678          */
1679
1680         g_signal_connect (priv->dialog,
1681                           "delete_event",
1682                           G_CALLBACK (chat_window_delete_event_cb),
1683                           window);
1684         g_signal_connect (priv->dialog,
1685                           "focus_in_event",
1686                           G_CALLBACK (chat_window_focus_in_event_cb),
1687                           window);
1688         g_signal_connect_after (priv->notebook,
1689                                 "switch_page",
1690                                 G_CALLBACK (chat_window_page_switched_cb),
1691                                 window);
1692         g_signal_connect (priv->notebook,
1693                           "page_added",
1694                           G_CALLBACK (chat_window_page_added_cb),
1695                           window);
1696         g_signal_connect (priv->notebook,
1697                           "page_removed",
1698                           G_CALLBACK (chat_window_page_removed_cb),
1699                           window);
1700
1701         /* Set up drag and drop */
1702         gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1703                            GTK_DEST_DEFAULT_ALL,
1704                            drag_types_dest,
1705                            G_N_ELEMENTS (drag_types_dest),
1706                            GDK_ACTION_MOVE);
1707
1708         g_signal_connect (priv->notebook,
1709                           "drag-data-received",
1710                           G_CALLBACK (chat_window_drag_data_received),
1711                           window);
1712
1713         chat_windows = g_list_prepend (chat_windows, window);
1714
1715         /* Set up private details */
1716         priv->chats = NULL;
1717         priv->chats_new_msg = NULL;
1718         priv->chats_composing = NULL;
1719         priv->current_chat = NULL;
1720
1721         priv->notify_mgr = empathy_notify_manager_dup_singleton ();
1722 }
1723
1724 EmpathyChatWindow *
1725 empathy_chat_window_new (void)
1726 {
1727         return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1728 }
1729
1730 /* Returns the window to open a new tab in if there is only one window
1731  * visble, otherwise, returns NULL indicating that a new window should
1732  * be added.
1733  */
1734 EmpathyChatWindow *
1735 empathy_chat_window_get_default (void)
1736 {
1737         GList    *l;
1738         gboolean  separate_windows = TRUE;
1739
1740         empathy_conf_get_bool (empathy_conf_get (),
1741                               EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1742                               &separate_windows);
1743
1744         if (separate_windows) {
1745                 /* Always create a new window */
1746                 return NULL;
1747         }
1748
1749         for (l = chat_windows; l; l = l->next) {
1750                 EmpathyChatWindow *chat_window;
1751                 GtkWidget         *dialog;
1752
1753                 chat_window = l->data;
1754
1755                 dialog = empathy_chat_window_get_dialog (chat_window);
1756                 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1757                         /* Found a visible window on this desktop */
1758                         return chat_window;
1759                 }
1760         }
1761
1762         return NULL;
1763 }
1764
1765 GtkWidget *
1766 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1767 {
1768         EmpathyChatWindowPriv *priv;
1769
1770         g_return_val_if_fail (window != NULL, NULL);
1771
1772         priv = GET_PRIV (window);
1773
1774         return priv->dialog;
1775 }
1776
1777 void
1778 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1779                               EmpathyChat       *chat)
1780 {
1781         EmpathyChatWindowPriv *priv;
1782         GtkWidget             *label;
1783         GtkWidget             *popup_label;
1784         GtkWidget             *child;
1785
1786         g_return_if_fail (window != NULL);
1787         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1788
1789         priv = GET_PRIV (window);
1790
1791         /* Reference the chat object */
1792         g_object_ref (chat);
1793
1794         /* If this window has just been created, position it */
1795         if (priv->chats == NULL) {
1796                 const gchar *name = "chat-window";
1797                 gboolean     separate_windows;
1798
1799                 empathy_conf_get_bool (empathy_conf_get (),
1800                                        EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1801                                        &separate_windows);
1802
1803                 if (separate_windows) {
1804                         name = empathy_chat_get_id (chat);
1805                 }
1806
1807                 empathy_geometry_bind (GTK_WINDOW (priv->dialog), name);
1808         }
1809
1810         child = GTK_WIDGET (chat);
1811         label = chat_window_create_label (window, chat, TRUE);
1812         popup_label = chat_window_create_label (window, chat, FALSE);
1813         gtk_widget_show (child);
1814
1815         g_signal_connect (chat, "notify::name",
1816                           G_CALLBACK (chat_window_chat_notify_cb),
1817                           NULL);
1818         g_signal_connect (chat, "notify::subject",
1819                           G_CALLBACK (chat_window_chat_notify_cb),
1820                           NULL);
1821         g_signal_connect (chat, "notify::remote-contact",
1822                           G_CALLBACK (chat_window_chat_notify_cb),
1823                           NULL);
1824         chat_window_chat_notify_cb (chat);
1825
1826         gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1827         gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1828         gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1829         gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1830                                             TRUE, TRUE, GTK_PACK_START);
1831
1832         DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1833 }
1834
1835 void
1836 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1837                                  EmpathyChat       *chat)
1838 {
1839         EmpathyChatWindowPriv *priv;
1840         gint                   position;
1841         EmpathyContact        *remote_contact;
1842
1843         g_return_if_fail (window != NULL);
1844         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1845
1846         priv = GET_PRIV (window);
1847
1848         g_signal_handlers_disconnect_by_func (chat,
1849                                               chat_window_chat_notify_cb,
1850                                               NULL);
1851         remote_contact = g_object_get_data (G_OBJECT (chat),
1852                                             "chat-window-remote-contact");
1853         if (remote_contact) {
1854                 g_signal_handlers_disconnect_by_func (remote_contact,
1855                                                       chat_window_update_chat_tab,
1856                                                       chat);
1857         }
1858
1859         position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1860                                           GTK_WIDGET (chat));
1861         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1862
1863         DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1864
1865         g_object_unref (chat);
1866 }
1867
1868 void
1869 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1870                                EmpathyChatWindow *new_window,
1871                                EmpathyChat       *chat)
1872 {
1873         GtkWidget *widget;
1874
1875         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1876         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1877         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1878
1879         widget = GTK_WIDGET (chat);
1880
1881         DEBUG ("Chat moving with widget:%p (%d references)", widget,
1882                 G_OBJECT (widget)->ref_count);
1883
1884         /* We reference here to make sure we don't loose the widget
1885          * and the EmpathyChat object during the move.
1886          */
1887         g_object_ref (chat);
1888         g_object_ref (widget);
1889
1890         empathy_chat_window_remove_chat (old_window, chat);
1891         empathy_chat_window_add_chat (new_window, chat);
1892
1893         g_object_unref (widget);
1894         g_object_unref (chat);
1895 }
1896
1897 void
1898 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1899                                     EmpathyChat       *chat)
1900 {
1901         EmpathyChatWindowPriv *priv;
1902         gint                  page_num;
1903
1904         g_return_if_fail (window != NULL);
1905         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1906
1907         priv = GET_PRIV (window);
1908
1909         page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1910                                           GTK_WIDGET (chat));
1911         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1912                                        page_num);
1913 }
1914
1915 gboolean
1916 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1917 {
1918         EmpathyChatWindowPriv *priv;
1919         gboolean              has_focus;
1920
1921         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1922
1923         priv = GET_PRIV (window);
1924
1925         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1926
1927         return has_focus;
1928 }
1929
1930 EmpathyChat *
1931 empathy_chat_window_find_chat (TpAccount   *account,
1932                                const gchar *id)
1933 {
1934         GList *l;
1935
1936         g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1937
1938         for (l = chat_windows; l; l = l->next) {
1939                 EmpathyChatWindowPriv *priv;
1940                 EmpathyChatWindow     *window;
1941                 GList                *ll;
1942
1943                 window = l->data;
1944                 priv = GET_PRIV (window);
1945
1946                 for (ll = priv->chats; ll; ll = ll->next) {
1947                         EmpathyChat *chat;
1948
1949                         chat = ll->data;
1950
1951                         if (account == empathy_chat_get_account (chat) &&
1952                             !tp_strdiff (id, empathy_chat_get_id (chat))) {
1953                                 return chat;
1954                         }
1955                 }
1956         }
1957
1958         return NULL;
1959 }
1960
1961 void
1962 empathy_chat_window_present_chat (EmpathyChat *chat)
1963 {
1964         EmpathyChatWindow     *window;
1965         EmpathyChatWindowPriv *priv;
1966
1967         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1968
1969         window = chat_window_find_chat (chat);
1970
1971         /* If the chat has no window, create one */
1972         if (window == NULL) {
1973                 window = empathy_chat_window_get_default ();
1974                 if (!window) {
1975                         window = empathy_chat_window_new ();
1976                 }
1977
1978                 empathy_chat_window_add_chat (window, chat);
1979         }
1980
1981         priv = GET_PRIV (window);
1982         empathy_chat_window_switch_to_chat (window, chat);
1983         empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1984
1985         gtk_widget_grab_focus (chat->input_text_view);
1986 }
1987