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