Add plugin deinitialization, configuration saving
authorChip Black <bytex64@bytex64.net>
Mon, 8 Jun 2009 08:53:14 +0000 (03:53 -0500)
committerChip Black <bytex64@bytex64.net>
Mon, 8 Jun 2009 08:53:14 +0000 (03:53 -0500)
Added a call to deinitialize all plugins on exit.  Added functions to
Config to save configuration to files both on a global basis and a
plugin basis.  Modified Vimish to save its options on exit.

Config.cs
PluginManager.cs
WebThing.cs
WebThingPlugin.cs
plugins/Vimish.cs

index 49e7203..ac70dc8 100644 (file)
--- a/Config.cs
+++ b/Config.cs
@@ -7,6 +7,7 @@ using System.Text.RegularExpressions;
 namespace bytex64.WebThing {
     public class Config {
         public static List<string> ConfigPath;
+        public static string ConfigPathOut = null;
 
         public static Dictionary<string,string> Options;
         public static Dictionary<string,Dictionary<string,string>> PluginOptions;
@@ -29,12 +30,17 @@ namespace bytex64.WebThing {
             ConfigPath = new List<string>();
             ConfigPath.Add("/etc/WebThing");
             if (Env.Contains("HOME")) {
-                ConfigPath.Add(Env["HOME"] + "/.config/WebThing");
-                ConfigPath.Add(Env["HOME"] + "/.WebThing");
+                string homepath = Env["HOME"] + "/.config/WebThing";
+                ConfigPath.Add(homepath);
+                if (Directory.Exists(homepath))
+                    ConfigPathOut = homepath;
             }
             string LocalAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
             if (LocalAppData != "") {
-                ConfigPath.Add(LocalAppData + "/WebThing");
+                string homepath = LocalAppData + "/WebThing";
+                ConfigPath.Add(homepath);
+                if (Directory.Exists(homepath))
+                    ConfigPathOut = homepath;
             }
         }
 
@@ -142,7 +148,7 @@ namespace bytex64.WebThing {
         public static void LoadFile(string filename) {
             TextReader f;
             try {
-                f = new StreamReader(File.OpenRead(filename));
+                f = new StreamReader(filename);
             } catch (FileNotFoundException) {
                 Console.WriteLine("Could not open configuration file {0}", filename);
                 return;
@@ -179,5 +185,46 @@ namespace bytex64.WebThing {
 
             f.Close();
         }
+
+        public static void SaveFile(string filename) {
+            TextWriter f;
+            try {
+                f = new StreamWriter(filename);
+            } catch (IOException) {
+                Console.WriteLine("Could not save configuration to {0}", filename);
+                return;
+            }
+
+            foreach (string key in Options.Keys) {
+                if (CommandLineOptions.Contains(key)) continue;
+                f.WriteLine("{0} = {1}", key, Options[key]);
+            }
+
+            foreach (string plugin in PluginOptions.Keys) {
+                foreach (string key in PluginOptions[plugin].Keys) {
+                    string pkey = String.Format("{0}.{1}", plugin, key);
+                    if (CommandLineOptions.Contains(pkey)) continue;
+                    f.WriteLine("{0} = {1}", pkey, PluginOptions[plugin][key]);
+                }
+            }
+
+            f.Close();
+        }
+
+        public static void SaveFile(WebThingPlugin p, string filename) {
+            string plugin_name = p.GetType().FullName;
+            SaveFile(plugin_name, filename);
+        }
+
+        public static void SaveFile(string plugin_name, string filename) {
+            // TODO: Throw exception if plugin does not exist
+            TextWriter f = new StreamWriter(filename);
+
+            foreach (string key in PluginOptions[plugin_name].Keys) {
+                f.WriteLine("{0}.{1} = {2}", plugin_name, key, PluginOptions[plugin_name][key]);
+            }
+
+            f.Close();
+        }
     }
 }
index 55f4d21..051c6ab 100644 (file)
@@ -38,5 +38,11 @@ namespace bytex64.WebThing {
                 Plugins[key].InitWebView(view);
             }
         }
+
+        public void Deinit() {
+            foreach (WebThingPlugin p in Plugins.Values) {
+                p.Deinit(wt);
+            }
+        }
     }
 }
index fd2eb99..08cfb1b 100644 (file)
@@ -105,7 +105,7 @@ namespace bytex64.WebThing {
         }
 
         public void Quit() {
-            // TODO: Create a way of shutting down plugins
+            Plugins.Deinit();
             Application.Quit();
         }
 
index 6ad3e9d..6f23e9d 100644 (file)
@@ -17,6 +17,14 @@ namespace bytex64.WebThing {
             }
         }
 
+        // Convenience function to easily save plugin configuration
+        protected void SaveConfig() {
+            if (Config.ConfigPathOut == null) return;
+
+            string plugin_name = this.GetType().FullName;
+            Config.SaveFile(plugin_name, String.Format("{0}/{1}.conf", Config.ConfigPathOut, plugin_name));
+        }
+
         // Plugin life cycle
         public virtual void Init(WebThing wt) {}
         public virtual void Deinit(WebThing wt) {}
index 968fae9..4dddb6a 100644 (file)
@@ -28,6 +28,10 @@ public class Vimish : WebThingPlugin {
         wv.KeyPressEvent += WebView_KeyPress;
     }
 
+    public override void Deinit(WebThing wt) {
+        SaveConfig();
+    }
+
     private void Window_KeyPress(object o, KeyPressEventArgs e) {
         if ((Gdk.ModifierType.ControlMask & e.Event.State) != 0) {
             switch(e.Event.Key) {