Refactor ProfileManager to prefer isolated instances

- Replace DB nomenclatures with ProfilesDoc/Profiles terms
- Convert SetLastProfile() to LastProfileID property
- Remove LoadLastProfileWithoutRecovery function
  - Use LoadWithoutRecovery(LastProfileID)
  - One caller, far more clear with only one LoadWithout function
- Move safe FileSystem UserName to AuthenticationData, like UserName
- Skip importing guest profiles that are missing PrinterSettings files
This commit is contained in:
John Lewin 2016-10-21 12:59:10 -07:00
parent 9cb1aa1575
commit 42cc88b9d4
6 changed files with 112 additions and 129 deletions

View file

@ -47,104 +47,95 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
{
public static RootedObjectEventHandler ProfilesListChanged = new RootedObjectEventHandler();
public static ProfileManager Instance { get; set; }
public static ProfileManager Instance { get; private set; }
private static EventHandler unregisterEvents;
public const string ProfileExtension = ".printer";
public const string ConfigFileExtension = ".slice";
public const string ProfileDocExtension = ".profiles";
private static object writeLock = new object();
private static EventHandler unregisterEvents;
private static readonly string userDataPath = ApplicationDataStorage.ApplicationUserDataPath;
/// <summary>
/// The user specific path to the Profiles directory
/// </summary>
private static string ProfilesPath
{
get
{
// Determine username
string username = ApplicationController.Instance.GetSessionUsernameForFileSystem();
if (string.IsNullOrEmpty(username))
{
username = "guest";
}
else
{
username = ApplicationController.EnvironmentName + username;
}
string path = Path.Combine(userDataPath, "Profiles", username);
// Ensure directory exists
Directory.CreateDirectory(path);
return path;
}
}
private const string userDBExtension = ".profiles";
private const string guestDBFileName = "guest" + userDBExtension;
internal static string GuestDBDirectory => Path.Combine(userDataPath, "Profiles", "guest");
private static string GuestDBPath => Path.Combine(GuestDBDirectory, guestDBFileName);
internal static string ProfilesDBPath
{
get
{
string username = ApplicationController.Instance.GetSessionUsernameForFileSystem();
if (string.IsNullOrEmpty(username))
{
username = GuestDBPath;
}
else
{
username = Path.Combine(ProfilesPath, $"{username}{userDBExtension}");
}
return username;
}
}
private object writeLock = new object();
static ProfileManager()
{
SliceSettingsWidget.SettingChanged.RegisterEvent(SettingsChanged, ref unregisterEvents);
// Ensure the profiles directory exists
Directory.CreateDirectory(ProfilesPath);
Reload();
}
public ProfileManager()
public ProfileManager(string userName)
{
this.UserName = userName;
}
public string UserName { get; set; }
/// <summary>
/// The user specific path to the Profiles directory
/// </summary>
[JsonIgnore]
private string UserProfilesDirectory => GetProfilesDirectoryForUser(this.UserName);
/// <summary>
/// The user specific path to the Profiles document
/// </summary>
[JsonIgnore]
public string ProfilesDocPath => GetProfilesDocPathForUser(this.UserName);
private static string GetProfilesDocPathForUser(string userName)
{
return Path.Combine(GetProfilesDirectoryForUser(userName), $"{userName}{ProfileDocExtension}");
}
private static string GetProfilesDirectoryForUser(string userName)
{
string userProfilesDirectory = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "Profiles", userName);
// Ensure directory exists
Directory.CreateDirectory(userProfilesDirectory);
return userProfilesDirectory;
}
[JsonIgnore]
public bool IsGuestProfile => Path.GetFileName(ProfilesDBPath) == guestDBFileName;
public bool IsGuestProfile { get; private set; } = false;
public static void Reload()
{
string userName = AuthenticationData.Instance.FileSystemSafeUserName;
if (string.IsNullOrEmpty(userName))
{
userName = "guest";
}
if (Instance?.UserName == userName)
{
return;
}
if (Instance?.Profiles != null)
{
// Release event registration
Instance.Profiles.CollectionChanged -= Profiles_CollectionChanged;
}
// Load the profiles document
if (File.Exists(ProfilesDBPath))
string profilesDocPath = GetProfilesDocPathForUser(userName);
// Reassign the active instance based on the logged in user
if (File.Exists(profilesDocPath))
{
string json = File.ReadAllText(ProfilesDBPath);
string json = File.ReadAllText(profilesDocPath);
Instance = JsonConvert.DeserializeObject<ProfileManager>(json);
Instance.UserName = userName;
}
else
{
Instance = new ProfileManager();
Instance = new ProfileManager(userName);
}
if (ActiveSliceSettings.Instance?.ID != Instance.LastProfileID)
{
// async so we can safely wait for LoadProfileAsync to complete
Task.Run(async () =>
{
// Load or download on a background thread
@ -152,6 +143,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
if (MatterControlApplication.IsLoading)
{
// TODO: Not true - we're on a background thread in an async lambda... what is the intent of this?
// Assign on the UI thread
ActiveSliceSettings.Instance = lastProfile ?? LoadEmptyProfile();
}
@ -170,15 +162,9 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
Instance.Profiles.CollectionChanged += Profiles_CollectionChanged;
}
internal static ProfileManager LoadGuestDB()
internal static ProfileManager LoadGuestProfiles()
{
if (File.Exists(GuestDBPath))
{
string json = File.ReadAllText(GuestDBPath);
return JsonConvert.DeserializeObject<ProfileManager>(json);
}
return null;
return new ProfileManager("guest") { IsGuestProfile = true };
}
internal static void SettingsChanged(object sender, EventArgs e)
@ -227,35 +213,27 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
{
get
{
string activeUserName = ApplicationController.Instance.GetSessionUsernameForFileSystem();
return UserSettings.Instance.get($"ActiveProfileID-{activeUserName}");
return UserSettings.Instance.get($"ActiveProfileID-{UserName}");
}
set
{
UserSettings.Instance.set($"ActiveProfileID-{UserName}", value);
}
}
public bool PrintersImported { get; set; } = false;
public PrinterSettings LoadLastProfileWithoutRecovery()
{
return LoadWithoutRecovery(this.LastProfileID);
}
public void SetLastProfile(string printerID)
{
string activeUserName = ApplicationController.Instance.GetSessionUsernameForFileSystem();
UserSettings.Instance.set($"ActiveProfileID-{activeUserName}", printerID);
}
public string ProfilePath(PrinterInfo printer)
{
return Path.Combine(ProfileManager.ProfilesPath, printer.ID + ProfileExtension);
}
public string ProfilePath(string printerID)
{
return ProfilePath(this[printerID]);
}
public static PrinterSettings LoadWithoutRecovery(string profileID)
public string ProfilePath(PrinterInfo printer)
{
return Path.Combine(UserProfilesDirectory, printer.ID + ProfileExtension);
}
public PrinterSettings LoadWithoutRecovery(string profileID)
{
var printerInfo = Instance[profileID];
@ -291,14 +269,14 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
}
// Only load profiles by ID that are defined in the profiles document
var printerInfo = ProfileManager.Instance[profileID];
var printerInfo = Instance[profileID];
if (printerInfo == null)
{
return null;
}
// Attempt to load from disk, pull from the web or fall back using recovery logic
PrinterSettings printerSettings = LoadWithoutRecovery(profileID);
PrinterSettings printerSettings = Instance.LoadWithoutRecovery(profileID);
if (printerSettings != null)
{
return printerSettings;
@ -553,7 +531,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
if (IsGuestProfile && !PrintersImported)
{
// Import Sqlite printer profiles into local json files
DataStorage.ClassicDB.ClassicSqlitePrinterProfiles.ImportPrinters(Instance, ProfilesPath);
DataStorage.ClassicDB.ClassicSqlitePrinterProfiles.ImportPrinters(Instance, UserProfilesDirectory);
PrintersImported = true;
Save();
}
@ -574,7 +552,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
{
lock(writeLock)
{
File.WriteAllText(ProfilesDBPath, JsonConvert.SerializeObject(this, Formatting.Indented));
File.WriteAllText(ProfilesDocPath, JsonConvert.SerializeObject(this, Formatting.Indented));
}
}
}