/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>  
 *
 *
 * Authors:
 *		Michael Zucchi <notzed@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include <gtk/gtk.h>
#include <glib/gstdio.h>
#include <gdk/gdkkeysyms.h>

#ifdef G_OS_WIN32
/* Work around 'DATADIR' and 'interface' lossage in <windows.h> */
#define DATADIR crap_DATADIR
#include <windows.h>
#undef DATADIR
#undef interface
#endif

#include <gtkhtml/gtkhtml.h>
#include <gtkhtml/gtkhtml-embedded.h>

#include <glade/glade.h>

#include <glib/gi18n.h>

#include <camel/camel-stream.h>
#include <camel/camel-stream-filter.h>
#include <camel/camel-stream-mem.h>
#include <camel/camel-mime-filter-tohtml.h>
#include <camel/camel-mime-part.h>
#include <camel/camel-multipart.h>
#include <camel/camel-internet-address.h>
#include <camel/camel-mime-message.h>
#include <camel/camel-cipher-context.h>
#include <camel/camel-folder.h>
#include <camel/camel-string-utils.h>
#include <camel/camel-operation.h>

#include <misc/e-cursors.h>
#include <e-util/e-util.h>
#include <e-util/e-util-private.h>

#include <libedataserver/e-msgport.h>
#include <e-util/e-gui-utils.h>
#include <e-util/e-dialog-utils.h>
#include <e-util/e-icon-factory.h>

#ifdef HAVE_NSS
#include "certificate-viewer.h"
#include "e-cert-db.h"
#endif

#include "mail-config.h"

#include "e-mail-display.h"
#include "em-format-html-display.h"
#include "em-icon-stream.h"
#include "em-utils.h"
#include "em-popup.h"
#include "widgets/misc/e-attachment-view.h"
#include "widgets/misc/e-attachment-icon-view.h"

#ifdef G_OS_WIN32
/* Undefine the similar macro from <pthread.h>,it doesn't check if
 * localtime() returns NULL.
 */
#undef localtime_r

/* The localtime() in Microsoft's C library is MT-safe */
#define localtime_r(tp,tmp) (localtime(tp)?(*(tmp)=*localtime(tp),(tmp)):0)
#endif

#define d(x)

#define EM_FORMAT_HTML_DISPLAY_GET_PRIVATE(obj) \
	(G_TYPE_INSTANCE_GET_PRIVATE \
	((obj), EM_TYPE_FORMAT_HTML_DISPLAY, EMFormatHTMLDisplayPrivate))

struct _EMFormatHTMLDisplayPrivate {
	/* for Attachment bar */
	GtkWidget *attachment_view;
	GtkWidget *attachment_box;
	GtkWidget *label;
	GtkWidget *save_txt;
	GtkWidget *arrow;
	GtkWidget *forward;
	GtkWidget *down;
	GtkWidget *attachment_area;
	gboolean  show_bar;
	GHashTable *files;
	gboolean updated;
};

struct _smime_pobject {
	EMFormatHTMLPObject object;

	int signature;
	CamelCipherValidity *valid;
	GtkWidget *widget;
};

/* TODO: move the dialogue elsehwere */
/* FIXME: also in em-format-html.c */
static const struct {
	const char *icon, *shortdesc, *description;
} smime_sign_table[5] = {
	{ "stock_signature-bad", N_("Unsigned"), N_("This message is not signed. There is no guarantee that this message is authentic.") },
	{ "stock_signature-ok", N_("Valid signature"), N_("This message is signed and is valid meaning that it is very likely that this message is authentic.") },
	{ "stock_signature-bad", N_("Invalid signature"), N_("The signature of this message cannot be verified, it may have been altered in transit.") },
	{ "stock_signature", N_("Valid signature, but cannot verify sender"), N_("This message is signed with a valid signature, but the sender of the message cannot be verified.") },
	{ "stock_signature-bad", N_("Signature exists, but need public key"), N_("This message is signed with a signature, but there is no corresponding public key.") },

};

static const struct {
	const char *icon, *shortdesc, *description;
} smime_encrypt_table[4] = {
	{ "stock_lock-broken", N_("Unencrypted"), N_("This message is not encrypted. Its content may be viewed in transit across the Internet.") },
	{ "stock_lock-ok", N_("Encrypted, weak"), N_("This message is encrypted, but with a weak encryption algorithm. It would be difficult, but not impossible for an outsider to view the content of this message in a practical amount of time.") },
	{ "stock_lock-ok", N_("Encrypted"), N_("This message is encrypted.  It would be difficult for an outsider to view the content of this message.") },
	{ "stock_lock-ok", N_("Encrypted, strong"), N_("This message is encrypted, with a strong encryption algorithm. It would be very difficult for an outsider to view the content of this message in a practical amount of time.") },
};

static const char *smime_sign_colour[5] = {
	"", " bgcolor=\"#88bb88\"", " bgcolor=\"#bb8888\"", " bgcolor=\"#e8d122\"",""
};

static void efhd_attachment_frame(EMFormat *emf, CamelStream *stream, EMFormatPURI *puri);
static gboolean efhd_attachment_image(EMFormatHTML *efh, GtkHTMLEmbedded *eb, EMFormatHTMLPObject *pobject);
static void efhd_message_add_bar(EMFormat *emf, CamelStream *stream, CamelMimePart *part, const EMFormatHandler *info);
static void efhd_message_update_bar(EMFormat *emf, CamelStream *stream, CamelMimePart *part, const EMFormatHandler *info);
static void efhd_attachment_button_show (GtkWidget *w, void *data);
static gboolean efhd_attachment_button (EMFormatHTML *efh, GtkHTMLEmbedded *eb, EMFormatHTMLPObject *pobject);
static gboolean efhd_attachment_optional (EMFormatHTML *efh, GtkHTMLEmbedded *eb, EMFormatHTMLPObject *object);
static void efhd_attachment_bar_refresh (EMFormatHTMLDisplay *efhd);

struct _attach_puri {
	EMFormatPURI puri;

	const EMFormatHandler *handle;

	const char *snoop_mime_type;

	/* for the > and V buttons */
	GtkWidget *forward, *down;
	/* currently no way to correlate this data to the frame :( */
	GtkHTML *frame;
	CamelStream *output;
	unsigned int shown:1;

	/* Embedded Frame */
	GtkHTMLEmbedded *html;

	/* Attachment */
	EAttachment *attachment;

	/* image stuff */
	int fit_width;
	int fit_height;
	GtkImage *image;
	GtkWidget *event_box;

	/* Optional Text Mem Stream */
	CamelStreamMem *mstream;

	/* Signed / Encrypted */
        camel_cipher_validity_sign_t sign;
        camel_cipher_validity_encrypt_t encrypt;
};


static void efhd_message_prefix(EMFormat *emf, CamelStream *stream, CamelMimePart *part, EMFormatHandler *info);

static void efhd_complete(EMFormat *);
gboolean efhd_mnemonic_show_bar (GtkWidget *widget, gboolean focus, GtkWidget *efhd);

static void efhd_builtin_init(EMFormatHTMLDisplayClass *efhc);

static EMFormatHTMLClass *parent_class;

static void
efhd_xpkcs7mime_free (EMFormatHTMLPObject *o)
{
	struct _smime_pobject *po = (struct _smime_pobject *)o;

	if (po->widget)
		gtk_widget_destroy(po->widget);
	camel_cipher_validity_free(po->valid);
}

static void
efhd_xpkcs7mime_info_response (GtkWidget *widget,
                               guint button,
                               struct _smime_pobject *po)
{
	gtk_widget_destroy (widget);
	po->widget = NULL;
}

#ifdef HAVE_NSS
static void
efhd_xpkcs7mime_viewcert_clicked (GtkWidget *button,
                                  struct _smime_pobject *po)
{
	CamelCipherCertInfo *info = g_object_get_data((GObject *)button, "e-cert-info");
	ECertDB *db = e_cert_db_peek();
	ECert *ec = NULL;

	if (info->email)
		ec = e_cert_db_find_cert_by_email_address(db, info->email, NULL);

	if (ec == NULL && info->name)
		ec = e_cert_db_find_cert_by_nickname(db, info->name, NULL);

	if (ec != NULL) {
		GtkWidget *w = certificate_viewer_show(ec);

		/* oddly enough certificate_viewer_show doesn't ... */
		gtk_widget_show(w);
		g_signal_connect(w, "response", G_CALLBACK(gtk_widget_destroy), NULL);

		if (w && po->widget)
			gtk_window_set_transient_for((GtkWindow *)w, (GtkWindow *)po->widget);

		g_object_unref(ec);
	} else {
		g_warning("can't find certificate for %s <%s>", info->name?info->name:"", info->email?info->email:"");
	}
}
#endif

static void
efhd_xpkcs7mime_add_cert_table (GtkWidget *vbox,
                                CamelDList *certlist,
                                struct _smime_pobject *po)
{
	CamelCipherCertInfo *info = (CamelCipherCertInfo *)certlist->head;
	GtkTable *table = (GtkTable *)gtk_table_new(camel_dlist_length(certlist), 2, FALSE);
	int n = 0;

