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