From aab0bf369d1759f62d588abe78e003e57d06de4e Mon Sep 17 00:00:00 2001 From: Yeechan Lu Date: Wed, 19 Mar 2014 03:05:43 +0800 Subject: [PATCH] Add PortableApps.com program source support. #42 --- .../PortableAppsProgramSource.cs | 119 ++++++++++++++++++ Wox.Plugin.System/Wox.Plugin.System.csproj | 4 + Wox.Plugin.System/packages.config | 1 + 3 files changed, 124 insertions(+) create mode 100644 Wox.Plugin.System/PortableAppsProgramSource.cs diff --git a/Wox.Plugin.System/PortableAppsProgramSource.cs b/Wox.Plugin.System/PortableAppsProgramSource.cs new file mode 100644 index 0000000000..7da206c393 --- /dev/null +++ b/Wox.Plugin.System/PortableAppsProgramSource.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; +using IniParser; +namespace Wox.Plugin.System +{ + public class PortableAppsProgramSource : AbstractProgramSource + { + public string BaseDirectory; + + public PortableAppsProgramSource(string baseDirectory) + { + BaseDirectory = baseDirectory; + } + + public override List LoadPrograms() + { + List list = new List(); + var ini = new IniParser.Parser.IniDataParser(); + ini.Configuration.AllowDuplicateKeys = true; + + string menuSettingsPath = Path.Combine(BaseDirectory, @"PortableApps.com\Data\PortableAppsMenu.ini"); + + IniParser.Model.KeyDataCollection appsRenamed = null, appsRecategorized = null, appsHidden = null; + if (File.Exists(menuSettingsPath)) + { + var menuSettings = ini.Parse(File.ReadAllText(menuSettingsPath, Encoding.Default)); + appsRenamed = menuSettings["AppsRenamed"]; + appsRecategorized = menuSettings["AppsRecategorized"]; + appsHidden = menuSettings["AppsHidden"]; + } + if (appsRenamed == null) appsRenamed = new IniParser.Model.KeyDataCollection(); + if (appsRecategorized == null) appsRecategorized = new IniParser.Model.KeyDataCollection(); + if (appsHidden == null) appsHidden = new IniParser.Model.KeyDataCollection(); + + foreach (var appDir in Directory.GetDirectories(BaseDirectory)) + { + var appDirName = Path.GetDirectoryName(appDir); + var appInfoPath = Path.Combine(appDir, @"App\AppInfo\appinfo.ini"); + var appInfoValid = false; + + if (File.Exists(appInfoPath)) + { + var appInfo = ini.Parse(File.ReadAllText(appInfoPath, Encoding.Default)); + var appName = appInfo["Details"]["Name"] ?? appDirName; + var control = appInfo["Control"]; + int count; + if (Int32.TryParse(control["Icons"], out count)) + { + appInfoValid = true; + for (int i = 1; i <= count; i++) + { + string cmdline, name, icon; + cmdline = control[String.Format("Start{0}", i)]; + name = control[String.Format("Name{0}", i)]; + icon = control[String.Format("ExtractIcon{0}", i)]; + + if (i == 1) + { + if (cmdline == null) cmdline = control["Start"]; + if (cmdline == null) continue; + + if (name == null) name = appName; + if (icon == null) icon = control["ExtractIcon"]; + if (icon == null && !File.Exists(icon = Path.Combine(appDir, @"App\AppInfo\appicon.ico"))) icon = null; + } + + if (cmdline == null) continue; + if (name == null) name = String.Format("{0} #{1}", appName, i); + if (icon == null) icon = Path.Combine(appDir, String.Format(@"App\AppInfo\appicon{0}.ico", i)); + + cmdline = Path.Combine(appDir, cmdline); + var menuKey = (appDirName + @"\" + cmdline).ToLower(); + + var renamed = appsRenamed[menuKey]; + if (renamed != null) + name = renamed; + + var hidden = appsHidden[menuKey] == "true"; + + if (!hidden) + { + Program p = new Program() + { + Title = name, + IcoPath = icon, + ExecutePath = cmdline + }; + list.Add(p); + } + } + } + } + + if (!appInfoValid) + { + foreach (var item in Directory.GetFiles(appDir, "*.exe", SearchOption.TopDirectoryOnly)) + { + var menuKey = Path.GetFullPath(item).Substring(Path.GetFullPath(BaseDirectory).Length + 1).ToLower(); + + if (appsHidden[menuKey] != "true") + { + var p = CreateEntry(item); + var renamed = appsRenamed[menuKey]; + if (renamed != null) + p.Title = renamed; + + list.Add(p); + } + } + } + } + + return list; + } + } +} diff --git a/Wox.Plugin.System/Wox.Plugin.System.csproj b/Wox.Plugin.System/Wox.Plugin.System.csproj index 66525c18a7..a98c52d3da 100644 --- a/Wox.Plugin.System/Wox.Plugin.System.csproj +++ b/Wox.Plugin.System/Wox.Plugin.System.csproj @@ -32,6 +32,9 @@ 4 + + ..\packages\ini-parser.2.0.2\lib\INIFileParser.dll + ..\packages\Newtonsoft.Json.5.0.8\lib\net35\Newtonsoft.Json.dll @@ -49,6 +52,7 @@ + diff --git a/Wox.Plugin.System/packages.config b/Wox.Plugin.System/packages.config index 0fa426d977..7cc723c824 100644 --- a/Wox.Plugin.System/packages.config +++ b/Wox.Plugin.System/packages.config @@ -1,5 +1,6 @@  + \ No newline at end of file