Add start Wox on system startup config & code refactorying.

This commit is contained in:
qianlifeng
2014-02-06 22:22:02 +08:00
parent 9e1980f843
commit 67f14d6a62
33 changed files with 818 additions and 811 deletions

View File

@@ -1,217 +0,0 @@
using System;
using System.Collections;
using System.IO;
namespace Wox.Helper
{
public class IniParser
{
private Hashtable keyPairs = new Hashtable();
private String iniFilePath;
private struct SectionPair
{
public String Section;
public String Key;
}
/// <summary>
/// Opens the INI file at the given path and enumerates the values in the IniParser.
/// </summary>
/// <param name="iniPath">Full path to INI file.</param>
public IniParser(String iniPath)
{
TextReader iniFile = null;
String strLine = null;
String currentRoot = null;
String[] keyPair = null;
iniFilePath = iniPath;
if (File.Exists(iniPath))
{
try
{
iniFile = new StreamReader(iniPath);
strLine = iniFile.ReadLine();
while (strLine != null)
{
strLine = strLine.Trim();
if (strLine != "")
{
if (strLine.StartsWith("[") && strLine.EndsWith("]"))
{
currentRoot = strLine.Substring(1, strLine.Length - 2).ToUpper();
}
else
{
keyPair = strLine.Split(new char[] { '=' }, 2);
SectionPair sectionPair;
String value = null;
if (currentRoot == null)
currentRoot = "ROOT";
sectionPair.Section = currentRoot;
sectionPair.Key = keyPair[0].ToUpper().Trim();
if (keyPair.Length > 1)
value = keyPair[1];
keyPairs.Add(sectionPair, value.Trim());
}
}
strLine = iniFile.ReadLine();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (iniFile != null)
iniFile.Close();
}
}
else
throw new FileNotFoundException("Unable to locate " + iniPath);
}
/// <summary>
/// Returns the value for the given section, key pair.
/// </summary>
/// <param name="sectionName">Section name.</param>
/// <param name="settingName">Key name.</param>
public String GetSetting(String sectionName, String settingName)
{
SectionPair sectionPair;
sectionPair.Section = sectionName.ToUpper().Trim();
sectionPair.Key = settingName.ToUpper().Trim();
return (String)keyPairs[sectionPair];
}
/// <summary>
/// Enumerates all lines for given section.
/// </summary>
/// <param name="sectionName">Section to enum.</param>
public String[] EnumSection(String sectionName)
{
ArrayList tmpArray = new ArrayList();
foreach (SectionPair pair in keyPairs.Keys)
{
if (pair.Section == sectionName.ToUpper())
tmpArray.Add(pair.Key);
}
return (String[])tmpArray.ToArray(typeof(String));
}
/// <summary>
/// Adds or replaces a setting to the table to be saved.
/// </summary>
/// <param name="sectionName">Section to add under.</param>
/// <param name="settingName">Key name to add.</param>
/// <param name="settingValue">Value of key.</param>
public void AddSetting(String sectionName, String settingName, String settingValue)
{
SectionPair sectionPair;
sectionPair.Section = sectionName.ToUpper();
sectionPair.Key = settingName.ToUpper();
if (keyPairs.ContainsKey(sectionPair))
keyPairs.Remove(sectionPair);
keyPairs.Add(sectionPair, settingValue);
}
/// <summary>
/// Adds or replaces a setting to the table to be saved with a null value.
/// </summary>
/// <param name="sectionName">Section to add under.</param>
/// <param name="settingName">Key name to add.</param>
public void AddSetting(String sectionName, String settingName)
{
AddSetting(sectionName, settingName, null);
}
/// <summary>
/// Remove a setting.
/// </summary>
/// <param name="sectionName">Section to add under.</param>
/// <param name="settingName">Key name to add.</param>
public void DeleteSetting(String sectionName, String settingName)
{
SectionPair sectionPair;
sectionPair.Section = sectionName.ToUpper();
sectionPair.Key = settingName.ToUpper();
if (keyPairs.ContainsKey(sectionPair))
keyPairs.Remove(sectionPair);
}
/// <summary>
/// Save settings to new file.
/// </summary>
/// <param name="newFilePath">New file path.</param>
public void SaveSettings(String newFilePath)
{
ArrayList sections = new ArrayList();
String tmpValue = "";
String strToSave = "";
foreach (SectionPair sectionPair in keyPairs.Keys)
{
if (!sections.Contains(sectionPair.Section))
sections.Add(sectionPair.Section);
}
foreach (String section in sections)
{
strToSave += ("[" + section + "]\r\n");
foreach (SectionPair sectionPair in keyPairs.Keys)
{
if (sectionPair.Section == section)
{
tmpValue = (String)keyPairs[sectionPair];
if (tmpValue != null)
tmpValue = "=" + tmpValue;
strToSave += (sectionPair.Key + tmpValue + "\r\n");
}
}
strToSave += "\r\n";
}
try
{
TextWriter tw = new StreamWriter(newFilePath);
tw.Write(strToSave);
tw.Close();
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Save settings back to ini file.
/// </summary>
public void SaveSettings()
{
SaveSettings(iniFilePath);
}
}
}

View File

@@ -0,0 +1,252 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.Win32;
using Wox.Infrastructure;
using Wox.Plugin;
namespace Wox.Helper
{
public class PluginInstaller
{
[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 + "\" \"%1\"";
commandKey.SetValue("", pathString);
//refresh cache
SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero);
}
public void RegisterInstaller()
{
string filePath = Directory.GetCurrentDirectory() + "\\Wox.Installer.exe";
string iconPath = Directory.GetCurrentDirectory() + "\\app.ico";
SaveReg(filePath, ".wox", iconPath, false);
}
public void Install(string path)
{
if (File.Exists(path))
{
string tempFoler = System.IO.Path.GetTempPath() + "\\wox\\workflows";
if (Directory.Exists(tempFoler))
{
Directory.Delete(tempFoler, true);
}
UnZip(path, tempFoler, true);
string iniPath = tempFoler + "\\plugin.ini";
if (!File.Exists(iniPath))
{
MessageBox.Show("Install failed: config is missing");
return;
}
PluginMetadata plugin = GetMetadataFromIni(tempFoler);
if (plugin == null || plugin.Name == null)
{
MessageBox.Show("Install failed: config of this workflow is invalid");
return;
}
string pluginFolerPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins";
if (!Directory.Exists(pluginFolerPath))
{
MessageBox.Show("Install failed: cound't find workflow directory");
return;
}
string newPluginPath = pluginFolerPath + "\\" + plugin.Name;
string content = string.Format(
"Do you want to install following workflow?\r\nName: {0}\r\nVersion: {1}\r\nAuthor: {2}",
plugin.Name, plugin.Version, plugin.Author);
if (Directory.Exists(newPluginPath))
{
PluginMetadata existingPlugin = GetMetadataFromIni(newPluginPath);
if (existingPlugin == null || existingPlugin.Name == null)
{
//maybe broken plugin, just delete it
Directory.Delete(newPluginPath, true);
}
else
{
content = string.Format(
"Do you want to update following workflow?\r\nName: {0}\r\nOld Version: {1}\r\nNew Version: {2}\r\nAuthor: {3}",
plugin.Name, existingPlugin.Version, plugin.Version, plugin.Author);
}
}
MessageBoxResult result = MessageBox.Show(content, "Install workflow",
MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
if (Directory.Exists(newPluginPath))
{
Directory.Delete(newPluginPath, true);
}
UnZip(path, newPluginPath, true);
Directory.Delete(tempFoler, true);
string wox = AppDomain.CurrentDomain.BaseDirectory + "Wox.exe";
if (File.Exists(wox))
{
ProcessStartInfo info = new ProcessStartInfo(wox, "reloadWorkflows")
{
UseShellExecute = true
};
Process.Start(info);
MessageBox.Show("You have installed workflow " + plugin.Name + " successfully.");
}
else
{
MessageBox.Show("You have installed workflow " + plugin.Name + " successfully. Please restart your wox to use new workflow.");
}
}
}
}
private PluginMetadata GetMetadataFromIni(string directory)
{
string iniPath = directory + "\\plugin.ini";
if (!File.Exists(iniPath))
{
return null;
}
try
{
PluginMetadata metadata = new PluginMetadata();
IniParser ini = new IniParser(iniPath);
metadata.Name = ini.GetSetting("plugin", "Name");
metadata.Author = ini.GetSetting("plugin", "Author");
metadata.Description = ini.GetSetting("plugin", "Description");
metadata.Language = ini.GetSetting("plugin", "Language");
metadata.Version = ini.GetSetting("plugin", "Version");
metadata.PluginType = PluginType.ThirdParty;
metadata.ActionKeyword = ini.GetSetting("plugin", "ActionKeyword");
metadata.ExecuteFilePath = directory + "\\" + ini.GetSetting("plugin", "ExecuteFile");
metadata.PluginDirecotry = directory + "\\";
metadata.ExecuteFileName = ini.GetSetting("plugin", "ExecuteFile");
if (!AllowedLanguage.IsAllowed(metadata.Language))
{
string error = string.Format("Parse ini {0} failed: invalid language {1}", iniPath,
metadata.Language);
return null;
}
if (!File.Exists(metadata.ExecuteFilePath))
{
string error = string.Format("Parse ini {0} failed: ExecuteFilePath didn't exist {1}", iniPath,
metadata.ExecuteFilePath);
return null;
}
return metadata;
}
catch (Exception e)
{
return null;
}
}
/// <summary>
/// unzip
/// </summary>
/// <param name="zipedFile">The ziped file.</param>
/// <param name="strDirectory">The STR directory.</param>
/// <param name="overWrite">overwirte</param>
private void UnZip(string zipedFile, string strDirectory, bool overWrite)
{
if (strDirectory == "")
strDirectory = Directory.GetCurrentDirectory();
if (!strDirectory.EndsWith("\\"))
strDirectory = strDirectory + "\\";
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name;
if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + "\\";
string fileName = Path.GetFileName(pathToZip);
Directory.CreateDirectory(strDirectory + directoryName);
if (fileName != "")
{
if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
{
using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
{
byte[] data = new byte[2048];
while (true)
{
int size = s.Read(data, 0, data.Length);
if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}
streamWriter.Close();
}
}
}
}
s.Close();
}
}
}
}