using Gee; using Qlite; using Xmpp; using Xmpp.Xep.ServiceDiscovery; namespace Dino { public class EntityCapabilitiesStorage : Xep.EntityCapabilities.Storage, Object { private Database db; private HashMap> features_cache = new HashMap>(); private HashMap identity_cache = new HashMap(); public EntityCapabilitiesStorage(Database db) { this.db = db; } public void store_features(string entity, Gee.List features) { if (features_cache.contains(entity)) return; foreach (string feature in features) { db.entity_feature.insert() .value(db.entity_feature.entity, entity) .value(db.entity_feature.feature, feature) .perform(); } } public void store_identities(string entity, Gee.Set identities) { foreach (Identity identity in identities) { if (identity.category == Identity.CATEGORY_CLIENT) { db.entity_identity.insert() .value(db.entity_identity.entity, entity) .value(db.entity_identity.category, identity.category) .value(db.entity_identity.type, identity.type_) .value(db.entity_identity.name, identity.name) .perform(); return; } } } public Gee.List get_features(string entity) { Gee.List? features = features_cache[entity]; if (features != null) { return features; } features = new ArrayList(); foreach (Row row in db.entity_feature.select({db.entity_feature.feature}).with(db.entity_feature.entity, "=", entity)) { features.add(row[db.entity_feature.feature]); } features_cache[entity] = features; return features; } public Identity? get_identities(string entity) { Identity? identity = identity_cache[entity]; if (identity != null) { return identity; } RowOption row = db.entity_identity.select().with(db.entity_identity.entity, "=", entity).single().row(); if (row.is_present()) { identity = new Identity(row[db.entity_identity.category], row[db.entity_identity.type], row[db.entity_identity.name]); } identity_cache[entity] = identity; return identity; } } }