]> git.0d.be Git - empathy.git/blob - src/empathy-chat-window.c
Make notifications for unfocused chat windows work properly.
[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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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  */
27
28 #include <config.h>
29
30 #include <string.h>
31
32 #include <gtk/gtk.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <glade/glade.h>
35 #include <glib/gi18n.h>
36 #include <libnotify/notification.h>
37
38 #include <telepathy-glib/util.h>
39 #include <libmissioncontrol/mission-control.h>
40
41 #include <libempathy/empathy-contact-factory.h>
42 #include <libempathy/empathy-contact.h>
43 #include <libempathy/empathy-message.h>
44 #include <libempathy/empathy-dispatcher.h>
45 #include <libempathy/empathy-chatroom-manager.h>
46 #include <libempathy/empathy-utils.h>
47
48 #include <libempathy-gtk/empathy-images.h>
49 #include <libempathy-gtk/empathy-conf.h>
50 #include <libempathy-gtk/empathy-contact-dialogs.h>
51 #include <libempathy-gtk/empathy-log-window.h>
52 #include <libempathy-gtk/empathy-geometry.h>
53 #include <libempathy-gtk/empathy-smiley-manager.h>
54 #include <libempathy-gtk/empathy-ui-utils.h>
55
56 #include "empathy-chat-window.h"
57 #include "empathy-about-dialog.h"
58
59 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
60 #include <libempathy/empathy-debug.h>
61
62 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatWindow)
63 typedef struct {
64         EmpathyChat *current_chat;
65         GList       *chats;
66         GList       *chats_new_msg;
67         GList       *chats_composing;
68         gboolean     page_added;
69         gboolean     dnd_same_window;
70         guint        save_geometry_id;
71         EmpathyChatroomManager *chatroom_manager;
72         GtkWidget   *dialog;
73         GtkWidget   *notebook;
74         NotifyNotification *notification;
75
76         /* Menu items. */
77         GtkWidget   *menu_conv_clear;
78         GtkWidget   *menu_conv_insert_smiley;
79         GtkWidget   *menu_conv_contact;
80         GtkWidget   *menu_conv_favorite;
81         GtkWidget   *menu_conv_close;
82
83         GtkWidget   *menu_edit_cut;
84         GtkWidget   *menu_edit_copy;
85         GtkWidget   *menu_edit_paste;
86
87         GtkWidget   *menu_tabs_next;
88         GtkWidget   *menu_tabs_prev;
89         GtkWidget   *menu_tabs_left;
90         GtkWidget   *menu_tabs_right;
91         GtkWidget   *menu_tabs_detach;
92         
93         GtkWidget   *menu_help_contents;
94         GtkWidget   *menu_help_about;
95 } EmpathyChatWindowPriv;
96
97 static GList *chat_windows = NULL;
98
99 static const guint tab_accel_keys[] = {
100         GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
101         GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
102 };
103
104 typedef enum {
105         DND_DRAG_TYPE_CONTACT_ID,
106         DND_DRAG_TYPE_TAB
107 } DndDragType;
108
109 static const GtkTargetEntry drag_types_dest[] = {
110         { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
111         { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
112 };
113
114 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
115
116 static void
117 chat_window_accel_cb (GtkAccelGroup    *accelgroup,
118                       GObject          *object,
119                       guint             key,
120                       GdkModifierType   mod,
121                       EmpathyChatWindow *window)
122 {
123         EmpathyChatWindowPriv *priv;
124         gint                  num = -1;
125         gint                  i;
126
127         priv = GET_PRIV (window);
128
129         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
130                 if (tab_accel_keys[i] == key) {
131                         num = i;
132                         break;
133                 }
134         }
135
136         if (num != -1) {
137                 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
138         }
139 }
140
141 static EmpathyChatWindow *
142 chat_window_find_chat (EmpathyChat *chat)
143 {
144         EmpathyChatWindowPriv *priv;
145         GList                 *l, *ll;
146
147         for (l = chat_windows; l; l = l->next) {
148                 priv = GET_PRIV (l->data);
149                 ll = g_list_find (priv->chats, chat);
150                 if (ll) {
151                         return l->data;
152                 }
153         }
154
155         return NULL;
156 }
157
158 static void
159 chat_window_close_clicked_cb (GtkWidget  *button,
160                               EmpathyChat *chat)
161 {
162         EmpathyChatWindow *window;
163
164         window = chat_window_find_chat (chat);
165         empathy_chat_window_remove_chat (window, chat);
166 }
167
168 static void
169 chat_window_close_button_style_set_cb (GtkWidget *button,
170                                        GtkStyle  *previous_style,
171                                        gpointer   user_data)
172 {
173         gint h, w;
174
175         gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
176                                            GTK_ICON_SIZE_MENU, &w, &h);
177
178         gtk_widget_set_size_request (button, w, h);
179 }
180
181 static GtkWidget *
182 chat_window_create_label (EmpathyChatWindow *window,
183                           EmpathyChat       *chat)
184 {
185         EmpathyChatWindowPriv *priv;
186         GtkWidget            *hbox;
187         GtkWidget            *name_label;
188         GtkWidget            *status_image;
189         GtkWidget            *close_button;
190         GtkWidget            *close_image;
191         GtkWidget            *event_box;
192         GtkWidget            *event_box_hbox;
193         PangoAttrList        *attr_list;
194         PangoAttribute       *attr;
195
196         priv = GET_PRIV (window);
197
198         /* The spacing between the button and the label. */
199         hbox = gtk_hbox_new (FALSE, 0);
200
201         event_box = gtk_event_box_new ();
202         gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
203
204         name_label = gtk_label_new (NULL);
205         gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
206
207         attr_list = pango_attr_list_new ();
208         attr = pango_attr_scale_new (1/1.2);
209         attr->start_index = 0;
210         attr->end_index = -1;
211         pango_attr_list_insert (attr_list, attr);
212         gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
213         pango_attr_list_unref (attr_list);
214
215         gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
216         gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
217         g_object_set_data (G_OBJECT (chat), "chat-window-tab-label", name_label);
218
219         status_image = gtk_image_new ();
220
221         /* Spacing between the icon and label. */
222         event_box_hbox = gtk_hbox_new (FALSE, 0);
223
224         gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
225         gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
226
227         g_object_set_data (G_OBJECT (chat), "chat-window-tab-image", status_image);
228         g_object_set_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget", event_box);
229
230         close_button = gtk_button_new ();
231         gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
232         g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
233
234         /* We don't want focus/keynav for the button to avoid clutter, and
235          * Ctrl-W works anyway.
236          */
237         GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_FOCUS);
238         GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_DEFAULT);
239
240         /* Set the name to make the special rc style match. */
241         gtk_widget_set_name (close_button, "empathy-close-button");
242
243         close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
244
245         gtk_container_add (GTK_CONTAINER (close_button), close_image);
246
247         gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
248         gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
249         gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
250
251         /* React to theme changes and also used to setup the initial size
252          * correctly.
253          */
254         g_signal_connect (close_button,
255                           "style-set",
256                           G_CALLBACK (chat_window_close_button_style_set_cb),
257                           chat);
258
259         g_signal_connect (close_button,
260                           "clicked",
261                           G_CALLBACK (chat_window_close_clicked_cb),
262                           chat);
263
264         gtk_widget_show_all (hbox);
265
266         return hbox;
267 }
268
269 static void
270 chat_window_update (EmpathyChatWindow *window)
271 {
272         EmpathyChatWindowPriv *priv = GET_PRIV (window);
273         gboolean               first_page;
274         gboolean               last_page;
275         gboolean               is_connected;
276         gint                   num_pages;
277         gint                   page_num;
278         gint                   i;
279         const gchar           *name;
280         guint                  n_chats;
281         GdkPixbuf             *icon;
282         EmpathyContact        *remote_contact;
283         gboolean               avatar_in_icon;
284         GtkWidget             *chat;
285         GtkWidget             *chat_close_button;
286
287         /* Get information */
288         page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
289         num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
290         first_page = (page_num == 0);
291         last_page = (page_num == (num_pages - 1));
292         is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
293         name = empathy_chat_get_name (priv->current_chat);
294         n_chats = g_list_length (priv->chats);
295
296         DEBUG ("Update window");
297
298         /* Update menu */
299         gtk_widget_set_sensitive (priv->menu_tabs_next, !last_page);
300         gtk_widget_set_sensitive (priv->menu_tabs_prev, !first_page);
301         gtk_widget_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
302         gtk_widget_set_sensitive (priv->menu_tabs_left, !first_page);
303         gtk_widget_set_sensitive (priv->menu_tabs_right, !last_page);
304         gtk_widget_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
305
306         /* Update window title */
307         if (n_chats == 1) {
308                 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
309         } else {
310                 gchar *title;
311
312                 title = g_strdup_printf (_("Conversations (%d)"), n_chats);
313                 gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
314                 g_free (title);
315         }
316
317         /* Update window icon */
318         if (priv->chats_new_msg) {
319                 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
320                                           EMPATHY_IMAGE_MESSAGE);
321         } else {
322                 empathy_conf_get_bool (empathy_conf_get (),
323                                        EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
324                                        &avatar_in_icon);
325
326                 if (n_chats == 1 && avatar_in_icon) {
327                         remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
328                         icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
329                         gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
330
331                         if (icon != NULL) {
332                                 g_object_unref (icon);
333                         }
334                 } else {
335                         gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
336                 }
337         }
338
339         if (num_pages == 1) {
340                 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
341                 chat_close_button = g_object_get_data (G_OBJECT (chat),
342                                 "chat-window-tab-close-button");
343                 gtk_widget_hide (chat_close_button);
344         } else {
345                 for (i=0; i<num_pages; i++) {
346                         chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
347                         chat_close_button = g_object_get_data (G_OBJECT (chat),
348                                         "chat-window-tab-close-button");
349                         gtk_widget_show (chat_close_button);
350                 }
351         }
352 }
353
354 static void
355 chat_window_update_chat_tab (EmpathyChat *chat)
356 {
357         EmpathyChatWindow     *window;
358         EmpathyChatWindowPriv *priv;
359         EmpathyContact        *remote_contact;
360         const gchar           *name;
361         McAccount             *account;
362         const gchar           *subject;
363         GtkWidget             *widget;
364         GString               *tooltip;
365         gchar                 *markup;
366         const gchar           *icon_name;
367
368         window = chat_window_find_chat (chat);
369         if (!window) {
370                 return;
371         }
372         priv = GET_PRIV (window);
373
374         /* Get information */
375         name = empathy_chat_get_name (chat);
376         account = empathy_chat_get_account (chat);
377         subject = empathy_chat_get_subject (chat);
378         remote_contact = empathy_chat_get_remote_contact (chat);
379
380         DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
381                 name, mc_account_get_unique_name (account), subject, remote_contact);
382
383         /* Update tab image */
384         if (g_list_find (priv->chats_new_msg, chat)) {
385                 icon_name = EMPATHY_IMAGE_MESSAGE;
386         }
387         else if (g_list_find (priv->chats_composing, chat)) {
388                 icon_name = EMPATHY_IMAGE_TYPING;
389         }
390         else if (remote_contact) {
391                 icon_name = empathy_icon_name_for_contact (remote_contact);
392         } else {
393                 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
394         }
395         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
396         gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
397
398         /* Update tab tooltip */
399         tooltip = g_string_new (NULL);
400
401         if (remote_contact) {
402                 markup = g_markup_printf_escaped ("<b>%s</b><small> (%s)</small>\n%s",
403                                                   empathy_contact_get_id (remote_contact),
404                                                   mc_account_get_display_name (account),
405                                                   empathy_contact_get_status (remote_contact));
406                 g_string_append (tooltip, markup);
407                 g_free (markup);
408         }
409         else {
410                 markup = g_markup_printf_escaped ("<b>%s</b><small>  (%s)</small>", name,
411                                                   mc_account_get_display_name (account));
412                 g_string_append (tooltip, markup);
413                 g_free (markup);
414         }
415
416         if (subject) {
417                 markup = g_markup_printf_escaped ("\n<b>%s</b> %s", _("Topic:"), subject);
418                 g_string_append (tooltip, markup);
419                 g_free (markup);
420         }
421         if (g_list_find (priv->chats_composing, chat)) {
422                 markup = g_markup_printf_escaped ("\n%s", _("Typing a message."));
423                 g_string_append (tooltip, markup);
424                 g_free (markup);
425         }
426
427         markup = g_string_free (tooltip, FALSE);
428         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
429         gtk_widget_set_tooltip_markup (widget, markup);
430         g_free (markup);
431
432         /* Update tab label */
433         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
434         gtk_label_set_text (GTK_LABEL (widget), name);
435
436         /* Update the window if it's the current chat */
437         if (priv->current_chat == chat) {
438                 chat_window_update (window);
439         }
440 }
441
442 static void
443 chat_window_chat_notify_cb (EmpathyChat *chat)
444 {
445         EmpathyContact *old_remote_contact;
446         EmpathyContact *remote_contact = NULL;
447
448         old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
449         remote_contact = empathy_chat_get_remote_contact (chat);
450
451         if (old_remote_contact != remote_contact) {
452                 /* The remote-contact associated with the chat changed, we need
453                  * to keep track of any change of that contact and update the
454                  * window each time. */
455                 if (remote_contact) {
456                         g_signal_connect_swapped (remote_contact, "notify",
457                                                   G_CALLBACK (chat_window_update_chat_tab),
458                                                   chat);
459                 }
460                 if (old_remote_contact) {
461                         g_signal_handlers_disconnect_by_func (old_remote_contact,
462                                                               chat_window_update_chat_tab,
463                                                               chat);
464                 }
465
466                 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
467                                    remote_contact);
468         }
469
470         chat_window_update_chat_tab (chat);
471 }
472
473 static void
474 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
475                                        EmpathySmiley        *smiley,
476                                        gpointer              window)
477 {
478         EmpathyChatWindowPriv *priv = GET_PRIV (window);
479         EmpathyChat           *chat;
480         GtkTextBuffer         *buffer;
481         GtkTextIter            iter;
482
483         chat = priv->current_chat;
484
485         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
486         gtk_text_buffer_get_end_iter (buffer, &iter);
487         gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
488 }
489
490 static void
491 chat_window_conv_activate_cb (GtkWidget         *menuitem,
492                               EmpathyChatWindow *window)
493 {
494         EmpathyChatWindowPriv *priv = GET_PRIV (window);
495         GtkWidget             *submenu = NULL;
496
497         /* Contact submenu */
498         submenu = empathy_chat_get_contact_menu (priv->current_chat);
499         if (submenu) {
500                 gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->menu_conv_contact),
501                                            submenu);
502                 gtk_widget_show (priv->menu_conv_contact);
503                 gtk_widget_show (submenu);
504         } else {
505                 gtk_widget_hide (priv->menu_conv_contact);
506         }
507
508         /* Favorite room menu */
509         if (empathy_chat_is_room (priv->current_chat)) {
510                 const gchar *room;
511                 McAccount   *account;
512                 gboolean     found;
513
514                 room = empathy_chat_get_id (priv->current_chat);
515                 account = empathy_chat_get_account (priv->current_chat);
516                 found = empathy_chatroom_manager_find (priv->chatroom_manager,
517                                                        account, room) != NULL;
518
519                 DEBUG ("This room %s favorite", found ? "is" : "is not");
520                 gtk_check_menu_item_set_active (
521                         GTK_CHECK_MENU_ITEM (priv->menu_conv_favorite), found);
522                 gtk_widget_show (priv->menu_conv_favorite);
523         } else {
524                 gtk_widget_hide (priv->menu_conv_favorite);
525         }
526 }
527
528 static void
529 chat_window_clear_activate_cb (GtkWidget        *menuitem,
530                                EmpathyChatWindow *window)
531 {
532         EmpathyChatWindowPriv *priv = GET_PRIV (window);
533
534         empathy_chat_clear (priv->current_chat);
535 }
536
537 static void
538 chat_window_favorite_toggled_cb (GtkCheckMenuItem  *menuitem,
539                                  EmpathyChatWindow *window)
540 {
541         EmpathyChatWindowPriv *priv = GET_PRIV (window);
542         gboolean               active;
543         McAccount             *account;
544         const gchar           *room;
545         EmpathyChatroom       *chatroom;
546
547         active = gtk_check_menu_item_get_active (menuitem);
548         account = empathy_chat_get_account (priv->current_chat);
549         room = empathy_chat_get_id (priv->current_chat);
550
551         chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
552                                                   account, room);
553
554         if (active && !chatroom) {
555                 const gchar *name;
556
557                 name = empathy_chat_get_name (priv->current_chat);
558                 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
559                 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
560                 g_object_unref (chatroom);
561                 return;
562         }
563         
564         if (!active && chatroom) {
565                 empathy_chatroom_manager_remove (priv->chatroom_manager, chatroom);
566         }
567 }
568
569 static const gchar *
570 chat_get_window_id_for_geometry (EmpathyChat *chat)
571 {
572         const gchar *res = NULL;
573         gboolean     separate_windows;
574
575         empathy_conf_get_bool (empathy_conf_get (),
576                                EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
577                                &separate_windows);
578
579         if (separate_windows) {
580                 res = empathy_chat_get_id (chat);
581         }
582
583         return res ? res : "chat-window";
584 }
585
586 static gboolean
587 chat_window_save_geometry_timeout_cb (EmpathyChatWindow *window)
588 {
589         EmpathyChatWindowPriv *priv;
590         gint                  x, y, w, h;
591
592         priv = GET_PRIV (window);
593
594         gtk_window_get_size (GTK_WINDOW (priv->dialog), &w, &h);
595         gtk_window_get_position (GTK_WINDOW (priv->dialog), &x, &y);
596
597         empathy_geometry_save (chat_get_window_id_for_geometry (priv->current_chat),
598                                x, y, w, h);
599
600         priv->save_geometry_id = 0;
601
602         return FALSE;
603 }
604
605 static gboolean
606 chat_window_configure_event_cb (GtkWidget         *widget,
607                                 GdkEventConfigure *event,
608                                 EmpathyChatWindow  *window)
609 {
610         EmpathyChatWindowPriv *priv;
611
612         priv = GET_PRIV (window);
613
614         if (priv->save_geometry_id != 0) {
615                 g_source_remove (priv->save_geometry_id);
616         }
617
618         priv->save_geometry_id =
619                 g_timeout_add_seconds (1,
620                                        (GSourceFunc) chat_window_save_geometry_timeout_cb,
621                                        window);
622
623         return FALSE;
624 }
625
626 static void
627 chat_window_close_activate_cb (GtkWidget        *menuitem,
628                                EmpathyChatWindow *window)
629 {
630         EmpathyChatWindowPriv *priv;
631
632         priv = GET_PRIV (window);
633
634         g_return_if_fail (priv->current_chat != NULL);
635
636         empathy_chat_window_remove_chat (window, priv->current_chat);
637 }
638
639 static void
640 chat_window_edit_activate_cb (GtkWidget        *menuitem,
641                               EmpathyChatWindow *window)
642 {
643         EmpathyChatWindowPriv *priv;
644         GtkClipboard         *clipboard;
645         GtkTextBuffer        *buffer;
646         gboolean              text_available;
647
648         priv = GET_PRIV (window);
649
650         g_return_if_fail (priv->current_chat != NULL);
651
652         if (!empathy_chat_get_tp_chat (priv->current_chat)) {
653                 gtk_widget_set_sensitive (priv->menu_edit_copy, FALSE);
654                 gtk_widget_set_sensitive (priv->menu_edit_cut, FALSE);
655                 gtk_widget_set_sensitive (priv->menu_edit_paste, FALSE);
656                 return;
657         }
658
659         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
660         if (gtk_text_buffer_get_has_selection (buffer)) {
661                 gtk_widget_set_sensitive (priv->menu_edit_copy, TRUE);
662                 gtk_widget_set_sensitive (priv->menu_edit_cut, TRUE);
663         } else {
664                 gboolean selection;
665
666                 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
667
668                 gtk_widget_set_sensitive (priv->menu_edit_cut, FALSE);
669                 gtk_widget_set_sensitive (priv->menu_edit_copy, selection);
670         }
671
672         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
673         text_available = gtk_clipboard_wait_is_text_available (clipboard);
674         gtk_widget_set_sensitive (priv->menu_edit_paste, text_available);
675 }
676
677 static void
678 chat_window_cut_activate_cb (GtkWidget        *menuitem,
679                              EmpathyChatWindow *window)
680 {
681         EmpathyChatWindowPriv *priv;
682
683         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
684
685         priv = GET_PRIV (window);
686
687         empathy_chat_cut (priv->current_chat);
688 }
689
690 static void
691 chat_window_copy_activate_cb (GtkWidget        *menuitem,
692                               EmpathyChatWindow *window)
693 {
694         EmpathyChatWindowPriv *priv;
695
696         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
697
698         priv = GET_PRIV (window);
699
700         empathy_chat_copy (priv->current_chat);
701 }
702
703 static void
704 chat_window_paste_activate_cb (GtkWidget        *menuitem,
705                                EmpathyChatWindow *window)
706 {
707         EmpathyChatWindowPriv *priv;
708
709         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
710
711         priv = GET_PRIV (window);
712
713         empathy_chat_paste (priv->current_chat);
714 }
715
716 static void
717 chat_window_tabs_left_activate_cb (GtkWidget        *menuitem,
718                                    EmpathyChatWindow *window)
719 {
720         EmpathyChatWindowPriv *priv;
721         EmpathyChat           *chat;
722         gint                  index;
723
724         priv = GET_PRIV (window);
725
726         chat = priv->current_chat;
727         index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
728         if (index <= 0) {
729                 return;
730         }
731
732         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
733                                     GTK_WIDGET (chat),
734                                     index - 1);
735 }
736
737 static void
738 chat_window_tabs_right_activate_cb (GtkWidget        *menuitem,
739                                     EmpathyChatWindow *window)
740 {
741         EmpathyChatWindowPriv *priv;
742         EmpathyChat           *chat;
743         gint                  index;
744
745         priv = GET_PRIV (window);
746
747         chat = priv->current_chat;
748         index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
749
750         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
751                                     GTK_WIDGET (chat),
752                                     index + 1);
753 }
754
755 static void
756 chat_window_detach_activate_cb (GtkWidget        *menuitem,
757                                 EmpathyChatWindow *window)
758 {
759         EmpathyChatWindowPriv *priv;
760         EmpathyChatWindow     *new_window;
761         EmpathyChat           *chat;
762
763         priv = GET_PRIV (window);
764
765         chat = priv->current_chat;
766         new_window = empathy_chat_window_new ();
767
768         empathy_chat_window_move_chat (window, new_window, chat);
769
770         priv = GET_PRIV (new_window);
771         gtk_widget_show (priv->dialog);
772 }
773
774 static void
775 chat_window_help_contents_cb (GtkWidget         *menuitem,
776                               EmpathyChatWindow *window)
777 {
778         empathy_url_show (menuitem, "ghelp:empathy?chat");
779 }
780
781 static void
782 chat_window_help_about_cb (GtkWidget         *menuitem,
783                            EmpathyChatWindow *window)
784 {
785         EmpathyChatWindowPriv *priv = GET_PRIV (window);
786
787         empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
788 }
789
790 static gboolean
791 chat_window_delete_event_cb (GtkWidget        *dialog,
792                              GdkEvent         *event,
793                              EmpathyChatWindow *window)
794 {
795         EmpathyChatWindowPriv *priv = GET_PRIV (window);
796
797         DEBUG ("Delete event received");
798
799         g_object_ref (window);
800         while (priv->chats) {
801                 empathy_chat_window_remove_chat (window, priv->chats->data);
802         }
803         g_object_unref (window);
804
805         return TRUE;
806 }
807
808 static void
809 chat_window_composing_cb (EmpathyChat       *chat,
810                           gboolean          is_composing,
811                           EmpathyChatWindow *window)
812 {
813         EmpathyChatWindowPriv *priv;
814
815         priv = GET_PRIV (window);
816
817         if (is_composing && !g_list_find (priv->chats_composing, chat)) {
818                 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
819         } else {
820                 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
821         }
822
823         chat_window_update_chat_tab (chat);
824 }
825
826 static void
827 chat_window_set_urgency_hint (EmpathyChatWindow *window,
828                               gboolean          urgent)
829 {
830         EmpathyChatWindowPriv *priv;
831
832         priv = GET_PRIV (window);
833
834         DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
835         gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
836 }
837
838 static gboolean
839 notification_closed_idle_cb (EmpathyChat *chat)
840 {
841         empathy_chat_window_present_chat (chat);
842
843         return FALSE;
844 }
845
846 static void
847 chat_window_notification_closed_cb (NotifyNotification *notify,
848                                     EmpathyChat *chat)
849 {
850         int reason;
851
852         reason = notify_notification_get_closed_reason (notify);
853
854         if (reason == 2) {
855                 empathy_chat_window_present_chat (chat);
856         }
857 }
858
859 static void
860 chat_window_show_or_update_notification (EmpathyMessage *message,
861                                          EmpathyChatWindow *window,
862                                          EmpathyChat *chat)
863 {
864         EmpathyContact *sender;
865         char *header;
866         const char *body;
867         GdkPixbuf *pixbuf;
868         EmpathyChatWindowPriv *priv = GET_PRIV (window);
869
870         if (!empathy_notification_should_show (TRUE)) {
871                 return;
872         }
873
874         sender = empathy_message_get_sender (message);
875         header = g_strdup_printf (_("New message from %s"),
876                                   empathy_contact_get_name (sender));
877         body = empathy_message_get_body (message);
878         pixbuf = empathy_pixbuf_avatar_from_contact_scaled (sender, 48, 48);
879         if (pixbuf == NULL) {
880                 /* we use INVALID here so we fall pack to 48px */
881                 pixbuf = empathy_pixbuf_from_icon_name (EMPATHY_IMAGE_NEW_MESSAGE,
882                                                         GTK_ICON_SIZE_INVALID);
883         }
884
885         if (priv->notification != NULL) {
886                 notify_notification_update (priv->notification,
887                                             header, body, NULL);
888                 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
889         } else {
890                 priv->notification = notify_notification_new (header, body, NULL, NULL);
891                 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
892                 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
893
894                 g_signal_connect (priv->notification, "closed",
895                                   G_CALLBACK (chat_window_notification_closed_cb), chat);
896         }
897
898         notify_notification_show (priv->notification, NULL);
899
900         g_object_unref (pixbuf);
901         g_free (header);
902 }
903
904 static void
905 chat_window_new_message_cb (EmpathyChat       *chat,
906                             EmpathyMessage    *message,
907                             EmpathyChatWindow *window)
908 {
909         EmpathyChatWindowPriv *priv;
910         gboolean              has_focus;
911         gboolean              needs_urgency;
912         EmpathyContact        *sender;
913
914         priv = GET_PRIV (window);
915
916         has_focus = empathy_chat_window_has_focus (window);
917
918         /* - if we're the sender, we play the sound if it's specified in the
919          *   preferences and we're not away.
920          * - if we receive a message, we play the sound if it's specified in the
921          *   prefereces and the window does not have focus on the chat receiving
922          *   the message.
923          */
924
925         sender = empathy_message_get_sender (message);
926
927         if (empathy_contact_is_user (sender) != FALSE) {
928                 empathy_sound_play (GTK_WIDGET (priv->dialog),
929                                     EMPATHY_SOUND_MESSAGE_OUTGOING);
930         } else {
931                 if ((!has_focus || priv->current_chat != chat)) {
932                         empathy_sound_play (GTK_WIDGET (priv->dialog),
933                                             EMPATHY_SOUND_MESSAGE_INCOMING);
934                 }
935         }
936
937         if (!has_focus) {
938                 chat_window_show_or_update_notification (message, window, chat);
939         }
940
941         if (has_focus && priv->current_chat == chat) {
942                 return;
943         }
944         
945         if (empathy_chat_get_members_count (chat) > 2) {
946                 needs_urgency = empathy_message_should_highlight (message);
947         } else {
948                 needs_urgency = TRUE;
949         }
950
951         if (needs_urgency && !has_focus) {
952                 chat_window_set_urgency_hint (window, TRUE);
953         }
954
955         if (!g_list_find (priv->chats_new_msg, chat)) {
956                 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
957                 chat_window_update_chat_tab (chat);
958         }
959 }
960
961 static GtkNotebook *
962 chat_window_detach_hook (GtkNotebook *source,
963                          GtkWidget   *page,
964                          gint         x,
965                          gint         y,
966                          gpointer     user_data)
967 {
968         EmpathyChatWindowPriv *priv;
969         EmpathyChatWindow     *window, *new_window;
970         EmpathyChat           *chat;
971
972         chat = EMPATHY_CHAT (page);
973         window = chat_window_find_chat (chat);
974
975         new_window = empathy_chat_window_new ();
976         priv = GET_PRIV (new_window);
977
978         DEBUG ("Detach hook called");
979
980         empathy_chat_window_move_chat (window, new_window, chat);
981
982         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
983         gtk_widget_show (priv->dialog);
984
985         return NULL;
986 }
987
988 static void
989 chat_window_page_switched_cb (GtkNotebook      *notebook,
990                               GtkNotebookPage  *page,
991                               gint              page_num,
992                               EmpathyChatWindow *window)
993 {
994         EmpathyChatWindowPriv *priv;
995         EmpathyChat           *chat;
996         GtkWidget            *child;
997
998         DEBUG ("Page switched");
999
1000         priv = GET_PRIV (window);
1001
1002         child = gtk_notebook_get_nth_page (notebook, page_num);
1003         chat = EMPATHY_CHAT (child);
1004
1005         if (priv->page_added) {
1006                 priv->page_added = FALSE;
1007                 empathy_chat_scroll_down (chat);
1008         }
1009         else if (priv->current_chat == chat) {
1010                 return;
1011         }
1012
1013         priv->current_chat = chat;
1014         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1015
1016         chat_window_update_chat_tab (chat);
1017 }
1018
1019 static void
1020 chat_window_page_added_cb (GtkNotebook      *notebook,
1021                            GtkWidget        *child,
1022                            guint             page_num,
1023                            EmpathyChatWindow *window)
1024 {
1025         EmpathyChatWindowPriv *priv;
1026         EmpathyChat           *chat;
1027
1028         priv = GET_PRIV (window);
1029
1030         /* If we just received DND to the same window, we don't want
1031          * to do anything here like removing the tab and then readding
1032          * it, so we return here and in "page-added".
1033          */
1034         if (priv->dnd_same_window) {
1035                 DEBUG ("Page added (back to the same window)");
1036                 priv->dnd_same_window = FALSE;
1037                 return;
1038         }
1039
1040         DEBUG ("Page added");
1041
1042         /* Get chat object */
1043         chat = EMPATHY_CHAT (child);
1044
1045         /* Connect chat signals for this window */
1046         g_signal_connect (chat, "composing",
1047                           G_CALLBACK (chat_window_composing_cb),
1048                           window);
1049         g_signal_connect (chat, "new-message",
1050                           G_CALLBACK (chat_window_new_message_cb),
1051                           window);
1052
1053         /* Set flag so we know to perform some special operations on
1054          * switch page due to the new page being added.
1055          */
1056         priv->page_added = TRUE;
1057
1058         /* Get list of chats up to date */
1059         priv->chats = g_list_append (priv->chats, chat);
1060
1061         chat_window_update_chat_tab (chat);
1062 }
1063
1064 static void
1065 chat_window_page_removed_cb (GtkNotebook      *notebook,
1066                              GtkWidget        *child,
1067                              guint             page_num,
1068                              EmpathyChatWindow *window)
1069 {
1070         EmpathyChatWindowPriv *priv;
1071         EmpathyChat           *chat;
1072
1073         priv = GET_PRIV (window);
1074
1075         /* If we just received DND to the same window, we don't want
1076          * to do anything here like removing the tab and then readding
1077          * it, so we return here and in "page-added".
1078          */
1079         if (priv->dnd_same_window) {
1080                 DEBUG ("Page removed (and will be readded to same window)");
1081                 return;
1082         }
1083
1084         DEBUG ("Page removed");
1085
1086         /* Get chat object */
1087         chat = EMPATHY_CHAT (child);
1088
1089         /* Disconnect all signal handlers for this chat and this window */
1090         g_signal_handlers_disconnect_by_func (chat,
1091                                               G_CALLBACK (chat_window_composing_cb),
1092                                               window);
1093         g_signal_handlers_disconnect_by_func (chat,
1094                                               G_CALLBACK (chat_window_new_message_cb),
1095                                               window);
1096
1097         /* Keep list of chats up to date */
1098         priv->chats = g_list_remove (priv->chats, chat);
1099         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1100         priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1101
1102         if (priv->chats == NULL) {
1103                 g_object_unref (window);
1104         } else {
1105                 chat_window_update (window);
1106         }
1107 }
1108
1109 static gboolean
1110 chat_window_focus_in_event_cb (GtkWidget        *widget,
1111                                GdkEvent         *event,
1112                                EmpathyChatWindow *window)
1113 {
1114         EmpathyChatWindowPriv *priv;
1115
1116         DEBUG ("Focus in event, updating title");
1117
1118         priv = GET_PRIV (window);
1119
1120         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1121
1122         chat_window_set_urgency_hint (window, FALSE);
1123         
1124         /* Update the title, since we now mark all unread messages as read. */
1125         chat_window_update_chat_tab (priv->current_chat);
1126
1127         return FALSE;
1128 }
1129
1130 static void
1131 chat_window_drag_data_received (GtkWidget        *widget,
1132                                 GdkDragContext   *context,
1133                                 int               x,
1134                                 int               y,
1135                                 GtkSelectionData *selection,
1136                                 guint             info,
1137                                 guint             time,
1138                                 EmpathyChatWindow *window)
1139 {
1140         if (info == DND_DRAG_TYPE_CONTACT_ID) {
1141                 EmpathyChat           *chat;
1142                 EmpathyChatWindow     *old_window;
1143                 McAccount             *account;
1144                 const gchar           *id;
1145                 gchar                **strv;
1146
1147                 id = (const gchar*) selection->data;
1148
1149                 DEBUG ("DND contact from roster with id:'%s'", id);
1150                 
1151                 strv = g_strsplit (id, "/", 2);
1152                 account = mc_account_lookup (strv[0]);
1153                 chat = empathy_chat_window_find_chat (account, strv[1]);
1154
1155                 if (!chat) {
1156                         empathy_dispatcher_chat_with_contact_id (account, strv[2], NULL, NULL);
1157                         g_object_unref (account);
1158                         g_strfreev (strv);
1159                         return;
1160                 }
1161                 g_object_unref (account);
1162                 g_strfreev (strv);
1163
1164                 old_window = chat_window_find_chat (chat);              
1165                 if (old_window) {
1166                         if (old_window == window) {
1167                                 gtk_drag_finish (context, TRUE, FALSE, time);
1168                                 return;
1169                         }
1170                         
1171                         empathy_chat_window_move_chat (old_window, window, chat);
1172                 } else {
1173                         empathy_chat_window_add_chat (window, chat);
1174                 }
1175                 
1176                 /* Added to take care of any outstanding chat events */
1177                 empathy_chat_window_present_chat (chat);
1178
1179                 /* We should return TRUE to remove the data when doing
1180                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1181                  * weird consequences, and we handle that internally
1182                  * anyway with add_chat() and remove_chat().
1183                  */
1184                 gtk_drag_finish (context, TRUE, FALSE, time);
1185         }
1186         else if (info == DND_DRAG_TYPE_TAB) {
1187                 EmpathyChat        **chat;
1188                 EmpathyChatWindow   *old_window = NULL;
1189
1190                 DEBUG ("DND tab");
1191
1192                 chat = (void*) selection->data;
1193                 old_window = chat_window_find_chat (*chat);
1194
1195                 if (old_window) {
1196                         EmpathyChatWindowPriv *priv;
1197
1198                         priv = GET_PRIV (window);
1199
1200                         if (old_window == window) {
1201                                 DEBUG ("DND tab (within same window)");
1202                                 priv->dnd_same_window = TRUE;
1203                                 gtk_drag_finish (context, TRUE, FALSE, time);
1204                                 return;
1205                         }
1206                         
1207                         priv->dnd_same_window = FALSE;
1208                 }
1209
1210                 /* We should return TRUE to remove the data when doing
1211                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1212                  * weird consequences, and we handle that internally
1213                  * anyway with add_chat() and remove_chat().
1214                  */
1215                 gtk_drag_finish (context, TRUE, FALSE, time);
1216         } else {
1217                 DEBUG ("DND from unknown source");
1218                 gtk_drag_finish (context, FALSE, FALSE, time);
1219         }
1220 }
1221
1222 static void
1223 chat_window_finalize (GObject *object)
1224 {
1225         EmpathyChatWindow     *window;
1226         EmpathyChatWindowPriv *priv;
1227
1228         window = EMPATHY_CHAT_WINDOW (object);
1229         priv = GET_PRIV (window);
1230
1231         DEBUG ("Finalized: %p", object);
1232
1233         g_object_unref (priv->chatroom_manager);
1234         if (priv->save_geometry_id != 0) {
1235                 g_source_remove (priv->save_geometry_id);
1236         }
1237
1238         if (priv->notification != NULL) {
1239                 notify_notification_close (priv->notification, NULL);
1240                 g_object_unref (priv->notification);
1241                 priv->notification = NULL;
1242         }
1243
1244         chat_windows = g_list_remove (chat_windows, window);
1245         gtk_widget_destroy (priv->dialog);
1246
1247         G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1248 }
1249
1250 static void
1251 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1252 {
1253         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1254
1255         object_class->finalize = chat_window_finalize;
1256
1257         g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1258
1259         /* Set up a style for the close button with no focus padding. */
1260         gtk_rc_parse_string (
1261                 "style \"empathy-close-button-style\"\n"
1262                 "{\n"
1263                 "  GtkWidget::focus-padding = 0\n"
1264                 "  xthickness = 0\n"
1265                 "  ythickness = 0\n"
1266                 "}\n"
1267                 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1268
1269         gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1270 }
1271
1272 static void
1273 empathy_chat_window_init (EmpathyChatWindow *window)
1274 {
1275         GladeXML              *glade;
1276         GtkAccelGroup         *accel_group;
1277         GClosure              *closure;
1278         GtkWidget             *menu_conv;
1279         GtkWidget             *menu;
1280         gint                   i;
1281         GtkWidget             *chat_vbox;
1282         gchar                 *filename;
1283         EmpathySmileyManager  *smiley_manager;
1284         EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1285                 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1286
1287         window->priv = priv;
1288         filename = empathy_file_lookup ("empathy-chat-window.glade", "src");
1289         glade = empathy_glade_get_file (filename,
1290                                        "chat_window",
1291                                        NULL,
1292                                        "chat_window", &priv->dialog,
1293                                        "chat_vbox", &chat_vbox,
1294                                        "menu_conv", &menu_conv,
1295                                        "menu_conv_clear", &priv->menu_conv_clear,
1296                                        "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1297                                        "menu_conv_contact", &priv->menu_conv_contact,
1298                                        "menu_conv_favorite", &priv->menu_conv_favorite,
1299                                        "menu_conv_close", &priv->menu_conv_close,
1300                                        "menu_edit_cut", &priv->menu_edit_cut,
1301                                        "menu_edit_copy", &priv->menu_edit_copy,
1302                                        "menu_edit_paste", &priv->menu_edit_paste,
1303                                        "menu_tabs_next", &priv->menu_tabs_next,
1304                                        "menu_tabs_prev", &priv->menu_tabs_prev,
1305                                        "menu_tabs_left", &priv->menu_tabs_left,
1306                                        "menu_tabs_right", &priv->menu_tabs_right,
1307                                        "menu_tabs_detach", &priv->menu_tabs_detach,
1308                                        "menu_help_contents", &priv->menu_help_contents,
1309                                        "menu_help_about", &priv->menu_help_about,
1310                                        NULL);
1311         g_free (filename);
1312
1313         empathy_glade_connect (glade,
1314                               window,
1315                               "chat_window", "configure-event", chat_window_configure_event_cb,
1316                               "menu_conv", "activate", chat_window_conv_activate_cb,
1317                               "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1318                               "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1319                               "menu_conv_close", "activate", chat_window_close_activate_cb,
1320                               "menu_edit", "activate", chat_window_edit_activate_cb,
1321                               "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1322                               "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1323                               "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1324                               "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1325                               "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1326                               "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1327                               "menu_help_contents", "activate", chat_window_help_contents_cb,
1328                               "menu_help_about", "activate", chat_window_help_about_cb,
1329                               NULL);
1330
1331         g_object_unref (glade);
1332
1333         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1334
1335         priv->notebook = gtk_notebook_new ();
1336         gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow"); 
1337         gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1338         gtk_widget_show (priv->notebook);
1339
1340         /* Set up accels */
1341         accel_group = gtk_accel_group_new ();
1342         gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1343
1344         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1345                 closure =  g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1346                                            window,
1347                                            NULL);
1348                 gtk_accel_group_connect (accel_group,
1349                                          tab_accel_keys[i],
1350                                          GDK_MOD1_MASK,
1351                                          0,
1352                                          closure);
1353         }
1354
1355         g_object_unref (accel_group);
1356
1357         /* Set up smiley menu */
1358         smiley_manager = empathy_smiley_manager_dup_singleton ();
1359         menu = empathy_smiley_menu_new (smiley_manager,
1360                                         chat_window_insert_smiley_activate_cb,
1361                                         window);
1362         gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->menu_conv_insert_smiley),
1363                                    menu);
1364         g_object_unref (smiley_manager);
1365
1366         /* Set up signals we can't do with glade since we may need to
1367          * block/unblock them at some later stage.
1368          */
1369
1370         g_signal_connect (priv->dialog,
1371                           "delete_event",
1372                           G_CALLBACK (chat_window_delete_event_cb),
1373                           window);
1374
1375         g_signal_connect_swapped (priv->menu_tabs_prev,
1376                                   "activate",
1377                                   G_CALLBACK (gtk_notebook_prev_page),
1378                                   priv->notebook);
1379         g_signal_connect_swapped (priv->menu_tabs_next,
1380                                   "activate",
1381                                   G_CALLBACK (gtk_notebook_next_page),
1382                                   priv->notebook);
1383
1384         g_signal_connect (priv->dialog,
1385                           "focus_in_event",
1386                           G_CALLBACK (chat_window_focus_in_event_cb),
1387                           window);
1388         g_signal_connect_after (priv->notebook,
1389                                 "switch_page",
1390                                 G_CALLBACK (chat_window_page_switched_cb),
1391                                 window);
1392         g_signal_connect (priv->notebook,
1393                           "page_added",
1394                           G_CALLBACK (chat_window_page_added_cb),
1395                           window);
1396         g_signal_connect (priv->notebook,
1397                           "page_removed",
1398                           G_CALLBACK (chat_window_page_removed_cb),
1399                           window);
1400
1401         /* Set up drag and drop */
1402         gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1403                            GTK_DEST_DEFAULT_ALL,
1404                            drag_types_dest,
1405                            G_N_ELEMENTS (drag_types_dest),
1406                            GDK_ACTION_MOVE);
1407
1408         g_signal_connect (priv->notebook,
1409                           "drag-data-received",
1410                           G_CALLBACK (chat_window_drag_data_received),
1411                           window);
1412
1413         chat_windows = g_list_prepend (chat_windows, window);
1414
1415         /* Set up private details */
1416         priv->chats = NULL;
1417         priv->chats_new_msg = NULL;
1418         priv->chats_composing = NULL;
1419         priv->current_chat = NULL;
1420 }
1421
1422 EmpathyChatWindow *
1423 empathy_chat_window_new (void)
1424 {
1425         return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1426 }
1427
1428 /* Returns the window to open a new tab in if there is only one window
1429  * visble, otherwise, returns NULL indicating that a new window should
1430  * be added.
1431  */
1432 EmpathyChatWindow *
1433 empathy_chat_window_get_default (void)
1434 {
1435         GList    *l;
1436         gboolean  separate_windows = TRUE;
1437
1438         empathy_conf_get_bool (empathy_conf_get (),
1439                               EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1440                               &separate_windows);
1441
1442         if (separate_windows) {
1443                 /* Always create a new window */
1444                 return NULL;
1445         }
1446
1447         for (l = chat_windows; l; l = l->next) {
1448                 EmpathyChatWindow *chat_window;
1449                 GtkWidget         *dialog;
1450
1451                 chat_window = l->data;
1452
1453                 dialog = empathy_chat_window_get_dialog (chat_window);
1454                 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1455                         /* Found a visible window on this desktop */
1456                         return chat_window;
1457                 }
1458         }
1459
1460         return NULL;
1461 }
1462
1463 GtkWidget *
1464 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1465 {
1466         EmpathyChatWindowPriv *priv;
1467
1468         g_return_val_if_fail (window != NULL, NULL);
1469
1470         priv = GET_PRIV (window);
1471
1472         return priv->dialog;
1473 }
1474
1475 void
1476 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1477                               EmpathyChat       *chat)
1478 {
1479         EmpathyChatWindowPriv *priv;
1480         GtkWidget             *label;
1481         GtkWidget             *child;
1482         gint                   x, y, w, h;
1483
1484         g_return_if_fail (window != NULL);
1485         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1486
1487         priv = GET_PRIV (window);
1488
1489         /* Reference the chat object */
1490         g_object_ref (chat);
1491
1492         /* If this window has just been created, position it */
1493         if (priv->chats == NULL) {
1494                 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1495                 
1496                 if (x >= 0 && y >= 0) {
1497                         /* Let the window manager position it if we don't have
1498                          * good x, y coordinates.
1499                          */
1500                         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1501                 }
1502                 
1503                 if (w > 0 && h > 0) {
1504                         /* Use the defaults from the glade file if we don't have
1505                          * good w, h geometry.
1506                          */
1507                         gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1508                 }
1509         }
1510
1511         child = GTK_WIDGET (chat);
1512         label = chat_window_create_label (window, chat); 
1513         gtk_widget_show (child);
1514
1515         g_signal_connect (chat, "notify::name",
1516                           G_CALLBACK (chat_window_chat_notify_cb),
1517                           NULL);
1518         g_signal_connect (chat, "notify::subject",
1519                           G_CALLBACK (chat_window_chat_notify_cb),
1520                           NULL);
1521         g_signal_connect (chat, "notify::remote-contact",
1522                           G_CALLBACK (chat_window_chat_notify_cb),
1523                           NULL);
1524         chat_window_chat_notify_cb (chat);
1525
1526         gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), child, label);
1527         gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1528         gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1529         gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1530                                             TRUE, TRUE, GTK_PACK_START); 
1531
1532         DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1533 }
1534
1535 void
1536 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1537                                  EmpathyChat       *chat)
1538 {
1539         EmpathyChatWindowPriv *priv;
1540         gint                   position;
1541         EmpathyContact        *remote_contact;
1542
1543         g_return_if_fail (window != NULL);
1544         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1545
1546         priv = GET_PRIV (window);
1547
1548         g_signal_handlers_disconnect_by_func (chat,
1549                                               chat_window_chat_notify_cb,
1550                                               NULL);
1551         remote_contact = g_object_get_data (G_OBJECT (chat),
1552                                             "chat-window-remote-contact");
1553         if (remote_contact) {
1554                 g_signal_handlers_disconnect_by_func (remote_contact,
1555                                                       chat_window_update_chat_tab,
1556                                                       chat);
1557         }
1558
1559         position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1560                                           GTK_WIDGET (chat));
1561         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1562
1563         DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1564
1565         g_object_unref (chat);
1566 }
1567
1568 void
1569 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1570                                EmpathyChatWindow *new_window,
1571                                EmpathyChat       *chat)
1572 {
1573         GtkWidget *widget;
1574
1575         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1576         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1577         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1578
1579         widget = GTK_WIDGET (chat);
1580
1581         DEBUG ("Chat moving with widget:%p (%d references)", widget,
1582                 G_OBJECT (widget)->ref_count);
1583
1584         /* We reference here to make sure we don't loose the widget
1585          * and the EmpathyChat object during the move.
1586          */
1587         g_object_ref (chat);
1588         g_object_ref (widget);
1589
1590         empathy_chat_window_remove_chat (old_window, chat);
1591         empathy_chat_window_add_chat (new_window, chat);
1592
1593         g_object_unref (widget);
1594         g_object_unref (chat);
1595 }
1596
1597 void
1598 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1599                                     EmpathyChat       *chat)
1600 {
1601         EmpathyChatWindowPriv *priv;
1602         gint                  page_num;
1603
1604         g_return_if_fail (window != NULL);
1605         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1606
1607         priv = GET_PRIV (window);
1608
1609         page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1610                                           GTK_WIDGET (chat));
1611         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1612                                        page_num);
1613 }
1614
1615 gboolean
1616 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1617 {
1618         EmpathyChatWindowPriv *priv;
1619         gboolean              has_focus;
1620
1621         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1622
1623         priv = GET_PRIV (window);
1624
1625         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1626
1627         return has_focus;
1628 }
1629
1630 EmpathyChat *
1631 empathy_chat_window_find_chat (McAccount   *account,
1632                                const gchar *id)
1633 {
1634         GList *l;
1635
1636         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1637         g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1638
1639         for (l = chat_windows; l; l = l->next) {
1640                 EmpathyChatWindowPriv *priv;
1641                 EmpathyChatWindow     *window;
1642                 GList                *ll;
1643
1644                 window = l->data;
1645                 priv = GET_PRIV (window);
1646
1647                 for (ll = priv->chats; ll; ll = ll->next) {
1648                         EmpathyChat *chat;
1649
1650                         chat = ll->data;
1651
1652                         if (empathy_account_equal (account, empathy_chat_get_account (chat)) &&
1653                             !tp_strdiff (id, empathy_chat_get_id (chat))) {
1654                                 return chat;
1655                         }
1656                 }
1657         }
1658
1659         return NULL;
1660 }
1661
1662 void
1663 empathy_chat_window_present_chat (EmpathyChat *chat)
1664 {
1665         EmpathyChatWindow     *window;
1666         EmpathyChatWindowPriv *priv;
1667
1668         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1669
1670         window = chat_window_find_chat (chat);
1671
1672         /* If the chat has no window, create one */
1673         if (window == NULL) {
1674                 window = empathy_chat_window_get_default ();
1675                 if (!window) {
1676                         window = empathy_chat_window_new ();
1677                 }
1678
1679                 empathy_chat_window_add_chat (window, chat);
1680         }
1681
1682         priv = GET_PRIV (window);
1683         empathy_chat_window_switch_to_chat (window, chat);
1684         empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1685
1686         gtk_widget_grab_focus (chat->input_text_view); 
1687 }
1688
1689 #if 0
1690 static gboolean
1691 chat_window_should_play_sound (EmpathyChatWindow *window)
1692 {
1693         EmpathyChatWindowPriv *priv = GET_PRIV (window);
1694         gboolean               has_focus = FALSE;
1695
1696         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1697
1698         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1699
1700         return !has_focus;
1701 }
1702 #endif