2 using System.Collections.Generic;
3 using System.Reflection;
5 using System.Text.RegularExpressions;
10 namespace bytex64.WebThing {
11 public enum CompassDirection {
15 public class WebThing {
16 public Gtk.Window Window {
17 get { return _Window; }
21 get { return _Window; }
24 public Gtk.ScrolledWindow ScrolledWindow {
25 get { return _ScrolledWindow; }
28 public Gtk.ScrolledWindow sw {
29 get { return _ScrolledWindow; }
32 public WebKit.WebView WebView {
33 get { return _WebView; }
36 public WebKit.WebView wv {
37 get { return _WebView; }
40 private Gtk.Window _Window;
41 private ScrolledWindow _ScrolledWindow;
42 private WebKit.WebView _WebView;
43 private Gtk.Table WidgetGrid;
44 private Gtk.Alignment InteriorOverlay;
46 public Dictionary<string,object> Plugins;
47 public Dictionary<string,string> Options;
48 public string[] Arguments;
55 _Window = new Gtk.Window("WebThing");
56 _Window.Destroyed += delegate { Quit(); };
58 WidgetGrid = new Gtk.Table(3, 3, false);
59 _Window.Add(WidgetGrid);
61 _ScrolledWindow = new Gtk.ScrolledWindow();
62 WidgetGrid.Attach(_ScrolledWindow, 1, 2, 1, 2);
64 InteriorOverlay = new Gtk.Alignment(1, 0, 0, 0);
65 WidgetGrid.Attach(InteriorOverlay, 1, 2, 1, 2);
67 _WebView = new WebKit.WebView();
68 _WebView.TitleChanged += delegate(object o, TitleChangedArgs e) {
69 _Window.Title = e.Title + " - WebThing";
71 _ScrolledWindow.Add(_WebView);
75 Plugins = new Dictionary<string, object>();
76 // TODO: Conf.Get("plugins") instead of hard-coded path?
77 using (TextReader f = new StreamReader("plugins.conf")) {
79 while ((line = f.ReadLine()) != null) {
88 protected void ParseArgs() {
89 Options = new Dictionary<string,string>();
90 Queue<string> q = new Queue<string>();
91 foreach (string s in System.Environment.GetCommandLineArgs()) {
94 q.Dequeue(); // Remove self argument
96 Regex SimpleOpt = new Regex(@"^-([a-zA-Z0-9]+)$");
97 Regex LongOpt = new Regex(@"^--([\w-]+)$");
98 string OptLast = null;
99 List<string> args = new List<string>();
101 while (q.Count > 0) {
102 string opt = q.Dequeue();
105 m = SimpleOpt.Match(opt);
107 string s = m.Groups[1].Value;
109 foreach (char c in s) {
110 Options[c.ToString()] = "";
120 m = LongOpt.Match(opt);
122 string s = m.Groups[1].Value;
130 if (OptLast != null) {
131 Options[OptLast] = opt;
137 Arguments = args.ToArray();
140 Console.WriteLine("Options:");
141 foreach (string key in Options.Keys)
142 Console.WriteLine(" {0}\t{1}", key, Options[key]);
144 Console.WriteLine("Arguments: {0}", String.Join(" ", Arguments));
148 public void LoadPlugin(string assemblyname) {
149 Assembly a = Assembly.LoadFile("plugins/" + assemblyname + ".dll");
150 object obj = a.CreateInstance("Plugin", false, BindingFlags.ExactBinding, null, new object[] { this }, null, null);
151 Plugins[assemblyname] = obj;
154 public void AttachWidget(Gtk.Widget widget, CompassDirection direction, AttachOptions xoptions, AttachOptions yoptions) {
156 case CompassDirection.N:
157 WidgetGrid.Attach(widget, 0, 3, 0, 1, xoptions, yoptions, 0, 0);
159 case CompassDirection.E:
160 WidgetGrid.Attach(widget, 2, 3, 1, 2, xoptions, yoptions, 0, 0);
162 case CompassDirection.S:
163 WidgetGrid.Attach(widget, 0, 3, 2, 3, xoptions, yoptions, 0, 0);
165 case CompassDirection.W:
166 WidgetGrid.Attach(widget, 0, 1, 1, 2, xoptions, yoptions, 0, 0);
168 case CompassDirection.Interior:
169 InteriorOverlay.Add(widget);
174 public void AttachWidget(Gtk.Widget widget, CompassDirection direction) {
175 AttachWidget(widget, direction, 0, 0);
178 public void OpenUri(string Uri) {
179 if (!Regex.IsMatch(Uri, @"://")) {
180 Uri = String.Format("http://{0}", Uri);
186 // TODO: Create a way of shutting down plugins