	while (info->next) {
		char *la = NULL;
		const char *l = NULL;

		if (info->name) {
			if (info->email && strcmp(info->name, info->email) != 0)
				l = la = g_strdup_printf("%s <%s>", info->name, info->email);
			else
				l = info->name;
		} else {
			if (info->email)
				l = info->email;
		}

		if (l) {
			GtkWidget *w;
#if defined(HAVE_NSS)
			ECertDB *db = e_cert_db_peek();
			ECert *ec = NULL;
#endif
			w = gtk_label_new(l);
			gtk_misc_set_alignment((GtkMisc *)w, 0.0, 0.5);
			g_free(la);
			gtk_table_attach(table, w, 0, 1, n, n+1, GTK_FILL, GTK_FILL, 3, 3);
#if defined(HAVE_NSS)
			w = gtk_button_new_with_mnemonic(_("_View Certificate"));
			gtk_table_attach(table, w, 1, 2, n, n+1, 0, 0, 3, 3);
			g_object_set_data((GObject *)w, "e-cert-info", info);
			g_signal_connect(w, "clicked", G_CALLBACK(efhd_xpkcs7mime_viewcert_clicked), po);

			if (info->email)
				ec = e_cert_db_find_cert_by_email_address(db, info->email, NULL);
			if (ec == NULL && info->name)
				ec = e_cert_db_find_cert_by_nickname(db, info->name, NULL);

			if (ec == NULL)
				gtk_widget_set_sensitive(w, FALSE);
			else
				g_object_unref(ec);
#else
			w = gtk_label_new (_("This certificate is not viewable"));
			gtk_table_attach(table, w, 1, 2, n, n+1, 0, 0, 3, 3);
#endif
			n++;
		}

		info = info->next;
	}

	gtk_box_pack_start((GtkBox *)vbox, (GtkWidget *)table, TRUE, TRUE, 6);
}

static void
efhd_xpkcs7mime_validity_clicked (GtkWidget *button,
                                  EMFormatHTMLPObject *pobject)
{
	struct _smime_pobject *po = (struct _smime_pobject *)pobject;
	GladeXML *xml;
	GtkWidget *vbox, *w;
	char *gladefile;

	if (po->widget)
		/* FIXME: window raise? */
		return;

	gladefile = g_build_filename (
		EVOLUTION_GLADEDIR, "mail-dialogs.glade", NULL);
	xml = glade_xml_new (gladefile, "message_security_dialog", NULL);
	g_free (gladefile);

	po->widget = glade_xml_get_widget(xml, "message_security_dialog");

	vbox = glade_xml_get_widget(xml, "signature_vbox");
	w = gtk_label_new (_(smime_sign_table[po->valid->sign.status].description));
	gtk_misc_set_alignment((GtkMisc *)w, 0.0, 0.5);
	gtk_label_set_line_wrap((GtkLabel *)w, TRUE);
	gtk_box_pack_start((GtkBox *)vbox, w, TRUE, TRUE, 6);
	if (po->valid->sign.description) {
		GtkTextBuffer *buffer;

		buffer = gtk_text_buffer_new(NULL);
		gtk_text_buffer_set_text(buffer, po->valid->sign.description, strlen(po->valid->sign.description));
		w = g_object_new(gtk_scrolled_window_get_type(),
				 "hscrollbar_policy", GTK_POLICY_AUTOMATIC,
				 "vscrollbar_policy", GTK_POLICY_AUTOMATIC,
				 "shadow_type", GTK_SHADOW_IN,
				 "child", g_object_new(gtk_text_view_get_type(),
						       "buffer", buffer,
						       "cursor_visible", FALSE,
						       "editable", FALSE,
						       "width_request", 500,
						       "height_request", 160,
						       NULL),
				 NULL);
		g_object_unref(buffer);

		gtk_box_pack_start((GtkBox *)vbox, w, TRUE, TRUE, 6);
	}

	if (!camel_dlist_empty(&po->valid->sign.signers))
		efhd_xpkcs7mime_add_cert_table(vbox, &po->valid->sign.signers, po);

	gtk_widget_show_all(vbox);

	vbox = glade_xml_get_widget(xml, "encryption_vbox");
	w = gtk_label_new(_(smime_encrypt_table[po->valid->encrypt.status].description));
	gtk_misc_set_alignment((GtkMisc *)w, 0.0, 0.5);
	gtk_label_set_line_wrap((GtkLabel *)w, TRUE);
	gtk_box_pack_start((GtkBox *)vbox, w, TRUE, TRUE, 6);
	if (po->valid->encrypt.description) {
		GtkTextBuffer *buffer;

		buffer = gtk_text_buffer_new(NULL);
		gtk_text_buffer_set_text(buffer, po->valid->encrypt.description, strlen(po->valid->encrypt.description));
		w = g_object_new(gtk_scrolled_window_get_type(),
				 "hscrollbar_policy", GTK_POLICY_AUTOMATIC,
				 "vscrollbar_policy", GTK_POLICY_AUTOMATIC,
				 "shadow_type", GTK_SHADOW_IN,
				 "child", g_object_new(gtk_text_view_get_type(),
						       "buffer", buffer,
						       "cursor_visible", FALSE,
						       "editable", FALSE,
						       "width_request", 500,
						       "height_request", 160,
						       NULL),
				 NULL);
		g_object_unref(buffer);

		gtk_box_pack_start((GtkBox *)vbox, w, TRUE, TRUE, 6);
	}

	if (!camel_dlist_empty(&po->valid->encrypt.encrypters))
		efhd_xpkcs7mime_add_cert_table(vbox, &po->valid->encrypt.encrypters, po);

	gtk_widget_show_all(vbox);

	g_object_unref(xml);

	g_signal_connect(po->widget, "response", G_CALLBACK(efhd_xpkcs7mime_info_response), po);
	gtk_widget_show(po->widget);
}

static gboolean
efhd_xpkcs7mime_button (EMFormatHTML *efh,
                        GtkHTMLEmbedded *eb,
                        EMFormatHTMLPObject *pobject)
{
	GtkWidget *container;
	GtkWidget *widget;
	struct _smime_pobject *po = (struct _smime_pobject *)pobject;
	const char *icon_name;

	/* FIXME: need to have it based on encryption and signing too */
	if (po->valid->sign.status != 0)
		icon_name = smime_sign_table[po->valid->sign.status].icon;
	else
		icon_name = smime_encrypt_table[po->valid->encrypt.status].icon;

	container = GTK_WIDGET (eb);

	widget = gtk_button_new ();
	g_signal_connect (
		widget, "clicked",
		G_CALLBACK (efhd_xpkcs7mime_validity_clicked), pobject);
	gtk_container_add (GTK_CONTAINER (container), widget);
	gtk_widget_show (widget);

	container = widget;

	widget = gtk_image_new_from_icon_name (
		icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR);
	gtk_container_add (GTK_CONTAINER (container), widget);
	gtk_widget_show (widget);

	return TRUE;
}

