Skip to content
This repository was archived by the owner on Feb 12, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7385243
Add enable portable mode logic
jjw24 Feb 20, 2020
d55560e
Add UserData operations- Copy, verify, remove
jjw24 Feb 21, 2020
69f66b7
update
jjw24 Feb 23, 2020
ad4c9fb
Add possible exception path too long handling
jjw24 Feb 23, 2020
ba46ce0
Update with exception handling and continue
jjw24 Feb 23, 2020
61845dd
Add disable portable mode code
jjw24 Feb 24, 2020
2531f71
Add disable portable mode programatically
jjw24 Mar 2, 2020
dbc52f5
Merge branch 'dev' into add_enable_portablemode_setting
jjw24 Mar 3, 2020
39eac96
Change method name to make intention clearer. Add additional methods
jjw24 Mar 5, 2020
e6b988d
Add code to enable portable mode
jjw24 Mar 5, 2020
7ac69f9
Update disable portable mode code
jjw24 Mar 5, 2020
56b8b5f
After restart, clean up data folder locations so only one is used
jjw24 Mar 5, 2020
f6ae266
Add settings check box ui and viewmodel
jjw24 Mar 5, 2020
c2d7e65
Add settings saving to file
jjw24 Mar 5, 2020
fd4f0f6
Add method used to indicate deletion of data location
jjw24 Mar 5, 2020
be8da80
Remove Wait on create uninstaller reg entry call
jjw24 Mar 5, 2020
4fc3016
separate get data location code from constant file
jjw24 Mar 5, 2020
6134550
Include DataLocation.cs in project file
jjw24 Mar 5, 2020
d0976a9
Update all applicable files to use the new data location class
jjw24 Mar 5, 2020
ef1b521
Change PortableDataLocationInUse property to method
jjw24 Mar 6, 2020
930ceef
Change method name for cleaning up after portability update
jjw24 Mar 6, 2020
82a24c9
Add validation prior to update portability
jjw24 Mar 6, 2020
83df0b3
Remove unnecessary method implementation
jjw24 Mar 6, 2020
31dd774
Update add comments change to internal
jjw24 Mar 7, 2020
a70615d
Add production logging
jjw24 Mar 8, 2020
830378e
update
jjw24 Mar 8, 2020
f933f9d
cater for debug mode
jjw24 Mar 8, 2020
f939abb
Remove need for saving to settings file by getting status dynamically
jjw24 Mar 8, 2020
c5dc21d
Merge branch 'dev' into add_enable_portablemode_setting
jjw24 Mar 22, 2020
62c11a0
Make debug mode clearer during exception
jjw24 Mar 22, 2020
3e76b38
Per comment change filename to Constant
jjw24 Mar 23, 2020
b85f5a1
Remove unnecessary interface methods
jjw24 Mar 23, 2020
86903e5
Wrap UpdateManager in using statement + enable methods run as standalone
jjw24 Mar 23, 2020
733326b
Revert accidental removal of interface methods
jjw24 Mar 23, 2020
7a6edd0
Per comment, simplify code in Setter
jjw24 Mar 23, 2020
cb0a4de
Merge branch 'dev' into add_enable_portablemode_setting
jjw24 Mar 31, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Plugins/Wox.Plugin.Program/Logger/ProgramLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NLog;
using NLog;
using NLog.Config;
using NLog.Targets;
using System;
Expand All @@ -7,6 +7,7 @@
using System.Runtime.CompilerServices;
using System.Security;
using Wox.Infrastructure;
using Wox.Infrastructure.UserSettings;

namespace Wox.Plugin.Program.Logger
{
Expand All @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions Wox.Core/Configuration/IPortable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

namespace Wox.Core.Configuration
{
public interface IPortable
{
void EnablePortableMode();
void DisablePortableMode();
void RemoveShortcuts();
void RemoveUninstallerEntry();
void CreateShortcuts();
void CreateUninstallerEntry();
void MoveUserDataFolder(string fromLocation, string toLocation);
void VerifyUserDataAfterMove(string fromLocation, string toLocation);
bool CanUpdatePortability();
}
}
200 changes: 200 additions & 0 deletions Wox.Core/Configuration/Portable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
using Microsoft.Win32;
using Squirrel;
using System;
using System.IO;
using System.Reflection;
using System.Windows;
using Wox.Infrastructure;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.UserSettings;
using Wox.Plugin.SharedCommands;

namespace Wox.Core.Configuration
{
public class Portable : IPortable
{
private UpdateManager portabilityUpdater;

public void DisablePortableMode()
{
portabilityUpdater = new UpdateManager(string.Empty, Constant.Wox, Constant.RootDirectory);

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, " +
"after the restart your portable data profile will be deleted and roaming data profile kept");

portabilityUpdater.Dispose();

UpdateManager.RestartApp();
}
catch (Exception e)
{
#if DEBUG
portabilityUpdater.Dispose();
throw;
#else
portabilityUpdater.Dispose();
Log.Exception("Portable", "Error occured while disabling portable mode", e);
throw;
#endif
}
}

public void EnablePortableMode()
{
portabilityUpdater = new UpdateManager(string.Empty, Constant.Wox, Constant.RootDirectory);

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, " +
"after the restart your roaming data profile will be deleted and portable data profile kept");

portabilityUpdater.Dispose();

UpdateManager.RestartApp();
}
catch (Exception e)
{
#if DEBUG
portabilityUpdater.Dispose();
throw;
#else
portabilityUpdater.Dispose();
Log.Exception("Portable", "Error occured while enabling portable mode", e);
throw;
#endif
}
}

