Fix issue#24

This commit is contained in:
qianlifeng
2014-03-11 22:17:10 +08:00
parent 8ef6827bd9
commit 9adf764af8
10 changed files with 98 additions and 179 deletions

View File

@@ -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
{
/// <summary>
/// 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));
/// }
///
/// </summary>
/// <param name="method"></param>
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;
}
}
}

View File

@@ -53,7 +53,6 @@
<Compile Include="GloablHotkey.cs" /> <Compile Include="GloablHotkey.cs" />
<Compile Include="HotkeyModel.cs" /> <Compile Include="HotkeyModel.cs" />
<Compile Include="IniParser.cs" /> <Compile Include="IniParser.cs" />
<Compile Include="UAC.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UserSettings\PluginHotkey.cs" /> <Compile Include="UserSettings\PluginHotkey.cs" />
<Compile Include="UserSettings\UserSelectedRecords.cs" /> <Compile Include="UserSettings\UserSelectedRecords.cs" />

View File

@@ -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);
/// <summary>
/// associate filetype with specified program
/// </summary>
/// <param name="filePath"></param>
/// <param name="fileType"></param>
/// <param name="iconPath"></param>
/// <param name="overrides"></param>
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);
}
}
}

View File

@@ -6,7 +6,7 @@ namespace Wox.UAC
{ {
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
PluginInstaller installer = new PluginInstaller(); FileTypeAssociateInstaller installer = new FileTypeAssociateInstaller();
public MainWindow() public MainWindow()
{ {
@@ -16,32 +16,12 @@ namespace Wox.UAC
{ {
switch (param[1]) switch (param[1])
{ {
case "UAC":
Invoke(param[2], param[3], param[4]);
break;
case "AssociatePluginInstaller": case "AssociatePluginInstaller":
installer.RegisterInstaller(); installer.RegisterInstaller();
break; break;
case "InstallPlugin":
var path = param[2];
installer.Install(path);
break;
} }
} }
Application.Current.Shutdown(0); 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);
}
}
} }
} }

View File

@@ -82,7 +82,7 @@
</Compile> </Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="PluginInstaller.cs" /> <Compile Include="FileTypeAssociateInstaller.cs" />
<Compile Include="Properties\AssemblyInfo.cs"> <Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>

View File

@@ -1,72 +1,20 @@
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Linq;
using System.Text;
using System.Windows; using System.Windows;
using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.Zip;
using Microsoft.Win32;
using Newtonsoft.Json; using Newtonsoft.Json;
using Wox.Plugin; using Wox.Plugin;
namespace Wox.UAC namespace Wox.Helper
{ {
public class PluginInstaller public class PluginInstaller
{ {
[DllImport("shell32.dll")]
private static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
/// <summary> public static void Install(string path)
/// associate filetype with specified program
/// </summary>
/// <param name="filePath"></param>
/// <param name="fileType"></param>
/// <param name="iconPath"></param>
/// <param name="overrides"></param>
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)
{ {
if (File.Exists(path)) if (File.Exists(path))
{ {
@@ -149,7 +97,7 @@ namespace Wox.UAC
} }
private static PluginMetadata GetMetadataFromJson(string pluginDirectory) private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
{ {
string configPath = Path.Combine(pluginDirectory, "plugin.json"); string configPath = Path.Combine(pluginDirectory, "plugin.json");
PluginMetadata metadata; PluginMetadata metadata;
@@ -208,7 +156,7 @@ namespace Wox.UAC
/// <param name="zipedFile">The ziped file.</param> /// <param name="zipedFile">The ziped file.</param>
/// <param name="strDirectory">The STR directory.</param> /// <param name="strDirectory">The STR directory.</param>
/// <param name="overWrite">overwirte</param> /// <param name="overWrite">overwirte</param>
private void UnZip(string zipedFile, string strDirectory, bool overWrite) private static void UnZip(string zipedFile, string strDirectory, bool overWrite)
{ {
if (strDirectory == "") if (strDirectory == "")
strDirectory = Directory.GetCurrentDirectory(); strDirectory = Directory.GetCurrentDirectory();

View File

@@ -230,6 +230,16 @@ namespace Wox
case "hidestart": case "hidestart":
HideApp(); HideApp();
break; break;
case "installplugin":
var path = args[1];
if (!File.Exists(path))
{
MessageBox.Show("Plugin " + path + " didn't exist");
return;
}
PluginInstaller.Install(path);
break;
} }
} }
} }

View File

@@ -23,7 +23,9 @@
<TextBlock Text="Theme:" /> <TextBlock Text="Theme:" />
<ComboBox x:Name="themeComboBox" SelectionChanged="ThemeComboBox_OnSelectionChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"/> <ComboBox x:Name="themeComboBox" SelectionChanged="ThemeComboBox_OnSelectionChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"/>
</StackPanel> </StackPanel>
<StackPanel Orientation="Horizontal" Margin="10">
<Button x:Name="btnEnableInstaller" Click="BtnEnableInstaller_OnClick">enable plugin installer</Button>
</StackPanel>
</StackPanel> </StackPanel>
</TabItem> </TabItem>
<TabItem Header="Hotkey"> <TabItem Header="Hotkey">

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
@@ -208,6 +209,9 @@ namespace Wox
#endregion #endregion
private void BtnEnableInstaller_OnClick(object sender, RoutedEventArgs e)
{
Process.Start("Wox.UAC.exe", "AssociatePluginInstaller");
}
} }
} }

View File

@@ -130,6 +130,7 @@
</Compile> </Compile>
<Compile Include="DispatcherExtensions.cs" /> <Compile Include="DispatcherExtensions.cs" />
<Compile Include="Helper\Log.cs" /> <Compile Include="Helper\Log.cs" />
<Compile Include="Helper\PluginInstaller.cs" />
<Compile Include="Helper\WoxException.cs" /> <Compile Include="Helper\WoxException.cs" />
<Compile Include="Helper\WoxPythonException.cs" /> <Compile Include="Helper\WoxPythonException.cs" />
<Compile Include="HotkeyControl.xaml.cs"> <Compile Include="HotkeyControl.xaml.cs">