]> git.0d.be Git - empathy.git/blob - src/empathy-chat-window.c
Merge branch 'fix-585882'
[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_new_message_cb (EmpathyChat       *chat,
999                             EmpathyMessage    *message,
1000                             EmpathyChatWindow *window)
1001 {
1002         EmpathyChatWindowPriv *priv;
1003         gboolean              has_focus;
1004         gboolean              needs_urgency;
1005         EmpathyContact        *sender;
1006
1007         priv = GET_PRIV (window);
1008
1009         has_focus = empathy_chat_window_has_focus (window);
1010
1011         /* - if we're the sender, we play the sound if it's specified in the
1012          *   preferences and we're not away.
1013          * - if we receive a message, we play the sound if it's specified in the
1014          *   preferences and the window does not have focus on the chat receiving
1015          *   the message.
1016          */
1017
1018         sender = empathy_message_get_sender (message);
1019
1020         if (empathy_contact_is_user (sender)) {
1021                 empathy_sound_play (GTK_WIDGET (priv->dialog),
1022                                     EMPATHY_SOUND_MESSAGE_OUTGOING);
1023         }
1024
1025         if (has_focus && priv->current_chat == chat) {
1026                 return;
1027         }
1028
1029         /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1030          * If empathy_chat_get_remote_contact () returns NULL, that means it's
1031          * an unamed MUC (msn-like).
1032          * In case of a MUC, we set urgency only if the message contains our
1033          * alias. */
1034         if (empathy_chat_is_room (chat) ||
1035             empathy_chat_get_remote_contact (chat) == NULL) {
1036                 needs_urgency = empathy_message_should_highlight (message);
1037         } else {
1038                 needs_urgency = TRUE;
1039         }
1040
1041         if (needs_urgency) {
1042                 if (!has_focus)
1043                         chat_window_set_urgency_hint (window, TRUE);
1044
1045                 empathy_sound_play (GTK_WIDGET (priv->dialog),
1046                     EMPATHY_SOUND_MESSAGE_INCOMING);
1047                 chat_window_show_or_update_notification (window, message, chat);
1048         }
1049
1050         if (!g_list_find (priv->chats_new_msg, chat)) {
1051                 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1052                 chat_window_update_chat_tab (chat);
1053         }
1054 }
1055
1056 static GtkNotebook *
1057 chat_window_detach_hook (GtkNotebook *source,
1058                          GtkWidget   *page,
1059                          gint         x,
1060                          gint         y,
1061                          gpointer     user_data)
1062 {
1063         EmpathyChatWindowPriv *priv;
1064         EmpathyChatWindow     *window, *new_window;
1065         EmpathyChat           *chat;
1066
1067         chat = EMPATHY_CHAT (page);
1068         window = chat_window_find_chat (chat);
1069
1070         new_window = empathy_chat_window_new ();
1071         priv = GET_PRIV (new_window);
1072
1073         DEBUG ("Detach hook called");
1074
1075         empathy_chat_window_move_chat (window, new_window, chat);
1076
1077         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1078         gtk_widget_show (priv->dialog);
1079
1080         return NULL;
1081 }
1082
1083 static void
1084 chat_window_page_switched_cb (GtkNotebook      *notebook,
1085                               GtkNotebookPage  *page,
1086                               gint              page_num,
1087                               EmpathyChatWindow *window)
1088 {
1089         EmpathyChatWindowPriv *priv;
1090         EmpathyChat           *chat;
1091         GtkWidget            *child;
1092
1093         DEBUG ("Page switched");
1094
1095         priv = GET_PRIV (window);
1096
1097         child = gtk_notebook_get_nth_page (notebook, page_num);
1098         chat = EMPATHY_CHAT (child);
1099
1100         if (priv->page_added) {
1101                 priv->page_added = FALSE;
1102                 empathy_chat_scroll_down (chat);
1103         }
1104         else if (priv->current_chat == chat) {
1105                 return;
1106         }
1107
1108         priv->current_chat = chat;
1109         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1110
1111         chat_window_update_chat_tab (chat);
1112 }
1113
1114 static void
1115 chat_window_page_added_cb (GtkNotebook      *notebook,
1116                            GtkWidget        *child,
1117                            guint             page_num,
1118                            EmpathyChatWindow *window)
1119 {
1120         EmpathyChatWindowPriv *priv;
1121         EmpathyChat           *chat;
1122
1123         priv = GET_PRIV (window);
1124
1125         /* If we just received DND to the same window, we don't want
1126          * to do anything here like removing the tab and then readding
1127          * it, so we return here and in "page-added".
1128          */
1129         if (priv->dnd_same_window) {
1130                 DEBUG ("Page added (back to the same window)");
1131                 priv->dnd_same_window = FALSE;
1132                 return;
1133         }
1134
1135         DEBUG ("Page added");
1136
1137         /* Get chat object */
1138         chat = EMPATHY_CHAT (child);
1139
1140         /* Connect chat signals for this window */
1141         g_signal_connect (chat, "composing",
1142                           G_CALLBACK (chat_window_composing_cb),
1143                           window);
1144         g_signal_connect (chat, "new-message",
1145                           G_CALLBACK (chat_window_new_message_cb),
1146                           window);
1147
1148         /* Set flag so we know to perform some special operations on
1149          * switch page due to the new page being added.
1150          */
1151         priv->page_added = TRUE;
1152
1153         /* Get list of chats up to date */
1154         priv->chats = g_list_append (priv->chats, chat);
1155
1156         chat_window_update_chat_tab (chat);
1157 }
1158
1159 static void
1160 chat_window_page_removed_cb (GtkNotebook      *notebook,
1161                              GtkWidget        *child,
1162                              guint             page_num,
1163                              EmpathyChatWindow *window)
1164 {
1165         EmpathyChatWindowPriv *priv;
1166         EmpathyChat           *chat;
1167
1168         priv = GET_PRIV (window);
1169
1170         /* If we just received DND to the same window, we don't want
1171          * to do anything here like removing the tab and then readding
1172          * it, so we return here and in "page-added".
1173          */
1174         if (priv->dnd_same_window) {
1175                 DEBUG ("Page removed (and will be readded to same window)");
1176                 return;
1177         }
1178
1179         DEBUG ("Page removed");
1180
1181         /* Get chat object */
1182         chat = EMPATHY_CHAT (child);
1183
1184         /* Disconnect all signal handlers for this chat and this window */
1185         g_signal_handlers_disconnect_by_func (chat,
1186                                               G_CALLBACK (chat_window_composing_cb),
1187                                               window);
1188         g_signal_handlers_disconnect_by_func (chat,
1189                                               G_CALLBACK (chat_window_new_message_cb),
1190                                               window);
1191
1192         /* Keep list of chats up to date */
1193         priv->chats = g_list_remove (priv->chats, chat);
1194         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1195         priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1196
1197         if (priv->chats == NULL) {
1198                 g_object_unref (window);
1199         } else {
1200                 chat_window_update (window);
1201         }
1202 }
1203
1204 static gboolean
1205 chat_window_focus_in_event_cb (GtkWidget        *widget,
1206                                GdkEvent         *event,
1207                                EmpathyChatWindow *window)
1208 {
1209         EmpathyChatWindowPriv *priv;
1210
1211         DEBUG ("Focus in event, updating title");
1212
1213         priv = GET_PRIV (window);
1214
1215         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1216
1217         chat_window_set_urgency_hint (window, FALSE);
1218
1219         /* Update the title, since we now mark all unread messages as read. */
1220         chat_window_update_chat_tab (priv->current_chat);
1221
1222         return FALSE;
1223 }
1224
1225 static void
1226 chat_window_drag_data_received (GtkWidget        *widget,
1227                                 GdkDragContext   *context,
1228                                 int               x,
1229                                 int               y,
1230                                 GtkSelectionData *selection,
1231                                 guint             info,
1232                                 guint             time,
1233                                 EmpathyChatWindow *window)
1234 {
1235         if (info == DND_DRAG_TYPE_CONTACT_ID) {
1236                 EmpathyChat           *chat;
1237                 EmpathyChatWindow     *old_window;
1238                 EmpathyAccount        *account;
1239                 EmpathyAccountManager *account_manager;
1240                 const gchar           *id;
1241                 gchar                **strv;
1242                 const gchar           *account_id;
1243                 const gchar           *contact_id;
1244
1245                 id = (const gchar*) gtk_selection_data_get_data (selection);
1246                 account_manager = empathy_account_manager_dup_singleton ();
1247
1248                 DEBUG ("DND contact from roster with id:'%s'", id);
1249
1250                 strv = g_strsplit (id, "/", 2);
1251                 account_id = strv[0];
1252                 contact_id = strv[1];
1253                 account = empathy_account_manager_lookup (account_manager, account_id);
1254                 chat = empathy_chat_window_find_chat (account, contact_id);
1255
1256                 if (!chat) {
1257                         TpConnection *connection;
1258
1259                         connection = empathy_account_get_connection (account);
1260
1261                         if (connection) {
1262                                 empathy_dispatcher_chat_with_contact_id (
1263                                         connection, contact_id, NULL, NULL);
1264                         }
1265
1266                         g_object_unref (account);
1267                         g_strfreev (strv);
1268                         return;
1269                 }
1270                 g_object_unref (account);
1271                 g_object_unref (account_manager);
1272                 g_strfreev (strv);
1273
1274                 old_window = chat_window_find_chat (chat);
1275                 if (old_window) {
1276                         if (old_window == window) {
1277                                 gtk_drag_finish (context, TRUE, FALSE, time);
1278                                 return;
1279                         }
1280
1281                         empathy_chat_window_move_chat (old_window, window, chat);
1282                 } else {
1283                         empathy_chat_window_add_chat (window, chat);
1284                 }
1285
1286                 /* Added to take care of any outstanding chat events */
1287                 empathy_chat_window_present_chat (chat);
1288
1289                 /* We should return TRUE to remove the data when doing
1290                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1291                  * weird consequences, and we handle that internally
1292                  * anyway with add_chat () and remove_chat ().
1293                  */
1294                 gtk_drag_finish (context, TRUE, FALSE, time);
1295         }
1296         else if (info == DND_DRAG_TYPE_TAB) {
1297                 EmpathyChat        **chat;
1298                 EmpathyChatWindow   *old_window = NULL;
1299
1300                 DEBUG ("DND tab");
1301
1302                 chat = (void *) gtk_selection_data_get_data (selection);
1303                 old_window = chat_window_find_chat (*chat);
1304
1305                 if (old_window) {
1306                         EmpathyChatWindowPriv *priv;
1307
1308                         priv = GET_PRIV (window);
1309
1310                         if (old_window == window) {
1311                                 DEBUG ("DND tab (within same window)");
1312                                 priv->dnd_same_window = TRUE;
1313                                 gtk_drag_finish (context, TRUE, FALSE, time);
1314                                 return;
1315                         }
1316
1317                         priv->dnd_same_window = FALSE;
1318                 }
1319
1320                 /* We should return TRUE to remove the data when doing
1321                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1322                  * weird consequences, and we handle that internally
1323                  * anyway with add_chat () and remove_chat ().
1324                  */
1325                 gtk_drag_finish (context, TRUE, FALSE, time);
1326         } else {
1327                 DEBUG ("DND from unknown source");
1328                 gtk_drag_finish (context, FALSE, FALSE, time);
1329         }
1330 }
1331
1332 static void
1333 chat_window_finalize (GObject *object)
1334 {
1335         EmpathyChatWindow     *window;
1336         EmpathyChatWindowPriv *priv;
1337
1338         window = EMPATHY_CHAT_WINDOW (object);
1339         priv = GET_PRIV (window);
1340
1341         DEBUG ("Finalized: %p", object);
1342
1343         g_object_unref (priv->ui_manager);
1344         g_object_unref (priv->chatroom_manager);
1345         if (priv->save_geometry_id != 0) {
1346                 g_source_remove (priv->save_geometry_id);
1347         }
1348
1349         if (priv->notification != NULL) {
1350                 notify_notification_close (priv->notification, NULL);
1351                 g_object_unref (priv->notification);
1352                 priv->notification = NULL;
1353         }
1354
1355         chat_windows = g_list_remove (chat_windows, window);
1356         gtk_widget_destroy (priv->dialog);
1357
1358         G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1359 }
1360
1361 static void
1362 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1363 {
1364         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1365
1366         object_class->finalize = chat_window_finalize;
1367
1368         g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1369
1370         /* Set up a style for the close button with no focus padding. */
1371         gtk_rc_parse_string (
1372                 "style \"empathy-close-button-style\"\n"
1373                 "{\n"
1374                 "  GtkWidget::focus-padding = 0\n"
1375                 "  xthickness = 0\n"
1376                 "  ythickness = 0\n"
1377                 "}\n"
1378                 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1379
1380         gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1381 }
1382
1383 static void
1384 empathy_chat_window_init (EmpathyChatWindow *window)
1385 {
1386         GtkBuilder            *gui;
1387         GtkAccelGroup         *accel_group;
1388         GClosure              *closure;
1389         GtkWidget             *menu;
1390         GtkWidget             *submenu;
1391         gint                   i;
1392         GtkWidget             *chat_vbox;
1393         gchar                 *filename;
1394         EmpathySmileyManager  *smiley_manager;
1395         EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1396                 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1397
1398         window->priv = priv;
1399         filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1400         gui = empathy_builder_get_file (filename,
1401                                        "chat_window", &priv->dialog,
1402                                        "chat_vbox", &chat_vbox,
1403                                        "ui_manager", &priv->ui_manager,
1404                                        "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1405                                        "menu_conv_favorite", &priv->menu_conv_favorite,
1406                                        "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1407                                        "menu_edit_cut", &priv->menu_edit_cut,
1408                                        "menu_edit_copy", &priv->menu_edit_copy,
1409                                        "menu_edit_paste", &priv->menu_edit_paste,
1410                                        "menu_tabs_next", &priv->menu_tabs_next,
1411                                        "menu_tabs_prev", &priv->menu_tabs_prev,
1412                                        "menu_tabs_left", &priv->menu_tabs_left,
1413                                        "menu_tabs_right", &priv->menu_tabs_right,
1414                                        "menu_tabs_detach", &priv->menu_tabs_detach,
1415                                        NULL);
1416         g_free (filename);
1417
1418         empathy_builder_connect (gui, window,
1419                               "chat_window", "configure-event", chat_window_configure_event_cb,
1420                               "menu_conv", "activate", chat_window_conv_activate_cb,
1421                               "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1422                               "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1423                               "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1424                               "menu_conv_close", "activate", chat_window_close_activate_cb,
1425                               "menu_edit", "activate", chat_window_edit_activate_cb,
1426                               "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1427                               "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1428                               "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1429                               "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1430                               "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1431                               "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1432                               "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1433                               "menu_help_about", "activate", chat_window_help_about_activate_cb,
1434                               NULL);
1435
1436         g_object_ref (priv->ui_manager);
1437         g_object_unref (gui);
1438
1439         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1440
1441         priv->notebook = gtk_notebook_new ();
1442         gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1443         gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1444         gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1445         gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1446         gtk_widget_show (priv->notebook);
1447
1448         /* Set up accels */
1449         accel_group = gtk_accel_group_new ();
1450         gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1451
1452         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1453                 closure =  g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1454                                            window,
1455                                            NULL);
1456                 gtk_accel_group_connect (accel_group,
1457                                          tab_accel_keys[i],
1458                                          GDK_MOD1_MASK,
1459                                          0,
1460                                          closure);
1461         }
1462
1463         g_object_unref (accel_group);
1464
1465         /* Set up smiley menu */
1466         smiley_manager = empathy_smiley_manager_dup_singleton ();
1467         submenu = empathy_smiley_menu_new (smiley_manager,
1468                                            chat_window_insert_smiley_activate_cb,
1469                                            window);
1470         menu = gtk_ui_manager_get_widget (priv->ui_manager,
1471                 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1472         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1473         g_object_unref (smiley_manager);
1474
1475         /* Set up signals we can't do with ui file since we may need to
1476          * block/unblock them at some later stage.
1477          */
1478
1479         g_signal_connect (priv->dialog,
1480                           "delete_event",
1481                           G_CALLBACK (chat_window_delete_event_cb),
1482                           window);
1483
1484         g_signal_connect_swapped (priv->menu_tabs_prev,
1485                                   "activate",
1486                                   G_CALLBACK (gtk_notebook_prev_page),
1487                                   priv->notebook);
1488         g_signal_connect_swapped (priv->menu_tabs_next,
1489                                   "activate",
1490                                   G_CALLBACK (gtk_notebook_next_page),
1491                                   priv->notebook);
1492
1493         g_signal_connect (priv->dialog,
1494                           "focus_in_event",
1495                           G_CALLBACK (chat_window_focus_in_event_cb),
1496                           window);
1497         g_signal_connect_after (priv->notebook,
1498                                 "switch_page",
1499                                 G_CALLBACK (chat_window_page_switched_cb),
1500                                 window);
1501         g_signal_connect (priv->notebook,
1502                           "page_added",
1503                           G_CALLBACK (chat_window_page_added_cb),
1504                           window);
1505         g_signal_connect (priv->notebook,
1506                           "page_removed",
1507                           G_CALLBACK (chat_window_page_removed_cb),
1508                           window);
1509
1510         /* Set up drag and drop */
1511         gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1512                            GTK_DEST_DEFAULT_ALL,
1513                            drag_types_dest,
1514                            G_N_ELEMENTS (drag_types_dest),
1515                            GDK_ACTION_MOVE);
1516
1517         g_signal_connect (priv->notebook,
1518                           "drag-data-received",
1519                           G_CALLBACK (chat_window_drag_data_received),
1520                           window);
1521
1522         chat_windows = g_list_prepend (chat_windows, window);
1523
1524         /* Set up private details */
1525         priv->chats = NULL;
1526         priv->chats_new_msg = NULL;
1527         priv->chats_composing = NULL;
1528         priv->current_chat = NULL;
1529 }
1530
1531 EmpathyChatWindow *
1532 empathy_chat_window_new (void)
1533 {
1534         return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1535 }
1536
1537 /* Returns the window to open a new tab in if there is only one window
1538  * visble, otherwise, returns NULL indicating that a new window should
1539  * be added.
1540  */
1541 EmpathyChatWindow *
1542 empathy_chat_window_get_default (void)
1543 {
1544         GList    *l;
1545         gboolean  separate_windows = TRUE;
1546
1547         empathy_conf_get_bool (empathy_conf_get (),
1548                               EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1549                               &separate_windows);
1550
1551         if (separate_windows) {
1552                 /* Always create a new window */
1553                 return NULL;
1554         }
1555
1556         for (l = chat_windows; l; l = l->next) {
1557                 EmpathyChatWindow *chat_window;
1558                 GtkWidget         *dialog;
1559
1560                 chat_window = l->data;
1561
1562                 dialog = empathy_chat_window_get_dialog (chat_window);
1563                 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1564                         /* Found a visible window on this desktop */
1565                         return chat_window;
1566                 }
1567         }
1568
1569         return NULL;
1570 }
1571
1572 GtkWidget *
1573 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1574 {
1575         EmpathyChatWindowPriv *priv;
1576
1577         g_return_val_if_fail (window != NULL, NULL);
1578
1579         priv = GET_PRIV (window);
1580
1581         return priv->dialog;
1582 }
1583
1584 void
1585 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1586                               EmpathyChat       *chat)
1587 {
1588         EmpathyChatWindowPriv *priv;
1589         GtkWidget             *label;
1590         GtkWidget             *popup_label;
1591         GtkWidget             *child;
1592         gint                   x, y, w, h;
1593
1594         g_return_if_fail (window != NULL);
1595         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1596
1597         priv = GET_PRIV (window);
1598
1599         /* Reference the chat object */
1600         g_object_ref (chat);
1601
1602         /* If this window has just been created, position it */
1603         if (priv->chats == NULL) {
1604                 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1605
1606                 if (x >= 0 && y >= 0) {
1607                         /* Let the window manager position it if we don't have
1608                          * good x, y coordinates.
1609                          */
1610                         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1611                 }
1612
1613                 if (w > 0 && h > 0) {
1614                         /* Use the defaults from the ui file if we don't have
1615                          * good w, h geometry.
1616                          */
1617                         gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1618                 }
1619         }
1620
1621         child = GTK_WIDGET (chat);
1622         label = chat_window_create_label (window, chat, TRUE);
1623         popup_label = chat_window_create_label (window, chat, FALSE);
1624         gtk_widget_show (child);
1625
1626         g_signal_connect (chat, "notify::name",
1627                           G_CALLBACK (chat_window_chat_notify_cb),
1628                           NULL);
1629         g_signal_connect (chat, "notify::subject",
1630                           G_CALLBACK (chat_window_chat_notify_cb),
1631                           NULL);
1632         g_signal_connect (chat, "notify::remote-contact",
1633                           G_CALLBACK (chat_window_chat_notify_cb),
1634                           NULL);
1635         chat_window_chat_notify_cb (chat);
1636
1637         gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1638         gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1639         gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1640         gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1641                                             TRUE, TRUE, GTK_PACK_START);
1642
1643         DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1644 }
1645
1646 void
1647 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1648                                  EmpathyChat       *chat)
1649 {
1650         EmpathyChatWindowPriv *priv;
1651         gint                   position;
1652         EmpathyContact        *remote_contact;
1653
1654         g_return_if_fail (window != NULL);
1655         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1656
1657         priv = GET_PRIV (window);
1658
1659         g_signal_handlers_disconnect_by_func (chat,
1660                                               chat_window_chat_notify_cb,
1661                                               NULL);
1662         remote_contact = g_object_get_data (G_OBJECT (chat),
1663                                             "chat-window-remote-contact");
1664         if (remote_contact) {
1665                 g_signal_handlers_disconnect_by_func (remote_contact,
1666                                                       chat_window_update_chat_tab,
1667                                                       chat);
1668         }
1669
1670         position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1671                                           GTK_WIDGET (chat));
1672         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1673
1674         DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1675
1676         g_object_unref (chat);
1677 }
1678
1679 void
1680 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1681                                EmpathyChatWindow *new_window,
1682                                EmpathyChat       *chat)
1683 {
1684         GtkWidget *widget;
1685
1686         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1687         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1688         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1689
1690         widget = GTK_WIDGET (chat);
1691
1692         DEBUG ("Chat moving with widget:%p (%d references)", widget,
1693                 G_OBJECT (widget)->ref_count);
1694
1695         /* We reference here to make sure we don't loose the widget
1696          * and the EmpathyChat object during the move.
1697          */
1698         g_object_ref (chat);
1699         g_object_ref (widget);
1700
1701         empathy_chat_window_remove_chat (old_window, chat);
1702         empathy_chat_window_add_chat (new_window, chat);
1703
1704         g_object_unref (widget);
1705         g_object_unref (chat);
1706 }
1707
1708 void
1709 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1710                                     EmpathyChat       *chat)
1711 {
1712         EmpathyChatWindowPriv *priv;
1713         gint                  page_num;
1714
1715         g_return_if_fail (window != NULL);
1716         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1717
1718         priv = GET_PRIV (window);
1719
1720         page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1721                                           GTK_WIDGET (chat));
1722         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1723                                        page_num);
1724 }
1725
1726 gboolean
1727 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1728 {
1729         EmpathyChatWindowPriv *priv;
1730         gboolean              has_focus;
1731
1732         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1733
1734         priv = GET_PRIV (window);
1735
1736         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1737
1738         return has_focus;
1739 }
1740
1741 EmpathyChat *
1742 empathy_chat_window_find_chat (EmpathyAccount   *account,
1743                                const gchar *id)
1744 {
1745         GList *l;
1746
1747         g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1748
1749         for (l = chat_windows; l; l = l->next) {
1750                 EmpathyChatWindowPriv *priv;
1751                 EmpathyChatWindow     *window;
1752                 GList                *ll;
1753
1754                 window = l->data;
1755                 priv = GET_PRIV (window);
1756
1757                 for (ll = priv->chats; ll; ll = ll->next) {
1758                         EmpathyChat *chat;
1759
1760                         chat = ll->data;
1761
1762                         if (empathy_account_equal (account, empathy_chat_get_account (chat)) &&
1763                             !tp_strdiff (id, empathy_chat_get_id (chat))) {
1764                                 return chat;
1765                         }
1766                 }
1767         }
1768
1769         return NULL;
1770 }
1771
1772 void
1773 empathy_chat_window_present_chat (EmpathyChat *chat)
1774 {
1775         EmpathyChatWindow     *window;
1776         EmpathyChatWindowPriv *priv;
1777
1778         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1779
1780         window = chat_window_find_chat (chat);
1781
1782         /* If the chat has no window, create one */
1783         if (window == NULL) {
1784                 window = empathy_chat_window_get_default ();
1785                 if (!window) {
1786                         window = empathy_chat_window_new ();
1787                 }
1788
1789                 empathy_chat_window_add_chat (window, chat);
1790         }
1791
1792         priv = GET_PRIV (window);
1793         empathy_chat_window_switch_to_chat (window, chat);
1794         empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1795
1796         gtk_widget_grab_focus (chat->input_text_view);
1797 }
1798