+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 *