/* Copyright (c) 2014, Lars Brubaker All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. */ using MatterHackers.Agg.PlatformAbstract; using MatterHackers.MatterControl.DataStorage; using MatterHackers.MatterControl.SlicerConfiguration; using MatterHackers.MatterControl.VersionManagement; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Net; using System.Runtime.Serialization; using System.Threading; using System.Threading.Tasks; namespace MatterHackers.MatterControl.SettingsManagement { using Agg.UI; using OemProfileDictionary = Dictionary>; public class OemSettings { private static OemSettings instance = null; public static OemSettings Instance { get { if (instance == null) { string oemSettings = StaticData.Instance.ReadAllText(Path.Combine("OEMSettings", "Settings.json")); instance = JsonConvert.DeserializeObject(oemSettings) as OemSettings; } return instance; } } public bool UseSimpleModeByDefault = false; public string ThemeColor = ""; public string AffiliateCode = ""; public string WindowTitleExtra = ""; public bool ShowShopButton = true; public bool CheckForUpdatesOnFirstRun = false; public List PrinterWhiteList { get; private set; } = new List(); public List ManufacturerNameMappings { get; set; } public List PreloadedLibraryFiles { get; } = new List(); internal void SetManufacturers(IEnumerable> unorderedManufacturers, List whitelist = null) { // Sort manufacturers by name List> manufacturers = new List>(); KeyValuePair otherInfo = new KeyValuePair(null, null); foreach (var printer in unorderedManufacturers.OrderBy(k => k.Value)) { if (printer.Value == "Other") { otherInfo = printer; } else { manufacturers.Add(printer); } } if (otherInfo.Key != null) { // add it at the end manufacturers.Add(otherInfo); } if (whitelist != null) { this.PrinterWhiteList = whitelist; } // Apply whitelist var whiteListedItems = manufacturers?.Where(keyValue => PrinterWhiteList.Contains(keyValue.Key)); if (whiteListedItems == null || whiteListedItems.Count() == 0) { AllOems = new List>(manufacturers); return; } var newItems = new List>(); // Apply manufacturer name mappings foreach (var keyValue in whiteListedItems) { string labelText = keyValue.Value; // Override the manufacturer name if a manufacturerNameMappings exists string mappedName = ManufacturerNameMappings.Where(m => m.NameOnDisk == keyValue.Key).FirstOrDefault()?.NameOnDisk; if (!string.IsNullOrEmpty(mappedName)) { labelText = mappedName; } newItems.Add(new KeyValuePair(keyValue.Key, labelText)); } AllOems = newItems; } public List> AllOems { get; private set; } public OemProfileDictionary OemProfiles { get; set; } [OnDeserialized] private void Deserialized(StreamingContext context) { // Load from StaticData to prepopulate oemProfiles for when user create a printer before load cacheable is done OemProfiles = JsonConvert.DeserializeObject(StaticData.Instance.ReadAllText(Path.Combine("Profiles", "oemprofiles.json"))); var manufacturesList = OemProfiles.Keys.ToDictionary(oem => oem).ToList(); SetManufacturers(manufacturesList); } public async Task ReloadOemProfiles() { // In public builds this won't be assigned to and we should abort and exit early if (ApplicationController.GetPublicProfileList == null) { return; } var oemProfiles = await ApplicationController.LoadCacheableAsync( "oemprofiles.json", "profiles", ApplicationController.GetPublicProfileList); // If we failed to get anything from load cacheable don't override potentially populated fields if (oemProfiles != null) { OemProfiles = oemProfiles; var manufactures = oemProfiles.Keys.ToDictionary(oem => oem); SetManufacturers(manufactures); await DownloadMissingProfiles(); } } private async Task DownloadMissingProfiles() { string cacheDirectory = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "data", "temp", "cache", "profiles"); foreach (string oem in OemProfiles.Keys) { foreach (string profileKey in OemProfiles[oem].Values) { string cacheKey = profileKey + ProfileManager.ProfileExtension; string cachePath = Path.Combine(cacheDirectory, cacheKey); if (!File.Exists(cachePath)) { var profile = await ApplicationController.DownloadPublicProfileAsync(profileKey); string profileJson = JsonConvert.SerializeObject(profile); if (!string.IsNullOrEmpty(profileJson)) { File.WriteAllText(cachePath, profileJson); } } } } } private OemSettings() { this.ManufacturerNameMappings = new List(); } } public class ManufacturerNameMapping { public string NameOnDisk { get; set; } public string NameToDisplay { get; set; } } }