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