]> git.0d.be Git - empathy.git/blob - src/empathy-chat-window.c
Utilitiy function to send files from a URI list, for dnd implementations
[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
1442                 priv = GET_PRIV (window);
1443                 contact = empathy_chat_get_remote_contact (priv->current_chat);
1444
1445                 if (!EMPATHY_IS_CONTACT (contact)) {
1446                         gtk_drag_finish (context, TRUE, FALSE, time);
1447                         return;
1448                 }
1449
1450                 data = (const gchar *) gtk_selection_data_get_data (selection);
1451                 empathy_send_file_from_uri_list (contact, data);
1452
1453                 gtk_drag_finish (context, TRUE, FALSE, time);
1454         }
1455         else if (info == DND_DRAG_TYPE_TAB) {
1456                 EmpathyChat        **chat;
1457                 EmpathyChatWindow   *old_window = NULL;
1458
1459                 DEBUG ("DND tab");
1460
1461                 chat = (void *) gtk_selection_data_get_data (selection);
1462                 old_window = chat_window_find_chat (*chat);
1463
1464                 if (old_window) {
1465                         EmpathyChatWindowPriv *priv;
1466
1467                         priv = GET_PRIV (window);
1468
1469                         if (old_window == window) {
1470                                 DEBUG ("DND tab (within same window)");
1471                                 priv->dnd_same_window = TRUE;
1472                                 gtk_drag_finish (context, TRUE, FALSE, time_);
1473                                 return;
1474                         }
1475
1476                         priv->dnd_same_window = FALSE;
1477                 }
1478
1479                 /* We should return TRUE to remove the data when doing
1480                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1481                  * weird consequences, and we handle that internally
1482                  * anyway with add_chat () and remove_chat ().
1483                  */
1484                 gtk_drag_finish (context, TRUE, FALSE, time_);
1485         } else {
1486                 DEBUG ("DND from unknown source");
1487                 gtk_drag_finish (context, FALSE, FALSE, time_);
1488         }
1489 }
1490
1491 static void
1492 chat_window_finalize (GObject *object)
1493 {
1494         EmpathyChatWindow     *window;
1495         EmpathyChatWindowPriv *priv;
1496
1497         window = EMPATHY_CHAT_WINDOW (object);
1498         priv = GET_PRIV (window);
1499
1500         DEBUG ("Finalized: %p", object);
1501
1502         g_object_unref (priv->ui_manager);
1503         g_object_unref (priv->chatroom_manager);
1504         g_object_unref (priv->notify_mgr);
1505
1506         if (priv->notification != NULL) {
1507                 notify_notification_close (priv->notification, NULL);
1508                 g_object_unref (priv->notification);
1509                 priv->notification = NULL;
1510                 if (priv->notification_data != NULL)
1511                         {
1512                                 free_notification_data (priv->notification_data);
1513                                 priv->notification_data = NULL;
1514                         }
1515         }
1516
1517         chat_windows = g_list_remove (chat_windows, window);
1518         gtk_widget_destroy (priv->dialog);
1519
1520         G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1521 }
1522
1523 static void
1524 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1525 {
1526         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1527
1528         object_class->finalize = chat_window_finalize;
1529
1530         g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1531
1532         /* Set up a style for the close button with no focus padding. */
1533         gtk_rc_parse_string (
1534                 "style \"empathy-close-button-style\"\n"
1535                 "{\n"
1536                 "  GtkWidget::focus-padding = 0\n"
1537                 "  xthickness = 0\n"
1538                 "  ythickness = 0\n"
1539                 "}\n"
1540                 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1541
1542         gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1543 }
1544
1545 static void
1546 empathy_chat_window_init (EmpathyChatWindow *window)
1547 {
1548         GtkBuilder            *gui;
1549         GtkAccelGroup         *accel_group;
1550         GClosure              *closure;
1551         GtkWidget             *menu;
1552         GtkWidget             *submenu;
1553         guint                  i;
1554         GtkWidget             *chat_vbox;
1555         gchar                 *filename;
1556         EmpathySmileyManager  *smiley_manager;
1557         EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1558                 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1559
1560         window->priv = priv;
1561         filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1562         gui = empathy_builder_get_file (filename,
1563                                        "chat_window", &priv->dialog,
1564                                        "chat_vbox", &chat_vbox,
1565                                        "ui_manager", &priv->ui_manager,
1566                                        "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1567                                        "menu_conv_favorite", &priv->menu_conv_favorite,
1568                                        "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1569                                        "menu_edit_cut", &priv->menu_edit_cut,
1570                                        "menu_edit_copy", &priv->menu_edit_copy,
1571                                        "menu_edit_paste", &priv->menu_edit_paste,
1572                                        "menu_tabs_next", &priv->menu_tabs_next,
1573                                        "menu_tabs_prev", &priv->menu_tabs_prev,
1574                                        "menu_tabs_left", &priv->menu_tabs_left,
1575                                        "menu_tabs_right", &priv->menu_tabs_right,
1576                                        "menu_tabs_detach", &priv->menu_tabs_detach,
1577                                        NULL);
1578         g_free (filename);
1579
1580         empathy_builder_connect (gui, window,
1581                               "menu_conv", "activate", chat_window_conv_activate_cb,
1582                               "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1583                               "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1584                               "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1585                               "menu_conv_close", "activate", chat_window_close_activate_cb,
1586                               "menu_edit", "activate", chat_window_edit_activate_cb,
1587                               "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1588                               "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1589                               "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1590                               "menu_tabs_next", "activate", chat_window_tabs_next_activate_cb,
1591                               "menu_tabs_prev", "activate", chat_window_tabs_previous_activate_cb,
1592                               "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1593                               "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1594                               "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1595                               "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1596                               "menu_help_about", "activate", chat_window_help_about_activate_cb,
1597                               NULL);
1598
1599         g_object_ref (priv->ui_manager);
1600         g_object_unref (gui);
1601
1602         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1603
1604         priv->notebook = gtk_notebook_new ();
1605         gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1606         gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1607         gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1608         gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1609         gtk_widget_show (priv->notebook);
1610
1611         /* Set up accels */
1612         accel_group = gtk_accel_group_new ();
1613         gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1614
1615         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1616                 closure =  g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1617                                            window,
1618                                            NULL);
1619                 gtk_accel_group_connect (accel_group,
1620                                          tab_accel_keys[i],
1621                                          GDK_MOD1_MASK,
1622                                          0,
1623                                          closure);
1624         }
1625
1626         g_object_unref (accel_group);
1627
1628         /* Set up smiley menu */
1629         smiley_manager = empathy_smiley_manager_dup_singleton ();
1630         submenu = empathy_smiley_menu_new (smiley_manager,
1631                                            chat_window_insert_smiley_activate_cb,
1632                                            window);
1633         menu = gtk_ui_manager_get_widget (priv->ui_manager,
1634                 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1635         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1636         g_object_unref (smiley_manager);
1637
1638         /* Set up signals we can't do with ui file since we may need to
1639          * block/unblock them at some later stage.
1640          */
1641
1642         g_signal_connect (priv->dialog,
1643                           "delete_event",
1644                           G_CALLBACK (chat_window_delete_event_cb),
1645                           window);
1646         g_signal_connect (priv->dialog,
1647                           "focus_in_event",
1648                           G_CALLBACK (chat_window_focus_in_event_cb),
1649                           window);
1650         g_signal_connect_after (priv->notebook,
1651                                 "switch_page",
1652                                 G_CALLBACK (chat_window_page_switched_cb),
1653                                 window);
1654         g_signal_connect (priv->notebook,
1655                           "page_added",
1656                           G_CALLBACK (chat_window_page_added_cb),
1657                           window);
1658         g_signal_connect (priv->notebook,
1659                           "page_removed",
1660                           G_CALLBACK (chat_window_page_removed_cb),
1661                           window);
1662
1663         /* Set up drag and drop */
1664         gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1665                            GTK_DEST_DEFAULT_ALL,
1666                            drag_types_dest,
1667                            G_N_ELEMENTS (drag_types_dest),
1668                            GDK_ACTION_MOVE | GDK_ACTION_COPY);
1669
1670         g_signal_connect (priv->notebook,
1671                           "drag-motion",
1672                           G_CALLBACK (chat_window_drag_motion),
1673                           window);
1674         g_signal_connect (priv->notebook,
1675                           "drag-data-received",
1676                           G_CALLBACK (chat_window_drag_data_received),
1677                           window);
1678
1679         chat_windows = g_list_prepend (chat_windows, window);
1680
1681         /* Set up private details */
1682         priv->chats = NULL;
1683         priv->chats_new_msg = NULL;
1684         priv->chats_composing = NULL;
1685         priv->current_chat = NULL;
1686
1687         priv->notify_mgr = empathy_notify_manager_dup_singleton ();
1688 }
1689
1690 EmpathyChatWindow *
1691 empathy_chat_window_new (void)
1692 {
1693         return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1694 }
1695
1696 /* Returns the window to open a new tab in if there is only one window
1697  * visble, otherwise, returns NULL indicating that a new window should
1698  * be added.
1699  */
1700 EmpathyChatWindow *
1701 empathy_chat_window_get_default (void)
1702 {
1703         GList    *l;
1704         gboolean  separate_windows = TRUE;
1705
1706         empathy_conf_get_bool (empathy_conf_get (),
1707                               EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1708                               &separate_windows);
1709
1710         if (separate_windows) {
1711                 /* Always create a new window */
1712                 return NULL;
1713         }
1714
1715         for (l = chat_windows; l; l = l->next) {
1716                 EmpathyChatWindow *chat_window;
1717                 GtkWidget         *dialog;
1718
1719                 chat_window = l->data;
1720
1721                 dialog = empathy_chat_window_get_dialog (chat_window);
1722                 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1723                         /* Found a visible window on this desktop */
1724                         return chat_window;
1725                 }
1726         }
1727
1728         return NULL;
1729 }
1730
1731 GtkWidget *
1732 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1733 {
1734         EmpathyChatWindowPriv *priv;
1735
1736         g_return_val_if_fail (window != NULL, NULL);
1737
1738         priv = GET_PRIV (window);
1739
1740         return priv->dialog;
1741 }
1742
1743 void
1744 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1745                               EmpathyChat       *chat)
1746 {
1747         EmpathyChatWindowPriv *priv;
1748         GtkWidget             *label;
1749         GtkWidget             *popup_label;
1750         GtkWidget             *child;
1751
1752         g_return_if_fail (window != NULL);
1753         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1754
1755         priv = GET_PRIV (window);
1756
1757         /* Reference the chat object */
1758         g_object_ref (chat);
1759
1760         /* If this window has just been created, position it */
1761         if (priv->chats == NULL) {
1762                 const gchar *name = "chat-window";
1763                 gboolean     separate_windows;
1764
1765                 empathy_conf_get_bool (empathy_conf_get (),
1766                                        EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1767                                        &separate_windows);
1768
1769                 if (separate_windows) {
1770                         name = empathy_chat_get_id (chat);
1771                 }
1772
1773                 empathy_geometry_bind (GTK_WINDOW (priv->dialog), name);
1774         }
1775
1776         child = GTK_WIDGET (chat);
1777         label = chat_window_create_label (window, chat, TRUE);
1778         popup_label = chat_window_create_label (window, chat, FALSE);
1779         gtk_widget_show (child);
1780
1781         g_signal_connect (chat, "notify::name",
1782                           G_CALLBACK (chat_window_chat_notify_cb),
1783                           NULL);
1784         g_signal_connect (chat, "notify::subject",
1785                           G_CALLBACK (chat_window_chat_notify_cb),
1786                           NULL);
1787         g_signal_connect (chat, "notify::remote-contact",
1788                           G_CALLBACK (chat_window_chat_notify_cb),
1789                           NULL);
1790         chat_window_chat_notify_cb (chat);
1791
1792         gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1793         gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1794         gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1795         gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1796                                             TRUE, TRUE, GTK_PACK_START);
1797
1798         DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1799 }
1800
1801 void
1802 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1803                                  EmpathyChat       *chat)
1804 {
1805         EmpathyChatWindowPriv *priv;
1806         gint                   position;
1807         EmpathyContact        *remote_contact;
1808
1809         g_return_if_fail (window != NULL);
1810         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1811
1812         priv = GET_PRIV (window);
1813
1814         g_signal_handlers_disconnect_by_func (chat,
1815                                               chat_window_chat_notify_cb,
1816                                               NULL);
1817         remote_contact = g_object_get_data (G_OBJECT (chat),
1818                                             "chat-window-remote-contact");
1819         if (remote_contact) {
1820                 g_signal_handlers_disconnect_by_func (remote_contact,
1821                                                       chat_window_update_chat_tab,
1822                                                       chat);
1823         }
1824
1825         position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1826                                           GTK_WIDGET (chat));
1827         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1828
1829         DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1830
1831         g_object_unref (chat);
1832 }
1833
1834 void
1835 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1836                                EmpathyChatWindow *new_window,
1837                                EmpathyChat       *chat)
1838 {
1839         GtkWidget *widget;
1840
1841         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1842         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1843         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1844
1845         widget = GTK_WIDGET (chat);
1846
1847         DEBUG ("Chat moving with widget:%p (%d references)", widget,
1848                 G_OBJECT (widget)->ref_count);
1849
1850         /* We reference here to make sure we don't loose the widget
1851          * and the EmpathyChat object during the move.
1852          */
1853         g_object_ref (chat);
1854         g_object_ref (widget);
1855
1856         empathy_chat_window_remove_chat (old_window, chat);
1857         empathy_chat_window_add_chat (new_window, chat);
1858
1859         g_object_unref (widget);
1860         g_object_unref (chat);
1861 }
1862
1863 void
1864 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1865                                     EmpathyChat       *chat)
1866 {
1867         EmpathyChatWindowPriv *priv;
1868         gint                  page_num;
1869
1870         g_return_if_fail (window != NULL);
1871         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1872
1873         priv = GET_PRIV (window);
1874
1875         page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1876                                           GTK_WIDGET (chat));
1877         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1878                                        page_num);
1879 }
1880
1881 gboolean
1882 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1883 {
1884         EmpathyChatWindowPriv *priv;
1885         gboolean              has_focus;
1886
1887         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1888
1889         priv = GET_PRIV (window);
1890
1891         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1892
1893         return has_focus;
1894 }
1895
1896 EmpathyChat *
1897 empathy_chat_window_find_chat (TpAccount   *account,
1898                                const gchar *id)
1899 {
1900         GList *l;
1901
1902         g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1903
1904         for (l = chat_windows; l; l = l->next) {
1905                 EmpathyChatWindowPriv *priv;
1906                 EmpathyChatWindow     *window;
1907                 GList                *ll;
1908
1909                 window = l->data;
1910                 priv = GET_PRIV (window);
1911
1912                 for (ll = priv->chats; ll; ll = ll->next) {
1913                         EmpathyChat *chat;
1914
1915                         chat = ll->data;
1916
1917                         if (account == empathy_chat_get_account (chat) &&
1918                             !tp_strdiff (id, empathy_chat_get_id (chat))) {
1919                                 return chat;
1920                         }
1921                 }
1922         }
1923
1924         return NULL;
1925 }
1926
1927 void
1928 empathy_chat_window_present_chat (EmpathyChat *chat)
1929 {
1930         EmpathyChatWindow     *window;
1931         EmpathyChatWindowPriv *priv;
1932
1933         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1934
1935         window = chat_window_find_chat (chat);
1936
1937         /* If the chat has no window, create one */
1938         if (window == NULL) {
1939                 window = empathy_chat_window_get_default ();
1940                 if (!window) {
1941                         window = empathy_chat_window_new ();
1942                 }
1943
1944                 empathy_chat_window_add_chat (window, chat);
1945         }
1946
1947         priv = GET_PRIV (window);
1948         empathy_chat_window_switch_to_chat (window, chat);
1949         empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1950
1951         gtk_widget_grab_focus (chat->input_text_view);
1952 }
1953