Blame gio/gio-tool-rename.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 const GOptionEntry entries[] = {
Packit ae235b
  { NULL }
Packit ae235b
};
Packit ae235b
Packit ae235b
int
Packit ae235b
handle_rename (int argc, char *argv[], gboolean do_help)
Packit ae235b
{
Packit ae235b
  GOptionContext *context;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GFile *file;
Packit ae235b
  GFile *new_file;
Packit ae235b
  int retval = 0;
Packit ae235b
  gchar *param;
Packit ae235b
Packit ae235b
  g_set_prgname ("gio rename");
Packit ae235b
Packit ae235b
  /* Translators: commandline placeholder */
Packit ae235b
  param = g_strdup_printf ("%s %s", _("LOCATION"), _("NAME"));
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
Packit ae235b
  g_option_context_set_summary (context, _("Rename a file."));
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
  if (argc < 3)
Packit ae235b
    {
Packit ae235b
      show_help (context, _("Missing argument"));
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
  if (argc > 3)
Packit ae235b
    {
Packit ae235b
      show_help (context, _("Too many arguments"));
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
  file = g_file_new_for_commandline_arg (argv[1]);
Packit ae235b
  new_file = g_file_set_display_name (file, argv[2], NULL, &error);
Packit ae235b
Packit ae235b
  if (new_file == NULL)
Packit ae235b
    {
Packit ae235b
      print_error ("%s", error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      retval = 1;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      char *uri = g_file_get_uri (new_file);
Packit ae235b
      g_print (_("Rename successful. New uri: %s\n"), uri);
Packit ae235b
      g_object_unref (new_file);
Packit ae235b
      g_free (uri);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (file);
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}