Created
April 13, 2013 12:12
-
-
Save voldyman/5378147 to your computer and use it in GitHub Desktop.
This is test code for libpeas using vala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace PApp { | |
void main () { | |
print ("Hello\n"); | |
for (;;) { | |
string? r = stdin.read_line (); | |
if (r != null && r != "quit") { | |
//do_something (r); | |
Plugins.run_cmd (r); | |
} else { | |
return; | |
} | |
} | |
} | |
void do_something (string read) { | |
print ("read: "+read+"\n"); | |
} | |
private struct Command { | |
string trigger; | |
string plugin; | |
} | |
namespace Plugins { | |
private Peas.Engine engine; | |
private Peas.ExtensionSet exten_set; | |
private SList<Command?> commands; | |
internal void init () { | |
engine = Peas.Engine.get_default (); | |
engine.add_search_path ("./plugins", null); | |
exten_set = new Peas.ExtensionSet (engine, typeof(Plugin)); | |
exten_set.extension_added.connect(on_extension_added); | |
exten_set.extension_removed.connect(on_extension_removed); | |
} | |
public bool load (string name) { | |
var plug_info = engine.get_plugin_info (name); | |
if (plug_info == null) { | |
warning ("Plugin Not found"); | |
return false; | |
} | |
return engine.try_load_plugin (plug_info); | |
} | |
public bool unload (string name) { | |
var plug_info = engine.get_plugin_info (name); | |
if (plug_info == null) { | |
warning ("Plugin Not found"); | |
return false; | |
} | |
return engine.try_unload_plugin (plug_info); | |
} | |
internal void run_cmd (string hook) { | |
foreach (var cmd in commands) { | |
if (cmd.trigger == hook) { | |
run_plug_cb (cmd.plugin, hook); | |
} | |
} | |
} | |
public void run_plug_cb (string plug_name, string line) { | |
var plugin_info = engine.get_plugin_info (plug_name); | |
var exten = exten_set.get_extension (plugin_info) as Plugin; | |
exten.do_something (line); | |
} | |
internal void register_command (string hook, string plug) { | |
var cmd = Command () { | |
trigger = hook, | |
plugin = plug | |
}; | |
commands.append (cmd); | |
} | |
private void on_extension_added(Peas.ExtensionSet extension_set, Peas.PluginInfo info, GLib.Object exten) { | |
var plugin = exten as Plugin; | |
try { | |
plugin.init(); | |
} catch (Error e) { | |
warning(e.message); | |
} | |
} | |
private void on_extension_removed(Peas.ExtensionSet extension_set, Peas.PluginInfo info, GLib.Object exten) { | |
commands.foreach((data) => { | |
if (data.plugin == info.get_module_name()) | |
commands.remove(data); | |
}); | |
} | |
} | |
public interface Plugin : Peas.ExtensionBase { | |
public abstract void init (); | |
protected void register_str (string str) { | |
var plugin_info = get_plugin_info(); | |
var plugin = plugin_info.get_module_name(); | |
Plugins.register_command(plugin, str); | |
} | |
public abstract void do_something (string str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment