Fix scrolling functionality
[WebThing.git] / SearchHandler.cs
index 54d5009..a8feb2a 100644 (file)
@@ -6,6 +6,7 @@ namespace bytex64.WebThing {
     public class SearchHandler {
         WebThing wt;
         Dictionary<string,ISearchPlugin> Handlers;
+        ISearchPlugin DefaultSearchHandler;
         
         public SearchHandler(WebThing wt) {
             Handlers = new Dictionary<string,ISearchPlugin>();
@@ -14,6 +15,16 @@ namespace bytex64.WebThing {
             foreach (SearchHandlerPair p in Config.SearchHandlers) {
                 AddHandler(p.Shortcut, p.Plugin);
             }
+            if (Config.DefaultSearchHandler != null) {
+                SetDefaultHandler(Config.DefaultSearchHandler);
+            }
+        }
+
+        private bool IsValidSearchHandler(WebThingPlugin p) {
+            Type ptype = p.GetType();
+            if (ptype.GetInterface("ISearchPlugin") != null)
+                return true;
+            return false;
         }
 
         public void AddHandler(string key, string plugin) {
@@ -27,9 +38,8 @@ namespace bytex64.WebThing {
             }
 
             WebThingPlugin p = wt.Plugins.Plugins[plugin];
-            Type ptype = p.GetType();
 
-            if (ptype.GetInterface("ISearchPlugin") != null) {
+            if (IsValidSearchHandler(p)) {
                 Handlers[key] = (ISearchPlugin) p;
                 Console.WriteLine("Added handler {0} for key {1}", plugin, key);
             } else {
@@ -37,6 +47,19 @@ namespace bytex64.WebThing {
             }
         }
 
+        public void SetDefaultHandler(string plugin) {
+            if (wt.Plugins.Plugins.ContainsKey(plugin)) {
+                WebThingPlugin p = wt.Plugins.Plugins[plugin];
+                if (IsValidSearchHandler(p)) {
+                    DefaultSearchHandler = (ISearchPlugin) p;
+                } else {
+                    Console.WriteLine("Could not set default search handler: {0} does not implement ISearchPlugin", plugin);
+                }
+            } else {
+                Console.WriteLine("Could not set default search handler: {0} is not loaded", plugin);
+            }
+        }
+
         // Call a search handler based on the leading word
         public string Transform(string search) {
             Regex get_handler_re = new Regex(@"^([\w-]+)\s+");
@@ -44,13 +67,23 @@ namespace bytex64.WebThing {
             if (m.Success) {
                 string key = m.Groups[1].Value;
                 string query = get_handler_re.Replace(search, "");
-                if (!Handlers.ContainsKey(key)) {
-                    Console.WriteLine("Could not search with {0}: No search handler defined", key);
-                    return null;
+                if (Handlers.ContainsKey(key)) {
+                    return Handlers[key].SearchTransform(query);
+                } else {
+                    if (DefaultSearchHandler != null) {
+                        return DefaultSearchHandler.SearchTransform(search);
+                    } else {
+                        Console.WriteLine("Could not search with {0} and no default search handler defined", key);
+                        return null;
+                    }
                 }
-                return Handlers[key].SearchTransform(query);
             } else {
-                return null;
+                if (DefaultSearchHandler != null) {
+                    return DefaultSearchHandler.SearchTransform(search);
+                } else {
+                    Console.WriteLine("Could not get search key and no default search handler defined");
+                    return null;
+                }
             }
         }
     }