Getting useful...
[WebThing.git] / WebThing.cs
index 04ede67..f36a8fa 100644 (file)
@@ -2,14 +2,14 @@ using System;
 using System.Collections.Generic;
 using System.Reflection;
 using System.IO;
+using System.Text.RegularExpressions;
 using Gtk;
 using GtkSharp;
 using WebKit;
 
 namespace bytex64.WebThing {
        public enum CompassDirection {
-               N, NE, E, SE, S, SW, W, NW,
-               Interior
+               N, E, S, W, Interior
        }
 
        public class WebThing {
@@ -44,11 +44,16 @@ namespace bytex64.WebThing {
                private Gtk.Alignment InteriorOverlay;
 
                public Dictionary<string,object> Plugins;
+               public Dictionary<string,string> Options;
+               public string[] Arguments;
 
                public void Run() {
                        Application.Init();
+
+                       ParseArgs();
+
                        _Window = new Gtk.Window("WebThing");
-                       _Window.Destroyed += delegate { Application.Quit(); };
+                       _Window.Destroyed += delegate { Quit(); };
 
                        WidgetGrid = new Gtk.Table(3, 3, false);
                        _Window.Add(WidgetGrid);
@@ -80,6 +85,66 @@ namespace bytex64.WebThing {
                        Application.Run();
                }
 
+               protected void ParseArgs() {
+                       Options = new Dictionary<string,string>();
+                       Queue<string> q = new Queue<string>();
+                       foreach (string s in System.Environment.GetCommandLineArgs()) {
+                               q.Enqueue(s);
+                       }
+                       q.Dequeue();   // Remove self argument
+
+                       Regex SimpleOpt = new Regex(@"^-([a-zA-Z0-9]+)$");
+                       Regex LongOpt   = new Regex(@"^--([\w-]+)$");
+                       string OptLast = null;
+                       List<string> args = new List<string>();
+
+                       while (q.Count > 0) {
+                               string opt = q.Dequeue();
+                               Match m;
+                              
+                               m = SimpleOpt.Match(opt);
+                               if (m.Success) {
+                                       string s = m.Groups[1].Value;
+                                       if (s.Length > 1) {
+                                               foreach (char c in s) {
+                                                       Options[c.ToString()] = "";
+                                               }
+                                               OptLast = null;
+                                       } else {
+                                               Options[s] = "";
+                                               OptLast = s;
+                                       }
+                                       continue;
+                               }
+
+                               m = LongOpt.Match(opt);
+                               if (m.Success) {
+                                       string s = m.Groups[1].Value;
+                                       Options[s] = "";
+                                       OptLast = s;
+                                       continue;
+                               }
+
+                               // else
+
+                               if (OptLast != null) {
+                                       Options[OptLast] = opt;
+                                       OptLast = null;
+                               } else {
+                                       args.Add(opt);
+                               }
+                       }
+                       Arguments = args.ToArray();
+
+                       /*
+                       Console.WriteLine("Options:");
+                       foreach (string key in Options.Keys)
+                               Console.WriteLine("   {0}\t{1}", key, Options[key]);
+
+                       Console.WriteLine("Arguments: {0}", String.Join(" ", Arguments));
+                       */
+               }
+
                public void LoadPlugin(string assemblyname) {
                        Assembly a = Assembly.LoadFile("plugins/" + assemblyname + ".dll");
                        object obj = a.CreateInstance("Plugin", false, BindingFlags.ExactBinding, null, new object[] { this }, null, null);
@@ -89,29 +154,17 @@ namespace bytex64.WebThing {
                public void AttachWidget(Gtk.Widget widget, CompassDirection direction, AttachOptions xoptions, AttachOptions yoptions) {
                        switch(direction) {
                        case CompassDirection.N:
-                               WidgetGrid.Attach(widget, 1, 2, 0, 1, xoptions, yoptions, 0, 0);
-                               break;
-                       case CompassDirection.NE:
-                               WidgetGrid.Attach(widget, 2, 3, 0, 1, xoptions, yoptions, 0, 0);
+                               WidgetGrid.Attach(widget, 0, 3, 0, 1, xoptions, yoptions, 0, 0);
                                break;
                        case CompassDirection.E:
                                WidgetGrid.Attach(widget, 2, 3, 1, 2, xoptions, yoptions, 0, 0);
                                break;
-                       case CompassDirection.SE:
-                               WidgetGrid.Attach(widget, 2, 3, 2, 3, xoptions, yoptions, 0, 0);
-                               break;
                        case CompassDirection.S:
-                               WidgetGrid.Attach(widget, 1, 2, 2, 3, xoptions, yoptions, 0, 0);
-                               break;
-                       case CompassDirection.SW:
-                               WidgetGrid.Attach(widget, 0, 1, 2, 3, xoptions, yoptions, 0, 0);
+                               WidgetGrid.Attach(widget, 0, 3, 2, 3, xoptions, yoptions, 0, 0);
                                break;
                        case CompassDirection.W:
                                WidgetGrid.Attach(widget, 0, 1, 1, 2, xoptions, yoptions, 0, 0);
                                break;
-                       case CompassDirection.NW:
-                               WidgetGrid.Attach(widget, 0, 1, 0, 1, xoptions, yoptions, 0, 0);
-                               break;
                        case CompassDirection.Interior:
                                InteriorOverlay.Add(widget);
                                break;
@@ -121,5 +174,17 @@ namespace bytex64.WebThing {
                public void AttachWidget(Gtk.Widget widget, CompassDirection direction) {
                        AttachWidget(widget, direction, 0, 0);
                }
+
+               public void OpenUri(string Uri) {
+                       if (!Regex.IsMatch(Uri, @"://")) {
+                               Uri = String.Format("http://{0}", Uri);
+                       }
+                       wv.Open(Uri);
+               }
+
+               public void Quit() {
+                       // TODO: Create a way of shutting down plugins
+                       Application.Quit();
+               }
        }
 }