------------------------------------------------------------ revno: 275 committer: Siegfried-Angel Gevatter Pujals <siegfr...@gevatter.com> branch nick: bluebird timestamp: Sun 2011-09-25 15:45:59 +0200 message: Add API enabling extensions to store their state in the database. Use it for blacklist. added: src/extension-store.vala modified: extensions/blacklist.vala extensions/ds-registry.vala extensions/histogram.vala src/Makefile.am src/engine.vala src/extension.vala
-- lp:~zeitgeist/zeitgeist/bluebird https://code.launchpad.net/~zeitgeist/zeitgeist/bluebird Your team Zeitgeist Framework Team is subscribed to branch lp:~zeitgeist/zeitgeist/bluebird. To unsubscribe from this branch go to https://code.launchpad.net/~zeitgeist/zeitgeist/bluebird/+edit-subscription
=== modified file 'extensions/blacklist.vala' --- extensions/blacklist.vala 2011-09-24 16:04:43 +0000 +++ extensions/blacklist.vala 2011-09-25 13:45:59 +0000 @@ -42,6 +42,47 @@ [DBus (signature = "s(asassay)")] Variant event_template); } + namespace BlacklistTemplates + { + private const string SIG_BLACKLIST = "a{s("+Utils.SIG_EVENT+")}"; + + private static HashTable<string, Event> from_variant ( + Variant templates_variant) + { + var blacklist = new HashTable<string, Event> (str_hash, str_equal); + + assert (templates_variant.get_type_string () == SIG_BLACKLIST); + foreach (Variant template_variant in templates_variant) + { + VariantIter iter = template_variant.iterator (); + string template_id = iter.next_value ().get_string (); + // FIXME: throw exception upon error instead of aborting + Event template = new Event.from_variant (iter.next_value ()); + blacklist.insert (template_id, template); + } + + return blacklist; + } + + public static Variant to_variant (HashTable<string, Event> blacklist) + { + var vb = new VariantBuilder (new VariantType (SIG_BLACKLIST)); + { + var iter = HashTableIter<string, Event> (blacklist); + string template_id; + Event event_template; + while (iter.next (out template_id, out event_template)) + { + vb.open (new VariantType ("{s("+Utils.SIG_EVENT+")}")); + vb.add ("s", template_id); + vb.add_value (event_template.to_variant ()); + vb.close (); + } + } + return vb.end (); + } + } + class Blacklist: Extension, RemoteBlacklist { private HashTable<string, Event> blacklist; @@ -54,9 +95,13 @@ construct { - blacklist = new HashTable<string, Event> (str_hash, str_equal); - - // FIXME: load blacklist from file + // Restore previous blacklist from database, or create an empty one + Variant? templates = retrieve_config ("blacklist", + BlacklistTemplates.SIG_BLACKLIST); + if (templates != null) + blacklist = BlacklistTemplates.from_variant (templates); + else + blacklist = new HashTable<string, Event> (str_hash, str_equal); // This will be called after bus is acquired, so it shouldn't block try @@ -71,6 +116,10 @@ } } + public override string get_name () { + return "blacklist"; + } + public override void unload () { try @@ -92,7 +141,8 @@ private void flush () { - // FIXME: write to file. + Variant v = BlacklistTemplates.to_variant (blacklist); + store_config ("blacklist", v); } public override void pre_insert_events (GenericArray<Event?> events, @@ -130,20 +180,7 @@ public Variant get_templates () { - var vb = new VariantBuilder (new VariantType ("a{s("+Utils.SIG_EVENT+")}")); - { - var iter = HashTableIter<string, Event> (blacklist); - string template_id; - Event event_template; - while (iter.next (out template_id, out event_template)) - { - vb.open (new VariantType ("{s("+Utils.SIG_EVENT+")}")); - vb.add ("s", template_id); - vb.add_value (event_template.to_variant ()); - vb.close (); - } - } - return vb.end (); + return BlacklistTemplates.to_variant (blacklist); } } === modified file 'extensions/ds-registry.vala' --- extensions/ds-registry.vala 2011-09-16 09:15:06 +0000 +++ extensions/ds-registry.vala 2011-09-25 13:45:59 +0000 @@ -140,7 +140,7 @@ var connection = Bus.get_sync (BusType.SESSION, null); registration_id = connection.register_object<RemoteRegistry> ( "/org/gnome/zeitgeist/data_source_registry", this); - + connection.signal_subscribe ("org.freedesktop.DBus", "org.freedesktop.DBus", "NameOwnerChanged", "/org/freedesktop/DBus", null, 0, @@ -199,6 +199,12 @@ // gobject.timeout_add(DISK_WRITE_TIMEOUT, self._write_to_disk) } + + public override string get_name () + { + return "data-source-registry"; + } + public override void unload () { try === modified file 'extensions/histogram.vala' --- extensions/histogram.vala 2011-09-05 16:19:38 +0000 +++ extensions/histogram.vala 2011-09-25 13:45:59 +0000 @@ -49,6 +49,11 @@ } } + public override string get_name () + { + return "histogram"; + } + public override void unload () { try === modified file 'src/Makefile.am' --- src/Makefile.am 2011-09-15 17:57:10 +0000 +++ src/Makefile.am 2011-09-25 13:45:59 +0000 @@ -32,6 +32,7 @@ remote.vala \ extension.vala \ extension-collection.vala \ + extension-store.vala \ notify.vala \ sql.vala \ utils.vala \ === modified file 'src/engine.vala' --- src/engine.vala 2011-09-19 14:12:03 +0000 +++ src/engine.vala 2011-09-25 13:45:59 +0000 @@ -35,6 +35,7 @@ { public Zeitgeist.SQLite.ZeitgeistDatabase database { get; private set; } + public ExtensionStore extension_store; private ExtensionCollection extension_collection; private unowned Sqlite.Database db; @@ -57,6 +58,7 @@ mimetypes_table = new TableLookup (database, "mimetype"); actors_table = new TableLookup (database, "actor"); + extension_store = new ExtensionStore (this); extension_collection = new ExtensionCollection (this); } @@ -701,7 +703,7 @@ manifestations_table.get_id (event.manifestation)); retrieval_stmt.bind_int64 (4, actors_table.get_id (event.actor)); - if ((rc = retrieval_stmt.step()) != Sqlite.ROW) { + if ((rc = retrieval_stmt.step ()) != Sqlite.ROW) { warning ("SQL error: %d, %s\n", rc, db.errmsg ()); return 0; } === added file 'src/extension-store.vala' --- src/extension-store.vala 1970-01-01 00:00:00 +0000 +++ src/extension-store.vala 2011-09-25 13:45:59 +0000 @@ -0,0 +1,118 @@ +/* extension-store.vala + * + * Copyright © 2011 Collabora Ltd. + * By Siegfried-Angel Gevatter Pujals <siegfr...@gevatter.com> + * + * 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 2.1 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 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 <http://www.gnu.org/licenses/>. + * + */ + +using Zeitgeist; + +namespace Zeitgeist +{ + public class ExtensionStore : Object + { + + private Zeitgeist.SQLite.ZeitgeistDatabase database; + private unowned Sqlite.Database db; + private Sqlite.Statement storage_stmt; + private Sqlite.Statement retrieval_stmt; + + public ExtensionStore (Zeitgeist.Engine engine) { + database = engine.database; + db = database.database; + prepare_queries (); + } + + private void prepare_queries () + throws EngineError + { + int rc; + string sql; + + // Prepare storage query + sql = """ + INSERT OR REPLACE INTO extensions_conf ( + extension, key, value + ) VALUES ( + ?, ?, ? + )"""; + rc = database.database.prepare_v2 (sql, -1, out storage_stmt); + database.assert_query_success (rc, "Storage query error"); + + // Prepare retrieval query + sql = """ + SELECT value + FROM extensions_conf + WHERE extension=? AND key=? + """; + rc = database.database.prepare_v2 (sql, -1, out retrieval_stmt); + database.assert_query_success (rc, "Retrieval query error"); + } + + /** + * Store the given Variant under the given (extension, key) + * identifier, replacing any previous value. + */ + public void store (string extension, string key, Variant data) + { + int rc; + storage_stmt.reset (); + storage_stmt.bind_text (1, extension); + storage_stmt.bind_text (2, key); + storage_stmt.bind_blob (3, data.get_data (), (int) data.get_size ()); + + if ((rc = storage_stmt.step ()) != Sqlite.DONE) + warning ("SQL error: %d, %s", rc, db.errmsg ()); + } + + /** + * Retrieve a previously stored value. + */ + public Variant? retrieve(string extension, string key, VariantType format) + { + retrieval_stmt.reset (); + retrieval_stmt.bind_text (1, extension); + retrieval_stmt.bind_text (2, key); + + int rc = retrieval_stmt.step (); + if (rc != Sqlite.ROW) + { + if (rc != Sqlite.DONE) + warning ("SQL error: %d, %s", rc, db.errmsg ()); + return null; + } + + unowned uchar[] blob; + blob = (uchar[]) retrieval_stmt.column_blob (0); + blob.length = retrieval_stmt.column_bytes (0); + + Variant? data = null; + if (blob != null) + { + ByteArray byte_array = new ByteArray.sized (blob.length); + byte_array.append (blob); + + data = Variant.new_from_data<ByteArray> (format, + byte_array.data, false, byte_array); + } + + retrieval_stmt.reset (); + return data; + } + + } +} +// vim:expandtab:ts=4:sw=4 === modified file 'src/extension.vala' --- src/extension.vala 2011-09-17 10:41:24 +0000 +++ src/extension.vala 2011-09-25 13:45:59 +0000 @@ -2,6 +2,8 @@ * * Copyright © 2011 Manish Sinha <manishsi...@ubuntu.com> * Copyright © 2011 Michal Hruby <michal....@gmail.com> + * Copyright © 2011 Collabora Ltd. + * By Siegfried-Angel Gevatter Pujals <siegfr...@gevatter.com> * * 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 @@ -36,6 +38,8 @@ { public unowned Engine engine { get; construct set; } + public abstract string get_name(); + /** * This method gets called before Zeitgeist stops. * @@ -122,6 +126,17 @@ public virtual void post_delete_events (uint32[] ids, BusName? sender) { } + + protected void store_config (string key, Variant data) + { + engine.extension_store.store (get_name (), key, data); + } + + protected Variant? retrieve_config (string key, string format) + { + VariantType type = new VariantType(format); + return engine.extension_store.retrieve (get_name (), key, type); + } } [CCode (has_target = false)]
_______________________________________________ Mailing list: https://launchpad.net/~zeitgeist Post to : zeitgeist@lists.launchpad.net Unsubscribe : https://launchpad.net/~zeitgeist More help : https://help.launchpad.net/ListHelp