f325ec3b35cb9b4dfd2ac8c2a59afd5958695f3e
[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.SetWmclass("WebThing", "webthing");
57                         _Window.Role = "browser";
58                         _Window.Destroyed += delegate { Quit(); };
59
60                         WidgetGrid = new Gtk.Table(3, 3, false);
61                         _Window.Add(WidgetGrid);
62
63                         _ScrolledWindow = new Gtk.ScrolledWindow();
64                         WidgetGrid.Attach(_ScrolledWindow, 1, 2, 1, 2);
65
66                         InteriorOverlay = new Gtk.Alignment(1, 0, 0, 0);
67                         WidgetGrid.Attach(InteriorOverlay, 1, 2, 1, 2);
68
69                         _WebView = new WebKit.WebView();
70                         _WebView.TitleChanged += delegate(object o, TitleChangedArgs e) {
71                                 _Window.Title = e.Title + " - WebThing";
72                         };
73                         _ScrolledWindow.Add(_WebView);
74
75                         _Window.ShowAll();
76
77                         Plugins = new Dictionary<string, object>();
78                         // TODO: Conf.Get("plugins") instead of hard-coded path?
79                         using (TextReader f = new StreamReader("plugins.conf")) {
80                                 string line;
81                                 while ((line = f.ReadLine()) != null) {
82                                         line = line.Trim();
83                                         LoadPlugin(line);
84                                 }
85                         }
86
87                         Application.Run();
88                 }
89
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()) {
94                                 q.Enqueue(s);
95                         }
96                         q.Dequeue();   // Remove self argument
97
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>();
102
103                         while (q.Count > 0) {
104                                 string opt = q.Dequeue();
105                                 Match m;
106                                
107                                 m = SimpleOpt.Match(opt);
108                                 if (m.Success) {
109                                         string s = m.Groups[1].Value;
110                                         if (s.Length > 1) {
111                                                 foreach (char c in s) {
112                                                         Options[c.ToString()] = "";
113                                                 }
114                                                 OptLast = null;
115                                         } else {
116                                                 Options[s] = "";
117                                                 OptLast = s;
118                                         }
119                                         continue;
120                                 }
121
122                                 m = LongOpt.Match(opt);
123                                 if (m.Success) {
124                                         string s = m.Groups[1].Value;
125                                         Options[s] = "";
126                                         OptLast = s;
127                                         continue;
128                                 }
129
130                                 // else
131
132                                 if (OptLast != null) {
133                                         Options[OptLast] = opt;
134                                         OptLast = null;
135                                 } else {
136                                         args.Add(opt);
137                                 }
138                         }
139                         Arguments = args.ToArray();
140
141                         /*
142                         Console.WriteLine("Options:");
143                         foreach (string key in Options.Keys)
144                                 Console.WriteLine("   {0}\t{1}", key, Options[key]);
145
146                         Console.WriteLine("Arguments: {0}", String.Join(" ", Arguments));
147                         */
148                 }
149
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;
154                 }
155
156                 public void AttachWidget(Gtk.Widget widget, CompassDirection direction, AttachOptions xoptions, AttachOptions yoptions) {
157                         switch(direction) {
158                         case CompassDirection.N:
159                                 WidgetGrid.Attach(widget, 0, 3, 0, 1, xoptions, yoptions, 0, 0);
160                                 break;
161                         case CompassDirection.E:
162                                 WidgetGrid.Attach(widget, 2, 3, 1, 2, xoptions, yoptions, 0, 0);
163                                 break;
164                         case CompassDirection.S:
165                                 WidgetGrid.Attach(widget, 0, 3, 2, 3, xoptions, yoptions, 0, 0);
166                                 break;
167                         case CompassDirection.W:
168                                 WidgetGrid.Attach(widget, 0, 1, 1, 2, xoptions, yoptions, 0, 0);
169                                 break;
170                         case CompassDirection.Interior:
171                                 InteriorOverlay.Add(widget);
172                                 break;
173                         }
174                 }
175
176                 public void AttachWidget(Gtk.Widget widget, CompassDirection direction) {
177                         AttachWidget(widget, direction, 0, 0);
178                 }
179
180                 public void OpenUri(string Uri) {
181                         if (!Regex.IsMatch(Uri, @"://")) {
182                                 Uri = String.Format("http://{0}", Uri);
183                         }
184                         wv.Open(Uri);
185                 }
186
187                 public void Quit() {
188                         // TODO: Create a way of shutting down plugins
189                         Application.Quit();
190                 }
191         }
192 }