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