diff options
author | Philip Van Hoof <philip@codeminded.be> | 2010-08-24 17:16:54 +0200 |
---|---|---|
committer | Philip Van Hoof <philip@codeminded.be> | 2010-09-01 17:06:30 +0200 |
commit | ea2bc4fae75313917f39730e0b846d6889615f55 (patch) | |
tree | c0580a816ff4d65cd07b233e9faaf2ac93b5c630 /examples | |
parent | 3b62635175114eeec8d18d7384572076e4d543bc (diff) | |
download | tracker-ea2bc4fae75313917f39730e0b846d6889615f55.tar.gz |
examples: class-signal: Add example in plain C
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile.am | 3 | ||||
-rw-r--r-- | examples/class-signal/Makefile.am | 20 | ||||
-rw-r--r-- | examples/class-signal/class-signal.c | 132 |
3 files changed, 154 insertions, 1 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am index 41a8a6762..de2b2a9e4 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -3,4 +3,5 @@ include $(top_srcdir)/Makefile.decl SUBDIRS = \ libtracker-extract \ libtracker-miner \ - rss-reader + rss-reader \ + class-signal diff --git a/examples/class-signal/Makefile.am b/examples/class-signal/Makefile.am new file mode 100644 index 000000000..8774fce2e --- /dev/null +++ b/examples/class-signal/Makefile.am @@ -0,0 +1,20 @@ +include $(top_srcdir)/Makefile.decl + +INCLUDES = \ + -I$(top_srcdir)/src/libtracker-sparql \ + -I$(top_srcdir)/src \ + $(WARN_CFLAGS) \ + $(DBUS_CFLAGS) \ + $(GLIB2_CFLAGS) \ + $(GIO_CFLAGS) \ + $(GTHREAD_CFLAGS) + +noinst_PROGRAMS = class-signal + +class_signal_SOURCES = class-signal.c + +class_signal_LDADD = \ + $(top_builddir)/src/libtracker-sparql/libtracker-sparql-$(TRACKER_API_VERSION).la \ + $(GIO_LIBS) \ + $(GLIB2_LIBS) \ + $(DBUS_LIBS) diff --git a/examples/class-signal/class-signal.c b/examples/class-signal/class-signal.c new file mode 100644 index 000000000..530d6cc77 --- /dev/null +++ b/examples/class-signal/class-signal.c @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2009, Nokia <ivan.frade@nokia.com> + * + * 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. + */ + +#include <dbus/dbus-glib-bindings.h> +#include <dbus/dbus-glib-lowlevel.h> + +#include <libtracker-sparql/tracker-sparql.h> + +#define TRACKER_TYPE_THREE_INT_ARRAY \ + dbus_g_type_get_collection ("GPtrArray", \ + dbus_g_type_get_struct("GValueArray", \ + G_TYPE_INT, \ + G_TYPE_INT, \ + G_TYPE_INT, \ + G_TYPE_INVALID)) + +#define TRACKER_SERVICE "org.freedesktop.Tracker1" +#define TRACKER_RESOURCES_OBJECT "/org/freedesktop/Tracker1/Resources" +#define TRACKER_INTERFACE_RESOURCES "org.freedesktop.Tracker1.Resources" + +#define DBUS_MATCH_STR "type='signal', " \ + "sender='" TRACKER_SERVICE "', " \ + "path='" TRACKER_RESOURCES_OBJECT "', " \ + "interface='" TRACKER_INTERFACE_RESOURCES "'" + +static TrackerSparqlConnection *con; + +static void +handle_statement (gint subject, gint predicate) +{ + gchar *query, *pred; + TrackerSparqlCursor *cursor; + + query = g_strdup_printf ("SELECT tracker:subject (%d) tracker:subject(%d) {}", + subject, predicate); + cursor = tracker_sparql_connection_query (con, query, NULL, NULL); + g_free (query); + tracker_sparql_cursor_next (cursor, NULL, NULL); + pred = g_strdup (tracker_sparql_cursor_get_string (cursor, 1, NULL)); + query = g_strdup_printf ("SELECT ?t { <%s> <%s> ?t }", + tracker_sparql_cursor_get_string (cursor, 0, NULL), + pred); + g_object_unref (cursor); + cursor = tracker_sparql_connection_query (con, query, NULL, NULL); + g_free (query); + while (tracker_sparql_cursor_next (cursor, NULL, NULL)) + g_print ("\t%s = %s\n", pred, tracker_sparql_cursor_get_string (cursor, 0, NULL)); + g_print ("\n"); + g_free (pred); + g_object_unref (cursor); +} + +static void +class_signal_cb (DBusMessage *message) +{ + DBusMessageIter iter, arr; + gchar *class_name; + gint arg_type, i; + + dbus_message_iter_init (message, &iter); + dbus_message_iter_get_basic (&iter, &class_name); + g_print ("%s:\n", class_name); + + for (i = 0; i < 2; i++) { + dbus_message_iter_next (&iter); + dbus_message_iter_recurse (&iter, &arr); + + while ((arg_type = dbus_message_iter_get_arg_type (&arr)) != DBUS_TYPE_INVALID) { + DBusMessageIter strct; + gint subject = 0, predicate = 0, object = 0; + + dbus_message_iter_recurse (&arr, &strct); + dbus_message_iter_get_basic (&strct, &subject); + dbus_message_iter_next (&strct); + dbus_message_iter_get_basic (&strct, &predicate); + dbus_message_iter_next (&strct); + dbus_message_iter_get_basic (&strct, &object); + handle_statement (subject, predicate); + dbus_message_iter_next (&arr); + } + } +} + +static DBusHandlerResult +message_filter (DBusConnection *connection, DBusMessage *message, gpointer ud) +{ + if (dbus_message_is_signal (message, TRACKER_INTERFACE_RESOURCES, "ClassSignal")) { + class_signal_cb (message); + return DBUS_HANDLER_RESULT_HANDLED; + } + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + + +gint +main (gint argc, gchar *argv[]) +{ + GMainLoop *loop; + GError *error = NULL; + DBusConnection *connection; + + g_type_init (); + loop = g_main_loop_new (NULL, FALSE); + con = tracker_sparql_connection_get (&error); + connection = dbus_bus_get_private (DBUS_BUS_SESSION, NULL); + dbus_bus_request_name (connection, TRACKER_SERVICE, 0, NULL); + dbus_connection_add_filter (connection, message_filter, NULL, NULL); + dbus_bus_add_match (connection, DBUS_MATCH_STR, NULL); + dbus_connection_setup_with_g_main (connection, NULL); + g_main_loop_run (loop); + g_main_loop_unref (loop); + g_object_unref (con); + dbus_connection_unref (connection); + + return 0; +} |