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