]> git.0d.be Git - empathy.git/blob - src/empathy-chat-window.c
Don't include "Available"/"Away"... in tooltips
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  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 <glib/gi18n.h>
35 #include <libnotify/notification.h>
36
37 #include <telepathy-glib/util.h>
38 #include <libmissioncontrol/mission-control.h>
39
40 #include <libempathy/empathy-contact.h>
41 #include <libempathy/empathy-message.h>
42 #include <libempathy/empathy-dispatcher.h>
43 #include <libempathy/empathy-chatroom-manager.h>
44 #include <libempathy/empathy-account-manager.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-contact-dialogs.h>
50 #include <libempathy-gtk/empathy-log-window.h>
51 #include <libempathy-gtk/empathy-geometry.h>
52 #include <libempathy-gtk/empathy-smiley-manager.h>
53 #include <libempathy-gtk/empathy-ui-utils.h>
54
55 #include "empathy-chat-window.h"
56 #include "empathy-about-dialog.h"
57 #include "empathy-misc.h"
58
59 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
60 #include <libempathy/empathy-debug.h>
61
62 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatWindow)
63 typedef struct {
64         EmpathyChat *current_chat;
65         GList       *chats;
66         GList       *chats_new_msg;
67         GList       *chats_composing;
68         gboolean     page_added;
69         gboolean     dnd_same_window;
70         guint        save_geometry_id;
71         EmpathyChatroomManager *chatroom_manager;
72         GtkWidget   *dialog;
73         GtkWidget   *notebook;
74         NotifyNotification *notification;
75
76         /* Menu items. */
77         GtkUIManager *ui_manager;
78         GtkAction   *menu_conv_insert_smiley;
79         GtkAction   *menu_conv_favorite;
80
81         GtkAction   *menu_edit_cut;
82         GtkAction   *menu_edit_copy;
83         GtkAction   *menu_edit_paste;
84
85         GtkAction   *menu_tabs_next;
86         GtkAction   *menu_tabs_prev;
87         GtkAction   *menu_tabs_left;
88         GtkAction   *menu_tabs_right;
89         GtkAction   *menu_tabs_detach;
90 } EmpathyChatWindowPriv;
91
92 static GList *chat_windows = NULL;
93
94 static const guint tab_accel_keys[] = {
95         GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
96         GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
97 };
98
99 typedef enum {
100         DND_DRAG_TYPE_CONTACT_ID,
101         DND_DRAG_TYPE_TAB
102 } DndDragType;
103
104 static const GtkTargetEntry drag_types_dest[] = {
105         { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
106         { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
107 };
108
109 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
110
111 static void
112 chat_window_accel_cb (GtkAccelGroup    *accelgroup,
113                       GObject          *object,
114                       guint             key,
115                       GdkModifierType   mod,
116                       EmpathyChatWindow *window)
117 {
118         EmpathyChatWindowPriv *priv;
119         gint                  num = -1;
120         gint                  i;
121
122         priv = GET_PRIV (window);
123
124         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
125                 if (tab_accel_keys[i] == key) {
126                         num = i;
127                         break;
128                 }
129         }
130
131         if (num != -1) {
132                 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
133         }
134 }
135
136 static EmpathyChatWindow *
137 chat_window_find_chat (EmpathyChat *chat)
138 {
139         EmpathyChatWindowPriv *priv;
140         GList                 *l, *ll;
141
142         for (l = chat_windows; l; l = l->next) {
143                 priv = GET_PRIV (l->data);
144                 ll = g_list_find (priv->chats, chat);
145                 if (ll) {
146                         return l->data;
147                 }
148         }
149
150         return NULL;
151 }
152
153 static void
154 chat_window_close_clicked_cb (GtkAction   *action,
155                               EmpathyChat *chat)
156 {
157         EmpathyChatWindow *window;
158
159         window = chat_window_find_chat (chat);
160         empathy_chat_window_remove_chat (window, chat);
161 }
162
163 static void
164 chat_tab_style_set_cb (GtkWidget *hbox,
165                                        GtkStyle  *previous_style,
166                                        gpointer   user_data)
167 {
168         GtkWidget *button;
169         int char_width, h, w;
170         PangoContext *context;
171         PangoFontMetrics *metrics;
172
173         button = g_object_get_data (G_OBJECT (user_data),
174                 "chat-window-tab-close-button");
175         context = gtk_widget_get_pango_context (hbox);
176
177         metrics = pango_context_get_metrics (context, hbox->style->font_desc,
178                 pango_context_get_language (context));
179         char_width = pango_font_metrics_get_approximate_char_width (metrics);
180         pango_font_metrics_unref (metrics);
181
182         gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
183                                            GTK_ICON_SIZE_MENU, &w, &h);
184
185         /* Request at least about 12 chars width plus at least space for the status
186          * image and the close button */
187         gtk_widget_set_size_request (hbox,
188                 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
189
190         gtk_widget_set_size_request (button, w, h);
191 }
192
193 static GtkWidget *
194 chat_window_create_label (EmpathyChatWindow *window,
195                           EmpathyChat       *chat,
196                           gboolean           is_tab_label)
197 {
198         EmpathyChatWindowPriv *priv;
199         GtkWidget            *hbox;
200         GtkWidget            *name_label;
201         GtkWidget            *status_image;
202         GtkWidget            *close_button;
203         GtkWidget            *close_image;
204         GtkWidget            *event_box;
205         GtkWidget            *event_box_hbox;
206         PangoAttrList        *attr_list;
207         PangoAttribute       *attr;
208
209         priv = GET_PRIV (window);
210
211         /* The spacing between the button and the label. */
212         hbox = gtk_hbox_new (FALSE, 0);
213
214         event_box = gtk_event_box_new ();
215         gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
216
217         name_label = gtk_label_new (NULL);
218         if (is_tab_label)
219                 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
220
221         attr_list = pango_attr_list_new ();
222         attr = pango_attr_scale_new (1/1.2);
223         attr->start_index = 0;
224         attr->end_index = -1;
225         pango_attr_list_insert (attr_list, attr);
226         gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
227         pango_attr_list_unref (attr_list);
228
229         gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
230         gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
231         g_object_set_data (G_OBJECT (chat),
232                 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
233                 name_label);
234
235         status_image = gtk_image_new ();
236
237         /* Spacing between the icon and label. */
238         event_box_hbox = gtk_hbox_new (FALSE, 0);
239
240         gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
241         gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
242
243         g_object_set_data (G_OBJECT (chat),
244                 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
245                 status_image);
246         g_object_set_data (G_OBJECT (chat),
247                 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
248                 event_box);
249
250         gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
251         gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
252
253         if (is_tab_label) {
254                 close_button = gtk_button_new ();
255                 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
256                 g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
257
258                 /* We don't want focus/keynav for the button to avoid clutter, and
259                  * Ctrl-W works anyway.
260                  */
261                 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_FOCUS);
262                 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_DEFAULT);
263
264                 /* Set the name to make the special rc style match. */
265                 gtk_widget_set_name (close_button, "empathy-close-button");
266
267                 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
268
269                 gtk_container_add (GTK_CONTAINER (close_button), close_image);
270
271                 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
272
273                 g_signal_connect (close_button,
274                                   "clicked",
275                                   G_CALLBACK (chat_window_close_clicked_cb),
276                                   chat);
277
278                 /* React to theme changes and also setup the size correctly.  */
279                 g_signal_connect (hbox,
280                                   "style-set",
281                                   G_CALLBACK (chat_tab_style_set_cb),
282                                   chat);
283         }
284
285         gtk_widget_show_all (hbox);
286
287         return hbox;
288 }
289
290 static void
291 chat_window_update (EmpathyChatWindow *window)
292 {
293         EmpathyChatWindowPriv *priv = GET_PRIV (window);
294         gboolean               first_page;
295         gboolean               last_page;
296         gboolean               is_connected;
297         gint                   num_pages;
298         gint                   page_num;
299         gint                   i;
300         const gchar           *name;
301         guint                  n_chats;
302         GdkPixbuf             *icon;
303         EmpathyContact        *remote_contact;
304         gboolean               avatar_in_icon;
305         GtkWidget             *chat;
306         GtkWidget             *chat_close_button;
307         GtkWidget             *submenu;
308         GtkWidget             *menu;
309
310         /* Get information */
311         page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
312         num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
313         first_page = (page_num == 0);
314         last_page = (page_num == (num_pages - 1));
315         is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
316         name = empathy_chat_get_name (priv->current_chat);
317         n_chats = g_list_length (priv->chats);
318
319         DEBUG ("Update window");
320
321         /* Update menu */
322         gtk_action_set_sensitive (priv->menu_tabs_next, !last_page);
323         gtk_action_set_sensitive (priv->menu_tabs_prev, !first_page);
324         gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
325         gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
326         gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
327         gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
328
329         /* Update Contact menu */
330         menu = gtk_ui_manager_get_widget (priv->ui_manager,
331                 "/chats_menubar/menu_contact");
332         submenu = empathy_chat_get_contact_menu (priv->current_chat);
333         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
334         gtk_widget_show (menu);
335
336         /* Update window title */
337         if (n_chats == 1) {
338                 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
339         } else {
340                 gchar *title;
341
342                 title = g_strdup_printf (_("Conversations (%d)"), n_chats);
343                 gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
344                 g_free (title);
345         }
346
347         /* Update window icon */
348         if (priv->chats_new_msg) {
349                 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
350                                           EMPATHY_IMAGE_MESSAGE);
351         } else {
352                 empathy_conf_get_bool (empathy_conf_get (),
353                                        EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
354                                        &avatar_in_icon);
355
356                 if (n_chats == 1 && avatar_in_icon) {
357                         remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
358                         icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
359                         gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
360
361                         if (icon != NULL) {
362                                 g_object_unref (icon);
363                         }
364                 } else {
365                         gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
366                 }
367         }
368
369         if (num_pages == 1) {
370                 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
371                 chat_close_button = g_object_get_data (G_OBJECT (chat),
372                                 "chat-window-tab-close-button");
373                 gtk_widget_hide (chat_close_button);
374         } else {
375                 for (i=0; i<num_pages; i++) {
376                         chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
377                         chat_close_button = g_object_get_data (G_OBJECT (chat),
378                                         "chat-window-tab-close-button");
379                         gtk_widget_show (chat_close_button);
380                 }
381         }
382 }
383
384 static void
385 append_markup_printf (GString    *string,
386                       const char *format,
387                       ...)
388 {
389         gchar *tmp;
390         va_list args;
391
392         va_start (args, format);
393
394         tmp = g_markup_vprintf_escaped (format, args);
395         g_string_append (string, tmp);
396         g_free (tmp);
397
398         va_end (args);
399 }
400
401 static void
402 chat_window_update_chat_tab (EmpathyChat *chat)
403 {
404         EmpathyChatWindow     *window;
405         EmpathyChatWindowPriv *priv;
406         EmpathyContact        *remote_contact;
407         const gchar           *name;
408         McAccount             *account;
409         const gchar           *subject;
410         const gchar           *status;
411         GtkWidget             *widget;
412         GString               *tooltip;
413         gchar                 *markup;
414         const gchar           *icon_name;
415
416         window = chat_window_find_chat (chat);
417         if (!window) {
418                 return;
419         }
420         priv = GET_PRIV (window);
421
422         /* Get information */
423         name = empathy_chat_get_name (chat);
424         account = empathy_chat_get_account (chat);
425         subject = empathy_chat_get_subject (chat);
426         remote_contact = empathy_chat_get_remote_contact (chat);
427
428         DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
429                 name, mc_account_get_unique_name (account), subject, remote_contact);
430
431         /* Update tab image */
432         if (g_list_find (priv->chats_new_msg, chat)) {
433                 icon_name = EMPATHY_IMAGE_MESSAGE;
434         }
435         else if (g_list_find (priv->chats_composing, chat)) {
436                 icon_name = EMPATHY_IMAGE_TYPING;
437         }
438         else if (remote_contact) {
439                 icon_name = empathy_icon_name_for_contact (remote_contact);
440         } else {
441                 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
442         }
443         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
444         gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
445         widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
446         gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
447
448         /* Update tab tooltip */
449         tooltip = g_string_new (NULL);
450
451         if (remote_contact) {
452                 append_markup_printf (tooltip,
453                                       "<b>%s</b><small> (%s)</small>",
454                                       empathy_contact_get_id (remote_contact),
455                                       mc_account_get_display_name (account));
456
457                 status = empathy_contact_get_presence_message (remote_contact);
458
459                 if (!EMP_STR_EMPTY (status)) {
460                         append_markup_printf (tooltip, "\n%s", status);
461                 }
462         }
463         else {
464                 append_markup_printf (tooltip,
465                                       "<b>%s</b><small>  (%s)</small>", name,
466                                       mc_account_get_display_name (account));
467         }
468
469         if (subject) {
470                 append_markup_printf (tooltip, "\n<b>%s</b> %s",
471                                       _("Topic:"), subject);
472         }
473
474         if (g_list_find (priv->chats_composing, chat)) {
475                 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
476         }
477
478         markup = g_string_free (tooltip, FALSE);
479         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
480         gtk_widget_set_tooltip_markup (widget, markup);
481         widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
482         gtk_widget_set_tooltip_markup (widget, markup);
483         g_free (markup);
484
485         /* Update tab and menu label */
486         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
487         gtk_label_set_text (GTK_LABEL (widget), name);
488         widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
489         gtk_label_set_text (GTK_LABEL (widget), name);
490
491         /* Update the window if it's the current chat */
492         if (priv->current_chat == chat) {
493                 chat_window_update (window);
494         }
495 }
496
497 static void
498 chat_window_chat_notify_cb (EmpathyChat *chat)
499 {
500         EmpathyContact *old_remote_contact;
501         EmpathyContact *remote_contact = NULL;
502
503         old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
504         remote_contact = empathy_chat_get_remote_contact (chat);
505
506         if (old_remote_contact != remote_contact) {
507                 /* The remote-contact associated with the chat changed, we need
508                  * to keep track of any change of that contact and update the
509                  * window each time. */
510                 if (remote_contact) {
511                         g_signal_connect_swapped (remote_contact, "notify",
512                                                   G_CALLBACK (chat_window_update_chat_tab),
513                                                   chat);
514                 }
515                 if (old_remote_contact) {
516                         g_signal_handlers_disconnect_by_func (old_remote_contact,
517                                                               chat_window_update_chat_tab,
518                                                               chat);
519                 }
520
521                 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
522                                    remote_contact);
523         }
524
525         chat_window_update_chat_tab (chat);
526 }
527
528 static void
529 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
530                                        EmpathySmiley        *smiley,
531                                        gpointer              window)
532 {
533         EmpathyChatWindowPriv *priv = GET_PRIV (window);
534         EmpathyChat           *chat;
535         GtkTextBuffer         *buffer;
536         GtkTextIter            iter;
537
538         chat = priv->current_chat;
539
540         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
541         gtk_text_buffer_get_end_iter (buffer, &iter);
542         gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
543 }
544
545 static void
546 chat_window_conv_activate_cb (GtkAction         *action,
547                               EmpathyChatWindow *window)
548 {
549         EmpathyChatWindowPriv *priv = GET_PRIV (window);
550         gboolean               is_room;
551
552         /* Favorite room menu */
553         is_room = empathy_chat_is_room (priv->current_chat);
554         if (is_room) {
555                 const gchar *room;
556                 McAccount   *account;
557                 gboolean     found;
558
559                 room = empathy_chat_get_id (priv->current_chat);
560                 account = empathy_chat_get_account (priv->current_chat);
561                 found = empathy_chatroom_manager_find (priv->chatroom_manager,
562                                                        account, room) != NULL;
563
564                 DEBUG ("This room %s favorite", found ? "is" : "is not");
565                 gtk_toggle_action_set_active (
566                         GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
567         }
568         gtk_action_set_visible (priv->menu_conv_favorite, is_room);
569 }
570
571 static void
572 chat_window_clear_activate_cb (GtkAction         *action,
573                                EmpathyChatWindow *window)
574 {
575         EmpathyChatWindowPriv *priv = GET_PRIV (window);
576
577         empathy_chat_clear (priv->current_chat);
578 }
579
580 static void
581 chat_window_favorite_toggled_cb (GtkToggleAction   *toggle_action,
582                                  EmpathyChatWindow *window)
583 {
584         EmpathyChatWindowPriv *priv = GET_PRIV (window);
585         gboolean               active;
586         McAccount             *account;
587         const gchar           *room;
588         EmpathyChatroom       *chatroom;
589
590         active = gtk_toggle_action_get_active (toggle_action);
591         account = empathy_chat_get_account (priv->current_chat);
592         room = empathy_chat_get_id (priv->current_chat);
593
594         chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
595                                                   account, room);
596
597         if (active && !chatroom) {
598                 const gchar *name;
599
600                 name = empathy_chat_get_name (priv->current_chat);
601                 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
602                 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
603                 g_object_unref (chatroom);
604                 return;
605         }
606         
607         if (!active && chatroom) {
608                 empathy_chatroom_manager_remove (priv->chatroom_manager, chatroom);
609         }
610 }
611
612 static const gchar *
613 chat_get_window_id_for_geometry (EmpathyChat *chat)
614 {
615         const gchar *res = NULL;
616         gboolean     separate_windows;
617
618         empathy_conf_get_bool (empathy_conf_get (),
619                                EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
620                                &separate_windows);
621
622         if (separate_windows) {
623                 res = empathy_chat_get_id (chat);
624         }
625
626         return res ? res : "chat-window";
627 }
628
629 static gboolean
630 chat_window_save_geometry_timeout_cb (EmpathyChatWindow *window)
631 {
632         EmpathyChatWindowPriv *priv;
633         gint                  x, y, w, h;
634
635         priv = GET_PRIV (window);
636
637         gtk_window_get_size (GTK_WINDOW (priv->dialog), &w, &h);
638         gtk_window_get_position (GTK_WINDOW (priv->dialog), &x, &y);
639
640         empathy_geometry_save (chat_get_window_id_for_geometry (priv->current_chat),
641                                x, y, w, h);
642
643         priv->save_geometry_id = 0;
644
645         return FALSE;
646 }
647
648 static gboolean
649 chat_window_configure_event_cb (GtkWidget         *widget,
650                                 GdkEventConfigure *event,
651                                 EmpathyChatWindow  *window)
652 {
653         EmpathyChatWindowPriv *priv;
654
655         priv = GET_PRIV (window);
656
657         if (priv->save_geometry_id != 0) {
658                 g_source_remove (priv->save_geometry_id);
659         }
660
661         priv->save_geometry_id =
662                 g_timeout_add_seconds (1,
663                                        (GSourceFunc) chat_window_save_geometry_timeout_cb,
664                                        window);
665
666         return FALSE;
667 }
668
669 static void
670 chat_window_close_activate_cb (GtkAction         *action,
671                                EmpathyChatWindow *window)
672 {
673         EmpathyChatWindowPriv *priv;
674
675         priv = GET_PRIV (window);
676
677         g_return_if_fail (priv->current_chat != NULL);
678
679         empathy_chat_window_remove_chat (window, priv->current_chat);
680 }
681
682 static void
683 chat_window_edit_activate_cb (GtkAction         *action,
684                               EmpathyChatWindow *window)
685 {
686         EmpathyChatWindowPriv *priv;
687         GtkClipboard         *clipboard;
688         GtkTextBuffer        *buffer;
689         gboolean              text_available;
690
691         priv = GET_PRIV (window);
692
693         g_return_if_fail (priv->current_chat != NULL);
694
695         if (!empathy_chat_get_tp_chat (priv->current_chat)) {
696                 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
697                 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
698                 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
699                 return;
700         }
701
702         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
703         if (gtk_text_buffer_get_has_selection (buffer)) {
704                 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
705                 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
706         } else {
707                 gboolean selection;
708
709                 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
710
711                 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
712                 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
713         }
714
715         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
716         text_available = gtk_clipboard_wait_is_text_available (clipboard);
717         gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
718 }
719
720 static void
721 chat_window_cut_activate_cb (GtkAction         *action,
722                              EmpathyChatWindow *window)
723 {
724         EmpathyChatWindowPriv *priv;
725
726         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
727
728         priv = GET_PRIV (window);
729
730         empathy_chat_cut (priv->current_chat);
731 }
732
733 static void
734 chat_window_copy_activate_cb (GtkAction         *action,
735                               EmpathyChatWindow *window)
736 {
737         EmpathyChatWindowPriv *priv;
738
739         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
740
741         priv = GET_PRIV (window);
742
743         empathy_chat_copy (priv->current_chat);
744 }
745
746 static void
747 chat_window_paste_activate_cb (GtkAction         *action,
748                                EmpathyChatWindow *window)
749 {
750         EmpathyChatWindowPriv *priv;
751
752         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
753
754         priv = GET_PRIV (window);
755
756         empathy_chat_paste (priv->current_chat);
757 }
758
759 static void
760 chat_window_tabs_left_activate_cb (GtkAction         *action,
761                                    EmpathyChatWindow *window)
762 {
763         EmpathyChatWindowPriv *priv;
764         EmpathyChat           *chat;
765         gint                  index;
766
767         priv = GET_PRIV (window);
768
769         chat = priv->current_chat;
770         index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
771         if (index <= 0) {
772                 return;
773         }
774
775         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
776                                     GTK_WIDGET (chat),
777                                     index - 1);
778 }
779
780 static void
781 chat_window_tabs_right_activate_cb (GtkAction         *action,
782                                     EmpathyChatWindow *window)
783 {
784         EmpathyChatWindowPriv *priv;
785         EmpathyChat           *chat;
786         gint                  index;
787
788         priv = GET_PRIV (window);
789
790         chat = priv->current_chat;
791         index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
792
793         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
794                                     GTK_WIDGET (chat),
795                                     index + 1);
796 }
797
798 static void
799 chat_window_detach_activate_cb (GtkAction         *action,
800                                 EmpathyChatWindow *window)
801 {
802         EmpathyChatWindowPriv *priv;
803         EmpathyChatWindow     *new_window;
804         EmpathyChat           *chat;
805
806         priv = GET_PRIV (window);
807
808         chat = priv->current_chat;
809         new_window = empathy_chat_window_new ();
810
811         empathy_chat_window_move_chat (window, new_window, chat);
812
813         priv = GET_PRIV (new_window);
814         gtk_widget_show (priv->dialog);
815 }
816
817 static void
818 chat_window_help_contents_activate_cb (GtkAction         *action,
819                                        EmpathyChatWindow *window)
820 {
821         EmpathyChatWindowPriv *priv = GET_PRIV (window);
822
823         empathy_url_show (priv->dialog, "ghelp:empathy?chat");
824 }
825
826 static void
827 chat_window_help_about_activate_cb (GtkAction         *action,
828                                     EmpathyChatWindow *window)
829 {
830         EmpathyChatWindowPriv *priv = GET_PRIV (window);
831
832         empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
833 }
834
835 static gboolean
836 chat_window_delete_event_cb (GtkWidget        *dialog,
837                              GdkEvent         *event,
838                              EmpathyChatWindow *window)
839 {
840         EmpathyChatWindowPriv *priv = GET_PRIV (window);
841
842         DEBUG ("Delete event received");
843
844         g_object_ref (window);
845         while (priv->chats) {
846                 empathy_chat_window_remove_chat (window, priv->chats->data);
847         }
848         g_object_unref (window);
849
850         return TRUE;
851 }
852
853 static void
854 chat_window_composing_cb (EmpathyChat       *chat,
855                           gboolean          is_composing,
856                           EmpathyChatWindow *window)
857 {
858         EmpathyChatWindowPriv *priv;
859
860         priv = GET_PRIV (window);
861
862         if (is_composing && !g_list_find (priv->chats_composing, chat)) {
863                 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
864         } else {
865                 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
866         }
867
868         chat_window_update_chat_tab (chat);
869 }
870
871 static void
872 chat_window_set_urgency_hint (EmpathyChatWindow *window,
873                               gboolean          urgent)
874 {
875         EmpathyChatWindowPriv *priv;
876
877         priv = GET_PRIV (window);
878
879         DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
880         gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
881 }
882
883 typedef struct {
884         EmpathyChatWindow *window;
885         EmpathyChat *chat;
886 } NotificationData;
887
888 static void
889 chat_window_notification_closed_cb (NotifyNotification *notify,
890                                     NotificationData *cb_data)
891 {
892         EmpathyNotificationClosedReason reason = 0;
893         EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
894
895 #ifdef notify_notification_get_closed_reason
896         reason = notify_notification_get_closed_reason (notify);
897 #endif
898         if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
899                 empathy_chat_window_present_chat (cb_data->chat);
900         }
901
902         g_object_unref (notify);
903         priv->notification = NULL;
904         g_object_unref (cb_data->chat);
905         g_slice_free (NotificationData, cb_data);
906 }
907
908 static void
909 chat_window_show_or_update_notification (EmpathyChatWindow *window,
910                                          EmpathyMessage *message,
911                                          EmpathyChat *chat)
912 {
913         EmpathyContact *sender;
914         char *header, *escaped;
915         const char *body;
916         GdkPixbuf *pixbuf;
917         NotificationData *cb_data;
918         EmpathyChatWindowPriv *priv = GET_PRIV (window);
919         gboolean res;
920
921         if (!empathy_notification_is_enabled ()) {
922                 return;
923         } else {
924                 empathy_conf_get_bool (empathy_conf_get (),
925                                        EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
926                 if (!res) {
927                         return;
928                 }
929         }
930
931         cb_data = g_slice_new0 (NotificationData);
932         cb_data->chat = g_object_ref (chat);
933         cb_data->window = window;
934
935         sender = empathy_message_get_sender (message);
936         header = g_strdup_printf (_("New message from %s"),
937                                   empathy_contact_get_name (sender));
938         body = empathy_message_get_body (message);
939         escaped = g_markup_escape_text (body, -1);
940
941         pixbuf = empathy_misc_get_pixbuf_for_notification (sender, EMPATHY_IMAGE_NEW_MESSAGE);
942
943         if (priv->notification != NULL) {
944                 notify_notification_update (priv->notification,
945                                             header, escaped, NULL);
946                 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
947         } else {
948                 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
949                 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
950                 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
951
952                 g_signal_connect (priv->notification, "closed",
953                                   G_CALLBACK (chat_window_notification_closed_cb), cb_data);
954         }
955
956         notify_notification_show (priv->notification, NULL);
957
958         g_object_unref (pixbuf);
959         g_free (header);
960         g_free (escaped);
961 }
962
963 static void
964 chat_window_new_message_cb (EmpathyChat       *chat,
965                             EmpathyMessage    *message,
966                             EmpathyChatWindow *window)
967 {
968         EmpathyChatWindowPriv *priv;
969         gboolean              has_focus;
970         gboolean              needs_urgency;
971         EmpathyContact        *sender;
972
973         priv = GET_PRIV (window);
974
975         has_focus = empathy_chat_window_has_focus (window);
976
977         /* - if we're the sender, we play the sound if it's specified in the
978          *   preferences and we're not away.
979          * - if we receive a message, we play the sound if it's specified in the
980          *   preferences and the window does not have focus on the chat receiving
981          *   the message.
982          */
983
984         sender = empathy_message_get_sender (message);
985
986         if (empathy_contact_is_user (sender)) {
987                 empathy_sound_play (GTK_WIDGET (priv->dialog),
988                                     EMPATHY_SOUND_MESSAGE_OUTGOING);
989         }
990
991         if (has_focus && priv->current_chat == chat) {
992                 return;
993         }
994
995         /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
996          * If empathy_chat_get_remote_contact () returns NULL, that means it's
997          * an unamed MUC (msn-like).
998          * In case of a MUC, we set urgency only if the message contains our
999          * alias. */
1000         if (empathy_chat_is_room (chat) ||
1001             empathy_chat_get_remote_contact (chat) == NULL) {
1002                 needs_urgency = empathy_message_should_highlight (message);
1003         } else {
1004                 needs_urgency = TRUE;
1005         }
1006
1007         if (needs_urgency) {
1008                 if (!has_focus)
1009                         chat_window_set_urgency_hint (window, TRUE);
1010
1011                 empathy_sound_play (GTK_WIDGET (priv->dialog),
1012                     EMPATHY_SOUND_MESSAGE_INCOMING);
1013                 chat_window_show_or_update_notification (window, message, chat);
1014         }
1015
1016         if (!g_list_find (priv->chats_new_msg, chat)) {
1017                 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1018                 chat_window_update_chat_tab (chat);
1019         }
1020 }
1021
1022 static GtkNotebook *
1023 chat_window_detach_hook (GtkNotebook *source,
1024                          GtkWidget   *page,
1025                          gint         x,
1026                          gint         y,
1027                          gpointer     user_data)
1028 {
1029         EmpathyChatWindowPriv *priv;
1030         EmpathyChatWindow     *window, *new_window;
1031         EmpathyChat           *chat;
1032
1033         chat = EMPATHY_CHAT (page);
1034         window = chat_window_find_chat (chat);
1035
1036         new_window = empathy_chat_window_new ();
1037         priv = GET_PRIV (new_window);
1038
1039         DEBUG ("Detach hook called");
1040
1041         empathy_chat_window_move_chat (window, new_window, chat);
1042
1043         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1044         gtk_widget_show (priv->dialog);
1045
1046         return NULL;
1047 }
1048
1049 static void
1050 chat_window_page_switched_cb (GtkNotebook      *notebook,
1051                               GtkNotebookPage  *page,
1052                               gint              page_num,
1053                               EmpathyChatWindow *window)
1054 {
1055         EmpathyChatWindowPriv *priv;
1056         EmpathyChat           *chat;
1057         GtkWidget            *child;
1058
1059         DEBUG ("Page switched");
1060
1061         priv = GET_PRIV (window);
1062
1063         child = gtk_notebook_get_nth_page (notebook, page_num);
1064         chat = EMPATHY_CHAT (child);
1065
1066         if (priv->page_added) {
1067                 priv->page_added = FALSE;
1068                 empathy_chat_scroll_down (chat);
1069         }
1070         else if (priv->current_chat == chat) {
1071                 return;
1072         }
1073
1074         priv->current_chat = chat;
1075         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1076
1077         chat_window_update_chat_tab (chat);
1078 }
1079
1080 static void
1081 chat_window_page_added_cb (GtkNotebook      *notebook,
1082                            GtkWidget        *child,
1083                            guint             page_num,
1084                            EmpathyChatWindow *window)
1085 {
1086         EmpathyChatWindowPriv *priv;
1087         EmpathyChat           *chat;
1088
1089         priv = GET_PRIV (window);
1090
1091         /* If we just received DND to the same window, we don't want
1092          * to do anything here like removing the tab and then readding
1093          * it, so we return here and in "page-added".
1094          */
1095         if (priv->dnd_same_window) {
1096                 DEBUG ("Page added (back to the same window)");
1097                 priv->dnd_same_window = FALSE;
1098                 return;
1099         }
1100
1101         DEBUG ("Page added");
1102
1103         /* Get chat object */
1104         chat = EMPATHY_CHAT (child);
1105
1106         /* Connect chat signals for this window */
1107         g_signal_connect (chat, "composing",
1108                           G_CALLBACK (chat_window_composing_cb),
1109                           window);
1110         g_signal_connect (chat, "new-message",
1111                           G_CALLBACK (chat_window_new_message_cb),
1112                           window);
1113
1114         /* Set flag so we know to perform some special operations on
1115          * switch page due to the new page being added.
1116          */
1117         priv->page_added = TRUE;
1118
1119         /* Get list of chats up to date */
1120         priv->chats = g_list_append (priv->chats, chat);
1121
1122         chat_window_update_chat_tab (chat);
1123 }
1124
1125 static void
1126 chat_window_page_removed_cb (GtkNotebook      *notebook,
1127                              GtkWidget        *child,
1128                              guint             page_num,
1129                              EmpathyChatWindow *window)
1130 {
1131         EmpathyChatWindowPriv *priv;
1132         EmpathyChat           *chat;
1133
1134         priv = GET_PRIV (window);
1135
1136         /* If we just received DND to the same window, we don't want
1137          * to do anything here like removing the tab and then readding
1138          * it, so we return here and in "page-added".
1139          */
1140         if (priv->dnd_same_window) {
1141                 DEBUG ("Page removed (and will be readded to same window)");
1142                 return;
1143         }
1144
1145         DEBUG ("Page removed");
1146
1147         /* Get chat object */
1148         chat = EMPATHY_CHAT (child);
1149
1150         /* Disconnect all signal handlers for this chat and this window */
1151         g_signal_handlers_disconnect_by_func (chat,
1152                                               G_CALLBACK (chat_window_composing_cb),
1153                                               window);
1154         g_signal_handlers_disconnect_by_func (chat,
1155                                               G_CALLBACK (chat_window_new_message_cb),
1156                                               window);
1157
1158         /* Keep list of chats up to date */
1159         priv->chats = g_list_remove (priv->chats, chat);
1160         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1161         priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1162
1163         if (priv->chats == NULL) {
1164                 g_object_unref (window);
1165         } else {
1166                 chat_window_update (window);
1167         }
1168 }
1169
1170 static gboolean
1171 chat_window_focus_in_event_cb (GtkWidget        *widget,
1172                                GdkEvent         *event,
1173                                EmpathyChatWindow *window)
1174 {
1175         EmpathyChatWindowPriv *priv;
1176
1177         DEBUG ("Focus in event, updating title");
1178
1179         priv = GET_PRIV (window);
1180
1181         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1182
1183         chat_window_set_urgency_hint (window, FALSE);
1184         
1185         /* Update the title, since we now mark all unread messages as read. */
1186         chat_window_update_chat_tab (priv->current_chat);
1187
1188         return FALSE;
1189 }
1190
1191 static void
1192 chat_window_drag_data_received (GtkWidget        *widget,
1193                                 GdkDragContext   *context,
1194                                 int               x,
1195                                 int               y,
1196                                 GtkSelectionData *selection,
1197                                 guint             info,
1198                                 guint             time,
1199                                 EmpathyChatWindow *window)
1200 {
1201         if (info == DND_DRAG_TYPE_CONTACT_ID) {
1202                 EmpathyChat           *chat;
1203                 EmpathyChatWindow     *old_window;
1204                 McAccount             *account;
1205                 const gchar           *id;
1206                 gchar                **strv;
1207                 const gchar           *account_id;
1208                 const gchar           *contact_id;
1209
1210                 id = (const gchar*) selection->data;
1211
1212                 DEBUG ("DND contact from roster with id:'%s'", id);
1213                 
1214                 strv = g_strsplit (id, "/", 2);
1215                 account_id = strv[0];
1216                 contact_id = strv[1];
1217                 account = mc_account_lookup (account_id);
1218                 chat = empathy_chat_window_find_chat (account, contact_id);
1219
1220                 if (!chat) {
1221                         EmpathyAccountManager *account_manager;
1222                         TpConnection *connection;
1223
1224                         account_manager = empathy_account_manager_dup_singleton ();
1225                         connection = empathy_account_manager_get_connection (
1226                                 account_manager, account);
1227
1228                         if (connection) {
1229                                 empathy_dispatcher_chat_with_contact_id (
1230                                         connection, contact_id, NULL, NULL);
1231                         }
1232
1233                         g_object_unref (account_manager);
1234                         g_object_unref (account);
1235                         g_strfreev (strv);
1236                         return;
1237                 }
1238                 g_object_unref (account);
1239                 g_strfreev (strv);
1240
1241                 old_window = chat_window_find_chat (chat);              
1242                 if (old_window) {
1243                         if (old_window == window) {
1244                                 gtk_drag_finish (context, TRUE, FALSE, time);
1245                                 return;
1246                         }
1247                         
1248                         empathy_chat_window_move_chat (old_window, window, chat);
1249                 } else {
1250                         empathy_chat_window_add_chat (window, chat);
1251                 }
1252                 
1253                 /* Added to take care of any outstanding chat events */
1254                 empathy_chat_window_present_chat (chat);
1255
1256                 /* We should return TRUE to remove the data when doing
1257                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1258                  * weird consequences, and we handle that internally
1259                  * anyway with add_chat () and remove_chat ().
1260                  */
1261                 gtk_drag_finish (context, TRUE, FALSE, time);
1262         }
1263         else if (info == DND_DRAG_TYPE_TAB) {
1264                 EmpathyChat        **chat;
1265                 EmpathyChatWindow   *old_window = NULL;
1266
1267                 DEBUG ("DND tab");
1268
1269                 chat = (void *) selection->data;
1270                 old_window = chat_window_find_chat (*chat);
1271
1272                 if (old_window) {
1273                         EmpathyChatWindowPriv *priv;
1274
1275                         priv = GET_PRIV (window);
1276
1277                         if (old_window == window) {
1278                                 DEBUG ("DND tab (within same window)");
1279                                 priv->dnd_same_window = TRUE;
1280                                 gtk_drag_finish (context, TRUE, FALSE, time);
1281                                 return;
1282                         }
1283                         
1284                         priv->dnd_same_window = FALSE;
1285                 }
1286
1287                 /* We should return TRUE to remove the data when doing
1288                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1289                  * weird consequences, and we handle that internally
1290                  * anyway with add_chat () and remove_chat ().
1291                  */
1292                 gtk_drag_finish (context, TRUE, FALSE, time);
1293         } else {
1294                 DEBUG ("DND from unknown source");
1295                 gtk_drag_finish (context, FALSE, FALSE, time);
1296         }
1297 }
1298
1299 static void
1300 chat_window_finalize (GObject *object)
1301 {
1302         EmpathyChatWindow     *window;
1303         EmpathyChatWindowPriv *priv;
1304
1305         window = EMPATHY_CHAT_WINDOW (object);
1306         priv = GET_PRIV (window);
1307
1308         DEBUG ("Finalized: %p", object);
1309
1310         g_object_unref (priv->ui_manager);
1311         g_object_unref (priv->chatroom_manager);
1312         if (priv->save_geometry_id != 0) {
1313                 g_source_remove (priv->save_geometry_id);
1314         }
1315
1316         if (priv->notification != NULL) {
1317                 notify_notification_close (priv->notification, NULL);
1318                 g_object_unref (priv->notification);
1319                 priv->notification = NULL;
1320         }
1321
1322         chat_windows = g_list_remove (chat_windows, window);
1323         gtk_widget_destroy (priv->dialog);
1324
1325         G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1326 }
1327
1328 static void
1329 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1330 {
1331         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1332
1333         object_class->finalize = chat_window_finalize;
1334
1335         g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1336
1337         /* Set up a style for the close button with no focus padding. */
1338         gtk_rc_parse_string (
1339                 "style \"empathy-close-button-style\"\n"
1340                 "{\n"
1341                 "  GtkWidget::focus-padding = 0\n"
1342                 "  xthickness = 0\n"
1343                 "  ythickness = 0\n"
1344                 "}\n"
1345                 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1346
1347         gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1348 }
1349
1350 static void
1351 empathy_chat_window_init (EmpathyChatWindow *window)
1352 {
1353         GtkBuilder            *gui;
1354         GtkAccelGroup         *accel_group;
1355         GClosure              *closure;
1356         GtkWidget             *menu;
1357         GtkWidget             *submenu;
1358         gint                   i;
1359         GtkWidget             *chat_vbox;
1360         gchar                 *filename;
1361         EmpathySmileyManager  *smiley_manager;
1362         EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1363                 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1364
1365         window->priv = priv;
1366         filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1367         gui = empathy_builder_get_file (filename,
1368                                        "chat_window", &priv->dialog,
1369                                        "chat_vbox", &chat_vbox,
1370                                        "ui_manager", &priv->ui_manager,
1371                                        "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1372                                        "menu_conv_favorite", &priv->menu_conv_favorite,
1373                                        "menu_edit_cut", &priv->menu_edit_cut,
1374                                        "menu_edit_copy", &priv->menu_edit_copy,
1375                                        "menu_edit_paste", &priv->menu_edit_paste,
1376                                        "menu_tabs_next", &priv->menu_tabs_next,
1377                                        "menu_tabs_prev", &priv->menu_tabs_prev,
1378                                        "menu_tabs_left", &priv->menu_tabs_left,
1379                                        "menu_tabs_right", &priv->menu_tabs_right,
1380                                        "menu_tabs_detach", &priv->menu_tabs_detach,
1381                                        NULL);
1382         g_free (filename);
1383
1384         empathy_builder_connect (gui, window,
1385                               "chat_window", "configure-event", chat_window_configure_event_cb,
1386                               "menu_conv", "activate", chat_window_conv_activate_cb,
1387                               "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1388                               "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1389                               "menu_conv_close", "activate", chat_window_close_activate_cb,
1390                               "menu_edit", "activate", chat_window_edit_activate_cb,
1391                               "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1392                               "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1393                               "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1394                               "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1395                               "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1396                               "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1397                               "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1398                               "menu_help_about", "activate", chat_window_help_about_activate_cb,
1399                               NULL);
1400
1401         g_object_ref (priv->ui_manager);
1402         g_object_unref (gui);
1403
1404         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1405
1406         priv->notebook = gtk_notebook_new ();
1407         gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1408         gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1409         gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1410         gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1411         gtk_widget_show (priv->notebook);
1412
1413         /* Set up accels */
1414         accel_group = gtk_accel_group_new ();
1415         gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1416
1417         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1418                 closure =  g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1419                                            window,
1420                                            NULL);
1421                 gtk_accel_group_connect (accel_group,
1422                                          tab_accel_keys[i],
1423                                          GDK_MOD1_MASK,
1424                                          0,
1425                                          closure);
1426         }
1427
1428         g_object_unref (accel_group);
1429
1430         /* Set up smiley menu */
1431         smiley_manager = empathy_smiley_manager_dup_singleton ();
1432         submenu = empathy_smiley_menu_new (smiley_manager,
1433                                            chat_window_insert_smiley_activate_cb,
1434                                            window);
1435         menu = gtk_ui_manager_get_widget (priv->ui_manager,
1436                 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1437         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1438         g_object_unref (smiley_manager);
1439
1440         /* Set up signals we can't do with ui file since we may need to
1441          * block/unblock them at some later stage.
1442          */
1443
1444         g_signal_connect (priv->dialog,
1445                           "delete_event",
1446                           G_CALLBACK (chat_window_delete_event_cb),
1447                           window);
1448
1449         g_signal_connect_swapped (priv->menu_tabs_prev,
1450                                   "activate",
1451                                   G_CALLBACK (gtk_notebook_prev_page),
1452                                   priv->notebook);
1453         g_signal_connect_swapped (priv->menu_tabs_next,
1454                                   "activate",
1455                                   G_CALLBACK (gtk_notebook_next_page),
1456                                   priv->notebook);
1457
1458         g_signal_connect (priv->dialog,
1459                           "focus_in_event",
1460                           G_CALLBACK (chat_window_focus_in_event_cb),
1461                           window);
1462         g_signal_connect_after (priv->notebook,
1463                                 "switch_page",
1464                                 G_CALLBACK (chat_window_page_switched_cb),
1465                                 window);
1466         g_signal_connect (priv->notebook,
1467                           "page_added",
1468                           G_CALLBACK (chat_window_page_added_cb),
1469                           window);
1470         g_signal_connect (priv->notebook,
1471                           "page_removed",
1472                           G_CALLBACK (chat_window_page_removed_cb),
1473                           window);
1474
1475         /* Set up drag and drop */
1476         gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1477                            GTK_DEST_DEFAULT_ALL,
1478                            drag_types_dest,
1479                            G_N_ELEMENTS (drag_types_dest),
1480                            GDK_ACTION_MOVE);
1481
1482         g_signal_connect (priv->notebook,
1483                           "drag-data-received",
1484                           G_CALLBACK (chat_window_drag_data_received),
1485                           window);
1486
1487         chat_windows = g_list_prepend (chat_windows, window);
1488
1489         /* Set up private details */
1490         priv->chats = NULL;
1491         priv->chats_new_msg = NULL;
1492         priv->chats_composing = NULL;
1493         priv->current_chat = NULL;
1494 }
1495
1496 EmpathyChatWindow *
1497 empathy_chat_window_new (void)
1498 {
1499         return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1500 }
1501
1502 /* Returns the window to open a new tab in if there is only one window
1503  * visble, otherwise, returns NULL indicating that a new window should
1504  * be added.
1505  */
1506 EmpathyChatWindow *
1507 empathy_chat_window_get_default (void)
1508 {
1509         GList    *l;
1510         gboolean  separate_windows = TRUE;
1511
1512         empathy_conf_get_bool (empathy_conf_get (),
1513                               EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1514                               &separate_windows);
1515
1516         if (separate_windows) {
1517                 /* Always create a new window */
1518                 return NULL;
1519         }
1520
1521         for (l = chat_windows; l; l = l->next) {
1522                 EmpathyChatWindow *chat_window;
1523                 GtkWidget         *dialog;
1524
1525                 chat_window = l->data;
1526
1527                 dialog = empathy_chat_window_get_dialog (chat_window);
1528                 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1529                         /* Found a visible window on this desktop */
1530                         return chat_window;
1531                 }
1532         }
1533
1534         return NULL;
1535 }
1536
1537 GtkWidget *
1538 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1539 {
1540         EmpathyChatWindowPriv *priv;
1541
1542         g_return_val_if_fail (window != NULL, NULL);
1543
1544         priv = GET_PRIV (window);
1545
1546         return priv->dialog;
1547 }
1548
1549 void
1550 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1551                               EmpathyChat       *chat)
1552 {
1553         EmpathyChatWindowPriv *priv;
1554         GtkWidget             *label;
1555         GtkWidget             *popup_label;
1556         GtkWidget             *child;
1557         gint                   x, y, w, h;
1558
1559         g_return_if_fail (window != NULL);
1560         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1561
1562         priv = GET_PRIV (window);
1563
1564         /* Reference the chat object */
1565         g_object_ref (chat);
1566
1567         /* If this window has just been created, position it */
1568         if (priv->chats == NULL) {
1569                 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1570                 
1571                 if (x >= 0 && y >= 0) {
1572                         /* Let the window manager position it if we don't have
1573                          * good x, y coordinates.
1574                          */
1575                         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1576                 }
1577                 
1578                 if (w > 0 && h > 0) {
1579                         /* Use the defaults from the ui file if we don't have
1580                          * good w, h geometry.
1581                          */
1582                         gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1583                 }
1584         }
1585
1586         child = GTK_WIDGET (chat);
1587         label = chat_window_create_label (window, chat, TRUE);
1588         popup_label = chat_window_create_label (window, chat, FALSE);
1589         gtk_widget_show (child);
1590
1591         g_signal_connect (chat, "notify::name",
1592                           G_CALLBACK (chat_window_chat_notify_cb),
1593                           NULL);
1594         g_signal_connect (chat, "notify::subject",
1595                           G_CALLBACK (chat_window_chat_notify_cb),
1596                           NULL);
1597         g_signal_connect (chat, "notify::remote-contact",
1598                           G_CALLBACK (chat_window_chat_notify_cb),
1599                           NULL);
1600         chat_window_chat_notify_cb (chat);
1601
1602         gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1603         gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1604         gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1605         gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1606                                             TRUE, TRUE, GTK_PACK_START);
1607
1608         DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1609 }
1610
1611 void
1612 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1613                                  EmpathyChat       *chat)
1614 {
1615         EmpathyChatWindowPriv *priv;
1616         gint                   position;
1617         EmpathyContact        *remote_contact;
1618
1619         g_return_if_fail (window != NULL);
1620         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1621
1622         priv = GET_PRIV (window);
1623
1624         g_signal_handlers_disconnect_by_func (chat,
1625                                               chat_window_chat_notify_cb,
1626                                               NULL);
1627         remote_contact = g_object_get_data (G_OBJECT (chat),
1628                                             "chat-window-remote-contact");
1629         if (remote_contact) {
1630                 g_signal_handlers_disconnect_by_func (remote_contact,
1631                                                       chat_window_update_chat_tab,
1632                                                       chat);
1633         }
1634
1635         position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1636                                           GTK_WIDGET (chat));
1637         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1638
1639         DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1640
1641         g_object_unref (chat);
1642 }
1643
1644 void
1645 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1646                                EmpathyChatWindow *new_window,
1647                                EmpathyChat       *chat)
1648 {
1649         GtkWidget *widget;
1650
1651         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1652         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1653         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1654
1655         widget = GTK_WIDGET (chat);
1656
1657         DEBUG ("Chat moving with widget:%p (%d references)", widget,
1658                 G_OBJECT (widget)->ref_count);
1659
1660         /* We reference here to make sure we don't loose the widget
1661          * and the EmpathyChat object during the move.
1662          */
1663         g_object_ref (chat);
1664         g_object_ref (widget);
1665
1666         empathy_chat_window_remove_chat (old_window, chat);
1667         empathy_chat_window_add_chat (new_window, chat);
1668
1669         g_object_unref (widget);
1670         g_object_unref (chat);
1671 }
1672
1673 void
1674 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1675                                     EmpathyChat       *chat)
1676 {
1677         EmpathyChatWindowPriv *priv;
1678         gint                  page_num;
1679
1680         g_return_if_fail (window != NULL);
1681         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1682
1683         priv = GET_PRIV (window);
1684
1685         page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1686                                           GTK_WIDGET (chat));
1687         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1688                                        page_num);
1689 }
1690
1691 gboolean
1692 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1693 {
1694         EmpathyChatWindowPriv *priv;
1695         gboolean              has_focus;
1696
1697         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1698
1699         priv = GET_PRIV (window);
1700
1701         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1702
1703         return has_focus;
1704 }
1705
1706 EmpathyChat *
1707 empathy_chat_window_find_chat (McAccount   *account,
1708                                const gchar *id)
1709 {
1710         GList *l;
1711
1712         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1713         g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1714
1715         for (l = chat_windows; l; l = l->next) {
1716                 EmpathyChatWindowPriv *priv;
1717                 EmpathyChatWindow     *window;
1718                 GList                *ll;
1719
1720                 window = l->data;
1721                 priv = GET_PRIV (window);
1722
1723                 for (ll = priv->chats; ll; ll = ll->next) {
1724                         EmpathyChat *chat;
1725
1726                         chat = ll->data;
1727
1728                         if (empathy_account_equal (account, empathy_chat_get_account (chat)) &&
1729                             !tp_strdiff (id, empathy_chat_get_id (chat))) {
1730                                 return chat;
1731                         }
1732                 }
1733         }
1734
1735         return NULL;
1736 }
1737
1738 void
1739 empathy_chat_window_present_chat (EmpathyChat *chat)
1740 {
1741         EmpathyChatWindow     *window;
1742         EmpathyChatWindowPriv *priv;
1743
1744         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1745
1746         window = chat_window_find_chat (chat);
1747
1748         /* If the chat has no window, create one */
1749         if (window == NULL) {
1750                 window = empathy_chat_window_get_default ();
1751                 if (!window) {
1752                         window = empathy_chat_window_new ();
1753                 }
1754
1755                 empathy_chat_window_add_chat (window, chat);
1756         }
1757
1758         priv = GET_PRIV (window);
1759         empathy_chat_window_switch_to_chat (window, chat);
1760         empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1761
1762         gtk_widget_grab_focus (chat->input_text_view);
1763 }
1764