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