commit:80865e6b884a4bb289cb4a835a6fa66b3529c872
author:Chip Black
committer:Chip Black
date:Wed Jun 3 04:10:26 2009 -0500
parents:1bff29702cf8ce8c1952d22243e4dc7a9df74105
Formalized plugin interface
diff --git a/Makefile b/Makefile
line changes: +2/-2
index 48a29e5..afeea07
--- 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 $@

diff --git a/WebThing.cs b/WebThing.cs
line changes: +11/-4
index 678194b..523a160
--- a/WebThing.cs
+++ b/WebThing.cs
@@ -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) {

diff --git a/plugins/DefaultPage.cs b/plugins/DefaultPage.cs
line changes: +2/-2
index 3de2410..38ed672
--- a/plugins/DefaultPage.cs
+++ b/plugins/DefaultPage.cs
@@ -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 {

diff --git a/plugins/FFNav.cs b/plugins/FFNav.cs
line changes: +2/-2
index 4f22b63..f8ba39c
--- a/plugins/FFNav.cs
+++ b/plugins/FFNav.cs
@@ -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;
     }

diff --git a/plugins/LoadProgress.cs b/plugins/LoadProgress.cs
line changes: +2/-2
index 9dc6838..a4be542
--- a/plugins/LoadProgress.cs
+++ b/plugins/LoadProgress.cs
@@ -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);
     }

diff --git a/plugins/Vimish.cs b/plugins/Vimish.cs
line changes: +2/-2
index 3b4089d..7acdad2
--- a/plugins/Vimish.cs
+++ b/plugins/Vimish.cs
@@ -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;