From 4c48bdc07291f55d7320721a5b0a29c092f7daa0 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Sat, 11 Mar 2017 01:25:45 +0100 Subject: Improve Plugin API (allow to move encryption into plugin) --- libdino/src/plugin/interfaces.vala | 16 +++++++++ libdino/src/plugin/loader.vala | 66 ++++++++++++++++++++++++++++++++++++++ libdino/src/plugin/registry.vala | 20 ++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 libdino/src/plugin/interfaces.vala create mode 100644 libdino/src/plugin/loader.vala create mode 100644 libdino/src/plugin/registry.vala (limited to 'libdino/src/plugin') diff --git a/libdino/src/plugin/interfaces.vala b/libdino/src/plugin/interfaces.vala new file mode 100644 index 00000000..19873003 --- /dev/null +++ b/libdino/src/plugin/interfaces.vala @@ -0,0 +1,16 @@ +namespace Dino.Plugins { + +public interface RootInterface : Object { + public abstract void registered(Dino.Application app); + + public abstract void shutdown(); +} + +public interface EncryptionListEntry : Object { + public abstract Entities.Encryption encryption { get; } + public abstract string name { get; } + + public abstract bool can_encrypt(Entities.Conversation conversation); +} + +} \ No newline at end of file diff --git a/libdino/src/plugin/loader.vala b/libdino/src/plugin/loader.vala new file mode 100644 index 00000000..43ce0801 --- /dev/null +++ b/libdino/src/plugin/loader.vala @@ -0,0 +1,66 @@ +namespace Dino.Plugins { + +public errordomain Error { + NOT_SUPPORTED, + UNEXPECTED_TYPE, + NO_REGISTRATION_FUNCTION, + FAILED +} + +private class Info : Object { + public Module module; + public Type gtype; + + public Info(Type type, owned Module module) { + this.module = (owned) module; + this.gtype = type; + } +} + +public class Loader : Object { + [CCode (has_target = false)] + private delegate Type RegisterPluginFunction (Module module); + + private RootInterface[] plugins = new RootInterface[0]; + private Info[] infos = new Info[0]; + + public RootInterface load(string name, Dino.Application app) throws Error { + if (Module.supported () == false) { + throw new Error.NOT_SUPPORTED ("Plugins are not supported"); + } + + Module module = Module.open ("plugins/" + name, ModuleFlags.BIND_LAZY); + if (module == null) { + throw new Error.FAILED (Module.error ()); + } + + void* function; + module.symbol ("register_plugin", out function); + if (function == null) { + throw new Error.NO_REGISTRATION_FUNCTION ("register_plugin () not found"); + } + + RegisterPluginFunction register_plugin = (RegisterPluginFunction) function; + Type type = register_plugin (module); + if (type.is_a (typeof (RootInterface)) == false) { + throw new Error.UNEXPECTED_TYPE ("Unexpected type"); + } + + Info info = new Plugins.Info (type, (owned) module); + infos += info; + + RootInterface plugin = (RootInterface) Object.new (type); + plugins += plugin; + plugin.registered (app); + + return plugin; + } + + public void shutdown() { + foreach (RootInterface p in plugins) { + p.shutdown(); + } + } +} + +} \ No newline at end of file diff --git a/libdino/src/plugin/registry.vala b/libdino/src/plugin/registry.vala new file mode 100644 index 00000000..8c75784e --- /dev/null +++ b/libdino/src/plugin/registry.vala @@ -0,0 +1,20 @@ +using Gee; + +namespace Dino.Plugins { + +public class Registry { + internal ArrayList encryption_list_entries = new ArrayList(); + + public bool register_encryption_list_entry(EncryptionListEntry entry) { + lock(encryption_list_entries) { + foreach(var e in encryption_list_entries) { + if (e.encryption == entry.encryption) return false; + } + encryption_list_entries.add(entry); + encryption_list_entries.sort((a,b) => b.name.collate(a.name)); + return true; + } + } +} + +} \ No newline at end of file -- cgit v1.2.3-70-g09d2