From e6234ebbc68f984e39507aec72816a423da00d80 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Mon, 20 Jun 2016 13:13:18 -0700 Subject: [PATCH] Fix missing printers and presets - Add code to save ProfileManager contents on Add Printer - Revise and rerun printer profile generation tool - Move CollectionChanged registration, don't run during deserialization - CollectionChanged.Remove no longer valid as Deletes are deferred - Fixes #982, #983 - Remove dead code --- .../Settings/ProfileManager.cs | 49 +++---------------- 1 file changed, 7 insertions(+), 42 deletions(-) diff --git a/SlicerConfiguration/Settings/ProfileManager.cs b/SlicerConfiguration/Settings/ProfileManager.cs index 280768e75..07c52e660 100644 --- a/SlicerConfiguration/Settings/ProfileManager.cs +++ b/SlicerConfiguration/Settings/ProfileManager.cs @@ -39,6 +39,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration using Agg; using Localizations; using System.Collections.ObjectModel; + using System.Collections.Specialized; using System.Net; public class ProfileManager @@ -73,12 +74,12 @@ namespace MatterHackers.MatterControl.SlicerConfiguration if (File.Exists(profilesDBPath)) { Instance = JsonConvert.DeserializeObject(File.ReadAllText(profilesDBPath)); + Instance.Profiles.CollectionChanged += Profiles_CollectionChanged; } } public ProfileManager() { - Profiles.CollectionChanged += Profiles_CollectionChanged; } internal static void SettingsChanged(object sender, EventArgs e) @@ -214,7 +215,9 @@ namespace MatterHackers.MatterControl.SlicerConfiguration var layeredProfile = new PrinterSettings(printerProfile, baseConfig) { - ID = guid + ID = guid, + // TODO: This should really be set by the system that generates the source documents + DocumentVersion = PrinterSettings.LatestVersion }; layeredProfile.UserLayer[SettingsKey.printer_name.ToString()] = printerName; @@ -288,50 +291,12 @@ namespace MatterHackers.MatterControl.SlicerConfiguration private static void Profiles_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { - if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) - { - // TODO: This doesn't look right. We need to delete the removed ID not the active on, in case they're different!!!! - string profilePath = Path.Combine(ProfilesPath, ActiveSliceSettings.Instance.ID + ".json"); - if (File.Exists(profilePath)) - { - File.Delete(profilePath); - } - - // Refresh after change - UiThread.RunOnIdle(() => ActiveSliceSettings.Instance = LoadEmptyProfile()); - } + // Any time the list changes, persist the updates to disk + Instance.Save(); ProfilesListChanged.CallEvents(null, null); } - /* - private static void LoadProfilesFromDisk() - { - foreach (string filePath in Directory.GetFiles(ProfilesPath, "*.json")) - { - string fileName = Path.GetFileName(filePath); - if (fileName == "config.json" || fileName == "profiles.json") - { - continue; - } - - try - { - var profile = new SettingsProfile(PrinterSettings.LoadFile(filePath)); - ProfileManager.Instance.Profiles.Add(new PrinterInfo() - { - ComPort = profile.ComPort(), - ID = profile.ID, - Name = profile.GetValue(SettingsKey.printer_name), - }); - } - catch (Exception ex) - { - System.Diagnostics.Debug.WriteLine("Error loading profile: {1}\r\n{2}", filePath, ex.Message); - } - } - }*/ - public void Save() { File.WriteAllText(profilesDBPath, JsonConvert.SerializeObject(this, Formatting.Indented));