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