diff --git a/Wox.Plugin.PluginBackup/Main.cs b/Wox.Plugin.PluginBackup/Main.cs index c55b5a3..f35efea 100644 --- a/Wox.Plugin.PluginBackup/Main.cs +++ b/Wox.Plugin.PluginBackup/Main.cs @@ -6,6 +6,7 @@ namespace Wox.Plugin.PluginBackup { + //defines strings of wox's paths public static class GlobalState { private static string _plugin_name = "Wox.Plugin.PluginBackup"; @@ -16,11 +17,13 @@ public static class GlobalState public static void Init() { + //get wox's root path by this dll's path UriBuilder uri = new UriBuilder(Assembly.GetExecutingAssembly().CodeBase); _plugin_path = Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path)); _wox_path = _plugin_path.Remove(_plugin_path.LastIndexOf(Path.DirectorySeparatorChar)); _wox_path = _wox_path.Remove(_wox_path.LastIndexOf(Path.DirectorySeparatorChar)); + //get desktop's directory(where plugins backup file will be save) _desktop_path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); } public static string PluginName() { return _plugin_name; } @@ -47,20 +50,48 @@ public void Init(PluginInitContext context) public List Query(Query query) { - return null; + List results = new List(); + const string ico = "Images\\pic.png"; + string path = ""; + if (query.RawQuery.IndexOf(' ') != -1) + { + path = query.RawQuery.Substring(query.RawQuery.IndexOf(' ')); + } + path = path.Length == 0 ? GlobalState.DesktopDir() + Path.DirectorySeparatorChar + GlobalState.BackupFileName() : path; + results.Add(new Result + { + Title = "backup to:", + SubTitle = path, + IcoPath = ico, + Action = e => + { + Backup(path); + return true; + } + }); + results.Add(new Result + { + Title = "restore from:", + SubTitle = path, + IcoPath = ico, + Action = e => + { + Restore(path); + return true; + } + }); + return results; } - public void Backup(string path) { - path = path.Length == 0 ? GlobalState.DesktopDir() + Path.DirectorySeparatorChar + GlobalState.BackupFileName() : path; if (File.Exists(path)) { try { File.Delete(path); } - catch (System.IO.IOException) + catch (IOException) { return; } @@ -81,7 +112,6 @@ private void AddDirToZip(ZipFile zip, string dir) public void Restore(string path) { - path = path.Length == 0 ? GlobalState.DesktopDir() + Path.DirectorySeparatorChar + GlobalState.BackupFileName() : path; ZipFile zip = ZipFile.Read(path); List existed_plugins = new List(); foreach (string pd in Directory.GetDirectories(GlobalState.WoxPluginsDir())) @@ -93,11 +123,25 @@ public void Restore(string path) } foreach (ZipEntry entry in zip) { - string filename = entry.FileName.Remove(entry.FileName.IndexOf('-')).Remove(0, entry.FileName.IndexOf('/') + 1); - if (!existed_plugins.Contains(filename)) - - entry.Extract(GlobalState.WoxDir()); + bool existed = false; + foreach (string name in existed_plugins) + { + if (entry.FileName.Contains(name)) + existed = true; + } + if (!existed) + { + try + { + entry.Extract(GlobalState.WoxDir()); + } + catch (Exception) + { + continue; + } + } } + //kill wox directly because when you exit wox normally it will rewrite its setting files System.Diagnostics.Process.GetCurrentProcess().Kill(); } } diff --git a/Wox.Plugin.PluginBackup/Wox.Plugin.PluginBackup.csproj b/Wox.Plugin.PluginBackup/Wox.Plugin.PluginBackup.csproj index e6d1e62..f3f93b9 100644 --- a/Wox.Plugin.PluginBackup/Wox.Plugin.PluginBackup.csproj +++ b/Wox.Plugin.PluginBackup/Wox.Plugin.PluginBackup.csproj @@ -18,7 +18,7 @@ true full false - bin\Debug\ + C:\Users\Miles\AppData\Roaming\Wox\Plugins\Wox.Plugin.PluginBackup\ DEBUG;TRACE prompt 4