From ca9bda7a570a3e4671a0a61bc387e8498a967479 Mon Sep 17 00:00:00 2001 From: Alex Z Date: Mon, 16 Mar 2020 01:11:09 +0300 Subject: [PATCH] defautl setttings generating fix --- .../Model/Dto/SettingsDtoModel.cs | 1 - .../Model/Provider/DataProvider.cs | 64 ++++++++++++++++--- .../UI/FormSettings.Designer.cs | 36 ++--------- .../Utils/JsonFileUtil.cs | 22 ++++++- 4 files changed, 79 insertions(+), 44 deletions(-) diff --git a/src/UniversalValveToolbox/Model/Dto/SettingsDtoModel.cs b/src/UniversalValveToolbox/Model/Dto/SettingsDtoModel.cs index 0bbd269..d2c9305 100644 --- a/src/UniversalValveToolbox/Model/Dto/SettingsDtoModel.cs +++ b/src/UniversalValveToolbox/Model/Dto/SettingsDtoModel.cs @@ -5,7 +5,6 @@ namespace UniversalValveToolbox.Model.Dto { private string lastSelectedProject; private string[] availableLanguages; private string language; - private string theme; public string LastSelectedProject { get => lastSelectedProject; diff --git a/src/UniversalValveToolbox/Model/Provider/DataProvider.cs b/src/UniversalValveToolbox/Model/Provider/DataProvider.cs index 47ef6ee..9498265 100644 --- a/src/UniversalValveToolbox/Model/Provider/DataProvider.cs +++ b/src/UniversalValveToolbox/Model/Provider/DataProvider.cs @@ -1,6 +1,8 @@ using System; +using System.Collections; using System.Collections.Generic; using System.ComponentModel; +using System.Globalization; using System.IO; using System.Runtime.CompilerServices; using System.Windows.Forms; @@ -11,27 +13,71 @@ using UniversalValveToolbox.Utils; namespace UniversalValveToolbox.Model.Provider { class DataProvider { - public static readonly string SettingsPath = Path.Combine(Application.StartupPath, "json", "settings.json"); - public static readonly string EnginesPath = Path.Combine(Application.StartupPath, "json", "engines"); - public static readonly string ProjectsPath = Path.Combine(Application.StartupPath, "json", "projects"); - public static readonly string AddonsPath = Path.Combine(Application.StartupPath, "json", "addons"); + public static readonly string JsonRootPath = Path.Combine(Application.StartupPath, "json"); + public static readonly string SettingsPath = Path.Combine(JsonRootPath, "settings.json"); + public static readonly string EnginesPath = Path.Combine(JsonRootPath, "engines"); + public static readonly string ProjectsPath = Path.Combine(JsonRootPath, "projects"); + public static readonly string AddonsPath = Path.Combine(JsonRootPath, "addons"); + + public static readonly SettingsDtoModel DefaultSettings = GenerateDefaultSettings(); + public SettingsDtoModel Settings { - get => JsonFileUtil.ReadValue(SettingsPath); - set => JsonFileUtil.WriteValue(SettingsPath, value); + get { + validateJsonFolder(); + + return JsonFileUtil.ReadValue(SettingsPath, DefaultSettings); + } + + set { + validateJsonFolder(); + + JsonFileUtil.WriteValue(SettingsPath, value); + } } public EngineDtoModel[] Engines { - get => JsonFileUtil.ReadValues(EnginesPath); + get { + validateJsonFolder(); + return JsonFileUtil.ReadValues(EnginesPath); + } } public AddonDtoModel[] Addons { - get => JsonFileUtil.ReadValues(AddonsPath); + get { + validateJsonFolder(); + return JsonFileUtil.ReadValues(AddonsPath); + } } public ProjectDtoModel[] Projects { - get => JsonFileUtil.ReadValues(ProjectsPath); + get { + validateJsonFolder(); + return JsonFileUtil.ReadValues(ProjectsPath); + } + } + + private void validateJsonFolder() { + if (!Directory.Exists(JsonRootPath)) { + Directory.CreateDirectory(JsonRootPath); + } + } + + private static SettingsDtoModel GenerateDefaultSettings() { + var result = new SettingsDtoModel(); + result.Language = "en-US"; //may be fix in future + + var listLang = new List(); + var resourceSet = Properties.translations.LangDict.ResourceManager.GetResourceSet(new CultureInfo("en-US"), true, true); + foreach (DictionaryEntry entry in resourceSet) { + listLang.Add(entry.Key.ToString()); + } + + result.AvailableLanguages = listLang.ToArray(); + + return result; + } } } diff --git a/src/UniversalValveToolbox/UI/FormSettings.Designer.cs b/src/UniversalValveToolbox/UI/FormSettings.Designer.cs index 09cb91f..2447a3b 100644 --- a/src/UniversalValveToolbox/UI/FormSettings.Designer.cs +++ b/src/UniversalValveToolbox/UI/FormSettings.Designer.cs @@ -29,8 +29,6 @@ this.buttonOK = new System.Windows.Forms.Button(); this.label5 = new System.Windows.Forms.Label(); this.comboBoxLang = new System.Windows.Forms.ComboBox(); - this.comboBoxTheme = new System.Windows.Forms.ComboBox(); - this.label6 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // labelDivider @@ -38,7 +36,7 @@ this.labelDivider.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.labelDivider.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; - this.labelDivider.Location = new System.Drawing.Point(-21, 272); + this.labelDivider.Location = new System.Drawing.Point(-21, 114); this.labelDivider.Name = "labelDivider"; this.labelDivider.Size = new System.Drawing.Size(385, 2); this.labelDivider.TabIndex = 26; @@ -46,7 +44,7 @@ // buttonCancel // this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonCancel.Location = new System.Drawing.Point(224, 284); + this.buttonCancel.Location = new System.Drawing.Point(224, 126); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(75, 23); this.buttonCancel.TabIndex = 25; @@ -58,7 +56,7 @@ // this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK; - this.buttonOK.Location = new System.Drawing.Point(143, 284); + this.buttonOK.Location = new System.Drawing.Point(143, 126); this.buttonOK.Name = "buttonOK"; this.buttonOK.Size = new System.Drawing.Size(75, 23); this.buttonOK.TabIndex = 24; @@ -87,35 +85,11 @@ this.comboBoxLang.Size = new System.Drawing.Size(287, 21); this.comboBoxLang.TabIndex = 30; // - // comboBoxTheme - // - this.comboBoxTheme.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.comboBoxTheme.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.comboBoxTheme.FormattingEnabled = true; - this.comboBoxTheme.Items.AddRange(new object[] { - "None"}); - this.comboBoxTheme.Location = new System.Drawing.Point(12, 65); - this.comboBoxTheme.Name = "comboBoxTheme"; - this.comboBoxTheme.Size = new System.Drawing.Size(287, 21); - this.comboBoxTheme.TabIndex = 32; - // - // label6 - // - this.label6.AutoSize = true; - this.label6.Location = new System.Drawing.Point(12, 49); - this.label6.Name = "label6"; - this.label6.Size = new System.Drawing.Size(43, 13); - this.label6.TabIndex = 31; - this.label6.Text = "Theme:"; - // // FormSettings // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(311, 319); - this.Controls.Add(this.comboBoxTheme); - this.Controls.Add(this.label6); + this.ClientSize = new System.Drawing.Size(311, 161); this.Controls.Add(this.comboBoxLang); this.Controls.Add(this.label5); this.Controls.Add(this.labelDivider); @@ -141,7 +115,5 @@ private System.Windows.Forms.Button buttonOK; private System.Windows.Forms.Label label5; private System.Windows.Forms.ComboBox comboBoxLang; - private System.Windows.Forms.ComboBox comboBoxTheme; - private System.Windows.Forms.Label label6; } } \ No newline at end of file diff --git a/src/UniversalValveToolbox/Utils/JsonFileUtil.cs b/src/UniversalValveToolbox/Utils/JsonFileUtil.cs index 8946311..d870250 100644 --- a/src/UniversalValveToolbox/Utils/JsonFileUtil.cs +++ b/src/UniversalValveToolbox/Utils/JsonFileUtil.cs @@ -8,14 +8,32 @@ using System.Threading.Tasks; namespace UniversalValveToolbox.Utils { static class JsonFileUtil { - public static T ReadValue(string path) => JsonConvert.DeserializeObject(File.ReadAllText(path)); + public static T ReadValue(string path) { + try { + var result = JsonConvert.DeserializeObject(File.ReadAllText(path)); + return result; + } catch (Exception) { + return default(T); + } + } + + public static T ReadValue(string path, T fileDefaultValue) { + try { + var result = JsonConvert.DeserializeObject(File.ReadAllText(path)); + return result; + } catch (Exception) { + WriteValue(path, fileDefaultValue); + + return fileDefaultValue; + } + } public static T[] ReadValues(string directoryPath) { if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } - return Directory.GetFiles(directoryPath, "*").Select(path => ReadValue(path)).ToArray(); + return Directory.GetFiles(directoryPath, "*").Select(path => ReadValue(path)).Where(value => value != null).ToArray(); } public static List ReadListValues(string directoryPath) => new List(ReadValues(directoryPath));