diff --git a/ApplicationView/AdvancedControlsPanel.cs b/ApplicationView/AdvancedControlsPanel.cs index 82a5db16e..5c3a0497f 100644 --- a/ApplicationView/AdvancedControlsPanel.cs +++ b/ApplicationView/AdvancedControlsPanel.cs @@ -57,7 +57,7 @@ namespace MatterHackers.MatterControl AddChild(advancedTab); AnchorAll(); - ApplicationController.Instance.ReloadAdvancedControlsPanelTrigger.RegisterEvent((s, e) => UiThread.RunOnIdle(ReloadSliceSettings), ref unregisterEvents); + ApplicationController.Instance.AdvancedControlsPanelReloading.RegisterEvent((s, e) => UiThread.RunOnIdle(ReloadSliceSettings), ref unregisterEvents); } public static string SliceSettingsTabName { get; } = "Slice Settings Tab"; diff --git a/ApplicationView/MainApplicationWidget.cs b/ApplicationView/MainApplicationWidget.cs index 863aa0225..49167f1e9 100644 --- a/ApplicationView/MainApplicationWidget.cs +++ b/ApplicationView/MainApplicationWidget.cs @@ -158,7 +158,7 @@ namespace MatterHackers.MatterControl public class ApplicationController { private static ApplicationController globalInstance; - public RootedObjectEventHandler ReloadAdvancedControlsPanelTrigger = new RootedObjectEventHandler(); + public RootedObjectEventHandler AdvancedControlsPanelReloading = new RootedObjectEventHandler(); public RootedObjectEventHandler CloudSyncStatusChanged = new RootedObjectEventHandler(); public RootedObjectEventHandler DoneReloadingAll = new RootedObjectEventHandler(); public RootedObjectEventHandler PluginsLoaded = new RootedObjectEventHandler(); @@ -185,12 +185,7 @@ namespace MatterHackers.MatterControl public ApplicationController() { //Name = "MainSlidePanel"; - ActiveTheme.ThemeChanged.RegisterEvent(ThemeChanged, ref unregisterEvents); - } - - public void ThemeChanged(object sender, EventArgs e) - { - ReloadAll(null, null); + ActiveTheme.ThemeChanged.RegisterEvent(ReloadAll, ref unregisterEvents); } public void StartLogin() @@ -322,7 +317,7 @@ namespace MatterHackers.MatterControl public void ReloadAdvancedControlsPanel() { - ReloadAdvancedControlsPanelTrigger.CallEvents(this, null); + AdvancedControlsPanelReloading.CallEvents(this, null); } public LibraryDataView CurrentLibraryDataView = null; @@ -360,7 +355,33 @@ namespace MatterHackers.MatterControl string activeUserName = ApplicationController.Instance.GetSessionUsernameForFileSystem(); UserSettings.Instance.set("ActiveUserName", activeUserName); + + UserChanged(); + } + + // Called after every startup and at the completion of every authentication change + public void UserChanged() + { ProfileManager.Reload(); + + var profileManager = ProfileManager.Instance; + + // Ensure SQLite printers are imported + profileManager.EnsurePrintersImported(); + + // If profiles.json was created, run the import wizard to pull in any SQLite printers + if (!profileManager.IsGuestProfile && !profileManager.PrintersImported) + { + var wizardPage = new CopyGuestProfilesToUser(() => + { + // On success, set state indicating import has been run and update ProfileManager state + profileManager.PrintersImported = true; + profileManager.Save(); + }); + + // Show the import printers wizard + WizardWindow.Show("/CopyGuestProfiles", "Copy Existing Profiles", wizardPage); + } } public class CloudSyncEventArgs : EventArgs diff --git a/ApplicationView/TouchscreenTabView.cs b/ApplicationView/TouchscreenTabView.cs index 32a5af62f..3c8595d46 100644 --- a/ApplicationView/TouchscreenTabView.cs +++ b/ApplicationView/TouchscreenTabView.cs @@ -219,7 +219,7 @@ namespace MatterHackers.MatterControl }, ref unregisterEvents); - ApplicationController.Instance.ReloadAdvancedControlsPanelTrigger.RegisterEvent((s, e) => UiThread.RunOnIdle(ReloadAdvancedControls), ref unregisterEvents); + ApplicationController.Instance.AdvancedControlsPanelReloading.RegisterEvent((s, e) => UiThread.RunOnIdle(ReloadAdvancedControls), ref unregisterEvents); UpdateControlData.Instance.UpdateStatusChanged.RegisterEvent(SetUpdateNotification, ref unregisterEvents); // Make sure we are on the right tab when we create this view diff --git a/ApplicationView/WidescreenPanel.cs b/ApplicationView/WidescreenPanel.cs index cc53d3834..17decfbfd 100644 --- a/ApplicationView/WidescreenPanel.cs +++ b/ApplicationView/WidescreenPanel.cs @@ -69,7 +69,7 @@ namespace MatterHackers.MatterControl Padding = new BorderDouble(4); PrinterConnectionAndCommunication.Instance.ActivePrintItemChanged.RegisterEvent(onActivePrintItemChanged, ref unregisterEvents); - ApplicationController.Instance.ReloadAdvancedControlsPanelTrigger.RegisterEvent((s, e) => UiThread.RunOnIdle(ReloadAdvancedControlsPanel), ref unregisterEvents); + ApplicationController.Instance.AdvancedControlsPanelReloading.RegisterEvent((s, e) => UiThread.RunOnIdle(ReloadAdvancedControlsPanel), ref unregisterEvents); this.BoundsChanged += onBoundsChanges; } diff --git a/DataStorage/Classic/ClassicSqlitePrinterProfiles.cs b/DataStorage/Classic/ClassicSqlitePrinterProfiles.cs index 4308a52bd..1bc304ebe 100644 --- a/DataStorage/Classic/ClassicSqlitePrinterProfiles.cs +++ b/DataStorage/Classic/ClassicSqlitePrinterProfiles.cs @@ -137,8 +137,12 @@ namespace MatterHackers.MatterControl.DataStorage.ClassicDB //layeredProfile.SetActiveValue(""calibration_files"", ???); layeredProfile.ID = printer.Id.ToString(); - string fullProfilePath = Path.Combine(profilePath, printer.Id + ".json"); - File.WriteAllText(fullProfilePath, JsonConvert.SerializeObject(layeredProfile, Formatting.Indented)); + + layeredProfile.DocumentVersion = PrinterSettings.LatestVersion; + + var settingsProfile = new SettingsProfile(layeredProfile); + settingsProfile.SaveChanges(); + } private static void LoadMaterialSettings(PrinterSettings layeredProfile, Printer printer) diff --git a/MatterControlApplication.cs b/MatterControlApplication.cs index 89a6cd3fb..9827e0975 100644 --- a/MatterControlApplication.cs +++ b/MatterControlApplication.cs @@ -451,8 +451,8 @@ namespace MatterHackers.MatterControl } #if DEBUG - //public static string MCWSBaseUri { get; } = "http://192.168.2.129:9206"; - public static string MCWSBaseUri { get; } = "https://mattercontrol-test.appspot.com"; + public static string MCWSBaseUri { get; } = "http://192.168.2.129:9206"; + //public static string MCWSBaseUri { get; } = "https://mattercontrol-test.appspot.com"; #else public static string MCWSBaseUri { get;} = "https://mattercontrol.appspot.com"; #endif @@ -694,6 +694,8 @@ namespace MatterHackers.MatterControl TerminalWindow.ShowIfLeftOpen(); + ApplicationController.Instance.UserChanged(); + #if false { SystemWindow releaseNotes = new SystemWindow(640, 480); diff --git a/PartPreviewWindow/BaseClasses/PartPreview3DWidget.cs b/PartPreviewWindow/BaseClasses/PartPreview3DWidget.cs index edb9681f5..e16a84b51 100644 --- a/PartPreviewWindow/BaseClasses/PartPreview3DWidget.cs +++ b/PartPreviewWindow/BaseClasses/PartPreview3DWidget.cs @@ -64,7 +64,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow public PartPreview3DWidget() { SliceSettingsWidget.SettingChanged.RegisterEvent(CheckSettingChanged, ref unregisterEvents); - ApplicationController.Instance.ReloadAdvancedControlsPanelTrigger.RegisterEvent(CheckSettingChanged, ref unregisterEvents); + ApplicationController.Instance.AdvancedControlsPanelReloading.RegisterEvent(CheckSettingChanged, ref unregisterEvents); #if false "extruder_offset", #endif diff --git a/PartPreviewWindow/ViewGcodeBasic.cs b/PartPreviewWindow/ViewGcodeBasic.cs index 84719f327..b30d91ebe 100644 --- a/PartPreviewWindow/ViewGcodeBasic.cs +++ b/PartPreviewWindow/ViewGcodeBasic.cs @@ -110,7 +110,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow CreateAndAddChildren(); SliceSettingsWidget.SettingChanged.RegisterEvent(CheckSettingChanged, ref unregisterEvents); - ApplicationController.Instance.ReloadAdvancedControlsPanelTrigger.RegisterEvent((s, e) => ClearGCode(), ref unregisterEvents); + ApplicationController.Instance.AdvancedControlsPanelReloading.RegisterEvent((s, e) => ClearGCode(), ref unregisterEvents); ActiveSliceSettings.ActivePrinterChanged.RegisterEvent(CheckSettingChanged, ref unregisterEvents); } diff --git a/SetupWizard/CopyGuestProfilesToUser.cs b/SetupWizard/CopyGuestProfilesToUser.cs index 94833ad37..5e256f4f3 100644 --- a/SetupWizard/CopyGuestProfilesToUser.cs +++ b/SetupWizard/CopyGuestProfilesToUser.cs @@ -35,18 +35,18 @@ using MatterHackers.MatterControl; using MatterHackers.MatterControl.CustomWidgets; using MatterHackers.Agg; using System.Collections.Generic; +using MatterHackers.MatterControl.SlicerConfiguration; namespace MatterHackers.MatterControl { public class CopyGuestProfilesToUser : WizardPage { - static string importMessage = "Select which printers you would like to copy into the user account '{0}'."; + static string importMessage = "Select what you would like to merge into your current profile.".Localize(); - List guestProfiles; List checkBoxes = new List(); - public CopyGuestProfilesToUser() - : base("Cancel", "Copy Printers to User") + public CopyGuestProfilesToUser(Action afterProfilesImported) + : base("Cancel", "Copy Guest Printers") { var scrollWindow = new ScrollableWidget() { @@ -63,35 +63,31 @@ namespace MatterHackers.MatterControl }; scrollWindow.AddChild(container); - container.AddChild(new WrappedTextWidget(importMessage.FormatWith(ApplicationController.Instance.GetSessionUsername()), - 10, textColor: ActiveTheme.Instance.PrimaryTextColor)); + container.AddChild(new WrappedTextWidget(importMessage, 10, textColor: ActiveTheme.Instance.PrimaryTextColor)); - guestProfiles = new List() - { - "TAZ6", - "JumpStart", - "Emulator", - "Other test printer", - }; + var byCheckbox = new Dictionary(); - if (guestProfiles.Count > 0) + var guestProfileManager = ProfileManager.LoadGuestDB(); + if (guestProfileManager.Profiles.Count > 0) { - container.AddChild(new TextWidget("") + container.AddChild(new TextWidget("Existing Printers:") { TextColor = ActiveTheme.Instance.PrimaryTextColor, - Margin = new BorderDouble(0, 3, 0, 3), + Margin = new BorderDouble(0, 3, 0, 15), }); - foreach (var profileName in guestProfiles) + foreach (var printerInfo in guestProfileManager.Profiles) { - CheckBox importButton = new CheckBox(profileName) + var checkBox = new CheckBox(printerInfo.Name) { TextColor = ActiveTheme.Instance.PrimaryTextColor, Margin = new BorderDouble(5, 0, 0, 0), HAnchor = HAnchor.ParentLeft, }; - checkBoxes.Add(importButton); - container.AddChild(importButton); + checkBoxes.Add(checkBox); + container.AddChild(checkBox); + + byCheckbox[checkBox] = printerInfo; } } @@ -99,18 +95,28 @@ namespace MatterHackers.MatterControl copyButton.Click += (s, e) => { // do the import - for(int i=0; i< checkBoxes.Count; i++) + foreach(var checkBox in checkBoxes) { - var checkBox = checkBoxes[i]; if (checkBox.Checked) { // import the printer + var printerInfo = byCheckbox[checkBox]; + ProfileManager.Instance.Profiles.Add(printerInfo); + guestProfileManager.Profiles.Remove(printerInfo); } } + guestProfileManager.Save(); + // close the window - UiThread.RunOnIdle(WizardWindow.Close); + UiThread.RunOnIdle(() => + { + WizardWindow.Close(); + + // Call back into the original source + afterProfilesImported(); + }); }; copyButton.Visible = true; diff --git a/SetupWizard/WizardWindow.cs b/SetupWizard/WizardWindow.cs index faf37da35..55990b4d7 100644 --- a/SetupWizard/WizardWindow.cs +++ b/SetupWizard/WizardWindow.cs @@ -80,21 +80,16 @@ namespace MatterHackers.MatterControl public static void Show(string uri, string title) where PanelType : WizardPage, new() { - WizardWindow existingWindow; + WizardWindow wizardWindow = GetWindow(uri); + wizardWindow.Title = title; + wizardWindow.ChangeToPage(); + } - if (allWindows.TryGetValue(uri, out existingWindow)) - { - existingWindow.BringToFront(); - } - else - { - existingWindow = new WizardWindow(); - existingWindow.Closed += (s, e) => allWindows.Remove(uri); - allWindows[uri] = existingWindow; - } - - existingWindow.Title = title; - existingWindow.ChangeToPage(); + public static void Show(string uri, string title, WizardPage wizardPage) + { + WizardWindow wizardWindow = GetWindow(uri); + wizardWindow.Title = title; + wizardWindow.ChangeToPage(wizardPage); } public static void Show(bool openToHome = false) @@ -110,6 +105,24 @@ namespace MatterHackers.MatterControl } } + private static WizardWindow GetWindow(string uri) + { + WizardWindow wizardWindow; + + if (allWindows.TryGetValue(uri, out wizardWindow)) + { + wizardWindow.BringToFront(); + } + else + { + wizardWindow = new WizardWindow(); + wizardWindow.Closed += (s, e) => allWindows.Remove(uri); + allWindows[uri] = wizardWindow; + } + + return wizardWindow; + } + public override void OnClosed(EventArgs e) { base.OnClosed(e); diff --git a/SlicerConfiguration/Settings/LayeredProfile.cs b/SlicerConfiguration/Settings/LayeredProfile.cs index a906453a2..f027da49f 100644 --- a/SlicerConfiguration/Settings/LayeredProfile.cs +++ b/SlicerConfiguration/Settings/LayeredProfile.cs @@ -168,7 +168,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration } } - private string DocumentPath => Path.Combine(ProfileManager.ProfilesPath, this.ID + ".json"); + private string DocumentPath => ProfileManager.Instance.ProfilePath(this.ID); internal void Save() { @@ -191,7 +191,10 @@ namespace MatterHackers.MatterControl.SlicerConfiguration File.WriteAllText(DocumentPath, json); - ActiveSliceSettings.ActiveProfileModified.CallEvents(null, null); + if (ActiveSliceSettings.Instance.ID == this.ID) + { + ActiveSliceSettings.ActiveProfileModified.CallEvents(null, null); + } } /// diff --git a/SlicerConfiguration/Settings/ProfileManager.cs b/SlicerConfiguration/Settings/ProfileManager.cs index 404e19f61..50724b08b 100644 --- a/SlicerConfiguration/Settings/ProfileManager.cs +++ b/SlicerConfiguration/Settings/ProfileManager.cs @@ -50,16 +50,21 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public static ProfileManager Instance { get; set; } - private static EventHandler unregisterEvents; - private static readonly string userDataPath = DataStorage.ApplicationDataStorage.ApplicationUserDataPath; - internal static readonly string ProfilesPath = Path.Combine(userDataPath, "Profiles"); + public static string ProfileExtension { get; } = ".json"; - private static string ProfilesDBPath + private static EventHandler unregisterEvents; + private static readonly string userDataPath = ApplicationDataStorage.ApplicationUserDataPath; + private static readonly string ProfilesPath = Path.Combine(userDataPath, "Profiles"); + private const string guestDBFileName = "guest.profiles"; + + private static string GuestDBPath => Path.Combine(ProfilesPath, guestDBFileName); + + internal static string ProfilesDBPath { get { string username = UserSettings.Instance.get("ActiveUserName"); - return Path.Combine(ProfilesPath, string.IsNullOrEmpty(username) ? "profiles.json" : username + ".json"); + return string.IsNullOrEmpty(username) ? GuestDBPath : Path.Combine(ProfilesPath, $"{username}.profiles"); } } @@ -77,8 +82,17 @@ namespace MatterHackers.MatterControl.SlicerConfiguration { } + [JsonIgnore] + public bool IsGuestProfile => Path.GetFileName(ProfilesDBPath) == guestDBFileName; + public static void Reload() { + if (Instance?.Profiles != null) + { + // Release event registration + Instance.Profiles.CollectionChanged -= Profiles_CollectionChanged; + } + // Load the profiles document if (File.Exists(ProfilesDBPath)) { @@ -88,28 +102,28 @@ namespace MatterHackers.MatterControl.SlicerConfiguration else { Instance = new ProfileManager(); - - if (Path.GetFileName(ProfilesDBPath) == "profiles.json") - { - // Import classic db based profiles into local json files - DataStorage.ClassicDB.ClassicSqlitePrinterProfiles.ImportPrinters(Instance, ProfilesPath); - } } - // Load the last profile or an empty profile + // Load the last selected printer profile or an empty profile ActiveSliceSettings.Instance = Instance.LoadLastProfile() ?? LoadEmptyProfile(); - if (Instance != null) - { - // Release event registration - Instance.Profiles.CollectionChanged -= Profiles_CollectionChanged; - } - // In either case, wire up the CollectionChanged event Instance.Profiles.CollectionChanged += Profiles_CollectionChanged; } + + internal static ProfileManager LoadGuestDB() + { + if (File.Exists(GuestDBPath)) + { + string json = File.ReadAllText(GuestDBPath); + return JsonConvert.DeserializeObject(json); + } + + return null; + } + internal static void SettingsChanged(object sender, EventArgs e) { string settingsKey = ((StringEventArgs)e).Data; @@ -119,7 +133,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration Instance.ActiveProfile.Name = ActiveSliceSettings.Instance.GetValue(SettingsKey.printer_name); Instance.Save(); break; - + case SettingsKey.com_port: Instance.ActiveProfile.ComPort = ActiveSliceSettings.Instance.ComPort(); Instance.Save(); @@ -161,6 +175,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration return new SettingsProfile(printerSettings); } + [JsonIgnore] public string LastProfileID { get @@ -172,6 +187,8 @@ namespace MatterHackers.MatterControl.SlicerConfiguration } } + public bool PrintersImported { get; set; } = false; + public SettingsProfile LoadLastProfile() { return LoadProfile(this.LastProfileID); @@ -185,6 +202,16 @@ namespace MatterHackers.MatterControl.SlicerConfiguration UserSettings.Instance.set(settingsKey, printerID); } + public string ProfilePath(PrinterInfo printer) + { + return Path.Combine(ProfileManager.ProfilesPath, printer.ID + ProfileExtension); + } + + public string ProfilePath(string printerID) + { + return Path.Combine(ProfileManager.ProfilesPath, printerID + ProfileExtension); + } + /// /// Loads the specified SettingsProfile /// @@ -193,9 +220,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration /// public static SettingsProfile LoadProfile(string profileID, bool useActiveInstance = true) { - //return LoadProfileFromMCWS(profileID); - - // Only load profiles by ID that are defined in the profiles.json document + // Only load profiles by ID that are defined in the profiles document if (ProfileManager.Instance[profileID] == null) { return null; @@ -358,6 +383,17 @@ namespace MatterHackers.MatterControl.SlicerConfiguration }); } + public void EnsurePrintersImported() + { + if (IsGuestProfile && !PrintersImported) + { + // Import Sqlite printer profiles into local json files + DataStorage.ClassicDB.ClassicSqlitePrinterProfiles.ImportPrinters(Instance, ProfilesPath); + PrintersImported = true; + Save(); + } + } + private static void Profiles_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { // Any time the list changes, persist the updates to disk diff --git a/SlicerConfiguration/Settings/SettingsProfile.cs b/SlicerConfiguration/Settings/SettingsProfile.cs index f69f26151..25aa1c488 100644 --- a/SlicerConfiguration/Settings/SettingsProfile.cs +++ b/SlicerConfiguration/Settings/SettingsProfile.cs @@ -966,6 +966,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration } [JsonIgnore] - public string ProfilePath => Path.Combine(ProfileManager.ProfilesPath, ID + ".json"); + public string ProfilePath => ProfileManager.Instance.ProfilePath(this); } } \ No newline at end of file