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