static void
efhd_finalize (GObject *object)
{
	EMFormatHTMLDisplayPrivate *priv;

	priv = EM_FORMAT_HTML_DISPLAY_GET_PRIVATE (object);

	if (priv->files != NULL)
		g_hash_table_destroy (priv->files);

	/* Chain up to parent's finalize() method. */
	G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
efhd_format_clone (EMFormat *emf,
                   CamelFolder *folder,
                   const gchar *uid,
                   CamelMimeMessage *msg,
                   EMFormat *src)
{
	EMFormatHTMLDisplayPrivate *priv;

	priv = EM_FORMAT_HTML_DISPLAY_GET_PRIVATE (emf);

	if (emf != src) {
		priv->show_bar = (src != NULL) ?
			EM_FORMAT_HTML_DISPLAY (src)->priv->show_bar : FALSE;

		EM_FORMAT_HTML (emf)->header_wrap_flags = 0;
	}

	/* Chain up to parent's format_clone() method. */
	EM_FORMAT_CLASS (parent_class)->
		format_clone (emf, folder, uid, msg, src);
}

static void
efhd_format_attachment (EMFormat *emf,
                        CamelStream *stream,
                        CamelMimePart *part,
                        const gchar *mime_type,
                        const EMFormatHandler *handle)
{
	char *classid, *text, *html;
	struct _attach_puri *info;

	classid = g_strdup_printf ("attachment%s", emf->part_id->str);
	info = (struct _attach_puri *) em_format_add_puri (
		emf, sizeof (*info), classid, part, efhd_attachment_frame);
	em_format_html_add_pobject (
		EM_FORMAT_HTML (emf), sizeof (EMFormatHTMLPObject),
		classid, part, efhd_attachment_button);
	info->handle = handle;
	info->shown = em_format_is_inline (
		emf, info->puri.part_id, info->puri.part, handle);
	info->snoop_mime_type = emf->snoop_mime_type;
	info->attachment = e_attachment_new ();
	e_attachment_set_mime_part (info->attachment, info->puri.part);

	if (emf->valid) {
		info->sign = emf->valid->sign.status;
		info->encrypt = emf->valid->encrypt.status;
	}

	camel_stream_write_string(
		stream, EM_FORMAT_HTML_VPAD
		"<table cellspacing=0 cellpadding=0><tr><td>"
		"<table width=10 cellspacing=0 cellpadding=0>"
		"<tr><td></td></tr></table></td>");

	camel_stream_printf (
		stream, "<td><object classid=\"%s\"></object></td>", classid);

	camel_stream_write_string (
		stream, "<td><table width=3 cellspacing=0 cellpadding=0>"
		"<tr><td></td></tr></table></td><td><font size=-1>");

	/* output some info about it */
	/* FIXME: should we look up mime_type from object again? */
	text = em_format_describe_part (part, mime_type);
	html = camel_text_to_html (
		text, EM_FORMAT_HTML (emf)->text_html_flags &
		CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS, 0);
	camel_stream_write_string (stream, html);
	g_free (html);
	g_free (text);

	camel_stream_write_string (
		stream, "</font></td></tr><tr></table>\n"
		EM_FORMAT_HTML_VPAD);

	if (handle && info->shown)
		handle->handler (emf, stream, part, handle);

	g_free (classid);
}

static void
efhd_format_optional (EMFormat *emf,
                      CamelStream *fstream,
                      CamelMimePart *part,
                      CamelStream *mstream)
{
	char *classid, *html;
	struct _attach_puri *info;
	CamelStream *stream;

	if (CAMEL_IS_STREAM_FILTER (fstream) && ((CamelStreamFilter *) fstream)->source)
		stream = ((CamelStreamFilter *) fstream)->source;
	else
		stream = fstream;

	classid = g_strdup_printf ("optional%s", emf->part_id->str);
	info = (struct _attach_puri *) em_format_add_puri (
		emf, sizeof (*info), classid, part, efhd_attachment_frame);
	em_format_html_add_pobject (
		EM_FORMAT_HTML (emf), sizeof (EMFormatHTMLPObject),
		classid, part, efhd_attachment_optional);
	info->handle = em_format_find_handler (emf, "text/plain");
	info->shown = FALSE;
	info->snoop_mime_type = "text/plain";
	info->attachment = e_attachment_new ();
	e_attachment_set_mime_part (info->attachment, info->puri.part);
	info->mstream = (CamelStreamMem *) mstream;
	if (emf->valid) {
		info->sign = emf->valid->sign.status;
		info->encrypt = emf->valid->encrypt.status;
	}

	camel_stream_write_string (
		stream, EM_FORMAT_HTML_VPAD
		"<table cellspacing=0 cellpadding=0><tr><td>"
		"<h3><font size=-1 color=red>");

	html = camel_text_to_html (
		_("Evolution cannot render this email as it is too "
		  "large to process. You can view it unformatted or "
		  "with an external text editor."),
		EM_FORMAT_HTML (emf)->text_html_flags &
		CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS, 0);
	camel_stream_write_string (stream, html);
	camel_stream_write_string (
		stream, "</font></h3></td></tr></table>\n");
	camel_stream_write_string (
		stream, "<table cellspacing=0 cellpadding=0><tr>");
	camel_stream_printf (
		stream, "<td><object classid=\"%s\"></object>"
		"</td></tr></table>", classid);

	g_free(html);

	camel_stream_write_string (
		stream, EM_FORMAT_HTML_VPAD);

	g_free (classid);
}

static void
efhd_format_secure (EMFormat *emf,
                    CamelStream *stream,
                    CamelMimePart *part,
                    CamelCipherValidity *valid)
{
	EMFormatClass *format_class;

	format_class = g_type_class_peek (EM_TYPE_FORMAT);
	format_class->format_secure (emf, stream, part, valid);

	if (emf->valid == valid
	    && (valid->encrypt.status != CAMEL_CIPHER_VALIDITY_ENCRYPT_NONE
		|| valid->sign.status != CAMEL_CIPHER_VALIDITY_SIGN_NONE)) {
		char *classid;
		struct _smime_pobject *pobj;

		camel_stream_printf (
			stream, "<table border=0 width=\"100%%\" "
			"cellpadding=3 cellspacing=0%s><tr>",
			smime_sign_colour[valid->sign.status]);

		classid = g_strdup_printf (
			"smime:///em-format-html/%s/icon/signed",
			emf->part_id->str);
		pobj = (struct _smime_pobject *) em_format_html_add_pobject (
			EM_FORMAT_HTML (emf), sizeof (*pobj),
			classid, part, efhd_xpkcs7mime_button);
		pobj->valid = camel_cipher_validity_clone(valid);
		pobj->object.free = efhd_xpkcs7mime_free;
		camel_stream_printf (
			stream, "<td valign=center><object classid=\"%s\">"
			"</object></td><td width=100%% valign=center>",
			classid);
		g_free (classid);

		if (valid->sign.status != CAMEL_CIPHER_VALIDITY_SIGN_NONE) {
			const gchar *desc;
			gint status;

			status = valid->sign.status;
			desc = smime_sign_table[status].shortdesc;
			camel_stream_printf (stream, "%s", gettext (desc));
		}

		if (valid->encrypt.status != CAMEL_CIPHER_VALIDITY_ENCRYPT_NONE) {
			const gchar *desc;
			gint status;

			if (valid->sign.status != CAMEL_CIPHER_VALIDITY_SIGN_NONE) {
				camel_stream_printf (stream, "<br>");
			}

			status = valid->encrypt.status;
			desc = smime_encrypt_table[status].shortdesc;
			camel_stream_printf (stream, "%s", gettext (desc));
		}

		camel_stream_printf(stream, "</td></tr></table>");
	}
}

static void
efhd_class_init (EMFormatHTMLDisplayClass *class)
{
	GObjectClass *object_class;
	EMFormatClass *format_class;
	EMFormatHTMLClass *format_html_class;

	parent_class = g_type_class_peek_parent (class);
	g_type_class_add_private (class, sizeof (EMFormatHTMLDisplayPrivate));

	object_class = G_OBJECT_CLASS (class);
	object_class->finalize = efhd_finalize;

	format_class = EM_FORMAT_CLASS (class);
	format_class->format_clone = efhd_format_clone;
	format_class->format_attachment = efhd_format_attachment;
	format_class->format_optional = efhd_format_optional;
	format_class->format_secure = efhd_format_secure;
	format_class->complete = efhd_complete;

	format_html_class = EM_FORMAT_HTML_CLASS (class);
	format_html_class->html_widget_type = E_TYPE_MAIL_DISPLAY;

	efhd_builtin_init (class);
}

static void
efhd_init (EMFormatHTMLDisplay *efhd)
{
	GtkHTML *html;

	html = EM_FORMAT_HTML (efhd)->html;

	efhd->priv = EM_FORMAT_HTML_DISPLAY_GET_PRIVATE (efhd);

	e_mail_display_set_formatter (
		E_MAIL_DISPLAY (html), EM_FORMAT_HTML (efhd));

	/* we want to convert url's etc */
	EM_FORMAT_HTML (efhd)->text_html_flags |=
		CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS |
		CAMEL_MIME_FILTER_TOHTML_CONVERT_ADDRESSES;

	efhd->priv->show_bar = FALSE;
	efhd->priv->files = NULL;
}

GType
em_format_html_display_get_type (void)
{
	static GType type = 0;

	if (G_UNLIKELY (type == 0)) {
		static const GTypeInfo type_info = {
			sizeof (EMFormatHTMLDisplayClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) efhd_class_init,
			(GClassFinalizeFunc) NULL,
			NULL,  /* class_data */
			sizeof (EMFormatHTMLDisplay),
			0,     /* n_preallocs */
			(GInstanceInitFunc) efhd_init,
			NULL   /* value_table */
		};

		type = g_type_register_static (
			EM_TYPE_FORMAT_HTML, "EMFormatHTMLDisplay",
			&type_info, 0);
	}

	return type;
}

EMFormatHTMLDisplay *
em_format_html_display_new (void)
{
	return g_object_new (EM_TYPE_FORMAT_HTML_DISPLAY, NULL);
}

void
em_format_html_display_cut (EMFormatHTMLDisplay *efhd)
{
	gtk_html_cut (((EMFormatHTML *) efhd)->html);
}

void
em_format_html_display_copy (EMFormatHTMLDisplay *efhd)
{
	gtk_html_copy (((EMFormatHTML *) efhd)->html);
}

void
em_format_html_display_paste (EMFormatHTMLDisplay *efhd)
{
	gtk_html_paste (((EMFormatHTML *) efhd)->html, FALSE);
}

void
em_format_html_display_zoom_in (EMFormatHTMLDisplay *efhd)
{
	gtk_html_zoom_in (((EMFormatHTML *) efhd)->html);
}

void
em_format_html_display_zoom_out (EMFormatHTMLDisplay *efhd)
{
	gtk_html_zoom_out (((EMFormatHTML *) efhd)->html);
}

void
em_format_html_display_zoom_reset (EMFormatHTMLDisplay *efhd)
{
	gtk_html_zoom_reset (((EMFormatHTML *) efhd)->html);
}

/* ********************************************************************** */

static void
efhd_complete(EMFormat *emf)
{
	EMFormatHTMLDisplay *efhd = (EMFormatHTMLDisplay *)emf;

	if (efhd->priv->files) {
		g_hash_table_destroy (efhd->priv->files);
		efhd->priv->files = NULL;
	}
}

/* ********************************************************************** */

static void
efhd_image(EMFormatHTML *efh, CamelStream *stream, CamelMimePart *part, EMFormatHandler *handle)
{
	char *classid;
	struct _attach_puri *info;

	classid = g_strdup_printf("image%s", ((EMFormat *)efh)->part_id->str);
	info = (struct _attach_puri *)em_format_add_puri((EMFormat *)efh, sizeof(*info), classid, part, efhd_attachment_frame);
	em_format_html_add_pobject(efh, sizeof(EMFormatHTMLPObject), classid, part, efhd_attachment_image);

	info->handle = handle;
	info->shown = TRUE;
	info->snoop_mime_type = ((EMFormat *) efh)->snoop_mime_type;
	if (camel_operation_cancel_check (NULL) || !info->puri.format || !((EMFormatHTML *)info->puri.format)->html) {
		/* some fake value, we are cancelled anyway, thus doesn't matter */
		info->fit_width = 256;
	} else {
		info->fit_width = ((GtkWidget *)((EMFormatHTML *)info->puri.format)->html)->allocation.width - 12;
	}

	camel_stream_printf(stream, "<td><object classid=\"%s\"></object></td>", classid);
	g_free(classid);
}

/* ********************************************************************** */

static EMFormatHandler type_builtin_table[] = {
	{ "image/gif", (EMFormatFunc)efhd_image },
	{ "image/jpeg", (EMFormatFunc)efhd_image },
	{ "image/png", (EMFormatFunc)efhd_image },
	{ "image/x-png", (EMFormatFunc)efhd_image },
	{ "image/tiff", (EMFormatFunc)efhd_image },
	{ "image/x-bmp", (EMFormatFunc)efhd_image },
	{ "image/bmp", (EMFormatFunc)efhd_image },
	{ "image/svg", (EMFormatFunc)efhd_image },
	{ "image/x-cmu-raster", (EMFormatFunc)efhd_image },
	{ "image/x-ico", (EMFormatFunc)efhd_image },
	{ "image/x-portable-anymap", (EMFormatFunc)efhd_image },
	{ "image/x-portable-bitmap", (EMFormatFunc)efhd_image },
	{ "image/x-portable-graymap", (EMFormatFunc)efhd_image },
	{ "image/x-portable-pixmap", (EMFormatFunc)efhd_image },
	{ "image/x-xpixmap", (EMFormatFunc)efhd_image },

	/* This is where one adds those busted, non-registered types,
	   that some idiot mailer writers out there decide to pull out
	   of their proverbials at random. */

	{ "image/jpg", (EMFormatFunc)efhd_image },
	{ "image/pjpeg", (EMFormatFunc)efhd_image },

	{ "x-evolution/message/prefix", (EMFormatFunc)efhd_message_prefix },
	{ "x-evolution/message/post-header", (EMFormatFunc)efhd_message_add_bar },
	{ "x-evolution/message/post-header-closure", (EMFormatFunc)efhd_message_update_bar },

};

static void
efhd_builtin_init(EMFormatHTMLDisplayClass *efhc)
{
	int i;

	for (i=0;i<sizeof(type_builtin_table)/sizeof(type_builtin_table[0]);i++)
		em_format_class_add_handler((EMFormatClass *)efhc, &type_builtin_table[i]);
}

static void
efhd_write_image(EMFormat *emf, CamelStream *stream, EMFormatPURI *puri)
{
	CamelDataWrapper *dw = camel_medium_get_content_object((CamelMedium *)puri->part);

	/* TODO: identical to efh_write_image */
	d(printf("writing image '%s'\n", puri->cid));
	camel_data_wrapper_decode_to_stream(dw, stream);
	camel_stream_close(stream);
}

static void
efhd_message_prefix(EMFormat *emf, CamelStream *stream, CamelMimePart *part, EMFormatHandler *info)
{
	const char *flag, *comp, *due;
	time_t date;
	char due_date[128];
	struct tm due_tm;
	char *iconpath;

	if (emf->folder == NULL || emf->uid == NULL
	    || (flag = camel_folder_get_message_user_tag(emf->folder, emf->uid, "follow-up")) == NULL
	    || flag[0] == 0)
		return;

	/* header displayed for message-flags in mail display */
	camel_stream_printf(stream, "<table border=1 width=\"100%%\" cellspacing=2 cellpadding=2><tr>");

	comp = camel_folder_get_message_user_tag(emf->folder, emf->uid, "completed-on");
	iconpath = e_icon_factory_get_icon_filename (comp && comp[0] ? "stock_flag-for-followup-done" : "stock_flag-for-followup", E_ICON_SIZE_MENU);
	if (iconpath) {
		CamelMimePart *iconpart;

		iconpart = em_format_html_file_part((EMFormatHTML *)emf, "image/png", iconpath);
		g_free (iconpath);
		if (iconpart) {
			char *classid;

			classid = g_strdup_printf("icon:///em-format-html-display/%s/%s", emf->part_id->str, comp&&comp[0]?"comp":"uncomp");
			camel_stream_printf(stream, "<td align=\"left\"><img src=\"%s\"></td>", classid);
			(void)em_format_add_puri(emf, sizeof(EMFormatPURI), classid, iconpart, efhd_write_image);
			g_free(classid);
			camel_object_unref(iconpart);
		}
	}

	camel_stream_printf(stream, "<td align=\"left\" width=\"100%%\">");

	if (comp && comp[0]) {
		date = camel_header_decode_date(comp, NULL);
		localtime_r(&date, &due_tm);
		e_utf8_strftime_fix_am_pm(due_date, sizeof (due_date), _("Completed on %B %d, %Y, %l:%M %p"), &due_tm);
		camel_stream_printf(stream, "%s, %s", flag, due_date);
	} else if ((due = camel_folder_get_message_user_tag(emf->folder, emf->uid, "due-by")) != NULL && due[0]) {
		time_t now;

		date = camel_header_decode_date(due, NULL);
		now = time(NULL);
		if (now > date)
			camel_stream_printf(stream, "<b>%s</b>&nbsp;", _("Overdue:"));

		localtime_r(&date, &due_tm);
		e_utf8_strftime_fix_am_pm(due_date, sizeof (due_date), _("by %B %d, %Y, %l:%M %p"), &due_tm);
		camel_stream_printf(stream, "%s %s", flag, due_date);
	} else {
		camel_stream_printf(stream, "%s", flag);
	}

	camel_stream_printf(stream, "</td></tr></table>");
}

/* ********************************************************************** */

/* Checks on the widget whether it can be processed, based on the state of EMFormatHTML.
   The widget should have set "efh" data as the EMFormatHTML instance. */
static gboolean
efhd_can_process_attachment (GtkWidget *button)
{
	EMFormatHTML *efh;

	if (!button)
		return FALSE;

	efh = g_object_get_data (G_OBJECT (button), "efh");

	return efh && efh->state != EM_FORMAT_HTML_STATE_RENDERING;
}

/* if it hasn't been processed yet, format the attachment */
static void
efhd_attachment_show(EPopup *ep, EPopupItem *item, void *data)
{
	struct _attach_puri *info = data;

	d(printf("show attachment button called %p\n", info));

	info->shown = ~info->shown;
	em_format_set_inline(info->puri.format, info->puri.part_id, info->shown);
}

static void
efhd_attachment_button_show(GtkWidget *w, void *data)
{
	if (!efhd_can_process_attachment (w))
		return;

	efhd_attachment_show(NULL, NULL, data);
}

static void
efhd_image_fit(EPopup *ep, EPopupItem *item, void *data)
{
	struct _attach_puri *info = data;

	info->fit_width = ((GtkWidget *)((EMFormatHTML *)info->puri.format)->html)->allocation.width - 12;
	gtk_image_set_from_pixbuf(info->image, em_icon_stream_get_image(info->puri.cid, info->fit_width, info->fit_height));
}

static void
efhd_image_unfit(EPopup *ep, EPopupItem *item, void *data)
{
	struct _attach_puri *info = data;

	info->fit_width = 0;
	gtk_image_set_from_pixbuf((GtkImage *)info->image, em_icon_stream_get_image(info->puri.cid, info->fit_width, info->fit_height));
}

static EPopupItem efhd_menu_items[] = {
	{ E_POPUP_BAR, "05.display", },
	{ E_POPUP_ITEM, "05.display.00", N_("_View Inline"), efhd_attachment_show },
	{ E_POPUP_ITEM, "05.display.00", N_("_Hide"), efhd_attachment_show },
	{ E_POPUP_ITEM, "05.display.01", N_("_Fit to Width"), efhd_image_fit, NULL, NULL, EM_POPUP_PART_IMAGE },
	{ E_POPUP_ITEM, "05.display.01", N_("Show _Original Size"), efhd_image_unfit, NULL, NULL, EM_POPUP_PART_IMAGE },
};

static void
efhd_menu_items_free(EPopup *ep, GSList *items, void *data)
{
	g_slist_free(items);
}

static void
efhd_popup_place_widget(GtkMenu *menu, int *x, int *y, gboolean *push_in, gpointer user_data)
{
	GtkWidget *w = user_data;

	gdk_window_get_origin(gtk_widget_get_parent_window(w), x, y);
	*x += w->allocation.x + w->allocation.width;
	*y += w->allocation.y;
}

static gboolean
efhd_attachment_popup(GtkWidget *w, GdkEventButton *event, struct _attach_puri *info)
{
	GtkMenu *menu;
	GSList *menus = NULL;
	EMPopup *emp;
	EMPopupTargetPart *target;

	d(printf("attachment popup, button %d\n", event->button));

	if (event && event->button != 1 && event->button != 3) {
		/* ?? gtk_propagate_event(GTK_WIDGET (user_data), (GdkEvent *)event);*/
		return FALSE;
	}

	if (!efhd_can_process_attachment (w))
		return FALSE;

	/** @HookPoint-EMPopup: Attachment Button Context Menu
	 * @Id: org.gnome.evolution.mail.formathtmldisplay.popup
	 * @Class: org.gnome.evolution.mail.popup:1.0
	 * @Target: EMPopupTargetPart
	 *
	 * This is the drop-down menu shown when a user clicks on the down arrow
	 * of the attachment button in inline mail content.
	 */
	emp = em_popup_new("org.gnome.evolution.mail.formathtmldisplay.popup");
	target = em_popup_target_new_part(emp, info->puri.part, info->handle?info->handle->mime_type:NULL);
	target->target.widget = w;

	/* add our local menus */
	if (info->handle) {
		/* show/hide menus, only if we have an inline handler */
		menus = g_slist_prepend(menus, &efhd_menu_items[0]);
		menus = g_slist_prepend(menus, &efhd_menu_items[info->shown?2:1]);
		if (info->shown && info->image) {
			if (info->fit_width != 0) {
				if (em_icon_stream_is_resized(info->puri.cid, info->fit_width, info->fit_height))
				    menus = g_slist_prepend(menus, &efhd_menu_items[4]);
			} else
				menus = g_slist_prepend(menus, &efhd_menu_items[3]);
		}
	}

	e_popup_add_items((EPopup *)emp, menus, NULL, efhd_menu_items_free, info);

	menu = e_popup_create_menu_once((EPopup *)emp, (EPopupTarget *)target, 0);
	if (event)
		gtk_menu_popup(menu, NULL, NULL, NULL, NULL, event->button, event->time);
	else
		gtk_menu_popup(menu, NULL, NULL, (GtkMenuPositionFunc)efhd_popup_place_widget, w, 0, gtk_get_current_event_time());

	return TRUE;
}

static gboolean
efhd_image_popup(GtkWidget *w, GdkEventButton *event, struct _attach_puri *info)
{
	if (event && event->button != 3)
		return FALSE;

	return efhd_attachment_popup(w, event, info);
}

static gboolean
efhd_attachment_popup_menu(GtkWidget *w, struct _attach_puri *info)
{
	return efhd_attachment_popup(w, NULL, info);
}

/* ********************************************************************** */

static void
efhd_drag_data_get(GtkWidget *w, GdkDragContext *drag, GtkSelectionData *data, guint info, guint time, EMFormatHTMLPObject *pobject)
{
	CamelMimePart *part = pobject->part;
	char *uri, *uri_crlf, *path;
	CamelStream *stream;

	switch (info) {
	case 0: /* mime/type request */
		stream = camel_stream_mem_new();
		/* TODO: shoudl format_format_text run on the content-object? */
		/* TODO: should we just do format_content? */
		if (camel_content_type_is (((CamelDataWrapper *)part)->mime_type, "text", "*")) {
			/* FIXME: this should be an em_utils method, it only needs a default charset param */
			em_format_format_text((EMFormat *)pobject->format, stream, (CamelDataWrapper *)part);
		} else {
			CamelDataWrapper *dw = camel_medium_get_content_object((CamelMedium *)part);

			camel_data_wrapper_decode_to_stream(dw, stream);
		}

		gtk_selection_data_set(data, data->target, 8,
				       ((CamelStreamMem *)stream)->buffer->data,
				       ((CamelStreamMem *)stream)->buffer->len);
		camel_object_unref(stream);
		break;
	case 1: /* text-uri-list request */
		/* Kludge around Nautilus requesting the same data many times */
		uri = g_object_get_data((GObject *)w, "e-drag-uri");
		if (uri) {
			gtk_selection_data_set(data, data->target, 8, (unsigned char *)uri, strlen(uri));
			return;
		}

		path = em_utils_temp_save_part(w, part, FALSE);
		if (path == NULL)
			return;

		uri = g_filename_to_uri(path, NULL, NULL);
		g_free(path);
		uri_crlf = g_strconcat(uri, "\r\n", NULL);
		g_free(uri);
		gtk_selection_data_set(data, data->target, 8, (unsigned char *)uri_crlf, strlen(uri_crlf));
		g_object_set_data_full((GObject *)w, "e-drag-uri", uri_crlf, g_free);
		break;
	default:
		abort();
	}
}

static void
efhd_drag_data_delete(GtkWidget *w, GdkDragContext *drag, EMFormatHTMLPObject *pobject)
{
	char *uri;

	uri = g_object_get_data((GObject *)w, "e-drag-uri");
	if (uri) {
		/* NB: this doesn't kill the dnd directory */
		/* NB: is this ever called? */
		/* NB even more: doesn't the e-drag-uri have \r\n
		 * appended? (see efhd_drag_data_get())
		 */
		char *filename = g_filename_from_uri (uri, NULL, NULL);
		g_unlink(filename);
		g_free(filename);
		g_object_set_data((GObject *)w, "e-drag-uri", NULL);
	}
}

static void
efhd_write_icon_job(struct _EMFormatHTMLJob *job, int cancelled)
{
	EMFormatHTMLPObject *pobject;
	CamelDataWrapper *dw;

	if (cancelled)
		return;

	pobject = job->u.data;
	dw = camel_medium_get_content_object((CamelMedium *)pobject->part);
	camel_data_wrapper_decode_to_stream(dw, job->stream);
	camel_stream_close(job->stream);
}

static void
efhd_image_resized(GtkWidget *w, GtkAllocation *event, struct _attach_puri *info)
{
	GdkPixbuf *pb;
	int width;

	if (info->fit_width == 0)
		return;

	width = ((GtkWidget *)((EMFormatHTML *)info->puri.format)->html)->allocation.width - 12;
	if (info->fit_width == width)
		return;
	info->fit_width = width;

	pb = em_icon_stream_get_image(info->puri.cid, info->fit_width, info->fit_height);
	if (pb) {
		gtk_image_set_from_pixbuf(info->image, pb);
		g_object_unref(pb);
	}
}

static void
efhd_change_cursor(GtkWidget *w, GdkEventCrossing *event, struct _attach_puri *info)
{
	if (info->shown && info->image) {
		if (info->fit_width != 0) {
			if (em_icon_stream_is_resized(info->puri.cid, info->fit_width, info->fit_height))
			    	e_cursor_set(w->window, E_CURSOR_ZOOM_IN);

		}
	}
}

static void
efhd_image_fit_width(GtkWidget *widget, GdkEventButton *event, struct _attach_puri *info)
{
	int width;

	width = ((GtkWidget *)((EMFormatHTML *)info->puri.format)->html)->allocation.width - 12;

	if (info->shown && info->image) {
		if (info->fit_width != 0) {
			if (em_icon_stream_is_resized(info->puri.cid, info->fit_width, info->fit_height)) {
				if(info->fit_width != width) {
					info->fit_width = width;
					e_cursor_set (widget->window, E_CURSOR_ZOOM_IN);
				} else {
					info->fit_width = 0;
					e_cursor_set(widget->window, E_CURSOR_ZOOM_OUT);
				}
			}
		} else {
			info->fit_width = width;
			e_cursor_set (widget->window, E_CURSOR_ZOOM_IN);
		}
	}

	gtk_image_set_from_pixbuf(info->image, em_icon_stream_get_image(info->puri.cid, info->fit_width, info->fit_height));
}

/* When the puri gets freed in the formatter thread and if the image is resized, crash will happen
   See bug #333864 So while freeing the puri, we disconnect the image attach resize attached with
   the puri */

static void
efhd_image_unallocate (struct _EMFormatPURI * puri)
{
	struct _attach_puri *info = (struct _attach_puri *) puri;
	g_signal_handlers_disconnect_by_func(info->html, efhd_image_resized, info);

	g_signal_handlers_disconnect_by_func(info->event_box, efhd_image_popup, info);
	g_signal_handlers_disconnect_by_func(info->event_box, efhd_change_cursor, info);
	g_signal_handlers_disconnect_by_func(info->event_box, efhd_attachment_popup_menu, info);
	g_signal_handlers_disconnect_by_func(info->event_box, efhd_image_fit_width, info);
}

static gboolean
efhd_attachment_image(EMFormatHTML *efh, GtkHTMLEmbedded *eb, EMFormatHTMLPObject *pobject)
{
	GtkWidget *box;
	EMFormatHTMLJob *job;
	struct _attach_puri *info;
	GdkPixbuf *pixbuf;
	GtkTargetEntry drag_types[] = {
		{ NULL, 0, 0 },
		{ "text/uri-list", 0, 1 },
	};
	char *simple_type;

	info = (struct _attach_puri *)em_format_find_puri((EMFormat *)efh, pobject->classid);

	info->image = (GtkImage *)gtk_image_new();
	info->html = eb;
	info->puri.free = efhd_image_unallocate;

	pixbuf = em_icon_stream_get_image(pobject->classid, info->fit_width, info->fit_height);
	if (pixbuf) {
		gtk_image_set_from_pixbuf(info->image, pixbuf);
		g_object_unref(pixbuf);
	} else {
		job = em_format_html_job_new(efh, efhd_write_icon_job, pobject);
		job->stream = (CamelStream *)em_icon_stream_new((GtkImage *)info->image, pobject->classid, info->fit_width, info->fit_height, TRUE);
		em_format_html_job_queue(efh, job);
	}

	box = gtk_event_box_new();
	info->event_box = box;
	gtk_container_add((GtkContainer *)box, (GtkWidget *)info->image);
	gtk_widget_show_all(box);
	gtk_container_add((GtkContainer *)eb, box);

	g_signal_connect(eb, "size_allocate", G_CALLBACK(efhd_image_resized), info);

	simple_type = camel_content_type_simple(((CamelDataWrapper *)pobject->part)->mime_type);
	camel_strdown(simple_type);

	drag_types[0].target = simple_type;
	gtk_drag_source_set(box, GDK_BUTTON1_MASK, drag_types, sizeof(drag_types)/sizeof(drag_types[0]), GDK_ACTION_COPY);
	g_free(simple_type);

	g_signal_connect(box, "drag-data-get", G_CALLBACK(efhd_drag_data_get), pobject);
	g_signal_connect (box, "drag-data-delete", G_CALLBACK(efhd_drag_data_delete), pobject);

	g_signal_connect(box, "button_press_event", G_CALLBACK(efhd_image_popup), info);
	g_signal_connect(box, "enter-notify-event", G_CALLBACK(efhd_change_cursor), info);
	g_signal_connect(box, "popup_menu", G_CALLBACK(efhd_attachment_popup_menu), info);
	g_signal_connect(box, "button-press-event", G_CALLBACK(efhd_image_fit_width), info);

	g_object_set_data (G_OBJECT (box), "efh", efh);

	return TRUE;
}

/* attachment button callback */
static gboolean
efhd_attachment_button(EMFormatHTML *efh, GtkHTMLEmbedded *eb, EMFormatHTMLPObject *pobject)
{
#if 0  /* KILL-BONOBO  !! FIXME !! */
	EMFormatHTMLDisplay *efhd = (EMFormatHTMLDisplay *)efh;
	EAttachment *new;
	struct _attach_puri *info;
	GtkWidget *hbox, *w, *button, *mainbox;
	char *simple_type, *tmp, *new_file = NULL;
	const char *file;
	GtkTargetEntry drag_types[] = {
		{ NULL, 0, 0 },
		{ "text/uri-list", 0, 1 },
	};
	AtkObject *a11y;

	/* FIXME: handle default shown case */
	d(printf("adding attachment button/content\n"));

	info = (struct _attach_puri *)em_format_find_puri((EMFormat *)efh, pobject->classid);

	if (!info || info->forward) {
		g_warning ("unable to expand the attachment\n");
		return TRUE;
	}

	if (efhd->priv->attachment_view) {
		EAttachmentView *view;
		EAttachmentStore *store;

		file = camel_mime_part_get_filename(info->puri.part);

		new = info->attachment;

		if (!file) {
			file = "attachment.dat";
			e_attachment_set_filename (new, file);
		}

		tmp = g_hash_table_lookup (efhd->priv->files, file);
		if (tmp) {
			guint count = GPOINTER_TO_UINT(tmp);
			char *ext;
			char *tmp_file = g_strdup (file);

			if ((ext = strrchr(tmp_file, '.'))) {
				ext[0] = 0;
				new_file = g_strdup_printf("%s(%d).%s", tmp_file, count++, ext+1);
			} else {
				new_file = g_strdup_printf("%s(%d)", tmp_file, count++);
			}

			g_free (tmp_file);
			g_hash_table_insert (efhd->priv->files, g_strdup(file), GUINT_TO_POINTER(count));
			e_attachment_set_filename (new, new_file);
			g_free (new_file);
		} else {
			g_hash_table_insert (efhd->priv->files, g_strdup(file), GUINT_TO_POINTER(1));
		}

		/* Store the status of encryption / signature on the attachment for emblem display
		 * FIXME: May not work well always
		 */
		new->sign = info->sign;
		new->encrypt = info->encrypt;

		/* Add the attachment to the bar.*/
		e_attachment_bar_add_attachment_silent (E_ATTACHMENT_BAR(efhd->priv->attachment_bar), new);
		efhd_attachment_bar_refresh(efhd);
	}

	mainbox = gtk_hbox_new(FALSE, 0);

	button = gtk_button_new();

	if (info->handle) {
		g_signal_connect(button, "clicked", G_CALLBACK(efhd_attachment_button_show), info);
		g_object_set_data (G_OBJECT (button), "efh", efh);
	} else {
		gtk_widget_set_sensitive(button, FALSE);
		GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
	}

	hbox = gtk_hbox_new(FALSE, 2);
	info->forward = gtk_image_new_from_stock(GTK_STOCK_GO_FORWARD, GTK_ICON_SIZE_BUTTON);
	gtk_box_pack_start((GtkBox *)hbox, info->forward, TRUE, TRUE, 0);
	if (info->handle) {
		info->down = gtk_image_new_from_stock(GTK_STOCK_GO_DOWN, GTK_ICON_SIZE_BUTTON);
		gtk_box_pack_start((GtkBox *)hbox, info->down, TRUE, TRUE, 0);
	}

	w = gtk_image_new();
	gtk_widget_set_size_request(w, 24, 24);
	gtk_box_pack_start((GtkBox *)hbox, w, TRUE, TRUE, 0);
	gtk_container_add((GtkContainer *)button, hbox);
	gtk_box_pack_start((GtkBox *)mainbox, button, TRUE, TRUE, 0);

	/* Check for snooped type to get the right icon/processing */
	if (info->snoop_mime_type)
		simple_type = g_strdup(info->snoop_mime_type);
	else
		simple_type = camel_content_type_simple (((CamelDataWrapper *)pobject->part)->mime_type);
	camel_strdown(simple_type);

	/* FIXME: offline parts, just get icon */
	if (camel_content_type_is(((CamelDataWrapper *)pobject->part)->mime_type, "image", "*")) {
		EMFormatHTMLJob *job;
		GdkPixbuf *mini;
		char *key;

		key = pobject->classid;
		mini = em_icon_stream_get_image(key, 24, 24);
		if (mini) {
			d(printf("got image from cache '%s'\n", key));
			gtk_image_set_from_pixbuf((GtkImage *)w, mini);
			g_object_unref(mini);
		} else {
			d(printf("need to create icon image '%s'\n", key));
			job = em_format_html_job_new(efh, efhd_write_icon_job, pobject);
			job->stream = (CamelStream *)em_icon_stream_new((GtkImage *)w, key, 24, 24, FALSE);
			em_format_html_job_queue(efh, job);
		}
	} else {
		GdkPixbuf *pixbuf, *mini;

		if ((pixbuf = e_icon_for_mime_type (simple_type, 24))) {
			if ((mini = e_icon_factory_pixbuf_scale (pixbuf, 24, 24))) {
				gtk_image_set_from_pixbuf ((GtkImage *) w, mini);
				g_object_unref (mini);
			}
			g_object_unref (pixbuf);
		}
	}

	drag_types[0].target = simple_type;
	gtk_drag_source_set(button, GDK_BUTTON1_MASK, drag_types, sizeof(drag_types)/sizeof(drag_types[0]), GDK_ACTION_COPY);
	g_signal_connect(button, "drag-data-get", G_CALLBACK(efhd_drag_data_get), pobject);
	g_signal_connect (button, "drag-data-delete", G_CALLBACK(efhd_drag_data_delete), pobject);
	g_free(simple_type);

	button = gtk_button_new();
	/*GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);*/
	gtk_container_add((GtkContainer *)button, gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE));

	a11y = gtk_widget_get_accessible (button);
	atk_object_set_name (a11y, _("Attachment"));

	g_signal_connect(button, "button_press_event", G_CALLBACK(efhd_attachment_popup), info);
	g_signal_connect(button, "popup_menu", G_CALLBACK(efhd_attachment_popup_menu), info);
	g_signal_connect(button, "clicked", G_CALLBACK(efhd_attachment_popup_menu), info);
	gtk_box_pack_start((GtkBox *)mainbox, button, TRUE, TRUE, 0);

	g_object_set_data (G_OBJECT (button), "efh", efh);

	gtk_widget_show_all(mainbox);

	if (info->shown)
		gtk_widget_hide(info->forward);
	else if (info->down)
		gtk_widget_hide(info->down);

	gtk_container_add((GtkContainer *)eb, mainbox);
#endif

	return TRUE;
}

