// // OptionProvider.cs // // Author: // Mike Becker // // Copyright (c) 2019 Mike Becker // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . using System; using MBS.Framework.Settings; namespace MBS.Framework { public abstract class SettingsProvider { public class SettingsProviderCollection : System.Collections.ObjectModel.Collection { private System.Collections.Generic.Dictionary _itemsByID = new System.Collections.Generic.Dictionary(); public bool Contains(Guid id) { return _itemsByID.ContainsKey(id); } protected override void ClearItems() { base.ClearItems(); _itemsByID.Clear(); } protected override void InsertItem(int index, SettingsProvider item) { _itemsByID[item.ID] = item; base.InsertItem(index, item); } protected override void RemoveItem(int index) { _itemsByID.Remove(this[index].ID); base.RemoveItem(index); } } public Guid ID { get; set; } = Guid.Empty; public SettingsGroup.SettingsGroupCollection SettingsGroups { get; } = new SettingsGroup.SettingsGroupCollection(); public Setting FindSetting(string name) { foreach (SettingsGroup sg in SettingsGroups) { foreach (Setting s in sg.Settings) { if (s is GroupSetting) { Setting r = (s as GroupSetting).FindSetting(name); if (r != null) return r; } else { if (s.Name == name) return s; } } } return null; } protected virtual void InitializeInternal() { } public void Initialize() { InitializeInternal(); } protected virtual void LoadSettingsInternal() { } public void LoadSettings() { LoadSettingsInternal (); } protected virtual void SaveSettingsInternal() { } public void SaveSettings() { SaveSettingsInternal (); } public int Count { get { int count = 0; for (int i = 0; i < SettingsGroups.Count; i++) { count += SettingsGroups[i].Count; } return count; } } } }