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