1 /*
2 * Copyright 2015 Red Hat, Inc.
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Matthias Clasen <mclasen@redhat.com>
20 */
21
22 #include "config.h"
23
24 #include <gio/gio.h>
25 #include <gi18n.h>
26
27 #include "gio-tool.h"
28
29
30 static const GOptionEntry entries[] = {
31 G_OPTION_ENTRY_NULL
32 };
33
34 int
35 handle_rename (int argc, char *argv[], gboolean do_help)
36 {
37 GOptionContext *context;
38 GError *error = NULL;
39 GFile *file;
40 GFile *new_file;
41 int retval = 0;
42 gchar *param;
43
44 g_set_prgname ("gio rename");
45
46 /* Translators: commandline placeholder */
47 param = g_strdup_printf ("%s %s", _("LOCATION"), _("NAME"));
48 context = g_option_context_new (param);
49 g_free (param);
50 g_option_context_set_help_enabled (context, FALSE);
51
52 g_option_context_set_summary (context, _("Rename a file."));
53 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
54
55 if (do_help)
56 {
57 show_help (context, NULL);
58 g_option_context_free (context);
59 return 0;
60 }
61
62 if (!g_option_context_parse (context, &argc, &argv, &error))
63 {
64 show_help (context, error->message);
65 g_error_free (error);
66 g_option_context_free (context);
67 return 1;
68 }
69
70 if (argc < 3)
71 {
72 show_help (context, _("Missing argument"));
73 g_option_context_free (context);
74 return 1;
75 }
76 if (argc > 3)
77 {
78 show_help (context, _("Too many arguments"));
79 g_option_context_free (context);
80 return 1;
81 }
82
83 g_option_context_free (context);
84
85 file = g_file_new_for_commandline_arg (argv[1]);
86 new_file = g_file_set_display_name (file, argv[2], NULL, &error);
87
88 if (new_file == NULL)
89 {
90 print_error ("%s", error->message);
91 g_error_free (error);
92 retval = 1;
93 }
94 else
95 {
96 char *uri = g_file_get_uri (new_file);
97 g_print (_("Rename successful. New uri: %s\n"), uri);
98 g_object_unref (new_file);
99 g_free (uri);
100 }
101
102 g_object_unref (file);
103
104 return retval;
105 }