diff --git a/Wox.Infrastructure/UAC.cs b/Wox.Infrastructure/UAC.cs deleted file mode 100644 index 2ee5aff981..0000000000 --- a/Wox.Infrastructure/UAC.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Drawing; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Runtime.InteropServices; -using System.Security.Principal; -using System.Text; -using System.Windows; -using System.Windows.Forms; -using System.Windows.Media.Imaging; -using Size = System.Drawing.Size; - -namespace Wox.Infrastructure -{ - - public static class UAC - { - /// - /// Execute methods that require Admin role, which will popup UAC window. - /// - /// Notes: - /// 1. Invoker method shouldn't have any parameters - /// 2. Add attribute [MethodImpl(MethodImplOptions.NoInlining)] to invoker method - /// - /// Example: - /// [MethodImpl(MethodImplOptions.NoInlining)] - /// private void OnStartWithWindowUnChecked() - /// { - /// UAC.ExecuteAdminMethod(() => SetStartup(false)); - /// } - /// - /// - /// - public static void ExecuteAdminMethod(Action method) - { - if (method == null) return; - if (Environment.OSVersion.Version.Major <= 5 || IsAdministrator()) - { - method(); - return; - } - - StackTrace stackTrace = new StackTrace(); - // Get calling method name - MethodBase callingMethod = stackTrace.GetFrame(1).GetMethod(); - string methodName = callingMethod.Name; - if (callingMethod.ReflectedType == null) return; - - string className = callingMethod.ReflectedType.Name; - string nameSpace = callingMethod.ReflectedType.Namespace; - string args = string.Format("UAC {0} {1} {2}", nameSpace,className,methodName); - Debug.WriteLine(args); - var psi = new ProcessStartInfo - { - FileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Wox.UAC.exe"), - Arguments = args, - CreateNoWindow = true, - Verb = "runas" - }; - - try - { - var process = new Process(); - process.StartInfo = psi; - process.Start(); - } - catch (Exception e) - { - MessageBox.Show("Execute failed: " + e); -#if (DEBUG) - { - throw; - } -#endif - - } - - } - - private static bool IsAdministrator() - { - var identity = WindowsIdentity.GetCurrent(); - if (identity != null) - { - var principal = new WindowsPrincipal(identity); - return principal.IsInRole(WindowsBuiltInRole.Administrator); - } - - return false; - } - } -} \ No newline at end of file diff --git a/Wox.Infrastructure/Wox.Infrastructure.csproj b/Wox.Infrastructure/Wox.Infrastructure.csproj index 723ebd6f0a..c34ad3a0ce 100644 --- a/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -53,7 +53,6 @@ - diff --git a/Wox.UAC/FileTypeAssociateInstaller.cs b/Wox.UAC/FileTypeAssociateInstaller.cs new file mode 100644 index 0000000000..e0ececd9cf --- /dev/null +++ b/Wox.UAC/FileTypeAssociateInstaller.cs @@ -0,0 +1,70 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Runtime.InteropServices; +using System.Windows; +using ICSharpCode.SharpZipLib.Zip; +using Microsoft.Win32; +using Newtonsoft.Json; +using Wox.Plugin; + +namespace Wox.UAC +{ + public class FileTypeAssociateInstaller + { + [DllImport("shell32.dll")] + private static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2); + + /// + /// associate filetype with specified program + /// + /// + /// + /// + /// + private static void SaveReg(string filePath, string fileType, string iconPath, bool overrides) + { + RegistryKey classRootKey = Registry.ClassesRoot.OpenSubKey("", true); + RegistryKey woxKey = classRootKey.OpenSubKey(fileType, true); + if (woxKey != null) + { + if (!overrides) + { + return; + } + classRootKey.DeleteSubKeyTree(fileType); + } + classRootKey.CreateSubKey(fileType); + woxKey = classRootKey.OpenSubKey(fileType, true); + woxKey.SetValue("", "wox.wox"); + woxKey.SetValue("Content Type", "application/wox"); + + RegistryKey iconKey = woxKey.CreateSubKey("DefaultIcon"); + iconKey.SetValue("", iconPath); + + woxKey.CreateSubKey("shell"); + RegistryKey shellKey = woxKey.OpenSubKey("shell", true); + shellKey.SetValue("", "Open"); + RegistryKey openKey = shellKey.CreateSubKey("open"); + openKey.SetValue("", "Open with wox"); + + openKey = shellKey.OpenSubKey("open", true); + openKey.CreateSubKey("command"); + RegistryKey commandKey = openKey.OpenSubKey("command", true); + string pathString = "\"" + filePath + "\" \"installPlugin\" \"%1\""; + commandKey.SetValue("", pathString); + + //refresh cache + SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero); + } + + public void RegisterInstaller() + { + string filePath = Directory.GetCurrentDirectory() + "\\Wox.exe"; + string iconPath = Directory.GetCurrentDirectory() + "\\app.ico"; + + SaveReg(filePath, ".wox", iconPath, true); + } + + } +} diff --git a/Wox.UAC/MainWindow.xaml.cs b/Wox.UAC/MainWindow.xaml.cs index 9b8e500fc1..bcf8e9780a 100644 --- a/Wox.UAC/MainWindow.xaml.cs +++ b/Wox.UAC/MainWindow.xaml.cs @@ -6,7 +6,7 @@ namespace Wox.UAC { public partial class MainWindow : Window { - PluginInstaller installer = new PluginInstaller(); + FileTypeAssociateInstaller installer = new FileTypeAssociateInstaller(); public MainWindow() { @@ -16,32 +16,12 @@ namespace Wox.UAC { switch (param[1]) { - case "UAC": - Invoke(param[2], param[3], param[4]); - break; - case "AssociatePluginInstaller": installer.RegisterInstaller(); break; - - case "InstallPlugin": - var path = param[2]; - installer.Install(path); - break; } } Application.Current.Shutdown(0); } - - private static void Invoke(string namespaceName, string className, string methodName) - { - Type type = Type.GetType(namespaceName + "." + className + "," + namespaceName); - if (type != null) - { - object instance = Activator.CreateInstance(type); - MethodInfo method = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance); - if (method != null) method.Invoke(instance, null); - } - } } } diff --git a/Wox.UAC/Wox.UAC.csproj b/Wox.UAC/Wox.UAC.csproj index 57d5bf8398..c69e5857fb 100644 --- a/Wox.UAC/Wox.UAC.csproj +++ b/Wox.UAC/Wox.UAC.csproj @@ -82,7 +82,7 @@ - + Code diff --git a/Wox.UAC/PluginInstaller.cs b/Wox/Helper/PluginInstaller.cs similarity index 75% rename from Wox.UAC/PluginInstaller.cs rename to Wox/Helper/PluginInstaller.cs index 044a84c660..44ccdb3481 100644 --- a/Wox.UAC/PluginInstaller.cs +++ b/Wox/Helper/PluginInstaller.cs @@ -1,72 +1,20 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.Runtime.InteropServices; +using System.Linq; +using System.Text; using System.Windows; using ICSharpCode.SharpZipLib.Zip; -using Microsoft.Win32; using Newtonsoft.Json; using Wox.Plugin; -namespace Wox.UAC +namespace Wox.Helper { public class PluginInstaller { - [DllImport("shell32.dll")] - private static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2); - /// - /// associate filetype with specified program - /// - /// - /// - /// - /// - private static void SaveReg(string filePath, string fileType, string iconPath, bool overrides) - { - RegistryKey classRootKey = Registry.ClassesRoot.OpenSubKey("", true); - RegistryKey woxKey = classRootKey.OpenSubKey(fileType, true); - if (woxKey != null) - { - if (!overrides) - { - return; - } - classRootKey.DeleteSubKeyTree(fileType); - } - classRootKey.CreateSubKey(fileType); - woxKey = classRootKey.OpenSubKey(fileType, true); - woxKey.SetValue("", "wox.wox"); - woxKey.SetValue("Content Type", "application/wox"); - - RegistryKey iconKey = woxKey.CreateSubKey("DefaultIcon"); - iconKey.SetValue("", iconPath); - - woxKey.CreateSubKey("shell"); - RegistryKey shellKey = woxKey.OpenSubKey("shell", true); - shellKey.SetValue("", "Open"); - RegistryKey openKey = shellKey.CreateSubKey("open"); - openKey.SetValue("", "Open with wox"); - - openKey = shellKey.OpenSubKey("open", true); - openKey.CreateSubKey("command"); - RegistryKey commandKey = openKey.OpenSubKey("command", true); - string pathString = "\"" + filePath + "\" \"installPlugin\" \"%1\""; - commandKey.SetValue("", pathString); - - //refresh cache - SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero); - } - - public void RegisterInstaller() - { - string filePath = Directory.GetCurrentDirectory() + "\\Wox.UAC.exe"; - string iconPath = Directory.GetCurrentDirectory() + "\\app.ico"; - - SaveReg(filePath, ".wox", iconPath, true); - } - - public void Install(string path) + public static void Install(string path) { if (File.Exists(path)) { @@ -149,7 +97,7 @@ namespace Wox.UAC } private static PluginMetadata GetMetadataFromJson(string pluginDirectory) - { + { string configPath = Path.Combine(pluginDirectory, "plugin.json"); PluginMetadata metadata; @@ -208,7 +156,7 @@ namespace Wox.UAC /// The ziped file. /// The STR directory. /// overwirte - private void UnZip(string zipedFile, string strDirectory, bool overWrite) + private static void UnZip(string zipedFile, string strDirectory, bool overWrite) { if (strDirectory == "") strDirectory = Directory.GetCurrentDirectory(); diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 81a145771f..9454e9a205 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -230,6 +230,16 @@ namespace Wox case "hidestart": HideApp(); break; + + case "installplugin": + var path = args[1]; + if (!File.Exists(path)) + { + MessageBox.Show("Plugin " + path + " didn't exist"); + return; + } + PluginInstaller.Install(path); + break; } } } diff --git a/Wox/SettingWindow.xaml b/Wox/SettingWindow.xaml index 5c3c5f390b..442f34085f 100644 --- a/Wox/SettingWindow.xaml +++ b/Wox/SettingWindow.xaml @@ -23,7 +23,9 @@ - + + + diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index 9e85b6d547..919cf73d64 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Windows; @@ -208,6 +209,9 @@ namespace Wox #endregion - + private void BtnEnableInstaller_OnClick(object sender, RoutedEventArgs e) + { + Process.Start("Wox.UAC.exe", "AssociatePluginInstaller"); + } } } diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index ab2cbd4ef0..3506ed8de6 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -130,6 +130,7 @@ +