+static GList*
+find_prev_or_next_not_purged (GList *list, gboolean prev, gboolean include_this)
+{
+ GList *tmp = NULL;
+ gboolean is_valid;
+
+ if (!include_this) {
+ if (prev) {
+ tmp = g_list_previous (list);
+ } else {
+ tmp = g_list_next (list);
+ }
+ } else {
+ tmp = list;
+ }
+
+ if (!tmp)
+ return NULL;
+
+ do {
+ ModestAttachmentView *att_view = (ModestAttachmentView *) tmp->data;
+ TnyMimePart *mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
+
+ /* Do not select purged attachments */
+ if (TNY_IS_MIME_PART (mime_part) && !tny_mime_part_is_purged (mime_part)) {
+ is_valid = TRUE;
+ } else {
+ if (prev)
+ tmp = g_list_previous (tmp);
+ else
+ tmp = g_list_next (tmp);
+ is_valid = FALSE;
+ }
+ g_object_unref (mime_part);
+ } while (!is_valid && tmp);
+
+ return tmp;
+}
+
+