]> git.0d.be Git - empathy.git/blob - src/empathy-chat-window.c
Fix load/save chat geometry
[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 const gchar *
562 chat_get_window_id_for_geometry (EmpathyChat *chat)
563 {
564         gboolean separate_windows;
565
566         empathy_conf_get_bool (empathy_conf_get (),
567                                EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
568                                &separate_windows);
569
570         if (separate_windows) {
571                 return empathy_chat_get_id (chat);
572         } else {
573                 return "chat-window";
574         }
575 }
576
577 static gboolean
578 chat_window_save_geometry_timeout_cb (EmpathyChatWindow *window)
579 {
580         EmpathyChatWindowPriv *priv;
581         gint                  x, y, w, h;
582
583         priv = GET_PRIV (window);
584
585         gtk_window_get_size (GTK_WINDOW (priv->dialog), &w, &h);
586         gtk_window_get_position (GTK_WINDOW (priv->dialog), &x, &y);
587
588         empathy_geometry_save (chat_get_window_id_for_geometry (priv->current_chat),
589                                x, y, w, h);
590
591         priv->save_geometry_id = 0;
592
593         return FALSE;
594 }
595
596 static gboolean
597 chat_window_configure_event_cb (GtkWidget         *widget,
598                                 GdkEventConfigure *event,
599                                 EmpathyChatWindow  *window)
600 {
601         EmpathyChatWindowPriv *priv;
602
603         priv = GET_PRIV (window);
604
605         if (priv->save_geometry_id != 0) {
606                 g_source_remove (priv->save_geometry_id);
607         }
608
609         priv->save_geometry_id =
610                 g_timeout_add_seconds (1,
611                                        (GSourceFunc) chat_window_save_geometry_timeout_cb,
612                                        window);
613
614         return FALSE;
615 }
616
617 static void
618 chat_window_conv_activate_cb (GtkWidget         *menuitem,
619                               EmpathyChatWindow *window)
620 {
621         EmpathyChatWindowPriv *priv;
622         EmpathyLogManager     *manager;
623         gboolean              log_exists = FALSE;
624         gboolean              can_voip = FALSE;
625
626         priv = GET_PRIV (window);
627
628         manager = empathy_log_manager_new ();
629         log_exists = empathy_log_manager_exists (manager,
630                                                  empathy_chat_get_account (priv->current_chat),
631                                                  empathy_chat_get_id (priv->current_chat),
632                                                  empathy_chat_is_group_chat (priv->current_chat));
633         g_object_unref (manager);
634
635         if (!empathy_chat_is_group_chat (priv->current_chat)) {
636                 EmpathyPrivateChat *chat;
637                 EmpathyContact     *contact;
638
639                 chat = EMPATHY_PRIVATE_CHAT (priv->current_chat);
640                 contact = empathy_private_chat_get_contact (chat);
641                 can_voip = empathy_contact_can_voip (contact);
642         }
643
644         gtk_widget_set_sensitive (priv->menu_conv_log, log_exists);
645 #ifdef HAVE_VOIP
646         gtk_widget_set_sensitive (priv->menu_conv_call, can_voip);
647 #else 
648         g_object_set (priv->menu_conv_call, "visible", FALSE, NULL);
649         g_object_set (priv->menu_conv_call_separator, "visible", FALSE, NULL);
650 #endif
651 }
652
653 static void
654 chat_window_show_contacts_toggled_cb (GtkWidget        *menuitem,
655                                       EmpathyChatWindow *window)
656 {
657         EmpathyChatWindowPriv *priv;
658         gboolean              show;
659
660         priv = GET_PRIV (window);
661
662         g_return_if_fail (priv->current_chat != NULL);
663
664         show = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (priv->menu_room_show_contacts));
665         empathy_group_chat_set_show_contacts (EMPATHY_GROUP_CHAT (priv->current_chat), show);
666 }
667
668 static void
669 chat_window_close_activate_cb (GtkWidget        *menuitem,
670                                EmpathyChatWindow *window)
671 {
672         EmpathyChatWindowPriv *priv;
673
674         priv = GET_PRIV (window);
675
676         g_return_if_fail (priv->current_chat != NULL);
677
678         empathy_chat_window_remove_chat (window, priv->current_chat);
679 }
680
681 static void
682 chat_window_room_set_topic_activate_cb (GtkWidget        *menuitem,
683                                         EmpathyChatWindow *window)
684 {
685         EmpathyChatWindowPriv *priv = GET_PRIV (window);
686         
687         if (empathy_chat_is_group_chat (priv->current_chat)) {
688                 empathy_group_chat_set_topic (EMPATHY_GROUP_CHAT (priv->current_chat));
689         }
690 }
691
692 static void
693 chat_window_room_join_new_activate_cb (GtkWidget        *menuitem,
694                                        EmpathyChatWindow *window)
695 {
696         EmpathyChatWindowPriv *priv;
697
698         priv = GET_PRIV (window);
699
700         empathy_new_chatroom_dialog_show (GTK_WINDOW (priv->dialog));
701 }
702
703 static void
704 chat_window_room_invite_activate_cb (GtkWidget        *menuitem,
705                                      EmpathyChatWindow *window)
706 {
707 /* FIXME:
708         EmpathyChatWindowPriv *priv;
709         EmpathyContact        *own_contact;
710         EmpathyChatroomId      id = 0;
711
712         priv = GET_PRIV (window);
713         own_contact = empathy_chat_get_own_contact (priv->current_chat);
714
715         if (empathy_chat_is_group_chat (priv->current_chat)) {
716                 EmpathyGroupChat *group_chat;
717
718                 group_chat = EMPATHY_GROUP_CHAT (priv->current_chat);
719                 id = empathy_group_chat_get_chatroom_id (group_chat);
720         }
721
722         empathy_chat_invite_dialog_show (own_contact, id);
723 */
724 }
725
726 static void
727 chat_window_room_add_activate_cb (GtkWidget        *menuitem,
728                                   EmpathyChatWindow *window)
729 {
730         EmpathyChatWindowPriv  *priv;
731         EmpathyChatroomManager *manager;
732         EmpathyChatroom        *chatroom;
733
734         priv = GET_PRIV (window);
735
736         g_return_if_fail (priv->current_chat != NULL);
737
738         if (!empathy_chat_is_group_chat (priv->current_chat)) {
739                 return;
740         }
741
742         chatroom = empathy_chatroom_new_full (empathy_chat_get_account (priv->current_chat),
743                                               empathy_chat_get_id (priv->current_chat),
744                                               empathy_chat_get_name (priv->current_chat),
745                                               FALSE);
746
747         manager = empathy_chatroom_manager_new ();
748         empathy_chatroom_manager_add (manager, chatroom);
749         chat_window_update_menu (window);
750
751         g_object_unref (chatroom);
752         g_object_unref (manager);
753 }
754
755 static void
756 chat_window_edit_activate_cb (GtkWidget        *menuitem,
757                               EmpathyChatWindow *window)
758 {
759         EmpathyChatWindowPriv *priv;
760         GtkClipboard         *clipboard;
761         GtkTextBuffer        *buffer;
762         gboolean              text_available;
763
764         priv = GET_PRIV (window);
765
766         g_return_if_fail (priv->current_chat != NULL);
767
768         if (!empathy_chat_is_connected (priv->current_chat)) {
769                 gtk_widget_set_sensitive (priv->menu_edit_copy, FALSE);
770                 gtk_widget_set_sensitive (priv->menu_edit_cut, FALSE);
771                 gtk_widget_set_sensitive (priv->menu_edit_paste, FALSE);
772                 return;
773         }
774
775         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
776         if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
777                 gtk_widget_set_sensitive (priv->menu_edit_copy, TRUE);
778                 gtk_widget_set_sensitive (priv->menu_edit_cut, TRUE);
779         } else {
780                 gboolean selection;
781
782                 selection = empathy_chat_view_get_selection_bounds (priv->current_chat->view, 
783                                                                    NULL, NULL);
784
785                 gtk_widget_set_sensitive (priv->menu_edit_cut, FALSE);
786                 gtk_widget_set_sensitive (priv->menu_edit_copy, selection);
787         }
788
789         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
790         text_available = gtk_clipboard_wait_is_text_available (clipboard);
791         gtk_widget_set_sensitive (priv->menu_edit_paste, text_available);
792 }
793
794 static void
795 chat_window_cut_activate_cb (GtkWidget        *menuitem,
796                              EmpathyChatWindow *window)
797 {
798         EmpathyChatWindowPriv *priv;
799
800         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
801
802         priv = GET_PRIV (window);
803
804         empathy_chat_cut (priv->current_chat);
805 }
806
807 static void
808 chat_window_copy_activate_cb (GtkWidget        *menuitem,
809                               EmpathyChatWindow *window)
810 {
811         EmpathyChatWindowPriv *priv;
812
813         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
814
815         priv = GET_PRIV (window);
816
817         empathy_chat_copy (priv->current_chat);
818 }
819
820 static void
821 chat_window_paste_activate_cb (GtkWidget        *menuitem,
822                                EmpathyChatWindow *window)
823 {
824         EmpathyChatWindowPriv *priv;
825
826         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
827
828         priv = GET_PRIV (window);
829
830         empathy_chat_paste (priv->current_chat);
831 }
832
833 static void
834 chat_window_tabs_left_activate_cb (GtkWidget        *menuitem,
835                                    EmpathyChatWindow *window)
836 {
837         EmpathyChatWindowPriv *priv;
838         EmpathyChat           *chat;
839         gint                  index;
840
841         priv = GET_PRIV (window);
842
843         chat = priv->current_chat;
844         index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
845         if (index <= 0) {
846                 return;
847         }
848
849         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
850                                     empathy_chat_get_widget (chat),
851                                     index - 1);
852
853         chat_window_update_menu (window);
854         chat_window_update_status (window, chat);
855 }
856
857 static void
858 chat_window_tabs_right_activate_cb (GtkWidget        *menuitem,
859                                     EmpathyChatWindow *window)
860 {
861         EmpathyChatWindowPriv *priv;
862         EmpathyChat           *chat;
863         gint                  index;
864
865         priv = GET_PRIV (window);
866
867         chat = priv->current_chat;
868         index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
869
870         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
871                                     empathy_chat_get_widget (chat),
872                                     index + 1);
873
874         chat_window_update_menu (window);
875         chat_window_update_status (window, chat);
876 }
877
878 static void
879 chat_window_detach_activate_cb (GtkWidget        *menuitem,
880                                 EmpathyChatWindow *window)
881 {
882         EmpathyChatWindowPriv *priv;
883         EmpathyChatWindow     *new_window;
884         EmpathyChat           *chat;
885
886         priv = GET_PRIV (window);
887
888         chat = priv->current_chat;
889         new_window = empathy_chat_window_new ();
890
891         empathy_chat_window_move_chat (window, new_window, chat);
892
893         priv = GET_PRIV (new_window);
894         gtk_widget_show (priv->dialog);
895 }
896
897 static void
898 chat_window_help_contents_cb (GtkWidget         *menuitem,
899                               EmpathyChatWindow *window)
900 {
901         //empathy_help_show ();
902 }
903
904 static void
905 chat_window_help_about_cb (GtkWidget         *menuitem,
906                            EmpathyChatWindow *window)
907 {
908         EmpathyChatWindowPriv *priv = GET_PRIV (window);
909
910         empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
911 }
912
913 static gboolean
914 chat_window_delete_event_cb (GtkWidget        *dialog,
915                              GdkEvent         *event,
916                              EmpathyChatWindow *window)
917 {
918         EmpathyChatWindowPriv *priv;
919         GList                *list;
920         GList                *l;
921
922         priv = GET_PRIV (window);
923
924         empathy_debug (DEBUG_DOMAIN, "Delete event received");
925
926         list = g_list_copy (priv->chats);
927
928         for (l = list; l; l = l->next) {
929                 empathy_chat_window_remove_chat (window, l->data);
930         }
931
932         g_list_free (list);
933
934         return TRUE;
935 }
936
937 static void
938 chat_window_status_changed_cb (EmpathyChat       *chat,
939                                EmpathyChatWindow *window)
940 {
941         chat_window_update_menu (window);
942         chat_window_update_status (window, chat);
943 }
944
945 static void
946 chat_window_name_changed_cb (EmpathyChat       *chat,
947                              const gchar      *name,
948                              EmpathyChatWindow *window)
949 {
950         GtkLabel *label;
951
952         label = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
953
954         gtk_label_set_text (label, name);
955 }
956
957 static void
958 chat_window_composing_cb (EmpathyChat       *chat,
959                           gboolean          is_composing,
960                           EmpathyChatWindow *window)
961 {
962         EmpathyChatWindowPriv *priv;
963
964         priv = GET_PRIV (window);
965
966         if (is_composing && !g_list_find (priv->chats_composing, chat)) {
967                 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
968         } else {
969                 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
970         }
971
972         chat_window_update_status (window, chat);
973 }
974
975 static void
976 chat_window_new_message_cb (EmpathyChat       *chat,
977                             EmpathyMessage    *message,
978 s                           EmpathyChatWindow *window)
979 {
980         EmpathyChatWindowPriv *priv;
981         gboolean              has_focus;
982         gboolean              needs_urgency;
983
984         priv = GET_PRIV (window);
985
986         has_focus = empathy_chat_window_has_focus (window);
987         
988         if (has_focus && priv->current_chat == chat) {
989                 return;
990         }
991         
992         needs_urgency = (empathy_chat_is_group_chat (chat) &&
993                          empathy_message_should_highlight (message));
994
995         if (needs_urgency && !has_focus) {
996                 chat_window_set_urgency_hint (window, TRUE);
997         }
998
999         if (!g_list_find (priv->chats_new_msg, chat)) {
1000                 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1001                 chat_window_update_status (window, chat);
1002         }
1003 }
1004
1005 static GtkNotebook *
1006 chat_window_detach_hook (GtkNotebook *source,
1007                          GtkWidget   *page,
1008                          gint         x,
1009                          gint         y,
1010                          gpointer     user_data)
1011 {
1012         EmpathyChatWindowPriv *priv;
1013         EmpathyChatWindow     *window, *new_window;
1014         EmpathyChat           *chat;
1015
1016         chat = g_object_get_data (G_OBJECT (page), "chat");
1017         window = chat_window_find_chat (chat);
1018
1019         new_window = empathy_chat_window_new ();
1020         priv = GET_PRIV (new_window);
1021
1022         empathy_debug (DEBUG_DOMAIN, "Detach hook called");
1023
1024         empathy_chat_window_move_chat (window, new_window, chat);
1025
1026         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1027         gtk_widget_show (priv->dialog);
1028
1029         return NULL;
1030 }
1031
1032 static void
1033 chat_window_page_switched_cb (GtkNotebook      *notebook,
1034                               GtkNotebookPage  *page,
1035                               gint              page_num,
1036                               EmpathyChatWindow *window)
1037 {
1038         EmpathyChatWindowPriv *priv;
1039         EmpathyChat           *chat;
1040         GtkWidget            *child;
1041
1042         empathy_debug (DEBUG_DOMAIN, "Page switched");
1043
1044         priv = GET_PRIV (window);
1045
1046         child = gtk_notebook_get_nth_page (notebook, page_num);
1047         chat = g_object_get_data (G_OBJECT (child), "chat");
1048
1049         if (priv->page_added) {
1050                 priv->page_added = FALSE;
1051                 empathy_chat_scroll_down (chat);
1052         }
1053         else if (priv->current_chat == chat) {
1054                 return;
1055         }
1056
1057         priv->current_chat = chat;
1058         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1059
1060         chat_window_update_menu (window);
1061         chat_window_update_status (window, chat);
1062 }
1063
1064 static void
1065 chat_window_page_reordered_cb (GtkNotebook      *notebook,
1066                                GtkWidget        *widget,
1067                                guint             page_num,
1068                                EmpathyChatWindow *window)
1069 {
1070         empathy_debug (DEBUG_DOMAIN, "Page reordered");
1071         
1072         chat_window_update_menu (window);
1073 }
1074
1075 static void
1076 chat_window_page_added_cb (GtkNotebook      *notebook,
1077                            GtkWidget        *child,
1078                            guint             page_num,
1079                            EmpathyChatWindow *window)
1080 {
1081         EmpathyChatWindowPriv *priv;
1082         EmpathyChat           *chat;
1083
1084         priv = GET_PRIV (window);
1085
1086         /* If we just received DND to the same window, we don't want
1087          * to do anything here like removing the tab and then readding
1088          * it, so we return here and in "page-added".
1089          */
1090         if (priv->dnd_same_window) {
1091                 empathy_debug (DEBUG_DOMAIN, "Page added (back to the same window)");
1092                 priv->dnd_same_window = FALSE;
1093                 return;
1094         }
1095
1096         empathy_debug (DEBUG_DOMAIN, "Page added");
1097
1098         /* Get chat object */
1099         chat = g_object_get_data (G_OBJECT (child), "chat");
1100
1101         /* Connect chat signals for this window */
1102         g_signal_connect (chat, "status-changed",
1103                           G_CALLBACK (chat_window_status_changed_cb),
1104                           window);
1105         g_signal_connect (chat, "name-changed",
1106                           G_CALLBACK (chat_window_name_changed_cb),
1107                           window);
1108         g_signal_connect (chat, "composing",
1109                           G_CALLBACK (chat_window_composing_cb),
1110                           window);
1111         g_signal_connect (chat, "new-message",
1112                           G_CALLBACK (chat_window_new_message_cb),
1113                           window);
1114
1115         /* Set flag so we know to perform some special operations on
1116          * switch page due to the new page being added.
1117          */
1118         priv->page_added = TRUE;
1119
1120         /* Get list of chats up to date */
1121         priv->chats = g_list_append (priv->chats, chat);
1122 }
1123
1124 static void
1125 chat_window_page_removed_cb (GtkNotebook      *notebook,
1126                              GtkWidget        *child,
1127                              guint             page_num,
1128                              EmpathyChatWindow *window)
1129 {
1130         EmpathyChatWindowPriv *priv;
1131         EmpathyChat           *chat;
1132
1133         priv = GET_PRIV (window);
1134
1135         /* If we just received DND to the same window, we don't want
1136          * to do anything here like removing the tab and then readding
1137          * it, so we return here and in "page-added".
1138          */
1139         if (priv->dnd_same_window) {
1140                 empathy_debug (DEBUG_DOMAIN, "Page removed (and will be readded to same window)");
1141                 return;
1142         }
1143
1144         empathy_debug (DEBUG_DOMAIN, "Page removed");
1145
1146         /* Get chat object */
1147         chat = g_object_get_data (G_OBJECT (child), "chat");
1148
1149         /* Disconnect all signal handlers for this chat and this window */
1150         g_signal_handlers_disconnect_by_func (chat,
1151                                               G_CALLBACK (chat_window_status_changed_cb),
1152                                               window);
1153         g_signal_handlers_disconnect_by_func (chat,
1154                                               G_CALLBACK (chat_window_name_changed_cb),
1155                                               window);
1156         g_signal_handlers_disconnect_by_func (chat,
1157                                               G_CALLBACK (chat_window_composing_cb),
1158                                               window);
1159         g_signal_handlers_disconnect_by_func (chat,
1160                                               G_CALLBACK (chat_window_new_message_cb),
1161                                               window);
1162
1163         /* Keep list of chats up to date */
1164         priv->chats = g_list_remove (priv->chats, chat);
1165         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1166         priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1167
1168         if (priv->chats == NULL) {
1169                 g_object_unref (window);
1170         } else {
1171                 chat_window_update_menu (window);
1172                 chat_window_update_title (window, NULL);
1173         }
1174 }
1175
1176 static gboolean
1177 chat_window_focus_in_event_cb (GtkWidget        *widget,
1178                                GdkEvent         *event,
1179                                EmpathyChatWindow *window)
1180 {
1181         EmpathyChatWindowPriv *priv;
1182
1183         empathy_debug (DEBUG_DOMAIN, "Focus in event, updating title");
1184
1185         priv = GET_PRIV (window);
1186
1187         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1188
1189         chat_window_set_urgency_hint (window, FALSE);
1190         
1191         /* Update the title, since we now mark all unread messages as read. */
1192         chat_window_update_status (window, priv->current_chat);
1193
1194         return FALSE;
1195 }
1196
1197 static void
1198 chat_window_drag_data_received (GtkWidget        *widget,
1199                                 GdkDragContext   *context,
1200                                 int               x,
1201                                 int               y,
1202                                 GtkSelectionData *selection,
1203                                 guint             info,
1204                                 guint             time,
1205                                 EmpathyChatWindow *window)
1206 {
1207         if (info == DND_DRAG_TYPE_CONTACT_ID) {
1208                 EmpathyChat           *chat;
1209                 EmpathyChatWindow     *old_window;
1210                 McAccount             *account;
1211                 const gchar           *id = NULL;
1212                 gchar                **strv;
1213
1214                 if (selection) {
1215                         id = (const gchar*) selection->data;
1216                 }
1217
1218                 empathy_debug (DEBUG_DOMAIN, "DND contact from roster with id:'%s'", id);
1219                 
1220                 strv = g_strsplit (id, "/", 2);
1221                 account = mc_account_lookup (strv[0]);
1222                 chat = empathy_chat_window_find_chat (account, strv[1]);
1223
1224                 if (!chat) {
1225                         empathy_chat_with_contact_id (account, strv[2]);
1226                         g_object_unref (account);
1227                         g_strfreev (strv);
1228                         return;
1229                 }
1230                 g_object_unref (account);
1231                 g_strfreev (strv);
1232
1233                 old_window = chat_window_find_chat (chat);              
1234                 if (old_window) {
1235                         if (old_window == window) {
1236                                 gtk_drag_finish (context, TRUE, FALSE, time);
1237                                 return;
1238                         }
1239                         
1240                         empathy_chat_window_move_chat (old_window, window, chat);
1241                 } else {
1242                         empathy_chat_window_add_chat (window, chat);
1243                 }
1244                 
1245                 /* Added to take care of any outstanding chat events */
1246                 empathy_chat_window_present_chat (chat);
1247
1248                 /* We should return TRUE to remove the data when doing
1249                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1250                  * weird consequences, and we handle that internally
1251                  * anyway with add_chat() and remove_chat().
1252                  */
1253                 gtk_drag_finish (context, TRUE, FALSE, time);
1254         }
1255         else if (info == DND_DRAG_TYPE_TAB) {
1256                 EmpathyChat        *chat = NULL;
1257                 EmpathyChatWindow  *old_window;
1258                 GtkWidget        **child = NULL;
1259
1260                 empathy_debug (DEBUG_DOMAIN, "DND tab");
1261
1262                 if (selection) {
1263                         child = (void*) selection->data;
1264                 }
1265
1266                 if (child) {
1267                         chat = g_object_get_data (G_OBJECT (*child), "chat");
1268                 }
1269
1270                 old_window = chat_window_find_chat (chat);
1271                 if (old_window) {
1272                         EmpathyChatWindowPriv *priv;
1273
1274                         priv = GET_PRIV (window);
1275
1276                         if (old_window == window) {
1277                                 empathy_debug (DEBUG_DOMAIN, "DND tab (within same window)");
1278                                 priv->dnd_same_window = TRUE;
1279                                 gtk_drag_finish (context, TRUE, FALSE, time);
1280                                 return;
1281                         }
1282                         
1283                         priv->dnd_same_window = FALSE;
1284                 }
1285
1286                 /* We should return TRUE to remove the data when doing
1287                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1288                  * weird consequences, and we handle that internally
1289                  * anyway with add_chat() and remove_chat().
1290                  */
1291                 gtk_drag_finish (context, TRUE, FALSE, time);
1292         } else {
1293                 empathy_debug (DEBUG_DOMAIN, "DND from unknown source");
1294                 gtk_drag_finish (context, FALSE, FALSE, time);
1295         }
1296 }
1297
1298 static void
1299 chat_window_set_urgency_hint (EmpathyChatWindow *window,
1300                               gboolean          urgent)
1301 {
1302         EmpathyChatWindowPriv *priv;
1303
1304         priv = GET_PRIV (window);
1305
1306         empathy_debug (DEBUG_DOMAIN, "Turning %s urgency hint",
1307                        urgent ? "on" : "off");
1308         gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
1309 }
1310
1311 static void
1312 chat_window_finalize (GObject *object)
1313 {
1314         EmpathyChatWindow     *window;
1315         EmpathyChatWindowPriv *priv;
1316
1317         window = EMPATHY_CHAT_WINDOW (object);
1318         priv = GET_PRIV (window);
1319
1320         empathy_debug (DEBUG_DOMAIN, "Finalized: %p", object);
1321
1322         if (priv->save_geometry_id != 0) {
1323                 g_source_remove (priv->save_geometry_id);
1324         }
1325
1326         chat_windows = g_list_remove (chat_windows, window);
1327         gtk_widget_destroy (priv->dialog);
1328
1329         g_signal_handlers_disconnect_by_func (priv->chatroom_manager,
1330                                               chat_window_update_menu,
1331                                               window);
1332         g_object_unref (priv->chatroom_manager);
1333
1334         G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1335 }
1336
1337 static void
1338 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1339 {
1340         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1341
1342         object_class->finalize = empathy_chat_window_finalize;
1343
1344         g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1345
1346         /* Set up a style for the close button with no focus padding. */
1347         gtk_rc_parse_string (
1348                 "style \"empathy-close-button-style\"\n"
1349                 "{\n"
1350                 "  GtkWidget::focus-padding = 0\n"
1351                 "  xthickness = 0\n"
1352                 "  ythickness = 0\n"
1353                 "}\n"
1354                 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1355
1356         gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1357 }
1358
1359 static void
1360 empathy_chat_window_init (EmpathyChatWindow *window)
1361 {
1362         EmpathyChatWindowPriv *priv;
1363         GladeXML             *glade;
1364         GtkAccelGroup        *accel_group;
1365         GtkWidget            *image;
1366         GClosure             *closure;
1367         GtkWidget            *menu_conv;
1368         GtkWidget            *menu;
1369         gint                  i;
1370         GtkWidget            *chat_vbox;
1371         gchar                *filename;
1372
1373         priv = GET_PRIV (window);
1374
1375         filename = empathy_file_lookup ("empathy-chat-window.glade", "src");
1376         glade = empathy_glade_get_file (filename,
1377                                        "chat_window",
1378                                        NULL,
1379                                        "chat_window", &priv->dialog,
1380                                        "chat_vbox", &chat_vbox,
1381                                        "menu_conv", &menu_conv,
1382                                        "menu_conv_clear", &priv->menu_conv_clear,
1383                                        "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1384                                        "menu_conv_call", &priv->menu_conv_call,
1385                                        "menu_conv_call_separator", &priv->menu_conv_call_separator,
1386                                        "menu_conv_log", &priv->menu_conv_log,
1387                                        "menu_conv_separator", &priv->menu_conv_separator,
1388                                        "menu_conv_add_contact", &priv->menu_conv_add_contact,
1389                                        "menu_conv_info", &priv->menu_conv_info,
1390                                        "menu_conv_close", &priv->menu_conv_close,
1391                                        "menu_room", &priv->menu_room,
1392                                        "menu_room_set_topic", &priv->menu_room_set_topic,
1393                                        "menu_room_join_new", &priv->menu_room_join_new,
1394                                        "menu_room_invite", &priv->menu_room_invite,
1395                                        "menu_room_add", &priv->menu_room_add,
1396                                        "menu_room_show_contacts", &priv->menu_room_show_contacts,
1397                                        "menu_edit_cut", &priv->menu_edit_cut,
1398                                        "menu_edit_copy", &priv->menu_edit_copy,
1399                                        "menu_edit_paste", &priv->menu_edit_paste,
1400                                        "menu_tabs_next", &priv->menu_tabs_next,
1401                                        "menu_tabs_prev", &priv->menu_tabs_prev,
1402                                        "menu_tabs_left", &priv->menu_tabs_left,
1403                                        "menu_tabs_right", &priv->menu_tabs_right,
1404                                        "menu_tabs_detach", &priv->menu_tabs_detach,
1405                                        "menu_help_contents", &priv->menu_help_contents,
1406                                        "menu_help_about", &priv->menu_help_about,
1407                                        NULL);
1408         g_free (filename);
1409
1410         empathy_glade_connect (glade,
1411                               window,
1412                               "chat_window", "configure-event", chat_window_configure_event_cb,
1413                               "menu_conv", "activate", chat_window_conv_activate_cb,
1414                               "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1415                               "menu_conv_call", "activate", chat_window_call_activate_cb,
1416                               "menu_conv_log", "activate", chat_window_log_activate_cb,
1417                               "menu_conv_add_contact", "activate", chat_window_add_contact_activate_cb,
1418                               "menu_conv_info", "activate", chat_window_info_activate_cb,
1419                               "menu_conv_close", "activate", chat_window_close_activate_cb,
1420                               "menu_room_set_topic", "activate", chat_window_room_set_topic_activate_cb,
1421                               "menu_room_join_new", "activate", chat_window_room_join_new_activate_cb,
1422                               "menu_room_invite", "activate", chat_window_room_invite_activate_cb,
1423                               "menu_room_add", "activate", chat_window_room_add_activate_cb,
1424                               "menu_edit", "activate", chat_window_edit_activate_cb,
1425                               "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1426                               "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1427                               "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1428                               "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1429                               "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1430                               "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1431                               "menu_help_contents", "activate", chat_window_help_contents_cb,
1432                               "menu_help_about", "activate", chat_window_help_about_cb,
1433                               NULL);
1434
1435         g_object_unref (glade);
1436
1437         /* Set up chatroom manager */
1438         priv->chatroom_manager = empathy_chatroom_manager_new ();
1439         g_signal_connect_swapped (priv->chatroom_manager, "chatroom-added",
1440                                   G_CALLBACK (chat_window_update_menu),
1441                                   window);
1442         g_signal_connect_swapped (priv->chatroom_manager, "chatroom-removed",
1443                                   G_CALLBACK (chat_window_update_menu),
1444                                   window);
1445
1446         priv->notebook = gtk_notebook_new ();
1447         gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow"); 
1448         gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1449         gtk_widget_show (priv->notebook);
1450
1451         /* Set up accels */
1452         accel_group = gtk_accel_group_new ();
1453         gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1454
1455         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1456                 closure =  g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1457                                            window,
1458                                            NULL);
1459                 gtk_accel_group_connect (accel_group,
1460                                          tab_accel_keys[i],
1461                                          GDK_MOD1_MASK,
1462                                          0,
1463                                          closure);
1464         }
1465
1466         g_object_unref (accel_group);
1467
1468         /* Set the contact information menu item image to the Empathy
1469          * stock image
1470          */
1471         image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (priv->menu_conv_info));
1472         gtk_image_set_from_icon_name (GTK_IMAGE (image),
1473                                       EMPATHY_IMAGE_CONTACT_INFORMATION,
1474                                       GTK_ICON_SIZE_MENU);
1475
1476         /* Set up smiley menu */
1477         menu = empathy_chat_view_get_smiley_menu (
1478                 G_CALLBACK (chat_window_insert_smiley_activate_cb),
1479                 window);
1480         gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->menu_conv_insert_smiley), menu);
1481
1482         /* Set up signals we can't do with glade since we may need to
1483          * block/unblock them at some later stage.
1484          */
1485
1486         g_signal_connect (priv->dialog,
1487                           "delete_event",
1488                           G_CALLBACK (chat_window_delete_event_cb),
1489                           window);
1490
1491         g_signal_connect (priv->menu_room_show_contacts,
1492                           "toggled",
1493                           G_CALLBACK (chat_window_show_contacts_toggled_cb),
1494                           window);
1495
1496         g_signal_connect_swapped (priv->menu_tabs_prev,
1497                                   "activate",
1498                                   G_CALLBACK (gtk_notebook_prev_page),
1499                                   priv->notebook);
1500         g_signal_connect_swapped (priv->menu_tabs_next,
1501                                   "activate",
1502                                   G_CALLBACK (gtk_notebook_next_page),
1503                                   priv->notebook);
1504
1505         g_signal_connect (priv->dialog,
1506                           "focus_in_event",
1507                           G_CALLBACK (chat_window_focus_in_event_cb),
1508                           window);
1509         g_signal_connect_after (priv->notebook,
1510                                 "switch_page",
1511                                 G_CALLBACK (chat_window_page_switched_cb),
1512                                 window);
1513         g_signal_connect (priv->notebook,
1514                           "page_reordered",
1515                           G_CALLBACK (chat_window_page_reordered_cb),
1516                           window);
1517         g_signal_connect (priv->notebook,
1518                           "page_added",
1519                           G_CALLBACK (chat_window_page_added_cb),
1520                           window);
1521         g_signal_connect (priv->notebook,
1522                           "page_removed",
1523                           G_CALLBACK (chat_window_page_removed_cb),
1524                           window);
1525
1526         /* Set up drag and drop */
1527         gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1528                            GTK_DEST_DEFAULT_ALL,
1529                            drag_types_dest,
1530                            G_N_ELEMENTS (drag_types_dest),
1531                            GDK_ACTION_MOVE);
1532
1533         g_signal_connect (priv->notebook,
1534                           "drag-data-received",
1535                           G_CALLBACK (chat_window_drag_data_received),
1536                           window);
1537
1538         chat_windows = g_list_prepend (chat_windows, window);
1539
1540         /* Set up private details */
1541         priv->chats = NULL;
1542         priv->chats_new_msg = NULL;
1543         priv->chats_composing = NULL;
1544         priv->current_chat = NULL;
1545 }
1546
1547 EmpathyChatWindow *
1548 empathy_chat_window_new (void)
1549 {
1550         return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1551 }
1552
1553 /* Returns the window to open a new tab in if there is only one window
1554  * visble, otherwise, returns NULL indicating that a new window should
1555  * be added.
1556  */
1557 EmpathyChatWindow *
1558 empathy_chat_window_get_default (void)
1559 {
1560         GList    *l;
1561         gboolean  separate_windows = TRUE;
1562
1563         empathy_conf_get_bool (empathy_conf_get (),
1564                               EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1565                               &separate_windows);
1566
1567         if (separate_windows) {
1568                 /* Always create a new window */
1569                 return NULL;
1570         }
1571
1572         for (l = chat_windows; l; l = l->next) {
1573                 EmpathyChatWindow *chat_window;
1574                 GtkWidget         *dialog;
1575
1576                 chat_window = l->data;
1577
1578                 dialog = empathy_chat_window_get_dialog (chat_window);
1579                 if (empathy_window_get_is_visible (GTK_WINDOW (GTK_WINDOW (dialog)))) {
1580                         /* Found a visible window on this desktop */
1581                         return chat_window;
1582                 }
1583         }
1584
1585         return NULL;
1586 }
1587
1588 GtkWidget *
1589 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1590 {
1591         EmpathyChatWindowPriv *priv;
1592
1593         g_return_val_if_fail (window != NULL, NULL);
1594
1595         priv = GET_PRIV (window);
1596
1597         return priv->dialog;
1598 }
1599
1600 void
1601 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1602                               EmpathyChat       *chat)
1603 {
1604         EmpathyChatWindowPriv *priv;
1605         GtkWidget             *label;
1606         GtkWidget             *child;
1607         gint                   x, y, w, h;
1608
1609         g_return_if_fail (window != NULL);
1610         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1611
1612         priv = GET_PRIV (window);
1613
1614         /* Reference the chat object */
1615         g_object_ref (chat);
1616
1617         empathy_geometry_load (chat_get_window_id_for_geometry (chat), x, y, w, h);
1618
1619         if (x >= 0 && y >= 0) {
1620                 /* Let the window manager position it if we don't have
1621                  * good x, y coordinates.
1622                  */
1623                 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1624         }
1625
1626         if (w > 0 && h > 0) {
1627                 /* Use the defaults from the glade file if we don't have
1628                  * good w, h geometry.
1629                  */
1630                 gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1631         }
1632
1633         child = empathy_chat_get_widget (chat);
1634         label = chat_window_create_label (window, chat); 
1635
1636         gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), child, label);
1637         gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1638         gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1639         gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1640                                             TRUE, TRUE, GTK_PACK_START); 
1641
1642         empathy_debug (DEBUG_DOMAIN, 
1643                       "Chat added (%d references)",
1644                       G_OBJECT (chat)->ref_count);
1645 }
1646
1647 void
1648 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1649                                  EmpathyChat       *chat)
1650 {
1651         EmpathyChatWindowPriv *priv;
1652         gint                  position;
1653
1654         g_return_if_fail (window != NULL);
1655         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1656
1657         priv = GET_PRIV (window);
1658
1659         position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1660                                           empathy_chat_get_widget (chat));
1661         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1662
1663         empathy_debug (DEBUG_DOMAIN, 
1664                       "Chat removed (%d references)", 
1665                       G_OBJECT (chat)->ref_count - 1);
1666
1667         g_object_unref (chat);
1668 }
1669
1670 void
1671 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1672                                EmpathyChatWindow *new_window,
1673                                EmpathyChat       *chat)
1674 {
1675         GtkWidget *widget;
1676
1677         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1678         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1679         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1680
1681         widget = empathy_chat_get_widget (chat);
1682
1683         empathy_debug (DEBUG_DOMAIN,
1684                       "Chat moving with widget:%p (%d references)", 
1685                       widget,
1686                       G_OBJECT (widget)->ref_count);
1687
1688         /* We reference here to make sure we don't loose the widget
1689          * and the EmpathyChat object during the move.
1690          */
1691         g_object_ref (chat);
1692         g_object_ref (widget);
1693
1694         empathy_chat_window_remove_chat (old_window, chat);
1695         empathy_chat_window_add_chat (new_window, chat);
1696
1697         g_object_unref (widget);
1698         g_object_unref (chat);
1699 }
1700
1701 void
1702 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1703                                     EmpathyChat       *chat)
1704 {
1705         EmpathyChatWindowPriv *priv;
1706         gint                  page_num;
1707
1708         g_return_if_fail (window != NULL);
1709         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1710
1711         priv = GET_PRIV (window);
1712
1713         page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1714                                           empathy_chat_get_widget (chat));
1715         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1716                                        page_num);
1717 }
1718
1719 gboolean
1720 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1721 {
1722         EmpathyChatWindowPriv *priv;
1723         gboolean              has_focus;
1724
1725         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1726
1727         priv = GET_PRIV (window);
1728
1729         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1730
1731         return has_focus;
1732 }
1733
1734 EmpathyChat *
1735 empathy_chat_window_find_chat (McAccount   *account,
1736                                const gchar *id)
1737 {
1738         GList *l;
1739
1740         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1741         g_return_val_if_fail (!G_STR_EMPTY (id), NULL);
1742
1743         for (l = chat_windows; l; l = l->next) {
1744                 EmpathyChatWindowPriv *priv;
1745                 EmpathyChatWindow     *window;
1746                 GList                *ll;
1747
1748                 window = l->data;
1749                 priv = GET_PRIV (window);
1750
1751                 for (ll = priv->chats; ll; ll = ll->next) {
1752                         EmpathyChat *chat;
1753
1754                         chat = ll->data;
1755
1756                         if (empathy_account_equal (account, empathy_chat_get_account (chat)) &&
1757                             strcmp (id, empathy_chat_get_id (chat)) == 0) {
1758                                 return chat;
1759                         }
1760                 }
1761         }
1762
1763         return NULL;
1764 }
1765
1766 void
1767 empathy_chat_window_present_chat (EmpathyChat *chat)
1768 {
1769         EmpathyChatWindow     *window;
1770         EmpathyChatWindowPriv *priv;
1771
1772         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1773
1774         window = chat_window_find_chat (chat);
1775
1776         /* If the chat has no window, create one */
1777         if (window == NULL) {
1778                 window = empathy_chat_window_get_default ();
1779                 if (!window) {
1780                         window = empathy_chat_window_new ();
1781                 }
1782
1783                 empathy_chat_window_add_chat (window, chat);
1784         }
1785
1786         priv = GET_PRIV (window);
1787         empathy_chat_window_switch_to_chat (window, chat);
1788         empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1789
1790         gtk_widget_grab_focus (chat->input_text_view); 
1791 }
1792
1793
1794 static gboolean
1795 chat_should_play_sound (EmpathyChat *chat)
1796 {
1797         EmpathyChatPriv *priv = GET_PRIV (chat);
1798         GtkWindow       *window;
1799         gboolean         has_focus = FALSE;
1800
1801         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1802
1803         window = empathy_get_toplevel_window (priv->widget);
1804         if (window) {
1805                 g_object_get (window, "has-toplevel-focus", &has_focus, NULL);
1806         }
1807
1808         return !has_focus;
1809 }
1810