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