Developing a client that uses Tracker's RDF store

This is deprecated. Use libtracker-sparql instead.

Using libtracker-client

Build environment

configure.ac

Create a variable that contains the CFLAGS and LIBS for libtracker-client

PKG_CHECK_MODULES(TRACKER, [tracker-client-0.7])
AC_SUBST(TRACKER_CFLAGS)
AC_SUBST(TRACKER_LIBS)

Makefile.am

INCLUDES += $(TRACKER_CFLAGS)
my_target_LDADD += $(TRACKER_LIBS)

Code for read query

#include <glib.h>

#include <libtracker-client/tracker.h>
#include <libtracker-common/tracker-common.h>

int
main (int argc, char **argv)
{
  GError *error = NULL;
  TrackerClient *client;
  client = tracker_connect (FALSE, G_MAXINT);
  const gchar *query = "SELECT ?r { ?r a rdfs:Resource }";

  results = tracker_resources_sparql_query (client, query, &error);

  if (results) {
    /* Print results */
    g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
    g_ptr_array_free (results, TRUE);
  }

  tracker_disconnect (client);
  g_clear_error (&error);

Code for write query

int
main (int argc, char **argv)
{
  GError *error = NULL;
  TrackerClient *client;

  /* always use G_MAXINT for batch writes */
  client = tracker_connect (FALSE, G_MAXINT);
  const gchar *query = "INSERT { ?r a rdfs:Resource }";

  tracker_resources_sparql_update (client, query, &error);

  /* For batch writes use this (make sure that you use G_MAXINT at
   * tracker_connect): tracker_resources_batch_sparql_update */

  tracker_disconnect (client);
  g_clear_error (&error);

Clientside timeout

When you use batch updates you need to set the timeout to G_MAXINT. You do it this way:

  client = tracker_connect (FALSE, G_MAXINT);

Using your own D-Bus connection (GLib style)

Build environment

configure.ac

Create a variable that contains the path to dbus-binding-tool

AC_PATH_PROG(DBUSBINDINGTOOL, dbus-binding-tool)
if test -z $DBUSBINDINGTOOL; then
   AC_MSG_ERROR([Could not find 'dbus-binding-tool'])
fi

Add CFLAGS and LIBS into variables

PKG_CHECK_MODULES(DBUS, [dbus-1 >= $DBUS_REQUIRED dbus-glib-1 >= $DBUS_GLIB_REQUIRED])
AC_SUBST(DBUS_CFLAGS)
AC_SUBST(DBUS_LIBS)

Makefile.am

Add the DBus CFLAGS and LIBS to the right variables

INCLUDES += $(DBUS_CFLAGS)
my_target_LDADD += $(DBUS_LIBS)

Generate binding glue sources

# Generate DBus files from XML data.
dbus_sources = \
        tracker-resources-glue.h \

%-glue.h: /usr/share/tracker/%.xml
        $(AM_V_GEN)$(DBUSBINDINGTOOL) \
        --mode=glib-client --output=$@ \
        --prefix=$(subst -,_,$*) $^

BUILT_SOURCES = $(dbus_sources)

CLEANFILES = $(BUILT_SOURCES)

Using DBusGProxy directly

void somefunc (void)
{
 DBusGProxy *proxy;
 GError *error = NULL;
 GPtrArray *retval = NULL;
 const gchar *query = "SELECT ?r { ?r a rdfs:Resource }";

 proxy = dbus_g_proxy_new_for_name (connection,
                                    "org.freedesktop.Tracker1",
                                    "/org/freedesktop/Tracker1/Resources",
                                    "org.freedesktop.Tracker1.Resources");


 dbus_g_proxy_call_with (proxy, "SparqlQuery", 
                         &error,
                         G_TYPE_STRING, query,
                         G_TYPE_INVALID, 
                         dbus_g_type_get_collection ("GPtrArray", G_TYPE_STRV),
                                     &retval,
                         G_TYPE_INVALID);

  if (retval) {
    /* Print results */
    g_ptr_array_foreach (retval, (GFunc) g_strfreev, NULL);
    g_ptr_array_free (retval, TRUE);
  }

  g_clear_error (&error);
}

Using the generated glue

With the buildenvironment of above you should have a generated file called tracker-resources-glue.h. You can use its generated API.

Clientside timeout

When you use batch updates you need to set the timeout to G_MAXINT. You do it this way:

  dbus_g_proxy_set_default_timeout (proxy, G_MAXINT);

Using libqttracker (Qt style)

Build environment

With qmake

TODO: Correct this example (I'm not a qmake expert)

project.pro:

TEMPLATE = app
CONFIG += debug
INCLUDEPATH += . /usr/include/QtTracker
LIBS += -L/usr/lib -lqttracker
SOURCES += ExampleTest.cpp

With cmake

TODO

Examples

#include <QObject>
#include <QString>
#include <QStringList>
#include <QUrl>

#include <QtTracker/Tracker>
#include <QtTracker/ontologies/nco.h>
#include <QtTracker/ontologies/nmo.h>

using namespace SopranoLive;

void somefunc(void)
{
  RDFSelect outer;
  RDFVariable from;
  RDFVariable name = outer.newColumn<nco::Contact>("name");
  from.isOfType<nco::Contact>();
  from.property<nco::hasEmailAddress>(name);
  RDFSelect inner = outer.subQuery();
  RDFVariable in_from = inner.newColumn("from");
  RDFVariable msg;
  msg.property<nmo::from>(in_from);
  msg.isOfType<nmo::Email>();
  outer.addCountColumn("total messages", msg);
  outer.groupBy(from);

  LiveNodes rows = ::tracker()->modelQuery(outer);
}

TODO: unfinished, qttracker docs

Direct calling

Calling RAW SPARQL using qttracker

TODO

Calling RAW SPARQL using QtDBus

TODO

Clientside timeout

When you use batch updates you need to set the timeout to maxint. You do it this way:

  TODO for Qt

Projects/Tracker/Documentation/ClientDevelopment (last edited 2013-11-25 12:54:23 by WilliamJonMcCann)