X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=SearchHandler.cs;h=a8feb2ac18dde7ad4c5e030f7d63824e0009963a;hb=2744fefc32dbe9b59a570f6515d51121b8fcfbe3;hp=54d50092a6d85cafed45c7ba5b1393f9dba8e257;hpb=11835c28c3407f9bbd4aa68b875a528d13ff0d73;p=WebThing.git diff --git a/SearchHandler.cs b/SearchHandler.cs index 54d5009..a8feb2a 100644 --- a/SearchHandler.cs +++ b/SearchHandler.cs @@ -6,6 +6,7 @@ namespace bytex64.WebThing { public class SearchHandler { WebThing wt; Dictionary Handlers; + ISearchPlugin DefaultSearchHandler; public SearchHandler(WebThing wt) { Handlers = new Dictionary(); @@ -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; + } } } }