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