You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
6.8 KiB

1 year ago
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using CK.SCP.Utils;
using DevComponents.DotNetBar.Controls;
namespace CK.SCP.Common
{
public static class SettingHelper
{
public static T SetConfigValues<T>(List<GroupPanel> groupList, string filename) where T : new()
{
var t = new T();
var properties = t.GetType().GetProperties();
foreach (var group in groupList)
{
if (group.GetType() != typeof(GroupPanel)) continue;
if (group.Name != GetPureName(filename)) continue;
foreach (var ctrl in group.Controls)
{
if (ctrl.GetType() == typeof(CktTextBox))
{
var textbox = (CktTextBox)ctrl;
foreach (var pi in properties)
{
var piName = pi.Name;
if (textbox.Name != piName) continue;
if (string.IsNullOrEmpty(textbox.Text.Trim()))
{
throw new Exception($"{piName} 不能为空");
}
var value = ListHelper.ConvertToType(textbox.Text, pi.PropertyType);
if (textbox.UserSystemPasswordChar)
value = EncryptHelper.Encrypt(textbox.Text);
if (!pi.CanWrite) continue;
pi.SetValue(t, value, null);
break;
}
}
if (ctrl.GetType() == typeof(CktSwitchButton))
{
var switchButton = (CktSwitchButton)ctrl;
foreach (var pi in properties)
{
var piName = pi.Name;
if (switchButton.Name != piName) continue;
var value = ListHelper.ConvertToType(switchButton.Value, pi.PropertyType);
if (!pi.CanWrite) continue;
pi.SetValue(t, value, null);
break;
}
}
}
break;
}
JsonHelper.WriteConfigToFile(filename, t);
return t;
}
public static GroupPanel GetConfigValues<T>(string filename) where T : new()
{
var t = FileHelper.Exists(filename)
? JsonHelper.ReadConfigFromFile<T>(filename)
: new T();
var properties = t.GetType().GetProperties();
var group = CreateGroupPanel(filename, properties.Length);
foreach (var pi in properties)
{
var piValue = pi.GetValue(t, null);
var piName = pi.Name;
if (pi.PropertyType == typeof(Boolean))
{
var switchButton = CreateSwitchButton<T>(piName);
switchButton.Value = Convert.ToBoolean(piValue);
@group.Controls.Add(switchButton);
switchButton.BringToFront();
}
else
{
var textbox = CreateTextbox<T>(piName);
if (piName.Contains("密码"))
{
textbox.UserSystemPasswordChar = true;
textbox.Text = piValue == null ? "" : EncryptHelper.Decrypt(piValue.ToString());
}
else
{
textbox.Text = piValue?.ToString() ?? "";
}
@group.Controls.Add(textbox);
textbox.BringToFront();
}
}
return group;
}
private static CktTextBox CreateTextbox<T>(string piName) where T : new()
{
return new CktTextBox()
{
LblText = piName,
LblTextAlign = StringAlignment.Far,
LblWidth = 150,
Dock = DockStyle.Top,
Name = piName,
};
}
private static CktSwitchButton CreateSwitchButton<T>(string piName) where T : new()
{
return new CktSwitchButton()
{
LblText = piName,
LblTextAlign = StringAlignment.Far,
LblWidth = 100,
Dock = DockStyle.Top,
Name = piName,
};
}
public static string GetPureName(string filename)
{
return filename.Replace(".ini", "");
}
private static GroupPanel CreateGroupPanel(string filename, int pQty)
{
var group = new GroupPanel
{
CanvasColor = System.Drawing.SystemColors.Control,
ColorSchemeStyle = DevComponents.DotNetBar.eDotNetBarStyle.Office2007,
DisabledBackColor = System.Drawing.Color.Empty,
Location = new System.Drawing.Point(3, 3),
Height = 25 * (pQty + 1),
Width = 300,
};
group.Name = GetPureName(filename);
group.Text = GetPureName(filename);
group.Style.BackColor2SchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBackground2;
group.Style.BackColorGradientAngle = 90;
group.Style.BackColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBackground;
group.Style.BorderBottom = DevComponents.DotNetBar.eStyleBorderType.Solid;
group.Style.BorderBottomWidth = 1;
group.Style.BorderColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBorder;
group.Style.BorderLeft = DevComponents.DotNetBar.eStyleBorderType.Solid;
group.Style.BorderLeftWidth = 1;
group.Style.BorderRight = DevComponents.DotNetBar.eStyleBorderType.Solid;
group.Style.BorderRightWidth = 1;
group.Style.BorderTop = DevComponents.DotNetBar.eStyleBorderType.Solid;
group.Style.BorderTopWidth = 1;
group.Style.CornerDiameter = 4;
group.Style.CornerType = DevComponents.DotNetBar.eCornerType.Rounded;
group.Style.TextAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center;
group.Style.TextColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelText;
group.Style.TextLineAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Near;
group.StyleMouseDown.CornerType = DevComponents.DotNetBar.eCornerType.Square;
group.StyleMouseOver.CornerType = DevComponents.DotNetBar.eCornerType.Square;
group.Visible = false;
return group;
}
}
}