/* not used currently */
/* frame source callback */
static void
efhd_attachment_frame(EMFormat *emf, CamelStream *stream, EMFormatPURI *puri)
{
	struct _attach_puri *info = (struct _attach_puri *)puri;

	if (info->shown) {
		d(printf("writing to frame content, handler is '%s'\n", info->handle->mime_type));
		info->handle->handler(emf, stream, info->puri.part, info->handle);
		camel_stream_close(stream);
	} else {
		/* FIXME: this is leaked if the object is closed without showing it
		   NB: need a virtual puri_free method? */
		info->output = stream;
		camel_object_ref(stream);
	}
}

static void
attachment_bar_arrow_clicked(GtkWidget *w, EMFormatHTMLDisplay *efhd)
{

	efhd->priv->show_bar = !efhd->priv->show_bar;

	if (efhd->priv->show_bar) {
		gtk_widget_show(efhd->priv->attachment_box);
		gtk_widget_show(efhd->priv->down);
		gtk_widget_hide(efhd->priv->forward);
	} else {
		gtk_widget_hide(efhd->priv->attachment_box);
		gtk_widget_show(efhd->priv->forward);
		gtk_widget_hide(efhd->priv->down);
	}
}

static void
attachments_save_all_clicked (GtkWidget *widget, EMFormatHTMLDisplay *efhd)
{
#if 0  /* KILL-BONOBO */
	GSList *attachment_parts;
	guint n_attachment_parts;
	gpointer parent;

	attachment_parts = e_attachment_bar_get_parts (
		E_ATTACHMENT_BAR (efhd->priv->attachment_bar));
	n_attachment_parts = g_slist_length (attachment_parts);
	g_return_if_fail (n_attachment_parts > 0);

	parent = gtk_widget_get_toplevel (widget);
	parent = GTK_WIDGET_TOPLEVEL (parent) ? parent : NULL;

	if (n_attachment_parts == 1)
		em_utils_save_part (
			parent, _("Save attachment as"),
			attachment_parts->data);
	else
		em_utils_save_parts (
			parent, _("Select folder to save all attachments"),
			attachment_parts);

        g_slist_free (attachment_parts);
#endif
}

