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