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