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.
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;
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;
}
}
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;
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();
+ }
}
}
Plugins[key].InitWebView(view);
}
}
+
+ public void Deinit() {
+ foreach (WebThingPlugin p in Plugins.Values) {
+ p.Deinit(wt);
+ }
+ }
}
}
}
public void Quit() {
- // TODO: Create a way of shutting down plugins
+ Plugins.Deinit();
Application.Quit();
}
}
}
+ // 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) {}
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) {