+static gboolean
+is_parent_of (TnyFolder *a, TnyFolder *b)
+{
+ const gchar *a_id;
+ gboolean retval = FALSE;
+
+ a_id = tny_folder_get_id (a);
+ if (a_id) {
+ gchar *string_to_match;
+ const gchar *b_id;
+
+ string_to_match = g_strconcat (a_id, "/", NULL);
+ b_id = tny_folder_get_id (b);
+ retval = g_str_has_prefix (b_id, string_to_match);
+ g_free (string_to_match);
+ }
+
+ return retval;
+}
+
+typedef struct _ForeachFolderInfo {
+ gchar *needle;
+ gboolean found;
+} ForeachFolderInfo;
+
+static gboolean
+foreach_folder_with_id (GtkTreeModel *model,
+ GtkTreePath *path,
+ GtkTreeIter *iter,
+ gpointer data)
+{
+ ForeachFolderInfo *info;
+ GObject *instance;
+
+ info = (ForeachFolderInfo *) data;
+ gtk_tree_model_get (model, iter,
+ INSTANCE_COLUMN, &instance,
+ -1);
+
+ if (TNY_IS_FOLDER (instance)) {
+ const gchar *id;
+ gchar *collate;
+ id = tny_folder_get_id (TNY_FOLDER (instance));
+ if (id) {
+ collate = g_utf8_collate_key (id, -1);
+ info->found = !strcmp (info->needle, collate);
+ g_free (collate);
+ }
+ }
+
+ if (instance)
+ g_object_unref (instance);
+
+ return info->found;
+
+}
+
+
+static gboolean
+has_folder_with_id (ModestFolderView *self, const gchar *id)
+{
+ GtkTreeModel *model;
+ ForeachFolderInfo info = {NULL, FALSE};
+
+ model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
+ info.needle = g_utf8_collate_key (id, -1);
+
+ gtk_tree_model_foreach (model, foreach_folder_with_id, &info);
+ g_free (info.needle);
+
+ return info.found;
+}
+
+static gboolean
+has_child_with_name_of (ModestFolderView *self, TnyFolder *a, TnyFolder *b)
+{
+ const gchar *a_id;
+ gboolean retval = FALSE;
+
+ a_id = tny_folder_get_id (a);
+ if (a_id) {
+ const gchar *b_id;
+ b_id = tny_folder_get_id (b);
+
+ if (b_id) {
+ const gchar *last_bar;
+ gchar *string_to_match;
+ last_bar = g_strrstr (b_id, "/");
+ if (last_bar)
+ last_bar++;
+ else
+ last_bar = b_id;
+ string_to_match = g_strconcat (a_id, "/", last_bar, NULL);
+ retval = has_folder_with_id (self, string_to_match);
+ g_free (string_to_match);
+ }
+ }
+
+ return retval;
+}
+
+static gboolean
+check_move_to_this_folder_valid (ModestFolderView *self, TnyFolder *folder)
+{
+ ModestFolderViewPrivate *priv;
+ TnyIterator *iterator;
+ gboolean retval = TRUE;
+
+ g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (self), FALSE);
+ priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
+
+ for (iterator = tny_list_create_iterator (priv->list_to_move);
+ retval && !tny_iterator_is_done (iterator);
+ tny_iterator_next (iterator)) {
+ GObject *instance;
+ instance = tny_iterator_get_current (iterator);
+ if (instance == (GObject *) folder) {
+ retval = FALSE;
+ } else if (TNY_IS_FOLDER (instance)) {
+ retval = !is_parent_of (TNY_FOLDER (instance), folder);
+ if (retval) {
+ retval = !has_child_with_name_of (self, folder, TNY_FOLDER (instance));
+ }
+ }
+ g_object_unref (instance);
+ }
+ g_object_unref (iterator);
+
+ return retval;
+}
+
+