Skip to content
This repository has been archived by the owner on Feb 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #143 from jjw24/add_enable_portablemode_setting
Browse files Browse the repository at this point in the history
Enable/disable portable mode setting
  • Loading branch information
jjw24 authored Mar 31, 2020
2 parents d6f1d13 + cb0a4de commit 7e26689
Show file tree
Hide file tree
Showing 19 changed files with 328 additions and 45 deletions.
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();
}
}
203 changes: 203 additions & 0 deletions Wox.Core/Configuration/Portable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
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
{
/// <summary>
/// As at Squirrel.Windows version 1.5.2, UpdateManager needs to be disposed after finish
/// </summary>
/// <returns></returns>
private UpdateManager NewUpdateManager()
{
return new UpdateManager(string.Empty, Constant.Wox, Constant.RootDirectory);
}

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

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

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

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

public void RemoveShortcuts()
{
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()
{
using (var portabilityUpdater = NewUpdateManager())
{
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()
{
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()
{
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);

using (var portabilityUpdater = NewUpdateManager())
{
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
23 changes: 3 additions & 20 deletions Wox.Infrastructure/Wox.cs → Wox.Infrastructure/Constant.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
Expand All @@ -13,25 +12,9 @@ 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");

public static bool IsPortableMode;
public const string PortableFolderName = "UserData";
public static string PortableDataPath = Path.Combine(ProgramDirectory, PortableFolderName);
public static string DetermineDataDirectory()
{
if (Directory.Exists(PortableDataPath))
{
IsPortableMode = true;
return PortableDataPath;
}
else
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Wox);
}
}

public static readonly string DataDirectory = DetermineDataDirectory();
public static readonly string PluginsDirectory = Path.Combine(DataDirectory, Plugins);
public static readonly string ApplicationDirectory = Directory.GetParent(ProgramDirectory).ToString();
public static readonly string RootDirectory = Directory.GetParent(ApplicationDirectory).ToString();

public static readonly string PreinstalledDirectory = Path.Combine(ProgramDirectory, Plugins);
public const string Issue = "https://github.com/jjw24/Wox/issues/new";
public static readonly string Version = FileVersionInfo.GetVersionInfo(Assembly.Location.NonNull()).ProductVersion;
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
Loading

0 comments on commit 7e26689

Please sign in to comment.