summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeif Lotfy <seif@lotfy.com>2012-11-11 21:58:06 +0100
committerSeif Lotfy <seif@lotfy.com>2012-11-11 21:58:06 +0100
commit4aa755cd11a45b9d722f77d0336d195995e6f302 (patch)
treedc749d96ad237c8a8816270f770a2c611c001af7
parentdbfef2fe720394b14984daf44ff59d63505c8cd7 (diff)
downloadzeitgeist-4aa755cd11a45b9d722f77d0336d195995e6f302.tar.gz
Add mising files for C examples
-rw-r--r--examples/c/Makefile.am35
-rw-r--r--examples/c/find-events.c93
-rw-r--r--examples/c/monitor-events.c83
-rw-r--r--examples/c/search-events.c108
4 files changed, 319 insertions, 0 deletions
diff --git a/examples/c/Makefile.am b/examples/c/Makefile.am
new file mode 100644
index 00000000..0d165cb6
--- /dev/null
+++ b/examples/c/Makefile.am
@@ -0,0 +1,35 @@
+include $(top_srcdir)/libzeitgeist/Makefile.decl
+
+INCLUDES = \
+ -I$(top_srcdir)/libzeitgeist -I$(top_builddir)/libzeitgeist \
+ $(GIO_UNIX_CFLAGS) \
+ $(ZEITGEIST_CFLAGS)
+
+AM_CFLAGS = \
+ -Wall \
+ -g \
+ -DZEITGEIST_COMPILATION \
+ -DTEST_DIR=\"$(top_srcdir)/tests\"
+
+zeitgeist_libs = $(top_builddir)/libzeitgeist/libzeitgeist-2.0.la $(ZEITGEIST_LIBS)
+
+EXTRA_DIST += \
+ test.desktop
+
+helper_sources = \
+ $(top_srcdir)/libzeitgeist/zeitgeist.h \
+ test.desktop
+
+noinst_PROGRAMS = \
+ find-events \
+ monitor-events \
+ search-events
+
+find_events_SOURCES = find-events.c
+find_events_LDADD = $(zeitgeist_libs)
+
+monitor_events_SOURCES = monitor-events.c
+monitor_events_LDADD = $(zeitgeist_libs)
+
+search_events_SOURCES = search-events.c
+search_events_LDADD = $(zeitgeist_libs)
diff --git a/examples/c/find-events.c b/examples/c/find-events.c
new file mode 100644
index 00000000..48a53ddc
--- /dev/null
+++ b/examples/c/find-events.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2010 Canonical Ltd
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * This program 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 program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
+ *
+ */
+
+#include <glib.h>
+#include <glib/gprintf.h>
+#include <glib-object.h>
+#include <zeitgeist.h>
+
+static void
+on_events_received (ZeitgeistLog *log,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ ZeitgeistResultSet *events;
+ ZeitgeistEvent *event;
+ ZeitgeistSubject *subject;
+ GError *error;
+ GMainLoop *mainloop = (GMainLoop*) user_data;
+ gint i;
+
+ error = NULL;
+ events = zeitgeist_log_find_events_finish (log, res, &error);
+
+ if (error)
+ {
+ g_warning ("Error reading results: %s", error->message);
+ g_error_free (error);
+ return;
+ }
+
+ g_message ("Got %i events:", zeitgeist_result_set_size (events));
+
+ while (zeitgeist_result_set_has_next (events))
+ {
+ event = zeitgeist_result_set_next_value (events);
+ for (i = 0; i < zeitgeist_event_num_subjects (event); i++)
+ {
+ subject = zeitgeist_event_get_subject (event, i);
+ g_printf ("%s\n", zeitgeist_subject_get_uri (subject));
+ }
+ }
+
+ g_object_unref (events);
+
+ g_main_loop_quit (mainloop);
+}
+
+gint
+main (gint argc,
+ gchar *argv[])
+{
+ GMainLoop *mainloop;
+ ZeitgeistLog *log;
+ GPtrArray *templates;
+
+ g_type_init ();
+
+ mainloop = g_main_loop_new (NULL, FALSE);
+ log = g_object_new (ZEITGEIST_TYPE_LOG, NULL);
+
+ templates = g_ptr_array_new ();
+ g_ptr_array_add (templates, zeitgeist_event_new ());
+
+ zeitgeist_log_find_events (log,
+ zeitgeist_time_range_new_to_now (),
+ templates,
+ ZEITGEIST_STORAGE_STATE_ANY,
+ 10,
+ ZEITGEIST_RESULT_TYPE_MOST_RECENT_EVENTS,
+ NULL,
+ (GAsyncReadyCallback)on_events_received,
+ mainloop);
+
+ g_main_loop_run (mainloop);
+
+ return 0;
+}
diff --git a/examples/c/monitor-events.c b/examples/c/monitor-events.c
new file mode 100644
index 00000000..593bf288
--- /dev/null
+++ b/examples/c/monitor-events.c
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2010 Canonical Ltd
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * This program 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 program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
+ *
+ */
+
+#include <glib.h>
+#include <glib/gprintf.h>
+#include <glib-object.h>
+#include <zeitgeist.h>
+
+static void
+on_events_inserted (ZeitgeistMonitor *mon,
+ ZeitgeistTimeRange *time_range,
+ ZeitgeistResultSet *events,
+ gpointer user_data)
+{
+ ZeitgeistEvent *event;
+ ZeitgeistSubject *subject;
+ gint j;
+
+ g_message ("%i events inserted", zeitgeist_result_set_size (events));
+
+ while (zeitgeist_result_set_has_next (events))
+ {
+ event = zeitgeist_result_set_next_value (events);
+ for (j = 0; j < zeitgeist_event_num_subjects (event); j++)
+ {
+ subject = zeitgeist_event_get_subject (event, j);
+ g_message (" * %s", zeitgeist_subject_get_uri (subject));
+ }
+ }
+}
+
+static void
+on_events_deleted (ZeitgeistMonitor *mon,
+ ZeitgeistTimeRange *time_range,
+ GArray *event_ids,
+ gpointer user_data)
+{
+ g_message ("%i events deleted", event_ids->len);
+}
+
+gint
+main (gint argc,
+ gchar *argv[])
+{
+ GMainLoop *mainloop;
+ ZeitgeistLog *log;
+ ZeitgeistMonitor *monitor;
+ GPtrArray *templates;
+
+ g_type_init ();
+
+ mainloop = g_main_loop_new (NULL, FALSE);
+ log = g_object_new (ZEITGEIST_TYPE_LOG, NULL);
+
+ /* Templates matching anything */
+ templates = g_ptr_array_new ();
+ g_ptr_array_add (templates, zeitgeist_event_new ());
+
+ monitor = zeitgeist_monitor_new (zeitgeist_time_range_new_anytime (),
+ templates);
+
+ zeitgeist_log_install_monitor (log, monitor, G_CALLBACK (on_events_inserted), NULL);
+
+ g_main_loop_run (mainloop);
+
+ return 0;
+}
diff --git a/examples/c/search-events.c b/examples/c/search-events.c
new file mode 100644
index 00000000..bc9de9b0
--- /dev/null
+++ b/examples/c/search-events.c
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2010 Canonical Ltd
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * This program 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 program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
+ *
+ */
+
+#include <glib.h>
+#include <glib/gprintf.h>
+#include <glib-object.h>
+#include <zeitgeist.h>
+
+static void
+on_events_received (ZeitgeistIndex *index,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ ZeitgeistResultSet *events;
+ ZeitgeistEvent *event;
+ ZeitgeistSubject *subject;
+ GError *error;
+ GMainLoop *mainloop = (GMainLoop*) user_data;
+ gint i;
+
+ error = NULL;
+ events = zeitgeist_index_search_finish (index, res, &error);
+
+ if (error)
+ {
+ g_warning ("Error reading results: %s", error->message);
+ g_error_free (error);
+ return;
+ }
+
+ g_message ("Got %u/%u events:",
+ zeitgeist_result_set_size (events),
+ zeitgeist_result_set_estimated_matches (events));
+
+ while (zeitgeist_result_set_has_next (events))
+ {
+ event = zeitgeist_result_set_next_value (events);
+ for (i = 0; i < zeitgeist_event_num_subjects (event); i++)
+ {
+ subject = zeitgeist_event_get_subject (event, i);
+ g_printf ("%s\n", zeitgeist_subject_get_uri (subject));
+ }
+ }
+
+ g_object_unref (events);
+
+ g_main_loop_quit (mainloop);
+}
+
+gint
+main (gint argc,
+ gchar *argv[])
+{
+ GMainLoop *mainloop;
+ ZeitgeistIndex *index;
+ gchar **queryv;
+ gchar *query;
+
+ g_type_init ();
+
+ if (argc <= 1)
+ {
+ g_printf ("Please specify a string to search for.\n");
+ return 1;
+ }
+
+ /* Construct query string by concatenating the command line args
+ * except the first one */
+ queryv = argv;
+ queryv++;
+ query = g_strjoinv (" ", queryv);
+
+ mainloop = g_main_loop_new (NULL, FALSE);
+ index = zeitgeist_index_new ();
+
+ g_printf ("Searching for '%s':\n", query);
+
+ zeitgeist_index_search (index,
+ query,
+ zeitgeist_time_range_new_anytime (),
+ g_ptr_array_new (),
+ 0,
+ 10,
+ ZEITGEIST_RESULT_TYPE_RELEVANCY,
+ NULL,
+ (GAsyncReadyCallback)on_events_received,
+ mainloop);
+
+ g_main_loop_run (mainloop);
+
+ return 0;
+}