|
| 1 | +using Microsoft.Win32; |
| 2 | +using Squirrel; |
| 3 | +using System; |
| 4 | +using System.IO; |
| 5 | +using System.Reflection; |
| 6 | +using System.Windows; |
| 7 | +using Wox.Infrastructure; |
| 8 | +using Wox.Infrastructure.Logger; |
| 9 | +using Wox.Infrastructure.UserSettings; |
| 10 | +using Wox.Plugin.SharedCommands; |
| 11 | + |
| 12 | +namespace Wox.Core.Configuration |
| 13 | +{ |
| 14 | + public class Portable : IPortable |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// As at Squirrel.Windows version 1.5.2, UpdateManager needs to be disposed after finish |
| 18 | + /// </summary> |
| 19 | + /// <returns></returns> |
| 20 | + private UpdateManager NewUpdateManager() |
| 21 | + { |
| 22 | + return new UpdateManager(string.Empty, Constant.Wox, Constant.RootDirectory); |
| 23 | + } |
| 24 | + |
| 25 | + public void DisablePortableMode() |
| 26 | + { |
| 27 | + try |
| 28 | + { |
| 29 | + MoveUserDataFolder(DataLocation.PortableDataPath, DataLocation.RoamingDataPath); |
| 30 | +#if DEBUG |
| 31 | + // Create shortcuts and uninstaller are not required in debug mode, |
| 32 | + // otherwise will repoint the path of the actual installed production version to the debug version |
| 33 | +#else |
| 34 | + CreateShortcuts(); |
| 35 | + CreateUninstallerEntry(); |
| 36 | +#endif |
| 37 | + IndicateDeletion(DataLocation.PortableDataPath); |
| 38 | + |
| 39 | + MessageBox.Show("Wox needs to restart to finish disabling portable mode, " + |
| 40 | + "after the restart your portable data profile will be deleted and roaming data profile kept"); |
| 41 | + |
| 42 | + UpdateManager.RestartApp(); |
| 43 | + } |
| 44 | + catch (Exception e) |
| 45 | + { |
| 46 | +#if !DEBUG |
| 47 | + Log.Exception("Portable", "Error occured while disabling portable mode", e); |
| 48 | +#endif |
| 49 | + throw; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public void EnablePortableMode() |
| 54 | + { |
| 55 | + try |
| 56 | + { |
| 57 | + MoveUserDataFolder(DataLocation.RoamingDataPath, DataLocation.PortableDataPath); |
| 58 | +#if DEBUG |
| 59 | + // Remove shortcuts and uninstaller are not required in debug mode, |
| 60 | + // otherwise will delete the actual installed production version |
| 61 | +#else |
| 62 | + RemoveShortcuts(); |
| 63 | + RemoveUninstallerEntry(); |
| 64 | +#endif |
| 65 | + IndicateDeletion(DataLocation.RoamingDataPath); |
| 66 | + |
| 67 | + MessageBox.Show("Wox needs to restart to finish enabling portable mode, " + |
| 68 | + "after the restart your roaming data profile will be deleted and portable data profile kept"); |
| 69 | + |
| 70 | + UpdateManager.RestartApp(); |
| 71 | + } |
| 72 | + catch (Exception e) |
| 73 | + { |
| 74 | +#if !DEBUG |
| 75 | + Log.Exception("Portable", "Error occured while enabling portable mode", e); |
| 76 | +#endif |
| 77 | + throw; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public void RemoveShortcuts() |
| 82 | + { |
| 83 | + using (var portabilityUpdater = NewUpdateManager()) |
| 84 | + { |
| 85 | + var exeName = Constant.Wox + ".exe"; |
| 86 | + portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.StartMenu); |
| 87 | + portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.Desktop); |
| 88 | + portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.Startup); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public void RemoveUninstallerEntry() |
| 93 | + { |
| 94 | + using (var portabilityUpdater = NewUpdateManager()) |
| 95 | + { |
| 96 | + portabilityUpdater.RemoveUninstallerRegistryEntry(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public void MoveUserDataFolder(string fromLocation, string toLocation) |
| 101 | + { |
| 102 | + FilesFolders.Copy(fromLocation, toLocation); |
| 103 | + VerifyUserDataAfterMove(fromLocation, toLocation); |
| 104 | + } |
| 105 | + |
| 106 | + public void VerifyUserDataAfterMove(string fromLocation, string toLocation) |
| 107 | + { |
| 108 | + FilesFolders.VerifyBothFolderFilesEqual(fromLocation, toLocation); |
| 109 | + } |
| 110 | + |
| 111 | + public void CreateShortcuts() |
| 112 | + { |
| 113 | + using (var portabilityUpdater = NewUpdateManager()) |
| 114 | + { |
| 115 | + var exeName = Constant.Wox + ".exe"; |
| 116 | + portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.StartMenu, false); |
| 117 | + portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.Desktop, false); |
| 118 | + portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.Startup, false); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public void CreateUninstallerEntry() |
| 123 | + { |
| 124 | + var uninstallRegSubKey = @"Software\Microsoft\Windows\CurrentVersion\Uninstall"; |
| 125 | + // NB: Sometimes the Uninstall key doesn't exist |
| 126 | + using (var parentKey = |
| 127 | + RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default) |
| 128 | + .CreateSubKey("Uninstall", RegistryKeyPermissionCheck.ReadWriteSubTree)) {; } |
| 129 | + |
| 130 | + var key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default) |
| 131 | + .CreateSubKey(uninstallRegSubKey + "\\" + Constant.Wox, RegistryKeyPermissionCheck.ReadWriteSubTree); |
| 132 | + key.SetValue("DisplayIcon", Constant.ApplicationDirectory + "\\app.ico", RegistryValueKind.String); |
| 133 | + |
| 134 | + using (var portabilityUpdater = NewUpdateManager()) |
| 135 | + { |
| 136 | + portabilityUpdater.CreateUninstallerRegistryEntry(); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + internal void IndicateDeletion(string filePathTodelete) |
| 141 | + { |
| 142 | + using (StreamWriter sw = File.CreateText(filePathTodelete + "\\" + DataLocation.DeletionIndicatorFile)){} |
| 143 | + } |
| 144 | + |
| 145 | + ///<summary> |
| 146 | + ///This method should be run at first before all methods during start up and should be run before determining which data location |
| 147 | + ///will be used for Wox. |
| 148 | + ///</summary> |
| 149 | + public void PreStartCleanUpAfterPortabilityUpdate() |
| 150 | + { |
| 151 | + // Specify here so this method does not rely on other environment variables to initialise |
| 152 | + var portableDataPath = Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location.NonNull()).ToString(), "UserData"); |
| 153 | + var roamingDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Wox"); |
| 154 | + |
| 155 | + bool DataLocationPortableDeleteRequired = false; |
| 156 | + bool DataLocationRoamingDeleteRequired = false; |
| 157 | + |
| 158 | + if ((roamingDataPath + "\\" + DataLocation.DeletionIndicatorFile).FileExits()) |
| 159 | + DataLocationRoamingDeleteRequired = true; |
| 160 | + |
| 161 | + if ((portableDataPath + "\\" + DataLocation.DeletionIndicatorFile).FileExits()) |
| 162 | + DataLocationPortableDeleteRequired = true; |
| 163 | + |
| 164 | + if (DataLocationRoamingDeleteRequired) |
| 165 | + { |
| 166 | + if(roamingDataPath.LocationExists()) |
| 167 | + MessageBox.Show("Wox detected you restarted after enabling portable mode, " + |
| 168 | + "your roaming data profile will now be deleted"); |
| 169 | + |
| 170 | + FilesFolders.RemoveFolderIfExists(roamingDataPath); |
| 171 | + |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + if(DataLocationPortableDeleteRequired) |
| 176 | + { |
| 177 | + MessageBox.Show("Wox detected you restarted after disabling portable mode, " + |
| 178 | + "your portable data profile will now be deleted"); |
| 179 | + |
| 180 | + FilesFolders.RemoveFolderIfExists(portableDataPath); |
| 181 | + |
| 182 | + return; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + public bool CanUpdatePortability() |
| 187 | + { |
| 188 | + var roamingLocationExists = DataLocation.RoamingDataPath.LocationExists(); |
| 189 | + var portableLocationExists = DataLocation.PortableDataPath.LocationExists(); |
| 190 | + |
| 191 | + if(roamingLocationExists && portableLocationExists) |
| 192 | + { |
| 193 | + MessageBox.Show(string.Format("Wox detected your user data exists both in {0} and " + |
| 194 | + "{1}. {2}{2}Please delete {1} in order to proceed. No changes have occured.", |
| 195 | + DataLocation.PortableDataPath, DataLocation.RoamingDataPath, Environment.NewLine)); |
| 196 | + |
| 197 | + return false; |
| 198 | + } |
| 199 | + |
| 200 | + return true; |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments