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