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