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.SetWmclass("WebThing", "webthing");
57 _Window.Role = "browser";
58 _Window.Destroyed += delegate { Quit(); };
60 WidgetGrid = new Gtk.Table(3, 3, false);
61 _Window.Add(WidgetGrid);
63 _ScrolledWindow = new Gtk.ScrolledWindow();
64 WidgetGrid.Attach(_ScrolledWindow, 1, 2, 1, 2);
66 InteriorOverlay = new Gtk.Alignment(1, 0, 0, 0);
67 WidgetGrid.Attach(InteriorOverlay, 1, 2, 1, 2);
69 _WebView = new WebKit.WebView();
70 _WebView.TitleChanged += delegate(object o, TitleChangedArgs e) {
71 _Window.Title = e.Title + " - WebThing";
73 _ScrolledWindow.Add(_WebView);
77 Plugins = new Dictionary<string, object>();
78 // TODO: Conf.Get("plugins") instead of hard-coded path?
79 using (TextReader f = new StreamReader("plugins.conf")) {
81 while ((line = f.ReadLine()) != null) {
90 protected void ParseArgs() {
91 Options = new Dictionary<string,string>();
92 Queue<string> q = new Queue<string>();
93 foreach (string s in System.Environment.GetCommandLineArgs()) {
96 q.Dequeue(); // Remove self argument
98 Regex SimpleOpt = new Regex(@"^-([a-zA-Z0-9]+)$");
99 Regex LongOpt = new Regex(@"^--([\w-]+)$");
100 string OptLast = null;
101 List<string> args = new List<string>();
103 while (q.Count > 0) {
104 string opt = q.Dequeue();
107 m = SimpleOpt.Match(opt);
109 string s = m.Groups[1].Value;
111 foreach (char c in s) {
112 Options[c.ToString()] = "";
122 m = LongOpt.Match(opt);
124 string s = m.Groups[1].Value;
132 if (OptLast != null) {
133 Options[OptLast] = opt;
139 Arguments = args.ToArray();
142 Console.WriteLine("Options:");
143 foreach (string key in Options.Keys)
144 Console.WriteLine(" {0}\t{1}", key, Options[key]);
146 Console.WriteLine("Arguments: {0}", String.Join(" ", Arguments));
150 public void LoadPlugin(string assemblyname) {
151 Assembly a = Assembly.LoadFile("plugins/" + assemblyname + ".dll");
152 object obj = a.CreateInstance("Plugin", false, BindingFlags.ExactBinding, null, new object[] { this }, null, null);
153 Plugins[assemblyname] = obj;
156 public void AttachWidget(Gtk.Widget widget, CompassDirection direction, AttachOptions xoptions, AttachOptions yoptions) {
158 case CompassDirection.N:
159 WidgetGrid.Attach(widget, 0, 3, 0, 1, xoptions, yoptions, 0, 0);
161 case CompassDirection.E:
162 WidgetGrid.Attach(widget, 2, 3, 1, 2, xoptions, yoptions, 0, 0);
164 case CompassDirection.S:
165 WidgetGrid.Attach(widget, 0, 3, 2, 3, xoptions, yoptions, 0, 0);
167 case CompassDirection.W:
168 WidgetGrid.Attach(widget, 0, 1, 1, 2, xoptions, yoptions, 0, 0);
170 case CompassDirection.Interior:
171 InteriorOverlay.Add(widget);
176 public void AttachWidget(Gtk.Widget widget, CompassDirection direction) {
177 AttachWidget(widget, direction, 0, 0);
180 public void OpenUri(string Uri) {
181 if (!Regex.IsMatch(Uri, @"://")) {
182 Uri = String.Format("http://{0}", Uri);
188 // TODO: Create a way of shutting down plugins