1. Developing a very simple client from scratch
Contents
1.1. Requirements
This example has been tested on the system with tracker-client-0.9 installed from sources. Might work with other versions after changing the configure.in.
1.2. Project files
main.c, Makefile.am, configure.in
1.2.1. configure.in
AC_INIT(main.c) AM_INIT_AUTOMAKE(main,1.0) AC_PROG_CC PKG_CHECK_MODULES(TRACKER, [tracker-client-0.9]) AC_SUBST(TRACKER_CFLAGS) AC_SUBST(TRACKER_LIBS) AC_PROG_INSTALL AC_OUTPUT(Makefile)
1.2.2. Makefile.am
INCLUDES = $(DBUS_CFLAGS) $(TRACKER_CFLAGS) main_LDADD = $(DBUS_LIBS) $(TRACKER_LIBS) bin_PROGRAMS = main main_SOURCES = main.c
1.2.3. main.c
#include <glib.h>
#include <libtracker-client/tracker-client.h>
void print_row (gpointer data, gpointer user_data)
{
gchar **columns = (gchar**) data;
g_print ("%s\n", columns[0]);
}
int main (int argc, char **argv)
{
TrackerClient *client = NULL;
GPtrArray *rows = NULL;
GError *error = NULL;
const gchar *query;
client = tracker_client_new (0, G_MAXINT);
query = "SELECT ?s WHERE { ?s rdf:type rdfs:Class . }";
// Uncomment the line below and comment out the line above if you have Tracker running and it contains some files
// query = "SELECT ?fileName WHERE { ?contact a nfo:FileDataObject; nfo:fileName ?fileName}";
// Returns an array containing pointers to arrays of strings
rows = tracker_resources_sparql_query (client, query, &error);
if (error) {
g_warning ("Could not query Tracker, %s", error->message);
g_error_free (error);
g_object_unref (client);
return -1;
}
// Processes a query result
g_ptr_array_foreach (rows, print_row, NULL);
g_ptr_array_free (rows, TRUE);
g_object_unref (client);
return 0;
}
1.3. Building the project
1.3.1. The simplest way
In this case Makefile.am and configure.in are not needed.
$ cc `pkg-config --cflags --libs tracker-client` main.c -o main
1.3.2. Using autotools
Put all three files in the same folder and execute the line below.
$ aclocal && autoconf && automake -a && ./configure