#if 0  /* KILL-BONOBO -- Move this to EAttachmentView */
static void
efhd_bar_save_selected(EPopup *ep, EPopupItem *item, void *data)
{
	EMFormatHTMLDisplay *efhd = (EMFormatHTMLDisplay *)data;
	GSList *attachment_parts, *tmp;
	GSList *parts = NULL;
	GtkWidget *widget;
	gpointer parent;

	widget = efhd->priv->attachment_bar;
	parent = gtk_widget_get_toplevel (widget);
	parent = GTK_WIDGET_TOPLEVEL (parent) ? parent : NULL;

	attachment_parts = e_attachment_bar_get_selected(E_ATTACHMENT_BAR(widget));

	for (tmp = attachment_parts; tmp; tmp=tmp->next) {
		EAttachment *attachment = tmp->data;
		CamelMimePart *mime_part;

		mime_part = e_attachment_get_mime_part (attachment);
		parts = g_slist_prepend (parts, mime_part);
	}

	parts = g_slist_reverse(parts);
	em_utils_save_parts(parent, _("Select folder to save selected attachments..."), parts);
        g_slist_free (parts);

	g_slist_foreach(attachment_parts, (GFunc)g_object_unref, NULL);
	g_slist_free (attachment_parts);
}

static EPopupItem efhd_bar_menu_items[] = {
	{ E_POPUP_BAR, "05.display", },
	{ E_POPUP_ITEM, "05.display.01", N_("_Save Selected..."), efhd_bar_save_selected, NULL, NULL, EM_POPUP_ATTACHMENTS_MULTIPLE},
};
#endif

