autoar_extractor_step_decide_destination (AutoarExtractor *self)
{
  /* Step 2: Decide destination */

  GList *files = NULL;
  GList *l;
  GFile *new_destination = NULL;
  g_autofree char *destination_name;

  for (l = self->files_list; l != NULL; l = l->next) {
    char *relative_path;
    GFile *file;

    relative_path = g_file_get_relative_path (self->output_file, l->data);
    file = g_file_resolve_relative_path (self->destination_dir,
                                         relative_path);
    files = g_list_prepend (files, file);

    g_free (relative_path);
  }

  files = g_list_reverse (files);

  /* When it exists, the common prefix is the actual output of the extraction
   * and the client has the opportunity to change it. Also, the old prefix is
   * needed in order to replace it with the new one
   */
  if (self->prefix != NULL) {
    autoar_extractor_signal_decide_destination (self,
                                                self->prefix,
                                                files,
                                                &new_destination);

    self->new_prefix = new_destination;
  } else {
    autoar_extractor_signal_decide_destination (self,
                                                self->destination_dir,
                                                files,
                                                &new_destination);

    if (new_destination) {
      g_object_unref (self->destination_dir);
      self->destination_dir = new_destination;
    }
  }

  new_destination = (self->new_prefix != NULL) ? self->new_prefix : self->destination_dir;
  destination_name = g_file_get_path (new_destination);
  g_debug ("autoar_extractor_step_decide_destination: destination %s", destination_name);

  g_file_make_directory_with_parents (new_destination,
                                      self->cancellable,
                                      &(self->error));

  if (g_error_matches (self->error, G_IO_ERROR, G_IO_ERROR_EXISTS)) {
    GFileType file_type;

    file_type = g_file_query_file_type (new_destination,
                                        G_FILE_QUERY_INFO_NONE,
                                        NULL);

    if (file_type == G_FILE_TYPE_DIRECTORY) {
      /* FIXME: Implement a way to solve directory conflicts */
      g_debug ("autoar_extractor_step_decide_destination: destination directory exists");
      g_clear_error (&self->error);
    }
  }

  g_list_free_full (files, g_object_unref);
}
autoar_extractor_step_decide_destination (AutoarExtractor *self)
{
   

  GList *files = NULL;
  GList *l;
  GFile *new_destination = NULL;
  g_autofree char *destination_name;

  for (l = self->files_list; l != NULL; l = l->next) {
    char *relative_path;
    GFile *file;

    relative_path = g_file_get_relative_path (self->output_file, l->data);
    file = g_file_resolve_relative_path (self->destination_dir,
                                         relative_path);
    files = g_list_prepend (files, file);

    g_free (relative_path);
  }

  files = g_list_reverse (files);

   
  if (self->prefix != NULL) {
    autoar_extractor_signal_decide_destination (self,
                                                self->prefix,
                                                files,
                                                &new_destination);

    self->new_prefix = new_destination;
  } else {
    autoar_extractor_signal_decide_destination (self,
                                                self->destination_dir,
                                                files,
                                                &new_destination);

    if (new_destination) {
      g_object_unref (self->destination_dir);
      self->destination_dir = new_destination;
    }
  }

  new_destination = (self->new_prefix != NULL) ? self->new_prefix : self->destination_dir;
  destination_name = g_file_get_path (new_destination);
  g_debug ("autoar_extractor_step_decide_destination: destination %s", destination_name);

  g_file_make_directory_with_parents (new_destination,
                                      self->cancellable,
                                      &(self->error));

  if (g_error_matches (self->error, G_IO_ERROR, G_IO_ERROR_EXISTS)) {
    GFileType file_type;

    file_type = g_file_query_file_type (new_destination,
                                        G_FILE_QUERY_INFO_NONE,
                                        NULL);

    if (file_type == G_FILE_TYPE_DIRECTORY) {
       
      g_debug ("autoar_extractor_step_decide_destination: destination directory exists");
      g_clear_error (&self->error);
    }
  }

  g_list_free_full (files, g_object_unref);
}