Chat window's tab label changes the font color when got an incoming msg. (Fixes ...
[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  */
27
28 #include <config.h>
29
30 #include <string.h>
31
32 #include <gtk/gtk.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <glib/gi18n.h>
35 #include <libnotify/notification.h>
36
37 #include <telepathy-glib/util.h>
38 #include <libmissioncontrol/mission-control.h>
39
40 #include <libempathy/empathy-contact.h>
41 #include <libempathy/empathy-message.h>
42 #include <libempathy/empathy-dispatcher.h>
43 #include <libempathy/empathy-chatroom-manager.h>
44 #include <libempathy/empathy-account-manager.h>
45 #include <libempathy/empathy-utils.h>
46
47 #include <libempathy-gtk/empathy-images.h>
48 #include <libempathy-gtk/empathy-conf.h>
49 #include <libempathy-gtk/empathy-contact-dialogs.h>
50 #include <libempathy-gtk/empathy-log-window.h>
51 #include <libempathy-gtk/empathy-geometry.h>
52 #include <libempathy-gtk/empathy-smiley-manager.h>
53 #include <libempathy-gtk/empathy-sound.h>
54 #include <libempathy-gtk/empathy-ui-utils.h>
55
56 #include "empathy-chat-window.h"
57 #include "empathy-about-dialog.h"
58 #include "empathy-misc.h"
59
60 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
61 #include <libempathy/empathy-debug.h>
62
63 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatWindow)
64 typedef struct {
65         EmpathyChat *current_chat;
66         GList       *chats;
67         GList       *chats_new_msg;
68         GList       *chats_composing;
69         gboolean     page_added;
70         gboolean     dnd_same_window;
71         guint        save_geometry_id;
72         EmpathyChatroomManager *chatroom_manager;
73         GtkWidget   *dialog;
74         GtkWidget   *notebook;
75         NotifyNotification *notification;
76
77         /* Menu items. */
78         GtkUIManager *ui_manager;
79         GtkAction   *menu_conv_insert_smiley;
80         GtkAction   *menu_conv_favorite;
81         GtkAction   *menu_conv_toggle_contacts;
82
83         GtkAction   *menu_edit_cut;
84         GtkAction   *menu_edit_copy;
85         GtkAction   *menu_edit_paste;
86
87         GtkAction   *menu_tabs_next;
88         GtkAction   *menu_tabs_prev;
89         GtkAction   *menu_tabs_left;
90         GtkAction   *menu_tabs_right;
91         GtkAction   *menu_tabs_detach;
92 } EmpathyChatWindowPriv;
93
94 static GList *chat_windows = NULL;
95
96 static const guint tab_accel_keys[] = {
97         GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
98         GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
99 };
100
101 typedef enum {
102         DND_DRAG_TYPE_CONTACT_ID,
103         DND_DRAG_TYPE_TAB
104 } DndDragType;
105
106 static const GtkTargetEntry drag_types_dest[] = {
107         { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
108         { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
109 };
110
111 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
112
113 static void
114 chat_window_accel_cb (GtkAccelGroup    *accelgroup,
115                       GObject          *object,
116                       guint             key,
117                       GdkModifierType   mod,
118                       EmpathyChatWindow *window)
119 {
120         EmpathyChatWindowPriv *priv;
121         gint                  num = -1;
122         gint                  i;
123
124         priv = GET_PRIV (window);
125
126         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
127                 if (tab_accel_keys[i] == key) {
128                         num = i;
129                         break;
130                 }
131         }
132
133         if (num != -1) {
134                 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
135         }
136 }
137
138 static EmpathyChatWindow *
139 chat_window_find_chat (EmpathyChat *chat)
140 {
141         EmpathyChatWindowPriv *priv;
142         GList                 *l, *ll;
143
144         for (l = chat_windows; l; l = l->next) {
145                 priv = GET_PRIV (l->data);
146                 ll = g_list_find (priv->chats, chat);
147                 if (ll) {
148                         return l->data;
149                 }
150         }
151
152         return NULL;
153 }
154
155 static void
156 chat_window_close_clicked_cb (GtkAction   *action,
157                               EmpathyChat *chat)
158 {
159         EmpathyChatWindow *window;
160
161         window = chat_window_find_chat (chat);
162         empathy_chat_window_remove_chat (window, chat);
163 }
164
165 static void
166 chat_tab_style_set_cb (GtkWidget *hbox,
167                                        GtkStyle  *previous_style,
168                                        gpointer   user_data)
169 {
170         GtkWidget *button;
171         int char_width, h, w;
172         PangoContext *context;
173         PangoFontMetrics *metrics;
174
175         button = g_object_get_data (G_OBJECT (user_data),
176                 "chat-window-tab-close-button");
177         context = gtk_widget_get_pango_context (hbox);
178
179         metrics = pango_context_get_metrics (context, gtk_widget_get_style (hbox)->font_desc,
180                 pango_context_get_language (context));
181         char_width = pango_font_metrics_get_approximate_char_width (metrics);
182         pango_font_metrics_unref (metrics);
183
184         gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
185                                            GTK_ICON_SIZE_MENU, &w, &h);
186
187         /* Request at least about 12 chars width plus at least space for the status
188          * image and the close button */
189         gtk_widget_set_size_request (hbox,
190                 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
191
192         gtk_widget_set_size_request (button, w, h);
193 }
194
195 static GtkWidget *
196 chat_window_create_label (EmpathyChatWindow *window,
197                           EmpathyChat       *chat,
198                           gboolean           is_tab_label)
199 {
200         EmpathyChatWindowPriv *priv;
201         GtkWidget            *hbox;
202         GtkWidget            *name_label;
203         GtkWidget            *status_image;
204         GtkWidget            *close_button;
205         GtkWidget            *close_image;
206         GtkWidget            *event_box;
207         GtkWidget            *event_box_hbox;
208         PangoAttrList        *attr_list;
209         PangoAttribute       *attr;
210
211         priv = GET_PRIV (window);
212
213         /* The spacing between the button and the label. */
214         hbox = gtk_hbox_new (FALSE, 0);
215
216         event_box = gtk_event_box_new ();
217         gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
218
219         name_label = gtk_label_new (NULL);
220         if (is_tab_label)
221                 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
222
223         attr_list = pango_attr_list_new ();
224         attr = pango_attr_scale_new (1/1.2);
225         attr->start_index = 0;
226         attr->end_index = -1;
227         pango_attr_list_insert (attr_list, attr);
228         gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
229         pango_attr_list_unref (attr_list);
230
231         gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
232         gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
233         g_object_set_data (G_OBJECT (chat),
234                 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
235                 name_label);
236
237         status_image = gtk_image_new ();
238
239         /* Spacing between the icon and label. */
240         event_box_hbox = gtk_hbox_new (FALSE, 0);
241
242         gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
243         gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
244
245         g_object_set_data (G_OBJECT (chat),
246                 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
247                 status_image);
248         g_object_set_data (G_OBJECT (chat),
249                 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
250                 event_box);
251
252         gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
253         gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
254
255         if (is_tab_label) {
256                 close_button = gtk_button_new ();
257                 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
258                 g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
259
260                 /* We don't want focus/keynav for the button to avoid clutter, and
261                  * Ctrl-W works anyway.
262                  */
263                 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_FOCUS);
264                 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_DEFAULT);
265
266                 /* Set the name to make the special rc style match. */
267                 gtk_widget_set_name (close_button, "empathy-close-button");
268
269                 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
270
271                 gtk_container_add (GTK_CONTAINER (close_button), close_image);
272
273                 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
274
275                 g_signal_connect (close_button,
276                                   "clicked",
277                                   G_CALLBACK (chat_window_close_clicked_cb),
278                                   chat);
279
280                 /* React to theme changes and also setup the size correctly.  */
281                 g_signal_connect (hbox,
282                                   "style-set",
283                                   G_CALLBACK (chat_tab_style_set_cb),
284                                   chat);
285         }
286
287         gtk_widget_show_all (hbox);
288
289         return hbox;
290 }
291
292 static void
293 chat_window_update (EmpathyChatWindow *window)
294 {
295         EmpathyChatWindowPriv *priv = GET_PRIV (window);
296         gboolean               first_page;
297         gboolean               last_page;
298         gboolean               is_connected;
299         gint                   num_pages;
300         gint                   page_num;
301         gint                   i;
302         const gchar           *name;
303         guint                  n_chats;
304         GdkPixbuf             *icon;
305         EmpathyContact        *remote_contact;
306         gboolean               avatar_in_icon;
307         GtkWidget             *chat;
308         GtkWidget             *chat_close_button;
309         GtkWidget             *submenu;
310         GtkWidget             *menu;
311
312         /* Get information */
313         page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
314         num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
315         first_page = (page_num == 0);
316         last_page = (page_num == (num_pages - 1));
317         is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
318         name = empathy_chat_get_name (priv->current_chat);
319         n_chats = g_list_length (priv->chats);
320
321         DEBUG ("Update window");
322
323         /* Update menu */
324         gtk_action_set_sensitive (priv->menu_tabs_next, !last_page);
325         gtk_action_set_sensitive (priv->menu_tabs_prev, !first_page);
326         gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
327         gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
328         gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
329         gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
330
331         /* Update Contact menu */
332         menu = gtk_ui_manager_get_widget (priv->ui_manager,
333                 "/chats_menubar/menu_contact");
334         submenu = empathy_chat_get_contact_menu (priv->current_chat);
335         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
336         gtk_widget_show (menu);
337
338         /* Update window title */
339         if (n_chats == 1) {
340                 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
341         } else {
342                 gchar *title;
343
344                 title = g_strdup_printf (_("Conversations (%d)"), n_chats);
345                 gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
346                 g_free (title);
347         }
348
349         /* Update window icon */
350         if (priv->chats_new_msg) {
351                 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
352                                           EMPATHY_IMAGE_MESSAGE);
353         } else {
354                 empathy_conf_get_bool (empathy_conf_get (),
355                                        EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
356                                        &avatar_in_icon);
357
358                 if (n_chats == 1 && avatar_in_icon) {
359                         remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
360                         icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
361                         gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
362
363                         if (icon != NULL) {
364                                 g_object_unref (icon);
365                         }
366                 } else {
367                         gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
368                 }
369         }
370
371         if (num_pages == 1) {
372                 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
373                 chat_close_button = g_object_get_data (G_OBJECT (chat),
374                                 "chat-window-tab-close-button");
375                 gtk_widget_hide (chat_close_button);
376         } else {
377                 for (i=0; i<num_pages; i++) {
378                         chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
379                         chat_close_button = g_object_get_data (G_OBJECT (chat),
380                                         "chat-window-tab-close-button");
381                         gtk_widget_show (chat_close_button);
382                 }
383         }
384 }
385
386 static void
387 append_markup_printf (GString    *string,
388                       const char *format,
389                       ...)
390 {
391         gchar *tmp;
392         va_list args;
393
394         va_start (args, format);
395
396         tmp = g_markup_vprintf_escaped (format, args);
397         g_string_append (string, tmp);
398         g_free (tmp);
399
400         va_end (args);
401 }
402
403 static void
404 chat_window_update_chat_tab (EmpathyChat *chat)
405 {
406         EmpathyChatWindow     *window;
407         EmpathyChatWindowPriv *priv;
408         EmpathyContact        *remote_contact;
409         const gchar           *name;
410         const gchar           *id;
411         EmpathyAccount        *account;
412         const gchar           *subject;
413         const gchar           *status = NULL;
414         GtkWidget             *widget;
415         GString               *tooltip;
416         gchar                 *markup;
417         const gchar           *icon_name;
418
419         window = chat_window_find_chat (chat);
420         if (!window) {
421                 return;
422         }
423         priv = GET_PRIV (window);
424
425         /* Get information */
426         name = empathy_chat_get_name (chat);
427         account = empathy_chat_get_account (chat);
428         subject = empathy_chat_get_subject (chat);
429         remote_contact = empathy_chat_get_remote_contact (chat);
430
431         DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
432                 name, empathy_account_get_unique_name (account), subject, remote_contact);
433
434         /* Update tab image */
435         if (g_list_find (priv->chats_new_msg, chat)) {
436                 icon_name = EMPATHY_IMAGE_MESSAGE;
437         }
438         else if (g_list_find (priv->chats_composing, chat)) {
439                 icon_name = EMPATHY_IMAGE_TYPING;
440         }
441         else if (remote_contact) {
442                 icon_name = empathy_icon_name_for_contact (remote_contact);
443         } else {
444                 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
445         }
446         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
447         gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
448         widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
449         gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
450
451         /* Update tab tooltip */
452         tooltip = g_string_new (NULL);
453
454         if (remote_contact) {
455                 id = empathy_contact_get_id (remote_contact);
456                 status = empathy_contact_get_presence_message (remote_contact);
457         } else {
458                 id = name;
459         }
460
461         append_markup_printf (tooltip,
462                               "<b>%s</b><small> (%s)</small>",
463                               id,
464                               empathy_account_get_display_name (account));
465
466         if (!EMP_STR_EMPTY (status)) {
467                 append_markup_printf (tooltip, "\n<i>%s</i>", status);
468         }
469
470         if (subject) {
471                 append_markup_printf (tooltip, "\n<b>%s</b> %s",
472                                       _("Topic:"), subject);
473         }
474
475         if (g_list_find (priv->chats_composing, chat)) {
476                 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
477         }
478
479         markup = g_string_free (tooltip, FALSE);
480         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
481         gtk_widget_set_tooltip_markup (widget, markup);
482         widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
483         gtk_widget_set_tooltip_markup (widget, markup);
484         g_free (markup);
485
486         /* Update tab and menu label */
487         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
488         gtk_label_set_text (GTK_LABEL (widget), name);
489         widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
490         gtk_label_set_text (GTK_LABEL (widget), name);
491
492         /* Update the window if it's the current chat */
493         if (priv->current_chat == chat) {
494                 chat_window_update (window);
495         }
496 }
497
498 static void
499 chat_window_chat_notify_cb (EmpathyChat *chat)
500 {
501         EmpathyContact *old_remote_contact;
502         EmpathyContact *remote_contact = NULL;
503
504         old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
505         remote_contact = empathy_chat_get_remote_contact (chat);
506
507         if (old_remote_contact != remote_contact) {
508                 /* The remote-contact associated with the chat changed, we need
509                  * to keep track of any change of that contact and update the
510                  * window each time. */
511                 if (remote_contact) {
512                         g_signal_connect_swapped (remote_contact, "notify",
513                                                   G_CALLBACK (chat_window_update_chat_tab),
514                                                   chat);
515                 }
516                 if (old_remote_contact) {
517                         g_signal_handlers_disconnect_by_func (old_remote_contact,
518                                                               chat_window_update_chat_tab,
519                                                               chat);
520                 }
521
522                 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
523                                    remote_contact);
524         }
525
526         chat_window_update_chat_tab (chat);
527 }
528
529 static void
530 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
531                                        EmpathySmiley        *smiley,
532                                        gpointer              window)
533 {
534         EmpathyChatWindowPriv *priv = GET_PRIV (window);
535         EmpathyChat           *chat;
536         GtkTextBuffer         *buffer;
537         GtkTextIter            iter;
538
539         chat = priv->current_chat;
540
541         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
542         gtk_text_buffer_get_end_iter (buffer, &iter);
543         gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
544 }
545
546 static void
547 chat_window_conv_activate_cb (GtkAction         *action,
548                               EmpathyChatWindow *window)
549 {
550         EmpathyChatWindowPriv *priv = GET_PRIV (window);
551         gboolean               is_room;
552         gboolean               active;
553         EmpathyContact        *remote_contact = NULL;
554
555         /* Favorite room menu */
556         is_room = empathy_chat_is_room (priv->current_chat);
557         if (is_room) {
558                 const gchar *room;
559                 EmpathyAccount   *account;
560                 gboolean     found = FALSE;
561                 EmpathyChatroom *chatroom;
562
563                 room = empathy_chat_get_id (priv->current_chat);
564                 account = empathy_chat_get_account (priv->current_chat);
565                 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
566                                                        account, room);
567                 if (chatroom != NULL)
568                         found = empathy_chatroom_is_favorite (chatroom);
569
570                 DEBUG ("This room %s favorite", found ? "is" : "is not");
571                 gtk_toggle_action_set_active (
572                         GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
573         }
574         gtk_action_set_visible (priv->menu_conv_favorite, is_room);
575
576         /* Show contacts menu */
577         g_object_get (priv->current_chat,
578                       "remote-contact", &remote_contact,
579                       "show-contacts", &active,
580                       NULL);
581         if (remote_contact == NULL) {
582                 gtk_toggle_action_set_active (
583                         GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
584                                            active);
585         }
586         gtk_action_set_visible (priv->menu_conv_toggle_contacts,
587                                 (remote_contact == NULL));
588         if (remote_contact != NULL) {
589                 g_object_unref (remote_contact);
590         }
591 }
592
593 static void
594 chat_window_clear_activate_cb (GtkAction         *action,
595                                EmpathyChatWindow *window)
596 {
597         EmpathyChatWindowPriv *priv = GET_PRIV (window);
598
599         empathy_chat_clear (priv->current_chat);
600 }
601
602 static void
603 chat_window_favorite_toggled_cb (GtkToggleAction   *toggle_action,
604                                  EmpathyChatWindow *window)
605 {
606         EmpathyChatWindowPriv *priv = GET_PRIV (window);
607         gboolean               active;
608         EmpathyAccount        *account;
609         const gchar           *room;
610         EmpathyChatroom       *chatroom;
611
612         active = gtk_toggle_action_get_active (toggle_action);
613         account = empathy_chat_get_account (priv->current_chat);
614         room = empathy_chat_get_id (priv->current_chat);
615
616         chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
617                                                   account, room);
618
619         if (chatroom == NULL) {
620                 const gchar *name;
621
622                 name = empathy_chat_get_name (priv->current_chat);
623                 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
624                 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
625                 g_object_unref (chatroom);
626   }
627
628         empathy_chatroom_set_favorite (chatroom, active);
629 }
630
631 static void
632 chat_window_contacts_toggled_cb (GtkToggleAction   *toggle_action,
633                                  EmpathyChatWindow *window)
634 {
635         EmpathyChatWindowPriv *priv = GET_PRIV (window);
636         gboolean               active;
637
638         active = gtk_toggle_action_get_active (toggle_action);
639
640         empathy_chat_set_show_contacts (priv->current_chat, active);
641 }
642
643 static const gchar *
644 chat_get_window_id_for_geometry (EmpathyChat *chat)
645 {
646         const gchar *res = NULL;
647         gboolean     separate_windows;
648
649         empathy_conf_get_bool (empathy_conf_get (),
650                                EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
651                                &separate_windows);
652
653         if (separate_windows) {
654                 res = empathy_chat_get_id (chat);
655         }
656
657         return res ? res : "chat-window";
658 }
659
660 static gboolean
661 chat_window_save_geometry_timeout_cb (EmpathyChatWindow *window)
662 {
663         EmpathyChatWindowPriv *priv;
664         gint                  x, y, w, h;
665
666         priv = GET_PRIV (window);
667
668         gtk_window_get_size (GTK_WINDOW (priv->dialog), &w, &h);
669         gtk_window_get_position (GTK_WINDOW (priv->dialog), &x, &y);
670
671         empathy_geometry_save (chat_get_window_id_for_geometry (priv->current_chat),
672                                x, y, w, h);
673
674         priv->save_geometry_id = 0;
675
676         return FALSE;
677 }
678
679 static gboolean
680 chat_window_configure_event_cb (GtkWidget         *widget,
681                                 GdkEventConfigure *event,
682                                 EmpathyChatWindow  *window)
683 {
684         EmpathyChatWindowPriv *priv;
685
686         priv = GET_PRIV (window);
687
688         if (priv->save_geometry_id != 0) {
689                 g_source_remove (priv->save_geometry_id);
690         }
691
692         priv->save_geometry_id =
693                 g_timeout_add_seconds (1,
694                                        (GSourceFunc) chat_window_save_geometry_timeout_cb,
695                                        window);
696
697         return FALSE;
698 }
699
700 static void
701 chat_window_close_activate_cb (GtkAction         *action,
702                                EmpathyChatWindow *window)
703 {
704         EmpathyChatWindowPriv *priv;
705
706         priv = GET_PRIV (window);
707
708         g_return_if_fail (priv->current_chat != NULL);
709
710         empathy_chat_window_remove_chat (window, priv->current_chat);
711 }
712
713 static void
714 chat_window_edit_activate_cb (GtkAction         *action,
715                               EmpathyChatWindow *window)
716 {
717         EmpathyChatWindowPriv *priv;
718         GtkClipboard         *clipboard;
719         GtkTextBuffer        *buffer;
720         gboolean              text_available;
721
722         priv = GET_PRIV (window);
723
724         g_return_if_fail (priv->current_chat != NULL);
725
726         if (!empathy_chat_get_tp_chat (priv->current_chat)) {
727                 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
728                 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
729                 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
730                 return;
731         }
732
733         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
734         if (gtk_text_buffer_get_has_selection (buffer)) {
735                 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
736                 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
737         } else {
738                 gboolean selection;
739
740                 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
741
742                 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
743                 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
744         }
745
746         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
747         text_available = gtk_clipboard_wait_is_text_available (clipboard);
748         gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
749 }
750
751 static void
752 chat_window_cut_activate_cb (GtkAction         *action,
753                              EmpathyChatWindow *window)
754 {
755         EmpathyChatWindowPriv *priv;
756
757         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
758
759         priv = GET_PRIV (window);
760
761         empathy_chat_cut (priv->current_chat);
762 }
763
764 static void
765 chat_window_copy_activate_cb (GtkAction         *action,
766                               EmpathyChatWindow *window)
767 {
768         EmpathyChatWindowPriv *priv;
769
770         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
771
772         priv = GET_PRIV (window);
773
774         empathy_chat_copy (priv->current_chat);
775 }
776
777 static void
778 chat_window_paste_activate_cb (GtkAction         *action,
779                                EmpathyChatWindow *window)
780 {
781         EmpathyChatWindowPriv *priv;
782
783         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
784
785         priv = GET_PRIV (window);
786
787         empathy_chat_paste (priv->current_chat);
788 }
789
790 static void
791 chat_window_tabs_left_activate_cb (GtkAction         *action,
792                                    EmpathyChatWindow *window)
793 {
794         EmpathyChatWindowPriv *priv;
795         EmpathyChat           *chat;
796         gint                  index;
797
798         priv = GET_PRIV (window);
799
800         chat = priv->current_chat;
801         index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
802         if (index <= 0) {
803                 return;
804         }
805
806         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
807                                     GTK_WIDGET (chat),
808                                     index - 1);
809 }
810
811 static void
812 chat_window_tabs_right_activate_cb (GtkAction         *action,
813                                     EmpathyChatWindow *window)
814 {
815         EmpathyChatWindowPriv *priv;
816         EmpathyChat           *chat;
817         gint                  index;
818
819         priv = GET_PRIV (window);
820
821         chat = priv->current_chat;
822         index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
823
824         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
825                                     GTK_WIDGET (chat),
826                                     index + 1);
827 }
828
829 static void
830 chat_window_detach_activate_cb (GtkAction         *action,
831                                 EmpathyChatWindow *window)
832 {
833         EmpathyChatWindowPriv *priv;
834         EmpathyChatWindow     *new_window;
835         EmpathyChat           *chat;
836
837         priv = GET_PRIV (window);
838
839         chat = priv->current_chat;
840         new_window = empathy_chat_window_new ();
841
842         empathy_chat_window_move_chat (window, new_window, chat);
843
844         priv = GET_PRIV (new_window);
845         gtk_widget_show (priv->dialog);
846 }
847
848 static void
849 chat_window_help_contents_activate_cb (GtkAction         *action,
850                                        EmpathyChatWindow *window)
851 {
852         EmpathyChatWindowPriv *priv = GET_PRIV (window);
853
854         empathy_url_show (priv->dialog, "ghelp:empathy?chat");
855 }
856
857 static void
858 chat_window_help_about_activate_cb (GtkAction         *action,
859                                     EmpathyChatWindow *window)
860 {
861         EmpathyChatWindowPriv *priv = GET_PRIV (window);
862
863         empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
864 }
865
866 static gboolean
867 chat_window_delete_event_cb (GtkWidget        *dialog,
868                              GdkEvent         *event,
869                              EmpathyChatWindow *window)
870 {
871         EmpathyChatWindowPriv *priv = GET_PRIV (window);
872
873         DEBUG ("Delete event received");
874
875         g_object_ref (window);
876         while (priv->chats) {
877                 empathy_chat_window_remove_chat (window, priv->chats->data);
878         }
879         g_object_unref (window);
880
881         return TRUE;
882 }
883
884 static void
885 chat_window_composing_cb (EmpathyChat       *chat,
886                           gboolean          is_composing,
887                           EmpathyChatWindow *window)
888 {
889         EmpathyChatWindowPriv *priv;
890
891         priv = GET_PRIV (window);
892
893         if (is_composing && !g_list_find (priv->chats_composing, chat)) {
894                 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
895         } else {
896                 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
897         }
898
899         chat_window_update_chat_tab (chat);
900 }
901
902 static void
903 chat_window_set_urgency_hint (EmpathyChatWindow *window,
904                               gboolean          urgent)
905 {
906         EmpathyChatWindowPriv *priv;
907
908         priv = GET_PRIV (window);
909
910         DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
911         gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
912 }
913
914 typedef struct {
915         EmpathyChatWindow *window;
916         EmpathyChat *chat;
917 } NotificationData;
918
919 static void
920 chat_window_notification_closed_cb (NotifyNotification *notify,
921                                     NotificationData *cb_data)
922 {
923         EmpathyNotificationClosedReason reason = 0;
924         EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
925
926 #ifdef notify_notification_get_closed_reason
927         reason = notify_notification_get_closed_reason (notify);
928 #endif
929         if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
930                 empathy_chat_window_present_chat (cb_data->chat);
931         }
932
933         g_object_unref (notify);
934         priv->notification = NULL;
935         g_object_unref (cb_data->chat);
936         g_slice_free (NotificationData, cb_data);
937 }
938
939 static void
940 chat_window_show_or_update_notification (EmpathyChatWindow *window,
941                                          EmpathyMessage *message,
942                                          EmpathyChat *chat)
943 {
944         EmpathyContact *sender;
945         const gchar *header;
946         char *escaped;
947         const char *body;
948         GdkPixbuf *pixbuf;
949         NotificationData *cb_data;
950         EmpathyChatWindowPriv *priv = GET_PRIV (window);
951         gboolean res;
952
953         if (!empathy_notification_is_enabled ()) {
954                 return;
955         } else {
956                 empathy_conf_get_bool (empathy_conf_get (),
957                                        EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
958                 if (!res) {
959                         return;
960                 }
961         }
962
963         cb_data = g_slice_new0 (NotificationData);
964         cb_data->chat = g_object_ref (chat);
965         cb_data->window = window;
966
967         sender = empathy_message_get_sender (message);
968         header = empathy_contact_get_name (sender);
969         body = empathy_message_get_body (message);
970         escaped = g_markup_escape_text (body, -1);
971
972         pixbuf = empathy_misc_get_pixbuf_for_notification (sender, EMPATHY_IMAGE_NEW_MESSAGE);
973
974         if (priv->notification != NULL) {
975                 notify_notification_update (priv->notification,
976                                             header, escaped, NULL);
977                 /* if icon doesn't exist libnotify will crash */
978                 if (pixbuf != NULL)
979                         notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
980         } else {
981                 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
982                 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
983                 /* if icon doesn't exist libnotify will crash */
984                 if (pixbuf != NULL)
985                         notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
986
987                 g_signal_connect (priv->notification, "closed",
988                                   G_CALLBACK (chat_window_notification_closed_cb), cb_data);
989         }
990
991         notify_notification_show (priv->notification, NULL);
992
993         g_object_unref (pixbuf);
994         g_free (escaped);
995 }
996
997 static void
998 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
999 {
1000         gchar *markup;
1001         GtkWidget *widget;
1002
1003         if (empathy_chat_is_room (chat) == FALSE)
1004                 return;
1005
1006         markup = g_markup_printf_escaped ("<span color=\"%s\">%s</span>",
1007                         "red",
1008                         empathy_chat_get_name (chat));
1009
1010         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1011         gtk_label_set_markup (GTK_LABEL (widget), markup);
1012         g_free (markup);
1013 }
1014
1015 static void
1016 chat_window_new_message_cb (EmpathyChat       *chat,
1017                             EmpathyMessage    *message,
1018                             EmpathyChatWindow *window)
1019 {
1020         EmpathyChatWindowPriv *priv;
1021         gboolean              has_focus;
1022         gboolean              needs_urgency;
1023         EmpathyContact        *sender;
1024
1025         priv = GET_PRIV (window);
1026
1027         has_focus = empathy_chat_window_has_focus (window);
1028
1029         /* - if we're the sender, we play the sound if it's specified in the
1030          *   preferences and we're not away.
1031          * - if we receive a message, we play the sound if it's specified in the
1032          *   preferences and the window does not have focus on the chat receiving
1033          *   the message.
1034          */
1035
1036         sender = empathy_message_get_sender (message);
1037
1038         if (empathy_contact_is_user (sender)) {
1039                 empathy_sound_play (GTK_WIDGET (priv->dialog),
1040                                     EMPATHY_SOUND_MESSAGE_OUTGOING);
1041         }
1042
1043         if (has_focus && priv->current_chat == chat) {
1044                 return;
1045         }
1046
1047         if (!g_list_find (priv->chats_new_msg, chat)) {
1048                 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1049                 chat_window_update_chat_tab (chat);
1050         }
1051
1052         /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1053          * If empathy_chat_get_remote_contact () returns NULL, that means it's
1054          * an unamed MUC (msn-like).
1055          * In case of a MUC, we set urgency only if the message contains our
1056          * alias. */
1057         if (empathy_chat_is_room (chat) ||
1058             empathy_chat_get_remote_contact (chat) == NULL) {
1059                 needs_urgency = empathy_message_should_highlight (message);
1060         } else {
1061                 needs_urgency = TRUE;
1062         }
1063
1064         if (needs_urgency) {
1065                 if (!has_focus) {
1066                         chat_window_set_urgency_hint (window, TRUE);
1067                         chat_window_set_highlight_room_tab_label (chat);
1068                 }
1069
1070                 empathy_sound_play (GTK_WIDGET (priv->dialog),
1071                     EMPATHY_SOUND_MESSAGE_INCOMING);
1072                 chat_window_show_or_update_notification (window, message, chat);
1073         }
1074 }
1075
1076 static GtkNotebook *
1077 chat_window_detach_hook (GtkNotebook *source,
1078                          GtkWidget   *page,
1079                          gint         x,
1080                          gint         y,
1081                          gpointer     user_data)
1082 {
1083         EmpathyChatWindowPriv *priv;
1084         EmpathyChatWindow     *window, *new_window;
1085         EmpathyChat           *chat;
1086
1087         chat = EMPATHY_CHAT (page);
1088         window = chat_window_find_chat (chat);
1089
1090         new_window = empathy_chat_window_new ();
1091         priv = GET_PRIV (new_window);
1092
1093         DEBUG ("Detach hook called");
1094
1095         empathy_chat_window_move_chat (window, new_window, chat);
1096
1097         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1098         gtk_widget_show (priv->dialog);
1099
1100         return NULL;
1101 }
1102
1103 static void
1104 chat_window_page_switched_cb (GtkNotebook      *notebook,
1105                               GtkNotebookPage  *page,
1106                               gint              page_num,
1107                               EmpathyChatWindow *window)
1108 {
1109         EmpathyChatWindowPriv *priv;
1110         EmpathyChat           *chat;
1111         GtkWidget            *child;
1112
1113         DEBUG ("Page switched");
1114
1115         priv = GET_PRIV (window);
1116
1117         child = gtk_notebook_get_nth_page (notebook, page_num);
1118         chat = EMPATHY_CHAT (child);
1119
1120         if (priv->page_added) {
1121                 priv->page_added = FALSE;
1122                 empathy_chat_scroll_down (chat);
1123         }
1124         else if (priv->current_chat == chat) {
1125                 return;
1126         }
1127
1128         priv->current_chat = chat;
1129         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1130
1131         chat_window_update_chat_tab (chat);
1132 }
1133
1134 static void
1135 chat_window_page_added_cb (GtkNotebook      *notebook,
1136                            GtkWidget        *child,
1137                            guint             page_num,
1138                            EmpathyChatWindow *window)
1139 {
1140         EmpathyChatWindowPriv *priv;
1141         EmpathyChat           *chat;
1142
1143         priv = GET_PRIV (window);
1144
1145         /* If we just received DND to the same window, we don't want
1146          * to do anything here like removing the tab and then readding
1147          * it, so we return here and in "page-added".
1148          */
1149         if (priv->dnd_same_window) {
1150                 DEBUG ("Page added (back to the same window)");
1151                 priv->dnd_same_window = FALSE;
1152                 return;
1153         }
1154
1155         DEBUG ("Page added");
1156
1157         /* Get chat object */
1158         chat = EMPATHY_CHAT (child);
1159
1160         /* Connect chat signals for this window */
1161         g_signal_connect (chat, "composing",
1162                           G_CALLBACK (chat_window_composing_cb),
1163                           window);
1164         g_signal_connect (chat, "new-message",
1165                           G_CALLBACK (chat_window_new_message_cb),
1166                           window);
1167
1168         /* Set flag so we know to perform some special operations on
1169          * switch page due to the new page being added.
1170          */
1171         priv->page_added = TRUE;
1172
1173         /* Get list of chats up to date */
1174         priv->chats = g_list_append (priv->chats, chat);
1175
1176         chat_window_update_chat_tab (chat);
1177 }
1178
1179 static void
1180 chat_window_page_removed_cb (GtkNotebook      *notebook,
1181                              GtkWidget        *child,
1182                              guint             page_num,
1183                              EmpathyChatWindow *window)
1184 {
1185         EmpathyChatWindowPriv *priv;
1186         EmpathyChat           *chat;
1187
1188         priv = GET_PRIV (window);
1189
1190         /* If we just received DND to the same window, we don't want
1191          * to do anything here like removing the tab and then readding
1192          * it, so we return here and in "page-added".
1193          */
1194         if (priv->dnd_same_window) {
1195                 DEBUG ("Page removed (and will be readded to same window)");
1196                 return;
1197         }
1198
1199         DEBUG ("Page removed");
1200
1201         /* Get chat object */
1202         chat = EMPATHY_CHAT (child);
1203
1204         /* Disconnect all signal handlers for this chat and this window */
1205         g_signal_handlers_disconnect_by_func (chat,
1206                                               G_CALLBACK (chat_window_composing_cb),
1207                                               window);
1208         g_signal_handlers_disconnect_by_func (chat,
1209                                               G_CALLBACK (chat_window_new_message_cb),
1210                                               window);
1211
1212         /* Keep list of chats up to date */
1213         priv->chats = g_list_remove (priv->chats, chat);
1214         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1215         priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1216
1217         if (priv->chats == NULL) {
1218                 g_object_unref (window);
1219         } else {
1220                 chat_window_update (window);
1221         }
1222 }
1223
1224 static gboolean
1225 chat_window_focus_in_event_cb (GtkWidget        *widget,
1226                                GdkEvent         *event,
1227                                EmpathyChatWindow *window)
1228 {
1229         EmpathyChatWindowPriv *priv;
1230
1231         DEBUG ("Focus in event, updating title");
1232
1233         priv = GET_PRIV (window);
1234
1235         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1236
1237         chat_window_set_urgency_hint (window, FALSE);
1238
1239         /* Update the title, since we now mark all unread messages as read. */
1240         chat_window_update_chat_tab (priv->current_chat);
1241
1242         return FALSE;
1243 }
1244
1245 static void
1246 chat_window_drag_data_received (GtkWidget        *widget,
1247                                 GdkDragContext   *context,
1248                                 int               x,
1249                                 int               y,
1250                                 GtkSelectionData *selection,
1251                                 guint             info,
1252                                 guint             time,
1253                                 EmpathyChatWindow *window)
1254 {
1255         if (info == DND_DRAG_TYPE_CONTACT_ID) {
1256                 EmpathyChat           *chat;
1257                 EmpathyChatWindow     *old_window;
1258                 EmpathyAccount        *account;
1259                 EmpathyAccountManager *account_manager;
1260                 const gchar           *id;
1261                 gchar                **strv;
1262                 const gchar           *account_id;
1263                 const gchar           *contact_id;
1264
1265                 id = (const gchar*) gtk_selection_data_get_data (selection);
1266                 account_manager = empathy_account_manager_dup_singleton ();
1267
1268                 DEBUG ("DND contact from roster with id:'%s'", id);
1269
1270                 strv = g_strsplit (id, "/", 2);
1271                 account_id = strv[0];
1272                 contact_id = strv[1];
1273                 account = empathy_account_manager_lookup (account_manager, account_id);
1274                 chat = empathy_chat_window_find_chat (account, contact_id);
1275
1276                 if (!chat) {
1277                         TpConnection *connection;
1278
1279                         connection = empathy_account_get_connection (account);
1280
1281                         if (connection) {
1282                                 empathy_dispatcher_chat_with_contact_id (
1283                                         connection, contact_id, NULL, NULL);
1284                         }
1285
1286                         g_object_unref (account);
1287                         g_strfreev (strv);
1288                         return;
1289                 }
1290                 g_object_unref (account);
1291                 g_object_unref (account_manager);
1292                 g_strfreev (strv);
1293
1294                 old_window = chat_window_find_chat (chat);
1295                 if (old_window) {
1296                         if (old_window == window) {
1297                                 gtk_drag_finish (context, TRUE, FALSE, time);
1298                                 return;
1299                         }
1300
1301                         empathy_chat_window_move_chat (old_window, window, chat);
1302                 } else {
1303                         empathy_chat_window_add_chat (window, chat);
1304                 }
1305
1306                 /* Added to take care of any outstanding chat events */
1307                 empathy_chat_window_present_chat (chat);
1308
1309                 /* We should return TRUE to remove the data when doing
1310                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1311                  * weird consequences, and we handle that internally
1312                  * anyway with add_chat () and remove_chat ().
1313                  */
1314                 gtk_drag_finish (context, TRUE, FALSE, time);
1315         }
1316         else if (info == DND_DRAG_TYPE_TAB) {
1317                 EmpathyChat        **chat;
1318                 EmpathyChatWindow   *old_window = NULL;
1319
1320                 DEBUG ("DND tab");
1321
1322                 chat = (void *) gtk_selection_data_get_data (selection);
1323                 old_window = chat_window_find_chat (*chat);
1324
1325                 if (old_window) {
1326                         EmpathyChatWindowPriv *priv;
1327
1328                         priv = GET_PRIV (window);
1329
1330                         if (old_window == window) {
1331                                 DEBUG ("DND tab (within same window)");
1332                                 priv->dnd_same_window = TRUE;
1333                                 gtk_drag_finish (context, TRUE, FALSE, time);
1334                                 return;
1335                         }
1336
1337                         priv->dnd_same_window = FALSE;
1338                 }
1339
1340                 /* We should return TRUE to remove the data when doing
1341                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1342                  * weird consequences, and we handle that internally
1343                  * anyway with add_chat () and remove_chat ().
1344                  */
1345                 gtk_drag_finish (context, TRUE, FALSE, time);
1346         } else {
1347                 DEBUG ("DND from unknown source");
1348                 gtk_drag_finish (context, FALSE, FALSE, time);
1349         }
1350 }
1351
1352 static void
1353 chat_window_finalize (GObject *object)
1354 {
1355         EmpathyChatWindow     *window;
1356         EmpathyChatWindowPriv *priv;
1357
1358         window = EMPATHY_CHAT_WINDOW (object);
1359         priv = GET_PRIV (window);
1360
1361         DEBUG ("Finalized: %p", object);
1362
1363         g_object_unref (priv->ui_manager);
1364         g_object_unref (priv->chatroom_manager);
1365         if (priv->save_geometry_id != 0) {
1366                 g_source_remove (priv->save_geometry_id);
1367         }
1368
1369         if (priv->notification != NULL) {
1370                 notify_notification_close (priv->notification, NULL);
1371                 g_object_unref (priv->notification);
1372                 priv->notification = NULL;
1373         }
1374
1375         chat_windows = g_list_remove (chat_windows, window);
1376         gtk_widget_destroy (priv->dialog);
1377
1378         G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1379 }
1380
1381 static void
1382 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1383 {
1384         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1385
1386         object_class->finalize = chat_window_finalize;
1387
1388         g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1389
1390         /* Set up a style for the close button with no focus padding. */
1391         gtk_rc_parse_string (
1392                 "style \"empathy-close-button-style\"\n"
1393                 "{\n"
1394                 "  GtkWidget::focus-padding = 0\n"
1395                 "  xthickness = 0\n"
1396                 "  ythickness = 0\n"
1397                 "}\n"
1398                 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1399
1400         gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1401 }
1402
1403 static void
1404 empathy_chat_window_init (EmpathyChatWindow *window)
1405 {
1406         GtkBuilder            *gui;
1407         GtkAccelGroup         *accel_group;
1408         GClosure              *closure;
1409         GtkWidget             *menu;
1410         GtkWidget             *submenu;
1411         gint                   i;
1412         GtkWidget             *chat_vbox;
1413         gchar                 *filename;
1414         EmpathySmileyManager  *smiley_manager;
1415         EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1416                 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1417
1418         window->priv = priv;
1419         filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1420         gui = empathy_builder_get_file (filename,
1421                                        "chat_window", &priv->dialog,
1422                                        "chat_vbox", &chat_vbox,
1423                                        "ui_manager", &priv->ui_manager,
1424                                        "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1425                                        "menu_conv_favorite", &priv->menu_conv_favorite,
1426                                        "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1427                                        "menu_edit_cut", &priv->menu_edit_cut,
1428                                        "menu_edit_copy", &priv->menu_edit_copy,
1429                                        "menu_edit_paste", &priv->menu_edit_paste,
1430                                        "menu_tabs_next", &priv->menu_tabs_next,
1431                                        "menu_tabs_prev", &priv->menu_tabs_prev,
1432                                        "menu_tabs_left", &priv->menu_tabs_left,
1433                                        "menu_tabs_right", &priv->menu_tabs_right,
1434                                        "menu_tabs_detach", &priv->menu_tabs_detach,
1435                                        NULL);
1436         g_free (filename);
1437
1438         empathy_builder_connect (gui, window,
1439                               "chat_window", "configure-event", chat_window_configure_event_cb,
1440                               "menu_conv", "activate", chat_window_conv_activate_cb,
1441                               "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1442                               "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1443                               "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1444                               "menu_conv_close", "activate", chat_window_close_activate_cb,
1445                               "menu_edit", "activate", chat_window_edit_activate_cb,
1446                               "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1447                               "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1448                               "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1449                               "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1450                               "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1451                               "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1452                               "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1453                               "menu_help_about", "activate", chat_window_help_about_activate_cb,
1454                               NULL);
1455
1456         g_object_ref (priv->ui_manager);
1457         g_object_unref (gui);
1458
1459         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1460
1461         priv->notebook = gtk_notebook_new ();
1462         gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1463         gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1464         gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1465         gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1466         gtk_widget_show (priv->notebook);
1467
1468         /* Set up accels */
1469         accel_group = gtk_accel_group_new ();
1470         gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1471
1472         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1473                 closure =  g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1474                                            window,
1475                                            NULL);
1476                 gtk_accel_group_connect (accel_group,
1477                                          tab_accel_keys[i],
1478                                          GDK_MOD1_MASK,
1479                                          0,
1480                                          closure);
1481         }
1482
1483         g_object_unref (accel_group);
1484
1485         /* Set up smiley menu */
1486         smiley_manager = empathy_smiley_manager_dup_singleton ();
1487         submenu = empathy_smiley_menu_new (smiley_manager,
1488                                            chat_window_insert_smiley_activate_cb,
1489                                            window);
1490         menu = gtk_ui_manager_get_widget (priv->ui_manager,
1491                 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1492         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1493         g_object_unref (smiley_manager);
1494
1495         /* Set up signals we can't do with ui file since we may need to
1496          * block/unblock them at some later stage.
1497          */
1498
1499         g_signal_connect (priv->dialog,
1500                           "delete_event",
1501                           G_CALLBACK (chat_window_delete_event_cb),
1502                           window);
1503
1504         g_signal_connect_swapped (priv->menu_tabs_prev,
1505                                   "activate",
1506                                   G_CALLBACK (gtk_notebook_prev_page),
1507                                   priv->notebook);
1508         g_signal_connect_swapped (priv->menu_tabs_next,
1509                                   "activate",
1510                                   G_CALLBACK (gtk_notebook_next_page),
1511                                   priv->notebook);
1512
1513         g_signal_connect (priv->dialog,
1514                           "focus_in_event",
1515                           G_CALLBACK (chat_window_focus_in_event_cb),
1516                           window);
1517         g_signal_connect_after (priv->notebook,
1518                                 "switch_page",
1519                                 G_CALLBACK (chat_window_page_switched_cb),
1520                                 window);
1521         g_signal_connect (priv->notebook,
1522                           "page_added",
1523                           G_CALLBACK (chat_window_page_added_cb),
1524                           window);
1525         g_signal_connect (priv->notebook,
1526                           "page_removed",
1527                           G_CALLBACK (chat_window_page_removed_cb),
1528                           window);
1529
1530         /* Set up drag and drop */
1531         gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1532                            GTK_DEST_DEFAULT_ALL,
1533                            drag_types_dest,
1534                            G_N_ELEMENTS (drag_types_dest),
1535                            GDK_ACTION_MOVE);
1536
1537         g_signal_connect (priv->notebook,
1538                           "drag-data-received",
1539                           G_CALLBACK (chat_window_drag_data_received),
1540                           window);
1541
1542         chat_windows = g_list_prepend (chat_windows, window);
1543
1544         /* Set up private details */
1545         priv->chats = NULL;
1546         priv->chats_new_msg = NULL;
1547         priv->chats_composing = NULL;
1548         priv->current_chat = NULL;
1549 }
1550
1551 EmpathyChatWindow *
1552 empathy_chat_window_new (void)
1553 {
1554         return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1555 }
1556
1557 /* Returns the window to open a new tab in if there is only one window
1558  * visble, otherwise, returns NULL indicating that a new window should
1559  * be added.
1560  */
1561 EmpathyChatWindow *
1562 empathy_chat_window_get_default (void)
1563 {
1564         GList    *l;
1565         gboolean  separate_windows = TRUE;
1566
1567         empathy_conf_get_bool (empathy_conf_get (),
1568                               EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1569                               &separate_windows);
1570
1571         if (separate_windows) {
1572                 /* Always create a new window */
1573                 return NULL;
1574         }
1575
1576         for (l = chat_windows; l; l = l->next) {
1577                 EmpathyChatWindow *chat_window;
1578                 GtkWidget         *dialog;
1579
1580                 chat_window = l->data;
1581
1582                 dialog = empathy_chat_window_get_dialog (chat_window);
1583                 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1584                         /* Found a visible window on this desktop */
1585                         return chat_window;
1586                 }
1587         }
1588
1589         return NULL;
1590 }
1591
1592 GtkWidget *
1593 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1594 {
1595         EmpathyChatWindowPriv *priv;
1596
1597         g_return_val_if_fail (window != NULL, NULL);
1598
1599         priv = GET_PRIV (window);
1600
1601         return priv->dialog;
1602 }
1603
1604 void
1605 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1606                               EmpathyChat       *chat)
1607 {
1608         EmpathyChatWindowPriv *priv;
1609         GtkWidget             *label;
1610         GtkWidget             *popup_label;
1611         GtkWidget             *child;
1612         gint                   x, y, w, h;
1613
1614         g_return_if_fail (window != NULL);
1615         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1616
1617         priv = GET_PRIV (window);
1618
1619         /* Reference the chat object */
1620         g_object_ref (chat);
1621
1622         /* If this window has just been created, position it */
1623         if (priv->chats == NULL) {
1624                 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1625
1626                 if (x >= 0 && y >= 0) {
1627                         /* Let the window manager position it if we don't have
1628                          * good x, y coordinates.
1629                          */
1630                         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1631                 }
1632
1633                 if (w > 0 && h > 0) {
1634                         /* Use the defaults from the ui file if we don't have
1635                          * good w, h geometry.
1636                          */
1637                         gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1638                 }
1639         }
1640
1641         child = GTK_WIDGET (chat);
1642         label = chat_window_create_label (window, chat, TRUE);
1643         popup_label = chat_window_create_label (window, chat, FALSE);
1644         gtk_widget_show (child);
1645
1646         g_signal_connect (chat, "notify::name",
1647                           G_CALLBACK (chat_window_chat_notify_cb),
1648                           NULL);
1649         g_signal_connect (chat, "notify::subject",
1650                           G_CALLBACK (chat_window_chat_notify_cb),
1651                           NULL);
1652         g_signal_connect (chat, "notify::remote-contact",
1653                           G_CALLBACK (chat_window_chat_notify_cb),
1654                           NULL);
1655         chat_window_chat_notify_cb (chat);
1656
1657         gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1658         gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1659         gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1660         gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1661                                             TRUE, TRUE, GTK_PACK_START);
1662
1663         DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1664 }
1665
1666 void
1667 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1668                                  EmpathyChat       *chat)
1669 {
1670         EmpathyChatWindowPriv *priv;
1671         gint                   position;
1672         EmpathyContact        *remote_contact;
1673
1674         g_return_if_fail (window != NULL);
1675         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1676
1677         priv = GET_PRIV (window);
1678
1679         g_signal_handlers_disconnect_by_func (chat,
1680                                               chat_window_chat_notify_cb,
1681                                               NULL);
1682         remote_contact = g_object_get_data (G_OBJECT (chat),
1683                                             "chat-window-remote-contact");
1684         if (remote_contact) {
1685                 g_signal_handlers_disconnect_by_func (remote_contact,
1686                                                       chat_window_update_chat_tab,
1687                                                       chat);
1688         }
1689
1690         position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1691                                           GTK_WIDGET (chat));
1692         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1693
1694         DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1695
1696         g_object_unref (chat);
1697 }
1698
1699 void
1700 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1701                                EmpathyChatWindow *new_window,
1702                                EmpathyChat       *chat)
1703 {
1704         GtkWidget *widget;
1705
1706         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1707         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1708         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1709
1710         widget = GTK_WIDGET (chat);
1711
1712         DEBUG ("Chat moving with widget:%p (%d references)", widget,
1713                 G_OBJECT (widget)->ref_count);
1714
1715         /* We reference here to make sure we don't loose the widget
1716          * and the EmpathyChat object during the move.
1717          */
1718         g_object_ref (chat);
1719         g_object_ref (widget);
1720
1721         empathy_chat_window_remove_chat (old_window, chat);
1722         empathy_chat_window_add_chat (new_window, chat);
1723
1724         g_object_unref (widget);
1725         g_object_unref (chat);
1726 }
1727
1728 void
1729 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1730                                     EmpathyChat       *chat)
1731 {
1732         EmpathyChatWindowPriv *priv;
1733         gint                  page_num;
1734
1735         g_return_if_fail (window != NULL);
1736         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1737
1738         priv = GET_PRIV (window);
1739
1740         page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1741                                           GTK_WIDGET (chat));
1742         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1743                                        page_num);
1744 }
1745
1746 gboolean
1747 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1748 {
1749         EmpathyChatWindowPriv *priv;
1750         gboolean              has_focus;
1751
1752         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1753
1754         priv = GET_PRIV (window);
1755
1756         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1757
1758         return has_focus;
1759 }
1760
1761 EmpathyChat *
1762 empathy_chat_window_find_chat (EmpathyAccount   *account,
1763                                const gchar *id)
1764 {
1765         GList *l;
1766
1767         g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1768
1769         for (l = chat_windows; l; l = l->next) {
1770                 EmpathyChatWindowPriv *priv;
1771                 EmpathyChatWindow     *window;
1772                 GList                *ll;
1773
1774                 window = l->data;
1775                 priv = GET_PRIV (window);
1776
1777                 for (ll = priv->chats; ll; ll = ll->next) {
1778                         EmpathyChat *chat;
1779
1780                         chat = ll->data;
1781
1782                         if (empathy_account_equal (account, empathy_chat_get_account (chat)) &&
1783                             !tp_strdiff (id, empathy_chat_get_id (chat))) {
1784                                 return chat;
1785                         }
1786                 }
1787         }
1788
1789         return NULL;
1790 }
1791
1792 void
1793 empathy_chat_window_present_chat (EmpathyChat *chat)
1794 {
1795         EmpathyChatWindow     *window;
1796         EmpathyChatWindowPriv *priv;
1797
1798         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1799
1800         window = chat_window_find_chat (chat);
1801
1802         /* If the chat has no window, create one */
1803         if (window == NULL) {
1804                 window = empathy_chat_window_get_default ();
1805                 if (!window) {
1806                         window = empathy_chat_window_new ();
1807                 }
1808
1809                 empathy_chat_window_add_chat (window, chat);
1810         }
1811
1812         priv = GET_PRIV (window);
1813         empathy_chat_window_switch_to_chat (window, chat);
1814         empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1815
1816         gtk_widget_grab_focus (chat->input_text_view);
1817 }
1818