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