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