commit:a875e9685526f28076048f31ed4bb279ab46abc6
author:Chip Black
committer:Chip Black
date:Tue Jun 9 01:08:48 2009 -0500
parents:a7173b63fbac8673fdb4a86e59805553b08f7574
Added session management plugin
diff --git a/Config.cs b/Config.cs
line changes: +13/-0
index ac70dc8..3ffd66d
--- a/Config.cs
+++ b/Config.cs
@@ -226,5 +226,18 @@ namespace bytex64.WebThing {
 
             f.Close();
         }
+
+        // Data parsers
+        public static bool ParseBool(string v) {
+            switch(v.ToLower()) {
+            case "true":
+            case "t":
+            case "1":
+            case "on":
+            case "yes":
+                return true;
+            }
+            return false;
+        }
     }
 }

diff --git a/plugins/Makefile b/plugins/Makefile
line changes: +1/-1
index 54f3996..105e807
--- a/plugins/Makefile
+++ b/plugins/Makefile
@@ -1,7 +1,7 @@
 CSFLAGS = -debug
 references = -r:../webkit-sharp.dll -pkg:gtk-sharp-2.0
 
-all: Vimish.dll FFNav.dll DefaultPage.dll LoadProgress.dll MiddleClickOpen.dll QuickSearch.dll
+all: Vimish.dll FFNav.dll DefaultPage.dll LoadProgress.dll MiddleClickOpen.dll QuickSearch.dll Session.dll
 
 clean:
 	rm -f *.dll *.mdb *.so

diff --git a/plugins/Session.cs b/plugins/Session.cs
line changes: +50/-0
index 0000000..37d848c
--- /dev/null
+++ b/plugins/Session.cs
@@ -0,0 +1,50 @@
+using System;
+using System.Collections.Generic;
+using System.Text.RegularExpressions;
+using bytex64.WebThing;
+
+public class Session : WebThingPlugin {
+    WebThing wt;
+
+    public override void Init(WebThing wt) {
+        this.wt = wt;
+        if (Config.Arguments.Length == 0) {
+            if (Config.Options.ContainsKey("Session")) {
+                RestoreSession(Config.Options["Session"]);
+            } else if (Options.ContainsKey("Default")) {
+                RestoreSession("Default");
+            }
+        }
+    }
+
+    public override void Deinit(WebThing wt) {
+        if (Options.ContainsKey("AutoSave") && Config.ParseBool(Options["AutoSave"])) {
+            SaveSession("Default");
+        }
+    }
+
+    public void RestoreSession(string SessionName) {
+        if (Options.ContainsKey(SessionName)) {
+            while (wt.Tabs.NPages > 0) {
+                WebThingView wtv = wt.Tabs.CurrentPageWidget as WebThingView;
+                wt.Tabs.Remove(wtv);
+                wtv.Dispose();
+            }
+            string[] pages = Regex.Split(Options[SessionName], @"\s+");
+            foreach (string page in pages) {
+                wt.OpenUriTab(page);
+            }
+        } else {
+            Console.WriteLine("Could not restore session {0} because it does not exist.", SessionName);
+        }
+    }
+
+    public void SaveSession(string SessionName) {
+        List<string> Uris = new List<string>();
+        foreach (WebThingView wtv in wt.Tabs) {
+            Uris.Add(wtv.WebView.MainFrame.Uri);
+        }
+        Options[SessionName] = String.Join(" ", Uris.ToArray());
+        SaveConfig();
+    }
+}

diff --git a/plugins/Vimish.cs b/plugins/Vimish.cs
line changes: +1/-13
index 4dddb6a..bcb1546
--- a/plugins/Vimish.cs
+++ b/plugins/Vimish.cs
@@ -147,7 +147,7 @@ public class Vimish : WebThingPlugin {
         foreach (string key in Options.Keys) {
             switch(key) {
             case "ShowTabs":
-                bool v = ParseBool(Options[key]);
+                bool v = Config.ParseBool(Options[key]);
                 wt.Tabs.ShowTabs = v;
                 break;
             default:
@@ -157,18 +157,6 @@ public class Vimish : WebThingPlugin {
         }
     }
 
-    private bool ParseBool(string v) {
-        switch(v.ToLower()) {
-        case "true":
-        case "t":
-        case "1":
-        case "on":
-        case "yes":
-            return true;
-        }
-        return false;
-    }
-
     private void Error(string format, params object[] values) {
         Console.WriteLine(format, values);
     }