Formalized plugin interface
authorChip Black <bytex64@bytex64.net>
Wed, 3 Jun 2009 09:10:26 +0000 (04:10 -0500)
committerChip Black <bytex64@bytex64.net>
Wed, 3 Jun 2009 09:10:26 +0000 (04:10 -0500)
Makefile
WebThing.cs
plugins/DefaultPage.cs
plugins/FFNav.cs
plugins/LoadProgress.cs
plugins/Vimish.cs

index 48a29e5..afeea07 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,8 +8,8 @@ all: main.exe plugins
 main.exe: main.cs WebThing.dll SoupSettings.so
        gmcs $(CSFLAGS) -r:WebThing.dll main.cs
 
-WebThing.dll: WebThing.cs
-       gmcs $(CSFLAGS) $(references) -target:library -out:$@ $<
+WebThing.dll: WebThing.cs WebThingPlugin.cs
+       gmcs $(CSFLAGS) $(references) -target:library -out:$@ $^
 
 SoupSettings.so: SoupSettings.c
        $(CC) $(CFLAGS) -shared $(LDFLAGS) $< -o $@
index 678194b..523a160 100644 (file)
@@ -44,7 +44,7 @@ namespace bytex64.WebThing {
         private Gtk.Table WidgetGrid;
         private Gtk.Alignment InteriorOverlay;
 
-        public Dictionary<string,object> Plugins;
+        public Dictionary<string,WebThingPlugin> Plugins;
         public Dictionary<string,string> Options;
         public string[] Arguments;
 
@@ -80,7 +80,7 @@ namespace bytex64.WebThing {
 
             _Window.ShowAll();
 
-            Plugins = new Dictionary<string, object>();
+            Plugins = new Dictionary<string,WebThingPlugin>();
             // TODO: Conf.Get("plugins") instead of hard-coded path?
             using (TextReader f = new StreamReader("plugins.conf")) {
                 string line;
@@ -155,8 +155,15 @@ namespace bytex64.WebThing {
 
         public void LoadPlugin(string assemblyname) {
             Assembly a = Assembly.LoadFile("plugins/" + assemblyname + ".dll");
-            object obj = a.CreateInstance("Plugin", false, BindingFlags.ExactBinding, null, new object[] { this }, null, null);
-            Plugins[assemblyname] = obj;
+            Type[] types = a.GetTypes();
+            foreach (Type t in types) {
+                if (t.GetInterface("WebThingPlugin") != null) {
+                    WebThingPlugin p = (WebThingPlugin) a.CreateInstance(t.FullName, false, BindingFlags.ExactBinding, null, null, null, null);
+                    p.Init(this);
+                    Plugins[t.FullName] = p;
+                    Console.WriteLine("Successfully loaded {0}", t.FullName);
+                }
+            }
         }
 
         public void AttachWidget(Gtk.Widget widget, CompassDirection direction, AttachOptions xoptions, AttachOptions yoptions) {
index 3de2410..38ed672 100644 (file)
@@ -1,8 +1,8 @@
 using System;
 using bytex64.WebThing;
 
-public class Plugin {
-    public Plugin(WebThing wt) {
+public class DefaultPage : WebThingPlugin {
+    public void Init(WebThing wt) {
         if (wt.Arguments.Length > 0) {
             wt.OpenUri(wt.Arguments[0]);
         } else {
index 4f22b63..f8ba39c 100644 (file)
@@ -2,10 +2,10 @@ using System;
 using Gtk;
 using bytex64.WebThing;
 
-public class Plugin {
+public class FFNav : WebThingPlugin {
     WebThing wt;
 
-    public Plugin(WebThing wt) {
+    public void Init(WebThing wt) {
         this.wt = wt;
         wt.WebView.KeyPressEvent += WebView_KeyPress;
     }
index 9dc6838..a4be542 100644 (file)
@@ -79,8 +79,8 @@ public class LoadThrobber : Gtk.DrawingArea {
     }
 }
 
-public class Plugin {
-    public Plugin(WebThing wt) {
+public class LoadThrobberPlugin : WebThingPlugin {
+    public void Init(WebThing wt) {
         LoadThrobber lt = new LoadThrobber(wt);
         wt.AttachWidget(lt, CompassDirection.Interior);
     }
index 3b4089d..7acdad2 100644 (file)
@@ -3,11 +3,11 @@ using System.Text.RegularExpressions;
 using Gtk;
 using bytex64.WebThing;
 
-public class Plugin {
+public class Vimish : WebThingPlugin {
     WebThing wt;
     Gtk.Entry commandline;
 
-    public Plugin(WebThing wt) {
+    public void Init(WebThing wt) {
         this.wt = wt;
         wt.WebView.KeyPressEvent += WebView_KeyPress;