This site has been retired. For up to date information, see handbook.gnome.org or gitlab.gnome.org.


[Home] [TitleIndex] [WordIndex

1. Developing a client that uses Tracker's RDF store

This is deprecated. Use libtracker-sparql instead.

1.1. Using libtracker-client

1.1.1. Build environment

1.1.1.1. 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)

1.1.1.2. Makefile.am

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

1.1.2. 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);

1.1.3. 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);

1.1.4. 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);

1.2. Using your own D-Bus connection (GLib style)

1.2.1. Build environment

1.2.1.1. 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)

1.2.1.2. 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)

1.2.2. 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);
}

1.2.3. 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.

1.2.4. 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);

1.3. Using libqttracker (Qt style)

1.3.1. Build environment

1.3.1.1. 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

1.3.1.2. With cmake

TODO

1.3.2. 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

1.3.3. Direct calling

1.3.3.1. Calling RAW SPARQL using qttracker

TODO

1.3.3.2. Calling RAW SPARQL using QtDBus

TODO

1.3.4. Clientside timeout

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

  TODO for Qt

2024-10-23 10:59