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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/* Code based on Marlin Plugin Manager
* Copyright (C) 2011 Lucas Baudin <xapantu@gmail.com>
*
* Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org> (from Rygel)
*
* This file is part of Marlin.
*
* Marlin is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Marlin 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 General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public class Gazette.PluginManager : GLib.Object
{
public signal void plugin_enabled (Plugin.Base plugin);
delegate Gazette.Plugin.Base ModuleInitFunc ();
Gee.HashMap<string,Gazette.Plugin.Base> plugin_hash;
Settings settings;
string settings_field;
string plugin_dir;
Gee.HashMap <string,string> names_paths;
public PluginManager (Settings settings, string field, string plugin_dir) {
settings_field = field;
this.settings = settings;
this.plugin_dir = plugin_dir;
this.settings.changed.connect(update_plugins);
plugin_hash = new Gee.HashMap<string,Gazette.Plugin.Base>();
names_paths = new Gee.HashMap<string,string>();
}
public Gee.Collection<Gazette.Plugin.Base> get_plugins () {
return plugin_hash.values;
}
public void load_plugins () {
load_modules_from_dir(plugin_dir);
}
private void load_modules_from_dir (string path) {
File dir = File.new_for_path(path);
string attributes = FileAttribute.STANDARD_NAME + "," +
FileAttribute.STANDARD_TYPE;
FileInfo info;
FileEnumerator enumerator;
try
{
enumerator = dir.enumerate_children
(attributes,
FileQueryInfoFlags.NONE);
info = enumerator.next_file ();
while(info != null)
{
string file_name = info.get_name ();
string file_path = Path.build_filename (dir.get_path (), file_name);
if(file_name.has_suffix(".plug"))
{
load_plugin_keyfile(file_path, dir.get_path ());
}
info = enumerator.next_file ();
}
}
catch(Error error)
{
critical ("Error listing contents of folder '%s': %s",
dir.get_path (),
error.message);
}
}
private void load_module (string file_path)
{
Module module = Module.open (file_path, ModuleFlags.BIND_LOCAL);
if (module == null)
{
warning ("Failed to load module from path '%s': %s",
file_path,
Module.error ());
return;
}
void* function;
if (!module.symbol("plugin_init", out function)) {
warning ("Failed to find entry point function '%s' in '%s': %s",
"plugin_init",
file_path,
Module.error ());
return;
}
unowned ModuleInitFunc plugin_init = (ModuleInitFunc) function;
assert (plugin_init != null);
/* We don't want our modules to ever unload */
module.make_resident ();
Gazette.Plugin.Base plug = plugin_init ();
debug ("Loaded module source: '%s', id: '%s'", module.name(), plug.id);
if(plug != null)
plugin_hash.set (plug.id, plug);
}
private void load_plugin_keyfile (string path, string parent) {
var keyfile = new KeyFile();
try
{
keyfile.load_from_file(path, KeyFileFlags.NONE);
string name = keyfile.get_string("Plugin", "Name");
string _path = keyfile.get_string("Plugin", "File");
names_paths[name] = _path;
if (name in settings.get_strv(settings_field))
{
load_module(Path.build_filename(parent, _path));
}
}
catch(Error e)
{
warning("Couldn't open the keyfile: %s, %s", path, e.message);
}
}
public string [] get_available_plugins () {
return names_paths.keys.to_array ();
}
void update_plugins (string field) {
debug(@"Settings field: $field changed");
if (field == settings_field) {
var enabled_plugins = settings.get_strv (settings_field);
var iterator = plugin_hash.map_iterator ();
while (iterator.has_next ()) {
iterator.next ();
var id = iterator.get_key ();
if (!(id in enabled_plugins)) {
disable_plugin(id);
iterator.unset ();
}
}
foreach (var id in enabled_plugins) {
if (!plugin_hash.has_key(id)) {
load_module (plugin_dir + "/" + names_paths[id]);
if (plugin_hash[id] != null)
plugin_enabled (plugin_hash[id]);
}
}
}
}
//FIXME: does not delete the plugin from memory.
void disable_plugin (string id) {
debug(@"Disabling plugin. ID: $id");
var plug = plugin_hash[id];
plug.destroy ();
}
}
|