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