Getting useful...
[WebThing.git] / WebThing.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Reflection;
4 using System.IO;
5 using System.Text.RegularExpressions;
6 using Gtk;
7 using GtkSharp;
8 using WebKit;
9
10 namespace bytex64.WebThing {
11         public enum CompassDirection {
12                 N, E, S, W, Interior
13         }
14
15         public class WebThing {
16                 public Gtk.Window Window {
17                         get { return _Window; }
18                 }
19
20                 public Gtk.Window w {
21                         get { return _Window; }
22                 }
23
24                 public Gtk.ScrolledWindow ScrolledWindow {
25                         get { return _ScrolledWindow; }
26                 }
27
28                 public Gtk.ScrolledWindow sw {
29                         get { return _ScrolledWindow; }
30                 }
31
32                 public WebKit.WebView WebView {
33                         get { return _WebView; }
34                 }
35
36                 public WebKit.WebView wv {
37                         get { return _WebView; }
38                 }
39
40                 private Gtk.Window _Window;
41                 private ScrolledWindow _ScrolledWindow;
42                 private WebKit.WebView _WebView;
43                 private Gtk.Table WidgetGrid;
44                 private Gtk.Alignment InteriorOverlay;
45
46                 public Dictionary<string,object> Plugins;
47                 public Dictionary<string,string> Options;
48                 public string[] Arguments;
49
50                 public void Run() {
51                         Application.Init();
52
53                         ParseArgs();
54
55                         _Window = new Gtk.Window("WebThing");
56                         _Window.Destroyed += delegate { Quit(); };
57
58                         WidgetGrid = new Gtk.Table(3, 3, false);
59                         _Window.Add(WidgetGrid);
60
61                         _ScrolledWindow = new Gtk.ScrolledWindow();
62                         WidgetGrid.Attach(_ScrolledWindow, 1, 2, 1, 2);
63
64                         InteriorOverlay = new Gtk.Alignment(1, 0, 0, 0);
65                         WidgetGrid.Attach(InteriorOverlay, 1, 2, 1, 2);
66
67                         _WebView = new WebKit.WebView();
68                         _WebView.TitleChanged += delegate(object o, TitleChangedArgs e) {
69                                 _Window.Title = e.Title + " - WebThing";
70                         };
71                         _ScrolledWindow.Add(_WebView);
72
73                         _Window.ShowAll();
74
75                         Plugins = new Dictionary<string, object>();
76                         // TODO: Conf.Get("plugins") instead of hard-coded path?
77                         using (TextReader f = new StreamReader("plugins.conf")) {
78                                 string line;
79                                 while ((line = f.ReadLine()) != null) {
80                                         line = line.Trim();
81                                         LoadPlugin(line);
82                                 }
83                         }
84
85                         Application.Run();
86                 }
87
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()) {
92                                 q.Enqueue(s);
93                         }
94                         q.Dequeue();   // Remove self argument
95
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>();
100
101                         while (q.Count > 0) {
102                                 string opt = q.Dequeue();
103                                 Match m;
104                                
105                                 m = SimpleOpt.Match(opt);
106                                 if (m.Success) {
107                                         string s = m.Groups[1].Value;
108                                         if (s.Length > 1) {
109                                                 foreach (char c in s) {
110                                                         Options[c.ToString()] = "";
111                                                 }
112                                                 OptLast = null;
113                                         } else {
114                                                 Options[s] = "";
115                                                 OptLast = s;
116                                         }
117                                         continue;
118                                 }
119
120                                 m = LongOpt.Match(opt);
121                                 if (m.Success) {
122                                         string s = m.Groups[1].Value;
123                                         Options[s] = "";
124                                         OptLast = s;
125                                         continue;
126                                 }
127
128                                 // else
129
130                                 if (OptLast != null) {
131                                         Options[OptLast] = opt;
132                                         OptLast = null;
133                                 } else {
134                                         args.Add(opt);
135                                 }
136                         }
137                         Arguments = args.ToArray();
138
139                         /*
140                         Console.WriteLine("Options:");
141                         foreach (string key in Options.Keys)
142                                 Console.WriteLine("   {0}\t{1}", key, Options[key]);
143
144                         Console.WriteLine("Arguments: {0}", String.Join(" ", Arguments));
145                         */
146                 }
147
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;
152                 }
153
154                 public void AttachWidget(Gtk.Widget widget, CompassDirection direction, AttachOptions xoptions, AttachOptions yoptions) {
155                         switch(direction) {
156                         case CompassDirection.N:
157                                 WidgetGrid.Attach(widget, 0, 3, 0, 1, xoptions, yoptions, 0, 0);
158                                 break;
159                         case CompassDirection.E:
160                                 WidgetGrid.Attach(widget, 2, 3, 1, 2, xoptions, yoptions, 0, 0);
161                                 break;
162                         case CompassDirection.S:
163                                 WidgetGrid.Attach(widget, 0, 3, 2, 3, xoptions, yoptions, 0, 0);
164                                 break;
165                         case CompassDirection.W:
166                                 WidgetGrid.Attach(widget, 0, 1, 1, 2, xoptions, yoptions, 0, 0);
167                                 break;
168                         case CompassDirection.Interior:
169                                 InteriorOverlay.Add(widget);
170                                 break;
171                         }
172                 }
173
174                 public void AttachWidget(Gtk.Widget widget, CompassDirection direction) {
175                         AttachWidget(widget, direction, 0, 0);
176                 }
177
178                 public void OpenUri(string Uri) {
179                         if (!Regex.IsMatch(Uri, @"://")) {
180                                 Uri = String.Format("http://{0}", Uri);
181                         }
182                         wv.Open(Uri);
183                 }
184
185                 public void Quit() {
186                         // TODO: Create a way of shutting down plugins
187                         Application.Quit();
188                 }
189         }
190 }