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