+static TnyMimePart*
+modest_tny_msg_find_body_part_in_alternative (TnyMimePart *msg, gboolean want_html)
+{
+ TnyList *parts;
+ TnyIterator *iter;
+ TnyMimePart *part = NULL;
+ TnyMimePart *first_part = NULL;
+ const gchar *desired_mime_type = want_html ? "text/html" : "text/plain";
+
+ parts = TNY_LIST (tny_simple_list_new());
+ tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
+
+ for (iter = tny_list_create_iterator(parts);
+ !tny_iterator_is_done (iter);
+ tny_iterator_next (iter)) {
+ gchar *content_type;
+ gboolean is_body;
+
+ part = TNY_MIME_PART (tny_iterator_get_current (iter));
+
+ if (first_part == NULL) {
+ g_object_ref (part);
+ first_part = part;
+ }
+
+ is_body = FALSE;
+ content_type = g_ascii_strdown (tny_mime_part_get_content_type (part), -1);
+ is_body = g_str_has_prefix (content_type, desired_mime_type);
+ if (is_body)
+ break;
+
+ g_object_unref (part);
+ part = NULL;
+
+ }
+ g_object_unref (iter);
+ g_object_unref (parts);
+
+ if (part == NULL) {
+ return first_part;
+ } else {
+ if (first_part) g_object_unref (first_part);
+ return part;
+ }
+}
+