]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-chat-view.c
Use gi18n-lib.h instead of gi18n.h for libraries.
[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-lib.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) EMPATHY_GET_PRIV (obj, EmpathyChatView)
65 typedef struct {
66         GtkTextBuffer *buffer;
67
68         EmpathyTheme   *theme;
69
70         time_t         last_timestamp;
71
72         gboolean       allow_scrolling;
73         guint          scroll_timeout;
74         GTimer        *scroll_time;
75
76         GtkTextMark   *find_mark_previous;
77         GtkTextMark   *find_mark_next;
78         gboolean       find_wrapped;
79         gboolean       find_last_direction;
80
81         /* This is for the group chat so we know if the "other" last contact
82          * changed, so we know whether to insert a header or not.
83          */
84         EmpathyContact *last_contact;
85
86         guint          notify_system_fonts_id;
87         guint          notify_show_avatars_id;
88 } EmpathyChatViewPriv;
89
90 static void     chat_view_finalize                   (GObject                  *object);
91 static gboolean chat_view_drag_motion                (GtkWidget                *widget,
92                                                       GdkDragContext           *context,
93                                                       gint                      x,
94                                                       gint                      y,
95                                                       guint                     time);
96 static void     chat_view_size_allocate              (GtkWidget                *widget,
97                                                       GtkAllocation            *alloc);
98 static void     chat_view_setup_tags                 (EmpathyChatView           *view);
99 static void     chat_view_system_font_update         (EmpathyChatView           *view);
100 static void     chat_view_notify_system_font_cb      (EmpathyConf               *conf,
101                                                       const gchar              *key,
102                                                       gpointer                  user_data);
103 static void     chat_view_notify_show_avatars_cb     (EmpathyConf               *conf,
104                                                       const gchar              *key,
105                                                       gpointer                  user_data);
106 static void     chat_view_populate_popup             (EmpathyChatView           *view,
107                                                       GtkMenu                  *menu,
108                                                       gpointer                  user_data);
109 static gboolean chat_view_event_cb                   (EmpathyChatView           *view,
110                                                       GdkEventMotion           *event,
111                                                       GtkTextTag               *tag);
112 static gboolean chat_view_url_event_cb               (GtkTextTag               *tag,
113                                                       GObject                  *object,
114                                                       GdkEvent                 *event,
115                                                       GtkTextIter              *iter,
116                                                       EmpathyChatView          *view);
117 static void     chat_view_open_address_cb            (GtkMenuItem              *menuitem,
118                                                       const gchar              *url);
119 static void     chat_view_copy_address_cb            (GtkMenuItem              *menuitem,
120                                                       const gchar              *url);
121 static void     chat_view_clear_view_cb              (GtkMenuItem              *menuitem,
122                                                       EmpathyChatView           *view);
123 static gboolean chat_view_is_scrolled_down           (EmpathyChatView           *view);
124 static void     chat_view_theme_changed_cb           (EmpathyThemeManager       *manager,
125                                                       EmpathyChatView           *view);
126 static void     chat_view_theme_notify_cb            (EmpathyTheme              *theme,
127                                                       GParamSpec                *param,
128                                                       EmpathyChatView           *view);
129
130 G_DEFINE_TYPE (EmpathyChatView, empathy_chat_view, GTK_TYPE_TEXT_VIEW);
131
132 static void
133 empathy_chat_view_class_init (EmpathyChatViewClass *klass)
134 {
135         GObjectClass   *object_class = G_OBJECT_CLASS (klass);
136         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
137
138         object_class->finalize = chat_view_finalize;
139         widget_class->size_allocate = chat_view_size_allocate;
140         widget_class->drag_motion = chat_view_drag_motion; 
141
142         g_type_class_add_private (object_class, sizeof (EmpathyChatViewPriv));
143 }
144
145 static void
146 empathy_chat_view_init (EmpathyChatView *view)
147 {
148         gboolean             show_avatars;
149         EmpathyChatViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
150                 EMPATHY_TYPE_CHAT_VIEW, EmpathyChatViewPriv);
151
152         view->priv = priv;
153         priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
154         priv->last_timestamp = 0;
155         priv->allow_scrolling = TRUE;
156
157         g_object_set (view,
158                       "wrap-mode", GTK_WRAP_WORD_CHAR,
159                       "editable", FALSE,
160                       "cursor-visible", FALSE,
161                       NULL);
162
163         priv->notify_system_fonts_id =
164                 empathy_conf_notify_add (empathy_conf_get (),
165                                          "/desktop/gnome/interface/document_font_name",
166                                          chat_view_notify_system_font_cb,
167                                          view);
168         chat_view_system_font_update (view);
169
170         priv->notify_show_avatars_id =
171                 empathy_conf_notify_add (empathy_conf_get (),
172                                          EMPATHY_PREFS_UI_SHOW_AVATARS,
173                                          chat_view_notify_show_avatars_cb,
174                                          view);
175
176         chat_view_setup_tags (view);
177
178         empathy_theme_manager_apply_saved (empathy_theme_manager_get (), view);
179
180         show_avatars = FALSE;
181         empathy_conf_get_bool (empathy_conf_get (),
182                                EMPATHY_PREFS_UI_SHOW_AVATARS,
183                                &show_avatars);
184
185         empathy_theme_set_show_avatars (priv->theme, show_avatars);
186
187         g_signal_connect (view,
188                           "populate-popup",
189                           G_CALLBACK (chat_view_populate_popup),
190                           NULL);
191
192         g_signal_connect_object (empathy_theme_manager_get (),
193                                  "theme-changed",
194                                  G_CALLBACK (chat_view_theme_changed_cb),
195                                  view,
196                                  0);
197 }
198
199 static void
200 chat_view_finalize (GObject *object)
201 {
202         EmpathyChatView     *view;
203         EmpathyChatViewPriv *priv;
204
205         view = EMPATHY_CHAT_VIEW (object);
206         priv = GET_PRIV (view);
207
208         DEBUG ("finalize: %p", object);
209
210         empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
211         empathy_conf_notify_remove (empathy_conf_get (), priv->notify_show_avatars_id);
212
213         if (priv->last_contact) {
214                 g_object_unref (priv->last_contact);
215         }
216         if (priv->scroll_time) {
217                 g_timer_destroy (priv->scroll_time);
218         }
219         if (priv->scroll_timeout) {
220                 g_source_remove (priv->scroll_timeout);
221         }
222
223         if (priv->theme) {
224                 g_signal_handlers_disconnect_by_func (priv->theme,
225                                                       chat_view_theme_notify_cb,
226                                                       view);
227                 g_object_unref (priv->theme);
228         }
229
230         G_OBJECT_CLASS (empathy_chat_view_parent_class)->finalize (object);
231 }
232
233 static gboolean
234 chat_view_drag_motion (GtkWidget        *widget,
235                        GdkDragContext   *context,
236                        gint              x,
237                        gint              y,
238                        guint             time)
239 {
240         /* Don't handle drag motion, since we don't want the view to scroll as
241          * the result of dragging something across it.
242          */
243
244         return FALSE;
245 }
246
247 static void
248 chat_view_size_allocate (GtkWidget     *widget,
249                          GtkAllocation *alloc)
250 {
251         gboolean down;
252
253         down = chat_view_is_scrolled_down (EMPATHY_CHAT_VIEW (widget));
254
255         GTK_WIDGET_CLASS (empathy_chat_view_parent_class)->size_allocate (widget, alloc);
256
257         if (down) {
258                 GtkAdjustment *adj;
259
260                 adj = GTK_TEXT_VIEW (widget)->vadjustment;
261                 gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
262         }
263 }
264
265 static void
266 chat_view_setup_tags (EmpathyChatView *view)
267 {
268         EmpathyChatViewPriv *priv;
269         GtkTextTag         *tag;
270
271         priv = GET_PRIV (view);
272
273         gtk_text_buffer_create_tag (priv->buffer,
274                                     "cut",
275                                     NULL);
276
277         /* FIXME: Move to the theme and come up with something that looks a bit
278          * nicer.
279          */
280         gtk_text_buffer_create_tag (priv->buffer,
281                                     "highlight",
282                                     "background", "yellow",
283                                     NULL);
284
285         tag = gtk_text_buffer_create_tag (priv->buffer,
286                                           "link",
287                                           NULL);
288
289         g_signal_connect (tag,
290                           "event",
291                           G_CALLBACK (chat_view_url_event_cb),
292                           view);
293
294         g_signal_connect (view,
295                           "motion-notify-event",
296                           G_CALLBACK (chat_view_event_cb),
297                           tag);
298 }
299
300 static void
301 chat_view_system_font_update (EmpathyChatView *view)
302 {
303         PangoFontDescription *font_description = NULL;
304         gchar                *font_name;
305
306         if (empathy_conf_get_string (empathy_conf_get (),
307                                      "/desktop/gnome/interface/document_font_name",
308                                      &font_name) && font_name) {
309                 font_description = pango_font_description_from_string (font_name);
310                 g_free (font_name);
311         } else {
312                 font_description = NULL;
313         }
314
315         gtk_widget_modify_font (GTK_WIDGET (view), font_description);
316
317         if (font_description) {
318                 pango_font_description_free (font_description);
319         }
320 }
321
322 static void
323 chat_view_notify_system_font_cb (EmpathyConf  *conf,
324                                  const gchar *key,
325                                  gpointer     user_data)
326 {
327         EmpathyChatView *view;
328         EmpathyChatViewPriv *priv;
329         gboolean        show_avatars = FALSE;
330
331         view = user_data;
332         priv = GET_PRIV (view);
333
334         chat_view_system_font_update (view);
335
336         /* Ugly, again, to adjust the vertical position of the nick... Will fix
337          * this when reworking the theme manager so that view register
338          * themselves with it instead of the other way around.
339          */
340         empathy_conf_get_bool (conf,
341                                EMPATHY_PREFS_UI_SHOW_AVATARS,
342                                &show_avatars);
343
344         empathy_theme_set_show_avatars (priv->theme, show_avatars);
345 }
346
347 static void
348 chat_view_notify_show_avatars_cb (EmpathyConf  *conf,
349                                   const gchar *key,
350                                   gpointer     user_data)
351 {
352         EmpathyChatView     *view;
353         EmpathyChatViewPriv *priv;
354         gboolean            show_avatars = FALSE;
355
356         view = user_data;
357         priv = GET_PRIV (view);
358
359         empathy_conf_get_bool (conf, key, &show_avatars);
360
361         empathy_theme_set_show_avatars (priv->theme, show_avatars);
362 }
363
364 static void
365 chat_view_populate_popup (EmpathyChatView *view,
366                           GtkMenu        *menu,
367                           gpointer        user_data)
368 {
369         EmpathyChatViewPriv *priv;
370         GtkTextTagTable    *table;
371         GtkTextTag         *tag;
372         gint                x, y;
373         GtkTextIter         iter, start, end;
374         GtkWidget          *item;
375         gchar              *str = NULL;
376
377         priv = GET_PRIV (view);
378
379         /* Clear menu item */
380         if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
381                 item = gtk_menu_item_new ();
382                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
383                 gtk_widget_show (item);
384
385                 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
386                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
387                 gtk_widget_show (item);
388
389                 g_signal_connect (item,
390                                   "activate",
391                                   G_CALLBACK (chat_view_clear_view_cb),
392                                   view);
393         }
394
395         /* Link context menu items */
396         table = gtk_text_buffer_get_tag_table (priv->buffer);
397         tag = gtk_text_tag_table_lookup (table, "link");
398
399         gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
400
401         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
402                                                GTK_TEXT_WINDOW_WIDGET,
403                                                x, y,
404                                                &x, &y);
405
406         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
407
408         start = end = iter;
409
410         if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
411             gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
412                 str = gtk_text_buffer_get_text (priv->buffer,
413                                                 &start, &end, FALSE);
414         }
415
416         if (G_STR_EMPTY (str)) {
417                 g_free (str);
418                 return;
419         }
420
421         /* NOTE: Set data just to get the string freed when not needed. */
422         g_object_set_data_full (G_OBJECT (menu),
423                                 "url", str,
424                                 (GDestroyNotify) g_free);
425
426         item = gtk_menu_item_new ();
427         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
428         gtk_widget_show (item);
429
430         item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
431         g_signal_connect (item,
432                           "activate",
433                           G_CALLBACK (chat_view_copy_address_cb),
434                           str);
435         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
436         gtk_widget_show (item);
437
438         item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
439         g_signal_connect (item,
440                           "activate",
441                           G_CALLBACK (chat_view_open_address_cb),
442                           str);
443         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
444         gtk_widget_show (item);
445 }
446
447 static gboolean
448 chat_view_event_cb (EmpathyChatView *view,
449                     GdkEventMotion *event,
450                     GtkTextTag     *tag)
451 {
452         static GdkCursor  *hand = NULL;
453         static GdkCursor  *beam = NULL;
454         GtkTextWindowType  type;
455         GtkTextIter        iter;
456         GdkWindow         *win;
457         gint               x, y, buf_x, buf_y;
458
459         type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
460                                               event->window);
461
462         if (type != GTK_TEXT_WINDOW_TEXT) {
463                 return FALSE;
464         }
465
466         /* Get where the pointer really is. */
467         win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
468         if (!win) {
469                 return FALSE;
470         }
471
472         gdk_window_get_pointer (win, &x, &y, NULL);
473
474         /* Get the iter where the cursor is at */
475         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
476                                                x, y,
477                                                &buf_x, &buf_y);
478
479         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
480                                             &iter,
481                                             buf_x, buf_y);
482
483         if (gtk_text_iter_has_tag (&iter, tag)) {
484                 if (!hand) {
485                         hand = gdk_cursor_new (GDK_HAND2);
486                         beam = gdk_cursor_new (GDK_XTERM);
487                 }
488                 gdk_window_set_cursor (win, hand);
489         } else {
490                 if (!beam) {
491                         beam = gdk_cursor_new (GDK_XTERM);
492                 }
493                 gdk_window_set_cursor (win, beam);
494         }
495
496         return FALSE;
497 }
498
499 static gboolean
500 chat_view_url_event_cb (GtkTextTag      *tag,
501                         GObject         *object,
502                         GdkEvent        *event,
503                         GtkTextIter     *iter,
504                         EmpathyChatView *view)
505 {
506         GtkTextIter          start, end;
507         gchar               *str;
508         EmpathyChatViewPriv *priv;
509
510         priv = GET_PRIV (view);
511
512         /* If the link is being selected, don't do anything. */
513         gtk_text_buffer_get_selection_bounds (priv->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 (priv->buffer,
524                                                         &start,
525                                                         &end,
526                                                         FALSE);
527
528                         empathy_url_show (GTK_WIDGET (view), 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 (GTK_WIDGET (menuitem), 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