+
+static gchar *
+construct_virtual_filename(const gchar *filename, const gint position, const gchar *id, const gboolean active)
+{
+ GString *s;
+ g_return_val_if_fail((position >= 0), "AttachmentInvalid");
+
+ s = g_string_new("");
+ if (active)
+ g_string_append(s, "Attachment:");
+ else
+ g_string_append(s, "attachment:");
+ g_string_append_printf(s, "%d:", position);
+ if (id)
+ g_string_append(s, id);
+ g_string_append_c(s, ':');
+ if (filename)
+ g_string_append(s, filename);
+ 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;
+
+ i1 = filename;
+ i2 = filename;
+
+ /* check prefix */
+ i2 = get_next_token(i2, &len);
+ if (strncmp(i1, "Attachment", len) != 0)
+ return -1;
+ 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;
+}
+
+