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