]> git.0d.be Git - empathy.git/blob - src/empathy-chat-window.c
bdc1a01960465e980a874c80ced337678bd711bd
[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         g_ptr_array_free (channels, TRUE);
891 }
892
893 static void
894 chat_window_close_activate_cb (GtkAction         *action,
895                                EmpathyChatWindow *window)
896 {
897         EmpathyChatWindowPriv *priv;
898
899         priv = GET_PRIV (window);
900
901         g_return_if_fail (priv->current_chat != NULL);
902
903         empathy_chat_window_remove_chat (window, priv->current_chat);
904 }
905
906 static void
907 chat_window_edit_activate_cb (GtkAction         *action,
908                               EmpathyChatWindow *window)
909 {
910         EmpathyChatWindowPriv *priv;
911         GtkClipboard         *clipboard;
912         GtkTextBuffer        *buffer;
913         gboolean              text_available;
914
915         priv = GET_PRIV (window);
916
917         g_return_if_fail (priv->current_chat != NULL);
918
919         if (!empathy_chat_get_tp_chat (priv->current_chat)) {
920                 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
921                 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
922                 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
923                 return;
924         }
925
926         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
927         if (gtk_text_buffer_get_has_selection (buffer)) {
928                 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
929                 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
930         } else {
931                 gboolean selection;
932
933                 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
934
935                 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
936                 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
937         }
938
939         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
940         text_available = gtk_clipboard_wait_is_text_available (clipboard);
941         gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
942 }
943
944 static void
945 chat_window_cut_activate_cb (GtkAction         *action,
946                              EmpathyChatWindow *window)
947 {
948         EmpathyChatWindowPriv *priv;
949
950         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
951
952         priv = GET_PRIV (window);
953
954         empathy_chat_cut (priv->current_chat);
955 }
956
957 static void
958 chat_window_copy_activate_cb (GtkAction         *action,
959                               EmpathyChatWindow *window)
960 {
961         EmpathyChatWindowPriv *priv;
962
963         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
964
965         priv = GET_PRIV (window);
966
967         empathy_chat_copy (priv->current_chat);
968 }
969
970 static void
971 chat_window_paste_activate_cb (GtkAction         *action,
972                                EmpathyChatWindow *window)
973 {
974         EmpathyChatWindowPriv *priv;
975
976         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
977
978         priv = GET_PRIV (window);
979
980         empathy_chat_paste (priv->current_chat);
981 }
982
983 static void
984 chat_window_tabs_next_activate_cb (GtkAction         *action,
985                                    EmpathyChatWindow *window)
986 {
987         EmpathyChatWindowPriv *priv;
988         EmpathyChat           *chat;
989         gint                  index_, numPages;
990
991         priv = GET_PRIV (window);
992
993         chat = priv->current_chat;
994         index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
995         numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
996
997         if (index_ == (numPages - 1)) {
998                 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), 0);
999                 return;
1000         }
1001
1002         gtk_notebook_next_page (GTK_NOTEBOOK (priv->notebook));
1003 }
1004
1005 static void
1006 chat_window_tabs_previous_activate_cb (GtkAction         *action,
1007                                    EmpathyChatWindow *window)
1008 {
1009         EmpathyChatWindowPriv *priv;
1010         EmpathyChat           *chat;
1011         gint                  index_, numPages;
1012
1013         priv = GET_PRIV (window);
1014
1015         chat = priv->current_chat;
1016         index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1017         numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
1018
1019         if (index_ <= 0) {
1020                 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), numPages - 1);
1021                 return;
1022         }
1023
1024         gtk_notebook_prev_page (GTK_NOTEBOOK (priv->notebook));
1025 }
1026
1027 static void
1028 chat_window_tabs_left_activate_cb (GtkAction         *action,
1029                                    EmpathyChatWindow *window)
1030 {
1031         EmpathyChatWindowPriv *priv;
1032         EmpathyChat           *chat;
1033         gint                  index_;
1034
1035         priv = GET_PRIV (window);
1036
1037         chat = priv->current_chat;
1038         index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1039         if (index_ <= 0) {
1040                 return;
1041         }
1042
1043         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
1044                                     GTK_WIDGET (chat),
1045                                     index_ - 1);
1046 }
1047
1048 static void
1049 chat_window_tabs_right_activate_cb (GtkAction         *action,
1050                                     EmpathyChatWindow *window)
1051 {
1052         EmpathyChatWindowPriv *priv;
1053         EmpathyChat           *chat;
1054         gint                  index_;
1055
1056         priv = GET_PRIV (window);
1057
1058         chat = priv->current_chat;
1059         index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1060
1061         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
1062                                     GTK_WIDGET (chat),
1063                                     index_ + 1);
1064 }
1065
1066 static void
1067 chat_window_detach_activate_cb (GtkAction         *action,
1068                                 EmpathyChatWindow *window)
1069 {
1070         EmpathyChatWindowPriv *priv;
1071         EmpathyChatWindow     *new_window;
1072         EmpathyChat           *chat;
1073
1074         priv = GET_PRIV (window);
1075
1076         chat = priv->current_chat;
1077         new_window = empathy_chat_window_new ();
1078
1079         empathy_chat_window_move_chat (window, new_window, chat);
1080
1081         priv = GET_PRIV (new_window);
1082         gtk_widget_show (priv->dialog);
1083 }
1084
1085 static void
1086 chat_window_help_contents_activate_cb (GtkAction         *action,
1087                                        EmpathyChatWindow *window)
1088 {
1089         EmpathyChatWindowPriv *priv = GET_PRIV (window);
1090
1091         empathy_url_show (priv->dialog, "ghelp:empathy");
1092 }
1093
1094 static void
1095 chat_window_help_about_activate_cb (GtkAction         *action,
1096                                     EmpathyChatWindow *window)
1097 {
1098         EmpathyChatWindowPriv *priv = GET_PRIV (window);
1099
1100         empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
1101 }
1102
1103 static gboolean
1104 chat_window_delete_event_cb (GtkWidget        *dialog,
1105                              GdkEvent         *event,
1106                              EmpathyChatWindow *window)
1107 {
1108         EmpathyChatWindowPriv *priv = GET_PRIV (window);
1109
1110         DEBUG ("Delete event received");
1111
1112         g_object_ref (window);
1113         while (priv->chats) {
1114                 empathy_chat_window_remove_chat (window, priv->chats->data);
1115         }
1116         g_object_unref (window);
1117
1118         return TRUE;
1119 }
1120
1121 static void
1122 chat_window_composing_cb (EmpathyChat       *chat,
1123                           gboolean          is_composing,
1124                           EmpathyChatWindow *window)
1125 {
1126         EmpathyChatWindowPriv *priv;
1127
1128         priv = GET_PRIV (window);
1129
1130         if (is_composing && !g_list_find (priv->chats_composing, chat)) {
1131                 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
1132         } else {
1133                 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1134         }
1135
1136         chat_window_update_chat_tab (chat);
1137 }
1138
1139 static void
1140 chat_window_set_urgency_hint (EmpathyChatWindow *window,
1141                               gboolean          urgent)
1142 {
1143         EmpathyChatWindowPriv *priv;
1144
1145         priv = GET_PRIV (window);
1146
1147         DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
1148         gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
1149 }
1150
1151 static void
1152 free_notification_data (NotificationData *data)
1153 {
1154         g_object_unref (data->chat);
1155         g_slice_free (NotificationData, data);
1156 }
1157
1158 static void
1159 chat_window_notification_closed_cb (NotifyNotification *notify,
1160                                     NotificationData *cb_data)
1161 {
1162         EmpathyNotificationClosedReason reason = 0;
1163         EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
1164
1165 #ifdef notify_notification_get_closed_reason
1166         reason = notify_notification_get_closed_reason (notify);
1167 #endif
1168         if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
1169                 empathy_chat_window_present_chat (cb_data->chat);
1170         }
1171
1172         g_object_unref (notify);
1173         priv->notification = NULL;
1174         free_notification_data (cb_data);
1175         priv->notification_data = NULL;
1176 }
1177
1178 static void
1179 chat_window_show_or_update_notification (EmpathyChatWindow *window,
1180                                          EmpathyMessage *message,
1181                                          EmpathyChat *chat)
1182 {
1183         EmpathyContact *sender;
1184         const gchar *header;
1185         char *escaped;
1186         const char *body;
1187         GdkPixbuf *pixbuf;
1188         EmpathyChatWindowPriv *priv = GET_PRIV (window);
1189         gboolean res;
1190
1191         if (!empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
1192                 return;
1193         } else {
1194                 empathy_conf_get_bool (empathy_conf_get (),
1195                                        EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
1196                 if (!res) {
1197                         return;
1198                 }
1199         }
1200
1201         sender = empathy_message_get_sender (message);
1202         header = empathy_contact_get_name (sender);
1203         body = empathy_message_get_body (message);
1204         escaped = g_markup_escape_text (body, -1);
1205
1206         if (priv->notification != NULL) {
1207                 notify_notification_update (priv->notification,
1208                                             header, escaped, NULL);
1209         } else {
1210                 NotificationData *cb_data = cb_data = g_slice_new0 (NotificationData);
1211
1212                 cb_data->chat = g_object_ref (chat);
1213                 cb_data->window = window;
1214
1215                 priv->notification_data = cb_data;
1216                 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
1217                 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
1218
1219                 g_signal_connect (priv->notification, "closed",
1220                                   G_CALLBACK (chat_window_notification_closed_cb), cb_data);
1221         }
1222
1223         pixbuf = empathy_notify_manager_get_pixbuf_for_notification (priv->notify_mgr,
1224                 sender, EMPATHY_IMAGE_NEW_MESSAGE);
1225
1226         if (pixbuf != NULL) {
1227                 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
1228                 g_object_unref (pixbuf);
1229         }
1230
1231         notify_notification_show (priv->notification, NULL);
1232
1233         g_free (escaped);
1234 }
1235
1236 static void
1237 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1238 {
1239         gchar *markup;
1240         GtkWidget *widget;
1241
1242         if (!empathy_chat_is_room (chat))
1243                 return;
1244
1245         markup = g_markup_printf_escaped (
1246                 "<span color=\"red\" weight=\"bold\">%s</span>",
1247                 empathy_chat_get_name (chat));
1248
1249         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1250         gtk_label_set_markup (GTK_LABEL (widget), markup);
1251         g_free (markup);
1252 }
1253
1254 static void
1255 chat_window_new_message_cb (EmpathyChat       *chat,
1256                             EmpathyMessage    *message,
1257                             EmpathyChatWindow *window)
1258 {
1259         EmpathyChatWindowPriv *priv;
1260         gboolean              has_focus;
1261         gboolean              needs_urgency;
1262         EmpathyContact        *sender;
1263
1264         priv = GET_PRIV (window);
1265
1266         has_focus = empathy_chat_window_has_focus (window);
1267
1268         /* - if we're the sender, we play the sound if it's specified in the
1269          *   preferences and we're not away.
1270          * - if we receive a message, we play the sound if it's specified in the
1271          *   preferences and the window does not have focus on the chat receiving
1272          *   the message.
1273          */
1274
1275         sender = empathy_message_get_sender (message);
1276
1277         if (empathy_contact_is_user (sender)) {
1278                 empathy_sound_play (GTK_WIDGET (priv->dialog),
1279                                     EMPATHY_SOUND_MESSAGE_OUTGOING);
1280         }
1281
1282         if (has_focus && priv->current_chat == chat) {
1283                 /* window and tab are focused so consider the message to be read */
1284                 empathy_chat_messages_read (chat);
1285                 return;
1286         }
1287
1288         if (!g_list_find (priv->chats_new_msg, chat)) {
1289                 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1290                 chat_window_update_chat_tab (chat);
1291         }
1292
1293         /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1294          * If empathy_chat_get_remote_contact () returns NULL, that means it's
1295          * an unamed MUC (msn-like).
1296          * In case of a MUC, we set urgency only if the message contains our
1297          * alias. */
1298         if (empathy_chat_is_room (chat) ||
1299             empathy_chat_get_remote_contact (chat) == NULL) {
1300                 needs_urgency = empathy_message_should_highlight (message);
1301         } else {
1302                 needs_urgency = TRUE;
1303         }
1304
1305         if (needs_urgency) {
1306                 if (!has_focus) {
1307                         chat_window_set_urgency_hint (window, TRUE);
1308                         chat_window_set_highlight_room_tab_label (chat);
1309                 }
1310
1311                 empathy_sound_play (GTK_WIDGET (priv->dialog),
1312                     EMPATHY_SOUND_MESSAGE_INCOMING);
1313                 chat_window_show_or_update_notification (window, message, chat);
1314         }
1315
1316         /* update the number of unread messages */
1317         chat_window_title_update (priv);
1318 }
1319
1320 static GtkNotebook *
1321 chat_window_detach_hook (GtkNotebook *source,
1322                          GtkWidget   *page,
1323                          gint         x,
1324                          gint         y,
1325                          gpointer     user_data)
1326 {
1327         EmpathyChatWindowPriv *priv;
1328         EmpathyChatWindow     *window, *new_window;
1329         EmpathyChat           *chat;
1330
1331         chat = EMPATHY_CHAT (page);
1332         window = chat_window_find_chat (chat);
1333
1334         new_window = empathy_chat_window_new ();
1335         priv = GET_PRIV (new_window);
1336
1337         DEBUG ("Detach hook called");
1338
1339         empathy_chat_window_move_chat (window, new_window, chat);
1340
1341         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1342         gtk_widget_show (priv->dialog);
1343
1344         return NULL;
1345 }
1346
1347 static void
1348 chat_window_page_switched_cb (GtkNotebook      *notebook,
1349                               GtkNotebookPage  *page,
1350                               gint              page_num,
1351                               EmpathyChatWindow *window)
1352 {
1353         EmpathyChatWindowPriv *priv;
1354         EmpathyChat           *chat;
1355         GtkWidget            *child;
1356
1357         DEBUG ("Page switched");
1358
1359         priv = GET_PRIV (window);
1360
1361         child = gtk_notebook_get_nth_page (notebook, page_num);
1362         chat = EMPATHY_CHAT (child);
1363
1364         if (priv->page_added) {
1365                 priv->page_added = FALSE;
1366                 empathy_chat_scroll_down (chat);
1367         }
1368         else if (priv->current_chat == chat) {
1369                 return;
1370         }
1371
1372         priv->current_chat = chat;
1373         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1374         empathy_chat_messages_read (chat);
1375
1376         chat_window_update_chat_tab (chat);
1377 }
1378
1379 static void
1380 chat_window_page_added_cb (GtkNotebook      *notebook,
1381                            GtkWidget        *child,
1382                            guint             page_num,
1383                            EmpathyChatWindow *window)
1384 {
1385         EmpathyChatWindowPriv *priv;
1386         EmpathyChat           *chat;
1387
1388         priv = GET_PRIV (window);
1389
1390         /* If we just received DND to the same window, we don't want
1391          * to do anything here like removing the tab and then readding
1392          * it, so we return here and in "page-added".
1393          */
1394         if (priv->dnd_same_window) {
1395                 DEBUG ("Page added (back to the same window)");
1396                 priv->dnd_same_window = FALSE;
1397                 return;
1398         }
1399
1400         DEBUG ("Page added");
1401
1402         /* Get chat object */
1403         chat = EMPATHY_CHAT (child);
1404
1405         /* Connect chat signals for this window */
1406         g_signal_connect (chat, "composing",
1407                           G_CALLBACK (chat_window_composing_cb),
1408                           window);
1409         g_signal_connect (chat, "new-message",
1410                           G_CALLBACK (chat_window_new_message_cb),
1411                           window);
1412         g_signal_connect (chat, "notify::tp-chat",
1413                           G_CALLBACK (chat_window_update_chat_tab),
1414                           window);
1415
1416         /* Set flag so we know to perform some special operations on
1417          * switch page due to the new page being added.
1418          */
1419         priv->page_added = TRUE;
1420
1421         /* Get list of chats up to date */
1422         priv->chats = g_list_append (priv->chats, chat);
1423
1424         chat_window_update_chat_tab (chat);
1425 }
1426
1427 static void
1428 chat_window_page_removed_cb (GtkNotebook      *notebook,
1429                              GtkWidget        *child,
1430                              guint             page_num,
1431                              EmpathyChatWindow *window)
1432 {
1433         EmpathyChatWindowPriv *priv;
1434         EmpathyChat           *chat;
1435
1436         priv = GET_PRIV (window);
1437
1438         /* If we just received DND to the same window, we don't want
1439          * to do anything here like removing the tab and then readding
1440          * it, so we return here and in "page-added".
1441          */
1442         if (priv->dnd_same_window) {
1443                 DEBUG ("Page removed (and will be readded to same window)");
1444                 return;
1445         }
1446
1447         DEBUG ("Page removed");
1448
1449         /* Get chat object */
1450         chat = EMPATHY_CHAT (child);
1451
1452         /* Disconnect all signal handlers for this chat and this window */
1453         g_signal_handlers_disconnect_by_func (chat,
1454                                               G_CALLBACK (chat_window_composing_cb),
1455                                               window);
1456         g_signal_handlers_disconnect_by_func (chat,
1457                                               G_CALLBACK (chat_window_new_message_cb),
1458                                               window);
1459         g_signal_handlers_disconnect_by_func (chat,
1460                                               G_CALLBACK (chat_window_update_chat_tab),
1461                                               window);
1462
1463         /* Keep list of chats up to date */
1464         priv->chats = g_list_remove (priv->chats, chat);
1465         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1466         empathy_chat_messages_read (chat);
1467         priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1468
1469         if (priv->chats == NULL) {
1470                 g_object_unref (window);
1471         } else {
1472                 chat_window_update (window);
1473         }
1474 }
1475
1476 static gboolean
1477 chat_window_focus_in_event_cb (GtkWidget        *widget,
1478                                GdkEvent         *event,
1479                                EmpathyChatWindow *window)
1480 {
1481         EmpathyChatWindowPriv *priv;
1482
1483         DEBUG ("Focus in event, updating title");
1484
1485         priv = GET_PRIV (window);
1486
1487         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1488         empathy_chat_messages_read (priv->current_chat);
1489
1490         chat_window_set_urgency_hint (window, FALSE);
1491
1492         /* Update the title, since we now mark all unread messages as read. */
1493         chat_window_update_chat_tab (priv->current_chat);
1494
1495         return FALSE;
1496 }
1497
1498 static gboolean
1499 chat_window_drag_motion (GtkWidget        *widget,
1500                          GdkDragContext   *context,
1501                          int               x,
1502                          int               y,
1503                          guint             time_,
1504                          EmpathyChatWindow *window)
1505 {
1506         GdkAtom target;
1507         EmpathyChatWindowPriv *priv;
1508         GdkAtom dest_target;
1509
1510         priv = GET_PRIV (window);
1511
1512         target = gtk_drag_dest_find_target (widget, context, NULL);
1513
1514         dest_target = gdk_atom_intern_static_string ("text/uri-list");
1515         if (target == dest_target) {
1516                 /* This is a file drag.  Ensure the contact is online and set the
1517                    drag type to COPY.  Note that it's possible that the tab will
1518                    be switched by GTK+ after a timeout from drag_motion without
1519                    getting another drag_motion to disable the drop.  You have
1520                    to hold your mouse really still.
1521                  */
1522                 EmpathyContact *contact;
1523
1524                 priv = GET_PRIV (window);
1525                 contact = empathy_chat_get_remote_contact (priv->current_chat);
1526                 /* contact is NULL for multi-user chats.  We don't do
1527                  * file transfers to MUCs.  We also don't send files
1528                  * to offline contacts or contacts that don't support
1529                  * file transfer.
1530                  */
1531                 if ((contact == NULL) || !empathy_contact_is_online (contact)) {
1532                         gdk_drag_status (context, 0, time_);
1533                         return FALSE;
1534                 }
1535                 if (!(empathy_contact_get_capabilities (contact)
1536                            & EMPATHY_CAPABILITIES_FT)) {
1537                         gdk_drag_status (context, 0, time_);
1538                         return FALSE;
1539                 }
1540                 gdk_drag_status (context, GDK_ACTION_COPY, time_);
1541                 return TRUE;
1542         }
1543
1544         dest_target = gdk_atom_intern_static_string ("text/contact-id");
1545         if (target == dest_target) {
1546                 /* This is a drag of a contact from a contact list.  Set to COPY.
1547                    FIXME: If this drag is to a MUC window, it invites the user.
1548                    Otherwise, it opens a chat.  Should we use a different drag
1549                    type for invites?  Should we allow ASK?
1550                  */
1551                 gdk_drag_status (context, GDK_ACTION_COPY, time_);
1552                 return TRUE;
1553         }
1554
1555         /* Otherwise, it must be a notebook tab drag.  Set to MOVE. */
1556         gdk_drag_status (context, GDK_ACTION_MOVE, time_);
1557         return TRUE;
1558 }
1559
1560 static void
1561 chat_window_drag_data_received (GtkWidget        *widget,
1562                                 GdkDragContext   *context,
1563                                 int               x,
1564                                 int               y,
1565                                 GtkSelectionData *selection,
1566                                 guint             info,
1567                                 guint             time_,
1568                                 EmpathyChatWindow *window)
1569 {
1570         if (info == DND_DRAG_TYPE_CONTACT_ID) {
1571                 EmpathyChat           *chat = NULL;
1572                 EmpathyChatWindow     *old_window;
1573                 TpAccount             *account = NULL;
1574                 TpAccountManager      *account_manager;
1575                 const gchar           *id;
1576                 gchar                **strv;
1577                 const gchar           *account_id;
1578                 const gchar           *contact_id;
1579
1580                 id = (const gchar*) gtk_selection_data_get_data (selection);
1581
1582                 /* FIXME: Perhaps should be sure that the account manager is
1583                  * prepared before calling _ensure_account on it. */
1584                 account_manager = tp_account_manager_dup ();
1585
1586                 DEBUG ("DND contact from roster with id:'%s'", id);
1587
1588                 strv = g_strsplit (id, ":", 2);
1589                 if (g_strv_length (strv) == 2) {
1590                         account_id = strv[0];
1591                         contact_id = strv[1];
1592                         account =
1593                                 tp_account_manager_ensure_account (account_manager, account_id);
1594                         if (account != NULL)
1595                                 chat = empathy_chat_window_find_chat (account, contact_id);
1596                 }
1597
1598                 if (account == NULL) {
1599                         g_strfreev (strv);
1600                         gtk_drag_finish (context, FALSE, FALSE, time_);
1601                         return;
1602                 }
1603
1604                 if (!chat) {
1605                         TpConnection *connection;
1606
1607                         connection = tp_account_get_connection (account);
1608
1609                         if (connection) {
1610                                 empathy_dispatcher_chat_with_contact_id (
1611                                         connection, contact_id, NULL, NULL);
1612                         }
1613
1614                         g_strfreev (strv);
1615                         return;
1616                 }
1617                 g_object_unref (account_manager);
1618                 g_strfreev (strv);
1619
1620                 old_window = chat_window_find_chat (chat);
1621                 if (old_window) {
1622                         if (old_window == window) {
1623                                 gtk_drag_finish (context, TRUE, FALSE, time_);
1624                                 return;
1625                         }
1626
1627                         empathy_chat_window_move_chat (old_window, window, chat);
1628                 } else {
1629                         empathy_chat_window_add_chat (window, chat);
1630                 }
1631
1632                 /* Added to take care of any outstanding chat events */
1633                 empathy_chat_window_present_chat (chat);
1634
1635                 /* We should return TRUE to remove the data when doing
1636                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1637                  * weird consequences, and we handle that internally
1638                  * anyway with add_chat () and remove_chat ().
1639                  */
1640                 gtk_drag_finish (context, TRUE, FALSE, time_);
1641         }
1642         else if (info == DND_DRAG_TYPE_URI_LIST) {
1643                 EmpathyChatWindowPriv *priv;
1644                 EmpathyContact *contact;
1645                 const gchar *data;
1646
1647                 priv = GET_PRIV (window);
1648                 contact = empathy_chat_get_remote_contact (priv->current_chat);
1649
1650                 /* contact is NULL when current_chat is a multi-user chat.
1651                  * We don't do file transfers to MUCs, so just cancel the drag.
1652                  */
1653                 if (contact == NULL) {
1654                         gtk_drag_finish (context, TRUE, FALSE, time_);
1655                         return;
1656                 }
1657
1658                 data = (const gchar *) gtk_selection_data_get_data (selection);
1659                 empathy_send_file_from_uri_list (contact, data);
1660
1661                 gtk_drag_finish (context, TRUE, FALSE, time_);
1662         }
1663         else if (info == DND_DRAG_TYPE_TAB) {
1664                 EmpathyChat        **chat;
1665                 EmpathyChatWindow   *old_window = NULL;
1666
1667                 DEBUG ("DND tab");
1668
1669                 chat = (void *) gtk_selection_data_get_data (selection);
1670                 old_window = chat_window_find_chat (*chat);
1671
1672                 if (old_window) {
1673                         EmpathyChatWindowPriv *priv;
1674
1675                         priv = GET_PRIV (window);
1676
1677                         if (old_window == window) {
1678                                 DEBUG ("DND tab (within same window)");
1679                                 priv->dnd_same_window = TRUE;
1680                                 gtk_drag_finish (context, TRUE, FALSE, time_);
1681                                 return;
1682                         }
1683
1684                         priv->dnd_same_window = FALSE;
1685                 }
1686
1687                 /* We should return TRUE to remove the data when doing
1688                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1689                  * weird consequences, and we handle that internally
1690                  * anyway with add_chat () and remove_chat ().
1691                  */
1692                 gtk_drag_finish (context, TRUE, FALSE, time_);
1693         } else {
1694                 DEBUG ("DND from unknown source");
1695                 gtk_drag_finish (context, FALSE, FALSE, time_);
1696         }
1697 }
1698
1699 static void
1700 chat_window_finalize (GObject *object)
1701 {
1702         EmpathyChatWindow     *window;
1703         EmpathyChatWindowPriv *priv;
1704
1705         window = EMPATHY_CHAT_WINDOW (object);
1706         priv = GET_PRIV (window);
1707
1708         DEBUG ("Finalized: %p", object);
1709
1710         g_object_unref (priv->ui_manager);
1711         g_object_unref (priv->chatroom_manager);
1712         g_object_unref (priv->notify_mgr);
1713
1714         if (priv->notification != NULL) {
1715                 notify_notification_close (priv->notification, NULL);
1716                 g_object_unref (priv->notification);
1717                 priv->notification = NULL;
1718                 if (priv->notification_data != NULL)
1719                         {
1720                                 free_notification_data (priv->notification_data);
1721                                 priv->notification_data = NULL;
1722                         }
1723         }
1724
1725         if (priv->contact_targets) {
1726                 gtk_target_list_unref (priv->contact_targets);
1727         }
1728         if (priv->file_targets) {
1729                 gtk_target_list_unref (priv->file_targets);
1730         }
1731
1732         chat_windows = g_list_remove (chat_windows, window);
1733         gtk_widget_destroy (priv->dialog);
1734
1735         G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1736 }
1737
1738 static void
1739 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1740 {
1741         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1742
1743         object_class->finalize = chat_window_finalize;
1744
1745         g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1746
1747         /* Set up a style for the close button with no focus padding. */
1748         gtk_rc_parse_string (
1749                 "style \"empathy-close-button-style\"\n"
1750                 "{\n"
1751                 "  GtkWidget::focus-padding = 0\n"
1752                 "  xthickness = 0\n"
1753                 "  ythickness = 0\n"
1754                 "}\n"
1755                 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1756
1757         gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1758 }
1759
1760 static void
1761 empathy_chat_window_init (EmpathyChatWindow *window)
1762 {
1763         GtkBuilder            *gui;
1764         GtkAccelGroup         *accel_group;
1765         GClosure              *closure;
1766         GtkWidget             *menu;
1767         GtkWidget             *submenu;
1768         guint                  i;
1769         GtkWidget             *chat_vbox;
1770         gchar                 *filename;
1771         EmpathySmileyManager  *smiley_manager;
1772         EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1773                 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1774
1775         window->priv = priv;
1776         filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1777         gui = empathy_builder_get_file (filename,
1778                                        "chat_window", &priv->dialog,
1779                                        "chat_vbox", &chat_vbox,
1780                                        "ui_manager", &priv->ui_manager,
1781                                        "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1782                                        "menu_conv_favorite", &priv->menu_conv_favorite,
1783                                        "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1784                                        "menu_edit_cut", &priv->menu_edit_cut,
1785                                        "menu_edit_copy", &priv->menu_edit_copy,
1786                                        "menu_edit_paste", &priv->menu_edit_paste,
1787                                        "menu_tabs_next", &priv->menu_tabs_next,
1788                                        "menu_tabs_prev", &priv->menu_tabs_prev,
1789                                        "menu_tabs_left", &priv->menu_tabs_left,
1790                                        "menu_tabs_right", &priv->menu_tabs_right,
1791                                        "menu_tabs_detach", &priv->menu_tabs_detach,
1792                                        NULL);
1793         g_free (filename);
1794
1795         empathy_builder_connect (gui, window,
1796                               "menu_conv", "activate", chat_window_conv_activate_cb,
1797                               "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1798                               "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1799                               "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1800                               "menu_conv_invite_participant", "activate", chat_window_invite_participant_activate_cb,
1801                               "menu_conv_close", "activate", chat_window_close_activate_cb,
1802                               "menu_edit", "activate", chat_window_edit_activate_cb,
1803                               "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1804                               "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1805                               "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1806                               "menu_tabs_next", "activate", chat_window_tabs_next_activate_cb,
1807                               "menu_tabs_prev", "activate", chat_window_tabs_previous_activate_cb,
1808                               "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1809                               "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1810                               "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1811                               "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1812                               "menu_help_about", "activate", chat_window_help_about_activate_cb,
1813                               NULL);
1814
1815         g_object_ref (priv->ui_manager);
1816         g_object_unref (gui);
1817
1818         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1819
1820         priv->notebook = gtk_notebook_new ();
1821         gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1822         gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1823         gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1824         gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1825         gtk_widget_show (priv->notebook);
1826
1827         /* Set up accels */
1828         accel_group = gtk_accel_group_new ();
1829         gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1830
1831         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1832                 closure =  g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1833                                            window,
1834                                            NULL);
1835                 gtk_accel_group_connect (accel_group,
1836                                          tab_accel_keys[i],
1837                                          GDK_MOD1_MASK,
1838                                          0,
1839                                          closure);
1840         }
1841
1842         g_object_unref (accel_group);
1843
1844         /* Set up drag target lists */
1845         priv->contact_targets = gtk_target_list_new (drag_types_dest_contact,
1846                                                      G_N_ELEMENTS (drag_types_dest_contact));
1847         priv->file_targets = gtk_target_list_new (drag_types_dest_file,
1848                                                   G_N_ELEMENTS (drag_types_dest_file));
1849
1850         /* Set up smiley menu */
1851         smiley_manager = empathy_smiley_manager_dup_singleton ();
1852         submenu = empathy_smiley_menu_new (smiley_manager,
1853                                            chat_window_insert_smiley_activate_cb,
1854                                            window);
1855         menu = gtk_ui_manager_get_widget (priv->ui_manager,
1856                 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1857         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1858         g_object_unref (smiley_manager);
1859
1860         /* Set up signals we can't do with ui file since we may need to
1861          * block/unblock them at some later stage.
1862          */
1863
1864         g_signal_connect (priv->dialog,
1865                           "delete_event",
1866                           G_CALLBACK (chat_window_delete_event_cb),
1867                           window);
1868         g_signal_connect (priv->dialog,
1869                           "focus_in_event",
1870                           G_CALLBACK (chat_window_focus_in_event_cb),
1871                           window);
1872         g_signal_connect_after (priv->notebook,
1873                                 "switch_page",
1874                                 G_CALLBACK (chat_window_page_switched_cb),
1875                                 window);
1876         g_signal_connect (priv->notebook,
1877                           "page_added",
1878                           G_CALLBACK (chat_window_page_added_cb),
1879                           window);
1880         g_signal_connect (priv->notebook,
1881                           "page_removed",
1882                           G_CALLBACK (chat_window_page_removed_cb),
1883                           window);
1884
1885         /* Set up drag and drop */
1886         gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1887                            GTK_DEST_DEFAULT_ALL,
1888                            drag_types_dest,
1889                            G_N_ELEMENTS (drag_types_dest),
1890                            GDK_ACTION_MOVE | GDK_ACTION_COPY);
1891
1892         /* connect_after to allow GtkNotebook's built-in tab switching */
1893         g_signal_connect_after (priv->notebook,
1894                                 "drag-motion",
1895                                 G_CALLBACK (chat_window_drag_motion),
1896                                 window);
1897         g_signal_connect (priv->notebook,
1898                           "drag-data-received",
1899                           G_CALLBACK (chat_window_drag_data_received),
1900                           window);
1901
1902         chat_windows = g_list_prepend (chat_windows, window);
1903
1904         /* Set up private details */
1905         priv->chats = NULL;
1906         priv->chats_new_msg = NULL;
1907         priv->chats_composing = NULL;
1908         priv->current_chat = NULL;
1909
1910         priv->notify_mgr = empathy_notify_manager_dup_singleton ();
1911 }
1912
1913 EmpathyChatWindow *
1914 empathy_chat_window_new (void)
1915 {
1916         return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1917 }
1918
1919 /* Returns the window to open a new tab in if there is only one window
1920  * visble, otherwise, returns NULL indicating that a new window should
1921  * be added.
1922  */
1923 EmpathyChatWindow *
1924 empathy_chat_window_get_default (gboolean room)
1925 {
1926         GList    *l;
1927         gboolean  separate_windows = TRUE;
1928
1929         empathy_conf_get_bool (empathy_conf_get (),
1930                               EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1931                               &separate_windows);
1932
1933         if (separate_windows) {
1934                 /* Always create a new window */
1935                 return NULL;
1936         }
1937
1938         for (l = chat_windows; l; l = l->next) {
1939                 EmpathyChatWindowPriv *priv;
1940                 EmpathyChatWindow *chat_window;
1941                 GtkWidget         *dialog;
1942
1943                 chat_window = l->data;
1944                 priv = GET_PRIV (chat_window);
1945
1946                 dialog = empathy_chat_window_get_dialog (chat_window);
1947                 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1948                         guint nb_rooms, nb_private;
1949                         empathy_chat_window_get_nb_chats (chat_window, &nb_rooms, &nb_private);
1950
1951                         /* Skip the window if there aren't any rooms in it */
1952                         if (room && nb_rooms == 0)
1953                                 continue;
1954
1955                         /* Skip the window if there aren't any 1-1 chats in it */
1956                         if (!room && nb_private == 0)
1957                                 continue;
1958
1959                         /* Found a visible window on this desktop */
1960                         return chat_window;
1961                 }
1962         }
1963
1964         return NULL;
1965 }
1966
1967 GtkWidget *
1968 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1969 {
1970         EmpathyChatWindowPriv *priv;
1971
1972         g_return_val_if_fail (window != NULL, NULL);
1973
1974         priv = GET_PRIV (window);
1975
1976         return priv->dialog;
1977 }
1978
1979 void
1980 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1981                               EmpathyChat       *chat)
1982 {
1983         EmpathyChatWindowPriv *priv;
1984         GtkWidget             *label;
1985         GtkWidget             *popup_label;
1986         GtkWidget             *child;
1987
1988         g_return_if_fail (window != NULL);
1989         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1990
1991         priv = GET_PRIV (window);
1992
1993         /* Reference the chat object */
1994         g_object_ref (chat);
1995
1996         /* If this window has just been created, position it */
1997         if (priv->chats == NULL) {
1998                 const gchar *name = "chat-window";
1999                 gboolean     separate_windows;
2000
2001                 empathy_conf_get_bool (empathy_conf_get (),
2002                                        EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
2003                                        &separate_windows);
2004
2005                 if (separate_windows) {
2006                         name = empathy_chat_get_id (chat);
2007                 }
2008                 else if (empathy_chat_is_room (chat)) {
2009                         name = "room-window";
2010                 }
2011
2012                 empathy_geometry_bind (GTK_WINDOW (priv->dialog), name);
2013         }
2014
2015         child = GTK_WIDGET (chat);
2016         label = chat_window_create_label (window, chat, TRUE);
2017         popup_label = chat_window_create_label (window, chat, FALSE);
2018         gtk_widget_show (child);
2019
2020         g_signal_connect (chat, "notify::name",
2021                           G_CALLBACK (chat_window_chat_notify_cb),
2022                           NULL);
2023         g_signal_connect (chat, "notify::subject",
2024                           G_CALLBACK (chat_window_chat_notify_cb),
2025                           NULL);
2026         g_signal_connect (chat, "notify::remote-contact",
2027                           G_CALLBACK (chat_window_chat_notify_cb),
2028                           NULL);
2029         chat_window_chat_notify_cb (chat);
2030
2031         gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
2032         gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
2033         gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
2034         gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
2035                                             TRUE, TRUE, GTK_PACK_START);
2036
2037         DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
2038 }
2039
2040 void
2041 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
2042                                  EmpathyChat       *chat)
2043 {
2044         EmpathyChatWindowPriv *priv;
2045         gint                   position;
2046         EmpathyContact        *remote_contact;
2047
2048         g_return_if_fail (window != NULL);
2049         g_return_if_fail (EMPATHY_IS_CHAT (chat));
2050
2051         priv = GET_PRIV (window);
2052
2053         g_signal_handlers_disconnect_by_func (chat,
2054                                               chat_window_chat_notify_cb,
2055                                               NULL);
2056         remote_contact = g_object_get_data (G_OBJECT (chat),
2057                                             "chat-window-remote-contact");
2058         if (remote_contact) {
2059                 g_signal_handlers_disconnect_by_func (remote_contact,
2060                                                       chat_window_update_chat_tab,
2061                                                       chat);
2062         }
2063
2064         position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
2065                                           GTK_WIDGET (chat));
2066         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
2067
2068         DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
2069
2070         g_object_unref (chat);
2071 }
2072
2073 void
2074 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
2075                                EmpathyChatWindow *new_window,
2076                                EmpathyChat       *chat)
2077 {
2078         GtkWidget *widget;
2079
2080         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
2081         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
2082         g_return_if_fail (EMPATHY_IS_CHAT (chat));
2083
2084         widget = GTK_WIDGET (chat);
2085
2086         DEBUG ("Chat moving with widget:%p (%d references)", widget,
2087                 G_OBJECT (widget)->ref_count);
2088
2089         /* We reference here to make sure we don't loose the widget
2090          * and the EmpathyChat object during the move.
2091          */
2092         g_object_ref (chat);
2093         g_object_ref (widget);
2094
2095         empathy_chat_window_remove_chat (old_window, chat);
2096         empathy_chat_window_add_chat (new_window, chat);
2097
2098         g_object_unref (widget);
2099         g_object_unref (chat);
2100 }
2101
2102 void
2103 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
2104                                     EmpathyChat       *chat)
2105 {
2106         EmpathyChatWindowPriv *priv;
2107         gint                  page_num;
2108
2109         g_return_if_fail (window != NULL);
2110         g_return_if_fail (EMPATHY_IS_CHAT (chat));
2111
2112         priv = GET_PRIV (window);
2113
2114         page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
2115                                           GTK_WIDGET (chat));
2116         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
2117                                        page_num);
2118 }
2119
2120 gboolean
2121 empathy_chat_window_has_focus (EmpathyChatWindow *window)
2122 {
2123         EmpathyChatWindowPriv *priv;
2124         gboolean              has_focus;
2125
2126         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
2127
2128         priv = GET_PRIV (window);
2129
2130         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
2131
2132         return has_focus;
2133 }
2134
2135 EmpathyChat *
2136 empathy_chat_window_find_chat (TpAccount   *account,
2137                                const gchar *id)
2138 {
2139         GList *l;
2140
2141         g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
2142
2143         for (l = chat_windows; l; l = l->next) {
2144                 EmpathyChatWindowPriv *priv;
2145                 EmpathyChatWindow     *window;
2146                 GList                *ll;
2147
2148                 window = l->data;
2149                 priv = GET_PRIV (window);
2150
2151                 for (ll = priv->chats; ll; ll = ll->next) {
2152                         EmpathyChat *chat;
2153
2154                         chat = ll->data;
2155
2156                         if (account == empathy_chat_get_account (chat) &&
2157                             !tp_strdiff (id, empathy_chat_get_id (chat))) {
2158                                 return chat;
2159                         }
2160                 }
2161         }
2162
2163         return NULL;
2164 }
2165
2166 void
2167 empathy_chat_window_present_chat (EmpathyChat *chat)
2168 {
2169         EmpathyChatWindow     *window;
2170         EmpathyChatWindowPriv *priv;
2171
2172         g_return_if_fail (EMPATHY_IS_CHAT (chat));
2173
2174         window = chat_window_find_chat (chat);
2175
2176         /* If the chat has no window, create one */
2177         if (window == NULL) {
2178                 window = empathy_chat_window_get_default (empathy_chat_is_room (chat));
2179                 if (!window) {
2180                         window = empathy_chat_window_new ();
2181                 }
2182
2183                 empathy_chat_window_add_chat (window, chat);
2184         }
2185
2186         priv = GET_PRIV (window);
2187         empathy_chat_window_switch_to_chat (window, chat);
2188         empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
2189
2190         gtk_widget_grab_focus (chat->input_text_view);
2191 }
2192
2193 void
2194 empathy_chat_window_get_nb_chats (EmpathyChatWindow *self,
2195                                guint *nb_rooms,
2196                                guint *nb_private)
2197 {
2198         EmpathyChatWindowPriv *priv = GET_PRIV (self);
2199         GList *l;
2200         guint _nb_rooms = 0, _nb_private = 0;
2201
2202         for (l = priv->chats; l != NULL; l = g_list_next (l)) {
2203                 if (empathy_chat_is_room (EMPATHY_CHAT (l->data)))
2204                         _nb_rooms++;
2205                 else
2206                         _nb_private++;
2207         }
2208
2209         if (nb_rooms != NULL)
2210                 *nb_rooms = _nb_rooms;
2211         if (nb_private != NULL)
2212                 *nb_private = _nb_private;
2213 }