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