From 7385243c1d1e88158c1bf23dc539333719b3d4e9 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Fri, 21 Feb 2020 08:27:46 +1100 Subject: [PATCH 01/33] Add enable portable mode logic --- Wox.Core/Configuration/IPortable.cs | 13 +++++ Wox.Core/Configuration/Portable.cs | 68 +++++++++++++++++++++++++ Wox.Core/Wox.Core.csproj | 2 + Wox.Infrastructure/Wox.cs | 2 + Wox/App.xaml.cs | 4 +- Wox/ViewModel/SettingWindowViewModel.cs | 10 +++- 6 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 Wox.Core/Configuration/IPortable.cs create mode 100644 Wox.Core/Configuration/Portable.cs diff --git a/Wox.Core/Configuration/IPortable.cs b/Wox.Core/Configuration/IPortable.cs new file mode 100644 index 000000000..e92db779a --- /dev/null +++ b/Wox.Core/Configuration/IPortable.cs @@ -0,0 +1,13 @@ + +namespace Wox.Core.Configuration +{ + public interface IPortable + { + void EnablePortableMode(); + void DisablePortableMode(); + void RemoveShortcuts(); + void RemoveUninstallerEntry(); + bool IsPortableModeEnabled(); + void MoveUserDataFolder(string fromLocation, string toLocation); + } +} \ No newline at end of file diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs new file mode 100644 index 000000000..92f6fac2d --- /dev/null +++ b/Wox.Core/Configuration/Portable.cs @@ -0,0 +1,68 @@ +using Squirrel; +using System; +using Wox.Infrastructure; + +namespace Wox.Core.Configuration +{ + public class Portable : IPortable + { + private string applicationName; + private string exeName; + private string rootAppDirectory; + private UpdateManager portabilityUpdater; + + public Portable() + { + applicationName = Constant.Wox; + exeName = applicationName + ".exe"; + rootAppDirectory = Constant.RootDirectory; + portabilityUpdater = new UpdateManager(string.Empty, applicationName, rootAppDirectory); + } + + public void DisablePortableMode() + { + throw new NotImplementedException(); + } + + public void EnablePortableMode() + { + try + { + RemoveShortcuts(); + RemoveUninstallerEntry(); + } + catch (Exception e) + { + //log and update error message to output above locations where shortcuts may not have been removed +#if DEBUG + throw; +#else + throw;// PRODUCTION LOGGING AND CONTINUE + +#endif + } + } + + public bool IsPortableModeEnabled() + { + throw new NotImplementedException(); + } + + public void RemoveShortcuts() + { + portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.StartMenu); + portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.Desktop); + portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.Startup); + } + + public void RemoveUninstallerEntry() + { + portabilityUpdater.RemoveUninstallerRegistryEntry(); + } + + public void MoveUserDataFolder(string fromLocation, string toLocation) + { + throw new NotImplementedException(); + } + } +} diff --git a/Wox.Core/Wox.Core.csproj b/Wox.Core/Wox.Core.csproj index c1758fdb3..b3d0adc8b 100644 --- a/Wox.Core/Wox.Core.csproj +++ b/Wox.Core/Wox.Core.csproj @@ -51,6 +51,8 @@ Properties\SolutionAssemblyInfo.cs + + diff --git a/Wox.Infrastructure/Wox.cs b/Wox.Infrastructure/Wox.cs index 396ee0bb1..61e2b411f 100644 --- a/Wox.Infrastructure/Wox.cs +++ b/Wox.Infrastructure/Wox.cs @@ -25,6 +25,8 @@ public static string DetermineDataDirectory() private static readonly Assembly Assembly = Assembly.GetExecutingAssembly(); public static readonly string ProgramDirectory = Directory.GetParent(Assembly.Location.NonNull()).ToString(); + private static readonly string ApplicationDirectory = Directory.GetParent(ProgramDirectory).ToString(); + public static readonly string RootDirectory = Directory.GetParent(ApplicationDirectory).ToString(); public static readonly string ExecutablePath = Path.Combine(ProgramDirectory, Wox + ".exe"); public static readonly string DataDirectory = DetermineDataDirectory(); public static readonly string PluginsDirectory = Path.Combine(DataDirectory, Plugins); diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index 0fc1e8c95..96c2858af 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -4,6 +4,7 @@ using System.Timers; using System.Windows; using Wox.Core; +using Wox.Core.Configuration; using Wox.Core.Plugin; using Wox.Core.Resource; using Wox.Helper; @@ -26,6 +27,7 @@ public partial class App : IDisposable, ISingleInstanceApp private MainViewModel _mainVM; private SettingWindowViewModel _settingsVM; private readonly Updater _updater = new Updater(Wox.Properties.Settings.Default.GithubRepo); + private readonly Portable _portable = new Portable(); private readonly Alphabet _alphabet = new Alphabet(); private StringMatcher _stringMatcher; @@ -53,7 +55,7 @@ private void OnStartup(object sender, StartupEventArgs e) ImageLoader.Initialize(); - _settingsVM = new SettingWindowViewModel(_updater); + _settingsVM = new SettingWindowViewModel(_updater, _portable); _settings = _settingsVM.Settings; _alphabet.Initialize(_settings); diff --git a/Wox/ViewModel/SettingWindowViewModel.cs b/Wox/ViewModel/SettingWindowViewModel.cs index 923e456b0..b827130b1 100644 --- a/Wox/ViewModel/SettingWindowViewModel.cs +++ b/Wox/ViewModel/SettingWindowViewModel.cs @@ -8,6 +8,7 @@ using System.Windows.Media; using System.Windows.Media.Imaging; using Wox.Core; +using Wox.Core.Configuration; using Wox.Core.Plugin; using Wox.Core.Resource; using Wox.Helper; @@ -22,11 +23,13 @@ namespace Wox.ViewModel public class SettingWindowViewModel : BaseModel { private readonly Updater _updater; + private readonly IPortable _portable; private readonly WoxJsonStorage _storage; - public SettingWindowViewModel(Updater updater) + public SettingWindowViewModel(Updater updater, IPortable portable) { _updater = updater; + _portable = portable; _storage = new WoxJsonStorage(); Settings = _storage.Load(); Settings.PropertyChanged += (s, e) => @@ -47,6 +50,11 @@ public async void UpdateApp() await _updater.UpdateApp(); } + public void EnablePortableMode() + { + _portable.EnablePortableMode(); + } + public void Save() { _storage.Save(); From d55560ea97c92528b25c71a85e8e0ba40c36bec5 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Fri, 21 Feb 2020 22:06:04 +1100 Subject: [PATCH 02/33] Add UserData operations- Copy, verify, remove --- Wox.Core/Configuration/IPortable.cs | 1 + Wox.Core/Configuration/Portable.cs | 16 ++++- Wox.Infrastructure/Wox.cs | 27 ++++---- Wox.Plugin/SharedCommands/FilesFolders.cs | 76 +++++++++++++++++++++++ Wox.Plugin/Wox.Plugin.csproj | 1 + 5 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 Wox.Plugin/SharedCommands/FilesFolders.cs diff --git a/Wox.Core/Configuration/IPortable.cs b/Wox.Core/Configuration/IPortable.cs index e92db779a..74e32fecd 100644 --- a/Wox.Core/Configuration/IPortable.cs +++ b/Wox.Core/Configuration/IPortable.cs @@ -9,5 +9,6 @@ public interface IPortable void RemoveUninstallerEntry(); bool IsPortableModeEnabled(); void MoveUserDataFolder(string fromLocation, string toLocation); + void VerifyUserDataAfterMove(string fromLocation, string toLocation); } } \ No newline at end of file diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 92f6fac2d..5f5a60be6 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -1,6 +1,7 @@ using Squirrel; using System; using Wox.Infrastructure; +using Wox.Plugin.SharedCommands; namespace Wox.Core.Configuration { @@ -10,6 +11,8 @@ public class Portable : IPortable private string exeName; private string rootAppDirectory; private UpdateManager portabilityUpdater; + private string roamingDataPath; + private string portableDataPath; public Portable() { @@ -17,6 +20,9 @@ public Portable() exeName = applicationName + ".exe"; rootAppDirectory = Constant.RootDirectory; portabilityUpdater = new UpdateManager(string.Empty, applicationName, rootAppDirectory); + + roamingDataPath = Constant.RoamingDataPath; + portableDataPath = Constant.PortableDataPath; } public void DisablePortableMode() @@ -28,6 +34,7 @@ public void EnablePortableMode() { try { + MoveUserDataFolder(roamingDataPath, portableDataPath); RemoveShortcuts(); RemoveUninstallerEntry(); } @@ -62,7 +69,14 @@ public void RemoveUninstallerEntry() public void MoveUserDataFolder(string fromLocation, string toLocation) { - throw new NotImplementedException(); + FilesFolders.Copy(fromLocation, toLocation); + VerifyUserDataAfterMove(fromLocation, toLocation); + FilesFolders.RemoveFolder(fromLocation); + } + + public void VerifyUserDataAfterMove(string fromLocation, string toLocation) + { + FilesFolders.VerifyBothFolderFilesEqual(fromLocation, toLocation); } } } diff --git a/Wox.Infrastructure/Wox.cs b/Wox.Infrastructure/Wox.cs index 61e2b411f..8507ee393 100644 --- a/Wox.Infrastructure/Wox.cs +++ b/Wox.Infrastructure/Wox.cs @@ -7,27 +7,30 @@ namespace Wox.Infrastructure { public static class Constant { + public const string Wox = "Wox"; + public const string Plugins = "Plugins"; + + private static readonly Assembly Assembly = Assembly.GetExecutingAssembly(); + public static readonly string ProgramDirectory = Directory.GetParent(Assembly.Location.NonNull()).ToString(); + public static readonly string ExecutablePath = Path.Combine(ProgramDirectory, Wox + ".exe"); + private static readonly string ApplicationDirectory = Directory.GetParent(ProgramDirectory).ToString(); + public static readonly string RootDirectory = Directory.GetParent(ApplicationDirectory).ToString(); + + public const string PortableFolderName = "UserData"; + public static string PortableDataPath = Path.Combine(ProgramDirectory, PortableFolderName); + public static string RoamingDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Wox); public static string DetermineDataDirectory() { - string portableDataPath = Path.Combine(ProgramDirectory, "UserData"); - if (Directory.Exists(portableDataPath)) + if (Directory.Exists(PortableDataPath)) { - return portableDataPath; + return PortableDataPath; } else { - return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Wox); + return RoamingDataPath; } } - public const string Wox = "Wox"; - public const string Plugins = "Plugins"; - - private static readonly Assembly Assembly = Assembly.GetExecutingAssembly(); - public static readonly string ProgramDirectory = Directory.GetParent(Assembly.Location.NonNull()).ToString(); - private static readonly string ApplicationDirectory = Directory.GetParent(ProgramDirectory).ToString(); - public static readonly string RootDirectory = Directory.GetParent(ApplicationDirectory).ToString(); - public static readonly string ExecutablePath = Path.Combine(ProgramDirectory, Wox + ".exe"); public static readonly string DataDirectory = DetermineDataDirectory(); public static readonly string PluginsDirectory = Path.Combine(DataDirectory, Plugins); public static readonly string PreinstalledDirectory = Path.Combine(ProgramDirectory, Plugins); diff --git a/Wox.Plugin/SharedCommands/FilesFolders.cs b/Wox.Plugin/SharedCommands/FilesFolders.cs new file mode 100644 index 000000000..b3f0a3c8b --- /dev/null +++ b/Wox.Plugin/SharedCommands/FilesFolders.cs @@ -0,0 +1,76 @@ +using System.IO; + +namespace Wox.Plugin.SharedCommands +{ + public static class FilesFolders + { + public static void Copy(this string sourcePath, string targetPath) + { + // Get the subdirectories for the specified directory. + DirectoryInfo dir = new DirectoryInfo(sourcePath); + + if (!dir.Exists) + { + throw new DirectoryNotFoundException( + "Source directory does not exist or could not be found: " + + sourcePath); + } + + DirectoryInfo[] dirs = dir.GetDirectories(); + // If the destination directory doesn't exist, create it. + if (!Directory.Exists(targetPath)) + { + Directory.CreateDirectory(targetPath); + } + + // Get the files in the directory and copy them to the new location. + FileInfo[] files = dir.GetFiles(); + foreach (FileInfo file in files) + { + string temppath = Path.Combine(targetPath, file.Name); + file.CopyTo(temppath, false); + } + + // Recursively copy subdirectories by calling itself on each subdirectory until there are no more to copy + foreach (DirectoryInfo subdir in dirs) + { + string temppath = Path.Combine(targetPath, subdir.Name); + Copy(subdir.FullName, temppath); + } + + } + + public static bool VerifyBothFolderFilesEqual(this string fromPath, string toPath) + { + var fromDir = new DirectoryInfo(fromPath); + var toDir = new DirectoryInfo(toPath); + + if (fromDir.GetFiles().Length != toDir.GetFiles().Length) + return false; + + if (Directory.GetDirectories(fromPath).Length != Directory.GetDirectories(toPath).Length) + return false; + + return true; + } + + public static void RemoveFolder(this string path) + { + try + { + if (Directory.Exists(path)) + Directory.Delete(path, true); + } + catch(PathTooLongException e) + { + //log and update error message to output +#if DEBUG + throw; +#else + throw;// PRODUCTION LOGGING AND CONTINUE + +#endif + } + } + } +} diff --git a/Wox.Plugin/Wox.Plugin.csproj b/Wox.Plugin/Wox.Plugin.csproj index ceba2bbf0..b0b457e98 100644 --- a/Wox.Plugin/Wox.Plugin.csproj +++ b/Wox.Plugin/Wox.Plugin.csproj @@ -65,6 +65,7 @@ + From 69f66b7a4b6c4beb39a026df409145941cf31738 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Sun, 23 Feb 2020 19:41:24 +1100 Subject: [PATCH 03/33] update --- Wox.Plugin/SharedCommands/FilesFolders.cs | 28 +++++++++++++++++------ 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/Wox.Plugin/SharedCommands/FilesFolders.cs b/Wox.Plugin/SharedCommands/FilesFolders.cs index b3f0a3c8b..9576ca7aa 100644 --- a/Wox.Plugin/SharedCommands/FilesFolders.cs +++ b/Wox.Plugin/SharedCommands/FilesFolders.cs @@ -42,16 +42,30 @@ public static void Copy(this string sourcePath, string targetPath) public static bool VerifyBothFolderFilesEqual(this string fromPath, string toPath) { - var fromDir = new DirectoryInfo(fromPath); - var toDir = new DirectoryInfo(toPath); + try + { + var fromDir = new DirectoryInfo(fromPath); + var toDir = new DirectoryInfo(toPath); + + if (fromDir.GetFiles("*", SearchOption.AllDirectories).Length != toDir.GetFiles("*", SearchOption.AllDirectories).Length) + return false; + + if (fromDir.GetDirectories("*", SearchOption.AllDirectories).Length != toDir.GetDirectories("*", SearchOption.AllDirectories).Length) + return false; - if (fromDir.GetFiles().Length != toDir.GetFiles().Length) - return false; + return true; + } + catch(PathTooLongException e) + { + //log and update error message to output +#if DEBUG + throw; +#else + throw;// PRODUCTION LOGGING AND CONTINUE - if (Directory.GetDirectories(fromPath).Length != Directory.GetDirectories(toPath).Length) - return false; +#endif + } - return true; } public static void RemoveFolder(this string path) From ad4c9fb4ef597684699cc90ba48ff6007908f61f Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Sun, 23 Feb 2020 20:21:47 +1100 Subject: [PATCH 04/33] Add possible exception path too long handling --- Wox.Plugin/SharedCommands/FilesFolders.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Wox.Plugin/SharedCommands/FilesFolders.cs b/Wox.Plugin/SharedCommands/FilesFolders.cs index 9576ca7aa..28d0176d5 100644 --- a/Wox.Plugin/SharedCommands/FilesFolders.cs +++ b/Wox.Plugin/SharedCommands/FilesFolders.cs @@ -57,12 +57,10 @@ public static bool VerifyBothFolderFilesEqual(this string fromPath, string toPat } catch(PathTooLongException e) { - //log and update error message to output #if DEBUG throw; #else - throw;// PRODUCTION LOGGING AND CONTINUE - + return false; #endif } From ba46ce09dc7e94f320265d42bdcaff2b4f747ff0 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Mon, 24 Feb 2020 07:20:34 +1100 Subject: [PATCH 05/33] Update with exception handling and continue --- Wox.Plugin/SharedCommands/FilesFolders.cs | 61 ++++++++++++++--------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/Wox.Plugin/SharedCommands/FilesFolders.cs b/Wox.Plugin/SharedCommands/FilesFolders.cs index 28d0176d5..767f00bb8 100644 --- a/Wox.Plugin/SharedCommands/FilesFolders.cs +++ b/Wox.Plugin/SharedCommands/FilesFolders.cs @@ -1,4 +1,6 @@ -using System.IO; +using System; +using System.IO; +using System.Windows; namespace Wox.Plugin.SharedCommands { @@ -16,26 +18,38 @@ public static void Copy(this string sourcePath, string targetPath) + sourcePath); } - DirectoryInfo[] dirs = dir.GetDirectories(); - // If the destination directory doesn't exist, create it. - if (!Directory.Exists(targetPath)) + try { - Directory.CreateDirectory(targetPath); - } + DirectoryInfo[] dirs = dir.GetDirectories(); + // If the destination directory doesn't exist, create it. + if (!Directory.Exists(targetPath)) + { + Directory.CreateDirectory(targetPath); + } - // Get the files in the directory and copy them to the new location. - FileInfo[] files = dir.GetFiles(); - foreach (FileInfo file in files) - { - string temppath = Path.Combine(targetPath, file.Name); - file.CopyTo(temppath, false); - } + // Get the files in the directory and copy them to the new location. + FileInfo[] files = dir.GetFiles(); + foreach (FileInfo file in files) + { + string temppath = Path.Combine(targetPath, file.Name); + file.CopyTo(temppath, false); + } - // Recursively copy subdirectories by calling itself on each subdirectory until there are no more to copy - foreach (DirectoryInfo subdir in dirs) + // Recursively copy subdirectories by calling itself on each subdirectory until there are no more to copy + foreach (DirectoryInfo subdir in dirs) + { + string temppath = Path.Combine(targetPath, subdir.Name); + Copy(subdir.FullName, temppath); + } + } + catch(Exception e) { - string temppath = Path.Combine(targetPath, subdir.Name); - Copy(subdir.FullName, temppath); +#if DEBUG + throw e; +#else + MessageBox.Show(string.Format("Copying path {0} has failed, it will now be deleted for consistency", targetPath)); + RemoveFolder(targetPath); +#endif } } @@ -55,11 +69,12 @@ public static bool VerifyBothFolderFilesEqual(this string fromPath, string toPat return true; } - catch(PathTooLongException e) + catch(Exception e) { #if DEBUG - throw; + throw e; #else + MessageBox.Show(string.Format("Unable to verify folders and files between {0} and {1}", fromPath, toPath)); return false; #endif } @@ -73,14 +88,12 @@ public static void RemoveFolder(this string path) if (Directory.Exists(path)) Directory.Delete(path, true); } - catch(PathTooLongException e) + catch(Exception e) { - //log and update error message to output #if DEBUG - throw; + throw e; #else - throw;// PRODUCTION LOGGING AND CONTINUE - + MessageBox.Show(string.Format("Not able to delete folder {0}, please go to the location and manually delete it", path)); #endif } } From 61845dd0db25b586ef2a6650b3da1652aabb9d81 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Mon, 24 Feb 2020 21:42:41 +1100 Subject: [PATCH 06/33] Add disable portable mode code --- Wox.Core/Configuration/IPortable.cs | 2 ++ Wox.Core/Configuration/Portable.cs | 34 ++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Wox.Core/Configuration/IPortable.cs b/Wox.Core/Configuration/IPortable.cs index 74e32fecd..95c1b345b 100644 --- a/Wox.Core/Configuration/IPortable.cs +++ b/Wox.Core/Configuration/IPortable.cs @@ -7,6 +7,8 @@ public interface IPortable void DisablePortableMode(); void RemoveShortcuts(); void RemoveUninstallerEntry(); + void CreateShortcuts(); + void CreateUninstallerEntry(); bool IsPortableModeEnabled(); void MoveUserDataFolder(string fromLocation, string toLocation); void VerifyUserDataAfterMove(string fromLocation, string toLocation); diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 5f5a60be6..422bbc2f8 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -27,7 +27,25 @@ public Portable() public void DisablePortableMode() { - throw new NotImplementedException(); + try + { + MoveUserDataFolder(portableDataPath, roamingDataPath); + CreateShortcuts(); + CreateUninstallerEntry(); + + // always dispose UpdateManager??????????? + // CHANGE TO PRIVATE/INTERNAL METHODS + } + catch (Exception e) + { + //log and update error message to output above locations where shortcuts may not have been removed +#if DEBUG + throw; +#else + throw;// PRODUCTION LOGGING AND CONTINUE + +#endif + } } public void EnablePortableMode() @@ -37,6 +55,8 @@ public void EnablePortableMode() MoveUserDataFolder(roamingDataPath, portableDataPath); RemoveShortcuts(); RemoveUninstallerEntry(); + + // always dispose UpdateManager??????????? } catch (Exception e) { @@ -78,5 +98,17 @@ public void VerifyUserDataAfterMove(string fromLocation, string toLocation) { FilesFolders.VerifyBothFolderFilesEqual(fromLocation, toLocation); } + + public void CreateShortcuts() + { + portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.StartMenu, false); + portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.Desktop, false); + portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.Startup, false); + } + + public void CreateUninstallerEntry() + { + portabilityUpdater.CreateUninstallerRegistryEntry(); + } } } From 2531f714029c24c4a5d19df7121b29337657aacc Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Tue, 3 Mar 2020 05:58:31 +1100 Subject: [PATCH 07/33] Add disable portable mode programatically --- Wox.Core/Configuration/Portable.cs | 20 ++++++++++++++++---- Wox.Infrastructure/Wox.cs | 2 +- Wox/ViewModel/SettingWindowViewModel.cs | 5 +++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 422bbc2f8..5cb746f54 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -1,3 +1,4 @@ +using Microsoft.Win32; using Squirrel; using System; using Wox.Infrastructure; @@ -16,12 +17,13 @@ public class Portable : IPortable public Portable() { + //NEED TO DYNAMICALLY GET WOX'S LOCATION OTHERWISE SHORTCUTS WONT WORK applicationName = Constant.Wox; exeName = applicationName + ".exe"; rootAppDirectory = Constant.RootDirectory; - portabilityUpdater = new UpdateManager(string.Empty, applicationName, rootAppDirectory); + portabilityUpdater = new UpdateManager(string.Empty, applicationName, rootAppDirectory); - roamingDataPath = Constant.RoamingDataPath; + roamingDataPath = Constant.RoamingDataPath; portableDataPath = Constant.PortableDataPath; } @@ -31,7 +33,7 @@ public void DisablePortableMode() { MoveUserDataFolder(portableDataPath, roamingDataPath); CreateShortcuts(); - CreateUninstallerEntry(); + CreateUninstallerEntry(); //DOES NOT CREATE THE UNINSTALLER ICON!!!!!! // always dispose UpdateManager??????????? // CHANGE TO PRIVATE/INTERNAL METHODS @@ -108,7 +110,17 @@ public void CreateShortcuts() public void CreateUninstallerEntry() { - portabilityUpdater.CreateUninstallerRegistryEntry(); + var uninstallRegSubKey = @"Software\Microsoft\Windows\CurrentVersion\Uninstall"; + // NB: Sometimes the Uninstall key doesn't exist + using (var parentKey = + RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default) + .CreateSubKey("Uninstall", RegistryKeyPermissionCheck.ReadWriteSubTree)) {; } + + var key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default) + .CreateSubKey(uninstallRegSubKey + "\\" + applicationName, RegistryKeyPermissionCheck.ReadWriteSubTree); + key.SetValue("DisplayIcon", Constant.ApplicationDirectory + "\\app.ico", RegistryValueKind.String); + + portabilityUpdater.CreateUninstallerRegistryEntry().Wait(); } } } diff --git a/Wox.Infrastructure/Wox.cs b/Wox.Infrastructure/Wox.cs index 8507ee393..35faa102c 100644 --- a/Wox.Infrastructure/Wox.cs +++ b/Wox.Infrastructure/Wox.cs @@ -13,7 +13,7 @@ public static class Constant private static readonly Assembly Assembly = Assembly.GetExecutingAssembly(); public static readonly string ProgramDirectory = Directory.GetParent(Assembly.Location.NonNull()).ToString(); public static readonly string ExecutablePath = Path.Combine(ProgramDirectory, Wox + ".exe"); - private static readonly string ApplicationDirectory = Directory.GetParent(ProgramDirectory).ToString(); + public static readonly string ApplicationDirectory = Directory.GetParent(ProgramDirectory).ToString(); public static readonly string RootDirectory = Directory.GetParent(ApplicationDirectory).ToString(); public const string PortableFolderName = "UserData"; diff --git a/Wox/ViewModel/SettingWindowViewModel.cs b/Wox/ViewModel/SettingWindowViewModel.cs index b827130b1..d26f86cb9 100644 --- a/Wox/ViewModel/SettingWindowViewModel.cs +++ b/Wox/ViewModel/SettingWindowViewModel.cs @@ -55,6 +55,11 @@ public void EnablePortableMode() _portable.EnablePortableMode(); } + public void DisablePortableMode() + { + _portable.DisablePortableMode(); + } + public void Save() { _storage.Save(); From 39eac9602305e595113ebb83b15c59f65414355b Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 5 Mar 2020 23:10:16 +1100 Subject: [PATCH 08/33] Change method name to make intention clearer. Add additional methods --- Wox.Plugin/SharedCommands/FilesFolders.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Wox.Plugin/SharedCommands/FilesFolders.cs b/Wox.Plugin/SharedCommands/FilesFolders.cs index 584a83bc6..f9eb13961 100644 --- a/Wox.Plugin/SharedCommands/FilesFolders.cs +++ b/Wox.Plugin/SharedCommands/FilesFolders.cs @@ -48,7 +48,7 @@ public static void Copy(this string sourcePath, string targetPath) throw e; #else MessageBox.Show(string.Format("Copying path {0} has failed, it will now be deleted for consistency", targetPath)); - RemoveFolder(targetPath); + RemoveFolderIfExists(targetPath); #endif } @@ -81,7 +81,7 @@ public static bool VerifyBothFolderFilesEqual(this string fromPath, string toPat } - public static void RemoveFolder(this string path) + public static void RemoveFolderIfExists(this string path) { try { @@ -97,5 +97,15 @@ public static void RemoveFolder(this string path) #endif } } + + public static bool LocationExists(this string path) + { + return Directory.Exists(path); + } + + public static bool FileExits(this string filePath) + { + return File.Exists(filePath); + } } } From e6b988d227ddc03556f0571b440fd7e08939fcf8 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 5 Mar 2020 23:13:06 +1100 Subject: [PATCH 09/33] Add code to enable portable mode --- Wox.Core/Configuration/IPortable.cs | 1 + Wox.Core/Configuration/Portable.cs | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Wox.Core/Configuration/IPortable.cs b/Wox.Core/Configuration/IPortable.cs index 95c1b345b..86a355b2b 100644 --- a/Wox.Core/Configuration/IPortable.cs +++ b/Wox.Core/Configuration/IPortable.cs @@ -12,5 +12,6 @@ public interface IPortable bool IsPortableModeEnabled(); void MoveUserDataFolder(string fromLocation, string toLocation); void VerifyUserDataAfterMove(string fromLocation, string toLocation); + void CleanUpFolderAfterPortabilityUpdate(); } } \ No newline at end of file diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 5cb746f54..458c8a96c 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -52,20 +52,30 @@ public void DisablePortableMode() public void EnablePortableMode() { + portabilityUpdater = new UpdateManager(string.Empty, Constant.Wox, Constant.RootDirectory); + try { - MoveUserDataFolder(roamingDataPath, portableDataPath); + MoveUserDataFolder(DataLocation.RoamingDataPath, DataLocation.PortableDataPath); RemoveShortcuts(); RemoveUninstallerEntry(); + IndicateDeletion(DataLocation.RoamingDataPath); - // always dispose UpdateManager??????????? + MessageBox.Show("Wox needs to restart to finish enabling portable mode, " + + "after the restart your roaming data profile will be deleted and portable data profile kept"); + + portabilityUpdater.Dispose(); + + UpdateManager.RestartApp(); } catch (Exception e) { //log and update error message to output above locations where shortcuts may not have been removed #if DEBUG + portabilityUpdater.Dispose(); throw; #else + portabilityUpdater.Dispose(); throw;// PRODUCTION LOGGING AND CONTINUE #endif @@ -79,6 +89,7 @@ public bool IsPortableModeEnabled() public void RemoveShortcuts() { + var exeName = Constant.Wox + ".exe"; portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.StartMenu); portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.Desktop); portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.Startup); @@ -93,7 +104,6 @@ public void MoveUserDataFolder(string fromLocation, string toLocation) { FilesFolders.Copy(fromLocation, toLocation); VerifyUserDataAfterMove(fromLocation, toLocation); - FilesFolders.RemoveFolder(fromLocation); } public void VerifyUserDataAfterMove(string fromLocation, string toLocation) From 7ac69f9bcd1936795c6610e5a1b7abc14848230e Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 5 Mar 2020 23:14:00 +1100 Subject: [PATCH 10/33] Update disable portable mode code - add message to inform user - create uninstaller entry - set delete indication --- Wox.Core/Configuration/Portable.cs | 38 ++++++++++++++---------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 458c8a96c..d0d0d7ace 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -1,49 +1,46 @@ using Microsoft.Win32; using Squirrel; using System; +using System.IO; +using System.Reflection; +using System.Windows; using Wox.Infrastructure; +using Wox.Infrastructure.UserSettings; using Wox.Plugin.SharedCommands; namespace Wox.Core.Configuration { public class Portable : IPortable { - private string applicationName; - private string exeName; - private string rootAppDirectory; private UpdateManager portabilityUpdater; - private string roamingDataPath; - private string portableDataPath; - - public Portable() - { - //NEED TO DYNAMICALLY GET WOX'S LOCATION OTHERWISE SHORTCUTS WONT WORK - applicationName = Constant.Wox; - exeName = applicationName + ".exe"; - rootAppDirectory = Constant.RootDirectory; - portabilityUpdater = new UpdateManager(string.Empty, applicationName, rootAppDirectory); - - roamingDataPath = Constant.RoamingDataPath; - portableDataPath = Constant.PortableDataPath; - } public void DisablePortableMode() { + portabilityUpdater = new UpdateManager(string.Empty, Constant.Wox, Constant.RootDirectory); + try { - MoveUserDataFolder(portableDataPath, roamingDataPath); + MoveUserDataFolder(DataLocation.PortableDataPath, DataLocation.RoamingDataPath); CreateShortcuts(); - CreateUninstallerEntry(); //DOES NOT CREATE THE UNINSTALLER ICON!!!!!! + CreateUninstallerEntry(); + IndicateDeletion(DataLocation.PortableDataPath); + + MessageBox.Show("Wox needs to restart to finish disabling portable mode, " + + "after the restart your portable data profile will be deleted and roaming data profile kept"); - // always dispose UpdateManager??????????? + portabilityUpdater.Dispose(); // CHANGE TO PRIVATE/INTERNAL METHODS + + UpdateManager.RestartApp(); } catch (Exception e) { //log and update error message to output above locations where shortcuts may not have been removed #if DEBUG + portabilityUpdater.Dispose(); throw; #else + portabilityUpdater.Dispose(); throw;// PRODUCTION LOGGING AND CONTINUE #endif @@ -113,6 +110,7 @@ public void VerifyUserDataAfterMove(string fromLocation, string toLocation) public void CreateShortcuts() { + var exeName = Constant.Wox + ".exe"; portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.StartMenu, false); portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.Desktop, false); portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.Startup, false); From 56b8b5f3d6fa0e029afa2268becb75ad44f96494 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 5 Mar 2020 23:20:55 +1100 Subject: [PATCH 11/33] After restart, clean up data folder locations so only one is used --- Wox.Core/Configuration/Portable.cs | 34 ++++++++++++++++++++++++++++++ Wox/App.xaml.cs | 2 ++ 2 files changed, 36 insertions(+) diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index d0d0d7ace..7a4f685ad 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -129,6 +129,40 @@ public void CreateUninstallerEntry() key.SetValue("DisplayIcon", Constant.ApplicationDirectory + "\\app.ico", RegistryValueKind.String); portabilityUpdater.CreateUninstallerRegistryEntry().Wait(); + public void CleanUpFolderAfterPortabilityUpdate() + { + var portableDataPath = Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location.NonNull()).ToString(), "UserData"); + var roamingDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Wox"); + + bool DataLocationPortableDeleteRequired = false; + bool DataLocationRoamingDeleteRequired = false; + + if ((roamingDataPath + "\\.dead").FileExits()) + DataLocationRoamingDeleteRequired = true; + + if ((portableDataPath + "\\.dead").FileExits()) + DataLocationPortableDeleteRequired = true; + + if (DataLocationRoamingDeleteRequired) + { + if(roamingDataPath.LocationExists()) + MessageBox.Show("Wox detected you restarted after enabling portable mode, " + + "your roaming data profile will now be deleted"); + + FilesFolders.RemoveFolderIfExists(roamingDataPath); + + return; + } + + if(DataLocationPortableDeleteRequired) + { + MessageBox.Show("Wox detected you restarted after disabling portable mode, " + + "your portable data profile will now be deleted"); + + FilesFolders.RemoveFolderIfExists(portableDataPath); + + return; + } } } } diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index 96c2858af..72834b7a5 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -48,6 +48,8 @@ private void OnStartup(object sender, StartupEventArgs e) { Stopwatch.Normal("|App.OnStartup|Startup cost", () => { + _portable.CleanUpFolderAfterPortabilityUpdate(); + Log.Info("|App.OnStartup|Begin Wox startup ----------------------------------------------------"); Log.Info($"|App.OnStartup|Runtime info:{ErrorReporting.RuntimeInfo()}"); RegisterAppDomainExceptions(); From f6ae266a88d5af3031715462665a5e74e384f88e Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 5 Mar 2020 23:23:44 +1100 Subject: [PATCH 12/33] Add settings check box ui and viewmodel --- Wox/SettingWindow.xaml | 3 +++ Wox/ViewModel/SettingWindowViewModel.cs | 30 ++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Wox/SettingWindow.xaml b/Wox/SettingWindow.xaml index 1eddd8ab6..b081cfbf6 100644 --- a/Wox/SettingWindow.xaml +++ b/Wox/SettingWindow.xaml @@ -33,6 +33,9 @@ + + + diff --git a/Wox/ViewModel/SettingWindowViewModel.cs b/Wox/ViewModel/SettingWindowViewModel.cs index 0a240aec7..197797534 100644 --- a/Wox/ViewModel/SettingWindowViewModel.cs +++ b/Wox/ViewModel/SettingWindowViewModel.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -13,7 +13,6 @@ using Wox.Core.Resource; using Wox.Helper; using Wox.Infrastructure; -using Wox.Infrastructure.Http; using Wox.Infrastructure.Storage; using Wox.Infrastructure.UserSettings; using Wox.Plugin; @@ -39,8 +38,7 @@ public SettingWindowViewModel(Updater updater, IPortable portable) OnPropertyChanged(nameof(ActivatedTimes)); } }; - - + _portableMode = Settings.PortableMode; } public Settings Settings { get; set; } @@ -50,14 +48,30 @@ public async void UpdateApp() await _updater.UpdateApp(false); } - public void EnablePortableMode() + private bool _portableMode; + public bool PortableMode { - _portable.EnablePortableMode(); + get { return _portableMode; } + set + { + _portableMode = value; + + Settings.PortableMode = value; + Save(); + PortabilityUpdate(value); + } } - public void DisablePortableMode() + private void PortabilityUpdate(bool enabled) { - _portable.DisablePortableMode(); + if (enabled) + { + _portable.EnablePortableMode(); + } + else + { + _portable.DisablePortableMode(); + } } public void Save() From c2d7e658b0b9b58a9c92b8952dbf0c85d28871d6 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 5 Mar 2020 23:24:14 +1100 Subject: [PATCH 13/33] Add settings saving to file --- Wox.Infrastructure/UserSettings/Settings.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Wox.Infrastructure/UserSettings/Settings.cs b/Wox.Infrastructure/UserSettings/Settings.cs index 7714768f0..487bf7d12 100644 --- a/Wox.Infrastructure/UserSettings/Settings.cs +++ b/Wox.Infrastructure/UserSettings/Settings.cs @@ -77,6 +77,8 @@ public string QuerySearchPrecisionString public bool DontPromptUpdateMsg { get; set; } public bool EnableUpdateLog { get; set; } + public bool PortableMode { get; set; } = false; + public bool StartWoxOnSystemStartup { get; set; } = true; public bool HideOnStartup { get; set; } bool _hideNotifyIcon { get; set; } From fd4f0f631bcb0ee8df47bccdbf1e375d7d181380 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 5 Mar 2020 23:25:15 +1100 Subject: [PATCH 14/33] Add method used to indicate deletion of data location .dead file add --- Wox.Core/Configuration/Portable.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 7a4f685ad..5f8634310 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -129,6 +129,12 @@ public void CreateUninstallerEntry() key.SetValue("DisplayIcon", Constant.ApplicationDirectory + "\\app.ico", RegistryValueKind.String); portabilityUpdater.CreateUninstallerRegistryEntry().Wait(); + + public void IndicateDeletion(string filePathTodelete) + { + using (StreamWriter sw = File.CreateText(filePathTodelete + "\\.dead")){} + } + public void CleanUpFolderAfterPortabilityUpdate() { var portableDataPath = Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location.NonNull()).ToString(), "UserData"); From be8da8035efea80237320839106827078bc425dd Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 5 Mar 2020 23:27:04 +1100 Subject: [PATCH 15/33] Remove Wait on create uninstaller reg entry call --- Wox.Core/Configuration/Portable.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 5f8634310..3a1cdce6a 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -128,7 +128,8 @@ public void CreateUninstallerEntry() .CreateSubKey(uninstallRegSubKey + "\\" + applicationName, RegistryKeyPermissionCheck.ReadWriteSubTree); key.SetValue("DisplayIcon", Constant.ApplicationDirectory + "\\app.ico", RegistryValueKind.String); - portabilityUpdater.CreateUninstallerRegistryEntry().Wait(); + portabilityUpdater.CreateUninstallerRegistryEntry(); + } public void IndicateDeletion(string filePathTodelete) { From 4fc30166bf1d326021e8bc46f48010a15338725a Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 5 Mar 2020 23:29:07 +1100 Subject: [PATCH 16/33] separate get data location code from constant file --- .../UserSettings/DataLocation.cs | 31 +++++++++++++++++++ Wox.Infrastructure/Wox.cs | 22 +------------ 2 files changed, 32 insertions(+), 21 deletions(-) create mode 100644 Wox.Infrastructure/UserSettings/DataLocation.cs diff --git a/Wox.Infrastructure/UserSettings/DataLocation.cs b/Wox.Infrastructure/UserSettings/DataLocation.cs new file mode 100644 index 000000000..f100cdfeb --- /dev/null +++ b/Wox.Infrastructure/UserSettings/DataLocation.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Wox.Infrastructure.UserSettings +{ + public static class DataLocation + { + public static bool PortableDataLocationInUse; + public const string PortableFolderName = "UserData"; + public static string PortableDataPath = Path.Combine(Constant.ProgramDirectory, PortableFolderName); + public static string RoamingDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Constant.Wox); + public static string DataDirectory() + { + if (Directory.Exists(PortableDataPath)) + { + PortableDataLocationInUse = true; + return PortableDataPath; + } + else + { + return RoamingDataPath; + } + } + + public static readonly string PluginsDirectory = Path.Combine(DataDirectory(), Constant.Plugins); + } +} diff --git a/Wox.Infrastructure/Wox.cs b/Wox.Infrastructure/Wox.cs index d9599e07a..8c57bf20d 100644 --- a/Wox.Infrastructure/Wox.cs +++ b/Wox.Infrastructure/Wox.cs @@ -1,4 +1,3 @@ -using System; using System.Diagnostics; using System.IO; using System.Reflection; @@ -15,26 +14,7 @@ public static class Constant public static readonly string ExecutablePath = Path.Combine(ProgramDirectory, Wox + ".exe"); public static readonly string ApplicationDirectory = Directory.GetParent(ProgramDirectory).ToString(); public static readonly string RootDirectory = Directory.GetParent(ApplicationDirectory).ToString(); - - public static bool IsPortableMode; - public const string PortableFolderName = "UserData"; - public static string PortableDataPath = Path.Combine(ProgramDirectory, PortableFolderName); - public static string RoamingDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Wox); - public static string DetermineDataDirectory() - { - if (Directory.Exists(PortableDataPath)) - { - IsPortableMode = true; - return PortableDataPath; - } - else - { - return RoamingDataPath; - } - } - - public static readonly string DataDirectory = DetermineDataDirectory(); - public static readonly string PluginsDirectory = Path.Combine(DataDirectory, Plugins); + public static readonly string PreinstalledDirectory = Path.Combine(ProgramDirectory, Plugins); public const string Issue = "https://github.com/Wox-launcher/Wox/issues/new"; public static readonly string Version = FileVersionInfo.GetVersionInfo(Assembly.Location.NonNull()).ProductVersion; From 6134550827d080a1bbc9b0952fe21d4f3045d074 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 5 Mar 2020 23:29:38 +1100 Subject: [PATCH 17/33] Include DataLocation.cs in project file --- Wox.Infrastructure/Wox.Infrastructure.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/Wox.Infrastructure/Wox.Infrastructure.csproj b/Wox.Infrastructure/Wox.Infrastructure.csproj index 445454093..414e06610 100644 --- a/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -77,6 +77,7 @@ + From d0976a99c96504aa7243a9f8de72ac98baaf2eec Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 5 Mar 2020 23:30:46 +1100 Subject: [PATCH 18/33] Update all applicable files to use the new data location class --- Plugins/Wox.Plugin.Program/Logger/ProgramLogger.cs | 5 +++-- Wox.Core/Configuration/Portable.cs | 2 +- Wox.Core/Plugin/PluginInstaller.cs | 2 +- Wox.Core/Plugin/PluginManager.cs | 8 ++++---- Wox.Core/Resource/Theme.cs | 2 +- Wox.Core/Updater.cs | 11 ++++++----- Wox.Infrastructure/Logger/Log.cs | 3 ++- Wox.Infrastructure/Storage/BinaryStorage.cs | 3 ++- Wox.Infrastructure/Storage/PluginJsonStorage.cs | 3 ++- Wox.Infrastructure/Storage/WoxJsonStorage.cs | 3 ++- 10 files changed, 24 insertions(+), 18 deletions(-) diff --git a/Plugins/Wox.Plugin.Program/Logger/ProgramLogger.cs b/Plugins/Wox.Plugin.Program/Logger/ProgramLogger.cs index 587aba685..d9f92bca2 100644 --- a/Plugins/Wox.Plugin.Program/Logger/ProgramLogger.cs +++ b/Plugins/Wox.Plugin.Program/Logger/ProgramLogger.cs @@ -1,4 +1,4 @@ -using NLog; +using NLog; using NLog.Config; using NLog.Targets; using System; @@ -7,6 +7,7 @@ using System.Runtime.CompilerServices; using System.Security; using Wox.Infrastructure; +using Wox.Infrastructure.UserSettings; namespace Wox.Plugin.Program.Logger { @@ -21,7 +22,7 @@ internal static class ProgramLogger static ProgramLogger() { - var path = Path.Combine(Constant.DataDirectory, DirectoryName, Constant.Version); + var path = Path.Combine(DataLocation.DataDirectory(), DirectoryName, Constant.Version); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 3a1cdce6a..d2ce2f1a2 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -125,7 +125,7 @@ public void CreateUninstallerEntry() .CreateSubKey("Uninstall", RegistryKeyPermissionCheck.ReadWriteSubTree)) {; } var key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default) - .CreateSubKey(uninstallRegSubKey + "\\" + applicationName, RegistryKeyPermissionCheck.ReadWriteSubTree); + .CreateSubKey(uninstallRegSubKey + "\\" + Constant.Wox, RegistryKeyPermissionCheck.ReadWriteSubTree); key.SetValue("DisplayIcon", Constant.ApplicationDirectory + "\\app.ico", RegistryValueKind.String); portabilityUpdater.CreateUninstallerRegistryEntry(); diff --git a/Wox.Core/Plugin/PluginInstaller.cs b/Wox.Core/Plugin/PluginInstaller.cs index ffdb0aa75..a867d2e10 100644 --- a/Wox.Core/Plugin/PluginInstaller.cs +++ b/Wox.Core/Plugin/PluginInstaller.cs @@ -34,7 +34,7 @@ internal static void Install(string path) return; } - string pluginFolerPath = Infrastructure.Constant.PluginsDirectory; + string pluginFolerPath = Infrastructure.UserSettings.DataLocation.PluginsDirectory; string newPluginName = plugin.Name .Replace("/", "_") diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index 0166690b7..15da91c98 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -32,20 +32,20 @@ public static class PluginManager // todo happlebao, this should not be public, the indicator function should be embeded public static PluginsSettings Settings; private static List _metadatas; - private static readonly string[] Directories = { Constant.PreinstalledDirectory, Constant.PluginsDirectory }; + private static readonly string[] Directories = { Constant.PreinstalledDirectory, DataLocation.PluginsDirectory }; private static void ValidateUserDirectory() { - if (!Directory.Exists(Constant.PluginsDirectory)) + if (!Directory.Exists(DataLocation.PluginsDirectory)) { - Directory.CreateDirectory(Constant.PluginsDirectory); + Directory.CreateDirectory(DataLocation.PluginsDirectory); } } private static void DeletePythonBinding() { const string binding = "wox.py"; - var directory = Constant.PluginsDirectory; + var directory = DataLocation.PluginsDirectory; foreach (var subDirectory in Directory.GetDirectories(directory)) { var path = Path.Combine(subDirectory, binding); diff --git a/Wox.Core/Resource/Theme.cs b/Wox.Core/Resource/Theme.cs index 4b8c37ccf..2d045ab19 100644 --- a/Wox.Core/Resource/Theme.cs +++ b/Wox.Core/Resource/Theme.cs @@ -23,7 +23,7 @@ public class Theme private const string Folder = "Themes"; private const string Extension = ".xaml"; private string DirectoryPath => Path.Combine(Constant.ProgramDirectory, Folder); - private string UserDirectoryPath => Path.Combine(Constant.DataDirectory, Folder); + private string UserDirectoryPath => Path.Combine(DataLocation.DataDirectory(), Folder); public Theme() { diff --git a/Wox.Core/Updater.cs b/Wox.Core/Updater.cs index 9262d78f4..b05984494 100644 --- a/Wox.Core/Updater.cs +++ b/Wox.Core/Updater.cs @@ -15,6 +15,7 @@ using Wox.Infrastructure.Http; using Wox.Infrastructure.Logger; using System.IO; +using Wox.Infrastructure.UserSettings; namespace Wox.Core { @@ -80,13 +81,13 @@ public async Task UpdateApp(bool silentIfLatestVersion = true) await updateManager.ApplyReleases(newUpdateInfo); - if (Constant.IsPortableMode) + if (DataLocation.PortableDataLocationInUse) { - var targetDestination = updateManager.RootAppDirectory + $"\\app-{newReleaseVersion.ToString()}\\{Constant.PortableFolderName}"; - FilesFolders.Copy(Constant.PortableDataPath, targetDestination); - if (!FilesFolders.VerifyBothFolderFilesEqual(Constant.PortableDataPath, targetDestination)) + var targetDestination = updateManager.RootAppDirectory + $"\\app-{newReleaseVersion.ToString()}\\{DataLocation.PortableFolderName}"; + FilesFolders.Copy(DataLocation.PortableDataPath, targetDestination); + if (!FilesFolders.VerifyBothFolderFilesEqual(DataLocation.PortableDataPath, targetDestination)) MessageBox.Show(string.Format("Wox was not able to move your user profile data to the new update version. Please manually" + - "move your profile data folder from {0} to {1}", Constant.PortableDataPath, targetDestination)); + "move your profile data folder from {0} to {1}", DataLocation.PortableDataPath, targetDestination)); } else { diff --git a/Wox.Infrastructure/Logger/Log.cs b/Wox.Infrastructure/Logger/Log.cs index 349920076..4b7a97241 100644 --- a/Wox.Infrastructure/Logger/Log.cs +++ b/Wox.Infrastructure/Logger/Log.cs @@ -4,6 +4,7 @@ using NLog; using NLog.Config; using NLog.Targets; +using Wox.Infrastructure.UserSettings; namespace Wox.Infrastructure.Logger { @@ -15,7 +16,7 @@ public static class Log static Log() { - CurrentLogDirectory = Path.Combine(Constant.DataDirectory, DirectoryName, Constant.Version); + CurrentLogDirectory = Path.Combine(DataLocation.DataDirectory(), DirectoryName, Constant.Version); if (!Directory.Exists(CurrentLogDirectory)) { Directory.CreateDirectory(CurrentLogDirectory); diff --git a/Wox.Infrastructure/Storage/BinaryStorage.cs b/Wox.Infrastructure/Storage/BinaryStorage.cs index d2c5a2da8..6f9031dde 100644 --- a/Wox.Infrastructure/Storage/BinaryStorage.cs +++ b/Wox.Infrastructure/Storage/BinaryStorage.cs @@ -5,6 +5,7 @@ using System.Runtime.Serialization.Formatters; using System.Runtime.Serialization.Formatters.Binary; using Wox.Infrastructure.Logger; +using Wox.Infrastructure.UserSettings; namespace Wox.Infrastructure.Storage { @@ -17,7 +18,7 @@ public class BinaryStorage public BinaryStorage(string filename) { const string directoryName = "Cache"; - var directoryPath = Path.Combine(Constant.DataDirectory, directoryName); + var directoryPath = Path.Combine(DataLocation.DataDirectory(), directoryName); Helper.ValidateDirectory(directoryPath); const string fileSuffix = ".cache"; diff --git a/Wox.Infrastructure/Storage/PluginJsonStorage.cs b/Wox.Infrastructure/Storage/PluginJsonStorage.cs index 27de500c7..77dd541c3 100644 --- a/Wox.Infrastructure/Storage/PluginJsonStorage.cs +++ b/Wox.Infrastructure/Storage/PluginJsonStorage.cs @@ -1,4 +1,5 @@ using System.IO; +using Wox.Infrastructure.UserSettings; namespace Wox.Infrastructure.Storage { @@ -9,7 +10,7 @@ public PluginJsonStorage() // C# releated, add python releated below var dataType = typeof(T); var assemblyName = typeof(T).Assembly.GetName().Name; - DirectoryPath = Path.Combine(Constant.DataDirectory, DirectoryName, Constant.Plugins, assemblyName); + DirectoryPath = Path.Combine(DataLocation.DataDirectory(), DirectoryName, Constant.Plugins, assemblyName); Helper.ValidateDirectory(DirectoryPath); FilePath = Path.Combine(DirectoryPath, $"{dataType.Name}{FileSuffix}"); diff --git a/Wox.Infrastructure/Storage/WoxJsonStorage.cs b/Wox.Infrastructure/Storage/WoxJsonStorage.cs index da0dbd073..1a6b22c4b 100644 --- a/Wox.Infrastructure/Storage/WoxJsonStorage.cs +++ b/Wox.Infrastructure/Storage/WoxJsonStorage.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Wox.Infrastructure.UserSettings; namespace Wox.Infrastructure.Storage { @@ -11,7 +12,7 @@ namespace Wox.Infrastructure.Storage { public WoxJsonStorage() { - var directoryPath = Path.Combine(Constant.DataDirectory, DirectoryName); + var directoryPath = Path.Combine(DataLocation.DataDirectory(), DirectoryName); Helper.ValidateDirectory(directoryPath); var filename = typeof(T).Name; From ef1b52122e58e1a06154f40ff641e3938281c177 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Fri, 6 Mar 2020 21:28:01 +1100 Subject: [PATCH 19/33] Change PortableDataLocationInUse property to method Added const for deletion indicator --- Wox.Core/Configuration/Portable.cs | 6 +++--- Wox.Core/Updater.cs | 2 +- .../UserSettings/DataLocation.cs | 21 +++++++++++-------- Wox.Infrastructure/UserSettings/Settings.cs | 2 +- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index d2ce2f1a2..6eb7e768d 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -133,7 +133,7 @@ public void CreateUninstallerEntry() public void IndicateDeletion(string filePathTodelete) { - using (StreamWriter sw = File.CreateText(filePathTodelete + "\\.dead")){} + using (StreamWriter sw = File.CreateText(filePathTodelete + "\\" + DataLocation.DeletionIndicatorFile)){} } public void CleanUpFolderAfterPortabilityUpdate() @@ -144,10 +144,10 @@ public void CleanUpFolderAfterPortabilityUpdate() bool DataLocationPortableDeleteRequired = false; bool DataLocationRoamingDeleteRequired = false; - if ((roamingDataPath + "\\.dead").FileExits()) + if ((roamingDataPath + "\\" + DataLocation.DeletionIndicatorFile).FileExits()) DataLocationRoamingDeleteRequired = true; - if ((portableDataPath + "\\.dead").FileExits()) + if ((portableDataPath + "\\" + DataLocation.DeletionIndicatorFile).FileExits()) DataLocationPortableDeleteRequired = true; if (DataLocationRoamingDeleteRequired) diff --git a/Wox.Core/Updater.cs b/Wox.Core/Updater.cs index b05984494..6a04def9f 100644 --- a/Wox.Core/Updater.cs +++ b/Wox.Core/Updater.cs @@ -81,7 +81,7 @@ public async Task UpdateApp(bool silentIfLatestVersion = true) await updateManager.ApplyReleases(newUpdateInfo); - if (DataLocation.PortableDataLocationInUse) + if (DataLocation.PortableDataLocationInUse()) { var targetDestination = updateManager.RootAppDirectory + $"\\app-{newReleaseVersion.ToString()}\\{DataLocation.PortableFolderName}"; FilesFolders.Copy(DataLocation.PortableDataPath, targetDestination); diff --git a/Wox.Infrastructure/UserSettings/DataLocation.cs b/Wox.Infrastructure/UserSettings/DataLocation.cs index f100cdfeb..ef85341c5 100644 --- a/Wox.Infrastructure/UserSettings/DataLocation.cs +++ b/Wox.Infrastructure/UserSettings/DataLocation.cs @@ -9,21 +9,24 @@ namespace Wox.Infrastructure.UserSettings { public static class DataLocation { - public static bool PortableDataLocationInUse; public const string PortableFolderName = "UserData"; + public const string DeletionIndicatorFile = ".dead"; public static string PortableDataPath = Path.Combine(Constant.ProgramDirectory, PortableFolderName); public static string RoamingDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Constant.Wox); public static string DataDirectory() { - if (Directory.Exists(PortableDataPath)) - { - PortableDataLocationInUse = true; + if (PortableDataLocationInUse()) return PortableDataPath; - } - else - { - return RoamingDataPath; - } + + return RoamingDataPath; + } + + public static bool PortableDataLocationInUse() + { + if (Directory.Exists(PortableDataPath) && !File.Exists(DeletionIndicatorFile)) + return true; + + return false; } public static readonly string PluginsDirectory = Path.Combine(DataDirectory(), Constant.Plugins); diff --git a/Wox.Infrastructure/UserSettings/Settings.cs b/Wox.Infrastructure/UserSettings/Settings.cs index 487bf7d12..9d8e46ae6 100644 --- a/Wox.Infrastructure/UserSettings/Settings.cs +++ b/Wox.Infrastructure/UserSettings/Settings.cs @@ -77,7 +77,7 @@ public string QuerySearchPrecisionString public bool DontPromptUpdateMsg { get; set; } public bool EnableUpdateLog { get; set; } - public bool PortableMode { get; set; } = false; + public bool PortableMode { get; set; } = DataLocation.PortableDataLocationInUse(); public bool StartWoxOnSystemStartup { get; set; } = true; public bool HideOnStartup { get; set; } From 930ceef55c3b8169846187835e73bc566896bd3f Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Fri, 6 Mar 2020 23:44:36 +1100 Subject: [PATCH 20/33] Change method name for cleaning up after portability update Make intentions clearer --- Wox.Core/Configuration/Portable.cs | 2 +- Wox/App.xaml.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 6eb7e768d..2d2a4492d 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -136,7 +136,7 @@ public void IndicateDeletion(string filePathTodelete) using (StreamWriter sw = File.CreateText(filePathTodelete + "\\" + DataLocation.DeletionIndicatorFile)){} } - public void CleanUpFolderAfterPortabilityUpdate() + public void PreStartCleanUpAfterPortabilityUpdate() { var portableDataPath = Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location.NonNull()).ToString(), "UserData"); var roamingDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Wox"); diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index 72834b7a5..83a0d2255 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -48,7 +48,7 @@ private void OnStartup(object sender, StartupEventArgs e) { Stopwatch.Normal("|App.OnStartup|Startup cost", () => { - _portable.CleanUpFolderAfterPortabilityUpdate(); + _portable.PreStartCleanUpAfterPortabilityUpdate(); Log.Info("|App.OnStartup|Begin Wox startup ----------------------------------------------------"); Log.Info($"|App.OnStartup|Runtime info:{ErrorReporting.RuntimeInfo()}"); From 82a24c995248f6734a839f65da9081ec70ab0523 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Fri, 6 Mar 2020 23:45:52 +1100 Subject: [PATCH 21/33] Add validation prior to update portability --- Wox.Core/Configuration/IPortable.cs | 4 ++-- Wox.Core/Configuration/Portable.cs | 17 +++++++++++++++++ Wox/ViewModel/SettingWindowViewModel.cs | 3 +++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Wox.Core/Configuration/IPortable.cs b/Wox.Core/Configuration/IPortable.cs index 86a355b2b..069b6fb5b 100644 --- a/Wox.Core/Configuration/IPortable.cs +++ b/Wox.Core/Configuration/IPortable.cs @@ -1,4 +1,4 @@ - + namespace Wox.Core.Configuration { public interface IPortable @@ -12,6 +12,6 @@ public interface IPortable bool IsPortableModeEnabled(); void MoveUserDataFolder(string fromLocation, string toLocation); void VerifyUserDataAfterMove(string fromLocation, string toLocation); - void CleanUpFolderAfterPortabilityUpdate(); + bool CanUpdatePortability(); } } \ No newline at end of file diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 2d2a4492d..888941824 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -171,5 +171,22 @@ public void PreStartCleanUpAfterPortabilityUpdate() return; } } + + public bool CanUpdatePortability() + { + var roamingLocationExists = DataLocation.RoamingDataPath.LocationExists(); + var portableLocationExists = DataLocation.PortableDataPath.LocationExists(); + + if(roamingLocationExists && portableLocationExists) + { + MessageBox.Show(string.Format("Wox detected your user data exists both in {0} and " + + "{1}. {2}{2}Please delete {1} in order to proceed. No changes have occured.", + DataLocation.PortableDataPath, DataLocation.RoamingDataPath, Environment.NewLine)); + + return false; + } + + return true; + } } } diff --git a/Wox/ViewModel/SettingWindowViewModel.cs b/Wox/ViewModel/SettingWindowViewModel.cs index 197797534..1e22635e5 100644 --- a/Wox/ViewModel/SettingWindowViewModel.cs +++ b/Wox/ViewModel/SettingWindowViewModel.cs @@ -54,6 +54,9 @@ public bool PortableMode get { return _portableMode; } set { + if (!_portable.CanUpdatePortability()) + return; + _portableMode = value; Settings.PortableMode = value; From 83df0b3b58f2a8b281add139c4b82ed2ffcf3aea Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Fri, 6 Mar 2020 23:46:30 +1100 Subject: [PATCH 22/33] Remove unnecessary method implementation Should be handled somewhere else --- Wox.Core/Configuration/IPortable.cs | 3 +-- Wox.Core/Configuration/Portable.cs | 5 ----- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/Wox.Core/Configuration/IPortable.cs b/Wox.Core/Configuration/IPortable.cs index 069b6fb5b..b0af43672 100644 --- a/Wox.Core/Configuration/IPortable.cs +++ b/Wox.Core/Configuration/IPortable.cs @@ -1,4 +1,4 @@ - + namespace Wox.Core.Configuration { public interface IPortable @@ -9,7 +9,6 @@ public interface IPortable void RemoveUninstallerEntry(); void CreateShortcuts(); void CreateUninstallerEntry(); - bool IsPortableModeEnabled(); void MoveUserDataFolder(string fromLocation, string toLocation); void VerifyUserDataAfterMove(string fromLocation, string toLocation); bool CanUpdatePortability(); diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 888941824..e24977455 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -79,11 +79,6 @@ public void EnablePortableMode() } } - public bool IsPortableModeEnabled() - { - throw new NotImplementedException(); - } - public void RemoveShortcuts() { var exeName = Constant.Wox + ".exe"; From 31dd774d75a521fc5f0ff0ea856d4066120d0b59 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Sat, 7 Mar 2020 22:41:03 +1100 Subject: [PATCH 23/33] Update add comments change to internal --- Wox.Core/Configuration/Portable.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index e24977455..83b8b15a1 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -29,7 +29,6 @@ public void DisablePortableMode() "after the restart your portable data profile will be deleted and roaming data profile kept"); portabilityUpdater.Dispose(); - // CHANGE TO PRIVATE/INTERNAL METHODS UpdateManager.RestartApp(); } @@ -126,13 +125,18 @@ public void CreateUninstallerEntry() portabilityUpdater.CreateUninstallerRegistryEntry(); } - public void IndicateDeletion(string filePathTodelete) + internal void IndicateDeletion(string filePathTodelete) { using (StreamWriter sw = File.CreateText(filePathTodelete + "\\" + DataLocation.DeletionIndicatorFile)){} } + /// + ///This method should be run at first before all methods during start up and should be run before determining which data location + ///will be used for Wox. + /// public void PreStartCleanUpAfterPortabilityUpdate() { + // Specify here so this method does not rely on other environment variables to initialise var portableDataPath = Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location.NonNull()).ToString(), "UserData"); var roamingDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Wox"); From a70615d79f8daea3bdce4ff0e778438aa4814c0a Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Sun, 8 Mar 2020 14:43:08 +1100 Subject: [PATCH 24/33] Add production logging --- Wox.Core/Configuration/Portable.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 83b8b15a1..bd42380c6 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -34,14 +34,13 @@ public void DisablePortableMode() } catch (Exception e) { - //log and update error message to output above locations where shortcuts may not have been removed #if DEBUG portabilityUpdater.Dispose(); throw; #else portabilityUpdater.Dispose(); - throw;// PRODUCTION LOGGING AND CONTINUE - + Log.Exception("Portable", "Error occured while disabling portable mode", e); + throw; #endif } } @@ -66,14 +65,13 @@ public void EnablePortableMode() } catch (Exception e) { - //log and update error message to output above locations where shortcuts may not have been removed #if DEBUG portabilityUpdater.Dispose(); throw; #else portabilityUpdater.Dispose(); - throw;// PRODUCTION LOGGING AND CONTINUE - + Log.Exception("Portable", "Error occured while enabling portable mode", e); + throw; #endif } } From 830378e96e2e8fceea013dc266e96780f132c80d Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Sun, 8 Mar 2020 14:44:06 +1100 Subject: [PATCH 25/33] update --- Wox.Core/Configuration/Portable.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index bd42380c6..388f52042 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -5,6 +5,7 @@ using System.Reflection; using System.Windows; using Wox.Infrastructure; +using Wox.Infrastructure.Logger; using Wox.Infrastructure.UserSettings; using Wox.Plugin.SharedCommands; From f933f9d10c33e5bef63e1989776030dbe58c7d73 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Sun, 8 Mar 2020 14:48:24 +1100 Subject: [PATCH 26/33] cater for debug mode --- Wox.Core/Configuration/Portable.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 388f52042..7789072bd 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -22,8 +22,13 @@ public void DisablePortableMode() try { MoveUserDataFolder(DataLocation.PortableDataPath, DataLocation.RoamingDataPath); +#if DEBUG + // Create shortcuts and uninstaller are not required in debug mode, + // otherwise will repoint the path of the actual installed production version to the debug version +#else CreateShortcuts(); CreateUninstallerEntry(); +#endif IndicateDeletion(DataLocation.PortableDataPath); MessageBox.Show("Wox needs to restart to finish disabling portable mode, " + @@ -53,8 +58,13 @@ public void EnablePortableMode() try { MoveUserDataFolder(DataLocation.RoamingDataPath, DataLocation.PortableDataPath); +#if DEBUG + // Remove shortcuts and uninstaller are not required in debug mode, + // otherwise will delete the actual installed production version +#else RemoveShortcuts(); RemoveUninstallerEntry(); +#endif IndicateDeletion(DataLocation.RoamingDataPath); MessageBox.Show("Wox needs to restart to finish enabling portable mode, " + From f939abb6e7802f7896c6600ce8e7d370089d10f3 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Sun, 8 Mar 2020 14:50:06 +1100 Subject: [PATCH 27/33] Remove need for saving to settings file by getting status dynamically --- Wox.Infrastructure/UserSettings/Settings.cs | 2 -- Wox/ViewModel/SettingWindowViewModel.cs | 24 +++++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Wox.Infrastructure/UserSettings/Settings.cs b/Wox.Infrastructure/UserSettings/Settings.cs index 9d8e46ae6..7714768f0 100644 --- a/Wox.Infrastructure/UserSettings/Settings.cs +++ b/Wox.Infrastructure/UserSettings/Settings.cs @@ -77,8 +77,6 @@ public string QuerySearchPrecisionString public bool DontPromptUpdateMsg { get; set; } public bool EnableUpdateLog { get; set; } - public bool PortableMode { get; set; } = DataLocation.PortableDataLocationInUse(); - public bool StartWoxOnSystemStartup { get; set; } = true; public bool HideOnStartup { get; set; } bool _hideNotifyIcon { get; set; } diff --git a/Wox/ViewModel/SettingWindowViewModel.cs b/Wox/ViewModel/SettingWindowViewModel.cs index 1e22635e5..c16d619cd 100644 --- a/Wox/ViewModel/SettingWindowViewModel.cs +++ b/Wox/ViewModel/SettingWindowViewModel.cs @@ -38,7 +38,6 @@ public SettingWindowViewModel(Updater updater, IPortable portable) OnPropertyChanged(nameof(ActivatedTimes)); } }; - _portableMode = Settings.PortableMode; } public Settings Settings { get; set; } @@ -48,7 +47,8 @@ public async void UpdateApp() await _updater.UpdateApp(false); } - private bool _portableMode; + // This is only required to set at startup. When portable mode enabled/disabled a restart is always required + private bool _portableMode = DataLocation.PortableDataLocationInUse(); public bool PortableMode { get { return _portableMode; } @@ -56,18 +56,24 @@ public bool PortableMode { if (!_portable.CanUpdatePortability()) return; - - _portableMode = value; - Settings.PortableMode = value; - Save(); - PortabilityUpdate(value); + bool switchToPortable; + if(DataLocation.PortableDataLocationInUse()) + { + switchToPortable = false; + } + else + { + switchToPortable = true; + } + + PortabilityUpdate(switchToPortable); } } - private void PortabilityUpdate(bool enabled) + private void PortabilityUpdate(bool enablePortableMode) { - if (enabled) + if (enablePortableMode) { _portable.EnablePortableMode(); } From 62c11a09c5dfbc2a591a75ee16af8c5fd6dc0da4 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Sun, 22 Mar 2020 19:52:16 +1100 Subject: [PATCH 28/33] Make debug mode clearer during exception --- Wox.Core/Configuration/Portable.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 7789072bd..1d17f000a 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -40,14 +40,11 @@ public void DisablePortableMode() } catch (Exception e) { -#if DEBUG - portabilityUpdater.Dispose(); - throw; -#else portabilityUpdater.Dispose(); +#if !DEBUG Log.Exception("Portable", "Error occured while disabling portable mode", e); - throw; #endif + throw; } } @@ -76,14 +73,11 @@ public void EnablePortableMode() } catch (Exception e) { -#if DEBUG - portabilityUpdater.Dispose(); - throw; -#else portabilityUpdater.Dispose(); +#if !DEBUG Log.Exception("Portable", "Error occured while enabling portable mode", e); - throw; #endif + throw; } } From 3e76b38a5631c3471205505f43973da802a7e739 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Mon, 23 Mar 2020 17:38:32 +1100 Subject: [PATCH 29/33] Per comment change filename to Constant --- Wox.Infrastructure/{Wox.cs => Constant.cs} | 0 Wox.Infrastructure/Wox.Infrastructure.csproj | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename Wox.Infrastructure/{Wox.cs => Constant.cs} (100%) diff --git a/Wox.Infrastructure/Wox.cs b/Wox.Infrastructure/Constant.cs similarity index 100% rename from Wox.Infrastructure/Wox.cs rename to Wox.Infrastructure/Constant.cs diff --git a/Wox.Infrastructure/Wox.Infrastructure.csproj b/Wox.Infrastructure/Wox.Infrastructure.csproj index a2a3086d0..65bd29d49 100644 --- a/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -78,7 +78,7 @@ - + From b85f5a147d581f4b031e462d1a15512f5ca872fd Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Mon, 23 Mar 2020 17:45:42 +1100 Subject: [PATCH 30/33] Remove unnecessary interface methods --- Wox.Core/Configuration/IPortable.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Wox.Core/Configuration/IPortable.cs b/Wox.Core/Configuration/IPortable.cs index b0af43672..a009b076b 100644 --- a/Wox.Core/Configuration/IPortable.cs +++ b/Wox.Core/Configuration/IPortable.cs @@ -3,8 +3,6 @@ namespace Wox.Core.Configuration { public interface IPortable { - void EnablePortableMode(); - void DisablePortableMode(); void RemoveShortcuts(); void RemoveUninstallerEntry(); void CreateShortcuts(); From 86903e5e63dc55d67c0e8a3c58aef846ee862a27 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Mon, 23 Mar 2020 18:09:52 +1100 Subject: [PATCH 31/33] Wrap UpdateManager in using statement + enable methods run as standalone --- Wox.Core/Configuration/Portable.cs | 51 ++++++++++++++++++------------ 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/Wox.Core/Configuration/Portable.cs b/Wox.Core/Configuration/Portable.cs index 1d17f000a..ee916001b 100644 --- a/Wox.Core/Configuration/Portable.cs +++ b/Wox.Core/Configuration/Portable.cs @@ -13,12 +13,17 @@ namespace Wox.Core.Configuration { public class Portable : IPortable { - private UpdateManager portabilityUpdater; + /// + /// As at Squirrel.Windows version 1.5.2, UpdateManager needs to be disposed after finish + /// + /// + private UpdateManager NewUpdateManager() + { + return new UpdateManager(string.Empty, Constant.Wox, Constant.RootDirectory); + } public void DisablePortableMode() { - portabilityUpdater = new UpdateManager(string.Empty, Constant.Wox, Constant.RootDirectory); - try { MoveUserDataFolder(DataLocation.PortableDataPath, DataLocation.RoamingDataPath); @@ -34,13 +39,10 @@ public void DisablePortableMode() MessageBox.Show("Wox needs to restart to finish disabling portable mode, " + "after the restart your portable data profile will be deleted and roaming data profile kept"); - portabilityUpdater.Dispose(); - UpdateManager.RestartApp(); } catch (Exception e) { - portabilityUpdater.Dispose(); #if !DEBUG Log.Exception("Portable", "Error occured while disabling portable mode", e); #endif @@ -50,8 +52,6 @@ public void DisablePortableMode() public void EnablePortableMode() { - portabilityUpdater = new UpdateManager(string.Empty, Constant.Wox, Constant.RootDirectory); - try { MoveUserDataFolder(DataLocation.RoamingDataPath, DataLocation.PortableDataPath); @@ -67,13 +67,10 @@ public void EnablePortableMode() MessageBox.Show("Wox needs to restart to finish enabling portable mode, " + "after the restart your roaming data profile will be deleted and portable data profile kept"); - portabilityUpdater.Dispose(); - UpdateManager.RestartApp(); } catch (Exception e) { - portabilityUpdater.Dispose(); #if !DEBUG Log.Exception("Portable", "Error occured while enabling portable mode", e); #endif @@ -83,15 +80,21 @@ public void EnablePortableMode() public void RemoveShortcuts() { - var exeName = Constant.Wox + ".exe"; - portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.StartMenu); - portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.Desktop); - portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.Startup); + using (var portabilityUpdater = NewUpdateManager()) + { + var exeName = Constant.Wox + ".exe"; + portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.StartMenu); + portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.Desktop); + portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.Startup); + } } public void RemoveUninstallerEntry() { - portabilityUpdater.RemoveUninstallerRegistryEntry(); + using (var portabilityUpdater = NewUpdateManager()) + { + portabilityUpdater.RemoveUninstallerRegistryEntry(); + } } public void MoveUserDataFolder(string fromLocation, string toLocation) @@ -107,10 +110,13 @@ public void VerifyUserDataAfterMove(string fromLocation, string toLocation) public void CreateShortcuts() { - var exeName = Constant.Wox + ".exe"; - portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.StartMenu, false); - portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.Desktop, false); - portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.Startup, false); + using (var portabilityUpdater = NewUpdateManager()) + { + var exeName = Constant.Wox + ".exe"; + portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.StartMenu, false); + portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.Desktop, false); + portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.Startup, false); + } } public void CreateUninstallerEntry() @@ -125,7 +131,10 @@ public void CreateUninstallerEntry() .CreateSubKey(uninstallRegSubKey + "\\" + Constant.Wox, RegistryKeyPermissionCheck.ReadWriteSubTree); key.SetValue("DisplayIcon", Constant.ApplicationDirectory + "\\app.ico", RegistryValueKind.String); - portabilityUpdater.CreateUninstallerRegistryEntry(); + using (var portabilityUpdater = NewUpdateManager()) + { + portabilityUpdater.CreateUninstallerRegistryEntry(); + } } internal void IndicateDeletion(string filePathTodelete) From 733326b329a42ec38b416c27aab0d40855280860 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Mon, 23 Mar 2020 18:23:40 +1100 Subject: [PATCH 32/33] Revert accidental removal of interface methods --- Wox.Core/Configuration/IPortable.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Wox.Core/Configuration/IPortable.cs b/Wox.Core/Configuration/IPortable.cs index a009b076b..b0af43672 100644 --- a/Wox.Core/Configuration/IPortable.cs +++ b/Wox.Core/Configuration/IPortable.cs @@ -3,6 +3,8 @@ namespace Wox.Core.Configuration { public interface IPortable { + void EnablePortableMode(); + void DisablePortableMode(); void RemoveShortcuts(); void RemoveUninstallerEntry(); void CreateShortcuts(); From 7a6edd0d8601e3961adc5038a20dd14d0d12453b Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Mon, 23 Mar 2020 19:19:57 +1100 Subject: [PATCH 33/33] Per comment, simplify code in Setter --- Wox/ViewModel/SettingWindowViewModel.cs | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/Wox/ViewModel/SettingWindowViewModel.cs b/Wox/ViewModel/SettingWindowViewModel.cs index 6898b5a83..3f315a9e3 100644 --- a/Wox/ViewModel/SettingWindowViewModel.cs +++ b/Wox/ViewModel/SettingWindowViewModel.cs @@ -57,29 +57,14 @@ public bool PortableMode if (!_portable.CanUpdatePortability()) return; - bool switchToPortable; - if(DataLocation.PortableDataLocationInUse()) + if (DataLocation.PortableDataLocationInUse()) { - switchToPortable = false; + _portable.DisablePortableMode(); } else { - switchToPortable = true; + _portable.EnablePortableMode(); } - - PortabilityUpdate(switchToPortable); - } - } - - private void PortabilityUpdate(bool enablePortableMode) - { - if (enablePortableMode) - { - _portable.EnablePortableMode(); - } - else - { - _portable.DisablePortableMode(); } }