framework-legacy/MBS.Framework/SettingsProvider.cs

159 lines
3.6 KiB
C#
Executable File

//
// OptionProvider.cs
//
// Author:
// Mike Becker <alcexhim@gmail.com>
//
// 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 <http://www.gnu.org/licenses/>.
using System;
using MBS.Framework.Settings;
namespace MBS.Framework
{
public abstract class SettingsProvider
{
public class SettingsProviderCollection
: System.Collections.ObjectModel.Collection<SettingsProvider>
{
private System.Collections.Generic.Dictionary<Guid, SettingsProvider> _itemsByID = new System.Collections.Generic.Dictionary<Guid, SettingsProvider>();
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;
}
public Setting FindSetting(Guid id)
{
foreach (SettingsGroup sg in SettingsGroups)
{
foreach (Setting s in sg.Settings)
{
if (s is GroupSetting)
{
Setting r = (s as GroupSetting).FindSetting(id);
if (r != null) return r;
}
else
{
if (s.ID == id)
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;
}
}
public void CopyTo(SettingsProvider other)
{
foreach (SettingsGroup group in SettingsGroups)
{
other.SettingsGroups.Add(group.Clone() as SettingsGroup);
}
}
public SettingsProvider Clone(string settingsGroupsPrefix)
{
CustomSettingsProvider prov = new CustomSettingsProvider();
foreach (SettingsGroup sg in this.SettingsGroups)
{
SettingsGroup sg2 = sg.Clone() as SettingsGroup;
string[] szpath = new string[sg.Path.Length + 1];
Array.Copy(sg.Path, 0, szpath, 1, sg.Path.Length);
szpath[0] = settingsGroupsPrefix;
sg2.Path = szpath;
prov.SettingsGroups.Add(sg2);
}
return prov;
}
}
}