Branch data Line data Source code
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 : 0 : handle_rename (int argc, char *argv[], gboolean do_help) 36 : : { 37 : : GOptionContext *context; 38 : 0 : GError *error = NULL; 39 : : GFile *file; 40 : : GFile *new_file; 41 : 0 : int retval = 0; 42 : : gchar *param; 43 : : 44 : 0 : g_set_prgname ("gio rename"); 45 : : 46 : : /* Translators: commandline placeholder */ 47 : 0 : param = g_strdup_printf ("%s %s", _("LOCATION"), _("NAME")); 48 : 0 : context = g_option_context_new (param); 49 : 0 : g_free (param); 50 : 0 : g_option_context_set_help_enabled (context, FALSE); 51 : : 52 : 0 : g_option_context_set_summary (context, _("Rename a file.")); 53 : 0 : g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); 54 : : 55 [ # # ]: 0 : if (do_help) 56 : : { 57 : 0 : show_help (context, NULL); 58 : 0 : g_option_context_free (context); 59 : 0 : return 0; 60 : : } 61 : : 62 [ # # ]: 0 : if (!g_option_context_parse (context, &argc, &argv, &error)) 63 : : { 64 : 0 : show_help (context, error->message); 65 : 0 : g_error_free (error); 66 : 0 : g_option_context_free (context); 67 : 0 : return 1; 68 : : } 69 : : 70 [ # # ]: 0 : if (argc < 3) 71 : : { 72 : 0 : show_help (context, _("Missing argument")); 73 : 0 : g_option_context_free (context); 74 : 0 : return 1; 75 : : } 76 [ # # ]: 0 : if (argc > 3) 77 : : { 78 : 0 : show_help (context, _("Too many arguments")); 79 : 0 : g_option_context_free (context); 80 : 0 : return 1; 81 : : } 82 : : 83 : 0 : g_option_context_free (context); 84 : : 85 : 0 : file = g_file_new_for_commandline_arg (argv[1]); 86 : 0 : new_file = g_file_set_display_name (file, argv[2], NULL, &error); 87 : : 88 [ # # ]: 0 : if (new_file == NULL) 89 : : { 90 : 0 : print_error ("%s", error->message); 91 : 0 : g_error_free (error); 92 : 0 : retval = 1; 93 : : } 94 : : else 95 : : { 96 : 0 : char *uri = g_file_get_uri (new_file); 97 : 0 : g_print (_("Rename successful. New uri: %s\n"), uri); 98 : 0 : g_object_unref (new_file); 99 : 0 : g_free (uri); 100 : : } 101 : : 102 : 0 : g_object_unref (file); 103 : : 104 : 0 : return retval; 105 : : }