Add some more cases to the app-id unit tests
[glib.git] / gio / gio-tool-trash.c
blob40f378c8fae4e60c238329abd25d954f07141aa2
1 /*
2 * Copyright 2015 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Matthias Clasen <mclasen@redhat.com>
20 #include "config.h"
22 #include <gio/gio.h>
23 #include <gi18n.h>
25 #include "gio-tool.h"
28 static gboolean force = FALSE;
29 static gboolean empty = FALSE;
30 static const GOptionEntry entries[] = {
31 { "force", 'f', 0, G_OPTION_ARG_NONE, &force, N_("Ignore nonexistent files, never prompt"), NULL },
32 { "empty", 0, 0, G_OPTION_ARG_NONE, &empty, N_("Empty the trash"), NULL },
33 { NULL }
36 static void
37 delete_trash_file (GFile *file, gboolean del_file, gboolean del_children)
39 GFileInfo *info;
40 GFile *child;
41 GFileEnumerator *enumerator;
43 if (del_children)
45 enumerator = g_file_enumerate_children (file,
46 G_FILE_ATTRIBUTE_STANDARD_NAME ","
47 G_FILE_ATTRIBUTE_STANDARD_TYPE,
48 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
49 NULL,
50 NULL);
51 if (enumerator)
53 while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL)
55 child = g_file_get_child (file, g_file_info_get_name (info));
56 delete_trash_file (child, TRUE, g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
57 g_object_unref (child);
58 g_object_unref (info);
60 g_file_enumerator_close (enumerator, NULL, NULL);
61 g_object_unref (enumerator);
65 if (del_file)
66 g_file_delete (file, NULL, NULL);
69 int
70 handle_trash (int argc, char *argv[], gboolean do_help)
72 GOptionContext *context;
73 gchar *param;
74 GError *error = NULL;
75 int retval = 0;
76 GFile *file;
78 g_set_prgname ("gio trash");
80 /* Translators: commandline placeholder */
81 param = g_strdup_printf ("[%s...]", _("LOCATION"));
82 context = g_option_context_new (param);
83 g_free (param);
84 g_option_context_set_help_enabled (context, FALSE);
85 g_option_context_set_summary (context,
86 _("Move files or directories to the trash."));
87 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
89 if (do_help)
91 show_help (context, NULL);
92 return 0;
95 if (!g_option_context_parse (context, &argc, &argv, &error))
97 show_help (context, error->message);
98 g_error_free (error);
99 return 1;
102 g_option_context_free (context);
104 if (argc > 1)
106 int i;
108 for (i = 1; i < argc; i++)
110 file = g_file_new_for_commandline_arg (argv[i]);
111 error = NULL;
112 if (!g_file_trash (file, NULL, &error))
114 if (!force ||
115 !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
117 print_file_error (file, error->message);
118 retval = 1;
120 g_error_free (error);
122 g_object_unref (file);
126 if (empty)
128 GFile *file;
129 file = g_file_new_for_uri ("trash:");
130 delete_trash_file (file, FALSE, TRUE);
131 g_object_unref (file);
134 return retval;