static void
efhd_attachment_bar_refresh (EMFormatHTMLDisplay *efhd)
{
	EAttachmentStore *store;
	EAttachmentView *view;
	guint nattachments;

	if (!efhd->priv->attachment_view)
		return;

	view = E_ATTACHMENT_VIEW (efhd->priv->attachment_view);
	store = e_attachment_view_get_store (view);

	nattachments = e_attachment_store_get_num_attachments (store);

	if (nattachments > 0) {
		char *txt;

		/* Cant i put in the number of attachments here ?*/
		txt = g_strdup_printf(ngettext("%d at_tachment", "%d at_tachments", nattachments), nattachments);
		gtk_label_set_text_with_mnemonic ((GtkLabel *)efhd->priv->label, txt);
		g_free (txt);

		/* Show the bar even when the first attachment is added */
		if (nattachments == 1) {
			gtk_widget_show_all (efhd->priv->attachment_area);
			gtk_label_set_text_with_mnemonic ((GtkLabel *)efhd->priv->save_txt, _("S_ave"));

			if (efhd->priv->show_bar) {
				gtk_widget_show(efhd->priv->down);
				gtk_widget_hide(efhd->priv->forward);
			} else {
				gtk_widget_show(efhd->priv->forward);
				gtk_widget_hide(efhd->priv->down);
				gtk_widget_hide(efhd->priv->attachment_box);
			}
		} else if (nattachments > 1) {
			gtk_label_set_text_with_mnemonic ((GtkLabel *)efhd->priv->save_txt, _("S_ave All"));
		}
	}
}

