]> git.0d.be Git - empathy.git/blob - src/empathy-chat-window.c
Merge commit 'staz/dnd'
[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 is_connected;
337         gint     page_num;
338
339         page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
340         first_page = (page_num == 0);
341         last_page = (page_num == (num_pages - 1));
342         is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
343
344         DEBUG ("Update window : Menu Contexts (Tabs & Conv)");
345
346         gtk_action_set_sensitive (priv->menu_tabs_next, TRUE);
347         gtk_action_set_sensitive (priv->menu_tabs_prev, TRUE);
348         gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
349         gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
350         gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
351         gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
352 }
353
354 static void
355 chat_window_conversation_menu_update (EmpathyChatWindowPriv *priv,
356                                       EmpathyChatWindow     *self)
357 {
358         EmpathyTpChat *tp_chat;
359         TpConnection *connection;
360         GtkAction *action;
361         gboolean sensitive = FALSE;
362
363         g_return_if_fail (priv->current_chat != NULL);
364
365         action = gtk_ui_manager_get_action (priv->ui_manager,
366                 "/chats_menubar/menu_conv/menu_conv_invite_participant");
367         tp_chat = empathy_chat_get_tp_chat (priv->current_chat);
368
369         if (tp_chat != NULL) {
370                 connection = empathy_tp_chat_get_connection (tp_chat);
371
372                 sensitive = empathy_tp_chat_can_add_contact (tp_chat) &&
373                         (tp_connection_get_status (connection, NULL) ==
374                          TP_CONNECTION_STATUS_CONNECTED);
375         }
376
377         gtk_action_set_sensitive (action, sensitive);
378 }
379
380 static void
381 chat_window_contact_menu_update (EmpathyChatWindowPriv *priv,
382                                  EmpathyChatWindow     *window)
383 {
384         GtkWidget *menu, *submenu, *orig_submenu;
385
386         menu = gtk_ui_manager_get_widget (priv->ui_manager,
387                 "/chats_menubar/menu_contact");
388         orig_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menu));
389
390         DEBUG ("Update window : Contact Menu");
391
392         if (orig_submenu == NULL || !GTK_WIDGET_VISIBLE (orig_submenu)) {
393                 submenu = empathy_chat_get_contact_menu (priv->current_chat);
394                 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
395                 gtk_widget_show (menu);
396         } else {
397                 empathy_signal_connect_weak (orig_submenu,
398                                              "notify::visible",
399                                              (GCallback)_submenu_notify_visible_changed_cb,
400                                              G_OBJECT (window));
401         }
402 }
403
404 static guint
405 get_all_unread_messages (EmpathyChatWindowPriv *priv)
406 {
407         GList *l;
408         guint nb = 0;
409
410         for (l = priv->chats_new_msg; l != NULL; l = g_list_next (l)) {
411                 EmpathyChat *chat = l->data;
412
413                 nb += empathy_chat_get_nb_unread_messages (chat);
414         }
415
416         return nb;
417 }
418
419 static gchar *
420 get_window_title_name (EmpathyChatWindowPriv *priv)
421 {
422         const gchar *active_name;
423         guint nb_chats;
424         guint current_unread_msgs;
425
426         nb_chats = g_list_length (priv->chats);
427         g_assert (nb_chats > 0);
428
429         active_name = empathy_chat_get_name (priv->current_chat);
430
431         current_unread_msgs = empathy_chat_get_nb_unread_messages (
432                         priv->current_chat);
433
434         if (nb_chats == 1) {
435                 /* only one tab */
436                 if (current_unread_msgs == 0)
437                         return g_strdup (active_name);
438                 else
439                         return g_strdup_printf (ngettext (
440                                 "%s (%d unread)",
441                                 "%s (%d unread)", current_unread_msgs),
442                                 active_name, current_unread_msgs);
443         } else {
444                 guint nb_others = nb_chats - 1;
445                 guint all_unread_msgs;
446
447                 all_unread_msgs = get_all_unread_messages (priv);
448
449                 if (all_unread_msgs == 0) {
450                         /* no unread message */
451                         return g_strdup_printf (ngettext (
452                                 "%s (and %u other)",
453                                 "%s (and %u others)", nb_others),
454                                 active_name, nb_others);
455                 }
456
457                 else if (all_unread_msgs == current_unread_msgs) {
458                         /* unread messages are in the current tab */
459                         return g_strdup_printf (ngettext (
460                                 "%s (%d unread)",
461                                 "%s (%d unread)", current_unread_msgs),
462                                 active_name, current_unread_msgs);
463                 }
464
465                 else if (current_unread_msgs == 0) {
466                         /* unread messages are in other tabs */
467                         return g_strdup_printf (ngettext (
468                                 "%s (%d unread from others)",
469                                 "%s (%d unread from others)",
470                                 all_unread_msgs),
471                                 active_name, all_unread_msgs);
472                 }
473
474                 else {
475                         /* unread messages are in all the tabs */
476                         return g_strdup_printf (ngettext (
477                                 "%s (%d unread from all)",
478                                 "%s (%d unread from all)",
479                                 all_unread_msgs),
480                                 active_name, all_unread_msgs);
481                 }
482         }
483 }
484
485 static void
486 chat_window_title_update (EmpathyChatWindowPriv *priv)
487 {
488         gchar *name;
489
490         DEBUG ("Update window : Title");
491
492         name = get_window_title_name (priv);
493         gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
494         g_free (name);
495 }
496
497 static void
498 chat_window_icon_update (EmpathyChatWindowPriv *priv)
499 {
500         GdkPixbuf      *icon;
501         EmpathyContact *remote_contact;
502         gboolean        avatar_in_icon;
503         guint           n_chats;
504
505         n_chats = g_list_length (priv->chats);
506
507         DEBUG ("Update window : Icon");
508
509         /* Update window icon */
510         if (priv->chats_new_msg) {
511                 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
512                                           EMPATHY_IMAGE_MESSAGE);
513         } else {
514                 empathy_conf_get_bool (empathy_conf_get (),
515                                        EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
516                                        &avatar_in_icon);
517
518                 if (n_chats == 1 && avatar_in_icon) {
519                         remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
520                         icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
521                         gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
522
523                         if (icon != NULL) {
524                                 g_object_unref (icon);
525                         }
526                 } else {
527                         gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
528                 }
529         }
530 }
531
532 static void
533 chat_window_close_button_update (EmpathyChatWindowPriv *priv,
534                                  gint num_pages)
535 {
536         GtkWidget *chat;
537         GtkWidget *chat_close_button;
538         gint       i;
539
540         DEBUG ("Update window : Close Button");
541
542         if (num_pages == 1) {
543                 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
544                 chat_close_button = g_object_get_data (G_OBJECT (chat),
545                                 "chat-window-tab-close-button");
546                 gtk_widget_hide (chat_close_button);
547         } else {
548                 for (i=0; i<num_pages; i++) {
549                         chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
550                         chat_close_button = g_object_get_data (G_OBJECT (chat),
551                                         "chat-window-tab-close-button");
552                         gtk_widget_show (chat_close_button);
553                 }
554         }
555 }
556
557 static void
558 chat_window_update (EmpathyChatWindow *window)
559 {
560         EmpathyChatWindowPriv *priv = GET_PRIV (window);
561         gint                   num_pages;
562
563         num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
564
565         DEBUG ("Update window");
566
567         /* Update Tab menu */
568         chat_window_menu_context_update (priv,
569                                          num_pages);
570
571         chat_window_conversation_menu_update (priv, window);
572
573         chat_window_contact_menu_update (priv,
574                                          window);
575
576         chat_window_title_update (priv);
577
578         chat_window_icon_update (priv);
579
580         chat_window_close_button_update (priv,
581                                          num_pages);
582 }
583
584 static void
585 append_markup_printf (GString    *string,
586                       const char *format,
587                       ...)
588 {
589         gchar *tmp;
590         va_list args;
591
592         va_start (args, format);
593
594         tmp = g_markup_vprintf_escaped (format, args);
595         g_string_append (string, tmp);
596         g_free (tmp);
597
598         va_end (args);
599 }
600
601 static void
602 chat_window_update_chat_tab (EmpathyChat *chat)
603 {
604         EmpathyChatWindow     *window;
605         EmpathyChatWindowPriv *priv;
606         EmpathyContact        *remote_contact;
607         const gchar           *name;
608         const gchar           *id;
609         TpAccount             *account;
610         const gchar           *subject;
611         const gchar           *status = NULL;
612         GtkWidget             *widget;
613         GString               *tooltip;
614         gchar                 *markup;
615         const gchar           *icon_name;
616         GtkWidget             *tab_image;
617         GtkWidget             *menu_image;
618
619         window = chat_window_find_chat (chat);
620         if (!window) {
621                 return;
622         }
623         priv = GET_PRIV (window);
624
625         /* Get information */
626         name = empathy_chat_get_name (chat);
627         account = empathy_chat_get_account (chat);
628         subject = empathy_chat_get_subject (chat);
629         remote_contact = empathy_chat_get_remote_contact (chat);
630
631         DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
632                 name, tp_proxy_get_object_path (account), subject, remote_contact);
633
634         /* Update tab image */
635         if (empathy_chat_get_tp_chat (chat) == NULL) {
636                 /* No TpChat, we are disconnected */
637                 icon_name = NULL;
638         }
639         else if (g_list_find (priv->chats_new_msg, chat)) {
640                 icon_name = EMPATHY_IMAGE_MESSAGE;
641         }
642         else if (g_list_find (priv->chats_composing, chat)) {
643                 icon_name = EMPATHY_IMAGE_TYPING;
644         }
645         else if (remote_contact) {
646                 icon_name = empathy_icon_name_for_contact (remote_contact);
647         } else {
648                 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
649         }
650
651         tab_image = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
652         menu_image = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
653         if (icon_name != NULL) {
654                 gtk_image_set_from_icon_name (GTK_IMAGE (tab_image), icon_name, GTK_ICON_SIZE_MENU);
655                 gtk_widget_show (tab_image);
656                 gtk_image_set_from_icon_name (GTK_IMAGE (menu_image), icon_name, GTK_ICON_SIZE_MENU);
657                 gtk_widget_show (menu_image);
658         } else {
659                 gtk_widget_hide (tab_image);
660                 gtk_widget_hide (menu_image);
661         }
662
663         /* Update tab tooltip */
664         tooltip = g_string_new (NULL);
665
666         if (remote_contact) {
667                 id = empathy_contact_get_id (remote_contact);
668                 status = empathy_contact_get_presence_message (remote_contact);
669         } else {
670                 id = name;
671         }
672
673         append_markup_printf (tooltip,
674                               "<b>%s</b><small> (%s)</small>",
675                               id,
676                               tp_account_get_display_name (account));
677
678         if (!EMP_STR_EMPTY (status)) {
679                 append_markup_printf (tooltip, "\n<i>%s</i>", status);
680         }
681
682         if (subject) {
683                 append_markup_printf (tooltip, "\n<b>%s</b> %s",
684                                       _("Topic:"), subject);
685         }
686
687         if (g_list_find (priv->chats_composing, chat)) {
688                 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
689         }
690
691         markup = g_string_free (tooltip, FALSE);
692         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
693         gtk_widget_set_tooltip_markup (widget, markup);
694         widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
695         gtk_widget_set_tooltip_markup (widget, markup);
696         g_free (markup);
697
698         /* Update tab and menu label */
699         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
700         gtk_label_set_text (GTK_LABEL (widget), name);
701         widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
702         gtk_label_set_text (GTK_LABEL (widget), name);
703
704         /* Update the window if it's the current chat */
705         if (priv->current_chat == chat) {
706                 chat_window_update (window);
707         }
708 }
709
710 static void
711 chat_window_chat_notify_cb (EmpathyChat *chat)
712 {
713         EmpathyContact *old_remote_contact;
714         EmpathyContact *remote_contact = NULL;
715
716         old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
717         remote_contact = empathy_chat_get_remote_contact (chat);
718
719         if (old_remote_contact != remote_contact) {
720                 /* The remote-contact associated with the chat changed, we need
721                  * to keep track of any change of that contact and update the
722                  * window each time. */
723                 if (remote_contact) {
724                         g_signal_connect_swapped (remote_contact, "notify",
725                                                   G_CALLBACK (chat_window_update_chat_tab),
726                                                   chat);
727                 }
728                 if (old_remote_contact) {
729                         g_signal_handlers_disconnect_by_func (old_remote_contact,
730                                                               chat_window_update_chat_tab,
731                                                               chat);
732                 }
733
734                 g_object_set_data_full (G_OBJECT (chat), "chat-window-remote-contact",
735                                    g_object_ref (remote_contact), (GDestroyNotify) g_object_unref);
736         }
737
738         chat_window_update_chat_tab (chat);
739 }
740
741 static void
742 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
743                                        EmpathySmiley        *smiley,
744                                        gpointer              window)
745 {
746         EmpathyChatWindowPriv *priv = GET_PRIV (window);
747         EmpathyChat           *chat;
748         GtkTextBuffer         *buffer;
749         GtkTextIter            iter;
750
751         chat = priv->current_chat;
752
753         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
754         gtk_text_buffer_get_end_iter (buffer, &iter);
755         gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
756 }
757
758 static void
759 chat_window_conv_activate_cb (GtkAction         *action,
760                               EmpathyChatWindow *window)
761 {
762         EmpathyChatWindowPriv *priv = GET_PRIV (window);
763         gboolean               is_room;
764         gboolean               active;
765         EmpathyContact        *remote_contact = NULL;
766
767         /* Favorite room menu */
768         is_room = empathy_chat_is_room (priv->current_chat);
769         if (is_room) {
770                 const gchar *room;
771                 TpAccount   *account;
772                 gboolean     found = FALSE;
773                 EmpathyChatroom *chatroom;
774
775                 room = empathy_chat_get_id (priv->current_chat);
776                 account = empathy_chat_get_account (priv->current_chat);
777                 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
778                                                        account, room);
779                 if (chatroom != NULL)
780                         found = empathy_chatroom_is_favorite (chatroom);
781
782                 DEBUG ("This room %s favorite", found ? "is" : "is not");
783                 gtk_toggle_action_set_active (
784                         GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
785         }
786         gtk_action_set_visible (priv->menu_conv_favorite, is_room);
787
788         /* Show contacts menu */
789         g_object_get (priv->current_chat,
790                       "remote-contact", &remote_contact,
791                       "show-contacts", &active,
792                       NULL);
793         if (remote_contact == NULL) {
794                 gtk_toggle_action_set_active (
795                         GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
796                                            active);
797         }
798         gtk_action_set_visible (priv->menu_conv_toggle_contacts,
799                                 (remote_contact == NULL));
800         if (remote_contact != NULL) {
801                 g_object_unref (remote_contact);
802         }
803 }
804
805 static void
806 chat_window_clear_activate_cb (GtkAction         *action,
807                                EmpathyChatWindow *window)
808 {
809         EmpathyChatWindowPriv *priv = GET_PRIV (window);
810
811         empathy_chat_clear (priv->current_chat);
812 }
813
814 static void
815 chat_window_favorite_toggled_cb (GtkToggleAction   *toggle_action,
816                                  EmpathyChatWindow *window)
817 {
818         EmpathyChatWindowPriv *priv = GET_PRIV (window);
819         gboolean               active;
820         TpAccount             *account;
821         const gchar           *room;
822         EmpathyChatroom       *chatroom;
823
824         active = gtk_toggle_action_get_active (toggle_action);
825         account = empathy_chat_get_account (priv->current_chat);
826         room = empathy_chat_get_id (priv->current_chat);
827
828         chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
829                                                   account, room);
830
831         if (chatroom == NULL) {
832                 const gchar *name;
833
834                 name = empathy_chat_get_name (priv->current_chat);
835                 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
836                 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
837                 g_object_unref (chatroom);
838         }
839
840         empathy_chatroom_set_favorite (chatroom, active);
841 }
842
843 static void
844 chat_window_contacts_toggled_cb (GtkToggleAction   *toggle_action,
845                                  EmpathyChatWindow *window)
846 {
847         EmpathyChatWindowPriv *priv = GET_PRIV (window);
848         gboolean               active;
849
850         active = gtk_toggle_action_get_active (toggle_action);
851
852         empathy_chat_set_show_contacts (priv->current_chat, active);
853 }
854
855 static void
856 got_contact_cb (EmpathyTpContactFactory *factory,
857                 EmpathyContact          *contact,
858                 const GError            *error,
859                 gpointer                 user_data,
860                 GObject                 *object)
861 {
862         EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (user_data);
863
864         if (error != NULL) {
865                 DEBUG ("Failed: %s", error->message);
866                 return;
867         } else {
868                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (tp_chat),
869                                 contact, _("Inviting you to this room"));
870         }
871 }
872
873 static void
874 chat_window_invite_participant_activate_cb (GtkAction         *action,
875                                             EmpathyChatWindow *window)
876 {
877         EmpathyChatWindowPriv *priv;
878         GtkWidget             *dialog;
879         EmpathyTpChat         *tp_chat;
880         TpChannel             *channel;
881         int                    response;
882         TpAccount             *account;
883
884         priv = GET_PRIV (window);
885
886         g_return_if_fail (priv->current_chat != NULL);
887
888         tp_chat = empathy_chat_get_tp_chat (priv->current_chat);
889         channel = empathy_tp_chat_get_channel (tp_chat);
890         account = empathy_chat_get_account (priv->current_chat);
891
892         dialog = empathy_invite_participant_dialog_new (
893                         GTK_WINDOW (priv->dialog), account);
894         gtk_widget_show (dialog);
895
896         response = gtk_dialog_run (GTK_DIALOG (dialog));
897
898         if (response == GTK_RESPONSE_ACCEPT) {
899                 TpConnection *connection;
900                 EmpathyTpContactFactory *factory;
901                 const char *id;
902
903                 id = empathy_contact_selector_dialog_get_selected (
904                                 EMPATHY_CONTACT_SELECTOR_DIALOG (dialog), NULL);
905                 if (EMP_STR_EMPTY (id)) goto out;
906
907                 connection = tp_channel_borrow_connection (channel);
908                 factory = empathy_tp_contact_factory_dup_singleton (connection);
909
910                 empathy_tp_contact_factory_get_from_id (factory, id,
911                         got_contact_cb, tp_chat,  NULL, NULL);
912
913                 g_object_unref (factory);
914         }
915
916 out:
917         gtk_widget_destroy (dialog);
918 }
919
920 static void
921 chat_window_close_activate_cb (GtkAction         *action,
922                                EmpathyChatWindow *window)
923 {
924         EmpathyChatWindowPriv *priv;
925
926         priv = GET_PRIV (window);
927
928         g_return_if_fail (priv->current_chat != NULL);
929
930         empathy_chat_window_remove_chat (window, priv->current_chat);
931 }
932
933 static void
934 chat_window_edit_activate_cb (GtkAction         *action,
935                               EmpathyChatWindow *window)
936 {
937         EmpathyChatWindowPriv *priv;
938         GtkClipboard         *clipboard;
939         GtkTextBuffer        *buffer;
940         gboolean              text_available;
941
942         priv = GET_PRIV (window);
943
944         g_return_if_fail (priv->current_chat != NULL);
945
946         if (!empathy_chat_get_tp_chat (priv->current_chat)) {
947                 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
948                 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
949                 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
950                 return;
951         }
952
953         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
954         if (gtk_text_buffer_get_has_selection (buffer)) {
955                 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
956                 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
957         } else {
958                 gboolean selection;
959
960                 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
961
962                 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
963                 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
964         }
965
966         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
967         text_available = gtk_clipboard_wait_is_text_available (clipboard);
968         gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
969 }
970
971 static void
972 chat_window_cut_activate_cb (GtkAction         *action,
973                              EmpathyChatWindow *window)
974 {
975         EmpathyChatWindowPriv *priv;
976
977         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
978
979         priv = GET_PRIV (window);
980
981         empathy_chat_cut (priv->current_chat);
982 }
983
984 static void
985 chat_window_copy_activate_cb (GtkAction         *action,
986                               EmpathyChatWindow *window)
987 {
988         EmpathyChatWindowPriv *priv;
989
990         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
991
992         priv = GET_PRIV (window);
993
994         empathy_chat_copy (priv->current_chat);
995 }
996
997 static void
998 chat_window_paste_activate_cb (GtkAction         *action,
999                                EmpathyChatWindow *window)
1000 {
1001         EmpathyChatWindowPriv *priv;
1002
1003         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
1004
1005         priv = GET_PRIV (window);
1006
1007         empathy_chat_paste (priv->current_chat);
1008 }
1009
1010 static void
1011 chat_window_find_activate_cb (GtkAction         *action,
1012                               EmpathyChatWindow *window)
1013 {
1014         EmpathyChatWindowPriv *priv;
1015
1016         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
1017
1018         priv = GET_PRIV (window);
1019
1020         empathy_chat_find (priv->current_chat);
1021 }
1022
1023 static void
1024 chat_window_tabs_next_activate_cb (GtkAction         *action,
1025                                    EmpathyChatWindow *window)
1026 {
1027         EmpathyChatWindowPriv *priv;
1028         EmpathyChat           *chat;
1029         gint                  index_, numPages;
1030
1031         priv = GET_PRIV (window);
1032
1033         chat = priv->current_chat;
1034         index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1035         numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
1036
1037         if (index_ == (numPages - 1)) {
1038                 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), 0);
1039                 return;
1040         }
1041
1042         gtk_notebook_next_page (GTK_NOTEBOOK (priv->notebook));
1043 }
1044
1045 static void
1046 chat_window_tabs_previous_activate_cb (GtkAction         *action,
1047                                    EmpathyChatWindow *window)
1048 {
1049         EmpathyChatWindowPriv *priv;
1050         EmpathyChat           *chat;
1051         gint                  index_, numPages;
1052
1053         priv = GET_PRIV (window);
1054
1055         chat = priv->current_chat;
1056         index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1057         numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
1058
1059         if (index_ <= 0) {
1060                 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), numPages - 1);
1061                 return;
1062         }
1063
1064         gtk_notebook_prev_page (GTK_NOTEBOOK (priv->notebook));
1065 }
1066
1067 static void
1068 chat_window_tabs_left_activate_cb (GtkAction         *action,
1069                                    EmpathyChatWindow *window)
1070 {
1071         EmpathyChatWindowPriv *priv;
1072         EmpathyChat           *chat;
1073         gint                  index_;
1074
1075         priv = GET_PRIV (window);
1076
1077         chat = priv->current_chat;
1078         index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1079         if (index_ <= 0) {
1080                 return;
1081         }
1082
1083         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
1084                                     GTK_WIDGET (chat),
1085                                     index_ - 1);
1086 }
1087
1088 static void
1089 chat_window_tabs_right_activate_cb (GtkAction         *action,
1090                                     EmpathyChatWindow *window)
1091 {
1092         EmpathyChatWindowPriv *priv;
1093         EmpathyChat           *chat;
1094         gint                  index_;
1095
1096         priv = GET_PRIV (window);
1097
1098         chat = priv->current_chat;
1099         index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1100
1101         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
1102                                     GTK_WIDGET (chat),
1103                                     index_ + 1);
1104 }
1105
1106 static void
1107 chat_window_detach_activate_cb (GtkAction         *action,
1108                                 EmpathyChatWindow *window)
1109 {
1110         EmpathyChatWindowPriv *priv;
1111         EmpathyChatWindow     *new_window;
1112         EmpathyChat           *chat;
1113
1114         priv = GET_PRIV (window);
1115
1116         chat = priv->current_chat;
1117         new_window = empathy_chat_window_new ();
1118
1119         empathy_chat_window_move_chat (window, new_window, chat);
1120
1121         priv = GET_PRIV (new_window);
1122         gtk_widget_show (priv->dialog);
1123 }
1124
1125 static void
1126 chat_window_help_contents_activate_cb (GtkAction         *action,
1127                                        EmpathyChatWindow *window)
1128 {
1129         EmpathyChatWindowPriv *priv = GET_PRIV (window);
1130
1131         empathy_url_show (priv->dialog, "ghelp:empathy");
1132 }
1133
1134 static void
1135 chat_window_help_about_activate_cb (GtkAction         *action,
1136                                     EmpathyChatWindow *window)
1137 {
1138         EmpathyChatWindowPriv *priv = GET_PRIV (window);
1139
1140         empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
1141 }
1142
1143 static gboolean
1144 chat_window_delete_event_cb (GtkWidget        *dialog,
1145                              GdkEvent         *event,
1146                              EmpathyChatWindow *window)
1147 {
1148         EmpathyChatWindowPriv *priv = GET_PRIV (window);
1149
1150         DEBUG ("Delete event received");
1151
1152         g_object_ref (window);
1153         while (priv->chats) {
1154                 empathy_chat_window_remove_chat (window, priv->chats->data);
1155         }
1156         g_object_unref (window);
1157
1158         return TRUE;
1159 }
1160
1161 static void
1162 chat_window_composing_cb (EmpathyChat       *chat,
1163                           gboolean          is_composing,
1164                           EmpathyChatWindow *window)
1165 {
1166         EmpathyChatWindowPriv *priv;
1167
1168         priv = GET_PRIV (window);
1169
1170         if (is_composing && !g_list_find (priv->chats_composing, chat)) {
1171                 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
1172         } else {
1173                 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1174         }
1175
1176         chat_window_update_chat_tab (chat);
1177 }
1178
1179 static void
1180 chat_window_set_urgency_hint (EmpathyChatWindow *window,
1181                               gboolean          urgent)
1182 {
1183         EmpathyChatWindowPriv *priv;
1184
1185         priv = GET_PRIV (window);
1186
1187         DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
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         DEBUG ("Focus in event, updating title");
1526
1527         priv = GET_PRIV (window);
1528
1529         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1530         empathy_chat_messages_read (priv->current_chat);
1531
1532         chat_window_set_urgency_hint (window, FALSE);
1533
1534         /* Update the title, since we now mark all unread messages as read. */
1535         chat_window_update_chat_tab (priv->current_chat);
1536
1537         return FALSE;
1538 }
1539
1540 static gboolean
1541 chat_window_drag_drop (GtkWidget        *widget,
1542                          GdkDragContext   *context,
1543                          int               x,
1544                          int               y,
1545                          guint             time_,
1546                          EmpathyChatWindow *window)
1547 {
1548         GdkAtom target;
1549         EmpathyChatWindowPriv *priv;
1550
1551         priv = GET_PRIV (window);
1552
1553         target = gtk_drag_dest_find_target (widget, context, priv->file_targets);
1554         if (target == GDK_NONE)
1555                 target = gtk_drag_dest_find_target (widget, context, priv->contact_targets);
1556
1557         if (target != GDK_NONE) {
1558                 gtk_drag_get_data (widget, context, target, time_);
1559                 return TRUE;
1560         }
1561
1562         return FALSE;
1563 }
1564
1565 static gboolean
1566 chat_window_drag_motion (GtkWidget        *widget,
1567                          GdkDragContext   *context,
1568                          int               x,
1569                          int               y,
1570                          guint             time_,
1571                          EmpathyChatWindow *window)
1572 {
1573         GdkAtom target;
1574         EmpathyChatWindowPriv *priv;
1575
1576         priv = GET_PRIV (window);
1577
1578         target = gtk_drag_dest_find_target (widget, context, priv->file_targets);
1579         if (target != GDK_NONE) {
1580                 /* This is a file drag.  Ensure the contact is online and set the
1581                    drag type to COPY.  Note that it's possible that the tab will
1582                    be switched by GTK+ after a timeout from drag_motion without
1583                    getting another drag_motion to disable the drop.  You have
1584                    to hold your mouse really still.
1585                  */
1586                 EmpathyContact *contact;
1587
1588                 priv = GET_PRIV (window);
1589                 contact = empathy_chat_get_remote_contact (priv->current_chat);
1590                 /* contact is NULL for multi-user chats.  We don't do
1591                  * file transfers to MUCs.  We also don't send files
1592                  * to offline contacts or contacts that don't support
1593                  * file transfer.
1594                  */
1595                 if ((contact == NULL) || !empathy_contact_is_online (contact)) {
1596                         gdk_drag_status (context, 0, time_);
1597                         return FALSE;
1598                 }
1599                 if (!(empathy_contact_get_capabilities (contact)
1600                            & EMPATHY_CAPABILITIES_FT)) {
1601                         gdk_drag_status (context, 0, time_);
1602                         return FALSE;
1603                 }
1604                 gdk_drag_status (context, GDK_ACTION_COPY, time_);
1605                 return TRUE;
1606         }
1607
1608         target = gtk_drag_dest_find_target (widget, context, priv->contact_targets);
1609         if (target != GDK_NONE) {
1610                 /* This is a drag of a contact from a contact list.  Set to COPY.
1611                    FIXME: If this drag is to a MUC window, it invites the user.
1612                    Otherwise, it opens a chat.  Should we use a different drag
1613                    type for invites?  Should we allow ASK?
1614                  */
1615                 gdk_drag_status (context, GDK_ACTION_COPY, time_);
1616                 return TRUE;
1617         }
1618
1619         return FALSE;
1620 }
1621
1622 static void
1623 chat_window_drag_data_received (GtkWidget        *widget,
1624                                 GdkDragContext   *context,
1625                                 int               x,
1626                                 int               y,
1627                                 GtkSelectionData *selection,
1628                                 guint             info,
1629                                 guint             time_,
1630                                 EmpathyChatWindow *window)
1631 {
1632         if (info == DND_DRAG_TYPE_CONTACT_ID) {
1633                 EmpathyChat           *chat = NULL;
1634                 EmpathyChatWindow     *old_window;
1635                 TpAccount             *account = NULL;
1636                 TpAccountManager      *account_manager;
1637                 const gchar           *id;
1638                 gchar                **strv;
1639                 const gchar           *account_id;
1640                 const gchar           *contact_id;
1641
1642                 id = (const gchar*) gtk_selection_data_get_data (selection);
1643
1644                 /* FIXME: Perhaps should be sure that the account manager is
1645                  * prepared before calling _ensure_account on it. */
1646                 account_manager = tp_account_manager_dup ();
1647
1648                 DEBUG ("DND contact from roster with id:'%s'", id);
1649
1650                 strv = g_strsplit (id, ":", 2);
1651                 if (g_strv_length (strv) == 2) {
1652                         account_id = strv[0];
1653                         contact_id = strv[1];
1654                         account =
1655                                 tp_account_manager_ensure_account (account_manager, account_id);
1656                         if (account != NULL)
1657                                 chat = empathy_chat_window_find_chat (account, contact_id);
1658                 }
1659
1660                 if (account == NULL) {
1661                         g_strfreev (strv);
1662                         gtk_drag_finish (context, FALSE, FALSE, time_);
1663                         return;
1664                 }
1665
1666                 if (!chat) {
1667                         TpConnection *connection;
1668
1669                         connection = tp_account_get_connection (account);
1670
1671                         if (connection) {
1672                                 empathy_dispatcher_chat_with_contact_id (
1673                                         connection, contact_id, NULL, NULL);
1674                         }
1675
1676                         g_strfreev (strv);
1677                         return;
1678                 }
1679                 g_object_unref (account_manager);
1680                 g_strfreev (strv);
1681
1682                 old_window = chat_window_find_chat (chat);
1683                 if (old_window) {
1684                         if (old_window == window) {
1685                                 gtk_drag_finish (context, TRUE, FALSE, time_);
1686                                 return;
1687                         }
1688
1689                         empathy_chat_window_move_chat (old_window, window, chat);
1690                 } else {
1691                         empathy_chat_window_add_chat (window, chat);
1692                 }
1693
1694                 /* Added to take care of any outstanding chat events */
1695                 empathy_chat_window_present_chat (chat);
1696
1697                 /* We should return TRUE to remove the data when doing
1698                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1699                  * weird consequences, and we handle that internally
1700                  * anyway with add_chat () and remove_chat ().
1701                  */
1702                 gtk_drag_finish (context, TRUE, FALSE, time_);
1703         }
1704         else if (info == DND_DRAG_TYPE_URI_LIST) {
1705                 EmpathyChatWindowPriv *priv;
1706                 EmpathyContact *contact;
1707                 const gchar *data;
1708
1709                 priv = GET_PRIV (window);
1710                 contact = empathy_chat_get_remote_contact (priv->current_chat);
1711
1712                 /* contact is NULL when current_chat is a multi-user chat.
1713                  * We don't do file transfers to MUCs, so just cancel the drag.
1714                  */
1715                 if (contact == NULL) {
1716                         gtk_drag_finish (context, TRUE, FALSE, time_);
1717                         return;
1718                 }
1719
1720                 data = (const gchar *) gtk_selection_data_get_data (selection);
1721                 empathy_send_file_from_uri_list (contact, data);
1722
1723                 gtk_drag_finish (context, TRUE, FALSE, time_);
1724         }
1725         else if (info == DND_DRAG_TYPE_TAB) {
1726                 EmpathyChat        **chat;
1727                 EmpathyChatWindow   *old_window = NULL;
1728
1729                 DEBUG ("DND tab");
1730
1731                 chat = (void *) gtk_selection_data_get_data (selection);
1732                 old_window = chat_window_find_chat (*chat);
1733
1734                 if (old_window) {
1735                         EmpathyChatWindowPriv *priv;
1736
1737                         priv = GET_PRIV (window);
1738                         priv->dnd_same_window = (old_window == window);
1739                         DEBUG ("DND tab (within same window: %s)",
1740                                 priv->dnd_same_window ? "Yes" : "No");
1741                 }
1742         } else {
1743                 DEBUG ("DND from unknown source");
1744                 gtk_drag_finish (context, FALSE, FALSE, time_);
1745         }
1746 }
1747
1748 static void
1749 chat_window_finalize (GObject *object)
1750 {
1751         EmpathyChatWindow     *window;
1752         EmpathyChatWindowPriv *priv;
1753
1754         window = EMPATHY_CHAT_WINDOW (object);
1755         priv = GET_PRIV (window);
1756
1757         DEBUG ("Finalized: %p", object);
1758
1759         g_object_unref (priv->ui_manager);
1760         g_object_unref (priv->chatroom_manager);
1761         g_object_unref (priv->notify_mgr);
1762
1763         if (priv->notification != NULL) {
1764                 notify_notification_close (priv->notification, NULL);
1765                 g_object_unref (priv->notification);
1766                 priv->notification = NULL;
1767                 if (priv->notification_data != NULL)
1768                         {
1769                                 free_notification_data (priv->notification_data);
1770                                 priv->notification_data = NULL;
1771                         }
1772         }
1773
1774         if (priv->contact_targets) {
1775                 gtk_target_list_unref (priv->contact_targets);
1776         }
1777         if (priv->file_targets) {
1778                 gtk_target_list_unref (priv->file_targets);
1779         }
1780
1781         chat_windows = g_list_remove (chat_windows, window);
1782         gtk_widget_destroy (priv->dialog);
1783
1784         G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1785 }
1786
1787 static void
1788 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1789 {
1790         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1791
1792         object_class->finalize = chat_window_finalize;
1793
1794         g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1795
1796         /* Set up a style for the close button with no focus padding. */
1797         gtk_rc_parse_string (
1798                 "style \"empathy-close-button-style\"\n"
1799                 "{\n"
1800                 "  GtkWidget::focus-padding = 0\n"
1801                 "  xthickness = 0\n"
1802                 "  ythickness = 0\n"
1803                 "}\n"
1804                 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1805
1806         gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1807 }
1808
1809 static void
1810 empathy_chat_window_init (EmpathyChatWindow *window)
1811 {
1812         GtkBuilder            *gui;
1813         GtkAccelGroup         *accel_group;
1814         GClosure              *closure;
1815         GtkWidget             *menu;
1816         GtkWidget             *submenu;
1817         guint                  i;
1818         GtkWidget             *chat_vbox;
1819         gchar                 *filename;
1820         EmpathySmileyManager  *smiley_manager;
1821         EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1822                 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1823
1824         window->priv = priv;
1825         filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1826         gui = empathy_builder_get_file (filename,
1827                                        "chat_window", &priv->dialog,
1828                                        "chat_vbox", &chat_vbox,
1829                                        "ui_manager", &priv->ui_manager,
1830                                        "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1831                                        "menu_conv_favorite", &priv->menu_conv_favorite,
1832                                        "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1833                                        "menu_edit_cut", &priv->menu_edit_cut,
1834                                        "menu_edit_copy", &priv->menu_edit_copy,
1835                                        "menu_edit_paste", &priv->menu_edit_paste,
1836                                        "menu_edit_find", &priv->menu_edit_find,
1837                                        "menu_tabs_next", &priv->menu_tabs_next,
1838                                        "menu_tabs_prev", &priv->menu_tabs_prev,
1839                                        "menu_tabs_left", &priv->menu_tabs_left,
1840                                        "menu_tabs_right", &priv->menu_tabs_right,
1841                                        "menu_tabs_detach", &priv->menu_tabs_detach,
1842                                        NULL);
1843         g_free (filename);
1844
1845         empathy_builder_connect (gui, window,
1846                               "menu_conv", "activate", chat_window_conv_activate_cb,
1847                               "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1848                               "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1849                               "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1850                               "menu_conv_invite_participant", "activate", chat_window_invite_participant_activate_cb,
1851                               "menu_conv_close", "activate", chat_window_close_activate_cb,
1852                               "menu_edit", "activate", chat_window_edit_activate_cb,
1853                               "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1854                               "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1855                               "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1856                               "menu_edit_find", "activate", chat_window_find_activate_cb,
1857                               "menu_tabs_next", "activate", chat_window_tabs_next_activate_cb,
1858                               "menu_tabs_prev", "activate", chat_window_tabs_previous_activate_cb,
1859                               "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1860                               "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1861                               "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1862                               "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1863                               "menu_help_about", "activate", chat_window_help_about_activate_cb,
1864                               NULL);
1865
1866         g_object_ref (priv->ui_manager);
1867         g_object_unref (gui);
1868
1869         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1870
1871         priv->notebook = gtk_notebook_new ();
1872         gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1873         gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1874         gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1875         gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1876         gtk_widget_show (priv->notebook);
1877
1878         /* Set up accels */
1879         accel_group = gtk_accel_group_new ();
1880         gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1881
1882         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1883                 closure =  g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1884                                            window,
1885                                            NULL);
1886                 gtk_accel_group_connect (accel_group,
1887                                          tab_accel_keys[i],
1888                                          GDK_MOD1_MASK,
1889                                          0,
1890                                          closure);
1891         }
1892
1893         g_object_unref (accel_group);
1894
1895         /* Set up drag target lists */
1896         priv->contact_targets = gtk_target_list_new (drag_types_dest_contact,
1897                                                      G_N_ELEMENTS (drag_types_dest_contact));
1898         priv->file_targets = gtk_target_list_new (drag_types_dest_file,
1899                                                   G_N_ELEMENTS (drag_types_dest_file));
1900
1901         /* Set up smiley menu */
1902         smiley_manager = empathy_smiley_manager_dup_singleton ();
1903         submenu = empathy_smiley_menu_new (smiley_manager,
1904                                            chat_window_insert_smiley_activate_cb,
1905                                            window);
1906         menu = gtk_ui_manager_get_widget (priv->ui_manager,
1907                 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1908         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1909         g_object_unref (smiley_manager);
1910
1911         /* Set up signals we can't do with ui file since we may need to
1912          * block/unblock them at some later stage.
1913          */
1914
1915         g_signal_connect (priv->dialog,
1916                           "delete_event",
1917                           G_CALLBACK (chat_window_delete_event_cb),
1918                           window);
1919         g_signal_connect (priv->dialog,
1920                           "focus_in_event",
1921                           G_CALLBACK (chat_window_focus_in_event_cb),
1922                           window);
1923         g_signal_connect_after (priv->notebook,
1924                                 "switch_page",
1925                                 G_CALLBACK (chat_window_page_switched_cb),
1926                                 window);
1927         g_signal_connect (priv->notebook,
1928                           "page_added",
1929                           G_CALLBACK (chat_window_page_added_cb),
1930                           window);
1931         g_signal_connect (priv->notebook,
1932                           "page_removed",
1933                           G_CALLBACK (chat_window_page_removed_cb),
1934                           window);
1935
1936         /* Set up drag and drop */
1937         gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1938                            GTK_DEST_DEFAULT_HIGHLIGHT,
1939                            drag_types_dest,
1940                            G_N_ELEMENTS (drag_types_dest),
1941                            GDK_ACTION_MOVE | GDK_ACTION_COPY);
1942
1943         /* connect_after to allow GtkNotebook's built-in tab switching */
1944         g_signal_connect_after (priv->notebook,
1945                                 "drag-motion",
1946                                 G_CALLBACK (chat_window_drag_motion),
1947                                 window);
1948         g_signal_connect (priv->notebook,
1949                           "drag-data-received",
1950                           G_CALLBACK (chat_window_drag_data_received),
1951                           window);
1952         g_signal_connect (priv->notebook,
1953                           "drag-drop",
1954                           G_CALLBACK (chat_window_drag_drop),
1955                           window);
1956
1957         chat_windows = g_list_prepend (chat_windows, window);
1958
1959         /* Set up private details */
1960         priv->chats = NULL;
1961         priv->chats_new_msg = NULL;
1962         priv->chats_composing = NULL;
1963         priv->current_chat = NULL;
1964
1965         priv->notify_mgr = empathy_notify_manager_dup_singleton ();
1966 }
1967
1968 EmpathyChatWindow *
1969 empathy_chat_window_new (void)
1970 {
1971         return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1972 }
1973
1974 /* Returns the window to open a new tab in if there is only one window
1975  * visble, otherwise, returns NULL indicating that a new window should
1976  * be added.
1977  */
1978 EmpathyChatWindow *
1979 empathy_chat_window_get_default (gboolean room)
1980 {
1981         GList    *l;
1982         gboolean  separate_windows = TRUE;
1983
1984         empathy_conf_get_bool (empathy_conf_get (),
1985                               EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1986                               &separate_windows);
1987
1988         if (separate_windows) {
1989                 /* Always create a new window */
1990                 return NULL;
1991         }
1992
1993         for (l = chat_windows; l; l = l->next) {
1994                 EmpathyChatWindowPriv *priv;
1995                 EmpathyChatWindow *chat_window;
1996                 GtkWidget         *dialog;
1997
1998                 chat_window = l->data;
1999                 priv = GET_PRIV (chat_window);
2000
2001                 dialog = empathy_chat_window_get_dialog (chat_window);
2002                 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
2003                         guint nb_rooms, nb_private;
2004                         empathy_chat_window_get_nb_chats (chat_window, &nb_rooms, &nb_private);
2005
2006                         /* Skip the window if there aren't any rooms in it */
2007                         if (room && nb_rooms == 0)
2008                                 continue;
2009
2010                         /* Skip the window if there aren't any 1-1 chats in it */
2011                         if (!room && nb_private == 0)
2012                                 continue;
2013
2014                         /* Found a visible window on this desktop */
2015                         return chat_window;
2016                 }
2017         }
2018
2019         return NULL;
2020 }
2021
2022 GtkWidget *
2023 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
2024 {
2025         EmpathyChatWindowPriv *priv;
2026
2027         g_return_val_if_fail (window != NULL, NULL);
2028
2029         priv = GET_PRIV (window);
2030
2031         return priv->dialog;
2032 }
2033
2034 void
2035 empathy_chat_window_add_chat (EmpathyChatWindow *window,
2036                               EmpathyChat       *chat)
2037 {
2038         EmpathyChatWindowPriv *priv;
2039         GtkWidget             *label;
2040         GtkWidget             *popup_label;
2041         GtkWidget             *child;
2042         GValue                value = { 0, };
2043
2044         g_return_if_fail (window != NULL);
2045         g_return_if_fail (EMPATHY_IS_CHAT (chat));
2046
2047         priv = GET_PRIV (window);
2048
2049         /* Reference the chat object */
2050         g_object_ref (chat);
2051
2052         /* If this window has just been created, position it */
2053         if (priv->chats == NULL) {
2054                 const gchar *name = "chat-window";
2055                 gboolean     separate_windows;
2056
2057                 empathy_conf_get_bool (empathy_conf_get (),
2058                                        EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
2059                                        &separate_windows);
2060
2061                 if (separate_windows) {
2062                         name = empathy_chat_get_id (chat);
2063                 }
2064                 else if (empathy_chat_is_room (chat)) {
2065                         name = "room-window";
2066                 }
2067
2068                 empathy_geometry_bind (GTK_WINDOW (priv->dialog), name);
2069         }
2070
2071         child = GTK_WIDGET (chat);
2072         label = chat_window_create_label (window, chat, TRUE);
2073         popup_label = chat_window_create_label (window, chat, FALSE);
2074         gtk_widget_show (child);
2075
2076         g_signal_connect (chat, "notify::name",
2077                           G_CALLBACK (chat_window_chat_notify_cb),
2078                           NULL);
2079         g_signal_connect (chat, "notify::subject",
2080                           G_CALLBACK (chat_window_chat_notify_cb),
2081                           NULL);
2082         g_signal_connect (chat, "notify::remote-contact",
2083                           G_CALLBACK (chat_window_chat_notify_cb),
2084                           NULL);
2085         chat_window_chat_notify_cb (chat);
2086
2087         gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
2088         gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
2089         gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
2090         g_value_init (&value, G_TYPE_BOOLEAN);
2091         g_value_set_boolean (&value, TRUE);
2092         gtk_container_child_set_property (GTK_CONTAINER (priv->notebook),
2093                                           child, "tab-expand" , &value);
2094         gtk_container_child_set_property (GTK_CONTAINER (priv->notebook),
2095                                           child,  "tab-fill" , &value);
2096         g_value_unset (&value);
2097
2098         DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
2099 }
2100
2101 void
2102 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
2103                                  EmpathyChat       *chat)
2104 {
2105         EmpathyChatWindowPriv *priv;
2106         gint                   position;
2107         EmpathyContact        *remote_contact;
2108
2109         g_return_if_fail (window != NULL);
2110         g_return_if_fail (EMPATHY_IS_CHAT (chat));
2111
2112         priv = GET_PRIV (window);
2113
2114         g_signal_handlers_disconnect_by_func (chat,
2115                                               chat_window_chat_notify_cb,
2116                                               NULL);
2117         remote_contact = g_object_get_data (G_OBJECT (chat),
2118                                             "chat-window-remote-contact");
2119         if (remote_contact) {
2120                 g_signal_handlers_disconnect_by_func (remote_contact,
2121                                                       chat_window_update_chat_tab,
2122                                                       chat);
2123         }
2124
2125         position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
2126                                           GTK_WIDGET (chat));
2127         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
2128
2129         DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
2130
2131         g_object_unref (chat);
2132 }
2133
2134 void
2135 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
2136                                EmpathyChatWindow *new_window,
2137                                EmpathyChat       *chat)
2138 {
2139         GtkWidget *widget;
2140
2141         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
2142         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
2143         g_return_if_fail (EMPATHY_IS_CHAT (chat));
2144
2145         widget = GTK_WIDGET (chat);
2146
2147         DEBUG ("Chat moving with widget:%p (%d references)", widget,
2148                 G_OBJECT (widget)->ref_count);
2149
2150         /* We reference here to make sure we don't loose the widget
2151          * and the EmpathyChat object during the move.
2152          */
2153         g_object_ref (chat);
2154         g_object_ref (widget);
2155
2156         empathy_chat_window_remove_chat (old_window, chat);
2157         empathy_chat_window_add_chat (new_window, chat);
2158
2159         g_object_unref (widget);
2160         g_object_unref (chat);
2161 }
2162
2163 void
2164 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
2165                                     EmpathyChat       *chat)
2166 {
2167         EmpathyChatWindowPriv *priv;
2168         gint                  page_num;
2169
2170         g_return_if_fail (window != NULL);
2171         g_return_if_fail (EMPATHY_IS_CHAT (chat));
2172
2173         priv = GET_PRIV (window);
2174
2175         page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
2176                                           GTK_WIDGET (chat));
2177         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
2178                                        page_num);
2179 }
2180
2181 gboolean
2182 empathy_chat_window_has_focus (EmpathyChatWindow *window)
2183 {
2184         EmpathyChatWindowPriv *priv;
2185         gboolean              has_focus;
2186
2187         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
2188
2189         priv = GET_PRIV (window);
2190
2191         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
2192
2193         return has_focus;
2194 }
2195
2196 EmpathyChat *
2197 empathy_chat_window_find_chat (TpAccount   *account,
2198                                const gchar *id)
2199 {
2200         GList *l;
2201
2202         g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
2203
2204         for (l = chat_windows; l; l = l->next) {
2205                 EmpathyChatWindowPriv *priv;
2206                 EmpathyChatWindow     *window;
2207                 GList                *ll;
2208
2209                 window = l->data;
2210                 priv = GET_PRIV (window);
2211
2212                 for (ll = priv->chats; ll; ll = ll->next) {
2213                         EmpathyChat *chat;
2214
2215                         chat = ll->data;
2216
2217                         if (account == empathy_chat_get_account (chat) &&
2218                             !tp_strdiff (id, empathy_chat_get_id (chat))) {
2219                                 return chat;
2220                         }
2221                 }
2222         }
2223
2224         return NULL;
2225 }
2226
2227 void
2228 empathy_chat_window_present_chat (EmpathyChat *chat)
2229 {
2230         EmpathyChatWindow     *window;
2231         EmpathyChatWindowPriv *priv;
2232
2233         g_return_if_fail (EMPATHY_IS_CHAT (chat));
2234
2235         window = chat_window_find_chat (chat);
2236
2237         /* If the chat has no window, create one */
2238         if (window == NULL) {
2239                 window = empathy_chat_window_get_default (empathy_chat_is_room (chat));
2240                 if (!window) {
2241                         window = empathy_chat_window_new ();
2242                 }
2243
2244                 empathy_chat_window_add_chat (window, chat);
2245         }
2246
2247         priv = GET_PRIV (window);
2248         empathy_chat_window_switch_to_chat (window, chat);
2249         empathy_window_present (GTK_WINDOW (priv->dialog));
2250
2251         gtk_widget_grab_focus (chat->input_text_view);
2252 }
2253
2254 void
2255 empathy_chat_window_get_nb_chats (EmpathyChatWindow *self,
2256                                guint *nb_rooms,
2257                                guint *nb_private)
2258 {
2259         EmpathyChatWindowPriv *priv = GET_PRIV (self);
2260         GList *l;
2261         guint _nb_rooms = 0, _nb_private = 0;
2262
2263         for (l = priv->chats; l != NULL; l = g_list_next (l)) {
2264                 if (empathy_chat_is_room (EMPATHY_CHAT (l->data)))
2265                         _nb_rooms++;
2266                 else
2267                         _nb_private++;
2268         }
2269
2270         if (nb_rooms != NULL)
2271                 *nb_rooms = _nb_rooms;
2272         if (nb_private != NULL)
2273                 *nb_private = _nb_private;
2274 }