static void
theme_boxes_append_message (EmpathyChatTextView *view,
			    EmpathyMessage      *message)
{
	EmpathyContact *sender;

	theme_boxes_maybe_append_header (EMPATHY_THEME_BOXES (view), message);

	sender = empathy_message_get_sender (message);
	if (empathy_message_get_tptype (message) ==
	    TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
		gchar *body;

		body = g_strdup_printf (" * %s %s",
					empathy_contact_get_alias (sender),
					empathy_message_get_body (message));
		empathy_chat_text_view_append_body (EMPATHY_CHAT_TEXT_VIEW (view),
						    body,
						    EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION);
	} else {
		empathy_chat_text_view_append_body (EMPATHY_CHAT_TEXT_VIEW (view),
						    empathy_message_get_body (message),
						    EMPATHY_CHAT_TEXT_VIEW_TAG_BODY);
	}
}
static void
chat_text_view_finalize (GObject *object)
{
	EmpathyChatTextView     *view;
	EmpathyChatTextViewPriv *priv;
	
	view = EMPATHY_CHAT_TEXT_VIEW (object);
	priv = GET_PRIV (view);
	
	DEBUG ("%p", object);
	
	empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
	
	if (priv->last_contact) {
		g_object_unref (priv->last_contact);
	}
	if (priv->scroll_time) {
		g_timer_destroy (priv->scroll_time);
	}
	if (priv->scroll_timeout) {
		g_source_remove (priv->scroll_timeout);
	}
	
	G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
}
static void
chat_text_view_finalize (GObject *object)
{
	EmpathyChatTextView     *view;
	EmpathyChatTextViewPriv *priv;

	view = EMPATHY_CHAT_TEXT_VIEW (object);
	priv = GET_PRIV (view);

	DEBUG ("%p", object);

	g_object_unref (priv->gsettings_chat);
	g_object_unref (priv->gsettings_desktop);

	if (priv->last_contact) {
		g_object_unref (priv->last_contact);
	}
	if (priv->scroll_time) {
		g_timer_destroy (priv->scroll_time);
	}
	if (priv->scroll_timeout) {
		g_source_remove (priv->scroll_timeout);
	}
	g_object_unref (priv->smiley_manager);

	G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
}
static void
chat_text_view_append_message (EmpathyChatView *view,
			       EmpathyMessage  *msg)
{
	EmpathyChatTextView     *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
	EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
	gboolean                 bottom;
	gint64                   timestamp;

	g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
	g_return_if_fail (EMPATHY_IS_MESSAGE (msg));

	if (!empathy_message_get_body (msg)) {
		return;
	}

	bottom = chat_text_view_is_scrolled_down (text_view);

	chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));

	timestamp = empathy_message_get_timestamp (msg);
	chat_text_maybe_append_date_and_time (text_view, timestamp);
	if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
		EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
									 msg);
	}

	if (bottom) {
		chat_text_view_scroll_down (view);
	}

	if (priv->last_contact) {
		g_object_unref (priv->last_contact);
	}
	priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
	g_object_notify (G_OBJECT (view), "last-contact");

	priv->last_timestamp = timestamp;
}
static void
chat_text_view_append_event (EmpathyChatView *view,
			     const gchar     *str)
{
	EmpathyChatTextView     *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
	EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
	gboolean                 bottom;
	GtkTextIter              iter;
	gchar                   *msg;


	g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
	g_return_if_fail (!EMP_STR_EMPTY (str));

	bottom = chat_text_view_is_scrolled_down (text_view);
	chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
	chat_text_maybe_append_date_and_time (text_view,
					      empathy_time_get_current ());

	gtk_text_buffer_get_end_iter (priv->buffer, &iter);
	msg = g_strdup_printf (" - %s\n", str);
	gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
						  msg, -1,
						  EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
						  NULL);
	g_free (msg);

	if (bottom) {
		chat_text_view_scroll_down (view);
	}
	
	if (priv->last_contact) {
		g_object_unref (priv->last_contact);
		priv->last_contact = NULL;
		g_object_notify (G_OBJECT (view), "last-contact");
	}
}
static void
chat_text_view_size_allocate (GtkWidget     *widget,
			      GtkAllocation *alloc)
{
	gboolean down;
	
	down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
	
	GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
	
	if (down) {
		GtkAdjustment *adj;
		
		adj = GTK_TEXT_VIEW (widget)->vadjustment;
		gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
	}
}
static void
chat_text_view_size_allocate (GtkWidget     *widget,
			      GtkAllocation *alloc)
{
	gboolean down;

	down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));

	GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);

	if (down) {
		GtkAdjustment *adj;

		adj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (widget));
		gtk_adjustment_set_value (adj,
					  gtk_adjustment_get_upper (adj) -
					  gtk_adjustment_get_page_size (adj));
	}
}
static void
theme_boxes_maybe_append_header (EmpathyThemeBoxes *theme,
				 EmpathyMessage    *msg)
{
	EmpathyChatTextView  *view = EMPATHY_CHAT_TEXT_VIEW (theme);
	EmpathyThemeBoxesPriv*priv = GET_PRIV (theme);
	EmpathyContact       *contact;
	EmpathyContact       *last_contact;
	GdkPixbuf            *avatar = NULL;
	GtkTextBuffer        *buffer;
	const gchar          *name;
	GtkTextIter           iter;
	GtkWidget            *label1, *label2;
	GtkTextChildAnchor   *anchor;
	GtkWidget            *box;
	gchar                *str;
	time_t                time_;
	gchar                *tmp;
	GtkTextIter           start;
	gboolean              color_set;
	GtkTextTagTable      *table;
	GtkTextTag           *tag;
	GString              *str_obj;
	gboolean              consecutive;

	contact = empathy_message_get_sender (msg);
	name = empathy_contact_get_alias (contact);
	last_contact = empathy_chat_text_view_get_last_contact (view);
	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (theme));
	time_ = empathy_message_get_timestamp (msg);
	consecutive = (time_ - empathy_chat_text_view_get_last_timestamp (view)
		< MESSAGE_JOIN_PERIOD);

	DEBUG ("Maybe add fancy header");

	/* Only insert a header if
	 *   - the previously inserted block is not the same as this one.
	 *   - the delay between two messages is lower then MESSAGE_JOIN_PERIOD
	 */
	if (empathy_contact_equal (last_contact, contact) && consecutive) {
		return;
	}

	empathy_chat_text_view_append_spacing (view);

	/* Insert header line */
	gtk_text_buffer_get_end_iter (buffer, &iter);
	gtk_text_buffer_insert_with_tags_by_name (buffer,
						  &iter,
						  "\n",
						  -1,
						  EMPATHY_THEME_BOXES_TAG_HEADER_LINE,
						  NULL);

	gtk_text_buffer_get_end_iter (buffer, &iter);
	anchor = gtk_text_buffer_create_child_anchor (buffer, &iter);

	/* Create a hbox for the header and resize it when the view allocation
	 * changes */
	box = gtk_hbox_new (FALSE, 0);
	g_signal_connect_object (view, "size-allocate",
				 G_CALLBACK (table_size_allocate_cb),
				 box, 0);

	/* Add avatar to the box if needed */
	if (priv->show_avatars) {
		avatar = theme_boxes_get_avatar_pixbuf_with_cache (contact);
		if (avatar) {
			GtkWidget *image;

			image = gtk_image_new_from_pixbuf (avatar);

			gtk_box_pack_start (GTK_BOX (box), image,
					    FALSE, TRUE, 2);
		}
	}

	/* Add contact alias */
	str = g_markup_printf_escaped ("<b>%s</b>", name);
	label1 = g_object_new (GTK_TYPE_LABEL,
			       "label", str,
			       "use-markup", TRUE,
			       "xalign", 0.0,
			       NULL);
	g_free (str);

	/* Add the message receive time */
	tmp = empathy_time_to_string_local (time_,
					   EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
	str = g_strdup_printf ("<i>%s</i>", tmp);
	label2 = g_object_new (GTK_TYPE_LABEL,
			       "label", str,
			       "use-markup", TRUE,
			       "xalign", 1.0,
			       NULL);

	str_obj = g_string_new ("\n- ");
	g_string_append (str_obj, name);
	g_string_append (str_obj, ", ");
	g_string_append (str_obj, tmp);
	g_string_append (str_obj, " -");
	g_free (tmp);
	g_free (str);

	/* Set foreground color of labels to the same color than the header tag. */
	table = gtk_text_buffer_get_tag_table (buffer);
	tag = gtk_text_tag_table_lookup (table, EMPATHY_THEME_BOXES_TAG_HEADER);
	g_object_get (tag, "foreground-set", &color_set, NULL);
	if (color_set) {
		GdkColor *color;

		g_object_get (tag, "foreground-gdk", &color, NULL);
		gtk_widget_modify_fg (label1, GTK_STATE_NORMAL, color);
		gtk_widget_modify_fg (label2, GTK_STATE_NORMAL, color);
		gdk_color_free (color);
	}

	/* Pack labels into the box */
	gtk_misc_set_alignment (GTK_MISC (label1), 0.0, 0.5);
	gtk_misc_set_alignment (GTK_MISC (label2), 1.0, 0.5);
	gtk_box_pack_start (GTK_BOX (box), label1, TRUE, TRUE, 0);
	gtk_box_pack_start (GTK_BOX (box), label2, TRUE, TRUE, 0);

	/* Add the header box to the text view */
	g_object_set_data_full (G_OBJECT (box),
				"str_obj",
				g_string_free (str_obj, FALSE),
				g_free);
	gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view),
					   box,
					   anchor);
	gtk_widget_show_all (box);

	/* Insert a header line */
	gtk_text_buffer_get_end_iter (buffer, &iter);
	start = iter;
	gtk_text_iter_backward_char (&start);
	gtk_text_buffer_apply_tag_by_name (buffer,
					   EMPATHY_THEME_BOXES_TAG_HEADER,
					   &start, &iter);
	gtk_text_buffer_insert_with_tags_by_name (buffer,
						  &iter,
						  "\n",
						  -1,
						  EMPATHY_THEME_BOXES_TAG_HEADER,
						  NULL);
	gtk_text_buffer_get_end_iter (buffer, &iter);
	gtk_text_buffer_insert_with_tags_by_name (buffer,
						  &iter,
						  "\n",
						  -1,
						  EMPATHY_THEME_BOXES_TAG_HEADER_LINE,
						  NULL);
}