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