tracker-0.16.2/src/tracker-utils/tracker-import.c

No issues found

  1 /*
  2  * Copyright (C) 2006, Jamie McCracken <jamiemcc@gnome.org>
  3  * Copyright (C) 2008-2009, Nokia <ivan.frade@nokia.com>
  4  *
  5  * This library is free software; you can redistribute it and/or
  6  * modify it under the terms of the GNU Library General Public
  7  * License as published by the Free Software Foundation; either
  8  * version 2 of the License, or (at your option) any later version.
  9  *
 10  * This library is distributed in the hope that it will be useful,
 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  * General Public License for more details.
 14  *
 15  * You should have received a copy of the GNU Library General Public
 16  * License along with this library; if not, write to the
 17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 18  * Boston, MA  02110-1301, USA.
 19  */
 20 
 21 #include "config.h"
 22 
 23 #include <stdlib.h>
 24 #include <string.h>
 25 #include <time.h>
 26 #include <locale.h>
 27 #include <stdint.h>
 28 
 29 #include <glib.h>
 30 #include <glib/gi18n.h>
 31 
 32 #include <libtracker-sparql/tracker-sparql.h>
 33 
 34 #define ABOUT \
 35 	"Tracker " PACKAGE_VERSION "\n"
 36 
 37 #define LICENSE \
 38 	"This program is free software and comes without any warranty.\n" \
 39 	"It is licensed under version 2 or later of the General Public " \
 40 	"License which can be viewed at:\n" \
 41 	"\n" \
 42 	"  http://www.gnu.org/licenses/gpl.txt\n"
 43 
 44 
 45 static gchar        **filenames = NULL;
 46 static gboolean       print_version;
 47 
 48 static GOptionEntry   entries[] = {
 49 	{ "version", 'V', 0, G_OPTION_ARG_NONE, &print_version,
 50 	  N_("Print version"),
 51 	  NULL,
 52 	},
 53 	{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames,
 54 	  N_("FILE"),
 55 	  N_("FILE")},
 56 	{ NULL }
 57 };
 58 
 59 int
 60 main (int argc, char **argv)
 61 {
 62 	TrackerSparqlConnection *connection;
 63 	GOptionContext *context;
 64 	GError *error = NULL;
 65 	gchar **p;
 66 
 67 	setlocale (LC_ALL, "");
 68 
 69 	bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
 70 	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 71 	textdomain (GETTEXT_PACKAGE);
 72 
 73 	/* Translators: this messagge will apper immediately after the  */
 74 	/* usage string - Usage: COMMAND [OPTION]... <THIS_MESSAGE>     */
 75 	context = g_option_context_new (_("- Import data using Turtle files"));
 76 
 77 	/* Translators: this message will appear after the usage string */
 78 	/* and before the list of options.                              */
 79 	g_option_context_add_main_entries (context, entries, NULL);
 80 	g_option_context_parse (context, &argc, &argv, NULL);
 81 
 82 	if (print_version) {
 83 		g_print ("\n" ABOUT "\n" LICENSE "\n");
 84 		g_option_context_free (context);
 85 
 86 		return EXIT_SUCCESS;
 87 	}
 88 
 89 	if (!filenames) {
 90 		gchar *help;
 91 
 92 		g_printerr ("%s\n\n",
 93 		            _("One or more files have not been specified"));
 94 
 95 		help = g_option_context_get_help (context, TRUE, NULL);
 96 		g_option_context_free (context);
 97 		g_printerr ("%s", help);
 98 		g_free (help);
 99 
100 		return EXIT_FAILURE;
101 	}
102 
103 	g_option_context_free (context);
104 
105 	connection = tracker_sparql_connection_get (NULL, &error);
106 
107 	if (!connection) {
108 		g_printerr ("%s: %s\n",
109 		            _("Could not establish a connection to Tracker"),
110 		            error ? error->message : _("No error given"));
111 		g_clear_error (&error);
112 		return EXIT_FAILURE;
113 	}
114 
115 	for (p = filenames; *p; p++) {
116 		GError *error = NULL;
117 		GFile *file;
118 
119 		g_print ("%s:'%s'\n",
120 		         _("Importing Turtle file"),
121 		         *p);
122 
123 		file = g_file_new_for_commandline_arg (*p);
124 		tracker_sparql_connection_load (connection, file, NULL, &error);
125 		g_object_unref (file);
126 
127 		if (error) {
128 			g_printerr ("  %s, %s\n",
129 			            _("Unable to import Turtle file"),
130 			            error->message);
131 
132 			g_error_free (error);
133 			continue;
134 		}
135 
136 		g_print ("  %s\n", _("Done"));
137 		g_print ("\n");
138 	}
139 
140 	g_object_unref (connection);
141 
142 	return EXIT_SUCCESS;
143 }