2 using System.Collections.Generic;
3 using System.Reflection;
5 using System.Text.RegularExpressions;
6 using System.Runtime.InteropServices;
11 namespace bytex64.WebThing {
12 public enum CompassDirection {
16 public class WebThing {
17 public Gtk.Window Window {
18 get { return _Window; }
22 get { return _Window; }
25 public Gtk.ScrolledWindow ScrolledWindow {
26 get { return _ScrolledWindow; }
29 public Gtk.ScrolledWindow sw {
30 get { return _ScrolledWindow; }
33 public WebKit.WebView WebView {
34 get { return _WebView; }
37 public WebKit.WebView wv {
38 get { return _WebView; }
41 private Gtk.Window _Window;
42 private ScrolledWindow _ScrolledWindow;
43 private WebKit.WebView _WebView;
44 private Gtk.Table WidgetGrid;
45 private Gtk.Alignment InteriorOverlay;
47 public Dictionary<string,WebThingPlugin> Plugins;
48 public Dictionary<string,string> Options;
49 public string[] Arguments;
51 [DllImport ("SoupSettings.dll")]
52 private static extern void soup_settings();
59 _Window = new Gtk.Window("WebThing");
60 _Window.SetWmclass("webthing", "WebThing");
61 _Window.Role = "browser";
62 _Window.Destroyed += delegate { Quit(); };
64 WidgetGrid = new Gtk.Table(3, 3, false);
65 _Window.Add(WidgetGrid);
67 _ScrolledWindow = new Gtk.ScrolledWindow();
68 WidgetGrid.Attach(_ScrolledWindow, 1, 2, 1, 2);
70 InteriorOverlay = new Gtk.Alignment(1, 0, 0, 0);
71 WidgetGrid.Attach(InteriorOverlay, 1, 2, 1, 2);
73 _WebView = new WebKit.WebView();
74 _WebView.TitleChanged += delegate(object o, TitleChangedArgs e) {
75 _Window.Title = e.Title + " - WebThing";
79 _ScrolledWindow.Add(_WebView);
83 Plugins = new Dictionary<string,WebThingPlugin>();
84 // TODO: Conf.Get("plugins") instead of hard-coded path?
85 using (TextReader f = new StreamReader("plugins.conf")) {
87 while ((line = f.ReadLine()) != null) {
96 protected void ParseArgs() {
97 Options = new Dictionary<string,string>();
98 Queue<string> q = new Queue<string>();
99 foreach (string s in System.Environment.GetCommandLineArgs()) {
102 q.Dequeue(); // Remove self argument
104 Regex SimpleOpt = new Regex(@"^-([a-zA-Z0-9]+)$");
105 Regex LongOpt = new Regex(@"^--([\w-]+)$");
106 string OptLast = null;
107 List<string> args = new List<string>();
109 while (q.Count > 0) {
110 string opt = q.Dequeue();
113 m = SimpleOpt.Match(opt);
115 string s = m.Groups[1].Value;
117 foreach (char c in s) {
118 Options[c.ToString()] = "";
128 m = LongOpt.Match(opt);
130 string s = m.Groups[1].Value;
138 if (OptLast != null) {
139 Options[OptLast] = opt;
145 Arguments = args.ToArray();
148 Console.WriteLine("Options:");
149 foreach (string key in Options.Keys)
150 Console.WriteLine(" {0}\t{1}", key, Options[key]);
152 Console.WriteLine("Arguments: {0}", String.Join(" ", Arguments));
156 public void LoadPlugin(string assemblyname) {
157 Assembly a = Assembly.LoadFile("plugins/" + assemblyname + ".dll");
158 Type[] types = a.GetTypes();
159 foreach (Type t in types) {
160 if (t.GetInterface("WebThingPlugin") != null) {
161 WebThingPlugin p = (WebThingPlugin) a.CreateInstance(t.FullName, false, BindingFlags.ExactBinding, null, null, null, null);
163 Plugins[t.FullName] = p;
164 Console.WriteLine("Successfully loaded {0}", t.FullName);
169 public void AttachWidget(Gtk.Widget widget, CompassDirection direction, AttachOptions xoptions, AttachOptions yoptions) {
171 case CompassDirection.N:
172 WidgetGrid.Attach(widget, 0, 3, 0, 1, xoptions, yoptions, 0, 0);
174 case CompassDirection.E:
175 WidgetGrid.Attach(widget, 2, 3, 1, 2, xoptions, yoptions, 0, 0);
177 case CompassDirection.S:
178 WidgetGrid.Attach(widget, 0, 3, 2, 3, xoptions, yoptions, 0, 0);
180 case CompassDirection.W:
181 WidgetGrid.Attach(widget, 0, 1, 1, 2, xoptions, yoptions, 0, 0);
183 case CompassDirection.Interior:
184 InteriorOverlay.Add(widget);
189 public void AttachWidget(Gtk.Widget widget, CompassDirection direction) {
190 AttachWidget(widget, direction, 0, 0);
193 public void OpenUri(string Uri) {
194 if (!Regex.IsMatch(Uri, @"://")) {
195 Uri = String.Format("http://{0}", Uri);
201 // TODO: Create a way of shutting down plugins