From 15dd27fb687bfe657469a5a54d87313bedd2c6e3 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 28 Mar 2023 07:25:10 +0300 Subject: [PATCH] Add Configuration Interface --- RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs | 10 ++- .../Controllers/RibbonController.cs | 7 +- RhSolutions.AddIn/Controllers/ToolBase.cs | 6 +- .../Services/AddInConfiguration.cs | 39 ++++++++++ RhSolutions.AddIn/Services/EventsUtil.cs | 8 ++ .../Services/IAddInConfiguration.cs | 13 ++++ RhSolutions.AddIn/Services/RegistryUtil.cs | 74 ------------------- 7 files changed, 74 insertions(+), 83 deletions(-) create mode 100644 RhSolutions.AddIn/Services/AddInConfiguration.cs create mode 100644 RhSolutions.AddIn/Services/IAddInConfiguration.cs delete mode 100644 RhSolutions.AddIn/Services/RegistryUtil.cs diff --git a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs index 4522d39..19ae437 100644 --- a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs +++ b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs @@ -3,6 +3,7 @@ using ExcelDna.IntelliSense; using Microsoft.Extensions.DependencyInjection; using Microsoft.Office.Interop.Excel; using RhSolutions.Services; +using System; using System.Net; namespace RhSolutions.AddIn @@ -11,6 +12,7 @@ namespace RhSolutions.AddIn { public static Application Excel { get; private set; } public static ServiceProvider ServiceProvider { get; set; } + public static IAddInConfiguration Configuration { get; set; } public void AutoOpen() { @@ -18,12 +20,13 @@ namespace RhSolutions.AddIn Excel = (Application)ExcelDnaUtil.Application; Services.AddHttpClient() - .AddSingleton(); + .AddSingleton() + .AddSingleton(); ServiceProvider = Services.BuildServiceProvider(); - + Configuration = ServiceProvider.GetService(); + IntelliSenseServer.Install(); - RegistryUtil.Initialize(); EventsUtil.Initialize(); ServicePointManager.SecurityProtocol = @@ -33,7 +36,6 @@ namespace RhSolutions.AddIn public void AutoClose() { IntelliSenseServer.Uninstall(); - RegistryUtil.Uninitialize(); EventsUtil.Uninitialize(); } } diff --git a/RhSolutions.AddIn/Controllers/RibbonController.cs b/RhSolutions.AddIn/Controllers/RibbonController.cs index ccfcf01..b95bb4f 100644 --- a/RhSolutions.AddIn/Controllers/RibbonController.cs +++ b/RhSolutions.AddIn/Controllers/RibbonController.cs @@ -1,4 +1,5 @@ using ExcelDna.Integration.CustomUI; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Office.Interop.Excel; using RhSolutions.AddIn; using RhSolutions.Services; @@ -51,13 +52,15 @@ namespace RhSolutions.Controllers ribbonUi.InvalidateControl(id); } } + public void OnSetPricePressed(IRibbonControl control) { string path = Models.Dialog.GetFilePath(); if (!string.IsNullOrEmpty(path)) { - RegistryUtil.PriceListPath = path; + RhSolutionsAddIn.Configuration.SetPriceListPath(path); + RhSolutionsAddIn.Configuration.SaveSettings(); } } @@ -131,7 +134,7 @@ namespace RhSolutions.Controllers public string GetPriceListPathLabel(IRibbonControl control) { - string name = RegistryUtil.GetPriceListName(); + string name = RhSolutionsAddIn.Configuration.GetPriceListFileName(); return string.IsNullOrEmpty(name) ? "Нет файла шаблона!" : name; } } diff --git a/RhSolutions.AddIn/Controllers/ToolBase.cs b/RhSolutions.AddIn/Controllers/ToolBase.cs index 08493d4..b54b45a 100644 --- a/RhSolutions.AddIn/Controllers/ToolBase.cs +++ b/RhSolutions.AddIn/Controllers/ToolBase.cs @@ -12,22 +12,22 @@ namespace RhSolutions.Controllers internal abstract class ToolBase { protected Application ExcelApp = RhSolutionsAddIn.Excel; + protected IAddInConfiguration Configuration = RhSolutionsAddIn.Configuration; protected TargetPriceList TargetFile { get; set; } protected ResultBar ResultBar { get; set; } protected ProgressBar ProgressBar { get; set; } - public abstract void FillTarget(); public void OpenNewPrice() { if (ExcelApp.Workbooks .Cast() - .FirstOrDefault(w => w.FullName == RegistryUtil.PriceListPath) != null) + .FirstOrDefault(w => w.FullName == Configuration.GetPriceListPath()) != null) { throw new ArgumentException("Шаблонный файл редактируется в другом месте"); } - Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath, null, true); + Workbook wb = ExcelApp.Workbooks.Open(Configuration.GetPriceListPath(), null, true); try { diff --git a/RhSolutions.AddIn/Services/AddInConfiguration.cs b/RhSolutions.AddIn/Services/AddInConfiguration.cs new file mode 100644 index 0000000..747e545 --- /dev/null +++ b/RhSolutions.AddIn/Services/AddInConfiguration.cs @@ -0,0 +1,39 @@ +using System; +using System.Configuration; +using System.IO; + +namespace RhSolutions.Services +{ + public class AddInConfiguration : ApplicationSettingsBase, IAddInConfiguration + { + [UserScopedSetting] + public string PriceListPath + { + get + { + return (string)this["PriceListPath"]; + } + set + { + this["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(); + } +} diff --git a/RhSolutions.AddIn/Services/EventsUtil.cs b/RhSolutions.AddIn/Services/EventsUtil.cs index bb37125..7d96d23 100644 --- a/RhSolutions.AddIn/Services/EventsUtil.cs +++ b/RhSolutions.AddIn/Services/EventsUtil.cs @@ -1,6 +1,7 @@ using Microsoft.Office.Interop.Excel; using RhSolutions.AddIn; using RhSolutions.Controllers; +using System.Configuration; namespace RhSolutions.Services { @@ -13,6 +14,7 @@ namespace RhSolutions.Services Excel.SheetSelectionChange += RefreshExportButton; Excel.SheetActivate += RefreshConvertButton; Excel.WorkbookActivate += RefreshConvertButton; + RhSolutionsAddIn.Configuration.OnSettingsChange += RefreshSettingTitle; } public static void Uninitialize() @@ -20,6 +22,7 @@ namespace RhSolutions.Services Excel.SheetSelectionChange -= RefreshExportButton; Excel.SheetActivate -= RefreshConvertButton; Excel.WorkbookActivate -= RefreshConvertButton; + RhSolutionsAddIn.Configuration.OnSettingsChange -= RefreshSettingTitle; } private static void RefreshConvertButton(object sh) @@ -31,5 +34,10 @@ namespace RhSolutions.Services { RibbonController.RefreshControl("export"); } + + private static void RefreshSettingTitle(object sender, SettingChangingEventArgs e) + { + RibbonController.RefreshControl("setPriceList"); + } } } diff --git a/RhSolutions.AddIn/Services/IAddInConfiguration.cs b/RhSolutions.AddIn/Services/IAddInConfiguration.cs new file mode 100644 index 0000000..c6bed6a --- /dev/null +++ b/RhSolutions.AddIn/Services/IAddInConfiguration.cs @@ -0,0 +1,13 @@ +using System.Configuration; + +namespace RhSolutions.Services +{ + public interface IAddInConfiguration + { + public string GetPriceListPath(); + public string GetPriceListFileName(); + public event SettingChangingEventHandler OnSettingsChange; + public void SetPriceListPath(string value); + public void SaveSettings(); + } +} \ No newline at end of file diff --git a/RhSolutions.AddIn/Services/RegistryUtil.cs b/RhSolutions.AddIn/Services/RegistryUtil.cs deleted file mode 100644 index 8b165ea..0000000 --- a/RhSolutions.AddIn/Services/RegistryUtil.cs +++ /dev/null @@ -1,74 +0,0 @@ -using Microsoft.Win32; -using RhSolutions.Controllers; -using RhSolutions.Models; -using System; -using System.IO; -using System.Windows.Forms; - -namespace RhSolutions.Services -{ - static class RegistryUtil - { - private static string priceListPath; - private static RegistryKey RootKey { get; set; } - - public static void Initialize() - { - RootKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\REHAU\SkuAssist"); - priceListPath = RootKey.GetValue("PriceListPath") as string; - } - - public static void Uninitialize() - { - RootKey.Close(); - } - - public static string PriceListPath - { - get - { - if (string.IsNullOrEmpty(priceListPath) || !File.Exists(priceListPath)) - { - DialogResult result = MessageBox.Show("Прайс-лист отсутствует или неверный файл шаблона прайс-листа. " + - "Укажите файл шаблона прайс-листа.", - "Нет файла шаблона", - MessageBoxButtons.OK, MessageBoxIcon.Warning); - - if (result == DialogResult.OK) - { - string fileName = Dialog.GetFilePath(); - - if (string.IsNullOrEmpty(fileName)) - { - throw new Exception("Нет файла шаблона"); - } - - priceListPath = fileName; - RootKey.SetValue("PriceListPath", fileName); - return priceListPath; - } - - else - throw new Exception("Нет файла шаблона"); - } - - else - { - return priceListPath; - } - } - - set - { - priceListPath = value; - RootKey.SetValue("PriceListPath", value); - RibbonController.RefreshControl("setPriceList"); - } - } - - public static string GetPriceListName() - { - return Path.GetFileName(priceListPath); - } - } -}