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