From bf0210888b2c1a7eb26656fdcaec4e0559569772 Mon Sep 17 00:00:00 2001 From: Chip Black Date: Mon, 8 Jun 2009 03:53:14 -0500 Subject: [PATCH] Add plugin deinitialization, configuration saving 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 | 55 +++++++++++++++++++++++++++++++++++++++++++---- PluginManager.cs | 6 ++++++ WebThing.cs | 2 +- WebThingPlugin.cs | 8 +++++++ plugins/Vimish.cs | 4 ++++ 5 files changed, 70 insertions(+), 5 deletions(-) diff --git a/Config.cs b/Config.cs index 49e7203..ac70dc8 100644 --- a/Config.cs +++ b/Config.cs @@ -7,6 +7,7 @@ using System.Text.RegularExpressions; namespace bytex64.WebThing { public class Config { public static List ConfigPath; + public static string ConfigPathOut = null; public static Dictionary Options; public static Dictionary> PluginOptions; @@ -29,12 +30,17 @@ namespace bytex64.WebThing { ConfigPath = new List(); 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(); + } } } diff --git a/PluginManager.cs b/PluginManager.cs index 55f4d21..051c6ab 100644 --- a/PluginManager.cs +++ b/PluginManager.cs @@ -38,5 +38,11 @@ namespace bytex64.WebThing { Plugins[key].InitWebView(view); } } + + public void Deinit() { + foreach (WebThingPlugin p in Plugins.Values) { + p.Deinit(wt); + } + } } } diff --git a/WebThing.cs b/WebThing.cs index fd2eb99..08cfb1b 100644 --- a/WebThing.cs +++ b/WebThing.cs @@ -105,7 +105,7 @@ namespace bytex64.WebThing { } public void Quit() { - // TODO: Create a way of shutting down plugins + Plugins.Deinit(); Application.Quit(); } diff --git a/WebThingPlugin.cs b/WebThingPlugin.cs index 6ad3e9d..6f23e9d 100644 --- a/WebThingPlugin.cs +++ b/WebThingPlugin.cs @@ -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) {} diff --git a/plugins/Vimish.cs b/plugins/Vimish.cs index 968fae9..4dddb6a 100644 --- a/plugins/Vimish.cs +++ b/plugins/Vimish.cs @@ -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) { -- 2.25.1