static void
efhd_bar_resize(GtkWidget *w, GtkAllocation *event, EMFormatHTML *efh)
{
#if 0 /* KILL-BONOBO -- Does EAttachmentIconView need a resize method? */
	int width;
	GtkRequisition req;
	EMFormatHTMLDisplay *efhd = (EMFormatHTMLDisplay *) efh;

	gtk_widget_size_request (efhd->priv->attachment_bar, &req);
	width = ((GtkWidget *) efh->html)->allocation.width - 16;

	/* Update the width of the bar when the width is greater than 1*/
	if (width > 0)
		e_attachment_bar_set_width(E_ATTACHMENT_BAR(efhd->priv->attachment_bar), width);
#endif
}

static gboolean
efhd_bar_scroll_event(GtkWidget *w, GdkEventScroll *event, EMFormatHTMLDisplay *efhd)
{
#if 0  /* KILL-BONOBO -- Do we still need this for a GtkIconView? */
	gboolean ret;

	/* Emulate the scroll over the attachment bar, as if it is scrolled in the window.
	*  It doesnt go automatically since the GnomeIconList is a layout by itself
	*/
	g_signal_emit_by_name (gtk_widget_get_parent((GtkWidget *)efhd->parent.html), "scroll_event", event, &ret);
#endif

	return TRUE;
}

gboolean
efhd_mnemonic_show_bar (GtkWidget *widget, gboolean focus, GtkWidget *efhd)
{
	attachment_bar_arrow_clicked (NULL, (EMFormatHTMLDisplay *)efhd);

	return TRUE;
}

static gboolean
efhd_update_bar(EMFormatHTML *efh, GtkHTMLEmbedded *eb, EMFormatHTMLPObject *pobject)
{
#if 0  /* KILL-BONOBO -- Does EAttachmentIconView need a refresh method? */
	EMFormatHTMLDisplay *efhd = (EMFormatHTMLDisplay *)efh;
	struct _EMFormatHTMLDisplayPrivate *priv = efhd->priv;

	if (priv->attachment_bar)
		e_attachment_bar_refresh (E_ATTACHMENT_BAR (priv->attachment_bar));
#endif

	return TRUE;
}

