From e6546254baf8130638aa0dee12f247769da4e308 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Sun, 12 Nov 2023 16:31:26 +0300 Subject: [PATCH] Save Configuration to registry --- .../Services/AddInConfiguration.cs | 131 ++++-------------- .../Services/IAddInConfiguration.cs | 9 +- RhSolutions.AddIn/Tools/EventsUtil.cs | 74 +++++----- 3 files changed, 69 insertions(+), 145 deletions(-) diff --git a/RhSolutions.AddIn/Services/AddInConfiguration.cs b/RhSolutions.AddIn/Services/AddInConfiguration.cs index 363c3f3..73b9a5b 100644 --- a/RhSolutions.AddIn/Services/AddInConfiguration.cs +++ b/RhSolutions.AddIn/Services/AddInConfiguration.cs @@ -1,114 +1,43 @@ -using System.Configuration; +using Microsoft.Win32; using System.IO; namespace RhSolutions.Services; -public class AddInConfiguration : ApplicationSettingsBase, IAddInConfiguration +public class AddInConfiguration : IAddInConfiguration { - private readonly Dictionary _priceListHeaders; + private RegistryKey _rootKey; + private string _priceListPath; + private Dictionary _priceListHeaders; + + public event IAddInConfiguration.SettingsHandler OnSettingsChange; public AddInConfiguration() { - _priceListHeaders = new Dictionary() + _rootKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\RhSolutions\RhSolutions-AddIn"); + _priceListPath = (string)_rootKey.GetValue("PriceListPath"); + _priceListHeaders = new() { - ["Amount"] = AmountHeader, - ["OldSku"] = OldSkuHeader, - ["Sku"] = SkuHeader, - ["ProductLine"] = ProductLineHeader, - ["Name"] = NameHeader, - ["Measure"] = MeasureHeader + ["Amount"] = "Кол-во", + ["OldSku"] = "Прежний материал", + ["Sku"] = "Актуальный материал", + ["ProductLine"] = "Программа", + ["Name"] = "Наименование", + ["Measure"] = "Ед. изм." }; } - [UserScopedSetting] - [DefaultSettingValue("Кол-во")] - public string AmountHeader - { - get - { - return (string)this[nameof(AmountHeader)]; - } - } - - [UserScopedSetting] - [DefaultSettingValue("Прежний материал")] - public string OldSkuHeader - { - get - { - return (string)this[nameof(OldSkuHeader)]; - } - } - - - [UserScopedSetting] - [DefaultSettingValue("Актуальный материал")] - public string SkuHeader - { - get - { - return (string)this[nameof(SkuHeader)]; - } - } - - [UserScopedSetting] - [DefaultSettingValue("Программа")] - public string ProductLineHeader - { - get - { - return (string)this[nameof(ProductLineHeader)]; - } - } - - [UserScopedSetting] - [DefaultSettingValue("Наименование")] - public string NameHeader - { - get - { - return (string)this[nameof(NameHeader)]; - } - } - - [UserScopedSetting] - [DefaultSettingValue("Ед. изм.")] - public string MeasureHeader - { - get - { - return (string)this[nameof(MeasureHeader)]; - } - } - - [UserScopedSetting] - public string PriceListPath - { - get - { - return (string)this[nameof(PriceListPath)]; - } - set - { - this[nameof(PriceListPath)] = value; - } - } - - public event SettingChangingEventHandler OnSettingsChange - { - add - { - base.SettingChanging += value; - } - remove - { - base.SettingChanging -= value; - } - } - - public string GetPriceListFileName() => Path.GetFileName(PriceListPath); - public string GetPriceListPath() => PriceListPath; - public void SetPriceListPath(string value) => PriceListPath = value; - public void SaveSettings() => base.Save(); + public string GetPriceListFileName() => Path.GetFileName(_priceListPath); public Dictionary GetPriceListHeaders() => _priceListHeaders; -} + public string GetPriceListPath() => _priceListPath; + + public void SaveSettings() + { + _rootKey.SetValue("PriceListPath", _priceListPath); + OnSettingsChange.Invoke(); + } + + public void SetPriceListPath(string value) + { + _priceListPath = value; + } +} \ No newline at end of file diff --git a/RhSolutions.AddIn/Services/IAddInConfiguration.cs b/RhSolutions.AddIn/Services/IAddInConfiguration.cs index 4eb79f1..e19da3d 100644 --- a/RhSolutions.AddIn/Services/IAddInConfiguration.cs +++ b/RhSolutions.AddIn/Services/IAddInConfiguration.cs @@ -1,13 +1,12 @@ -using System.Configuration; - -namespace RhSolutions.Services; +namespace RhSolutions.Services; public interface IAddInConfiguration { public string GetPriceListPath(); + public void SetPriceListPath(string value); public string GetPriceListFileName(); public Dictionary GetPriceListHeaders(); - public event SettingChangingEventHandler OnSettingsChange; - public void SetPriceListPath(string value); + public delegate void SettingsHandler(); + public event SettingsHandler OnSettingsChange; public void SaveSettings(); } \ No newline at end of file diff --git a/RhSolutions.AddIn/Tools/EventsUtil.cs b/RhSolutions.AddIn/Tools/EventsUtil.cs index e62de23..6723835 100644 --- a/RhSolutions.AddIn/Tools/EventsUtil.cs +++ b/RhSolutions.AddIn/Tools/EventsUtil.cs @@ -1,52 +1,48 @@ -using Microsoft.Office.Interop.Excel; -using RhSolutions.AddIn; -using RhSolutions.Controllers; -using System.Configuration; +using RhSolutions.Controllers; #if !NET472 using System.Runtime.Versioning; #endif -namespace RhSolutions.Tools -{ +namespace RhSolutions.Tools; + #if !NET472 - [SupportedOSPlatform("windows")] +[SupportedOSPlatform("windows")] #endif - internal static class EventsUtil +internal static class EventsUtil +{ + public static void Initialize() { - public static void Initialize() - { - RhSolutionsAddIn.Excel.SheetSelectionChange += RefreshExportButton; - RhSolutionsAddIn.Excel.SheetActivate += RefreshButtons; - RhSolutionsAddIn.Excel.WorkbookActivate += RefreshButtons; - RhSolutionsAddIn.Configuration.OnSettingsChange += RefreshSettingTitle; - } + RhSolutionsAddIn.Excel.SheetSelectionChange += RefreshExportButton; + RhSolutionsAddIn.Excel.SheetActivate += RefreshButtons; + RhSolutionsAddIn.Excel.WorkbookActivate += RefreshButtons; + RhSolutionsAddIn.Configuration.OnSettingsChange += RefreshSettingTitle; + } - public static void Uninitialize() - { - RhSolutionsAddIn.Excel.SheetSelectionChange -= RefreshExportButton; - RhSolutionsAddIn.Excel.SheetActivate -= RefreshButtons; - RhSolutionsAddIn.Excel.WorkbookActivate -= RefreshButtons; - RhSolutionsAddIn.Configuration.OnSettingsChange -= RefreshSettingTitle; - } + public static void Uninitialize() + { + RhSolutionsAddIn.Excel.SheetSelectionChange -= RefreshExportButton; + RhSolutionsAddIn.Excel.SheetActivate -= RefreshButtons; + RhSolutionsAddIn.Excel.WorkbookActivate -= RefreshButtons; + RhSolutionsAddIn.Configuration.OnSettingsChange -= RefreshSettingTitle; + } - private static void RefreshButtons(object sh) - { - RibbonController.UpdateWorkbookValidation(); - RibbonController.RefreshControl("convert"); - RibbonController.RefreshControl("dxfexport"); - RibbonController.RefreshControl("guess"); - RibbonController.RefreshControl("fillsleeves"); - RibbonController.RefreshControl("fillcouplings"); - } + private static void RefreshButtons(object sh) + { + RibbonController.UpdateWorkbookValidation(); + RibbonController.RefreshControl("convert"); + RibbonController.RefreshControl("dxfexport"); + RibbonController.RefreshControl("guess"); + RibbonController.RefreshControl("fillsleeves"); + RibbonController.RefreshControl("fillcouplings"); + } - private static void RefreshExportButton(object sh, Range target) - { - RibbonController.RefreshControl("export"); - } + private static void RefreshExportButton(object sh, Range target) + { + RibbonController.RefreshControl("export"); + } - private static void RefreshSettingTitle(object sender, SettingChangingEventArgs e) - { - RibbonController.RefreshControl("setPriceList"); - } + private static void RefreshSettingTitle() + { + RibbonController.RefreshControl("setPriceList"); } }