]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-chat-view.c
d6c85606f2fbc90e61e4461abd10d1113599dc90
[empathy.git] / libempathy-gtk / empathy-chat-view.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2002-2007 Imendio AB
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  * 
20  * Authors: Mikael Hallendal <micke@imendio.com>
21  *          Richard Hult <richard@imendio.com>
22  *          Martyn Russell <martyn@imendio.com>
23  */
24
25 #include "config.h"
26
27 #include <sys/types.h>
28 #include <string.h>
29 #include <time.h>
30
31 #include <glib/gi18n.h>
32 #include <gtk/gtkbutton.h>
33 #include <gtk/gtkimage.h>
34 #include <gtk/gtkmenu.h>
35 #include <gtk/gtkmenuitem.h>
36 #include <gtk/gtkimagemenuitem.h>
37 #include <gtk/gtkstock.h>
38 #include <gtk/gtkscrolledwindow.h>
39 #include <gtk/gtksizegroup.h>
40 #include <glade/glade.h>
41
42 #include <telepathy-glib/util.h>
43 #include <libmissioncontrol/mc-account.h>
44
45 #include <libempathy/empathy-utils.h>
46 #include <libempathy/empathy-debug.h>
47 #include <libempathy/empathy-conf.h>
48
49 #include "empathy-chat-view.h"
50 #include "empathy-chat.h"
51 #include "empathy-preferences.h"
52 #include "empathy-theme-manager.h"
53 #include "empathy-ui-utils.h"
54 #include "empathy-smiley-manager.h"
55
56 #define DEBUG_DOMAIN "ChatView"
57
58 /* Number of seconds between timestamps when using normal mode, 5 minutes. */
59 #define TIMESTAMP_INTERVAL 300
60
61 #define MAX_LINES 800
62 #define MAX_SCROLL_TIME 0.4 /* seconds */
63 #define SCROLL_DELAY 33     /* milliseconds */
64
65 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CHAT_VIEW, EmpathyChatViewPriv))
66
67 struct _EmpathyChatViewPriv {
68         GtkTextBuffer *buffer;
69
70         EmpathyTheme   *theme;
71
72         time_t         last_timestamp;
73         EmpathyChatViewBlock last_block_type;
74
75         gboolean       allow_scrolling;
76         guint          scroll_timeout;
77         GTimer        *scroll_time;
78         gboolean       is_group_chat;
79
80         GtkTextMark   *find_mark_previous;
81         GtkTextMark   *find_mark_next;
82         gboolean       find_wrapped;
83         gboolean       find_last_direction;
84
85         /* This is for the group chat so we know if the "other" last contact
86          * changed, so we know whether to insert a header or not.
87          */
88         EmpathyContact *last_contact;
89
90         guint          notify_system_fonts_id;
91         guint          notify_show_avatars_id;
92 };
93
94 static void     empathy_chat_view_class_init          (EmpathyChatViewClass      *klass);
95 static void     empathy_chat_view_init                (EmpathyChatView           *view);
96 static void     chat_view_finalize                   (GObject                  *object);
97 static gboolean chat_view_drag_motion                (GtkWidget                *widget,
98                                                       GdkDragContext           *context,
99                                                       gint                      x,
100                                                       gint                      y,
101                                                       guint                     time);
102 static void     chat_view_size_allocate              (GtkWidget                *widget,
103                                                       GtkAllocation            *alloc);
104 static void     chat_view_setup_tags                 (EmpathyChatView           *view);
105 static void     chat_view_system_font_update         (EmpathyChatView           *view);
106 static void     chat_view_notify_system_font_cb      (EmpathyConf               *conf,
107                                                       const gchar              *key,
108                                                       gpointer                  user_data);
109 static void     chat_view_notify_show_avatars_cb     (EmpathyConf               *conf,
110                                                       const gchar              *key,
111                                                       gpointer                  user_data);
112 static void     chat_view_populate_popup             (EmpathyChatView           *view,
113                                                       GtkMenu                  *menu,
114                                                       gpointer                  user_data);
115 static gboolean chat_view_event_cb                   (EmpathyChatView           *view,
116                                                       GdkEventMotion           *event,
117                                                       GtkTextTag               *tag);
118 static gboolean chat_view_url_event_cb               (GtkTextTag               *tag,
119                                                       GObject                  *object,
120                                                       GdkEvent                 *event,
121                                                       GtkTextIter              *iter,
122                                                       GtkTextBuffer            *buffer);
123 static void     chat_view_open_address_cb            (GtkMenuItem              *menuitem,
124                                                       const gchar              *url);
125 static void     chat_view_copy_address_cb            (GtkMenuItem              *menuitem,
126                                                       const gchar              *url);
127 static void     chat_view_clear_view_cb              (GtkMenuItem              *menuitem,
128                                                       EmpathyChatView           *view);
129 static gboolean chat_view_is_scrolled_down           (EmpathyChatView           *view);
130 static void     chat_view_theme_changed_cb           (EmpathyThemeManager       *manager,
131                                                       EmpathyChatView           *view);
132 static void     chat_view_theme_notify_cb            (EmpathyTheme              *theme,
133                                                       GParamSpec                *param,
134                                                       EmpathyChatView           *view);
135
136 G_DEFINE_TYPE (EmpathyChatView, empathy_chat_view, GTK_TYPE_TEXT_VIEW);
137
138 static void
139 empathy_chat_view_class_init (EmpathyChatViewClass *klass)
140 {
141         GObjectClass   *object_class = G_OBJECT_CLASS (klass);
142         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
143
144         object_class->finalize = chat_view_finalize;
145         widget_class->size_allocate = chat_view_size_allocate;
146         widget_class->drag_motion = chat_view_drag_motion; 
147
148         g_type_class_add_private (object_class, sizeof (EmpathyChatViewPriv));
149 }
150
151 static void
152 empathy_chat_view_init (EmpathyChatView *view)
153 {
154         EmpathyChatViewPriv *priv;
155         gboolean            show_avatars;
156
157         priv = GET_PRIV (view);
158
159         priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
160
161         priv->last_block_type = EMPATHY_CHAT_VIEW_BLOCK_NONE;
162         priv->last_timestamp = 0;
163
164         priv->allow_scrolling = TRUE;
165
166         priv->is_group_chat = FALSE;
167
168         g_object_set (view,
169                       "wrap-mode", GTK_WRAP_WORD_CHAR,
170                       "editable", FALSE,
171                       "cursor-visible", FALSE,
172                       NULL);
173
174         priv->notify_system_fonts_id =
175                 empathy_conf_notify_add (empathy_conf_get (),
176                                          "/desktop/gnome/interface/document_font_name",
177                                          chat_view_notify_system_font_cb,
178                                          view);
179         chat_view_system_font_update (view);
180
181         priv->notify_show_avatars_id =
182                 empathy_conf_notify_add (empathy_conf_get (),
183                                          EMPATHY_PREFS_UI_SHOW_AVATARS,
184                                          chat_view_notify_show_avatars_cb,
185                                          view);
186
187         chat_view_setup_tags (view);
188
189         empathy_theme_manager_apply_saved (empathy_theme_manager_get (), view);
190
191         show_avatars = FALSE;
192         empathy_conf_get_bool (empathy_conf_get (),
193                                EMPATHY_PREFS_UI_SHOW_AVATARS,
194                                &show_avatars);
195
196         empathy_theme_set_show_avatars (priv->theme, show_avatars);
197
198         g_signal_connect (view,
199                           "populate-popup",
200                           G_CALLBACK (chat_view_populate_popup),
201                           NULL);
202
203         g_signal_connect_object (empathy_theme_manager_get (),
204                                  "theme-changed",
205                                  G_CALLBACK (chat_view_theme_changed_cb),
206                                  view,
207                                  0);
208 }
209
210 static void
211 chat_view_finalize (GObject *object)
212 {
213         EmpathyChatView     *view;
214         EmpathyChatViewPriv *priv;
215
216         view = EMPATHY_CHAT_VIEW (object);
217         priv = GET_PRIV (view);
218
219         empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
220
221         empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
222         empathy_conf_notify_remove (empathy_conf_get (), priv->notify_show_avatars_id);
223
224         if (priv->last_contact) {
225                 g_object_unref (priv->last_contact);
226         }
227         if (priv->scroll_time) {
228                 g_timer_destroy (priv->scroll_time);
229         }
230         if (priv->scroll_timeout) {
231                 g_source_remove (priv->scroll_timeout);
232         }
233
234         if (priv->theme) {
235                 g_signal_handlers_disconnect_by_func (priv->theme,
236                                                       chat_view_theme_notify_cb,
237                                                       view);
238                 g_object_unref (priv->theme);
239         }
240
241         G_OBJECT_CLASS (empathy_chat_view_parent_class)->finalize (object);
242 }
243
244 static gboolean
245 chat_view_drag_motion (GtkWidget        *widget,
246                        GdkDragContext   *context,
247                        gint              x,
248                        gint              y,
249                        guint             time)
250 {
251         /* Don't handle drag motion, since we don't want the view to scroll as
252          * the result of dragging something across it.
253          */
254
255         return FALSE;
256 }
257
258 static void
259 chat_view_size_allocate (GtkWidget     *widget,
260                          GtkAllocation *alloc)
261 {
262         gboolean down;
263
264         down = chat_view_is_scrolled_down (EMPATHY_CHAT_VIEW (widget));
265
266         GTK_WIDGET_CLASS (empathy_chat_view_parent_class)->size_allocate (widget, alloc);
267
268         if (down) {
269                 GtkAdjustment *adj;
270
271                 adj = GTK_TEXT_VIEW (widget)->vadjustment;
272                 gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
273         }
274 }
275
276 static void
277 chat_view_setup_tags (EmpathyChatView *view)
278 {
279         EmpathyChatViewPriv *priv;
280         GtkTextTag         *tag;
281
282         priv = GET_PRIV (view);
283
284         gtk_text_buffer_create_tag (priv->buffer,
285                                     "cut",
286                                     NULL);
287
288         /* FIXME: Move to the theme and come up with something that looks a bit
289          * nicer.
290          */
291         gtk_text_buffer_create_tag (priv->buffer,
292                                     "highlight",
293                                     "background", "yellow",
294                                     NULL);
295
296         tag = gtk_text_buffer_create_tag (priv->buffer,
297                                           "link",
298                                           NULL);
299
300         g_signal_connect (tag,
301                           "event",
302                           G_CALLBACK (chat_view_url_event_cb),
303                           priv->buffer);
304
305         g_signal_connect (view,
306                           "motion-notify-event",
307                           G_CALLBACK (chat_view_event_cb),
308                           tag);
309 }
310
311 static void
312 chat_view_system_font_update (EmpathyChatView *view)
313 {
314         PangoFontDescription *font_description = NULL;
315         gchar                *font_name;
316
317         if (empathy_conf_get_string (empathy_conf_get (),
318                                      "/desktop/gnome/interface/document_font_name",
319                                      &font_name) && font_name) {
320                 font_description = pango_font_description_from_string (font_name);
321                 g_free (font_name);
322         } else {
323                 font_description = NULL;
324         }
325
326         gtk_widget_modify_font (GTK_WIDGET (view), font_description);
327
328         if (font_description) {
329                 pango_font_description_free (font_description);
330         }
331 }
332
333 static void
334 chat_view_notify_system_font_cb (EmpathyConf  *conf,
335                                  const gchar *key,
336                                  gpointer     user_data)
337 {
338         EmpathyChatView *view;
339         EmpathyChatViewPriv *priv;
340         gboolean        show_avatars = FALSE;
341
342         view = user_data;
343         priv = GET_PRIV (view);
344
345         chat_view_system_font_update (view);
346
347         /* Ugly, again, to adjust the vertical position of the nick... Will fix
348          * this when reworking the theme manager so that view register
349          * themselves with it instead of the other way around.
350          */
351         empathy_conf_get_bool (conf,
352                                EMPATHY_PREFS_UI_SHOW_AVATARS,
353                                &show_avatars);
354
355         empathy_theme_set_show_avatars (priv->theme, show_avatars);
356 }
357
358 static void
359 chat_view_notify_show_avatars_cb (EmpathyConf  *conf,
360                                   const gchar *key,
361                                   gpointer     user_data)
362 {
363         EmpathyChatView     *view;
364         EmpathyChatViewPriv *priv;
365         gboolean            show_avatars = FALSE;
366
367         view = user_data;
368         priv = GET_PRIV (view);
369
370         empathy_conf_get_bool (conf, key, &show_avatars);
371
372         empathy_theme_set_show_avatars (priv->theme, show_avatars);
373 }
374
375 static void
376 chat_view_populate_popup (EmpathyChatView *view,
377                           GtkMenu        *menu,
378                           gpointer        user_data)
379 {
380         EmpathyChatViewPriv *priv;
381         GtkTextTagTable    *table;
382         GtkTextTag         *tag;
383         gint                x, y;
384         GtkTextIter         iter, start, end;
385         GtkWidget          *item;
386         gchar              *str = NULL;
387
388         priv = GET_PRIV (view);
389
390         /* Clear menu item */
391         if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
392                 item = gtk_menu_item_new ();
393                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
394                 gtk_widget_show (item);
395
396                 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
397                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
398                 gtk_widget_show (item);
399
400                 g_signal_connect (item,
401                                   "activate",
402                                   G_CALLBACK (chat_view_clear_view_cb),
403                                   view);
404         }
405
406         /* Link context menu items */
407         table = gtk_text_buffer_get_tag_table (priv->buffer);
408         tag = gtk_text_tag_table_lookup (table, "link");
409
410         gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
411
412         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
413                                                GTK_TEXT_WINDOW_WIDGET,
414                                                x, y,
415                                                &x, &y);
416
417         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
418
419         start = end = iter;
420
421         if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
422             gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
423                 str = gtk_text_buffer_get_text (priv->buffer,
424                                                 &start, &end, FALSE);
425         }
426
427         if (G_STR_EMPTY (str)) {
428                 g_free (str);
429                 return;
430         }
431
432         /* NOTE: Set data just to get the string freed when not needed. */
433         g_object_set_data_full (G_OBJECT (menu),
434                                 "url", str,
435                                 (GDestroyNotify) g_free);
436
437         item = gtk_menu_item_new ();
438         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
439         gtk_widget_show (item);
440
441         item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
442         g_signal_connect (item,
443                           "activate",
444                           G_CALLBACK (chat_view_copy_address_cb),
445                           str);
446         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
447         gtk_widget_show (item);
448
449         item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
450         g_signal_connect (item,
451                           "activate",
452                           G_CALLBACK (chat_view_open_address_cb),
453                           str);
454         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
455         gtk_widget_show (item);
456 }
457
458 static gboolean
459 chat_view_event_cb (EmpathyChatView *view,
460                     GdkEventMotion *event,
461                     GtkTextTag     *tag)
462 {
463         static GdkCursor  *hand = NULL;
464         static GdkCursor  *beam = NULL;
465         GtkTextWindowType  type;
466         GtkTextIter        iter;
467         GdkWindow         *win;
468         gint               x, y, buf_x, buf_y;
469
470         type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
471                                               event->window);
472
473         if (type != GTK_TEXT_WINDOW_TEXT) {
474                 return FALSE;
475         }
476
477         /* Get where the pointer really is. */
478         win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
479         if (!win) {
480                 return FALSE;
481         }
482
483         gdk_window_get_pointer (win, &x, &y, NULL);
484
485         /* Get the iter where the cursor is at */
486         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
487                                                x, y,
488                                                &buf_x, &buf_y);
489
490         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
491                                             &iter,
492                                             buf_x, buf_y);
493
494         if (gtk_text_iter_has_tag (&iter, tag)) {
495                 if (!hand) {
496                         hand = gdk_cursor_new (GDK_HAND2);
497                         beam = gdk_cursor_new (GDK_XTERM);
498                 }
499                 gdk_window_set_cursor (win, hand);
500         } else {
501                 if (!beam) {
502                         beam = gdk_cursor_new (GDK_XTERM);
503                 }
504                 gdk_window_set_cursor (win, beam);
505         }
506
507         return FALSE;
508 }
509
510 static gboolean
511 chat_view_url_event_cb (GtkTextTag    *tag,
512                         GObject       *object,
513                         GdkEvent      *event,
514                         GtkTextIter   *iter,
515                         GtkTextBuffer *buffer)
516 {
517         GtkTextIter  start, end;
518         gchar       *str;
519
520         /* If the link is being selected, don't do anything. */
521         gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
522         if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
523                 return FALSE;
524         }
525
526         if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
527                 start = end = *iter;
528
529                 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
530                     gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
531                         str = gtk_text_buffer_get_text (buffer,
532                                                         &start,
533                                                         &end,
534                                                         FALSE);
535
536                         empathy_url_show (str);
537                         g_free (str);
538                 }
539         }
540
541         return FALSE;
542 }
543
544 static void
545 chat_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
546 {
547         empathy_url_show (url);
548 }
549
550 static void
551 chat_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
552 {
553         GtkClipboard *clipboard;
554
555         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
556         gtk_clipboard_set_text (clipboard, url, -1);
557
558         clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
559         gtk_clipboard_set_text (clipboard, url, -1);
560 }
561
562 static void
563 chat_view_clear_view_cb (GtkMenuItem *menuitem, EmpathyChatView *view)
564 {
565         empathy_chat_view_clear (view);
566 }
567
568 static gboolean
569 chat_view_is_scrolled_down (EmpathyChatView *view)
570 {
571         GtkWidget *sw;
572
573         sw = gtk_widget_get_parent (GTK_WIDGET (view));
574         if (GTK_IS_SCROLLED_WINDOW (sw)) {
575                 GtkAdjustment *vadj;
576
577                 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
578
579                 if (vadj->value + vadj->page_size / 2 < vadj->upper - vadj->page_size) {
580                         return FALSE;
581                 }
582         }
583
584         return TRUE;
585 }
586
587 static void
588 chat_view_maybe_trim_buffer (EmpathyChatView *view)
589 {
590         EmpathyChatViewPriv *priv;
591         GtkTextIter         top, bottom;
592         gint                line;
593         gint                remove;
594         GtkTextTagTable    *table;
595         GtkTextTag         *tag;
596
597         priv = GET_PRIV (view);
598
599         gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
600         line = gtk_text_iter_get_line (&bottom);
601         if (line < MAX_LINES) {
602                 return;
603         }
604
605         remove = line - MAX_LINES;
606         gtk_text_buffer_get_start_iter (priv->buffer, &top);
607
608         bottom = top;
609         if (!gtk_text_iter_forward_lines (&bottom, remove)) {
610                 return;
611         }
612
613         /* Track backwords to a place where we can safely cut, we don't do it in
614          * the middle of a tag.
615          */
616         table = gtk_text_buffer_get_tag_table (priv->buffer);
617         tag = gtk_text_tag_table_lookup (table, "cut");
618         if (!tag) {
619                 return;
620         }
621
622         if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
623                 return;
624         }
625
626         if (!gtk_text_iter_equal (&top, &bottom)) {
627                 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
628         }
629 }
630
631 static void
632 chat_view_theme_changed_cb (EmpathyThemeManager *manager,
633                             EmpathyChatView     *view)
634 {
635         EmpathyChatViewPriv *priv;
636         gboolean            show_avatars = FALSE;
637         gboolean            theme_rooms = FALSE;
638
639         priv = GET_PRIV (view);
640
641         priv->last_block_type = EMPATHY_CHAT_VIEW_BLOCK_NONE;
642
643         empathy_conf_get_bool (empathy_conf_get (),
644                               EMPATHY_PREFS_CHAT_THEME_CHAT_ROOM,
645                               &theme_rooms);
646         if (!theme_rooms && priv->is_group_chat) {
647                 empathy_theme_manager_apply (manager, view, NULL);
648         } else {
649                 empathy_theme_manager_apply_saved (manager, view);
650         }
651
652         /* Needed for now to update the "rise" property of the names to get it
653          * vertically centered.
654          */
655         empathy_conf_get_bool (empathy_conf_get (),
656                                EMPATHY_PREFS_UI_SHOW_AVATARS,
657                                &show_avatars);
658         empathy_theme_set_show_avatars (priv->theme, show_avatars);
659 }
660
661 /* Pads a pixbuf to the specified size, by centering it in a larger transparent
662  * pixbuf. Returns a new ref.
663  */
664 static GdkPixbuf *
665 chat_view_pad_to_size (GdkPixbuf *pixbuf,
666                        gint       width,
667                        gint       height,
668                        gint       extra_padding_right)
669 {
670         gint       src_width, src_height;
671         GdkPixbuf *padded;
672         gint       x_offset, y_offset;
673
674         src_width = gdk_pixbuf_get_width (pixbuf);
675         src_height = gdk_pixbuf_get_height (pixbuf);
676
677         x_offset = (width - src_width) / 2;
678         y_offset = (height - src_height) / 2;
679
680         padded = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (pixbuf),
681                                  TRUE, /* alpha */
682                                  gdk_pixbuf_get_bits_per_sample (pixbuf),
683                                  width + extra_padding_right,
684                                  height);
685
686         gdk_pixbuf_fill (padded, 0);
687
688         gdk_pixbuf_copy_area (pixbuf,
689                               0, /* source coords */
690                               0,
691                               src_width,
692                               src_height,
693                               padded,
694                               x_offset, /* dest coords */
695                               y_offset);
696
697         return padded;
698 }
699
700 typedef struct {
701         GdkPixbuf *pixbuf;
702         gchar     *token;
703 } AvatarData;
704
705 static void
706 chat_view_avatar_cache_data_free (gpointer ptr)
707 {
708         AvatarData *data = ptr;
709
710         g_object_unref (data->pixbuf);
711         g_free (data->token);
712         g_slice_free (AvatarData, data);
713 }
714
715 GdkPixbuf *
716 empathy_chat_view_get_avatar_pixbuf_with_cache (EmpathyContact *contact)
717 {
718         static GHashTable *avatar_cache = NULL;
719         AvatarData        *data;
720         EmpathyAvatar     *avatar;
721         GdkPixbuf         *tmp_pixbuf;
722         GdkPixbuf         *pixbuf = NULL;
723
724         /* Init avatar cache */
725         if (!avatar_cache) {
726                 avatar_cache = g_hash_table_new_full (empathy_contact_hash,
727                                                       empathy_contact_equal,
728                                                       g_object_unref,
729                                                       chat_view_avatar_cache_data_free);
730         }
731
732         /* Check if avatar is in cache and if it's up to date */
733         avatar = empathy_contact_get_avatar (contact);
734         data = g_hash_table_lookup (avatar_cache, contact);
735         if (data) {
736                 if (avatar && !tp_strdiff (avatar->token, data->token)) {
737                         /* We have the avatar in cache */
738                         return data->pixbuf;
739                 }
740
741                 /* The cache is outdate */
742                 g_hash_table_remove (avatar_cache, contact);
743         }
744
745         /* Avatar not in cache, create pixbuf */
746         tmp_pixbuf = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
747         if (tmp_pixbuf) {
748                 pixbuf = chat_view_pad_to_size (tmp_pixbuf, 32, 32, 6);
749                 g_object_unref (tmp_pixbuf);
750         }
751         if (!pixbuf) {
752                 return NULL;
753         }
754
755         /* Insert new pixbuf in cache */
756         data = g_slice_new0 (AvatarData);
757         data->token = g_strdup (avatar->token);
758         data->pixbuf = pixbuf;
759
760         g_hash_table_insert (avatar_cache,
761                              g_object_ref (contact),
762                              data);
763
764         return data->pixbuf;
765 }
766
767 EmpathyChatView *
768 empathy_chat_view_new (void)
769 {
770         return g_object_new (EMPATHY_TYPE_CHAT_VIEW, NULL);
771 }
772
773 void
774 empathy_chat_view_append_message (EmpathyChatView *view,
775                                   EmpathyMessage  *msg)
776 {
777         EmpathyChatViewPriv *priv = GET_PRIV (view);
778         EmpathyContact      *sender;
779         gboolean             bottom;
780         gboolean             from_self;
781
782         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
783         g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
784
785         if (!empathy_message_get_body (msg)) {
786                 return;
787         }
788
789         bottom = chat_view_is_scrolled_down (view);
790         sender = empathy_message_get_sender (msg);
791         from_self = empathy_contact_is_user (sender);
792         
793         chat_view_maybe_trim_buffer (view);
794
795         empathy_theme_append_message (priv->theme, view, msg);
796
797         if (bottom) {
798                 empathy_chat_view_scroll_down (view);
799         }
800 }
801
802 void
803 empathy_chat_view_append_event (EmpathyChatView *view,
804                                const gchar    *str)
805 {
806         EmpathyChatViewPriv *priv;
807         gboolean            bottom;
808
809         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
810         g_return_if_fail (!G_STR_EMPTY (str));
811
812         priv = GET_PRIV (view);
813
814         bottom = chat_view_is_scrolled_down (view);
815
816         chat_view_maybe_trim_buffer (view);
817
818         empathy_theme_append_event (priv->theme, view, str);
819
820         if (bottom) {
821                 empathy_chat_view_scroll_down (view);
822         }
823
824         priv->last_block_type = EMPATHY_CHAT_VIEW_BLOCK_EVENT;
825 }
826
827 void
828 empathy_chat_view_append_button (EmpathyChatView *view,
829                                 const gchar    *message,
830                                 GtkWidget      *button1,
831                                 GtkWidget      *button2)
832 {
833         EmpathyChatViewPriv   *priv;
834         GtkTextChildAnchor   *anchor;
835         GtkTextIter           iter;
836         gboolean              bottom;
837         const gchar          *tag;
838
839         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
840         g_return_if_fail (button1 != NULL);
841
842         priv = GET_PRIV (view);
843
844         tag = "invite";
845
846         bottom = chat_view_is_scrolled_down (view);
847
848         empathy_theme_append_timestamp (priv->theme, view, NULL, TRUE, TRUE);
849
850         if (message) {
851                 empathy_theme_append_text (priv->theme, view, message, tag, NULL);
852         }
853
854         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
855
856         anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
857         gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button1, anchor);
858         gtk_widget_show (button1);
859
860         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
861                                                   &iter,
862                                                   " ",
863                                                   1,
864                                                   tag,
865                                                   NULL);
866
867         if (button2) {
868                 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
869                 
870                 anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
871                 gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button2, anchor);
872                 gtk_widget_show (button2);
873                 
874                 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
875                                                           &iter,
876                                                           " ",
877                                                           1,
878                                                           tag,
879                                                           NULL);
880         }
881
882         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
883         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
884                                                   &iter,
885                                                   "\n\n",
886                                                   2,
887                                                   tag,
888                                                   NULL);
889
890         if (bottom) {
891                 empathy_chat_view_scroll_down (view);
892         }
893
894         priv->last_block_type = EMPATHY_CHAT_VIEW_BLOCK_INVITE;
895 }
896
897 void
898 empathy_chat_view_scroll (EmpathyChatView *view,
899                          gboolean        allow_scrolling)
900 {
901         EmpathyChatViewPriv *priv;
902
903         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
904
905         priv = GET_PRIV (view);
906
907         priv->allow_scrolling = allow_scrolling;
908
909         empathy_debug (DEBUG_DOMAIN, "Scrolling %s",
910                       allow_scrolling ? "enabled" : "disabled");
911 }
912
913 /* Code stolen from pidgin/gtkimhtml.c */
914 static gboolean
915 chat_view_scroll_cb (EmpathyChatView *view)
916 {
917         EmpathyChatViewPriv *priv;
918         GtkAdjustment      *adj;
919         gdouble             max_val;
920
921         priv = GET_PRIV (view);
922         adj = GTK_TEXT_VIEW (view)->vadjustment;
923         max_val = adj->upper - adj->page_size;
924
925         g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
926
927         if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
928                 /* time's up. jump to the end and kill the timer */
929                 gtk_adjustment_set_value (adj, max_val);
930                 g_timer_destroy (priv->scroll_time);
931                 priv->scroll_time = NULL;
932                 priv->scroll_timeout = 0;
933                 return FALSE;
934         }
935
936         /* scroll by 1/3rd the remaining distance */
937         gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
938         return TRUE;
939 }
940
941 void
942 empathy_chat_view_scroll_down (EmpathyChatView *view)
943 {
944         EmpathyChatViewPriv *priv;
945
946         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
947
948         priv = GET_PRIV (view);
949
950         if (!priv->allow_scrolling) {
951                 return;
952         }
953
954         empathy_debug (DEBUG_DOMAIN, "Scrolling down");
955
956         if (priv->scroll_time) {
957                 g_timer_reset (priv->scroll_time);
958         } else {
959                 priv->scroll_time = g_timer_new();
960         }
961         if (!priv->scroll_timeout) {
962                 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
963                                                       (GSourceFunc) chat_view_scroll_cb,
964                                                       view);
965         }
966 }
967
968 gboolean
969 empathy_chat_view_get_selection_bounds (EmpathyChatView *view,
970                                        GtkTextIter    *start,
971                                        GtkTextIter    *end)
972 {
973         GtkTextBuffer *buffer;
974
975         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
976
977         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
978
979         return gtk_text_buffer_get_selection_bounds (buffer, start, end);
980 }
981
982 void
983 empathy_chat_view_clear (EmpathyChatView *view)
984 {
985         GtkTextBuffer      *buffer;
986         EmpathyChatViewPriv *priv;
987
988         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
989
990         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
991         gtk_text_buffer_set_text (buffer, "", -1);
992
993         /* We set these back to the initial values so we get
994          * timestamps when clearing the window to know when
995          * conversations start.
996          */
997         priv = GET_PRIV (view);
998
999         priv->last_block_type = EMPATHY_CHAT_VIEW_BLOCK_NONE;
1000         priv->last_timestamp = 0;
1001 }
1002
1003 gboolean
1004 empathy_chat_view_find_previous (EmpathyChatView *view,
1005                                 const gchar    *search_criteria,
1006                                 gboolean        new_search)
1007 {
1008         EmpathyChatViewPriv *priv;
1009         GtkTextBuffer      *buffer;
1010         GtkTextIter         iter_at_mark;
1011         GtkTextIter         iter_match_start;
1012         GtkTextIter         iter_match_end;
1013         gboolean            found;
1014         gboolean            from_start = FALSE;
1015
1016         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1017         g_return_val_if_fail (search_criteria != NULL, FALSE);
1018
1019         priv = GET_PRIV (view);
1020
1021         buffer = priv->buffer;
1022
1023         if (G_STR_EMPTY (search_criteria)) {
1024                 if (priv->find_mark_previous) {
1025                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1026
1027                         gtk_text_buffer_move_mark (buffer,
1028                                                    priv->find_mark_previous,
1029                                                    &iter_at_mark);
1030                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1031                                                       priv->find_mark_previous,
1032                                                       0.0,
1033                                                       TRUE,
1034                                                       0.0,
1035                                                       0.0);
1036                         gtk_text_buffer_select_range (buffer,
1037                                                       &iter_at_mark,
1038                                                       &iter_at_mark);
1039                 }
1040
1041                 return FALSE;
1042         }
1043
1044         if (new_search) {
1045                 from_start = TRUE;
1046         }
1047
1048         if (priv->find_mark_previous) {
1049                 gtk_text_buffer_get_iter_at_mark (buffer,
1050                                                   &iter_at_mark,
1051                                                   priv->find_mark_previous);
1052         } else {
1053                 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
1054                 from_start = TRUE;
1055         }
1056
1057         priv->find_last_direction = FALSE;
1058
1059         found = empathy_text_iter_backward_search (&iter_at_mark,
1060                                                   search_criteria,
1061                                                   &iter_match_start,
1062                                                   &iter_match_end,
1063                                                   NULL);
1064
1065         if (!found) {
1066                 gboolean result = FALSE;
1067
1068                 if (from_start) {
1069                         return result;
1070                 }
1071
1072                 /* Here we wrap around. */
1073                 if (!new_search && !priv->find_wrapped) {
1074                         priv->find_wrapped = TRUE;
1075                         result = empathy_chat_view_find_previous (view, 
1076                                                                  search_criteria, 
1077                                                                  FALSE);
1078                         priv->find_wrapped = FALSE;
1079                 }
1080
1081                 return result;
1082         }
1083
1084         /* Set new mark and show on screen */
1085         if (!priv->find_mark_previous) {
1086                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1087                                                                         &iter_match_start,
1088                                                                         TRUE);
1089         } else {
1090                 gtk_text_buffer_move_mark (buffer,
1091                                            priv->find_mark_previous,
1092                                            &iter_match_start);
1093         }
1094
1095         if (!priv->find_mark_next) {
1096                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1097                                                                     &iter_match_end,
1098                                                                     TRUE);
1099         } else {
1100                 gtk_text_buffer_move_mark (buffer,
1101                                            priv->find_mark_next,
1102                                            &iter_match_end);
1103         }
1104
1105         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1106                                       priv->find_mark_previous,
1107                                       0.0,
1108                                       TRUE,
1109                                       0.5,
1110                                       0.5);
1111
1112         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1113         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1114
1115         return TRUE;
1116 }
1117
1118 gboolean
1119 empathy_chat_view_find_next (EmpathyChatView *view,
1120                             const gchar    *search_criteria,
1121                             gboolean        new_search)
1122 {
1123         EmpathyChatViewPriv *priv;
1124         GtkTextBuffer      *buffer;
1125         GtkTextIter         iter_at_mark;
1126         GtkTextIter         iter_match_start;
1127         GtkTextIter         iter_match_end;
1128         gboolean            found;
1129         gboolean            from_start = FALSE;
1130
1131         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1132         g_return_val_if_fail (search_criteria != NULL, FALSE);
1133
1134         priv = GET_PRIV (view);
1135
1136         buffer = priv->buffer;
1137
1138         if (G_STR_EMPTY (search_criteria)) {
1139                 if (priv->find_mark_next) {
1140                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1141
1142                         gtk_text_buffer_move_mark (buffer,
1143                                                    priv->find_mark_next,
1144                                                    &iter_at_mark);
1145                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1146                                                       priv->find_mark_next,
1147                                                       0.0,
1148                                                       TRUE,
1149                                                       0.0,
1150                                                       0.0);
1151                         gtk_text_buffer_select_range (buffer,
1152                                                       &iter_at_mark,
1153                                                       &iter_at_mark);
1154                 }
1155
1156                 return FALSE;
1157         }
1158
1159         if (new_search) {
1160                 from_start = TRUE;
1161         }
1162
1163         if (priv->find_mark_next) {
1164                 gtk_text_buffer_get_iter_at_mark (buffer,
1165                                                   &iter_at_mark,
1166                                                   priv->find_mark_next);
1167         } else {
1168                 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1169                 from_start = TRUE;
1170         }
1171
1172         priv->find_last_direction = TRUE;
1173
1174         found = empathy_text_iter_forward_search (&iter_at_mark,
1175                                                  search_criteria,
1176                                                  &iter_match_start,
1177                                                  &iter_match_end,
1178                                                  NULL);
1179
1180         if (!found) {
1181                 gboolean result = FALSE;
1182
1183                 if (from_start) {
1184                         return result;
1185                 }
1186
1187                 /* Here we wrap around. */
1188                 if (!new_search && !priv->find_wrapped) {
1189                         priv->find_wrapped = TRUE;
1190                         result = empathy_chat_view_find_next (view, 
1191                                                              search_criteria, 
1192                                                              FALSE);
1193                         priv->find_wrapped = FALSE;
1194                 }
1195
1196                 return result;
1197         }
1198
1199         /* Set new mark and show on screen */
1200         if (!priv->find_mark_next) {
1201                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1202                                                                &iter_match_end,
1203                                                                TRUE);
1204         } else {
1205                 gtk_text_buffer_move_mark (buffer,
1206                                            priv->find_mark_next,
1207                                            &iter_match_end);
1208         }
1209
1210         if (!priv->find_mark_previous) {
1211                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1212                                                                         &iter_match_start,
1213                                                                         TRUE);
1214         } else {
1215                 gtk_text_buffer_move_mark (buffer,
1216                                            priv->find_mark_previous,
1217                                            &iter_match_start);
1218         }
1219
1220         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1221                                       priv->find_mark_next,
1222                                       0.0,
1223                                       TRUE,
1224                                       0.5,
1225                                       0.5);
1226
1227         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1228         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1229
1230         return TRUE;
1231 }
1232
1233
1234 void
1235 empathy_chat_view_find_abilities (EmpathyChatView *view,
1236                                  const gchar    *search_criteria,
1237                                  gboolean       *can_do_previous,
1238                                  gboolean       *can_do_next)
1239 {
1240         EmpathyChatViewPriv *priv;
1241         GtkTextBuffer      *buffer;
1242         GtkTextIter         iter_at_mark;
1243         GtkTextIter         iter_match_start;
1244         GtkTextIter         iter_match_end;
1245
1246         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1247         g_return_if_fail (search_criteria != NULL);
1248         g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1249
1250         priv = GET_PRIV (view);
1251
1252         buffer = priv->buffer;
1253
1254         if (can_do_previous) {
1255                 if (priv->find_mark_previous) {
1256                         gtk_text_buffer_get_iter_at_mark (buffer,
1257                                                           &iter_at_mark,
1258                                                           priv->find_mark_previous);
1259                 } else {
1260                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1261                 }
1262                 
1263                 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1264                                                                      search_criteria,
1265                                                                      &iter_match_start,
1266                                                                      &iter_match_end,
1267                                                                      NULL);
1268         }
1269
1270         if (can_do_next) {
1271                 if (priv->find_mark_next) {
1272                         gtk_text_buffer_get_iter_at_mark (buffer,
1273                                                           &iter_at_mark,
1274                                                           priv->find_mark_next);
1275                 } else {
1276                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1277                 }
1278                 
1279                 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1280                                                                 search_criteria,
1281                                                                 &iter_match_start,
1282                                                                 &iter_match_end,
1283                                                                 NULL);
1284         }
1285 }
1286
1287 void
1288 empathy_chat_view_highlight (EmpathyChatView *view,
1289                              const gchar     *text)
1290 {
1291         GtkTextBuffer *buffer;
1292         GtkTextIter    iter;
1293         GtkTextIter    iter_start;
1294         GtkTextIter    iter_end;
1295         GtkTextIter    iter_match_start;
1296         GtkTextIter    iter_match_end;
1297         gboolean       found;
1298
1299         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1300
1301         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1302
1303         gtk_text_buffer_get_start_iter (buffer, &iter);
1304
1305         gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1306         gtk_text_buffer_remove_tag_by_name (buffer, "highlight",
1307                                             &iter_start,
1308                                             &iter_end);
1309
1310         if (G_STR_EMPTY (text)) {
1311                 return;
1312         }
1313
1314         while (1) {
1315                 found = empathy_text_iter_forward_search (&iter,
1316                                                          text,
1317                                                          &iter_match_start,
1318                                                          &iter_match_end,
1319                                                          NULL);
1320
1321                 if (!found) {
1322                         break;
1323                 }
1324
1325                 gtk_text_buffer_apply_tag_by_name (buffer, "highlight",
1326                                                    &iter_match_start,
1327                                                    &iter_match_end);
1328
1329                 iter = iter_match_end;
1330                 gtk_text_iter_forward_char (&iter);
1331         }
1332 }
1333
1334 void
1335 empathy_chat_view_copy_clipboard (EmpathyChatView *view)
1336 {
1337         GtkTextBuffer *buffer;
1338         GtkClipboard  *clipboard;
1339
1340         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1341
1342         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1343         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1344
1345         gtk_text_buffer_copy_clipboard (buffer, clipboard);
1346 }
1347
1348 EmpathyTheme *
1349 empathy_chat_view_get_theme (EmpathyChatView *view)
1350 {
1351         EmpathyChatViewPriv *priv;
1352
1353         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1354
1355         priv = GET_PRIV (view);
1356
1357         return priv->theme;
1358 }
1359
1360 static void
1361 chat_view_theme_notify_cb (EmpathyTheme    *theme,
1362                            GParamSpec      *param,
1363                            EmpathyChatView *view)
1364 {
1365         empathy_theme_update_view (theme, view);
1366 }
1367
1368 void
1369 empathy_chat_view_set_theme (EmpathyChatView *view, EmpathyTheme *theme)
1370 {
1371         EmpathyChatViewPriv *priv;
1372
1373         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1374         g_return_if_fail (EMPATHY_IS_THEME (theme));
1375
1376         priv = GET_PRIV (view);
1377
1378         if (priv->theme) {
1379                 g_signal_handlers_disconnect_by_func (priv->theme,
1380                                                       chat_view_theme_notify_cb,
1381                                                       view);
1382                 g_object_unref (priv->theme);
1383         }
1384
1385         priv->theme = g_object_ref (theme);
1386
1387         empathy_theme_update_view (theme, view);
1388         g_signal_connect (priv->theme, "notify",
1389                           G_CALLBACK (chat_view_theme_notify_cb),
1390                           view);
1391
1392         /* FIXME: Redraw all messages using the new theme */
1393 }
1394
1395 void
1396 empathy_chat_view_set_margin (EmpathyChatView *view,
1397                              gint            margin)
1398 {
1399         EmpathyChatViewPriv *priv;
1400
1401         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1402
1403         priv = GET_PRIV (view);
1404
1405         g_object_set (view,
1406                       "left-margin", margin,
1407                       "right-margin", margin,
1408                       NULL);
1409 }
1410
1411 GtkWidget *
1412 empathy_chat_view_get_smiley_menu (GCallback    callback,
1413                                    gpointer     user_data)
1414 {
1415         EmpathySmileyManager *smiley_manager;
1416         GSList               *smileys, *l;
1417         GtkWidget            *menu;
1418         gint                  x = 0;
1419         gint                  y = 0;
1420
1421         g_return_val_if_fail (callback != NULL, NULL);
1422
1423         menu = gtk_menu_new ();
1424
1425         smiley_manager = empathy_smiley_manager_new ();
1426         smileys = empathy_smiley_manager_get_all (smiley_manager);
1427         for (l = smileys; l; l = l->next) {
1428                 EmpathySmiley *smiley;
1429                 GtkWidget     *item;
1430                 GtkWidget     *image;
1431
1432                 smiley = l->data;
1433                 image = gtk_image_new_from_pixbuf (smiley->pixbuf);
1434
1435                 item = gtk_image_menu_item_new_with_label ("");
1436                 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1437
1438                 gtk_menu_attach (GTK_MENU (menu), item,
1439                                  x, x + 1, y, y + 1);
1440
1441                 gtk_widget_set_tooltip_text (item, smiley->str);
1442
1443                 g_object_set_data  (G_OBJECT (item), "smiley_text", smiley->str);
1444                 g_signal_connect (item, "activate", callback, user_data);
1445
1446                 if (x > 3) {
1447                         y++;
1448                         x = 0;
1449                 } else {
1450                         x++;
1451                 }
1452         }
1453         g_object_unref (smiley_manager);
1454
1455         gtk_widget_show_all (menu);
1456
1457         return menu;
1458 }
1459
1460 /* FIXME: Do we really need this? Better to do it internally only at setup time,
1461  * we will never change it on the fly.
1462  */
1463 void
1464 empathy_chat_view_set_is_group_chat (EmpathyChatView *view,
1465                                     gboolean        is_group_chat)
1466 {
1467         EmpathyChatViewPriv *priv;
1468         gboolean            theme_rooms = FALSE;
1469
1470         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1471
1472         priv = GET_PRIV (view);
1473
1474         priv->is_group_chat = is_group_chat;
1475
1476         empathy_conf_get_bool (empathy_conf_get (),
1477                               EMPATHY_PREFS_CHAT_THEME_CHAT_ROOM,
1478                               &theme_rooms);
1479
1480         if (!theme_rooms && is_group_chat) {
1481                 empathy_theme_manager_apply (empathy_theme_manager_get (),
1482                                             view,
1483                                             NULL);
1484         } else {
1485                 empathy_theme_manager_apply_saved (empathy_theme_manager_get (),
1486                                                   view);
1487         }
1488 }
1489
1490 time_t
1491 empathy_chat_view_get_last_timestamp (EmpathyChatView *view)
1492 {
1493         EmpathyChatViewPriv *priv;
1494
1495         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), 0);
1496
1497         priv = GET_PRIV (view);
1498
1499         return priv->last_timestamp;
1500 }
1501
1502 void
1503 empathy_chat_view_set_last_timestamp (EmpathyChatView *view,
1504                                      time_t          timestamp)
1505 {
1506         EmpathyChatViewPriv *priv;
1507
1508         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1509
1510         priv = GET_PRIV (view);
1511
1512         priv->last_timestamp = timestamp;
1513 }
1514
1515 EmpathyChatViewBlock
1516 empathy_chat_view_get_last_block_type (EmpathyChatView *view)
1517 {
1518         EmpathyChatViewPriv *priv;
1519
1520         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), 0);
1521
1522         priv = GET_PRIV (view);
1523
1524         return priv->last_block_type;
1525 }
1526
1527 void
1528 empathy_chat_view_set_last_block_type (EmpathyChatView      *view, 
1529                                        EmpathyChatViewBlock  block_type)
1530 {
1531         EmpathyChatViewPriv *priv;
1532
1533         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1534
1535         priv = GET_PRIV (view);
1536
1537         priv->last_block_type = block_type;
1538 }
1539
1540 EmpathyContact *
1541 empathy_chat_view_get_last_contact (EmpathyChatView *view)
1542 {
1543         EmpathyChatViewPriv *priv;
1544
1545         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1546
1547         priv = GET_PRIV (view);
1548
1549         return priv->last_contact;
1550 }
1551
1552 void
1553 empathy_chat_view_set_last_contact (EmpathyChatView *view, EmpathyContact *contact)
1554 {
1555         EmpathyChatViewPriv *priv;
1556
1557         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1558
1559         priv = GET_PRIV (view);
1560
1561         if (priv->last_contact) {
1562                 g_object_unref (priv->last_contact);
1563                 priv->last_contact = NULL;
1564         }
1565
1566         if (contact) {
1567                 priv->last_contact = g_object_ref (contact);
1568         }
1569 }
1570