static gboolean
efhd_add_bar(EMFormatHTML *efh, GtkHTMLEmbedded *eb, EMFormatHTMLPObject *pobject)
{
	EMFormatHTMLDisplay *efhd = (EMFormatHTMLDisplay *)efh;
	struct _EMFormatHTMLDisplayPrivate *priv = efhd->priv;
	GtkWidget *hbox1, *hbox2, *hbox3, *vbox, *txt, *image, *save, *scroll;
	int width, height, bar_width;

	priv->attachment_view = e_attachment_icon_view_new ();
	scroll = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);

	priv->forward = gtk_arrow_new(GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
	priv->down = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
	hbox3 = gtk_hbox_new (FALSE, 0);
	gtk_box_pack_start ((GtkBox *)hbox3, priv->forward, FALSE, FALSE, 0);
	gtk_box_pack_start ((GtkBox *)hbox3, priv->down, FALSE, FALSE, 0);
	priv->arrow = (GtkWidget *)gtk_tool_button_new(hbox3, NULL);
	g_signal_connect (priv->arrow, "mnemonic_activate", G_CALLBACK (efhd_mnemonic_show_bar), efh);
	atk_object_set_name (gtk_widget_get_accessible (priv->arrow), _("Show Attachments"));

	priv->label = gtk_label_new(_("No Attachment"));
	gtk_label_set_mnemonic_widget (GTK_LABEL (priv->label), priv->arrow);
	save = gtk_button_new();
	image = gtk_image_new_from_stock ("gtk-save", GTK_ICON_SIZE_BUTTON);
	txt = gtk_label_new_with_mnemonic(_("S_ave"));
	priv->save_txt = txt;
	hbox1 = gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start((GtkBox *)hbox1, image, FALSE, FALSE, 2);
	gtk_box_pack_start((GtkBox *)hbox1, txt, FALSE, FALSE, 0);

	gtk_container_add((GtkContainer *)save, hbox1);

	hbox2 = gtk_hbox_new (FALSE, 0);
	gtk_box_pack_start ((GtkBox *)hbox2, priv->arrow, FALSE, FALSE, 0);
	gtk_box_pack_start ((GtkBox *)hbox2, priv->label, FALSE, FALSE, 2);
	gtk_box_pack_start ((GtkBox *)hbox2, save, FALSE, FALSE, 2);

	priv->attachment_box = scroll;
	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll), GTK_SHADOW_IN);
	gtk_container_add ((GtkContainer *)priv->attachment_box, priv->attachment_view);

	gtk_widget_get_size_request(priv->attachment_view, &width, &height);

	/* FIXME: What if the text is more?. Should we reduce the text with appending ...?
	 * or resize the bar? How to figure out that, it needs more space? */
	bar_width = ((GtkWidget *)efh->html)->parent->allocation.width - /* FIXME */16;
	gtk_widget_set_size_request (priv->attachment_view,
				     bar_width > 0 ? bar_width : 0,
				     84 /* FIXME: Default show only one row, Dont hardcode size*/);

	vbox = gtk_vbox_new (FALSE, 0);
	gtk_box_pack_start ((GtkBox *)vbox, hbox2, FALSE, FALSE, 2);
	gtk_box_pack_start ((GtkBox *)vbox, priv->attachment_box, TRUE, TRUE, 2);

	gtk_container_add ((GtkContainer *)eb, vbox);
	gtk_widget_show ((GtkWidget *)eb);

	/* Lets hide it by default and show only when there are attachments */
	priv->attachment_area = vbox;
	gtk_widget_hide_all (priv->attachment_area);

	g_signal_connect (priv->arrow, "clicked", G_CALLBACK(attachment_bar_arrow_clicked), efh);
	g_signal_connect (save, "clicked", G_CALLBACK(attachments_save_all_clicked), efh);
	g_signal_connect (eb, "size_allocate", G_CALLBACK (efhd_bar_resize), efh);

	return TRUE;
}
static void
efhd_message_update_bar(EMFormat *emf, CamelStream *stream, CamelMimePart *part, const EMFormatHandler *info)
{
	EMFormatHTMLDisplay *efhd = (EMFormatHTMLDisplay *) emf;
	const char *classid = "attachment-bar-refresh";

	if (efhd->priv->updated)
		return;

	efhd->priv->files = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
	efhd->priv->updated = TRUE;
	em_format_html_add_pobject((EMFormatHTML *)emf, sizeof(EMFormatHTMLPObject), classid, part, efhd_update_bar);
	camel_stream_printf(stream, "<td><object classid=\"%s\"></object></td>", classid);

}

static void
efhd_message_add_bar(EMFormat *emf, CamelStream *stream, CamelMimePart *part, const EMFormatHandler *info)
{
	EMFormatHTMLDisplay *efhd = (EMFormatHTMLDisplay *) emf;
	const char *classid = "attachment-bar";

	if (efhd->priv->files)
		return;

	efhd->priv->files = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
	efhd->priv->updated = FALSE;

	em_format_html_add_pobject((EMFormatHTML *)emf, sizeof(EMFormatHTMLPObject), classid, part, efhd_add_bar);
	camel_stream_printf(stream, "<td><object classid=\"%s\"></object></td>", classid);
}

static void
efhd_optional_button_show (GtkWidget *widget, GtkWidget *w)
{
	GtkWidget *label = g_object_get_data (G_OBJECT (widget), "text-label");

	if (GTK_WIDGET_VISIBLE (w)) {
		gtk_widget_hide (w);
		gtk_label_set_text_with_mnemonic (GTK_LABEL (label), _("View _Unformatted"));
	} else {
		gtk_label_set_text_with_mnemonic (GTK_LABEL (label), _("Hide _Unformatted"));
		gtk_widget_show (w);
	}
}

static void
efhd_resize (GtkWidget *w, GtkAllocation *event, EMFormatHTML *efh)
{
	gtk_widget_set_size_request (w, ((GtkWidget *)efh->html)->allocation.width-48, 250);
}

/* optional render attachment button callback */
static gboolean
efhd_attachment_optional(EMFormatHTML *efh, GtkHTMLEmbedded *eb, EMFormatHTMLPObject *pobject)
{
	struct _attach_puri *info;
	GtkWidget *hbox, *vbox, *button, *mainbox, *scroll, *label, *img;
	AtkObject *a11y;
	GtkWidget *view;
	GtkTextBuffer *buffer;

	/* FIXME: handle default shown case */
	d(printf("adding attachment button/content for optional rendering\n"));

	info = (struct _attach_puri *)em_format_find_puri((EMFormat *)efh, pobject->classid);
	if (!info || info->forward) {
		g_warning ("unable to expand the attachment\n");
		return TRUE;
	}

	scroll = gtk_scrolled_window_new (NULL, NULL);
	mainbox = gtk_hbox_new(FALSE, 0);

	button = gtk_button_new();
	hbox = gtk_hbox_new (FALSE, 0);
	img = gtk_image_new_from_icon_name (
		"stock_show-all", GTK_ICON_SIZE_BUTTON);
	label = gtk_label_new_with_mnemonic(_("View _Unformatted"));
	g_object_set_data (G_OBJECT (button), "text-label", (gpointer)label);
	gtk_box_pack_start (GTK_BOX (hbox), img, TRUE, TRUE, 2);
	gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 2);
	gtk_widget_show_all (hbox);
	gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (hbox));
	if (info->handle)
		g_signal_connect(G_OBJECT (button), "clicked", G_CALLBACK(efhd_optional_button_show), scroll);
	else {
		gtk_widget_set_sensitive(button, FALSE);
		GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
	}

	vbox = gtk_vbox_new (FALSE, 0);
	gtk_box_pack_start(GTK_BOX (mainbox), button, FALSE, FALSE, 6);

	button = gtk_button_new();
	hbox = gtk_hbox_new (FALSE, 0);
	img = gtk_image_new_from_stock (
		GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
	label = gtk_label_new_with_mnemonic(_("O_pen With"));
	gtk_box_pack_start (GTK_BOX (hbox), img, TRUE, TRUE, 2);
	gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 2);
	gtk_box_pack_start (GTK_BOX (hbox), gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE), TRUE, TRUE, 2);
	gtk_widget_show_all (hbox);
	gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (hbox));

	a11y = gtk_widget_get_accessible (button);
	atk_object_set_name (a11y, _("Attachment"));

	g_signal_connect(button, "button_press_event", G_CALLBACK(efhd_attachment_popup), info);
	g_signal_connect(button, "popup_menu", G_CALLBACK(efhd_attachment_popup_menu), info);
	g_signal_connect(button, "clicked", G_CALLBACK(efhd_attachment_popup_menu), info);
	gtk_box_pack_start(GTK_BOX (mainbox), button, FALSE, FALSE, 6);

	gtk_widget_show_all(mainbox);

	gtk_box_pack_start(GTK_BOX (vbox), mainbox, FALSE, FALSE, 6);

	view = gtk_text_view_new ();
	gtk_text_view_set_editable (GTK_TEXT_VIEW (view), FALSE);
	gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (view), FALSE);
	buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (view));
	gtk_text_buffer_set_text (buffer, (char *)info->mstream->buffer->data, info->mstream->buffer->len);
	camel_object_unref(info->mstream);
	info->mstream = NULL;
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
					GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll), GTK_SHADOW_IN);
	gtk_container_add (GTK_CONTAINER (scroll), GTK_WIDGET (view));
	gtk_box_pack_start(GTK_BOX (vbox), scroll, TRUE, TRUE, 6);
	gtk_widget_show (GTK_WIDGET(view));

	gtk_widget_set_size_request (scroll, (GTK_WIDGET (efh->html))->allocation.width - 48, 250);
	g_signal_connect (scroll, "size_allocate", G_CALLBACK(efhd_resize), efh);
	gtk_widget_show (scroll);

	if (!info->shown)
		gtk_widget_hide (scroll);

	gtk_widget_show (vbox);
	gtk_container_add(GTK_CONTAINER (eb), vbox);
	info->handle = NULL;

	return TRUE;
}