diff options
author | Seif Lotfy <seif@lotfy.com> | 2012-11-11 21:59:52 +0100 |
---|---|---|
committer | Seif Lotfy <seif@lotfy.com> | 2012-11-11 21:59:52 +0100 |
commit | e0c8dde5e3fb1f19e482198f58295dc52382e102 (patch) | |
tree | 3fed177791fc2ca466c93c02144222dba937aeb4 | |
parent | 4aa755cd11a45b9d722f77d0336d195995e6f302 (diff) | |
download | zeitgeist-e0c8dde5e3fb1f19e482198f58295dc52382e102.tar.gz |
Moved Vala examples to examples/vala
-rw-r--r-- | examples/vala/Makefile.am | 45 | ||||
-rw-r--r-- | examples/vala/data-source-stuff.vala | 79 | ||||
-rw-r--r-- | examples/vala/get-events-with-id.vala | 28 | ||||
-rw-r--r-- | examples/vala/insert-events.vala | 38 | ||||
-rw-r--r-- | examples/vala/monitor-events.vala | 32 | ||||
-rw-r--r-- | examples/vala/most-recent-events.vala | 28 |
6 files changed, 250 insertions, 0 deletions
diff --git a/examples/vala/Makefile.am b/examples/vala/Makefile.am new file mode 100644 index 00000000..5a6d7138 --- /dev/null +++ b/examples/vala/Makefile.am @@ -0,0 +1,45 @@ +NULL = + +AM_CPPFLAGS = \ + $(ZEITGEIST_CFLAGS) \ + -include $(CONFIG_HEADER) \ + -I $(top_srcdir)/libzeitgeist/ \ + -w \ + $(NULL) + +AM_VALAFLAGS = \ + --target-glib=2.26 \ + --pkg gio-2.0 \ + $(top_srcdir)/libzeitgeist/zeitgeist-2.0.vapi \ + $(top_srcdir)/config.vapi \ + $(NULL) + +zeitgeist_libs = \ + $(top_builddir)/libzeitgeist/libzeitgeist-2.0.la \ + $(ZEITGEIST_LIBS) + +noinst_PROGRAMS = \ + data-source-stuff \ + get-events-with-id \ + monitor-events \ + most-recent-events \ + insert-events \ + $(NULL) + +data_source_stuff_SOURCES = data-source-stuff.vala +data_source_stuff_LDADD = $(zeitgeist_libs) + +get_events_with_id_SOURCES = get-events-with-id.vala +get_events_with_id_LDADD = $(zeitgeist_libs) + +monitor_events_SOURCES = monitor-events.vala +monitor_events_LDADD = $(zeitgeist_libs) + +most_recent_events_SOURCES = most-recent-events.vala +most_recent_events_LDADD = $(zeitgeist_libs) + +insert_events_SOURCES = insert-events.vala +insert_events_LDADD = $(zeitgeist_libs) + +distclean-local: + rm -f *.c *.o *.~[0-9]~ diff --git a/examples/vala/data-source-stuff.vala b/examples/vala/data-source-stuff.vala new file mode 100644 index 00000000..6e744287 --- /dev/null +++ b/examples/vala/data-source-stuff.vala @@ -0,0 +1,79 @@ +using Zeitgeist; + +MainLoop loop; + +async void do_stuff () throws Error +{ + var registry = new Zeitgeist.DataSourceRegistry (); + + registry.source_registered.connect (on_source_registered); + registry.source_enabled.connect (on_source_enabled); + + DataSource my_data_source = new DataSource.full ( + "com.example.test/my-ds", "Example Data-Source", + "An example data-source for testing libzeitgeist2", + new GenericArray<Event>()); + + stdout.printf ("Registering with data-source registry...\n"); + bool enabled = yield registry.register_data_source (my_data_source); + stdout.printf ("Done. The data-source is %s.\n\n", + (enabled) ? "enabled" : "disabled"); + + stdout.printf ("Disabling the data-source...\n"); + yield registry.set_data_source_enabled (my_data_source.unique_id, false); + + yield print_data_sources (registry); + + stdout.printf ("\nEnabling it again...\n"); + yield registry.set_data_source_enabled (my_data_source.unique_id, true); + + yield print_data_sources (registry); + + loop.quit (); +} + +void on_source_registered (DataSource source) +{ + stdout.printf("%s registered!\n", source.name); +} + +async void on_source_enabled (string unique_id, bool enabled) +{ + var registry = new Zeitgeist.DataSourceRegistry (); + DataSource source; + try + { + source = yield registry.get_data_source_from_id (unique_id); + stdout.printf("%s has been %s!\n", source.name, + (enabled) ? "enabled" : "disabled"); + } + catch (Error e) + { + critical ("Error retrieving data-source information: %s", e.message); + } +} + +async void print_data_sources (DataSourceRegistry registry) throws Error +{ + GenericArray<DataSource> datasources = null; + datasources = yield registry.get_data_sources (); + + stdout.printf ("\nThe following data-sources are registered:\n"); + for (int i = 0; i < datasources.length; ++i) + { + DataSource datasource = datasources[i]; + stdout.printf (" - %s [%s]\n", datasource.name, + (datasource.enabled) ? "enabled" : "disabled"); + } +} + +int main () +{ + loop = new MainLoop(); + + do_stuff(); + + loop.run (); + + return 0; +} diff --git a/examples/vala/get-events-with-id.vala b/examples/vala/get-events-with-id.vala new file mode 100644 index 00000000..32682372 --- /dev/null +++ b/examples/vala/get-events-with-id.vala @@ -0,0 +1,28 @@ +int main () +{ + var ids = new Array<uint32>(); + uint32 id1 = 2100000; + uint32 id2 = 222; + ids.append_val(id1); + ids.append_val(id2); + + var loop = new MainLoop(); + + Zeitgeist.Log zg = new Zeitgeist.Log (); + zg.get_events (ids, null, (obj, res) => { + GenericArray<Zeitgeist.Event?> events = zg.get_events.end (res); + for (int i = 0; i < events.length; ++i) + { + Zeitgeist.Event event = events[i]; + if (event != null) + stdout.printf ("First subject: %s\n", event.subjects[0].uri); + else + stdout.printf ("Event %d doesn't exist.\n", (int) ids.index (i)); + } + loop.quit(); + }); + + loop.run (); + + return 0; +} diff --git a/examples/vala/insert-events.vala b/examples/vala/insert-events.vala new file mode 100644 index 00000000..aad9393d --- /dev/null +++ b/examples/vala/insert-events.vala @@ -0,0 +1,38 @@ +using Zeitgeist; + +int main () +{ + var mainloop = new MainLoop(MainContext.default ()); + var events = new GenericArray<Event> (); + var ev = new Event (); + var su = new Subject (); + ev.add_subject (su); + events.add (ev); + ev.interpretation = "foo://Interp"; + ev.manifestation = "foo://Manif"; + ev.actor = "app://firefox.desktop"; + + su.uri = "file:///tmp/bar.txt"; + su.interpretation = "foo://TextDoc"; + su.manifestation = "foo://File"; + su.mimetype = "text/plain"; + su.origin = "file:///tmp"; + su.text = "bar.txt"; + su.storage = "bfb486f6-f5f8-4296-8871-0cc749cf8ef7"; + + Zeitgeist.Log.get_default ().insert_events ( + events, null, (log, res) => { + Array<uint32> event_ids; + Zeitgeist.Log zg = (Zeitgeist.Log) log; + try { + event_ids = zg.insert_events.end (res); + } + catch (Error error) { + critical ("Failed to insert events: %s", error.message); + return; + } + mainloop.quit (); + }); + mainloop.run (); + return 0; +} diff --git a/examples/vala/monitor-events.vala b/examples/vala/monitor-events.vala new file mode 100644 index 00000000..ed6feab4 --- /dev/null +++ b/examples/vala/monitor-events.vala @@ -0,0 +1,32 @@ +void on_events_inserted (Zeitgeist.TimeRange tr, Zeitgeist.ResultSet events) +{ + message ("%u events inserted", events.size ()); + + foreach (Zeitgeist.Event event in events) + { + for (int i = 0; i < event.num_subjects (); ++i ) + { + Zeitgeist.Subject subject = event.subjects[i]; + message (" * %s", subject.uri); + } + } +} + +int main () +{ + var loop = new MainLoop(); + + var time_range = new Zeitgeist.TimeRange.anytime (); + var template = new GenericArray<Zeitgeist.Event> (); + + var monitor = new Zeitgeist.Monitor(time_range, template); + Zeitgeist.Log log = new Zeitgeist.Log (); + + monitor.events_inserted.connect (on_events_inserted); + //monitor.events_deleted.connect (on_events_deleted); + + log.install_monitor (monitor); + + loop.run (); + return 0; +} diff --git a/examples/vala/most-recent-events.vala b/examples/vala/most-recent-events.vala new file mode 100644 index 00000000..316e9f43 --- /dev/null +++ b/examples/vala/most-recent-events.vala @@ -0,0 +1,28 @@ +using Zeitgeist; + +int main () +{ + var loop = new MainLoop(); + var log = new Zeitgeist.Log (); + + var time_range = new TimeRange.anytime (); + var templates = new GenericArray<Event> (); + int num_events = 20; + + log.find_events (time_range, templates, StorageState.ANY, num_events, + ResultType.MOST_RECENT_SUBJECTS, null, (obj, res) => + { + ResultSet events = log.find_events.end (res); + stdout.printf ("%u most recent subjects:", events.size ()); + foreach (Event event in events) + { + stdout.printf (" - %s\n", event.subjects[0].uri); + } + loop.quit(); + } + ); + + loop.run (); + + return 0; +} |