/* * Zeitgeist * * Copyright (C) 2010 Michal Hruby * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Authored by Michal Hruby * */ using Zeitgeist; [DBus (name = "org.gnome.zeitgeist.datahub")] public interface DataHubService : Object { public abstract string[] get_data_providers () throws GLib.Error; } public class DataHub : Object, DataHubService { private Zeitgeist.Log zg_log; private Zeitgeist.DataSourceRegistry registry; private MainLoop main_loop; private List providers; private List sources_info; // list got from ZG's Registry private GenericArray queued_events; private uint idle_id = 0; public int return_code { get; private set; default = 0; } public DataHub () { GLib.Object (); } construct { providers = new List (); sources_info = new List (); queued_events = new GenericArray (); main_loop = new MainLoop (); zg_log = new Zeitgeist.Log (); zg_log.notify["connected"].connect (() => { if (!zg_log.is_connected) { debug ("Zeitgeist-daemon disappeared from the bus, exitting..."); quit (); } }); registry = new DataSourceRegistry (); } private void data_source_registered (DataSource ds) { unowned List iter = sources_info; while (iter != null) { if (iter.data.unique_id == ds.unique_id) { break; } iter = iter.next; } if (iter != null) { iter.data = ds; } else { sources_info.prepend (ds); } } private async void start_data_providers () throws Error { try { registry.source_registered.connect (data_source_registered); var sources = yield registry.get_data_sources (null); for (uint i=0; i ()); // FIXME: templates! try { enabled = yield registry.register_data_source (ds, null); } catch (GLib.Error reg_err) { warning ("%s", reg_err.message); } } prov.items_available.connect (this.items_available); if (enabled) { prov.last_timestamp = timestamp; prov.start (); } } } private void items_available (DataProvider prov, GenericArray events) { if (!prov.enabled) return; events.foreach ((e) => { queued_events.add (e); }); if (queued_events.length > 0 && idle_id == 0) { idle_id = Idle.add (() => { insert_events (); idle_id = 0; return false; }); } } private void insert_events () { debug ("Inserting %u events", queued_events.length); batch_insert_events.begin (); queued_events = new GenericArray (); } protected async void batch_insert_events () { // copy the events to GenericArray (with a ref on them) GenericArray all_events = new GenericArray (); queued_events.foreach ((e) => { all_events.add (e); }); while (all_events.length > 0) { uint elements_pushed = uint.min ((uint) all_events.length, 100); GenericArray ptr_arr = new GenericArray (); for (uint i=0; i { conn.register_object (OBJECT_PATH, (DataHubService) this); }, () => { start_data_providers.begin (); }, () => { warning ("Unable to get name \"org.gnome.zeitgeist.datahub\"" + " on the bus!"); this.return_code = 1; this.quit (); } ); main_loop.run (); } protected void quit () { // dispose all providers providers = new List (); main_loop.quit (); } public string[] get_data_source_actors (bool only_enabled = true) { string[] actors = {}; foreach (unowned DataSource src in sources_info) { if (only_enabled && !src.enabled) continue; var template_arr = src.event_templates; if (template_arr != null) { for (uint i=0; i