public void SetValue(string settingsKey, string settingsValue, PrinterSettingsLayer layer = null) { // Stash user overrides if a non-user override is being set if (layer != null && layer != UserLayer) { StashUserOverride(layer, settingsKey); } else { // Remove any staged/conflicting user override, making this the new and active user override if (StagedUserSettings.ContainsKey(settingsKey)) { StagedUserSettings.Remove(settingsKey); } } var persistenceLayer = layer ?? UserLayer; // If the setting exists and is set to the requested value, exit without setting or saving string existingValue; if (persistenceLayer.TryGetValue(settingsKey, out existingValue) && existingValue == settingsValue) { return; } // Otherwise, set and save persistenceLayer[settingsKey] = settingsValue; Save(); ActiveSliceSettings.OnSettingChanged(settingsKey); }
private void MenuItem_Selected(object sender, EventArgs e) { // When a preset is selected store the current values of all known settings to compare against after applying the preset Dictionary <string, string> settingBeforeChange = new Dictionary <string, string>(); foreach (var keyName in PrinterSettings.KnownSettings) { settingBeforeChange.Add(keyName, printer.Settings.GetValue(keyName)); } var activeSettings = printer.Settings; MenuItem item = (MenuItem)sender; if (layerType == NamedSettingsLayers.Material) { if (activeSettings.ActiveMaterialKey != item.Value) { // Restore deactivated user overrides by iterating the Material preset we're coming off of activeSettings.RestoreConflictingUserOverrides(activeSettings.MaterialLayer); activeSettings.ActiveMaterialKey = item.Value; // Deactivate conflicting user overrides by iterating the Material preset we've just switched to activeSettings.DeactivateConflictingUserOverrides(activeSettings.MaterialLayer); } } else if (layerType == NamedSettingsLayers.Quality) { if (activeSettings.ActiveQualityKey != item.Value) { // Restore deactivated user overrides by iterating the Quality preset we're coming off of activeSettings.RestoreConflictingUserOverrides(activeSettings.QualityLayer); activeSettings.ActiveQualityKey = item.Value; // Deactivate conflicting user overrides by iterating the Quality preset we've just switched to activeSettings.DeactivateConflictingUserOverrides(activeSettings.QualityLayer); } } // Ensure that activated or deactivated user overrides are always persisted to disk activeSettings.Save(); UiThread.RunOnIdle(() => { foreach (var keyName in PrinterSettings.KnownSettings) { if (settingBeforeChange[keyName] != printer.Settings.GetValue(keyName)) { ActiveSliceSettings.OnSettingChanged(keyName); } } }); editButton.Enabled = item.Text != defaultMenuItemText; }
private void MenuItem_Selected(object sender, EventArgs e) { Dictionary <string, string> settingBeforeChange = new Dictionary <string, string>(); foreach (var keyName in PrinterSettings.KnownSettings) { settingBeforeChange.Add(keyName, ActiveSliceSettings.Instance.GetValue(keyName)); } var activeSettings = ActiveSliceSettings.Instance; MenuItem item = (MenuItem)sender; if (layerType == NamedSettingsLayers.Material) { if (activeSettings.GetMaterialPresetKey(extruderIndex) != item.Value) { // Restore deactivated user overrides by iterating the Material preset we're coming off of activeSettings.RestoreConflictingUserOverrides(activeSettings.MaterialLayer); activeSettings.SetMaterialPreset(extruderIndex, item.Value); // Deactivate conflicting user overrides by iterating the Material preset we've just switched to activeSettings.DeactivateConflictingUserOverrides(activeSettings.MaterialLayer); } } else if (layerType == NamedSettingsLayers.Quality) { if (activeSettings.ActiveQualityKey != item.Value) { // Restore deactivated user overrides by iterating the Quality preset we're coming off of activeSettings.RestoreConflictingUserOverrides(activeSettings.QualityLayer); activeSettings.ActiveQualityKey = item.Value; // Deactivate conflicting user overrides by iterating the Quality preset we've just switched to activeSettings.DeactivateConflictingUserOverrides(activeSettings.QualityLayer); } } // Ensure that activated or deactivated user overrides are always persisted to disk activeSettings.Save(); UiThread.RunOnIdle(() => { ApplicationController.Instance.ReloadAdvancedControlsPanel(); foreach (var keyName in PrinterSettings.KnownSettings) { if (settingBeforeChange[keyName] != ActiveSliceSettings.Instance.GetValue(keyName)) { ActiveSliceSettings.OnSettingChanged(keyName); } } }); editButton.Enabled = item.Text != defaultMenuItemText; }
public async static Task <PrinterSettings> RecoverProfile(PrinterInfo printerInfo) { bool userIsLoggedIn = !ShouldShowAuthPanel?.Invoke() ?? false; if (userIsLoggedIn && printerInfo != null) { // Attempt to load from MCWS history var printerSettings = await GetFirstValidHistoryItem(printerInfo); if (printerSettings == null) { // Fall back to OemProfile defaults if load from history fails printerSettings = RestoreFromOemProfile(printerInfo); } if (printerSettings == null) { // If we still have failed to recover a profile, create an empty profile with // just enough data to delete the printer printerSettings = ProfileManager.LoadEmptyProfile(); printerSettings.ID = printerInfo.ID; printerSettings.UserLayer[SettingsKey.device_token] = printerInfo.DeviceToken; printerSettings.Helpers.SetComPort(printerInfo.ComPort); printerSettings.SetValue(SettingsKey.printer_name, printerInfo.Name); // Add any setting value to the OemLayer to pass the .PrinterSelected property printerSettings.OemLayer = new PrinterSettingsLayer(); printerSettings.OemLayer.Add("empty", "setting"); printerSettings.Save(); } if (printerSettings != null) { // Persist any profile recovered above as the current printerSettings.Save(); // Update active instance without calling ReloadAll ActiveSliceSettings.RefreshActiveInstance(printerSettings); WarnAboutRevert(printerInfo); } return(printerSettings); } return(null); }
internal void ClearValue(string settingsKey, PrinterSettingsLayer layer = null) { var persistenceLayer = layer ?? UserLayer; if (persistenceLayer.ContainsKey(settingsKey)) { persistenceLayer.Remove(settingsKey); // Restore user overrides if a non-user override is being cleared if (layer != null && layer != UserLayer) { RestoreUserOverride(layer, settingsKey); } Save(); ActiveSliceSettings.OnSettingChanged(settingsKey); } }
static ActiveSliceSettings() { // Ensure the profiles directory exists Directory.CreateDirectory(profilesPath); if (true) { ProfileData = new ProfileData(); if (!File.Exists(profilesDBPath)) { // Import class profiles from the db into local json files DataStorage.ClassicDB.ClassicSqlitePrinterProfiles.ImportPrinters(ProfileData, profilesPath); File.WriteAllText(profilesDBPath, JsonConvert.SerializeObject(ProfileData, Formatting.Indented)); // TODO: Upload new profiles to webservice } ProfileData.Profiles.CollectionChanged += Profiles_CollectionChanged; 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(LayeredProfile.LoadFile(filePath)); ProfileData.Profiles.Add(new PrinterInfo() { AutoConnect = profile.DoAutoConnect(), BaudRate = profile.BaudRate(), ComPort = profile.ComPort(), DriverType = profile.DriverType(), Id = profile.ID, Make = profile.Make, Model = profile.Model, Name = profile.Name(), }); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Error loading profile: {1}\r\n{2}", filePath, ex.Message); } } } else { // Load or import the profiles.json document if (File.Exists(profilesDBPath)) { ProfileData = JsonConvert.DeserializeObject <ProfileData>(File.ReadAllText(profilesDBPath)); } else { ProfileData = new ProfileData(); // Import class profiles from the db into local json files DataStorage.ClassicDB.ClassicSqlitePrinterProfiles.ImportPrinters(ProfileData, profilesPath); File.WriteAllText(profilesDBPath, JsonConvert.SerializeObject(ProfileData, Formatting.Indented)); // TODO: Upload new profiles to webservice } } ActiveSliceSettings.LoadStartupProfile(); }