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