diff --git a/Plugins/WinAlfred.Plugin.Doc/Doc.cs b/Plugins/WinAlfred.Plugin.Doc/Doc.cs new file mode 100644 index 0000000000..c14239a387 --- /dev/null +++ b/Plugins/WinAlfred.Plugin.Doc/Doc.cs @@ -0,0 +1,10 @@ +namespace WinAlfred.Plugin.Doc +{ + public class Doc + { + public string DBPath { get; set; } + public string DBType { get; set; } + public string Name { get; set; } + public string IconPath { get; set; } + } +} \ No newline at end of file diff --git a/Plugins/WinAlfred.Plugin.Doc/DocViewFrm.Designer.cs b/Plugins/WinAlfred.Plugin.Doc/DocViewFrm.Designer.cs index e9caf7ef48..8873faeeaf 100644 --- a/Plugins/WinAlfred.Plugin.Doc/DocViewFrm.Designer.cs +++ b/Plugins/WinAlfred.Plugin.Doc/DocViewFrm.Designer.cs @@ -33,12 +33,11 @@ // // webBrowser1 // - this.webBrowser1.AllowNavigation = false; this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill; - this.webBrowser1.IsWebBrowserContextMenuEnabled = false; this.webBrowser1.Location = new System.Drawing.Point(0, 0); this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20); this.webBrowser1.Name = "webBrowser1"; + this.webBrowser1.ScriptErrorsSuppressed = true; this.webBrowser1.Size = new System.Drawing.Size(926, 611); this.webBrowser1.TabIndex = 0; // diff --git a/Plugins/WinAlfred.Plugin.Doc/DocViewFrm.cs b/Plugins/WinAlfred.Plugin.Doc/DocViewFrm.cs index f1e1066a21..1d2c96d1e7 100644 --- a/Plugins/WinAlfred.Plugin.Doc/DocViewFrm.cs +++ b/Plugins/WinAlfred.Plugin.Doc/DocViewFrm.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; +using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; @@ -14,10 +15,19 @@ namespace WinAlfred.Plugin.Doc public DocViewFrm() { InitializeComponent(); + FormClosing+=DocViewFrm_FormClosing; + } + + private void DocViewFrm_FormClosing(object sender, FormClosingEventArgs e) + { + e.Cancel = true; + Hide(); } public void ShowDoc(string path) { + //string html = File.ReadAllText(path); + //webBrowser1.DocumentText = html; webBrowser1.Url = new Uri(String.Format("file:///{0}", path)); Show(); } diff --git a/Plugins/WinAlfred.Plugin.Doc/Main.cs b/Plugins/WinAlfred.Plugin.Doc/Main.cs index a88670bbaf..a8e525dd12 100644 --- a/Plugins/WinAlfred.Plugin.Doc/Main.cs +++ b/Plugins/WinAlfred.Plugin.Doc/Main.cs @@ -1,41 +1,53 @@ using System; using System.Collections.Generic; using System.Data.SQLite; +using System.Diagnostics; using System.IO; using System.Linq; +using System.Net; using System.Reflection; using System.Text; +using System.Web; +using Microsoft.Win32; namespace WinAlfred.Plugin.Doc { public class Main : IPlugin { - static public string AssemblyDirectory - { - get - { - string codeBase = Assembly.GetExecutingAssembly().CodeBase; - UriBuilder uri = new UriBuilder(codeBase); - string path = Uri.UnescapeDataString(uri.Path); - return Path.GetDirectoryName(path); - } - } + private List docs = new List(); + DocViewFrm frm = new DocViewFrm(); + private string docsetBasePath; public List Query(Query query) { - string path = @"D:\Personal\WinAlfred\WinAlfred\bin\Debug\Plugins\Doc\Docset\jQuery.docset\Contents\Resources\docSet.dsidx"; + List results = new List(); if (query.ActionParameters.Count == 0) { - //todo:return available docsets name - return new List(); + results.Add(new Result() + { + Title = "Current supported docs:" + }); + results.AddRange(docs.Select(o => new Result() + { + Title = o.Name.Replace(".docset", ""), + IcoPath = o.IconPath + }).ToList()); + return results; } - return QuerySqllite(path, query.ActionParameters[0]); + + foreach (Doc doc in docs) + { + results.AddRange(QuerySqllite(doc, query.ActionParameters[0])); + } + + return results; } public void Init(PluginInitContext context) { + //todo:move to common place - var otherCompanyDlls = new DirectoryInfo(AssemblyDirectory + "\\Plugins\\Doc").GetFiles("*.dll"); + var otherCompanyDlls = new DirectoryInfo(context.PluginMetadata.PluginDirecotry).GetFiles("*.dll"); AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { var dll = otherCompanyDlls.FirstOrDefault(fi => @@ -57,39 +69,124 @@ namespace WinAlfred.Plugin.Doc return Assembly.LoadFile(dll.FullName); }; + + docsetBasePath = context.PluginMetadata.PluginDirecotry + @"Docset"; + foreach (string path in Directory.GetDirectories(docsetBasePath)) + { + string name = path.Substring(path.LastIndexOf('\\') + 1); + string dbPath = path + @"\Contents\Resources\docSet.dsidx"; + string dbType = CheckTableExists("searchIndex", dbPath) ? "DASH" : "ZDASH"; + docs.Add(new Doc + { + Name = name, + DBPath = dbPath, + DBType = dbType, + IconPath = TryGetIcon(name, path) + }); + } } - public List QuerySqllite(string path, string key) + private string TryGetIcon(string name, string path) { - SQLiteConnection conn = null; - string dbPath = "Data Source =" + path; - conn = new SQLiteConnection(dbPath); + string url = "https://raw.github.com/jkozera/zeal/master/zeal/icons/" + + name.Replace(".docset", "").Replace(" ", "_") + ".png"; + string imagePath = path + "\\icon.png"; + if (!File.Exists(imagePath)) + { + HttpWebRequest lxRequest = (HttpWebRequest)WebRequest.Create(url); + // returned values are returned as a stream, then read into a string + String lsResponse = string.Empty; + using (HttpWebResponse lxResponse = (HttpWebResponse)lxRequest.GetResponse()) + { + using (BinaryReader reader = new BinaryReader(lxResponse.GetResponseStream())) + { + Byte[] lnByte = reader.ReadBytes(1 * 1024 * 1024 * 10); + using (FileStream lxFS = new FileStream(imagePath, FileMode.Create)) + { + lxFS.Write(lnByte, 0, lnByte.Length); + } + } + } + + } + return imagePath; + } + + private List QuerySqllite(Doc doc, string key) + { + string dbPath = "Data Source =" + doc.DBPath; + SQLiteConnection conn = new SQLiteConnection(dbPath); conn.Open(); - string sql = "select * from searchIndex where name like '%" + key + "%'"; + string sql = GetSqlByDocDBType(doc.DBType).Replace("{0}", key); SQLiteCommand cmdQ = new SQLiteCommand(sql, conn); SQLiteDataReader reader = cmdQ.ExecuteReader(); List results = new List(); while (reader.Read()) { - string name = reader.GetString(1); - string type = reader.GetString(2); - string docPath = reader.GetString(3); + string name = reader.GetString(reader.GetOrdinal("name")); + string docPath = reader.GetString(reader.GetOrdinal("path")); results.Add(new Result { Title = name, - SubTitle = AssemblyDirectory + "\\Plugins\\Doc\\Docset\\" + docPath, + SubTitle = doc.Name.Replace(".docset", ""), + IcoPath = doc.IconPath, Action = () => - { - DocViewFrm frm = new DocViewFrm(); - frm.ShowDoc(AssemblyDirectory + @"\Plugins\Doc\Docset\jQuery.docset\Contents\Resources\Documents\" + docPath); - } + { + string url = string.Format(@"{0}\{1}\Contents\Resources\Documents\{2}#{3}", docsetBasePath, + doc.Name, docPath, name); + + //frm.ShowDoc(url); + string browser = GetDefaultBrowserPath(); + Process.Start(browser, String.Format("\"file:///{0}\"", url)); + } }); } + conn.Close(); return results; } + + private static string GetDefaultBrowserPath() + { + string key = @"HTTP\shell\open\command"; + using (RegistryKey registrykey = Registry.ClassesRoot.OpenSubKey(key, false)) + { + if (registrykey != null) return ((string)registrykey.GetValue(null, null)).Split('"')[1]; + } + return null; + } + + private string GetSqlByDocDBType(string type) + { + string sql = string.Empty; + if (type == "DASH") + { + sql = "select * from searchIndex where name like '%{0}%' order by name asc, path asc limit 30"; + } + if (type == "ZDASH") + { + sql = @"select ztokenname as name, zpath as path from ztoken +join ztokenmetainformation on ztoken.zmetainformation = ztokenmetainformation.z_pk +join zfilepath on ztokenmetainformation.zfile = zfilepath.z_pk +where (ztokenname like '%{0}%') order by lower(ztokenname) asc, zpath asc limit 30"; + } + + return sql; + } + + private bool CheckTableExists(string table, string path) + { + string dbPath = "Data Source =" + path; + SQLiteConnection conn = new SQLiteConnection(dbPath); + conn.Open(); + string sql = "SELECT name FROM sqlite_master WHERE type='table' AND name='" + table + "';"; + SQLiteCommand cmdQ = new SQLiteCommand(sql, conn); + object obj = cmdQ.ExecuteScalar(); + conn.Close(); + return obj != null; + } } } diff --git a/Plugins/WinAlfred.Plugin.Doc/WinAlfred.Plugin.Doc.csproj b/Plugins/WinAlfred.Plugin.Doc/WinAlfred.Plugin.Doc.csproj index 2324e3d3f1..f6ebbe54d3 100644 --- a/Plugins/WinAlfred.Plugin.Doc/WinAlfred.Plugin.Doc.csproj +++ b/Plugins/WinAlfred.Plugin.Doc/WinAlfred.Plugin.Doc.csproj @@ -52,11 +52,13 @@ - - ..\..\packages\System.Data.SQLite.1.0.90.0\lib\net20\System.Data.SQLite.dll + + False + ..\..\packages\System.Data.SQLite.x64.1.0.90.0\lib\net20\System.Data.SQLite.dll - - ..\..\packages\System.Data.SQLite.1.0.90.0\lib\net20\System.Data.SQLite.Linq.dll + + False + ..\..\packages\System.Data.SQLite.x64.1.0.90.0\lib\net20\System.Data.SQLite.Linq.dll @@ -66,6 +68,7 @@ + Form @@ -91,14 +94,6 @@ - - - Always - - - Always - - DocViewFrm.cs diff --git a/Plugins/WinAlfred.Plugin.Doc/packages.config b/Plugins/WinAlfred.Plugin.Doc/packages.config index db58057cd6..88d9c92811 100644 --- a/Plugins/WinAlfred.Plugin.Doc/packages.config +++ b/Plugins/WinAlfred.Plugin.Doc/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/Plugins/WinAlfred.Plugin.Doc/x64/SQLite.Interop.dll b/Plugins/WinAlfred.Plugin.Doc/x64/SQLite.Interop.dll deleted file mode 100644 index 078e83647d..0000000000 Binary files a/Plugins/WinAlfred.Plugin.Doc/x64/SQLite.Interop.dll and /dev/null differ diff --git a/Plugins/WinAlfred.Plugin.Doc/x86/SQLite.Interop.dll b/Plugins/WinAlfred.Plugin.Doc/x86/SQLite.Interop.dll deleted file mode 100644 index 36cfca0464..0000000000 Binary files a/Plugins/WinAlfred.Plugin.Doc/x86/SQLite.Interop.dll and /dev/null differ diff --git a/WinAlfred.Plugin/PluginInitContext.cs b/WinAlfred.Plugin/PluginInitContext.cs index 81d049961b..1e25a60b8e 100644 --- a/WinAlfred.Plugin/PluginInitContext.cs +++ b/WinAlfred.Plugin/PluginInitContext.cs @@ -8,13 +8,12 @@ namespace WinAlfred.Plugin public class PluginInitContext { public List Plugins { get; set; } + public PluginMetadata PluginMetadata { get; set; } public Action ChangeQuery { get; set; } public Action CloseApp { get; set; } public Action HideApp { get; set; } public Action ShowApp { get; set; } public Action ShowMsg { get; set; } - - } } diff --git a/WinAlfred/PluginLoader/CSharpPluginLoader.cs b/WinAlfred/PluginLoader/CSharpPluginLoader.cs index 7fdd379f4b..7e5a991e8c 100644 --- a/WinAlfred/PluginLoader/CSharpPluginLoader.cs +++ b/WinAlfred/PluginLoader/CSharpPluginLoader.cs @@ -51,13 +51,7 @@ namespace WinAlfred.PluginLoader } - InitPlugin(plugins); return plugins; } - - private void InitPlugin(List plugins) - { - - } } } diff --git a/WinAlfred/PluginLoader/Plugins.cs b/WinAlfred/PluginLoader/Plugins.cs index e832febb73..19745be993 100644 --- a/WinAlfred/PluginLoader/Plugins.cs +++ b/WinAlfred/PluginLoader/Plugins.cs @@ -22,15 +22,21 @@ namespace WinAlfred.PluginLoader foreach (IPlugin plugin in plugins.Select(pluginPair => pluginPair.Plugin)) { IPlugin plugin1 = plugin; - ThreadPool.QueueUserWorkItem(o => plugin1.Init(new PluginInitContext() + PluginPair pluginPair = plugins.FirstOrDefault(o => o.Plugin == plugin1); + if (pluginPair != null) { - Plugins = plugins, - ChangeQuery = s => window.ChangeQuery(s), - CloseApp = window.CloseApp, - HideApp = window.HideApp, - ShowApp = () => window.ShowApp(), - ShowMsg = (title, subTitle, iconPath) => window.ShowMsg(title, subTitle, iconPath) - })); + PluginMetadata metadata = pluginPair.Metadata; + ThreadPool.QueueUserWorkItem(o => plugin1.Init(new PluginInitContext() + { + Plugins = plugins, + PluginMetadata = metadata, + ChangeQuery = s => window.ChangeQuery(s), + CloseApp = window.CloseApp, + HideApp = window.HideApp, + ShowApp = () => window.ShowApp(), + ShowMsg = (title, subTitle, iconPath) => window.ShowMsg(title, subTitle, iconPath) + })); + } } }