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