]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-chat-view.c
Updated Basque translation.
[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) 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                                                       GtkTextBuffer            *buffer);
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                           priv->buffer);
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                         GtkTextBuffer *buffer)
505 {
506         GtkTextIter  start, end;
507         gchar       *str;
508
509         /* If the link is being selected, don't do anything. */
510         gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
511         if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
512                 return FALSE;
513         }
514
515         if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
516                 start = end = *iter;
517
518                 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
519                     gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
520                         str = gtk_text_buffer_get_text (buffer,
521                                                         &start,
522                                                         &end,
523                                                         FALSE);
524
525                         empathy_url_show (str);
526                         g_free (str);
527                 }
528         }
529
530         return FALSE;
531 }
532
533 static void
534 chat_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
535 {
536         empathy_url_show (url);
537 }
538
539 static void
540 chat_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
541 {
542         GtkClipboard *clipboard;
543
544         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
545         gtk_clipboard_set_text (clipboard, url, -1);
546
547         clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
548         gtk_clipboard_set_text (clipboard, url, -1);
549 }
550
551 static void
552 chat_view_clear_view_cb (GtkMenuItem *menuitem, EmpathyChatView *view)
553 {
554         empathy_chat_view_clear (view);
555 }
556
557 static gboolean
558 chat_view_is_scrolled_down (EmpathyChatView *view)
559 {
560         GtkWidget *sw;
561
562         sw = gtk_widget_get_parent (GTK_WIDGET (view));
563         if (GTK_IS_SCROLLED_WINDOW (sw)) {
564                 GtkAdjustment *vadj;
565
566                 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
567
568                 if (vadj->value + vadj->page_size / 2 < vadj->upper - vadj->page_size) {
569                         return FALSE;
570                 }
571         }
572
573         return TRUE;
574 }
575
576 static void
577 chat_view_maybe_trim_buffer (EmpathyChatView *view)
578 {
579         EmpathyChatViewPriv *priv;
580         GtkTextIter         top, bottom;
581         gint                line;
582         gint                remove;
583         GtkTextTagTable    *table;
584         GtkTextTag         *tag;
585
586         priv = GET_PRIV (view);
587
588         gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
589         line = gtk_text_iter_get_line (&bottom);
590         if (line < MAX_LINES) {
591                 return;
592         }
593
594         remove = line - MAX_LINES;
595         gtk_text_buffer_get_start_iter (priv->buffer, &top);
596
597         bottom = top;
598         if (!gtk_text_iter_forward_lines (&bottom, remove)) {
599                 return;
600         }
601
602         /* Track backwords to a place where we can safely cut, we don't do it in
603          * the middle of a tag.
604          */
605         table = gtk_text_buffer_get_tag_table (priv->buffer);
606         tag = gtk_text_tag_table_lookup (table, "cut");
607         if (!tag) {
608                 return;
609         }
610
611         if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
612                 return;
613         }
614
615         if (!gtk_text_iter_equal (&top, &bottom)) {
616                 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
617         }
618 }
619
620 static void
621 chat_view_theme_changed_cb (EmpathyThemeManager *manager,
622                             EmpathyChatView     *view)
623 {
624         EmpathyChatViewPriv *priv;
625         gboolean            show_avatars = FALSE;
626
627         priv = GET_PRIV (view);
628
629         empathy_theme_manager_apply_saved (manager, view);
630
631         /* Needed for now to update the "rise" property of the names to get it
632          * vertically centered.
633          */
634         empathy_conf_get_bool (empathy_conf_get (),
635                                EMPATHY_PREFS_UI_SHOW_AVATARS,
636                                &show_avatars);
637         empathy_theme_set_show_avatars (priv->theme, show_avatars);
638 }
639
640 /* Pads a pixbuf to the specified size, by centering it in a larger transparent
641  * pixbuf. Returns a new ref.
642  */
643 static GdkPixbuf *
644 chat_view_pad_to_size (GdkPixbuf *pixbuf,
645                        gint       width,
646                        gint       height,
647                        gint       extra_padding_right)
648 {
649         gint       src_width, src_height;
650         GdkPixbuf *padded;
651         gint       x_offset, y_offset;
652
653         src_width = gdk_pixbuf_get_width (pixbuf);
654         src_height = gdk_pixbuf_get_height (pixbuf);
655
656         x_offset = (width - src_width) / 2;
657         y_offset = (height - src_height) / 2;
658
659         padded = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (pixbuf),
660                                  TRUE, /* alpha */
661                                  gdk_pixbuf_get_bits_per_sample (pixbuf),
662                                  width + extra_padding_right,
663                                  height);
664
665         gdk_pixbuf_fill (padded, 0);
666
667         gdk_pixbuf_copy_area (pixbuf,
668                               0, /* source coords */
669                               0,
670                               src_width,
671                               src_height,
672                               padded,
673                               x_offset, /* dest coords */
674                               y_offset);
675
676         return padded;
677 }
678
679 typedef struct {
680         GdkPixbuf *pixbuf;
681         gchar     *token;
682 } AvatarData;
683
684 static void
685 chat_view_avatar_cache_data_free (gpointer ptr)
686 {
687         AvatarData *data = ptr;
688
689         g_object_unref (data->pixbuf);
690         g_free (data->token);
691         g_slice_free (AvatarData, data);
692 }
693
694 GdkPixbuf *
695 empathy_chat_view_get_avatar_pixbuf_with_cache (EmpathyContact *contact)
696 {
697         AvatarData        *data;
698         EmpathyAvatar     *avatar;
699         GdkPixbuf         *tmp_pixbuf;
700         GdkPixbuf         *pixbuf = NULL;
701
702         /* Check if avatar is in cache and if it's up to date */
703         avatar = empathy_contact_get_avatar (contact);
704         data = g_object_get_data (G_OBJECT (contact), "chat-view-avatar-cache");
705         if (data) {
706                 if (avatar && !tp_strdiff (avatar->token, data->token)) {
707                         /* We have the avatar in cache */
708                         return data->pixbuf;
709                 }
710         }
711
712         /* Avatar not in cache, create pixbuf */
713         tmp_pixbuf = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
714         if (tmp_pixbuf) {
715                 pixbuf = chat_view_pad_to_size (tmp_pixbuf, 32, 32, 6);
716                 g_object_unref (tmp_pixbuf);
717         }
718         if (!pixbuf) {
719                 return NULL;
720         }
721
722         /* Insert new pixbuf in cache */
723         data = g_slice_new0 (AvatarData);
724         data->token = g_strdup (avatar->token);
725         data->pixbuf = pixbuf;
726
727         g_object_set_data_full (G_OBJECT (contact), "chat-view-avatar-cache",
728                                 data, chat_view_avatar_cache_data_free);
729
730         return data->pixbuf;
731 }
732
733 EmpathyChatView *
734 empathy_chat_view_new (void)
735 {
736         return g_object_new (EMPATHY_TYPE_CHAT_VIEW, NULL);
737 }
738
739 /* Code stolen from pidgin/gtkimhtml.c */
740 static gboolean
741 chat_view_scroll_cb (EmpathyChatView *view)
742 {
743         EmpathyChatViewPriv *priv;
744         GtkAdjustment      *adj;
745         gdouble             max_val;
746
747         priv = GET_PRIV (view);
748         adj = GTK_TEXT_VIEW (view)->vadjustment;
749         max_val = adj->upper - adj->page_size;
750
751         g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
752
753         if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
754                 /* time's up. jump to the end and kill the timer */
755                 gtk_adjustment_set_value (adj, max_val);
756                 g_timer_destroy (priv->scroll_time);
757                 priv->scroll_time = NULL;
758                 priv->scroll_timeout = 0;
759                 return FALSE;
760         }
761
762         /* scroll by 1/3rd the remaining distance */
763         gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
764         return TRUE;
765 }
766
767 void
768 empathy_chat_view_scroll_down (EmpathyChatView *view)
769 {
770         EmpathyChatViewPriv *priv;
771
772         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
773
774         priv = GET_PRIV (view);
775
776         if (!priv->allow_scrolling) {
777                 return;
778         }
779
780         DEBUG ("Scrolling down");
781
782         if (priv->scroll_time) {
783                 g_timer_reset (priv->scroll_time);
784         } else {
785                 priv->scroll_time = g_timer_new();
786         }
787         if (!priv->scroll_timeout) {
788                 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
789                                                       (GSourceFunc) chat_view_scroll_cb,
790                                                       view);
791         }
792 }
793
794 void
795 empathy_chat_view_append_message (EmpathyChatView *view,
796                                   EmpathyMessage  *msg)
797 {
798         EmpathyChatViewPriv *priv = GET_PRIV (view);
799         gboolean             bottom;
800
801         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
802         g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
803
804         if (!empathy_message_get_body (msg)) {
805                 return;
806         }
807
808         bottom = chat_view_is_scrolled_down (view);
809         
810         chat_view_maybe_trim_buffer (view);
811
812         empathy_theme_append_message (priv->theme, view, msg);
813
814         if (bottom) {
815                 empathy_chat_view_scroll_down (view);
816         }
817
818         if (priv->last_contact) {
819                 g_object_unref (priv->last_contact);
820         }
821         priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
822 }
823
824 void
825 empathy_chat_view_append_event (EmpathyChatView *view,
826                                const gchar    *str)
827 {
828         EmpathyChatViewPriv *priv;
829         gboolean            bottom;
830
831         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
832         g_return_if_fail (!G_STR_EMPTY (str));
833
834         priv = GET_PRIV (view);
835
836         bottom = chat_view_is_scrolled_down (view);
837
838         chat_view_maybe_trim_buffer (view);
839
840         empathy_theme_append_event (priv->theme, view, str);
841
842         if (bottom) {
843                 empathy_chat_view_scroll_down (view);
844         }
845
846         if (priv->last_contact) {
847                 g_object_unref (priv->last_contact);
848                 priv->last_contact = NULL;
849         }
850 }
851
852 void
853 empathy_chat_view_append_button (EmpathyChatView *view,
854                                 const gchar    *message,
855                                 GtkWidget      *button1,
856                                 GtkWidget      *button2)
857 {
858         EmpathyChatViewPriv   *priv;
859         GtkTextChildAnchor   *anchor;
860         GtkTextIter           iter;
861         gboolean              bottom;
862         const gchar          *tag;
863
864         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
865         g_return_if_fail (button1 != NULL);
866
867         priv = GET_PRIV (view);
868
869         tag = "invite";
870
871         bottom = chat_view_is_scrolled_down (view);
872
873         empathy_theme_append_timestamp (priv->theme, view, NULL, TRUE, TRUE);
874
875         if (message) {
876                 empathy_theme_append_text (priv->theme, view, message, tag, NULL);
877         }
878
879         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
880
881         anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
882         gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button1, anchor);
883         gtk_widget_show (button1);
884
885         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
886                                                   &iter,
887                                                   " ",
888                                                   1,
889                                                   tag,
890                                                   NULL);
891
892         if (button2) {
893                 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
894                 
895                 anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
896                 gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button2, anchor);
897                 gtk_widget_show (button2);
898                 
899                 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
900                                                           &iter,
901                                                           " ",
902                                                           1,
903                                                           tag,
904                                                           NULL);
905         }
906
907         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
908         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
909                                                   &iter,
910                                                   "\n\n",
911                                                   2,
912                                                   tag,
913                                                   NULL);
914
915         if (bottom) {
916                 empathy_chat_view_scroll_down (view);
917         }
918
919         if (priv->last_contact) {
920                 g_object_unref (priv->last_contact);
921                 priv->last_contact = NULL;
922         }
923 }
924
925 void
926 empathy_chat_view_scroll (EmpathyChatView *view,
927                          gboolean        allow_scrolling)
928 {
929         EmpathyChatViewPriv *priv = GET_PRIV (view);
930
931         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
932
933         DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
934
935         priv->allow_scrolling = allow_scrolling;
936         if (allow_scrolling) {
937                 empathy_chat_view_scroll_down (view);
938         }
939 }
940
941 gboolean
942 empathy_chat_view_get_selection_bounds (EmpathyChatView *view,
943                                        GtkTextIter    *start,
944                                        GtkTextIter    *end)
945 {
946         GtkTextBuffer *buffer;
947
948         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
949
950         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
951
952         return gtk_text_buffer_get_selection_bounds (buffer, start, end);
953 }
954
955 void
956 empathy_chat_view_clear (EmpathyChatView *view)
957 {
958         GtkTextBuffer      *buffer;
959         EmpathyChatViewPriv *priv;
960
961         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
962
963         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
964         gtk_text_buffer_set_text (buffer, "", -1);
965
966         /* We set these back to the initial values so we get
967          * timestamps when clearing the window to know when
968          * conversations start.
969          */
970         priv = GET_PRIV (view);
971
972         priv->last_timestamp = 0;
973 }
974
975 gboolean
976 empathy_chat_view_find_previous (EmpathyChatView *view,
977                                 const gchar    *search_criteria,
978                                 gboolean        new_search)
979 {
980         EmpathyChatViewPriv *priv;
981         GtkTextBuffer      *buffer;
982         GtkTextIter         iter_at_mark;
983         GtkTextIter         iter_match_start;
984         GtkTextIter         iter_match_end;
985         gboolean            found;
986         gboolean            from_start = FALSE;
987
988         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
989         g_return_val_if_fail (search_criteria != NULL, FALSE);
990
991         priv = GET_PRIV (view);
992
993         buffer = priv->buffer;
994
995         if (G_STR_EMPTY (search_criteria)) {
996                 if (priv->find_mark_previous) {
997                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
998
999                         gtk_text_buffer_move_mark (buffer,
1000                                                    priv->find_mark_previous,
1001                                                    &iter_at_mark);
1002                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1003                                                       priv->find_mark_previous,
1004                                                       0.0,
1005                                                       TRUE,
1006                                                       0.0,
1007                                                       0.0);
1008                         gtk_text_buffer_select_range (buffer,
1009                                                       &iter_at_mark,
1010                                                       &iter_at_mark);
1011                 }
1012
1013                 return FALSE;
1014         }
1015
1016         if (new_search) {
1017                 from_start = TRUE;
1018         }
1019
1020         if (priv->find_mark_previous) {
1021                 gtk_text_buffer_get_iter_at_mark (buffer,
1022                                                   &iter_at_mark,
1023                                                   priv->find_mark_previous);
1024         } else {
1025                 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
1026                 from_start = TRUE;
1027         }
1028
1029         priv->find_last_direction = FALSE;
1030
1031         found = empathy_text_iter_backward_search (&iter_at_mark,
1032                                                   search_criteria,
1033                                                   &iter_match_start,
1034                                                   &iter_match_end,
1035                                                   NULL);
1036
1037         if (!found) {
1038                 gboolean result = FALSE;
1039
1040                 if (from_start) {
1041                         return result;
1042                 }
1043
1044                 /* Here we wrap around. */
1045                 if (!new_search && !priv->find_wrapped) {
1046                         priv->find_wrapped = TRUE;
1047                         result = empathy_chat_view_find_previous (view, 
1048                                                                  search_criteria, 
1049                                                                  FALSE);
1050                         priv->find_wrapped = FALSE;
1051                 }
1052
1053                 return result;
1054         }
1055
1056         /* Set new mark and show on screen */
1057         if (!priv->find_mark_previous) {
1058                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1059                                                                         &iter_match_start,
1060                                                                         TRUE);
1061         } else {
1062                 gtk_text_buffer_move_mark (buffer,
1063                                            priv->find_mark_previous,
1064                                            &iter_match_start);
1065         }
1066
1067         if (!priv->find_mark_next) {
1068                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1069                                                                     &iter_match_end,
1070                                                                     TRUE);
1071         } else {
1072                 gtk_text_buffer_move_mark (buffer,
1073                                            priv->find_mark_next,
1074                                            &iter_match_end);
1075         }
1076
1077         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1078                                       priv->find_mark_previous,
1079                                       0.0,
1080                                       TRUE,
1081                                       0.5,
1082                                       0.5);
1083
1084         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1085         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1086
1087         return TRUE;
1088 }
1089
1090 gboolean
1091 empathy_chat_view_find_next (EmpathyChatView *view,
1092                             const gchar    *search_criteria,
1093                             gboolean        new_search)
1094 {
1095         EmpathyChatViewPriv *priv;
1096         GtkTextBuffer      *buffer;
1097         GtkTextIter         iter_at_mark;
1098         GtkTextIter         iter_match_start;
1099         GtkTextIter         iter_match_end;
1100         gboolean            found;
1101         gboolean            from_start = FALSE;
1102
1103         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1104         g_return_val_if_fail (search_criteria != NULL, FALSE);
1105
1106         priv = GET_PRIV (view);
1107
1108         buffer = priv->buffer;
1109
1110         if (G_STR_EMPTY (search_criteria)) {
1111                 if (priv->find_mark_next) {
1112                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1113
1114                         gtk_text_buffer_move_mark (buffer,
1115                                                    priv->find_mark_next,
1116                                                    &iter_at_mark);
1117                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1118                                                       priv->find_mark_next,
1119                                                       0.0,
1120                                                       TRUE,
1121                                                       0.0,
1122                                                       0.0);
1123                         gtk_text_buffer_select_range (buffer,
1124                                                       &iter_at_mark,
1125                                                       &iter_at_mark);
1126                 }
1127
1128                 return FALSE;
1129         }
1130
1131         if (new_search) {
1132                 from_start = TRUE;
1133         }
1134
1135         if (priv->find_mark_next) {
1136                 gtk_text_buffer_get_iter_at_mark (buffer,
1137                                                   &iter_at_mark,
1138                                                   priv->find_mark_next);
1139         } else {
1140                 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1141                 from_start = TRUE;
1142         }
1143
1144         priv->find_last_direction = TRUE;
1145
1146         found = empathy_text_iter_forward_search (&iter_at_mark,
1147                                                  search_criteria,
1148                                                  &iter_match_start,
1149                                                  &iter_match_end,
1150                                                  NULL);
1151
1152         if (!found) {
1153                 gboolean result = FALSE;
1154
1155                 if (from_start) {
1156                         return result;
1157                 }
1158
1159                 /* Here we wrap around. */
1160                 if (!new_search && !priv->find_wrapped) {
1161                         priv->find_wrapped = TRUE;
1162                         result = empathy_chat_view_find_next (view, 
1163                                                              search_criteria, 
1164                                                              FALSE);
1165                         priv->find_wrapped = FALSE;
1166                 }
1167
1168                 return result;
1169         }
1170
1171         /* Set new mark and show on screen */
1172         if (!priv->find_mark_next) {
1173                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1174                                                                &iter_match_end,
1175                                                                TRUE);
1176         } else {
1177                 gtk_text_buffer_move_mark (buffer,
1178                                            priv->find_mark_next,
1179                                            &iter_match_end);
1180         }
1181
1182         if (!priv->find_mark_previous) {
1183                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1184                                                                         &iter_match_start,
1185                                                                         TRUE);
1186         } else {
1187                 gtk_text_buffer_move_mark (buffer,
1188                                            priv->find_mark_previous,
1189                                            &iter_match_start);
1190         }
1191
1192         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1193                                       priv->find_mark_next,
1194                                       0.0,
1195                                       TRUE,
1196                                       0.5,
1197                                       0.5);
1198
1199         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1200         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1201
1202         return TRUE;
1203 }
1204
1205
1206 void
1207 empathy_chat_view_find_abilities (EmpathyChatView *view,
1208                                  const gchar    *search_criteria,
1209                                  gboolean       *can_do_previous,
1210                                  gboolean       *can_do_next)
1211 {
1212         EmpathyChatViewPriv *priv;
1213         GtkTextBuffer      *buffer;
1214         GtkTextIter         iter_at_mark;
1215         GtkTextIter         iter_match_start;
1216         GtkTextIter         iter_match_end;
1217
1218         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1219         g_return_if_fail (search_criteria != NULL);
1220         g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1221
1222         priv = GET_PRIV (view);
1223
1224         buffer = priv->buffer;
1225
1226         if (can_do_previous) {
1227                 if (priv->find_mark_previous) {
1228                         gtk_text_buffer_get_iter_at_mark (buffer,
1229                                                           &iter_at_mark,
1230                                                           priv->find_mark_previous);
1231                 } else {
1232                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1233                 }
1234                 
1235                 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1236                                                                      search_criteria,
1237                                                                      &iter_match_start,
1238                                                                      &iter_match_end,
1239                                                                      NULL);
1240         }
1241
1242         if (can_do_next) {
1243                 if (priv->find_mark_next) {
1244                         gtk_text_buffer_get_iter_at_mark (buffer,
1245                                                           &iter_at_mark,
1246                                                           priv->find_mark_next);
1247                 } else {
1248                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1249                 }
1250                 
1251                 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1252                                                                 search_criteria,
1253                                                                 &iter_match_start,
1254                                                                 &iter_match_end,
1255                                                                 NULL);
1256         }
1257 }
1258
1259 void
1260 empathy_chat_view_highlight (EmpathyChatView *view,
1261                              const gchar     *text)
1262 {
1263         GtkTextBuffer *buffer;
1264         GtkTextIter    iter;
1265         GtkTextIter    iter_start;
1266         GtkTextIter    iter_end;
1267         GtkTextIter    iter_match_start;
1268         GtkTextIter    iter_match_end;
1269         gboolean       found;
1270
1271         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1272
1273         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1274
1275         gtk_text_buffer_get_start_iter (buffer, &iter);
1276
1277         gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1278         gtk_text_buffer_remove_tag_by_name (buffer, "highlight",
1279                                             &iter_start,
1280                                             &iter_end);
1281
1282         if (G_STR_EMPTY (text)) {
1283                 return;
1284         }
1285
1286         while (1) {
1287                 found = empathy_text_iter_forward_search (&iter,
1288                                                          text,
1289                                                          &iter_match_start,
1290                                                          &iter_match_end,
1291                                                          NULL);
1292
1293                 if (!found) {
1294                         break;
1295                 }
1296
1297                 gtk_text_buffer_apply_tag_by_name (buffer, "highlight",
1298                                                    &iter_match_start,
1299                                                    &iter_match_end);
1300
1301                 iter = iter_match_end;
1302                 gtk_text_iter_forward_char (&iter);
1303         }
1304 }
1305
1306 void
1307 empathy_chat_view_copy_clipboard (EmpathyChatView *view)
1308 {
1309         GtkTextBuffer *buffer;
1310         GtkClipboard  *clipboard;
1311
1312         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1313
1314         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1315         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1316
1317         gtk_text_buffer_copy_clipboard (buffer, clipboard);
1318 }
1319
1320 EmpathyTheme *
1321 empathy_chat_view_get_theme (EmpathyChatView *view)
1322 {
1323         EmpathyChatViewPriv *priv;
1324
1325         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1326
1327         priv = GET_PRIV (view);
1328
1329         return priv->theme;
1330 }
1331
1332 static void
1333 chat_view_theme_notify_cb (EmpathyTheme    *theme,
1334                            GParamSpec      *param,
1335                            EmpathyChatView *view)
1336 {
1337         empathy_theme_update_view (theme, view);
1338 }
1339
1340 void
1341 empathy_chat_view_set_theme (EmpathyChatView *view, EmpathyTheme *theme)
1342 {
1343         EmpathyChatViewPriv *priv;
1344
1345         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1346         g_return_if_fail (EMPATHY_IS_THEME (theme));
1347
1348         priv = GET_PRIV (view);
1349
1350         if (priv->theme) {
1351                 g_signal_handlers_disconnect_by_func (priv->theme,
1352                                                       chat_view_theme_notify_cb,
1353                                                       view);
1354                 g_object_unref (priv->theme);
1355         }
1356
1357         priv->theme = g_object_ref (theme);
1358
1359         empathy_theme_update_view (theme, view);
1360         g_signal_connect (priv->theme, "notify",
1361                           G_CALLBACK (chat_view_theme_notify_cb),
1362                           view);
1363
1364         /* FIXME: Redraw all messages using the new theme */
1365 }
1366
1367 void
1368 empathy_chat_view_set_margin (EmpathyChatView *view,
1369                              gint            margin)
1370 {
1371         EmpathyChatViewPriv *priv;
1372
1373         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1374
1375         priv = GET_PRIV (view);
1376
1377         g_object_set (view,
1378                       "left-margin", margin,
1379                       "right-margin", margin,
1380                       NULL);
1381 }
1382
1383 GtkWidget *
1384 empathy_chat_view_get_smiley_menu (GCallback    callback,
1385                                    gpointer     user_data)
1386 {
1387         EmpathySmileyManager *smiley_manager;
1388         GSList               *smileys, *l;
1389         GtkWidget            *menu;
1390         gint                  x = 0;
1391         gint                  y = 0;
1392
1393         g_return_val_if_fail (callback != NULL, NULL);
1394
1395         menu = gtk_menu_new ();
1396
1397         smiley_manager = empathy_smiley_manager_new ();
1398         smileys = empathy_smiley_manager_get_all (smiley_manager);
1399         for (l = smileys; l; l = l->next) {
1400                 EmpathySmiley *smiley;
1401                 GtkWidget     *item;
1402                 GtkWidget     *image;
1403
1404                 smiley = l->data;
1405                 image = gtk_image_new_from_pixbuf (smiley->pixbuf);
1406
1407                 item = gtk_image_menu_item_new_with_label ("");
1408                 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1409
1410                 gtk_menu_attach (GTK_MENU (menu), item,
1411                                  x, x + 1, y, y + 1);
1412
1413                 gtk_widget_set_tooltip_text (item, smiley->str);
1414
1415                 g_object_set_data  (G_OBJECT (item), "smiley_text", smiley->str);
1416                 g_signal_connect (item, "activate", callback, user_data);
1417
1418                 if (x > 3) {
1419                         y++;
1420                         x = 0;
1421                 } else {
1422                         x++;
1423                 }
1424         }
1425         g_object_unref (smiley_manager);
1426
1427         gtk_widget_show_all (menu);
1428
1429         return menu;
1430 }
1431
1432 time_t
1433 empathy_chat_view_get_last_timestamp (EmpathyChatView *view)
1434 {
1435         EmpathyChatViewPriv *priv;
1436
1437         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), 0);
1438
1439         priv = GET_PRIV (view);
1440
1441         return priv->last_timestamp;
1442 }
1443
1444 void
1445 empathy_chat_view_set_last_timestamp (EmpathyChatView *view,
1446                                      time_t          timestamp)
1447 {
1448         EmpathyChatViewPriv *priv;
1449
1450         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1451
1452         priv = GET_PRIV (view);
1453
1454         priv->last_timestamp = timestamp;
1455 }
1456
1457 EmpathyContact *
1458 empathy_chat_view_get_last_contact (EmpathyChatView *view)
1459 {
1460         EmpathyChatViewPriv *priv;
1461
1462         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1463
1464         priv = GET_PRIV (view);
1465
1466         return priv->last_contact;
1467 }
1468