]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-chat-view.c
Update to new icon naming spec for new avatars. Fixes bug #490173 (Michael
[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 <libmissioncontrol/mc-account.h>
43
44 #include <libempathy/empathy-utils.h>
45 #include <libempathy/empathy-debug.h>
46 #include <libempathy/empathy-conf.h>
47
48 #include "empathy-chat-view.h"
49 #include "empathy-chat.h"
50 #include "empathy-preferences.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 typedef enum {
67         BLOCK_TYPE_NONE,
68         BLOCK_TYPE_SELF,
69         BLOCK_TYPE_OTHER,
70         BLOCK_TYPE_EVENT,
71         BLOCK_TYPE_TIME,
72         BLOCK_TYPE_INVITE
73 } BlockType;
74
75 struct _EmpathyChatViewPriv {
76         EmpathySmileyManager *smiley_manager;
77
78         GtkTextBuffer *buffer;
79
80         gboolean       irc_style;
81         time_t         last_timestamp;
82         BlockType      last_block_type;
83
84         gboolean       allow_scrolling;
85         guint          scroll_timeout;
86         GTimer        *scroll_time;
87         gboolean       is_group_chat;
88
89         GtkTextMark   *find_mark_previous;
90         GtkTextMark   *find_mark_next;
91         gboolean       find_wrapped;
92         gboolean       find_last_direction;
93
94         /* This is for the group chat so we know if the "other" last contact
95          * changed, so we know whether to insert a header or not.
96          */
97         EmpathyContact *last_contact;
98
99         guint          notify_system_fonts_id;
100         guint          notify_show_avatars_id;
101 };
102
103 static void     empathy_chat_view_class_init          (EmpathyChatViewClass      *klass);
104 static void     empathy_chat_view_init                (EmpathyChatView           *view);
105 static void     chat_view_finalize                   (GObject                  *object);
106 static gboolean chat_view_drag_motion                (GtkWidget                *widget,
107                                                       GdkDragContext           *context,
108                                                       gint                      x,
109                                                       gint                      y,
110                                                       guint                     time);
111 static void     chat_view_size_allocate              (GtkWidget                *widget,
112                                                       GtkAllocation            *alloc);
113 static void     chat_view_setup_tags                 (EmpathyChatView           *view);
114 static void     chat_view_system_font_update         (EmpathyChatView           *view);
115 static void     chat_view_notify_system_font_cb      (EmpathyConf               *conf,
116                                                       const gchar              *key,
117                                                       gpointer                  user_data);
118 static void     chat_view_notify_show_avatars_cb     (EmpathyConf               *conf,
119                                                       const gchar              *key,
120                                                       gpointer                  user_data);
121 static void     chat_view_populate_popup             (EmpathyChatView           *view,
122                                                       GtkMenu                  *menu,
123                                                       gpointer                  user_data);
124 static gboolean chat_view_event_cb                   (EmpathyChatView           *view,
125                                                       GdkEventMotion           *event,
126                                                       GtkTextTag               *tag);
127 static gboolean chat_view_url_event_cb               (GtkTextTag               *tag,
128                                                       GObject                  *object,
129                                                       GdkEvent                 *event,
130                                                       GtkTextIter              *iter,
131                                                       GtkTextBuffer            *buffer);
132 static void     chat_view_open_address_cb            (GtkMenuItem              *menuitem,
133                                                       const gchar              *url);
134 static void     chat_view_copy_address_cb            (GtkMenuItem              *menuitem,
135                                                       const gchar              *url);
136 static void     chat_view_clear_view_cb              (GtkMenuItem              *menuitem,
137                                                       EmpathyChatView           *view);
138 static void     chat_view_insert_text_with_emoticons (EmpathyChatView          *view,
139                                                       GtkTextIter              *iter,
140                                                       const gchar              *str);
141 static gboolean chat_view_is_scrolled_down           (EmpathyChatView           *view);
142 static void     chat_view_theme_changed_cb           (EmpathyThemeManager       *manager,
143                                                       EmpathyChatView           *view);
144 static void     chat_view_maybe_append_date_and_time (EmpathyChatView           *view,
145                                                       EmpathyMessage            *msg);
146 static void     chat_view_append_spacing             (EmpathyChatView           *view);
147 static void     chat_view_append_text                (EmpathyChatView           *view,
148                                                       const gchar              *body,
149                                                       const gchar              *tag);
150 static void     chat_view_maybe_append_fancy_header  (EmpathyChatView           *view,
151                                                       EmpathyMessage            *msg);
152 static void     chat_view_append_irc_action          (EmpathyChatView           *view,
153                                                       EmpathyMessage            *msg);
154 static void     chat_view_append_fancy_action        (EmpathyChatView           *view,
155                                                       EmpathyMessage            *msg);
156 static void     chat_view_append_irc_message         (EmpathyChatView           *view,
157                                                       EmpathyMessage            *msg);
158 static void     chat_view_append_fancy_message       (EmpathyChatView           *view,
159                                                       EmpathyMessage            *msg);
160 static GdkPixbuf *chat_view_pad_to_size              (GdkPixbuf                *pixbuf,
161                                                       gint                      width,
162                                                       gint                      height,
163                                                       gint                      extra_padding_right);
164
165 G_DEFINE_TYPE (EmpathyChatView, empathy_chat_view, GTK_TYPE_TEXT_VIEW);
166
167 static void
168 empathy_chat_view_class_init (EmpathyChatViewClass *klass)
169 {
170         GObjectClass   *object_class = G_OBJECT_CLASS (klass);
171         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
172
173         object_class->finalize = chat_view_finalize;
174         widget_class->size_allocate = chat_view_size_allocate;
175         widget_class->drag_motion = chat_view_drag_motion; 
176
177         g_type_class_add_private (object_class, sizeof (EmpathyChatViewPriv));
178 }
179
180 static void
181 empathy_chat_view_init (EmpathyChatView *view)
182 {
183         EmpathyChatViewPriv *priv;
184         gboolean            show_avatars;
185
186         priv = GET_PRIV (view);
187
188         priv->smiley_manager = empathy_smiley_manager_new ();
189         priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
190
191         priv->last_block_type = BLOCK_TYPE_NONE;
192         priv->last_timestamp = 0;
193
194         priv->allow_scrolling = TRUE;
195
196         priv->is_group_chat = FALSE;
197
198         g_object_set (view,
199                       "wrap-mode", GTK_WRAP_WORD_CHAR,
200                       "editable", FALSE,
201                       "cursor-visible", FALSE,
202                       NULL);
203
204         priv->notify_system_fonts_id =
205                 empathy_conf_notify_add (empathy_conf_get (),
206                                          "/desktop/gnome/interface/document_font_name",
207                                          chat_view_notify_system_font_cb,
208                                          view);
209         chat_view_system_font_update (view);
210
211         priv->notify_show_avatars_id =
212                 empathy_conf_notify_add (empathy_conf_get (),
213                                          EMPATHY_PREFS_UI_SHOW_AVATARS,
214                                          chat_view_notify_show_avatars_cb,
215                                          view);
216
217         chat_view_setup_tags (view);
218
219         empathy_theme_manager_apply_saved (empathy_theme_manager_get (), view);
220
221         show_avatars = FALSE;
222         empathy_conf_get_bool (empathy_conf_get (),
223                                EMPATHY_PREFS_UI_SHOW_AVATARS,
224                                &show_avatars);
225
226         empathy_theme_manager_update_show_avatars (empathy_theme_manager_get (),
227                                                   view, show_avatars);
228
229         g_signal_connect (view,
230                           "populate-popup",
231                           G_CALLBACK (chat_view_populate_popup),
232                           NULL);
233
234         g_signal_connect_object (empathy_theme_manager_get (),
235                                  "theme-changed",
236                                  G_CALLBACK (chat_view_theme_changed_cb),
237                                  view,
238                                  0);
239 }
240
241 static void
242 chat_view_finalize (GObject *object)
243 {
244         EmpathyChatView     *view;
245         EmpathyChatViewPriv *priv;
246
247         view = EMPATHY_CHAT_VIEW (object);
248         priv = GET_PRIV (view);
249
250         empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
251
252         empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
253         empathy_conf_notify_remove (empathy_conf_get (), priv->notify_show_avatars_id);
254
255         if (priv->smiley_manager) {
256                 g_object_unref (priv->smiley_manager);
257         }
258         if (priv->last_contact) {
259                 g_object_unref (priv->last_contact);
260         }
261         if (priv->scroll_time) {
262                 g_timer_destroy (priv->scroll_time);
263         }
264         if (priv->scroll_timeout) {
265                 g_source_remove (priv->scroll_timeout);
266         }
267
268         G_OBJECT_CLASS (empathy_chat_view_parent_class)->finalize (object);
269 }
270
271 static gboolean
272 chat_view_drag_motion (GtkWidget        *widget,
273                        GdkDragContext   *context,
274                        gint              x,
275                        gint              y,
276                        guint             time)
277 {
278         /* Don't handle drag motion, since we don't want the view to scroll as
279          * the result of dragging something across it.
280          */
281
282         return FALSE;
283 }
284
285 static void
286 chat_view_size_allocate (GtkWidget     *widget,
287                          GtkAllocation *alloc)
288 {
289         gboolean down;
290
291         down = chat_view_is_scrolled_down (EMPATHY_CHAT_VIEW (widget));
292
293         GTK_WIDGET_CLASS (empathy_chat_view_parent_class)->size_allocate (widget, alloc);
294
295         if (down) {
296                 GtkAdjustment *adj;
297
298                 adj = GTK_TEXT_VIEW (widget)->vadjustment;
299                 gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
300         }
301 }
302
303 static void
304 chat_view_setup_tags (EmpathyChatView *view)
305 {
306         EmpathyChatViewPriv *priv;
307         GtkTextTag         *tag;
308
309         priv = GET_PRIV (view);
310
311         gtk_text_buffer_create_tag (priv->buffer,
312                                     "cut",
313                                     NULL);
314
315         /* FIXME: Move to the theme and come up with something that looks a bit
316          * nicer.
317          */
318         gtk_text_buffer_create_tag (priv->buffer,
319                                     "highlight",
320                                     "background", "yellow",
321                                     NULL);
322
323         tag = gtk_text_buffer_create_tag (priv->buffer,
324                                           "link",
325                                           NULL);
326
327         g_signal_connect (tag,
328                           "event",
329                           G_CALLBACK (chat_view_url_event_cb),
330                           priv->buffer);
331
332         g_signal_connect (view,
333                           "motion-notify-event",
334                           G_CALLBACK (chat_view_event_cb),
335                           tag);
336 }
337
338 static void
339 chat_view_system_font_update (EmpathyChatView *view)
340 {
341         PangoFontDescription *font_description = NULL;
342         gchar                *font_name;
343
344         if (empathy_conf_get_string (empathy_conf_get (),
345                                      "/desktop/gnome/interface/document_font_name",
346                                      &font_name) && font_name) {
347                 font_description = pango_font_description_from_string (font_name);
348                 g_free (font_name);
349         } else {
350                 font_description = NULL;
351         }
352
353         gtk_widget_modify_font (GTK_WIDGET (view), font_description);
354
355         if (font_description) {
356                 pango_font_description_free (font_description);
357         }
358 }
359
360 static void
361 chat_view_notify_system_font_cb (EmpathyConf  *conf,
362                                  const gchar *key,
363                                  gpointer     user_data)
364 {
365         EmpathyChatView *view;
366         gboolean        show_avatars = FALSE;
367
368         view = user_data;
369
370         chat_view_system_font_update (view);
371
372         /* Ugly, again, to adjust the vertical position of the nick... Will fix
373          * this when reworking the theme manager so that view register
374          * themselves with it instead of the other way around.
375          */
376         empathy_conf_get_bool (conf,
377                                EMPATHY_PREFS_UI_SHOW_AVATARS,
378                                &show_avatars);
379
380         empathy_theme_manager_update_show_avatars (empathy_theme_manager_get (),
381                                                   view, show_avatars);
382 }
383
384 static void
385 chat_view_notify_show_avatars_cb (EmpathyConf  *conf,
386                                   const gchar *key,
387                                   gpointer     user_data)
388 {
389         EmpathyChatView     *view;
390         EmpathyChatViewPriv *priv;
391         gboolean            show_avatars = FALSE;
392
393         view = user_data;
394         priv = GET_PRIV (view);
395
396         empathy_conf_get_bool (conf, key, &show_avatars);
397
398         empathy_theme_manager_update_show_avatars (empathy_theme_manager_get (),
399                                                   view, show_avatars);
400 }
401
402 static void
403 chat_view_populate_popup (EmpathyChatView *view,
404                           GtkMenu        *menu,
405                           gpointer        user_data)
406 {
407         EmpathyChatViewPriv *priv;
408         GtkTextTagTable    *table;
409         GtkTextTag         *tag;
410         gint                x, y;
411         GtkTextIter         iter, start, end;
412         GtkWidget          *item;
413         gchar              *str = NULL;
414
415         priv = GET_PRIV (view);
416
417         /* Clear menu item */
418         if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
419                 item = gtk_menu_item_new ();
420                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
421                 gtk_widget_show (item);
422
423                 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
424                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
425                 gtk_widget_show (item);
426
427                 g_signal_connect (item,
428                                   "activate",
429                                   G_CALLBACK (chat_view_clear_view_cb),
430                                   view);
431         }
432
433         /* Link context menu items */
434         table = gtk_text_buffer_get_tag_table (priv->buffer);
435         tag = gtk_text_tag_table_lookup (table, "link");
436
437         gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
438
439         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
440                                                GTK_TEXT_WINDOW_WIDGET,
441                                                x, y,
442                                                &x, &y);
443
444         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
445
446         start = end = iter;
447
448         if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
449             gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
450                 str = gtk_text_buffer_get_text (priv->buffer,
451                                                 &start, &end, FALSE);
452         }
453
454         if (G_STR_EMPTY (str)) {
455                 g_free (str);
456                 return;
457         }
458
459         /* NOTE: Set data just to get the string freed when not needed. */
460         g_object_set_data_full (G_OBJECT (menu),
461                                 "url", str,
462                                 (GDestroyNotify) g_free);
463
464         item = gtk_menu_item_new ();
465         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
466         gtk_widget_show (item);
467
468         item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
469         g_signal_connect (item,
470                           "activate",
471                           G_CALLBACK (chat_view_copy_address_cb),
472                           str);
473         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
474         gtk_widget_show (item);
475
476         item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
477         g_signal_connect (item,
478                           "activate",
479                           G_CALLBACK (chat_view_open_address_cb),
480                           str);
481         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
482         gtk_widget_show (item);
483 }
484
485 static gboolean
486 chat_view_event_cb (EmpathyChatView *view,
487                     GdkEventMotion *event,
488                     GtkTextTag     *tag)
489 {
490         static GdkCursor  *hand = NULL;
491         static GdkCursor  *beam = NULL;
492         GtkTextWindowType  type;
493         GtkTextIter        iter;
494         GdkWindow         *win;
495         gint               x, y, buf_x, buf_y;
496
497         type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
498                                               event->window);
499
500         if (type != GTK_TEXT_WINDOW_TEXT) {
501                 return FALSE;
502         }
503
504         /* Get where the pointer really is. */
505         win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
506         if (!win) {
507                 return FALSE;
508         }
509
510         gdk_window_get_pointer (win, &x, &y, NULL);
511
512         /* Get the iter where the cursor is at */
513         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
514                                                x, y,
515                                                &buf_x, &buf_y);
516
517         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
518                                             &iter,
519                                             buf_x, buf_y);
520
521         if (gtk_text_iter_has_tag (&iter, tag)) {
522                 if (!hand) {
523                         hand = gdk_cursor_new (GDK_HAND2);
524                         beam = gdk_cursor_new (GDK_XTERM);
525                 }
526                 gdk_window_set_cursor (win, hand);
527         } else {
528                 if (!beam) {
529                         beam = gdk_cursor_new (GDK_XTERM);
530                 }
531                 gdk_window_set_cursor (win, beam);
532         }
533
534         return FALSE;
535 }
536
537 static gboolean
538 chat_view_url_event_cb (GtkTextTag    *tag,
539                         GObject       *object,
540                         GdkEvent      *event,
541                         GtkTextIter   *iter,
542                         GtkTextBuffer *buffer)
543 {
544         GtkTextIter  start, end;
545         gchar       *str;
546
547         /* If the link is being selected, don't do anything. */
548         gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
549         if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
550                 return FALSE;
551         }
552
553         if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
554                 start = end = *iter;
555
556                 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
557                     gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
558                         str = gtk_text_buffer_get_text (buffer,
559                                                         &start,
560                                                         &end,
561                                                         FALSE);
562
563                         empathy_url_show (str);
564                         g_free (str);
565                 }
566         }
567
568         return FALSE;
569 }
570
571 static void
572 chat_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
573 {
574         empathy_url_show (url);
575 }
576
577 static void
578 chat_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
579 {
580         GtkClipboard *clipboard;
581
582         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
583         gtk_clipboard_set_text (clipboard, url, -1);
584
585         clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
586         gtk_clipboard_set_text (clipboard, url, -1);
587 }
588
589 static void
590 chat_view_clear_view_cb (GtkMenuItem *menuitem, EmpathyChatView *view)
591 {
592         empathy_chat_view_clear (view);
593 }
594
595 static void
596 chat_view_insert_text_with_emoticons (EmpathyChatView *view,
597                                       GtkTextIter     *iter,
598                                       const gchar     *str)
599 {
600         EmpathyChatViewPriv *priv = GET_PRIV (view);
601         GtkTextBuffer       *buf = priv->buffer;
602         gboolean             use_smileys = FALSE;
603         GSList              *smileys, *l;
604
605         empathy_conf_get_bool (empathy_conf_get (),
606                                EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
607                                &use_smileys);
608
609         if (!use_smileys) {
610                 gtk_text_buffer_insert (buf, iter, str, -1);
611                 return;
612         }
613
614         smileys = empathy_smiley_manager_parse (priv->smiley_manager, str);
615         for (l = smileys; l; l = l->next) {
616                 EmpathySmiley *smiley;
617
618                 smiley = l->data;
619                 if (smiley->pixbuf) {
620                         gtk_text_buffer_insert_pixbuf (buf, iter, smiley->pixbuf);
621                 } else {
622                         gtk_text_buffer_insert (buf, iter, smiley->str, -1);
623                 }
624                 empathy_smiley_free (smiley);
625         }
626         g_slist_free (smileys);
627 }
628
629 static gboolean
630 chat_view_is_scrolled_down (EmpathyChatView *view)
631 {
632         GtkWidget *sw;
633
634         sw = gtk_widget_get_parent (GTK_WIDGET (view));
635         if (GTK_IS_SCROLLED_WINDOW (sw)) {
636                 GtkAdjustment *vadj;
637
638                 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
639
640                 if (vadj->value + vadj->page_size / 2 < vadj->upper - vadj->page_size) {
641                         return FALSE;
642                 }
643         }
644
645         return TRUE;
646 }
647
648 static void
649 chat_view_maybe_trim_buffer (EmpathyChatView *view)
650 {
651         EmpathyChatViewPriv *priv;
652         GtkTextIter         top, bottom;
653         gint                line;
654         gint                remove;
655         GtkTextTagTable    *table;
656         GtkTextTag         *tag;
657
658         priv = GET_PRIV (view);
659
660         gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
661         line = gtk_text_iter_get_line (&bottom);
662         if (line < MAX_LINES) {
663                 return;
664         }
665
666         remove = line - MAX_LINES;
667         gtk_text_buffer_get_start_iter (priv->buffer, &top);
668
669         bottom = top;
670         if (!gtk_text_iter_forward_lines (&bottom, remove)) {
671                 return;
672         }
673
674         /* Track backwords to a place where we can safely cut, we don't do it in
675          * the middle of a tag.
676          */
677         table = gtk_text_buffer_get_tag_table (priv->buffer);
678         tag = gtk_text_tag_table_lookup (table, "cut");
679         if (!tag) {
680                 return;
681         }
682
683         if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
684                 return;
685         }
686
687         if (!gtk_text_iter_equal (&top, &bottom)) {
688                 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
689         }
690 }
691
692 static void
693 chat_view_maybe_append_date_and_time (EmpathyChatView *view,
694                                       EmpathyMessage  *msg)
695 {
696         EmpathyChatViewPriv *priv;
697         const gchar        *tag;
698         time_t              timestamp;
699         GDate              *date, *last_date;
700         GtkTextIter         iter;
701         gboolean            append_date, append_time;
702         GString            *str;
703
704         priv = GET_PRIV (view);
705
706         if (priv->irc_style) {
707                 tag = "irc-time";
708         } else {
709                 tag = "fancy-time";
710         }
711
712         if (priv->last_block_type == BLOCK_TYPE_TIME) {
713                 return;
714         }
715
716         str = g_string_new (NULL);
717
718         timestamp = 0;
719         if (msg) {
720                 timestamp = empathy_message_get_timestamp (msg);
721         }
722
723         if (timestamp <= 0) {
724                 timestamp = empathy_time_get_current ();
725         }
726
727         date = g_date_new ();
728         g_date_set_time_t (date, timestamp);
729
730         last_date = g_date_new ();
731         g_date_set_time_t (last_date, priv->last_timestamp);
732
733         append_date = FALSE;
734         append_time = FALSE;
735
736         if (g_date_compare (date, last_date) > 0) {
737                 append_date = TRUE;
738                 append_time = TRUE;
739         }
740
741         if (priv->last_timestamp + TIMESTAMP_INTERVAL < timestamp) {
742                 append_time = TRUE;
743         }
744
745         if (append_time || append_date) {
746                 chat_view_append_spacing (view);
747
748                 g_string_append (str, "- ");
749         }
750
751         if (append_date) {
752                 gchar buf[256];
753
754                 g_date_strftime (buf, 256, _("%A %d %B %Y"), date);
755                 g_string_append (str, buf);
756
757                 if (append_time) {
758                         g_string_append (str, ", ");
759                 }
760         }
761
762         g_date_free (date);
763         g_date_free (last_date);
764
765         if (append_time) {
766                 gchar *tmp;
767
768                 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
769                 g_string_append (str, tmp);
770                 g_free (tmp);
771         }
772
773         if (append_time || append_date) {
774                 g_string_append (str, " -\n");
775
776                 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
777                 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
778                                                           &iter,
779                                                           str->str, -1,
780                                                           tag,
781                                                           NULL);
782
783                 priv->last_block_type = BLOCK_TYPE_TIME;
784                 priv->last_timestamp = timestamp;
785         }
786
787         g_string_free (str, TRUE);
788 }
789
790 static void
791 chat_view_append_spacing (EmpathyChatView *view)
792 {
793         EmpathyChatViewPriv *priv;
794         const gchar        *tag;
795         GtkTextIter         iter;
796
797         priv = GET_PRIV (view);
798
799         if (priv->irc_style) {
800                 tag = "irc-spacing";
801         } else {
802                 tag = "fancy-spacing";
803         }
804
805         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
806         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
807                                                   &iter,
808                                                   "\n",
809                                                   -1,
810                                                   "cut",
811                                                   tag,
812                                                   NULL);
813 }
814
815 static void
816 chat_view_append_text (EmpathyChatView *view,
817                        const gchar    *body,
818                        const gchar    *tag)
819 {
820         EmpathyChatViewPriv *priv;
821         GtkTextIter         start_iter, end_iter;
822         GtkTextMark        *mark;
823         GtkTextIter         iter;
824         gint                num_matches, i;
825         GArray             *start, *end;
826         const gchar        *link_tag;
827
828         priv = GET_PRIV (view);
829
830         if (priv->irc_style) {
831                 link_tag = "irc-link";
832         } else {
833                 link_tag = "fancy-link";
834         }
835
836         gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
837         mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
838
839         start = g_array_new (FALSE, FALSE, sizeof (gint));
840         end = g_array_new (FALSE, FALSE, sizeof (gint));
841
842         num_matches = empathy_regex_match (EMPATHY_REGEX_ALL, body, start, end);
843
844         if (num_matches == 0) {
845                 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
846                 chat_view_insert_text_with_emoticons (view, &iter, body);
847         } else {
848                 gint   last = 0;
849                 gint   s = 0, e = 0;
850                 gchar *tmp;
851
852                 for (i = 0; i < num_matches; i++) {
853                         s = g_array_index (start, gint, i);
854                         e = g_array_index (end, gint, i);
855
856                         if (s > last) {
857                                 tmp = empathy_substring (body, last, s);
858
859                                 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
860                                 chat_view_insert_text_with_emoticons (view,
861                                                                       &iter,
862                                                                       tmp);
863                                 g_free (tmp);
864                         }
865
866                         tmp = empathy_substring (body, s, e);
867
868                         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
869                         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
870                                                                   &iter,
871                                                                   tmp,
872                                                                   -1,
873                                                                   link_tag,
874                                                                   "link",
875                                                                   NULL);
876
877                         g_free (tmp);
878
879                         last = e;
880                 }
881
882                 if (e < strlen (body)) {
883                         tmp = empathy_substring (body, e, strlen (body));
884
885                         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
886                         chat_view_insert_text_with_emoticons (view,
887                                                               &iter,
888                                                               tmp);
889                         g_free (tmp);
890                 }
891         }
892
893         g_array_free (start, TRUE);
894         g_array_free (end, TRUE);
895
896         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
897         gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
898
899         /* Apply the style to the inserted text. */
900         gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
901         gtk_text_buffer_get_end_iter (priv->buffer, &end_iter);
902
903         gtk_text_buffer_apply_tag_by_name (priv->buffer,
904                                            tag,
905                                            &start_iter,
906                                            &end_iter);
907
908         gtk_text_buffer_delete_mark (priv->buffer, mark);
909 }
910
911 static void
912 chat_view_maybe_append_fancy_header (EmpathyChatView *view,
913                                      EmpathyMessage  *msg)
914 {
915         EmpathyChatViewPriv *priv;
916         EmpathyContact      *sender;
917         const gchar        *name;
918         gboolean            header;
919         GtkTextIter         iter;
920         gchar              *tmp;
921         const gchar        *tag;
922         const gchar        *avatar_tag;
923         const gchar        *line_top_tag;
924         const gchar        *line_bottom_tag;
925         gboolean            from_self;
926         GdkPixbuf          *pixbuf = NULL;
927         GdkPixbuf          *avatar = NULL;
928
929         priv = GET_PRIV (view);
930
931         sender = empathy_message_get_sender (msg);
932         name = empathy_contact_get_name (sender);
933         from_self = empathy_contact_is_user (sender);
934
935         empathy_debug (DEBUG_DOMAIN, "Maybe add fancy header");
936
937         if (from_self) {
938                 tag = "fancy-header-self";
939                 line_top_tag = "fancy-line-top-self";
940                 line_bottom_tag = "fancy-line-bottom-self";
941         } else {
942                 tag = "fancy-header-other";
943                 line_top_tag = "fancy-line-top-other";
944                 line_bottom_tag = "fancy-line-bottom-other";
945         }
946
947         header = FALSE;
948
949         /* Only insert a header if the previously inserted block is not the same
950          * as this one. This catches all the different cases:
951          */
952         if (priv->last_block_type != BLOCK_TYPE_SELF &&
953             priv->last_block_type != BLOCK_TYPE_OTHER) {
954                 header = TRUE;
955         }
956         else if (from_self && priv->last_block_type == BLOCK_TYPE_OTHER) {
957                 header = TRUE;
958         }
959         else if (!from_self && priv->last_block_type == BLOCK_TYPE_SELF) {
960                 header = TRUE;
961         }
962         else if (!from_self &&
963                  (!priv->last_contact ||
964                   !empathy_contact_equal (sender, priv->last_contact))) {
965                 header = TRUE;
966         }
967
968         if (!header) {
969                 return;
970         }
971
972         chat_view_append_spacing (view);
973
974         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
975         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
976                                                   &iter,
977                                                   "\n",
978                                                   -1,
979                                                   line_top_tag,
980                                                   NULL);
981
982         /* FIXME: we should have a cash of avatar pixbufs */
983         pixbuf = empathy_pixbuf_avatar_from_contact_scaled (sender, 32, 32);
984         if (pixbuf) {
985                 avatar = chat_view_pad_to_size (pixbuf, 32, 32, 6);
986                 g_object_unref (pixbuf);
987         }
988
989         if (avatar) {
990                 GtkTextIter start;
991
992                 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
993                 gtk_text_buffer_insert_pixbuf (priv->buffer, &iter, avatar);
994
995                 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
996                 start = iter;
997                 gtk_text_iter_backward_char (&start);
998
999                 if (from_self) {
1000                         gtk_text_buffer_apply_tag_by_name (priv->buffer,
1001                                                            "fancy-avatar-self",
1002                                                            &start, &iter);
1003                         avatar_tag = "fancy-header-self-avatar";
1004                 } else {
1005                         gtk_text_buffer_apply_tag_by_name (priv->buffer,
1006                                                            "fancy-avatar-other",
1007                                                            &start, &iter);
1008                         avatar_tag = "fancy-header-other-avatar";
1009                 }
1010
1011                 g_object_unref (avatar);
1012         } else {
1013                 avatar_tag = NULL;
1014         }
1015
1016         tmp = g_strdup_printf ("%s\n", name);
1017
1018         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1019         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1020                                                   &iter,
1021                                                   tmp,
1022                                                   -1,
1023                                                   tag,
1024                                                   avatar_tag,
1025                                                   NULL);
1026         g_free (tmp);
1027
1028         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1029         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1030                                                   &iter,
1031                                                   "\n",
1032                                                   -1,
1033                                                   line_bottom_tag,
1034                                                   NULL);
1035 }
1036
1037 static void
1038 chat_view_append_irc_action (EmpathyChatView *view,
1039                              EmpathyMessage  *msg)
1040 {
1041         EmpathyChatViewPriv *priv;
1042         EmpathyContact      *sender;
1043         const gchar        *name;
1044         GtkTextIter         iter;
1045         const gchar        *body;
1046         gchar              *tmp;
1047         const gchar        *tag;
1048
1049         priv = GET_PRIV (view);
1050
1051         empathy_debug (DEBUG_DOMAIN, "Add IRC action");
1052
1053         sender = empathy_message_get_sender (msg);
1054         name = empathy_contact_get_name (sender);
1055
1056         if (empathy_contact_is_user (sender)) {
1057                 tag = "irc-action-self";
1058         } else {
1059                 tag = "irc-action-other";
1060         }
1061
1062         if (priv->last_block_type != BLOCK_TYPE_SELF &&
1063             priv->last_block_type != BLOCK_TYPE_OTHER) {
1064                 chat_view_append_spacing (view);
1065         }
1066
1067         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1068
1069         tmp = g_strdup_printf (" * %s ", name);
1070         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1071                                                   &iter,
1072                                                   tmp,
1073                                                   -1,
1074                                                   "cut",
1075                                                   tag,
1076                                                   NULL);
1077         g_free (tmp);
1078
1079         body = empathy_message_get_body (msg);
1080         chat_view_append_text (view, body, tag);
1081 }
1082
1083 static void
1084 chat_view_append_fancy_action (EmpathyChatView *view,
1085                                EmpathyMessage  *msg)
1086 {
1087         EmpathyChatViewPriv *priv;
1088         EmpathyContact      *sender;
1089         const gchar        *name;
1090         const gchar        *body;
1091         GtkTextIter         iter;
1092         gchar              *tmp;
1093         const gchar        *tag;
1094         const gchar        *line_tag;
1095
1096         priv = GET_PRIV (view);
1097
1098         empathy_debug (DEBUG_DOMAIN, "Add fancy action");
1099
1100         sender = empathy_message_get_sender (msg);
1101         name = empathy_contact_get_name (sender);
1102
1103         if (empathy_contact_is_user (sender)) {
1104                 tag = "fancy-action-self";
1105                 line_tag = "fancy-line-self";
1106         } else {
1107                 tag = "fancy-action-other";
1108                 line_tag = "fancy-line-other";
1109         }
1110
1111         tmp = g_strdup_printf (" * %s ", name);
1112         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1113         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1114                                                   &iter,
1115                                                   tmp,
1116                                                   -1,
1117                                                   tag,
1118                                                   NULL);
1119         g_free (tmp);
1120
1121         body = empathy_message_get_body (msg);
1122         chat_view_append_text (view, body, tag);
1123 }
1124
1125 static void
1126 chat_view_append_irc_message (EmpathyChatView *view,
1127                               EmpathyMessage  *msg)
1128 {
1129         EmpathyChatViewPriv *priv;
1130         EmpathyContact      *sender;
1131         const gchar        *name;
1132         const gchar        *body;
1133         const gchar        *nick_tag;
1134         const gchar        *body_tag;
1135         GtkTextIter         iter;
1136         gchar              *tmp;
1137
1138         priv = GET_PRIV (view);
1139
1140         empathy_debug (DEBUG_DOMAIN, "Add IRC message");
1141
1142         body = empathy_message_get_body (msg);
1143         sender = empathy_message_get_sender (msg);
1144         name = empathy_contact_get_name (sender);
1145
1146         if (empathy_contact_is_user (sender)) {
1147                 nick_tag = "irc-nick-self";
1148                 body_tag = "irc-body-self";
1149         } else {
1150                 if (empathy_chat_should_highlight_nick (msg)) {
1151                         nick_tag = "irc-nick-highlight";
1152                 } else {
1153                         nick_tag = "irc-nick-other";
1154                 }
1155
1156                 body_tag = "irc-body-other";
1157         }
1158
1159         if (priv->last_block_type != BLOCK_TYPE_SELF &&
1160             priv->last_block_type != BLOCK_TYPE_OTHER) {
1161                 chat_view_append_spacing (view);
1162         }
1163
1164         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1165
1166         /* The nickname. */
1167         tmp = g_strdup_printf ("%s: ", name);
1168         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1169                                                   &iter,
1170                                                   tmp,
1171                                                   -1,
1172                                                   "cut",
1173                                                   nick_tag,
1174                                                   NULL);
1175         g_free (tmp);
1176
1177         /* The text body. */
1178         chat_view_append_text (view, body, body_tag);
1179 }
1180
1181 static void
1182 chat_view_append_fancy_message (EmpathyChatView *view,
1183                                 EmpathyMessage  *msg)
1184 {
1185         EmpathyChatViewPriv *priv;
1186         EmpathyContact      *sender;
1187         const gchar        *body;
1188         const gchar        *tag;
1189
1190         priv = GET_PRIV (view);
1191
1192         sender = empathy_message_get_sender (msg);
1193
1194         if (empathy_contact_is_user (sender)) {
1195                 tag = "fancy-body-self";
1196         } else {
1197                 tag = "fancy-body-other";
1198
1199                 /* FIXME: Might want to support nick highlighting here... */
1200         }
1201
1202         body = empathy_message_get_body (msg);
1203         chat_view_append_text (view, body, tag);
1204 }
1205
1206 static void
1207 chat_view_theme_changed_cb (EmpathyThemeManager *manager,
1208                             EmpathyChatView     *view)
1209 {
1210         EmpathyChatViewPriv *priv;
1211         gboolean            show_avatars = FALSE;
1212         gboolean            theme_rooms = FALSE;
1213
1214         priv = GET_PRIV (view);
1215
1216         priv->last_block_type = BLOCK_TYPE_NONE;
1217
1218         empathy_conf_get_bool (empathy_conf_get (),
1219                               EMPATHY_PREFS_CHAT_THEME_CHAT_ROOM,
1220                               &theme_rooms);
1221         if (!theme_rooms && priv->is_group_chat) {
1222                 empathy_theme_manager_apply (manager, view, NULL);
1223         } else {
1224                 empathy_theme_manager_apply_saved (manager, view);
1225         }
1226
1227         /* Needed for now to update the "rise" property of the names to get it
1228          * vertically centered.
1229          */
1230         empathy_conf_get_bool (empathy_conf_get (),
1231                                EMPATHY_PREFS_UI_SHOW_AVATARS,
1232                                &show_avatars);
1233         empathy_theme_manager_update_show_avatars (manager, view, show_avatars);
1234 }
1235
1236 /* Pads a pixbuf to the specified size, by centering it in a larger transparent
1237  * pixbuf. Returns a new ref.
1238  */
1239 static GdkPixbuf *
1240 chat_view_pad_to_size (GdkPixbuf *pixbuf,
1241                        gint       width,
1242                        gint       height,
1243                        gint       extra_padding_right)
1244 {
1245         gint       src_width, src_height;
1246         GdkPixbuf *padded;
1247         gint       x_offset, y_offset;
1248
1249         src_width = gdk_pixbuf_get_width (pixbuf);
1250         src_height = gdk_pixbuf_get_height (pixbuf);
1251
1252         x_offset = (width - src_width) / 2;
1253         y_offset = (height - src_height) / 2;
1254
1255         padded = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (pixbuf),
1256                                  TRUE, /* alpha */
1257                                  gdk_pixbuf_get_bits_per_sample (pixbuf),
1258                                  width + extra_padding_right,
1259                                  height);
1260
1261         gdk_pixbuf_fill (padded, 0);
1262
1263         gdk_pixbuf_copy_area (pixbuf,
1264                               0, /* source coords */
1265                               0,
1266                               src_width,
1267                               src_height,
1268                               padded,
1269                               x_offset, /* dest coords */
1270                               y_offset);
1271
1272         return padded;
1273 }
1274
1275 EmpathyChatView *
1276 empathy_chat_view_new (void)
1277 {
1278         return g_object_new (EMPATHY_TYPE_CHAT_VIEW, NULL);
1279 }
1280
1281 /* The name is optional, if NULL, the sender for msg is used. */
1282 void
1283 empathy_chat_view_append_message (EmpathyChatView *view,
1284                                  EmpathyMessage  *msg)
1285 {
1286         EmpathyChatViewPriv *priv;
1287         EmpathyContact      *sender;
1288         const gchar        *body;
1289         gboolean            scroll_down;
1290
1291         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1292         g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
1293
1294         priv = GET_PRIV (view);
1295
1296         body = empathy_message_get_body (msg);
1297         if (!body) {
1298                 return;
1299         }
1300
1301         scroll_down = chat_view_is_scrolled_down (view);
1302
1303         chat_view_maybe_trim_buffer (view);
1304         chat_view_maybe_append_date_and_time (view, msg);
1305
1306         sender = empathy_message_get_sender (msg);
1307
1308         if (!priv->irc_style) {
1309                 chat_view_maybe_append_fancy_header (view, msg);
1310         }
1311
1312         if (empathy_message_get_type (msg) == EMPATHY_MESSAGE_TYPE_ACTION) {
1313                 if (priv->irc_style) {
1314                         chat_view_append_irc_action (view, msg);
1315                 } else {
1316                         chat_view_append_fancy_action (view, msg);
1317                 }
1318         } else {
1319                 if (priv->irc_style) {
1320                         chat_view_append_irc_message (view, msg);
1321                 } else {
1322                         chat_view_append_fancy_message (view, msg);
1323                 }
1324         }
1325
1326         /* Reset the last inserted contact. */
1327         if (priv->last_contact) {
1328                 g_object_unref (priv->last_contact);
1329         }
1330
1331         if (empathy_contact_is_user (sender)) {
1332                 priv->last_block_type = BLOCK_TYPE_SELF;
1333                 priv->last_contact = NULL;
1334         } else {
1335                 priv->last_block_type = BLOCK_TYPE_OTHER;
1336                 priv->last_contact = g_object_ref (sender);
1337         }
1338
1339         if (scroll_down) {
1340                 empathy_chat_view_scroll_down (view);
1341         }
1342 }
1343
1344 void
1345 empathy_chat_view_append_event (EmpathyChatView *view,
1346                                const gchar    *str)
1347 {
1348         EmpathyChatViewPriv *priv;
1349         gboolean            bottom;
1350         GtkTextIter         iter;
1351         gchar              *msg;
1352         const gchar        *tag;
1353
1354         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1355         g_return_if_fail (!G_STR_EMPTY (str));
1356
1357         priv = GET_PRIV (view);
1358
1359         bottom = chat_view_is_scrolled_down (view);
1360
1361         chat_view_maybe_trim_buffer (view);
1362
1363         if (priv->irc_style) {
1364                 tag = "irc-event";
1365                 msg = g_strdup_printf (" - %s\n", str);
1366         } else {
1367                 tag = "fancy-event";
1368                 msg = g_strdup_printf (" - %s\n", str);
1369         }
1370
1371         if (priv->last_block_type != BLOCK_TYPE_EVENT) {
1372                 /* Comment out for now. */
1373                 /*chat_view_append_spacing (view);*/
1374         }
1375
1376         chat_view_maybe_append_date_and_time (view, NULL);
1377
1378         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1379
1380         gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
1381                                                   msg, -1,
1382                                                   tag,
1383                                                   NULL);
1384         g_free (msg);
1385
1386         if (bottom) {
1387                 empathy_chat_view_scroll_down (view);
1388         }
1389
1390         priv->last_block_type = BLOCK_TYPE_EVENT;
1391 }
1392
1393 void
1394 empathy_chat_view_append_button (EmpathyChatView *view,
1395                                 const gchar    *message,
1396                                 GtkWidget      *button1,
1397                                 GtkWidget      *button2)
1398 {
1399         EmpathyChatViewPriv   *priv;
1400         GtkTextChildAnchor   *anchor;
1401         GtkTextIter           iter;
1402         gboolean              bottom;
1403         const gchar          *tag;
1404
1405         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1406         g_return_if_fail (button1 != NULL);
1407
1408         priv = GET_PRIV (view);
1409
1410         if (priv->irc_style) {
1411                 tag = "irc-invite";
1412         } else {
1413                 tag = "fancy-invite";
1414         }
1415
1416         bottom = chat_view_is_scrolled_down (view);
1417
1418         chat_view_maybe_append_date_and_time (view, NULL);
1419
1420         if (message) {
1421                 chat_view_append_text (view, message, tag);
1422         }
1423
1424         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1425
1426         anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
1427         gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button1, anchor);
1428         gtk_widget_show (button1);
1429
1430         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1431                                                   &iter,
1432                                                   " ",
1433                                                   1,
1434                                                   tag,
1435                                                   NULL);
1436
1437         if (button2) {
1438                 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1439                 
1440                 anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
1441                 gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button2, anchor);
1442                 gtk_widget_show (button2);
1443                 
1444                 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1445                                                           &iter,
1446                                                           " ",
1447                                                           1,
1448                                                           tag,
1449                                                           NULL);
1450         }
1451
1452         gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1453         gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1454                                                   &iter,
1455                                                   "\n\n",
1456                                                   2,
1457                                                   tag,
1458                                                   NULL);
1459
1460         if (bottom) {
1461                 empathy_chat_view_scroll_down (view);
1462         }
1463
1464         priv->last_block_type = BLOCK_TYPE_INVITE;
1465 }
1466
1467 void
1468 empathy_chat_view_scroll (EmpathyChatView *view,
1469                          gboolean        allow_scrolling)
1470 {
1471         EmpathyChatViewPriv *priv;
1472
1473         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1474
1475         priv = GET_PRIV (view);
1476
1477         priv->allow_scrolling = allow_scrolling;
1478
1479         empathy_debug (DEBUG_DOMAIN, "Scrolling %s",
1480                       allow_scrolling ? "enabled" : "disabled");
1481 }
1482
1483 /* Code stolen from pidgin/gtkimhtml.c */
1484 static gboolean
1485 chat_view_scroll_cb (EmpathyChatView *view)
1486 {
1487         EmpathyChatViewPriv *priv;
1488         GtkAdjustment      *adj;
1489         gdouble             max_val;
1490
1491         priv = GET_PRIV (view);
1492         adj = GTK_TEXT_VIEW (view)->vadjustment;
1493         max_val = adj->upper - adj->page_size;
1494
1495         g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
1496
1497         if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
1498                 /* time's up. jump to the end and kill the timer */
1499                 gtk_adjustment_set_value (adj, max_val);
1500                 g_timer_destroy (priv->scroll_time);
1501                 priv->scroll_time = NULL;
1502                 priv->scroll_timeout = 0;
1503                 return FALSE;
1504         }
1505
1506         /* scroll by 1/3rd the remaining distance */
1507         gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
1508         return TRUE;
1509 }
1510
1511 void
1512 empathy_chat_view_scroll_down (EmpathyChatView *view)
1513 {
1514         EmpathyChatViewPriv *priv;
1515
1516         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1517
1518         priv = GET_PRIV (view);
1519
1520         if (!priv->allow_scrolling) {
1521                 return;
1522         }
1523
1524         empathy_debug (DEBUG_DOMAIN, "Scrolling down");
1525
1526         if (priv->scroll_time) {
1527                 g_timer_reset (priv->scroll_time);
1528         } else {
1529                 priv->scroll_time = g_timer_new();
1530         }
1531         if (!priv->scroll_timeout) {
1532                 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
1533                                                       (GSourceFunc) chat_view_scroll_cb,
1534                                                       view);
1535         }
1536 }
1537
1538 gboolean
1539 empathy_chat_view_get_selection_bounds (EmpathyChatView *view,
1540                                        GtkTextIter    *start,
1541                                        GtkTextIter    *end)
1542 {
1543         GtkTextBuffer *buffer;
1544
1545         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1546
1547         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1548
1549         return gtk_text_buffer_get_selection_bounds (buffer, start, end);
1550 }
1551
1552 void
1553 empathy_chat_view_clear (EmpathyChatView *view)
1554 {
1555         GtkTextBuffer      *buffer;
1556         EmpathyChatViewPriv *priv;
1557
1558         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1559
1560         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1561         gtk_text_buffer_set_text (buffer, "", -1);
1562
1563         /* We set these back to the initial values so we get
1564          * timestamps when clearing the window to know when
1565          * conversations start.
1566          */
1567         priv = GET_PRIV (view);
1568
1569         priv->last_block_type = BLOCK_TYPE_NONE;
1570         priv->last_timestamp = 0;
1571 }
1572
1573 gboolean
1574 empathy_chat_view_find_previous (EmpathyChatView *view,
1575                                 const gchar    *search_criteria,
1576                                 gboolean        new_search)
1577 {
1578         EmpathyChatViewPriv *priv;
1579         GtkTextBuffer      *buffer;
1580         GtkTextIter         iter_at_mark;
1581         GtkTextIter         iter_match_start;
1582         GtkTextIter         iter_match_end;
1583         gboolean            found;
1584         gboolean            from_start = FALSE;
1585
1586         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1587         g_return_val_if_fail (search_criteria != NULL, FALSE);
1588
1589         priv = GET_PRIV (view);
1590
1591         buffer = priv->buffer;
1592
1593         if (G_STR_EMPTY (search_criteria)) {
1594                 if (priv->find_mark_previous) {
1595                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1596
1597                         gtk_text_buffer_move_mark (buffer,
1598                                                    priv->find_mark_previous,
1599                                                    &iter_at_mark);
1600                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1601                                                       priv->find_mark_previous,
1602                                                       0.0,
1603                                                       TRUE,
1604                                                       0.0,
1605                                                       0.0);
1606                         gtk_text_buffer_select_range (buffer,
1607                                                       &iter_at_mark,
1608                                                       &iter_at_mark);
1609                 }
1610
1611                 return FALSE;
1612         }
1613
1614         if (new_search) {
1615                 from_start = TRUE;
1616         }
1617
1618         if (priv->find_mark_previous) {
1619                 gtk_text_buffer_get_iter_at_mark (buffer,
1620                                                   &iter_at_mark,
1621                                                   priv->find_mark_previous);
1622         } else {
1623                 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
1624                 from_start = TRUE;
1625         }
1626
1627         priv->find_last_direction = FALSE;
1628
1629         found = empathy_text_iter_backward_search (&iter_at_mark,
1630                                                   search_criteria,
1631                                                   &iter_match_start,
1632                                                   &iter_match_end,
1633                                                   NULL);
1634
1635         if (!found) {
1636                 gboolean result = FALSE;
1637
1638                 if (from_start) {
1639                         return result;
1640                 }
1641
1642                 /* Here we wrap around. */
1643                 if (!new_search && !priv->find_wrapped) {
1644                         priv->find_wrapped = TRUE;
1645                         result = empathy_chat_view_find_previous (view, 
1646                                                                  search_criteria, 
1647                                                                  FALSE);
1648                         priv->find_wrapped = FALSE;
1649                 }
1650
1651                 return result;
1652         }
1653
1654         /* Set new mark and show on screen */
1655         if (!priv->find_mark_previous) {
1656                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1657                                                                         &iter_match_start,
1658                                                                         TRUE);
1659         } else {
1660                 gtk_text_buffer_move_mark (buffer,
1661                                            priv->find_mark_previous,
1662                                            &iter_match_start);
1663         }
1664
1665         if (!priv->find_mark_next) {
1666                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1667                                                                     &iter_match_end,
1668                                                                     TRUE);
1669         } else {
1670                 gtk_text_buffer_move_mark (buffer,
1671                                            priv->find_mark_next,
1672                                            &iter_match_end);
1673         }
1674
1675         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1676                                       priv->find_mark_previous,
1677                                       0.0,
1678                                       TRUE,
1679                                       0.5,
1680                                       0.5);
1681
1682         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1683         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1684
1685         return TRUE;
1686 }
1687
1688 gboolean
1689 empathy_chat_view_find_next (EmpathyChatView *view,
1690                             const gchar    *search_criteria,
1691                             gboolean        new_search)
1692 {
1693         EmpathyChatViewPriv *priv;
1694         GtkTextBuffer      *buffer;
1695         GtkTextIter         iter_at_mark;
1696         GtkTextIter         iter_match_start;
1697         GtkTextIter         iter_match_end;
1698         gboolean            found;
1699         gboolean            from_start = FALSE;
1700
1701         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1702         g_return_val_if_fail (search_criteria != NULL, FALSE);
1703
1704         priv = GET_PRIV (view);
1705
1706         buffer = priv->buffer;
1707
1708         if (G_STR_EMPTY (search_criteria)) {
1709                 if (priv->find_mark_next) {
1710                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1711
1712                         gtk_text_buffer_move_mark (buffer,
1713                                                    priv->find_mark_next,
1714                                                    &iter_at_mark);
1715                         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1716                                                       priv->find_mark_next,
1717                                                       0.0,
1718                                                       TRUE,
1719                                                       0.0,
1720                                                       0.0);
1721                         gtk_text_buffer_select_range (buffer,
1722                                                       &iter_at_mark,
1723                                                       &iter_at_mark);
1724                 }
1725
1726                 return FALSE;
1727         }
1728
1729         if (new_search) {
1730                 from_start = TRUE;
1731         }
1732
1733         if (priv->find_mark_next) {
1734                 gtk_text_buffer_get_iter_at_mark (buffer,
1735                                                   &iter_at_mark,
1736                                                   priv->find_mark_next);
1737         } else {
1738                 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1739                 from_start = TRUE;
1740         }
1741
1742         priv->find_last_direction = TRUE;
1743
1744         found = empathy_text_iter_forward_search (&iter_at_mark,
1745                                                  search_criteria,
1746                                                  &iter_match_start,
1747                                                  &iter_match_end,
1748                                                  NULL);
1749
1750         if (!found) {
1751                 gboolean result = FALSE;
1752
1753                 if (from_start) {
1754                         return result;
1755                 }
1756
1757                 /* Here we wrap around. */
1758                 if (!new_search && !priv->find_wrapped) {
1759                         priv->find_wrapped = TRUE;
1760                         result = empathy_chat_view_find_next (view, 
1761                                                              search_criteria, 
1762                                                              FALSE);
1763                         priv->find_wrapped = FALSE;
1764                 }
1765
1766                 return result;
1767         }
1768
1769         /* Set new mark and show on screen */
1770         if (!priv->find_mark_next) {
1771                 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1772                                                                &iter_match_end,
1773                                                                TRUE);
1774         } else {
1775                 gtk_text_buffer_move_mark (buffer,
1776                                            priv->find_mark_next,
1777                                            &iter_match_end);
1778         }
1779
1780         if (!priv->find_mark_previous) {
1781                 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1782                                                                         &iter_match_start,
1783                                                                         TRUE);
1784         } else {
1785                 gtk_text_buffer_move_mark (buffer,
1786                                            priv->find_mark_previous,
1787                                            &iter_match_start);
1788         }
1789
1790         gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1791                                       priv->find_mark_next,
1792                                       0.0,
1793                                       TRUE,
1794                                       0.5,
1795                                       0.5);
1796
1797         gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1798         gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1799
1800         return TRUE;
1801 }
1802
1803
1804 void
1805 empathy_chat_view_find_abilities (EmpathyChatView *view,
1806                                  const gchar    *search_criteria,
1807                                  gboolean       *can_do_previous,
1808                                  gboolean       *can_do_next)
1809 {
1810         EmpathyChatViewPriv *priv;
1811         GtkTextBuffer      *buffer;
1812         GtkTextIter         iter_at_mark;
1813         GtkTextIter         iter_match_start;
1814         GtkTextIter         iter_match_end;
1815
1816         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1817         g_return_if_fail (search_criteria != NULL);
1818         g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1819
1820         priv = GET_PRIV (view);
1821
1822         buffer = priv->buffer;
1823
1824         if (can_do_previous) {
1825                 if (priv->find_mark_previous) {
1826                         gtk_text_buffer_get_iter_at_mark (buffer,
1827                                                           &iter_at_mark,
1828                                                           priv->find_mark_previous);
1829                 } else {
1830                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1831                 }
1832                 
1833                 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1834                                                                      search_criteria,
1835                                                                      &iter_match_start,
1836                                                                      &iter_match_end,
1837                                                                      NULL);
1838         }
1839
1840         if (can_do_next) {
1841                 if (priv->find_mark_next) {
1842                         gtk_text_buffer_get_iter_at_mark (buffer,
1843                                                           &iter_at_mark,
1844                                                           priv->find_mark_next);
1845                 } else {
1846                         gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1847                 }
1848                 
1849                 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1850                                                                 search_criteria,
1851                                                                 &iter_match_start,
1852                                                                 &iter_match_end,
1853                                                                 NULL);
1854         }
1855 }
1856
1857 void
1858 empathy_chat_view_highlight (EmpathyChatView *view,
1859                              const gchar     *text)
1860 {
1861         GtkTextBuffer *buffer;
1862         GtkTextIter    iter;
1863         GtkTextIter    iter_start;
1864         GtkTextIter    iter_end;
1865         GtkTextIter    iter_match_start;
1866         GtkTextIter    iter_match_end;
1867         gboolean       found;
1868
1869         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1870
1871         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1872
1873         gtk_text_buffer_get_start_iter (buffer, &iter);
1874
1875         gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1876         gtk_text_buffer_remove_tag_by_name (buffer, "highlight",
1877                                             &iter_start,
1878                                             &iter_end);
1879
1880         if (G_STR_EMPTY (text)) {
1881                 return;
1882         }
1883
1884         while (1) {
1885                 found = empathy_text_iter_forward_search (&iter,
1886                                                          text,
1887                                                          &iter_match_start,
1888                                                          &iter_match_end,
1889                                                          NULL);
1890
1891                 if (!found) {
1892                         break;
1893                 }
1894
1895                 gtk_text_buffer_apply_tag_by_name (buffer, "highlight",
1896                                                    &iter_match_start,
1897                                                    &iter_match_end);
1898
1899                 iter = iter_match_end;
1900                 gtk_text_iter_forward_char (&iter);
1901         }
1902 }
1903
1904 void
1905 empathy_chat_view_copy_clipboard (EmpathyChatView *view)
1906 {
1907         GtkTextBuffer *buffer;
1908         GtkClipboard  *clipboard;
1909
1910         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1911
1912         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1913         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1914
1915         gtk_text_buffer_copy_clipboard (buffer, clipboard);
1916 }
1917
1918 gboolean
1919 empathy_chat_view_get_irc_style (EmpathyChatView *view)
1920 {
1921         EmpathyChatViewPriv *priv;
1922
1923         g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1924
1925         priv = GET_PRIV (view);
1926
1927         return priv->irc_style;
1928 }
1929
1930 void
1931 empathy_chat_view_set_irc_style (EmpathyChatView *view,
1932                                 gboolean        irc_style)
1933 {
1934         EmpathyChatViewPriv *priv;
1935
1936         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1937
1938         priv = GET_PRIV (view);
1939
1940         priv->irc_style = irc_style;
1941 }
1942
1943 void
1944 empathy_chat_view_set_margin (EmpathyChatView *view,
1945                              gint            margin)
1946 {
1947         EmpathyChatViewPriv *priv;
1948
1949         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1950
1951         priv = GET_PRIV (view);
1952
1953         g_object_set (view,
1954                       "left-margin", margin,
1955                       "right-margin", margin,
1956                       NULL);
1957 }
1958
1959 GtkWidget *
1960 empathy_chat_view_get_smiley_menu (GCallback    callback,
1961                                    gpointer     user_data)
1962 {
1963         EmpathySmileyManager *smiley_manager;
1964         GSList               *smileys, *l;
1965         GtkWidget            *menu;
1966         gint                  x = 0;
1967         gint                  y = 0;
1968
1969         g_return_val_if_fail (callback != NULL, NULL);
1970
1971         menu = gtk_menu_new ();
1972
1973         smiley_manager = empathy_smiley_manager_new ();
1974         smileys = empathy_smiley_manager_get_all (smiley_manager);
1975         for (l = smileys; l; l = l->next) {
1976                 EmpathySmiley *smiley;
1977                 GtkWidget     *item;
1978                 GtkWidget     *image;
1979
1980                 smiley = l->data;
1981                 image = gtk_image_new_from_pixbuf (smiley->pixbuf);
1982
1983                 item = gtk_image_menu_item_new_with_label ("");
1984                 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1985
1986                 gtk_menu_attach (GTK_MENU (menu), item,
1987                                  x, x + 1, y, y + 1);
1988
1989                 gtk_widget_set_tooltip_text (item, smiley->str);
1990
1991                 g_object_set_data  (G_OBJECT (item), "smiley_text", smiley->str);
1992                 g_signal_connect (item, "activate", callback, user_data);
1993
1994                 if (x > 3) {
1995                         y++;
1996                         x = 0;
1997                 } else {
1998                         x++;
1999                 }
2000         }
2001         g_object_unref (smiley_manager);
2002
2003         gtk_widget_show_all (menu);
2004
2005         return menu;
2006 }
2007
2008 /* FIXME: Do we really need this? Better to do it internally only at setup time,
2009  * we will never change it on the fly.
2010  */
2011 void
2012 empathy_chat_view_set_is_group_chat (EmpathyChatView *view,
2013                                     gboolean        is_group_chat)
2014 {
2015         EmpathyChatViewPriv *priv;
2016         gboolean            theme_rooms = FALSE;
2017
2018         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
2019
2020         priv = GET_PRIV (view);
2021
2022         priv->is_group_chat = is_group_chat;
2023
2024         empathy_conf_get_bool (empathy_conf_get (),
2025                               EMPATHY_PREFS_CHAT_THEME_CHAT_ROOM,
2026                               &theme_rooms);
2027
2028         if (!theme_rooms && is_group_chat) {
2029                 empathy_theme_manager_apply (empathy_theme_manager_get (),
2030                                             view,
2031                                             NULL);
2032         } else {
2033                 empathy_theme_manager_apply_saved (empathy_theme_manager_get (),
2034                                                   view);
2035         }
2036 }