summaryrefslogtreecommitdiff
path: root/utils/tracker-resdump/tracker-resdump.vala
blob: 2f116d12dac20d6d169e434aa30dbcfb1b4c0e91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2010, Nokia
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

using Tracker.Sparql;
using GLib;

static Connection conn;

static void usage (string[] args) {
	stderr.printf("Usage: %s urn\n", args[0]);
}

static GLib.GenericSet<string> looked_up_iris;

static bool dump_resource (string urn) {
	GLib.List<string> iris_to_lookup = new GLib.List<string> ();

	looked_up_iris.add (urn);

	try {
		Cursor cursor = conn.query ("SELECT ?p rdfs:range(?p) ?o {<%s> ?p ?o}".printf (urn));

		GLib.List<string> type_statements = new GLib.List<string>();
		GLib.List<string> statements = new GLib.List<string>();

		while (cursor.next ()) {
			// Skip tracker internal stuff
			if (cursor.get_string (0).has_prefix ("http://www.tracker-project.org/ontologies/tracker#")) {
				continue;
			}

			string statement = "<%s> <%s> ".printf (urn, cursor.get_string (0));

			switch (cursor.get_string(1)) {
				case "http://www.w3.org/2001/XMLSchema#string":
				case "http://www.w3.org/2001/XMLSchema#dateTime":
					statement += "\"%s\"".printf (escape_string (cursor.get_string (2)));
					break;
				case "http://www.w3.org/2001/XMLSchema#integer":
				case "http://www.w3.org/2001/XMLSchema#double":
				case "http://www.w3.org/2001/XMLSchema#boolean":
					statement += "%s".printf (cursor.get_string (2));
					break;
				default:
					// Assume resource
					unowned string obj = cursor.get_string (2);
					if (!looked_up_iris.contains (obj)) {
						iris_to_lookup.append (obj);
					}
					statement += "<%s>".printf (obj);
					break;
			}

			if (cursor.get_string (0) == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") {
				type_statements.append (statement);
			} else {
				statements.append (statement);
			}
		}

		foreach (string s in type_statements) {
			stdout.printf ("%s .\n", s);
		}

		foreach (string s in statements) {
			stdout.printf ("%s .\n", s);
		}

		foreach (string s in iris_to_lookup) {
			if (!dump_resource (s)) {
				return false;
			}
		}
	} catch (GLib.Error e) {
		critical ("Couldn't query info for resource %s: %s", urn, e.message);
		return false;
	}

	return true;
}

static int main(string[] args)
{
	if (args.length != 2) {
		usage (args);
		return 1;
	}

	try {
		conn = Connection.bus_new("org.freedesktop.Tracker3.Miner.Files", null);
	} catch (GLib.Error e) {
		critical("Couldn't connect to Tracker: %s", e.message);
		return 1;
	}

	looked_up_iris = new GLib.GenericSet<string>(GLib.str_hash, GLib.str_equal);

	if (dump_resource (args[1])) {
		return 0;
	}

	return 1;
}