Fix scrolling functionality
[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     string CurrentSession = "Default";
9
10     public override void Init(WebThing wt) {
11         this.wt = wt;
12         if (Config.Arguments.Length == 0) {
13             if (Config.Options.ContainsKey("Session")) {
14                 RestoreSession(Config.Options["Session"]);
15             } else if (Options.ContainsKey("Default")) {
16                 RestoreSession("Default");
17             }
18         }
19     }
20
21     public override void Deinit(WebThing wt) {
22         if (Options.ContainsKey("AutoSave") && Config.ParseBool(Options["AutoSave"])) {
23             SaveSession(CurrentSession);
24         }
25     }
26
27     public void RestoreSession(string SessionName) {
28         if (Options.ContainsKey(SessionName)) {
29             while (wt.Tabs.NPages > 0) {
30                 WebThingView wtv = wt.Tabs.CurrentPageWidget as WebThingView;
31                 wt.Tabs.Remove(wtv);
32                 wtv.Dispose();
33             }
34             string[] pages = Regex.Split(Options[SessionName], @"\s+");
35             foreach (string page in pages) {
36                 wt.OpenTab(page);
37             }
38             CurrentSession = SessionName;
39         } else {
40             Console.WriteLine("Could not restore session {0} because it does not exist.", SessionName);
41         }
42     }
43
44     public void SaveSession(string SessionName) {
45         List<string> Uris = new List<string>();
46         foreach (WebThingView wtv in wt.Tabs) {
47             Uris.Add(wtv.WebView.MainFrame.Uri);
48         }
49         Options[SessionName] = String.Join(" ", Uris.ToArray());
50         SaveConfig();
51     }
52
53     public void DeleteSession(string SessionName) {
54         Options.Remove(SessionName);
55         SaveConfig();
56     }
57 }