1/*
2 * Copyright 2015 Red Hat, Inc.
3 *
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.1 of the License, or (at your option) any later version.
8 *
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.
13 *
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/>.
16 *
17 * Author: Matthias Clasen <mclasen@redhat.com>
18 */
19
20#include "config.h"
21
22#include <gio/gio.h>
23#include <gi18n.h>
24
25#include "gio-tool.h"
26
27
28static const GOptionEntry entries[] = {
29 { NULL }
30};
31
32int
33handle_rename (int argc, char *argv[], gboolean do_help)
34{
35 GOptionContext *context;
36 GError *error = NULL;
37 GFile *file;
38 GFile *new_file;
39 int retval = 0;
40 gchar *param;
41
42 g_set_prgname (prgname: "gio rename");
43
44 /* Translators: commandline placeholder */
45 param = g_strdup_printf (format: "%s %s", _("LOCATION"), _("NAME"));
46 context = g_option_context_new (parameter_string: param);
47 g_free (mem: param);
48 g_option_context_set_help_enabled (context, FALSE);
49
50 g_option_context_set_summary (context, _("Rename a file."));
51 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
52
53 if (do_help)
54 {
55 show_help (context, NULL);
56 g_option_context_free (context);
57 return 0;
58 }
59
60 if (!g_option_context_parse (context, argc: &argc, argv: &argv, error: &error))
61 {
62 show_help (context, message: error->message);
63 g_error_free (error);
64 g_option_context_free (context);
65 return 1;
66 }
67
68 if (argc < 3)
69 {
70 show_help (context, _("Missing argument"));
71 g_option_context_free (context);
72 return 1;
73 }
74 if (argc > 3)
75 {
76 show_help (context, _("Too many arguments"));
77 g_option_context_free (context);
78 return 1;
79 }
80
81 g_option_context_free (context);
82
83 file = g_file_new_for_commandline_arg (arg: argv[1]);
84 new_file = g_file_set_display_name (file, display_name: argv[2], NULL, error: &error);
85
86 if (new_file == NULL)
87 {
88 print_error (format: "%s", error->message);
89 g_error_free (error);
90 retval = 1;
91 }
92 else
93 {
94 char *uri = g_file_get_uri (file: new_file);
95 g_print (_("Rename successful. New uri: %s\n"), uri);
96 g_object_unref (object: new_file);
97 g_free (mem: uri);
98 }
99
100 g_object_unref (object: file);
101
102 return retval;
103}
104

source code of gtk/subprojects/glib/gio/gio-tool-rename.c