Blame gio/gio-tool-trash.c

Packit ae235b
/*
Packit ae235b
 * Copyright 2015 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Matthias Clasen <mclasen@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <gio/gio.h>
Packit ae235b
#include <gi18n.h>
Packit ae235b
Packit ae235b
#include "gio-tool.h"
Packit ae235b
Packit ae235b
Packit ae235b
static gboolean force = FALSE;
Packit ae235b
static gboolean empty = FALSE;
Packit ae235b
static const GOptionEntry entries[] = {
Packit ae235b
  { "force", 'f', 0, G_OPTION_ARG_NONE, &force, N_("Ignore nonexistent files, never prompt"), NULL },
Packit ae235b
  { "empty", 0, 0, G_OPTION_ARG_NONE, &empty, N_("Empty the trash"), NULL },
Packit ae235b
  { NULL }
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
delete_trash_file (GFile *file, gboolean del_file, gboolean del_children)
Packit ae235b
{
Packit ae235b
  GFileInfo *info;
Packit ae235b
  GFile *child;
Packit ae235b
  GFileEnumerator *enumerator;
Packit ae235b
Packit ae235b
  if (del_children)
Packit ae235b
    {
Packit ae235b
      enumerator = g_file_enumerate_children (file,
Packit ae235b
                                              G_FILE_ATTRIBUTE_STANDARD_NAME ","
Packit ae235b
                                              G_FILE_ATTRIBUTE_STANDARD_TYPE,
Packit ae235b
                                              G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
Packit ae235b
                                              NULL,
Packit ae235b
                                              NULL);
Packit ae235b
      if (enumerator)
Packit ae235b
        {
Packit ae235b
          while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL)
Packit ae235b
            {
Packit ae235b
              child = g_file_get_child (file, g_file_info_get_name (info));
Packit ae235b
              delete_trash_file (child, TRUE, g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
Packit ae235b
              g_object_unref (child);
Packit ae235b
              g_object_unref (info);
Packit ae235b
            }
Packit ae235b
          g_file_enumerator_close (enumerator, NULL, NULL);
Packit ae235b
          g_object_unref (enumerator);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (del_file)
Packit ae235b
    g_file_delete (file, NULL, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
handle_trash (int argc, char *argv[], gboolean do_help)
Packit ae235b
{
Packit ae235b
  GOptionContext *context;
Packit ae235b
  gchar *param;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  int retval = 0;
Packit ae235b
  GFile *file;
Packit ae235b
Packit ae235b
  g_set_prgname ("gio trash");
Packit ae235b
Packit ae235b
  /* Translators: commandline placeholder */
Packit ae235b
  param = g_strdup_printf ("[%s...]", _("LOCATION"));
Packit ae235b
  context = g_option_context_new (param);
Packit ae235b
  g_free (param);
Packit ae235b
  g_option_context_set_help_enabled (context, FALSE);
Packit ae235b
  g_option_context_set_summary (context,
Packit ae235b
      _("Move files or directories to the trash."));
Packit ae235b
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
Packit ae235b
Packit ae235b
  if (do_help)
Packit ae235b
    {
Packit ae235b
      show_help (context, NULL);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_option_context_parse (context, &argc, &argv, &error))
Packit ae235b
    {
Packit ae235b
      show_help (context, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_option_context_free (context);
Packit ae235b
Packit ae235b
  if (argc > 1)
Packit ae235b
    {
Packit ae235b
      int i;
Packit ae235b
Packit ae235b
      for (i = 1; i < argc; i++)
Packit ae235b
        {
Packit ae235b
          file = g_file_new_for_commandline_arg (argv[i]);
Packit ae235b
          error = NULL;
Packit ae235b
          if (!g_file_trash (file, NULL, &error))
Packit ae235b
            {
Packit ae235b
              if (!force ||
Packit ae235b
                  !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
Packit ae235b
                {
Packit ae235b
                  print_file_error (file, error->message);
Packit ae235b
                  retval = 1;
Packit ae235b
                }
Packit ae235b
              g_error_free (error);
Packit ae235b
            }
Packit ae235b
          g_object_unref (file);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (empty)
Packit ae235b
    {
Packit ae235b
      GFile *file;
Packit ae235b
      file = g_file_new_for_uri ("trash:");
Packit ae235b
      delete_trash_file (file, FALSE, TRUE);
Packit ae235b
      g_object_unref (file);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}