Flesh out life cycle of a plugin
[WebThing.git] / PluginManager.cs
1 using System;
2 using System.Reflection;
3 using System.Collections.Generic;
4 using WebKit;
5
6 namespace bytex64.WebThing {
7     public class PluginManager {
8         public Dictionary<string,WebThingPlugin> Plugins;
9         
10         private WebThing wt;
11
12         public PluginManager(WebThing wt) {
13             Plugins = new Dictionary<string,WebThingPlugin>();
14             this.wt = wt;
15         }
16
17         public void Load() {
18             foreach (string plugin in Config.Plugins) {
19                 LoadPlugin(plugin);
20             }
21         }
22
23         public void LoadPlugin(string assemblyname) {
24             Assembly a = Assembly.LoadFile(Environment.GetEnvironmentVariable("WEBTHING_HOME") + "/plugins/" + assemblyname + ".dll");
25             Type[] types = a.GetTypes();
26             foreach (Type t in types) {
27                 if (t.IsSubclassOf(typeof(WebThingPlugin))) {
28                     WebThingPlugin p = (WebThingPlugin) a.CreateInstance(t.FullName, false, BindingFlags.ExactBinding, null, null, null, null);
29                     p.Init(wt);
30                     Plugins[t.FullName] = p;
31                     Console.WriteLine("Successfully loaded {0}", t.FullName);
32                 }
33             }
34         }
35
36         public void WebViewSetup(WebView view) {
37             foreach (string key in Plugins.Keys) {
38                 Plugins[key].InitWebView(view);
39             }
40         }
41     }
42 }