Move the FsElementElementAddedNotifier over the main pipeline
[empathy-mirror.git] / src / empathy-chat-window.c
blobe818e42402bf39bc4add3a924f071605c0e0ab30
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.
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.
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.
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
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>
28 #include <config.h>
30 #include <string.h>
32 #include <gtk/gtk.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <glib/gi18n.h>
35 #include <libnotify/notification.h>
37 #include <telepathy-glib/util.h>
38 #include <libmissioncontrol/mission-control.h>
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>
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-sound.h>
54 #include <libempathy-gtk/empathy-ui-utils.h>
56 #include "empathy-chat-window.h"
57 #include "empathy-about-dialog.h"
58 #include "empathy-misc.h"
60 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
61 #include <libempathy/empathy-debug.h>
63 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatWindow)
64 typedef struct {
65 EmpathyChat *current_chat;
66 GList *chats;
67 GList *chats_new_msg;
68 GList *chats_composing;
69 gboolean page_added;
70 gboolean dnd_same_window;
71 guint save_geometry_id;
72 EmpathyChatroomManager *chatroom_manager;
73 GtkWidget *dialog;
74 GtkWidget *notebook;
75 NotifyNotification *notification;
77 /* Menu items. */
78 GtkUIManager *ui_manager;
79 GtkAction *menu_conv_insert_smiley;
80 GtkAction *menu_conv_favorite;
81 GtkAction *menu_conv_toggle_contacts;
83 GtkAction *menu_edit_cut;
84 GtkAction *menu_edit_copy;
85 GtkAction *menu_edit_paste;
87 GtkAction *menu_tabs_next;
88 GtkAction *menu_tabs_prev;
89 GtkAction *menu_tabs_left;
90 GtkAction *menu_tabs_right;
91 GtkAction *menu_tabs_detach;
92 } EmpathyChatWindowPriv;
94 static GList *chat_windows = NULL;
96 static const guint tab_accel_keys[] = {
97 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
98 GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
101 typedef enum {
102 DND_DRAG_TYPE_CONTACT_ID,
103 DND_DRAG_TYPE_TAB
104 } DndDragType;
106 static const GtkTargetEntry drag_types_dest[] = {
107 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
108 { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
111 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
113 static void
114 chat_window_accel_cb (GtkAccelGroup *accelgroup,
115 GObject *object,
116 guint key,
117 GdkModifierType mod,
118 EmpathyChatWindow *window)
120 EmpathyChatWindowPriv *priv;
121 gint num = -1;
122 gint i;
124 priv = GET_PRIV (window);
126 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
127 if (tab_accel_keys[i] == key) {
128 num = i;
129 break;
133 if (num != -1) {
134 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
138 static EmpathyChatWindow *
139 chat_window_find_chat (EmpathyChat *chat)
141 EmpathyChatWindowPriv *priv;
142 GList *l, *ll;
144 for (l = chat_windows; l; l = l->next) {
145 priv = GET_PRIV (l->data);
146 ll = g_list_find (priv->chats, chat);
147 if (ll) {
148 return l->data;
152 return NULL;
155 static void
156 chat_window_close_clicked_cb (GtkAction *action,
157 EmpathyChat *chat)
159 EmpathyChatWindow *window;
161 window = chat_window_find_chat (chat);
162 empathy_chat_window_remove_chat (window, chat);
165 static void
166 chat_tab_style_set_cb (GtkWidget *hbox,
167 GtkStyle *previous_style,
168 gpointer user_data)
170 GtkWidget *button;
171 int char_width, h, w;
172 PangoContext *context;
173 PangoFontMetrics *metrics;
175 button = g_object_get_data (G_OBJECT (user_data),
176 "chat-window-tab-close-button");
177 context = gtk_widget_get_pango_context (hbox);
179 metrics = pango_context_get_metrics (context, gtk_widget_get_style (hbox)->font_desc,
180 pango_context_get_language (context));
181 char_width = pango_font_metrics_get_approximate_char_width (metrics);
182 pango_font_metrics_unref (metrics);
184 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
185 GTK_ICON_SIZE_MENU, &w, &h);
187 /* Request at least about 12 chars width plus at least space for the status
188 * image and the close button */
189 gtk_widget_set_size_request (hbox,
190 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
192 gtk_widget_set_size_request (button, w, h);
195 static GtkWidget *
196 chat_window_create_label (EmpathyChatWindow *window,
197 EmpathyChat *chat,
198 gboolean is_tab_label)
200 EmpathyChatWindowPriv *priv;
201 GtkWidget *hbox;
202 GtkWidget *name_label;
203 GtkWidget *status_image;
204 GtkWidget *close_button;
205 GtkWidget *close_image;
206 GtkWidget *event_box;
207 GtkWidget *event_box_hbox;
208 PangoAttrList *attr_list;
209 PangoAttribute *attr;
211 priv = GET_PRIV (window);
213 /* The spacing between the button and the label. */
214 hbox = gtk_hbox_new (FALSE, 0);
216 event_box = gtk_event_box_new ();
217 gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
219 name_label = gtk_label_new (NULL);
220 if (is_tab_label)
221 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
223 attr_list = pango_attr_list_new ();
224 attr = pango_attr_scale_new (1/1.2);
225 attr->start_index = 0;
226 attr->end_index = -1;
227 pango_attr_list_insert (attr_list, attr);
228 gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
229 pango_attr_list_unref (attr_list);
231 gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
232 gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
233 g_object_set_data (G_OBJECT (chat),
234 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
235 name_label);
237 status_image = gtk_image_new ();
239 /* Spacing between the icon and label. */
240 event_box_hbox = gtk_hbox_new (FALSE, 0);
242 gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
243 gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
245 g_object_set_data (G_OBJECT (chat),
246 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
247 status_image);
248 g_object_set_data (G_OBJECT (chat),
249 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
250 event_box);
252 gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
253 gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
255 if (is_tab_label) {
256 close_button = gtk_button_new ();
257 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
258 g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
260 /* We don't want focus/keynav for the button to avoid clutter, and
261 * Ctrl-W works anyway.
263 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_FOCUS);
264 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_DEFAULT);
266 /* Set the name to make the special rc style match. */
267 gtk_widget_set_name (close_button, "empathy-close-button");
269 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
271 gtk_container_add (GTK_CONTAINER (close_button), close_image);
273 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
275 g_signal_connect (close_button,
276 "clicked",
277 G_CALLBACK (chat_window_close_clicked_cb),
278 chat);
280 /* React to theme changes and also setup the size correctly. */
281 g_signal_connect (hbox,
282 "style-set",
283 G_CALLBACK (chat_tab_style_set_cb),
284 chat);
287 gtk_widget_show_all (hbox);
289 return hbox;
292 static void
293 chat_window_update (EmpathyChatWindow *window)
295 EmpathyChatWindowPriv *priv = GET_PRIV (window);
296 gboolean first_page;
297 gboolean last_page;
298 gboolean is_connected;
299 gint num_pages;
300 gint page_num;
301 gint i;
302 const gchar *name;
303 guint n_chats;
304 GdkPixbuf *icon;
305 EmpathyContact *remote_contact;
306 gboolean avatar_in_icon;
307 GtkWidget *chat;
308 GtkWidget *chat_close_button;
309 GtkWidget *submenu;
310 GtkWidget *menu;
312 /* Get information */
313 page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
314 num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
315 first_page = (page_num == 0);
316 last_page = (page_num == (num_pages - 1));
317 is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
318 name = empathy_chat_get_name (priv->current_chat);
319 n_chats = g_list_length (priv->chats);
321 DEBUG ("Update window");
323 /* Update menu */
324 gtk_action_set_sensitive (priv->menu_tabs_next, !last_page);
325 gtk_action_set_sensitive (priv->menu_tabs_prev, !first_page);
326 gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
327 gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
328 gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
329 gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
331 /* Update Contact menu */
332 menu = gtk_ui_manager_get_widget (priv->ui_manager,
333 "/chats_menubar/menu_contact");
334 submenu = empathy_chat_get_contact_menu (priv->current_chat);
335 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
336 gtk_widget_show (menu);
338 /* Update window title */
339 if (n_chats == 1) {
340 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
341 } else {
342 gchar *title;
344 title = g_strdup_printf (_("Conversations (%d)"), n_chats);
345 gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
346 g_free (title);
349 /* Update window icon */
350 if (priv->chats_new_msg) {
351 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
352 EMPATHY_IMAGE_MESSAGE);
353 } else {
354 empathy_conf_get_bool (empathy_conf_get (),
355 EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
356 &avatar_in_icon);
358 if (n_chats == 1 && avatar_in_icon) {
359 remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
360 icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
361 gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
363 if (icon != NULL) {
364 g_object_unref (icon);
366 } else {
367 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
371 if (num_pages == 1) {
372 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
373 chat_close_button = g_object_get_data (G_OBJECT (chat),
374 "chat-window-tab-close-button");
375 gtk_widget_hide (chat_close_button);
376 } else {
377 for (i=0; i<num_pages; i++) {
378 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
379 chat_close_button = g_object_get_data (G_OBJECT (chat),
380 "chat-window-tab-close-button");
381 gtk_widget_show (chat_close_button);
386 static void
387 append_markup_printf (GString *string,
388 const char *format,
389 ...)
391 gchar *tmp;
392 va_list args;
394 va_start (args, format);
396 tmp = g_markup_vprintf_escaped (format, args);
397 g_string_append (string, tmp);
398 g_free (tmp);
400 va_end (args);
403 static void
404 chat_window_update_chat_tab (EmpathyChat *chat)
406 EmpathyChatWindow *window;
407 EmpathyChatWindowPriv *priv;
408 EmpathyContact *remote_contact;
409 const gchar *name;
410 const gchar *id;
411 EmpathyAccount *account;
412 const gchar *subject;
413 const gchar *status = NULL;
414 GtkWidget *widget;
415 GString *tooltip;
416 gchar *markup;
417 const gchar *icon_name;
419 window = chat_window_find_chat (chat);
420 if (!window) {
421 return;
423 priv = GET_PRIV (window);
425 /* Get information */
426 name = empathy_chat_get_name (chat);
427 account = empathy_chat_get_account (chat);
428 subject = empathy_chat_get_subject (chat);
429 remote_contact = empathy_chat_get_remote_contact (chat);
431 DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
432 name, empathy_account_get_unique_name (account), subject, remote_contact);
434 /* Update tab image */
435 if (g_list_find (priv->chats_new_msg, chat)) {
436 icon_name = EMPATHY_IMAGE_MESSAGE;
438 else if (g_list_find (priv->chats_composing, chat)) {
439 icon_name = EMPATHY_IMAGE_TYPING;
441 else if (remote_contact) {
442 icon_name = empathy_icon_name_for_contact (remote_contact);
443 } else {
444 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
446 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
447 gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
448 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
449 gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
451 /* Update tab tooltip */
452 tooltip = g_string_new (NULL);
454 if (remote_contact) {
455 id = empathy_contact_get_id (remote_contact);
456 status = empathy_contact_get_presence_message (remote_contact);
457 } else {
458 id = name;
461 append_markup_printf (tooltip,
462 "<b>%s</b><small> (%s)</small>",
464 empathy_account_get_display_name (account));
466 if (!EMP_STR_EMPTY (status)) {
467 append_markup_printf (tooltip, "\n<i>%s</i>", status);
470 if (subject) {
471 append_markup_printf (tooltip, "\n<b>%s</b> %s",
472 _("Topic:"), subject);
475 if (g_list_find (priv->chats_composing, chat)) {
476 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
479 markup = g_string_free (tooltip, FALSE);
480 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
481 gtk_widget_set_tooltip_markup (widget, markup);
482 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
483 gtk_widget_set_tooltip_markup (widget, markup);
484 g_free (markup);
486 /* Update tab and menu label */
487 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
488 gtk_label_set_text (GTK_LABEL (widget), name);
489 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
490 gtk_label_set_text (GTK_LABEL (widget), name);
492 /* Update the window if it's the current chat */
493 if (priv->current_chat == chat) {
494 chat_window_update (window);
498 static void
499 chat_window_chat_notify_cb (EmpathyChat *chat)
501 EmpathyContact *old_remote_contact;
502 EmpathyContact *remote_contact = NULL;
504 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
505 remote_contact = empathy_chat_get_remote_contact (chat);
507 if (old_remote_contact != remote_contact) {
508 /* The remote-contact associated with the chat changed, we need
509 * to keep track of any change of that contact and update the
510 * window each time. */
511 if (remote_contact) {
512 g_signal_connect_swapped (remote_contact, "notify",
513 G_CALLBACK (chat_window_update_chat_tab),
514 chat);
516 if (old_remote_contact) {
517 g_signal_handlers_disconnect_by_func (old_remote_contact,
518 chat_window_update_chat_tab,
519 chat);
522 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
523 remote_contact);
526 chat_window_update_chat_tab (chat);
529 static void
530 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
531 EmpathySmiley *smiley,
532 gpointer window)
534 EmpathyChatWindowPriv *priv = GET_PRIV (window);
535 EmpathyChat *chat;
536 GtkTextBuffer *buffer;
537 GtkTextIter iter;
539 chat = priv->current_chat;
541 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
542 gtk_text_buffer_get_end_iter (buffer, &iter);
543 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
546 static void
547 chat_window_conv_activate_cb (GtkAction *action,
548 EmpathyChatWindow *window)
550 EmpathyChatWindowPriv *priv = GET_PRIV (window);
551 gboolean is_room;
552 gboolean active;
553 EmpathyContact *remote_contact = NULL;
555 /* Favorite room menu */
556 is_room = empathy_chat_is_room (priv->current_chat);
557 if (is_room) {
558 const gchar *room;
559 EmpathyAccount *account;
560 gboolean found = FALSE;
561 EmpathyChatroom *chatroom;
563 room = empathy_chat_get_id (priv->current_chat);
564 account = empathy_chat_get_account (priv->current_chat);
565 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
566 account, room);
567 if (chatroom != NULL)
568 found = empathy_chatroom_is_favorite (chatroom);
570 DEBUG ("This room %s favorite", found ? "is" : "is not");
571 gtk_toggle_action_set_active (
572 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
574 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
576 /* Show contacts menu */
577 g_object_get (priv->current_chat,
578 "remote-contact", &remote_contact,
579 "show-contacts", &active,
580 NULL);
581 if (remote_contact == NULL) {
582 gtk_toggle_action_set_active (
583 GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
584 active);
586 gtk_action_set_visible (priv->menu_conv_toggle_contacts,
587 (remote_contact == NULL));
588 if (remote_contact != NULL) {
589 g_object_unref (remote_contact);
593 static void
594 chat_window_clear_activate_cb (GtkAction *action,
595 EmpathyChatWindow *window)
597 EmpathyChatWindowPriv *priv = GET_PRIV (window);
599 empathy_chat_clear (priv->current_chat);
602 static void
603 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
604 EmpathyChatWindow *window)
606 EmpathyChatWindowPriv *priv = GET_PRIV (window);
607 gboolean active;
608 EmpathyAccount *account;
609 const gchar *room;
610 EmpathyChatroom *chatroom;
612 active = gtk_toggle_action_get_active (toggle_action);
613 account = empathy_chat_get_account (priv->current_chat);
614 room = empathy_chat_get_id (priv->current_chat);
616 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
617 account, room);
619 if (chatroom == NULL) {
620 const gchar *name;
622 name = empathy_chat_get_name (priv->current_chat);
623 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
624 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
625 g_object_unref (chatroom);
628 empathy_chatroom_set_favorite (chatroom, active);
631 static void
632 chat_window_contacts_toggled_cb (GtkToggleAction *toggle_action,
633 EmpathyChatWindow *window)
635 EmpathyChatWindowPriv *priv = GET_PRIV (window);
636 gboolean active;
638 active = gtk_toggle_action_get_active (toggle_action);
640 empathy_chat_set_show_contacts (priv->current_chat, active);
643 static const gchar *
644 chat_get_window_id_for_geometry (EmpathyChat *chat)
646 const gchar *res = NULL;
647 gboolean separate_windows;
649 empathy_conf_get_bool (empathy_conf_get (),
650 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
651 &separate_windows);
653 if (separate_windows) {
654 res = empathy_chat_get_id (chat);
657 return res ? res : "chat-window";
660 static gboolean
661 chat_window_save_geometry_timeout_cb (EmpathyChatWindow *window)
663 EmpathyChatWindowPriv *priv;
664 gint x, y, w, h;
666 priv = GET_PRIV (window);
668 gtk_window_get_size (GTK_WINDOW (priv->dialog), &w, &h);
669 gtk_window_get_position (GTK_WINDOW (priv->dialog), &x, &y);
671 empathy_geometry_save (chat_get_window_id_for_geometry (priv->current_chat),
672 x, y, w, h);
674 priv->save_geometry_id = 0;
676 return FALSE;
679 static gboolean
680 chat_window_configure_event_cb (GtkWidget *widget,
681 GdkEventConfigure *event,
682 EmpathyChatWindow *window)
684 EmpathyChatWindowPriv *priv;
686 priv = GET_PRIV (window);
688 if (priv->save_geometry_id != 0) {
689 g_source_remove (priv->save_geometry_id);
692 priv->save_geometry_id =
693 g_timeout_add_seconds (1,
694 (GSourceFunc) chat_window_save_geometry_timeout_cb,
695 window);
697 return FALSE;
700 static void
701 chat_window_close_activate_cb (GtkAction *action,
702 EmpathyChatWindow *window)
704 EmpathyChatWindowPriv *priv;
706 priv = GET_PRIV (window);
708 g_return_if_fail (priv->current_chat != NULL);
710 empathy_chat_window_remove_chat (window, priv->current_chat);
713 static void
714 chat_window_edit_activate_cb (GtkAction *action,
715 EmpathyChatWindow *window)
717 EmpathyChatWindowPriv *priv;
718 GtkClipboard *clipboard;
719 GtkTextBuffer *buffer;
720 gboolean text_available;
722 priv = GET_PRIV (window);
724 g_return_if_fail (priv->current_chat != NULL);
726 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
727 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
728 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
729 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
730 return;
733 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
734 if (gtk_text_buffer_get_has_selection (buffer)) {
735 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
736 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
737 } else {
738 gboolean selection;
740 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
742 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
743 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
746 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
747 text_available = gtk_clipboard_wait_is_text_available (clipboard);
748 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
751 static void
752 chat_window_cut_activate_cb (GtkAction *action,
753 EmpathyChatWindow *window)
755 EmpathyChatWindowPriv *priv;
757 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
759 priv = GET_PRIV (window);
761 empathy_chat_cut (priv->current_chat);
764 static void
765 chat_window_copy_activate_cb (GtkAction *action,
766 EmpathyChatWindow *window)
768 EmpathyChatWindowPriv *priv;
770 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
772 priv = GET_PRIV (window);
774 empathy_chat_copy (priv->current_chat);
777 static void
778 chat_window_paste_activate_cb (GtkAction *action,
779 EmpathyChatWindow *window)
781 EmpathyChatWindowPriv *priv;
783 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
785 priv = GET_PRIV (window);
787 empathy_chat_paste (priv->current_chat);
790 static void
791 chat_window_tabs_left_activate_cb (GtkAction *action,
792 EmpathyChatWindow *window)
794 EmpathyChatWindowPriv *priv;
795 EmpathyChat *chat;
796 gint index;
798 priv = GET_PRIV (window);
800 chat = priv->current_chat;
801 index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
802 if (index <= 0) {
803 return;
806 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
807 GTK_WIDGET (chat),
808 index - 1);
811 static void
812 chat_window_tabs_right_activate_cb (GtkAction *action,
813 EmpathyChatWindow *window)
815 EmpathyChatWindowPriv *priv;
816 EmpathyChat *chat;
817 gint index;
819 priv = GET_PRIV (window);
821 chat = priv->current_chat;
822 index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
824 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
825 GTK_WIDGET (chat),
826 index + 1);
829 static void
830 chat_window_detach_activate_cb (GtkAction *action,
831 EmpathyChatWindow *window)
833 EmpathyChatWindowPriv *priv;
834 EmpathyChatWindow *new_window;
835 EmpathyChat *chat;
837 priv = GET_PRIV (window);
839 chat = priv->current_chat;
840 new_window = empathy_chat_window_new ();
842 empathy_chat_window_move_chat (window, new_window, chat);
844 priv = GET_PRIV (new_window);
845 gtk_widget_show (priv->dialog);
848 static void
849 chat_window_help_contents_activate_cb (GtkAction *action,
850 EmpathyChatWindow *window)
852 EmpathyChatWindowPriv *priv = GET_PRIV (window);
854 empathy_url_show (priv->dialog, "ghelp:empathy?chat");
857 static void
858 chat_window_help_about_activate_cb (GtkAction *action,
859 EmpathyChatWindow *window)
861 EmpathyChatWindowPriv *priv = GET_PRIV (window);
863 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
866 static gboolean
867 chat_window_delete_event_cb (GtkWidget *dialog,
868 GdkEvent *event,
869 EmpathyChatWindow *window)
871 EmpathyChatWindowPriv *priv = GET_PRIV (window);
873 DEBUG ("Delete event received");
875 g_object_ref (window);
876 while (priv->chats) {
877 empathy_chat_window_remove_chat (window, priv->chats->data);
879 g_object_unref (window);
881 return TRUE;
884 static void
885 chat_window_composing_cb (EmpathyChat *chat,
886 gboolean is_composing,
887 EmpathyChatWindow *window)
889 EmpathyChatWindowPriv *priv;
891 priv = GET_PRIV (window);
893 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
894 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
895 } else {
896 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
899 chat_window_update_chat_tab (chat);
902 static void
903 chat_window_set_urgency_hint (EmpathyChatWindow *window,
904 gboolean urgent)
906 EmpathyChatWindowPriv *priv;
908 priv = GET_PRIV (window);
910 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
911 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
914 typedef struct {
915 EmpathyChatWindow *window;
916 EmpathyChat *chat;
917 } NotificationData;
919 static void
920 chat_window_notification_closed_cb (NotifyNotification *notify,
921 NotificationData *cb_data)
923 EmpathyNotificationClosedReason reason = 0;
924 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
926 #ifdef notify_notification_get_closed_reason
927 reason = notify_notification_get_closed_reason (notify);
928 #endif
929 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
930 empathy_chat_window_present_chat (cb_data->chat);
933 g_object_unref (notify);
934 priv->notification = NULL;
935 g_object_unref (cb_data->chat);
936 g_slice_free (NotificationData, cb_data);
939 static void
940 chat_window_show_or_update_notification (EmpathyChatWindow *window,
941 EmpathyMessage *message,
942 EmpathyChat *chat)
944 EmpathyContact *sender;
945 const gchar *header;
946 char *escaped;
947 const char *body;
948 GdkPixbuf *pixbuf;
949 NotificationData *cb_data;
950 EmpathyChatWindowPriv *priv = GET_PRIV (window);
951 gboolean res;
953 if (!empathy_notification_is_enabled ()) {
954 return;
955 } else {
956 empathy_conf_get_bool (empathy_conf_get (),
957 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
958 if (!res) {
959 return;
963 cb_data = g_slice_new0 (NotificationData);
964 cb_data->chat = g_object_ref (chat);
965 cb_data->window = window;
967 sender = empathy_message_get_sender (message);
968 header = empathy_contact_get_name (sender);
969 body = empathy_message_get_body (message);
970 escaped = g_markup_escape_text (body, -1);
972 if (priv->notification != NULL) {
973 notify_notification_update (priv->notification,
974 header, escaped, NULL);
975 } else {
976 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
977 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
979 g_signal_connect (priv->notification, "closed",
980 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
983 pixbuf = empathy_misc_get_pixbuf_for_notification (sender, EMPATHY_IMAGE_NEW_MESSAGE);
985 if (pixbuf != NULL) {
986 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
987 g_object_unref (pixbuf);
990 notify_notification_show (priv->notification, NULL);
992 g_free (escaped);
995 static void
996 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
998 gchar *markup;
999 GtkWidget *widget;
1001 if (!empathy_chat_is_room (chat))
1002 return;
1004 markup = g_markup_printf_escaped (
1005 "<span color=\"red\" weight=\"bold\">%s</span>",
1006 empathy_chat_get_name (chat));
1008 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1009 gtk_label_set_markup (GTK_LABEL (widget), markup);
1010 g_free (markup);
1013 static void
1014 chat_window_new_message_cb (EmpathyChat *chat,
1015 EmpathyMessage *message,
1016 EmpathyChatWindow *window)
1018 EmpathyChatWindowPriv *priv;
1019 gboolean has_focus;
1020 gboolean needs_urgency;
1021 EmpathyContact *sender;
1023 priv = GET_PRIV (window);
1025 has_focus = empathy_chat_window_has_focus (window);
1027 /* - if we're the sender, we play the sound if it's specified in the
1028 * preferences and we're not away.
1029 * - if we receive a message, we play the sound if it's specified in the
1030 * preferences and the window does not have focus on the chat receiving
1031 * the message.
1034 sender = empathy_message_get_sender (message);
1036 if (empathy_contact_is_user (sender)) {
1037 empathy_sound_play (GTK_WIDGET (priv->dialog),
1038 EMPATHY_SOUND_MESSAGE_OUTGOING);
1041 if (has_focus && priv->current_chat == chat) {
1042 return;
1045 if (!g_list_find (priv->chats_new_msg, chat)) {
1046 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1047 chat_window_update_chat_tab (chat);
1050 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1051 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1052 * an unamed MUC (msn-like).
1053 * In case of a MUC, we set urgency only if the message contains our
1054 * alias. */
1055 if (empathy_chat_is_room (chat) ||
1056 empathy_chat_get_remote_contact (chat) == NULL) {
1057 needs_urgency = empathy_message_should_highlight (message);
1058 } else {
1059 needs_urgency = TRUE;
1062 if (needs_urgency) {
1063 if (!has_focus) {
1064 chat_window_set_urgency_hint (window, TRUE);
1065 chat_window_set_highlight_room_tab_label (chat);
1068 empathy_sound_play (GTK_WIDGET (priv->dialog),
1069 EMPATHY_SOUND_MESSAGE_INCOMING);
1070 chat_window_show_or_update_notification (window, message, chat);
1074 static GtkNotebook *
1075 chat_window_detach_hook (GtkNotebook *source,
1076 GtkWidget *page,
1077 gint x,
1078 gint y,
1079 gpointer user_data)
1081 EmpathyChatWindowPriv *priv;
1082 EmpathyChatWindow *window, *new_window;
1083 EmpathyChat *chat;
1085 chat = EMPATHY_CHAT (page);
1086 window = chat_window_find_chat (chat);
1088 new_window = empathy_chat_window_new ();
1089 priv = GET_PRIV (new_window);
1091 DEBUG ("Detach hook called");
1093 empathy_chat_window_move_chat (window, new_window, chat);
1095 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1096 gtk_widget_show (priv->dialog);
1098 return NULL;
1101 static void
1102 chat_window_page_switched_cb (GtkNotebook *notebook,
1103 GtkNotebookPage *page,
1104 gint page_num,
1105 EmpathyChatWindow *window)
1107 EmpathyChatWindowPriv *priv;
1108 EmpathyChat *chat;
1109 GtkWidget *child;
1111 DEBUG ("Page switched");
1113 priv = GET_PRIV (window);
1115 child = gtk_notebook_get_nth_page (notebook, page_num);
1116 chat = EMPATHY_CHAT (child);
1118 if (priv->page_added) {
1119 priv->page_added = FALSE;
1120 empathy_chat_scroll_down (chat);
1122 else if (priv->current_chat == chat) {
1123 return;
1126 priv->current_chat = chat;
1127 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1129 chat_window_update_chat_tab (chat);
1132 static void
1133 chat_window_page_added_cb (GtkNotebook *notebook,
1134 GtkWidget *child,
1135 guint page_num,
1136 EmpathyChatWindow *window)
1138 EmpathyChatWindowPriv *priv;
1139 EmpathyChat *chat;
1141 priv = GET_PRIV (window);
1143 /* If we just received DND to the same window, we don't want
1144 * to do anything here like removing the tab and then readding
1145 * it, so we return here and in "page-added".
1147 if (priv->dnd_same_window) {
1148 DEBUG ("Page added (back to the same window)");
1149 priv->dnd_same_window = FALSE;
1150 return;
1153 DEBUG ("Page added");
1155 /* Get chat object */
1156 chat = EMPATHY_CHAT (child);
1158 /* Connect chat signals for this window */
1159 g_signal_connect (chat, "composing",
1160 G_CALLBACK (chat_window_composing_cb),
1161 window);
1162 g_signal_connect (chat, "new-message",
1163 G_CALLBACK (chat_window_new_message_cb),
1164 window);
1166 /* Set flag so we know to perform some special operations on
1167 * switch page due to the new page being added.
1169 priv->page_added = TRUE;
1171 /* Get list of chats up to date */
1172 priv->chats = g_list_append (priv->chats, chat);
1174 chat_window_update_chat_tab (chat);
1177 static void
1178 chat_window_page_removed_cb (GtkNotebook *notebook,
1179 GtkWidget *child,
1180 guint page_num,
1181 EmpathyChatWindow *window)
1183 EmpathyChatWindowPriv *priv;
1184 EmpathyChat *chat;
1186 priv = GET_PRIV (window);
1188 /* If we just received DND to the same window, we don't want
1189 * to do anything here like removing the tab and then readding
1190 * it, so we return here and in "page-added".
1192 if (priv->dnd_same_window) {
1193 DEBUG ("Page removed (and will be readded to same window)");
1194 return;
1197 DEBUG ("Page removed");
1199 /* Get chat object */
1200 chat = EMPATHY_CHAT (child);
1202 /* Disconnect all signal handlers for this chat and this window */
1203 g_signal_handlers_disconnect_by_func (chat,
1204 G_CALLBACK (chat_window_composing_cb),
1205 window);
1206 g_signal_handlers_disconnect_by_func (chat,
1207 G_CALLBACK (chat_window_new_message_cb),
1208 window);
1210 /* Keep list of chats up to date */
1211 priv->chats = g_list_remove (priv->chats, chat);
1212 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1213 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1215 if (priv->chats == NULL) {
1216 g_object_unref (window);
1217 } else {
1218 chat_window_update (window);
1222 static gboolean
1223 chat_window_focus_in_event_cb (GtkWidget *widget,
1224 GdkEvent *event,
1225 EmpathyChatWindow *window)
1227 EmpathyChatWindowPriv *priv;
1229 DEBUG ("Focus in event, updating title");
1231 priv = GET_PRIV (window);
1233 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1235 chat_window_set_urgency_hint (window, FALSE);
1237 /* Update the title, since we now mark all unread messages as read. */
1238 chat_window_update_chat_tab (priv->current_chat);
1240 return FALSE;
1243 static void
1244 chat_window_drag_data_received (GtkWidget *widget,
1245 GdkDragContext *context,
1246 int x,
1247 int y,
1248 GtkSelectionData *selection,
1249 guint info,
1250 guint time,
1251 EmpathyChatWindow *window)
1253 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1254 EmpathyChat *chat;
1255 EmpathyChatWindow *old_window;
1256 EmpathyAccount *account;
1257 EmpathyAccountManager *account_manager;
1258 const gchar *id;
1259 gchar **strv;
1260 const gchar *account_id;
1261 const gchar *contact_id;
1263 id = (const gchar*) gtk_selection_data_get_data (selection);
1264 account_manager = empathy_account_manager_dup_singleton ();
1266 DEBUG ("DND contact from roster with id:'%s'", id);
1268 strv = g_strsplit (id, "/", 2);
1269 account_id = strv[0];
1270 contact_id = strv[1];
1271 account = empathy_account_manager_lookup (account_manager, account_id);
1272 chat = empathy_chat_window_find_chat (account, contact_id);
1274 if (!chat) {
1275 TpConnection *connection;
1277 connection = empathy_account_get_connection (account);
1279 if (connection) {
1280 empathy_dispatcher_chat_with_contact_id (
1281 connection, contact_id, NULL, NULL);
1284 g_object_unref (account);
1285 g_strfreev (strv);
1286 return;
1288 g_object_unref (account);
1289 g_object_unref (account_manager);
1290 g_strfreev (strv);
1292 old_window = chat_window_find_chat (chat);
1293 if (old_window) {
1294 if (old_window == window) {
1295 gtk_drag_finish (context, TRUE, FALSE, time);
1296 return;
1299 empathy_chat_window_move_chat (old_window, window, chat);
1300 } else {
1301 empathy_chat_window_add_chat (window, chat);
1304 /* Added to take care of any outstanding chat events */
1305 empathy_chat_window_present_chat (chat);
1307 /* We should return TRUE to remove the data when doing
1308 * GDK_ACTION_MOVE, but we don't here otherwise it has
1309 * weird consequences, and we handle that internally
1310 * anyway with add_chat () and remove_chat ().
1312 gtk_drag_finish (context, TRUE, FALSE, time);
1314 else if (info == DND_DRAG_TYPE_TAB) {
1315 EmpathyChat **chat;
1316 EmpathyChatWindow *old_window = NULL;
1318 DEBUG ("DND tab");
1320 chat = (void *) gtk_selection_data_get_data (selection);
1321 old_window = chat_window_find_chat (*chat);
1323 if (old_window) {
1324 EmpathyChatWindowPriv *priv;
1326 priv = GET_PRIV (window);
1328 if (old_window == window) {
1329 DEBUG ("DND tab (within same window)");
1330 priv->dnd_same_window = TRUE;
1331 gtk_drag_finish (context, TRUE, FALSE, time);
1332 return;
1335 priv->dnd_same_window = FALSE;
1338 /* We should return TRUE to remove the data when doing
1339 * GDK_ACTION_MOVE, but we don't here otherwise it has
1340 * weird consequences, and we handle that internally
1341 * anyway with add_chat () and remove_chat ().
1343 gtk_drag_finish (context, TRUE, FALSE, time);
1344 } else {
1345 DEBUG ("DND from unknown source");
1346 gtk_drag_finish (context, FALSE, FALSE, time);
1350 static void
1351 chat_window_finalize (GObject *object)
1353 EmpathyChatWindow *window;
1354 EmpathyChatWindowPriv *priv;
1356 window = EMPATHY_CHAT_WINDOW (object);
1357 priv = GET_PRIV (window);
1359 DEBUG ("Finalized: %p", object);
1361 g_object_unref (priv->ui_manager);
1362 g_object_unref (priv->chatroom_manager);
1363 if (priv->save_geometry_id != 0) {
1364 g_source_remove (priv->save_geometry_id);
1367 if (priv->notification != NULL) {
1368 notify_notification_close (priv->notification, NULL);
1369 g_object_unref (priv->notification);
1370 priv->notification = NULL;
1373 chat_windows = g_list_remove (chat_windows, window);
1374 gtk_widget_destroy (priv->dialog);
1376 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1379 static void
1380 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1382 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1384 object_class->finalize = chat_window_finalize;
1386 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1388 /* Set up a style for the close button with no focus padding. */
1389 gtk_rc_parse_string (
1390 "style \"empathy-close-button-style\"\n"
1391 "{\n"
1392 " GtkWidget::focus-padding = 0\n"
1393 " xthickness = 0\n"
1394 " ythickness = 0\n"
1395 "}\n"
1396 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1398 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1401 static void
1402 empathy_chat_window_init (EmpathyChatWindow *window)
1404 GtkBuilder *gui;
1405 GtkAccelGroup *accel_group;
1406 GClosure *closure;
1407 GtkWidget *menu;
1408 GtkWidget *submenu;
1409 gint i;
1410 GtkWidget *chat_vbox;
1411 gchar *filename;
1412 EmpathySmileyManager *smiley_manager;
1413 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1414 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1416 window->priv = priv;
1417 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1418 gui = empathy_builder_get_file (filename,
1419 "chat_window", &priv->dialog,
1420 "chat_vbox", &chat_vbox,
1421 "ui_manager", &priv->ui_manager,
1422 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1423 "menu_conv_favorite", &priv->menu_conv_favorite,
1424 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1425 "menu_edit_cut", &priv->menu_edit_cut,
1426 "menu_edit_copy", &priv->menu_edit_copy,
1427 "menu_edit_paste", &priv->menu_edit_paste,
1428 "menu_tabs_next", &priv->menu_tabs_next,
1429 "menu_tabs_prev", &priv->menu_tabs_prev,
1430 "menu_tabs_left", &priv->menu_tabs_left,
1431 "menu_tabs_right", &priv->menu_tabs_right,
1432 "menu_tabs_detach", &priv->menu_tabs_detach,
1433 NULL);
1434 g_free (filename);
1436 empathy_builder_connect (gui, window,
1437 "chat_window", "configure-event", chat_window_configure_event_cb,
1438 "menu_conv", "activate", chat_window_conv_activate_cb,
1439 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1440 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1441 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1442 "menu_conv_close", "activate", chat_window_close_activate_cb,
1443 "menu_edit", "activate", chat_window_edit_activate_cb,
1444 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1445 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1446 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1447 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1448 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1449 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1450 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1451 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1452 NULL);
1454 g_object_ref (priv->ui_manager);
1455 g_object_unref (gui);
1457 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1459 priv->notebook = gtk_notebook_new ();
1460 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1461 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1462 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1463 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1464 gtk_widget_show (priv->notebook);
1466 /* Set up accels */
1467 accel_group = gtk_accel_group_new ();
1468 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1470 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1471 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1472 window,
1473 NULL);
1474 gtk_accel_group_connect (accel_group,
1475 tab_accel_keys[i],
1476 GDK_MOD1_MASK,
1478 closure);
1481 g_object_unref (accel_group);
1483 /* Set up smiley menu */
1484 smiley_manager = empathy_smiley_manager_dup_singleton ();
1485 submenu = empathy_smiley_menu_new (smiley_manager,
1486 chat_window_insert_smiley_activate_cb,
1487 window);
1488 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1489 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1490 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1491 g_object_unref (smiley_manager);
1493 /* Set up signals we can't do with ui file since we may need to
1494 * block/unblock them at some later stage.
1497 g_signal_connect (priv->dialog,
1498 "delete_event",
1499 G_CALLBACK (chat_window_delete_event_cb),
1500 window);
1502 g_signal_connect_swapped (priv->menu_tabs_prev,
1503 "activate",
1504 G_CALLBACK (gtk_notebook_prev_page),
1505 priv->notebook);
1506 g_signal_connect_swapped (priv->menu_tabs_next,
1507 "activate",
1508 G_CALLBACK (gtk_notebook_next_page),
1509 priv->notebook);
1511 g_signal_connect (priv->dialog,
1512 "focus_in_event",
1513 G_CALLBACK (chat_window_focus_in_event_cb),
1514 window);
1515 g_signal_connect_after (priv->notebook,
1516 "switch_page",
1517 G_CALLBACK (chat_window_page_switched_cb),
1518 window);
1519 g_signal_connect (priv->notebook,
1520 "page_added",
1521 G_CALLBACK (chat_window_page_added_cb),
1522 window);
1523 g_signal_connect (priv->notebook,
1524 "page_removed",
1525 G_CALLBACK (chat_window_page_removed_cb),
1526 window);
1528 /* Set up drag and drop */
1529 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1530 GTK_DEST_DEFAULT_ALL,
1531 drag_types_dest,
1532 G_N_ELEMENTS (drag_types_dest),
1533 GDK_ACTION_MOVE);
1535 g_signal_connect (priv->notebook,
1536 "drag-data-received",
1537 G_CALLBACK (chat_window_drag_data_received),
1538 window);
1540 chat_windows = g_list_prepend (chat_windows, window);
1542 /* Set up private details */
1543 priv->chats = NULL;
1544 priv->chats_new_msg = NULL;
1545 priv->chats_composing = NULL;
1546 priv->current_chat = NULL;
1549 EmpathyChatWindow *
1550 empathy_chat_window_new (void)
1552 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1555 /* Returns the window to open a new tab in if there is only one window
1556 * visble, otherwise, returns NULL indicating that a new window should
1557 * be added.
1559 EmpathyChatWindow *
1560 empathy_chat_window_get_default (void)
1562 GList *l;
1563 gboolean separate_windows = TRUE;
1565 empathy_conf_get_bool (empathy_conf_get (),
1566 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1567 &separate_windows);
1569 if (separate_windows) {
1570 /* Always create a new window */
1571 return NULL;
1574 for (l = chat_windows; l; l = l->next) {
1575 EmpathyChatWindow *chat_window;
1576 GtkWidget *dialog;
1578 chat_window = l->data;
1580 dialog = empathy_chat_window_get_dialog (chat_window);
1581 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1582 /* Found a visible window on this desktop */
1583 return chat_window;
1587 return NULL;
1590 GtkWidget *
1591 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1593 EmpathyChatWindowPriv *priv;
1595 g_return_val_if_fail (window != NULL, NULL);
1597 priv = GET_PRIV (window);
1599 return priv->dialog;
1602 void
1603 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1604 EmpathyChat *chat)
1606 EmpathyChatWindowPriv *priv;
1607 GtkWidget *label;
1608 GtkWidget *popup_label;
1609 GtkWidget *child;
1610 gint x, y, w, h;
1612 g_return_if_fail (window != NULL);
1613 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1615 priv = GET_PRIV (window);
1617 /* Reference the chat object */
1618 g_object_ref (chat);
1620 /* If this window has just been created, position it */
1621 if (priv->chats == NULL) {
1622 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1624 if (x >= 0 && y >= 0) {
1625 /* Let the window manager position it if we don't have
1626 * good x, y coordinates.
1628 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1631 if (w > 0 && h > 0) {
1632 /* Use the defaults from the ui file if we don't have
1633 * good w, h geometry.
1635 gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1639 child = GTK_WIDGET (chat);
1640 label = chat_window_create_label (window, chat, TRUE);
1641 popup_label = chat_window_create_label (window, chat, FALSE);
1642 gtk_widget_show (child);
1644 g_signal_connect (chat, "notify::name",
1645 G_CALLBACK (chat_window_chat_notify_cb),
1646 NULL);
1647 g_signal_connect (chat, "notify::subject",
1648 G_CALLBACK (chat_window_chat_notify_cb),
1649 NULL);
1650 g_signal_connect (chat, "notify::remote-contact",
1651 G_CALLBACK (chat_window_chat_notify_cb),
1652 NULL);
1653 chat_window_chat_notify_cb (chat);
1655 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1656 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1657 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1658 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1659 TRUE, TRUE, GTK_PACK_START);
1661 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1664 void
1665 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1666 EmpathyChat *chat)
1668 EmpathyChatWindowPriv *priv;
1669 gint position;
1670 EmpathyContact *remote_contact;
1672 g_return_if_fail (window != NULL);
1673 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1675 priv = GET_PRIV (window);
1677 g_signal_handlers_disconnect_by_func (chat,
1678 chat_window_chat_notify_cb,
1679 NULL);
1680 remote_contact = g_object_get_data (G_OBJECT (chat),
1681 "chat-window-remote-contact");
1682 if (remote_contact) {
1683 g_signal_handlers_disconnect_by_func (remote_contact,
1684 chat_window_update_chat_tab,
1685 chat);
1688 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1689 GTK_WIDGET (chat));
1690 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1692 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1694 g_object_unref (chat);
1697 void
1698 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1699 EmpathyChatWindow *new_window,
1700 EmpathyChat *chat)
1702 GtkWidget *widget;
1704 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1705 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1706 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1708 widget = GTK_WIDGET (chat);
1710 DEBUG ("Chat moving with widget:%p (%d references)", widget,
1711 G_OBJECT (widget)->ref_count);
1713 /* We reference here to make sure we don't loose the widget
1714 * and the EmpathyChat object during the move.
1716 g_object_ref (chat);
1717 g_object_ref (widget);
1719 empathy_chat_window_remove_chat (old_window, chat);
1720 empathy_chat_window_add_chat (new_window, chat);
1722 g_object_unref (widget);
1723 g_object_unref (chat);
1726 void
1727 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1728 EmpathyChat *chat)
1730 EmpathyChatWindowPriv *priv;
1731 gint page_num;
1733 g_return_if_fail (window != NULL);
1734 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1736 priv = GET_PRIV (window);
1738 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1739 GTK_WIDGET (chat));
1740 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1741 page_num);
1744 gboolean
1745 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1747 EmpathyChatWindowPriv *priv;
1748 gboolean has_focus;
1750 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1752 priv = GET_PRIV (window);
1754 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1756 return has_focus;
1759 EmpathyChat *
1760 empathy_chat_window_find_chat (EmpathyAccount *account,
1761 const gchar *id)
1763 GList *l;
1765 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1767 for (l = chat_windows; l; l = l->next) {
1768 EmpathyChatWindowPriv *priv;
1769 EmpathyChatWindow *window;
1770 GList *ll;
1772 window = l->data;
1773 priv = GET_PRIV (window);
1775 for (ll = priv->chats; ll; ll = ll->next) {
1776 EmpathyChat *chat;
1778 chat = ll->data;
1780 if (empathy_account_equal (account, empathy_chat_get_account (chat)) &&
1781 !tp_strdiff (id, empathy_chat_get_id (chat))) {
1782 return chat;
1787 return NULL;
1790 void
1791 empathy_chat_window_present_chat (EmpathyChat *chat)
1793 EmpathyChatWindow *window;
1794 EmpathyChatWindowPriv *priv;
1796 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1798 window = chat_window_find_chat (chat);
1800 /* If the chat has no window, create one */
1801 if (window == NULL) {
1802 window = empathy_chat_window_get_default ();
1803 if (!window) {
1804 window = empathy_chat_window_new ();
1807 empathy_chat_window_add_chat (window, chat);
1810 priv = GET_PRIV (window);
1811 empathy_chat_window_switch_to_chat (window, chat);
1812 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1814 gtk_widget_grab_focus (chat->input_text_view);