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