-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplugin-engine.vala
137 lines (110 loc) · 3.78 KB
/
plugin-engine.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
errordomain PluginError {
UNKNOWN_PLUGIN,
LOADING_FAILED
}
class PluginEngine<T> : Object {
[Compact]
class Plugin {
public unowned Peas.PluginInfo info;
public List<Object> extensions;
public Plugin (Peas.PluginInfo info) {
this.info = info;
}
}
class PluginTable {
HashTable<string, Plugin> plugins;
static string get_key (Peas.PluginInfo info) {
return info.get_name ();
}
public bool has_name (string name) {
return name in plugins;
}
public bool contains (Peas.PluginInfo info) {
return has_name (get_key (info));
}
public unowned Plugin get_for_name (string name)
requires (has_name (name))
{
return plugins[name];
}
public unowned Plugin @get (Peas.PluginInfo info)
requires (contains (info))
{
return this.get_for_name (get_key (info));
}
public void add (Peas.PluginInfo info) {
if (info in this) {
warn_if_fail (this[info].info == info);
return;
}
plugins[get_key (info)] = new Plugin (info);
}
public void remove (Peas.PluginInfo info)
requires (contains (info))
{
warn_if_fail (this[info].extensions.length () == 0);
plugins.remove (get_key (info));
}
public PluginTable () {
plugins = new HashTable<string, Plugin> (str_hash, str_equal);
}
}
Peas.Engine engine;
Peas.ExtensionSet extension_set;
PluginTable plugins;
public void add_search_path (string module_dir, string? data_dir = null) {
engine.add_search_path (module_dir, data_dir);
}
public void prepend_search_path (
string module_dir,
string? data_dir = null
) {
engine.prepend_search_path (module_dir, data_dir);
}
public new unowned List<T> @get (string plugin_name) throws PluginError {
if (!plugins.has_name (plugin_name)) {
unowned Peas.PluginInfo? info =
engine.get_plugin_info (plugin_name);
if (info == null)
throw new PluginError.UNKNOWN_PLUGIN (
"Unknown plugin '%s'", plugin_name
);
plugins.add ((!) info);
}
unowned Plugin plugin = plugins.get_for_name (plugin_name);
if (!plugin.info.is_loaded () && !engine.try_load_plugin (plugin.info))
{
try {
plugin.info.is_available ();
return_val_if_reached (null); // above should throw
} catch (Error e) {
if (e is Peas.PluginInfoError)
throw new PluginError.LOADING_FAILED (e.message);
return_val_if_reached (null);
}
}
return plugin.extensions;
}
public unowned List<Peas.PluginInfo> get_all () {
return engine.get_plugin_list ();
}
public PluginEngine ()
requires (typeof (T).is_a (typeof (Object)))
requires (typeof (T).is_interface ())
{
engine = new Peas.Engine.with_nonglobal_loaders ();
extension_set = new Peas.ExtensionSet (engine, typeof (T));
plugins = new PluginTable ();
extension_set.extension_added.connect ((info, extension) => {
plugins.add (info);
plugins[info].extensions.append (extension);
});
extension_set.extension_removed.connect ((info, extension) => {
if (!plugins.contains (info))
return;
plugins[info].extensions.remove_all (extension);
if (plugins[info].extensions.length () == 0)
plugins.remove (info);
});
}
}