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