Added session management plugin
[WebThing.git] / plugins / Session.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text.RegularExpressions;
4 using bytex64.WebThing;
5
6 public class Session : WebThingPlugin {
7     WebThing wt;
8
9     public override void Init(WebThing wt) {
10         this.wt = wt;
11         if (Config.Arguments.Length == 0) {
12             if (Config.Options.ContainsKey("Session")) {
13                 RestoreSession(Config.Options["Session"]);
14             } else if (Options.ContainsKey("Default")) {
15                 RestoreSession("Default");
16             }
17         }
18     }
19
20     public override void Deinit(WebThing wt) {
21         if (Options.ContainsKey("AutoSave") && Config.ParseBool(Options["AutoSave"])) {
22             SaveSession("Default");
23         }
24     }
25
26     public void RestoreSession(string SessionName) {
27         if (Options.ContainsKey(SessionName)) {
28             while (wt.Tabs.NPages > 0) {
29                 WebThingView wtv = wt.Tabs.CurrentPageWidget as WebThingView;
30                 wt.Tabs.Remove(wtv);
31                 wtv.Dispose();
32             }
33             string[] pages = Regex.Split(Options[SessionName], @"\s+");
34             foreach (string page in pages) {
35                 wt.OpenUriTab(page);
36             }
37         } else {
38             Console.WriteLine("Could not restore session {0} because it does not exist.", SessionName);
39         }
40     }
41
42     public void SaveSession(string SessionName) {
43         List<string> Uris = new List<string>();
44         foreach (WebThingView wtv in wt.Tabs) {
45             Uris.Add(wtv.WebView.MainFrame.Uri);
46         }
47         Options[SessionName] = String.Join(" ", Uris.ToArray());
48         SaveConfig();
49     }
50 }