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