public void RemoveShortcuts()
{
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();
}

public void MoveUserDataFolder(string fromLocation, string toLocation)
{
FilesFolders.Copy(fromLocation, toLocation);
VerifyUserDataAfterMove(fromLocation, toLocation);
}

public void VerifyUserDataAfterMove(string fromLocation, string toLocation)
{
FilesFolders.VerifyBothFolderFilesEqual(fromLocation, 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);
}

public void CreateUninstallerEntry()
{
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 + "\\" + Constant.Wox, RegistryKeyPermissionCheck.ReadWriteSubTree);
key.SetValue("DisplayIcon", Constant.ApplicationDirectory + "\\app.ico", RegistryValueKind.String);

portabilityUpdater.CreateUninstallerRegistryEntry();
}

internal void IndicateDeletion(string filePathTodelete)
{
using (StreamWriter sw = File.CreateText(filePathTodelete + "\\" + DataLocation.DeletionIndicatorFile)){}
}

///<summary>
///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.
///</summary>
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");

bool DataLocationPortableDeleteRequired = false;
bool DataLocationRoamingDeleteRequired = false;

if ((roamingDataPath + "\\" + DataLocation.DeletionIndicatorFile).FileExits())
DataLocationRoamingDeleteRequired = true;

if ((portableDataPath + "\\" + DataLocation.DeletionIndicatorFile).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;
}
}

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;
}
}
}
2 changes: 1 addition & 1 deletion Wox.Core/Plugin/PluginInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("/", "_")
Expand Down
8 changes: 4 additions & 4 deletions Wox.Core/Plugin/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PluginMetadata> _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);
Expand Down
2 changes: 1 addition & 1 deletion Wox.Core/Resource/Theme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
11 changes: 6 additions & 5 deletions Wox.Core/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Wox.Infrastructure.Http;
using Wox.Infrastructure.Logger;
using System.IO;
using Wox.Infrastructure.UserSettings;

namespace Wox.Core
{
Expand Down Expand Up @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions Wox.Core/Wox.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
<Compile Include="..\SolutionAssemblyInfo.cs">
<Link>Properties\SolutionAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Configuration\IPortable.cs" />
<Compile Include="Configuration\Portable.cs" />
<Compile Include="Plugin\ExecutablePlugin.cs" />
<Compile Include="Plugin\PluginsLoader.cs" />
<Compile Include="Resource\LocalizationConverter.cs" />
Expand Down
3 changes: 2 additions & 1 deletion Wox.Infrastructure/Logger/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using NLog;
using NLog.Config;
using NLog.Targets;
using Wox.Infrastructure.UserSettings;

namespace Wox.Infrastructure.Logger
{
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion Wox.Infrastructure/Storage/BinaryStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -17,7 +18,7 @@ public class BinaryStorage<T>
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";
Expand Down
3 changes: 2 additions & 1 deletion Wox.Infrastructure/Storage/PluginJsonStorage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using Wox.Infrastructure.UserSettings;

namespace Wox.Infrastructure.Storage
{
Expand All @@ -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}");
Expand Down
3 changes: 2 additions & 1 deletion Wox.Infrastructure/Storage/WoxJsonStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wox.Infrastructure.UserSettings;

namespace Wox.Infrastructure.Storage
{
public class WoxJsonStorage<T> : JsonStrorage<T> where T : new()
{
public WoxJsonStorage()
{
var directoryPath = Path.Combine(Constant.DataDirectory, DirectoryName);
var directoryPath = Path.Combine(DataLocation.DataDirectory(), DirectoryName);
Helper.ValidateDirectory(directoryPath);

var filename = typeof(T).Name;
Expand Down
Loading