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