+static gchar *get_signed_protocol (TnyMimePart *part)
+{
+ TnyList *header_pairs;
+ TnyIterator *iterator;
+ gchar *result = NULL;
+
+ header_pairs = TNY_LIST (tny_simple_list_new ());
+ tny_mime_part_get_header_pairs (part, header_pairs);
+ iterator = tny_list_create_iterator (header_pairs);
+
+ while (!result && !tny_iterator_is_done (iterator)) {
+ TnyPair *pair;
+ const gchar *name;
+
+ pair = TNY_PAIR (tny_iterator_get_current (iterator));
+ name = tny_pair_get_name (pair);
+ if (name && !g_ascii_strcasecmp (name, "Content-Type")) {
+ const gchar *s;
+ s = tny_pair_get_value (pair);
+ if (s) {
+ s = strstr (s, "protocol=");
+ if (s) {
+ const gchar *t;
+ s += 9;
+ if (*s == '\"') {
+ s++;
+ t = strstr (s, "\"");
+ } else {
+ t = strstr (s, ";");
+ }
+ result = g_strndup (s, t - s);
+ }
+ }
+ }
+
+ g_object_unref (pair);
+ tny_iterator_next (iterator);
+ }
+
+ g_object_unref (iterator);
+ g_object_unref (header_pairs);
+
+ return result;
+}
+
+TnyMimePart *
+modest_tny_msg_get_attachments_parent (TnyMsg *msg)
+{
+ TnyMimePart *result;
+ const gchar *content_type;
+
+ result = NULL;
+
+ content_type = tny_mime_part_get_content_type (TNY_MIME_PART (msg));
+ if (content_type && !strcmp (content_type, "multipart/signed")) {
+ TnyList *msg_children;
+ TnyIterator *iterator;
+ gchar *signed_protocol;
+
+ msg_children = TNY_LIST (tny_simple_list_new ());
+ tny_mime_part_get_parts (TNY_MIME_PART (msg), msg_children);
+
+ iterator = tny_list_create_iterator (msg_children);
+ signed_protocol = get_signed_protocol (TNY_MIME_PART (msg));
+
+ while (!result && !tny_iterator_is_done (iterator)) {
+ TnyMimePart *part;
+
+ part = TNY_MIME_PART (tny_iterator_get_current (iterator));
+ if (signed_protocol) {
+ const gchar *part_content_type;
+
+ part_content_type = tny_mime_part_get_content_type (part);
+ if (part_content_type && g_ascii_strcasecmp (part_content_type, signed_protocol)) {
+ result = g_object_ref (part);
+ }
+ } else {
+ result = g_object_ref (part);
+ }
+
+ g_object_unref (part);
+ tny_iterator_next (iterator);
+ }
+
+ g_object_unref (iterator);
+ g_free (signed_protocol);
+ g_object_unref (msg_children);
+ }
+ if (result == NULL) {
+ result = g_object_ref (msg);
+ }
+
+ return result;
+}
+