Add default size configuration
[WebThing.git] / WebThing.cs
index 08cfb1b..8bc589a 100644 (file)
@@ -44,6 +44,7 @@ namespace bytex64.WebThing {
         private Gtk.Alignment InteriorOverlay;
 
         public PluginManager Plugins;
+        public SearchHandler Search;
 
         [DllImport ("SoupSettings.dll")]
         private static extern void soup_settings();
@@ -64,6 +65,21 @@ namespace bytex64.WebThing {
             // based on the executable name, which I don't like.
             _Window.SetWmclass("webthing", "WebThing");  
             _Window.Role = "browser";
+            {
+                int w, h;
+
+                if (Config.Options.ContainsKey("Width"))
+                    w = Convert.ToInt32(Config.Options["Width"]);
+                else
+                    w = 640;
+
+                if (Config.Options.ContainsKey("Height"))
+                    h = Convert.ToInt32(Config.Options["Height"]);
+                else
+                    h = 480;
+
+                _Window.DefaultSize = new Gdk.Size(w, h);
+            }
             _Window.Destroyed += delegate { Quit(); };
 
             // Initialize WidgetGrid
@@ -95,10 +111,13 @@ namespace bytex64.WebThing {
             Plugins = new PluginManager(this);
             Plugins.Load();
 
+            // Load Search Handlers
+            Search = new SearchHandler(this);
+
             // Create a new, default WebThingView if one has not already
             // been created.
             if (Tabs.NPages == 0)
-                OpenUriTab("http://dominionofawesome.com/");
+                OpenTab("http://dominionofawesome.com/");
             WebView.GrabFocus();
 
             Application.Run();
@@ -175,20 +194,36 @@ namespace bytex64.WebThing {
         }
 
         // Uri loading
-        public string FixUri(string Uri) {
-            if (!Regex.IsMatch(Uri, @"://")) {
-                return String.Format("http://{0}", Uri);
+        private string GetUri(string query) {
+            Uri u;
+            try {
+                u = new Uri(query);
+                return u.ToString();
+            } catch(UriFormatException) {
+                try {
+                    u = new Uri(String.Format("http://{0}", query));
+                    return u.ToString();
+                } catch (UriFormatException) {
+                    return Search.Transform(query);
+                }
             }
-            return Uri;
         }
 
-        public void OpenUri(string Uri) {
-            wv.Open(FixUri(Uri));
+        public bool Open(string query) {
+            string uri = GetUri(query);
+            if (uri == null) return false;
+            
+            wv.Open(uri);
+            return true;
         }
 
-        public void OpenUriTab(string Uri) {
+        public bool OpenTab(string query) {
             WebThingView wtv = NewTab();
-            wtv.WebView.Open(FixUri(Uri));
+            string uri = GetUri(query);
+            if (uri == null) return false;
+
+            wtv.WebView.Open(uri);
+            return true;
         }
     }
 }