+static gchar *
+secure_filename(const gchar *fn)
+{
+ gchar *tmp, *p;
+ GString *s;
+
+ s = g_string_new("");
+#if 1 || DEBUG
+ tmp = g_strdup(fn);
+ for (p = tmp; p[0] ; p++ ) {
+ p[0] &= 0x5f; /* 01011111 */
+ p[0] |= 0x40; /* 01000000 */
+ }
+ g_string_printf(s, "0x%x:%s", g_str_hash(fn), tmp);
+ g_free(tmp);
+ return g_string_free(s, FALSE);
+#else
+ g_string_printf(s, "0x%x", g_str_hash(fn));
+ return g_string_free(s, FALSE);
+#endif
+}
+
+
+static gchar *
+construct_virtual_filename(const gchar *filename,
+ const gint position,
+ const gchar *id,
+ const gboolean active)
+{
+ GString *s;
+ gchar *fn;
+
+ if (position < 0)
+ return g_strdup("AttachmentInvalid");
+
+ s = g_string_new("");
+ if (active)
+ g_string_append(s, ATTACHMENT_ID_INLINE);
+ else
+ g_string_append(s, ATTACHMENT_ID_LINK);
+ g_string_append_printf(s, ":%d:", position);
+ if (id)
+ g_string_append(s, id);
+ g_string_append_c(s, ':');
+
+ fn = secure_filename(filename);
+ if (fn)
+ g_string_append(s, fn);
+ g_free(fn);
+ g_string_append_c(s, ':');
+ return g_string_free(s, FALSE);
+}
+
+
+static gchar *
+construct_virtual_filename_from_mime_part(TnyMsgMimePartIface *msg, const gint position)
+{
+ const gchar *id, *filename;
+ const gboolean active = TRUE;
+
+ filename = tny_msg_mime_part_iface_get_filename(
+ TNY_MSG_MIME_PART_IFACE(msg));
+ if (!filename)
+ filename = "[unknown]";
+ id = tny_msg_mime_part_iface_get_content_id(
+ TNY_MSG_MIME_PART_IFACE(msg));
+
+ return construct_virtual_filename(filename, position, id, active);
+}
+
+const gchar *
+get_next_token(const gchar *s, gint *len)
+{
+ gchar *i1, *i2;
+ i1 = (char *) s;
+ i2 = (char *) s;
+
+ while (i2[0]) {
+ if (i2[0] == ':')
+ break;
+ i2++;
+ }
+ if (!i2[0])
+ return NULL;
+ *len = i2 - i1;
+ return ++i2;
+}
+
+/* maybe I should use libregexp */
+gint
+virtual_filename_get_pos(const gchar *filename)
+{
+ const gchar *i1, *i2;
+ gint len, pos;
+ GString *dummy;
+
+ /* check prefix */
+ if ((!g_str_has_prefix(filename, ATTACHMENT_ID_INLINE ":")) &&
+ (!g_str_has_prefix(filename, ATTACHMENT_ID_LINK ":")))
+ return -1;
+
+ i2 = filename;
+ i2 = get_next_token(i2, &len);
+ i1 = i2;
+
+ /* get position */
+ i2 = get_next_token(i2, &len);
+ if (i2 == NULL)
+ return -1;
+ dummy = g_string_new_len(i1, len);
+ pos = atoi(dummy->str);
+ g_string_free(dummy, FALSE);
+ return pos;
+}
+
+
+static gchar *
+attachments_as_html(ModestTnyMsgView *self, TnyMsgIface *msg)
+{
+ ModestTnyMsgViewPrivate *priv;
+ gboolean attachments_found = FALSE;
+ GString *appendix;
+ const GList *attachment_list, *attachment;
+ const gchar *content_type, *filename, *id;
+ gchar *virtual_filename;
+
+ if (!msg)
+ return g_malloc0(1);
+
+ priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (self);
+
+ appendix = g_string_new("");
+ g_string_printf(appendix, "<HTML><BODY>\n<hr><h5>%s:</h5>\n", _("Attachments"));
+
+ attachment_list = tny_msg_iface_get_parts(msg);
+ attachment = attachment_list;
+ while (attachment) {
+ filename = "";
+ content_type = tny_msg_mime_part_iface_get_content_type(
+ TNY_MSG_MIME_PART_IFACE(attachment->data));
+ if (!content_type)
+ continue;
+
+ if ((strcmp("image/jpeg", content_type) == 0) ||
+ (strcmp("image/gif", content_type) == 0)) {
+ filename = tny_msg_mime_part_iface_get_filename(
+ TNY_MSG_MIME_PART_IFACE(attachment->data));
+ if (!filename)
+ filename = "[unknown]";
+ else
+ attachments_found = TRUE;
+ id = tny_msg_mime_part_iface_get_content_id(
+ TNY_MSG_MIME_PART_IFACE(attachment->data));
+ if (modest_tny_msg_view_get_show_attachments_inline_flag(self)) {
+ virtual_filename = construct_virtual_filename(filename,
+ g_list_position((GList *)attachment_list, (GList *) attachment),
+ id, TRUE);
+ g_string_append_printf(appendix, "<IMG src=\"%s\">\n<BR>", virtual_filename);
+ g_free(virtual_filename);
+ }
+ virtual_filename = construct_virtual_filename(filename,
+ g_list_position((GList *)attachment_list, (GList *) attachment),
+ id, FALSE);
+ g_string_append_printf(appendix,
+ "<A href=\"%s\">%s</A>: %s<BR>\n",
+ virtual_filename, filename, content_type);
+ g_free(virtual_filename);
+ }
+ attachment = attachment->next;
+ }
+ g_string_append(appendix, "</BODY></HTML>");
+ if (!attachments_found)
+ g_string_assign(appendix, "");
+ return g_string_free(appendix, FALSE);
+}
+