netmonitor/NetMonitorClient/EditNetworks.cs
2010-07-15 00:41:08 +00:00

273 lines
9.1 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace NetMonitorClient
{
public partial class EditNetworks : Form
{
public enum Options
{
Show,
Edit
}
internal enum RWFlag
{
Read,
Write
}
internal enum AMMode
{
Auto,
Manu,
Disable
}
private RWFlag acess;
private InIReader networkini;
public EditNetworks(Options option)
{
if (option == Options.Show)
{
this.acess = RWFlag.Read;
}
if (option == Options.Edit)
{
this.acess = RWFlag.Write;
}
InitializeComponent();
ReadNetworksSettings();
SetRWSettings();
}
private void SetRWSettings()
{
if (this.acess == RWFlag.Read)
{
this.BoxDns.ReadOnly = true;
this.BoxGateway.ReadOnly = true;
this.BoxIpAdresse.ReadOnly = true;
this.BoxName.ReadOnly = true;
this.BoxSubnet.ReadOnly = true;
this.BoxWins.ReadOnly = true;
this.Text = "Netzwerke Ansehen";
this.RadioAllAuto.Enabled = false;
this.RadioAllManu.Enabled = false;
this.RadioDnsAuto.Enabled = false;
this.RadioDnsManu.Enabled = false;
this.RadioIpAuto.Enabled = false;
this.RadioIpManu.Enabled = false;
this.RadioWinsAuto.Enabled = false;
this.RadioWinsManu.Enabled = false;
}
if (this.acess == RWFlag.Write)
{
this.BoxDns.ReadOnly = false;
this.BoxGateway.ReadOnly = false;
this.BoxIpAdresse.ReadOnly = false;
this.BoxName.ReadOnly = false;
this.BoxSubnet.ReadOnly = false;
this.BoxWins.ReadOnly = false;
this.Text = "Netzwerke Ändern";
this.RadioAllAuto.Enabled = true;
this.RadioAllManu.Enabled = true;
this.RadioDnsAuto.Enabled = true;
this.RadioDnsManu.Enabled = true;
this.RadioIpAuto.Enabled = true;
this.RadioIpManu.Enabled = true;
this.RadioWinsAuto.Enabled = true;
this.RadioWinsManu.Enabled = true;
}
}
private void ReadNetworksSettings()
{
this.networkini = new InIReader("network.ini");
ArrayList networks = this.networkini.getSections();
foreach (String net in networks)
{
this.NetworksList.Items.Add(net);
}
}
private void NetworksList_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox Item = (ListBox)sender;
String name = networkini.getValue(Item.Text, "Name");
String ip = networkini.getValue(Item.Text, "Ip");
String subnet = networkini.getValue(Item.Text, "Subnet");
String gateway = networkini.getValue(Item.Text, "Gateway");
String dns = networkini.getValue(Item.Text, "Dns");
String wins = networkini.getValue(Item.Text, "Wins");
String adapter = networkini.getValue(Item.Text, "Adapter");
this.BoxName.Text = name;
if (ip == "auto" && dns == "auto" && wins == "auto")
{
this.setAllMode(AMMode.Auto);
}
else
{
this.setAllMode(AMMode.Manu);
if (ip == "auto")
setIpMode(AMMode.Auto);
else
{
setIpMode(AMMode.Manu);
this.BoxIpAdresse.Text = ConvIp(ip);
this.BoxSubnet.Text = ConvSubnet(subnet);
this.BoxGateway.Text = ConvIp(gateway);
}
if (dns == "auto")
setDnsMode(AMMode.Auto);
else
{
setDnsMode(AMMode.Manu);
this.BoxDns.Text = ConvIp(dns);
}
if (wins == "auto")
setWinsMode(AMMode.Auto);
else
{
setWinsMode(AMMode.Manu);
this.BoxWins.Text = ConvIp(wins);
}
}
}
private string ConvIp(string ip)
{
String[] ips = ip.Split('.');
String[] ret = new String[ips.Length];
for (int i = 0; i < ips.Length;i++)
{
ips[i] = int.Parse(ips[i]).ToString("D3");
}
return string.Join(".", ips);
}
private string ConvSubnet(string subnet)
{
String[] ips = subnet.Split('.');
String Bits = "";
foreach (String part in ips)
{
Bits += Convert.ToString(byte.Parse(part), 2).PadLeft(8, '0');
}
int meng = 0;
foreach (char bit in Bits)
{
if (bit == '1')
meng++;
}
return meng.ToString();
}
private void setAllMode(AMMode mode)
{
if (mode == AMMode.Auto)
{
this.RadioAllAuto.Checked = true;
this.RadioAllManu.Checked = false;
setIpMode(AMMode.Disable);
setDnsMode(AMMode.Disable);
setWinsMode(AMMode.Disable);
}
if (mode == AMMode.Manu)
{
this.RadioAllManu.Checked = true;
this.RadioAllAuto.Checked = false;
setIpMode(AMMode.Manu);
setDnsMode(AMMode.Manu);
setWinsMode(AMMode.Manu);
}
}
private void setWinsMode(AMMode mode)
{
if (mode == AMMode.Auto)
{
this.RadioWinsAuto.Checked = true;
this.RadioWinsManu.Checked = false;
this.BoxWins.Enabled = false;
this.BoxWins.Text = "";
}
if (mode == AMMode.Disable)
{
this.RadioWinsManu.Checked = false;
this.RadioWinsAuto.Checked = false;
this.BoxWins.Enabled = false;
this.BoxWins.Text = "";
}
if (mode == AMMode.Manu)
{
this.RadioWinsAuto.Checked = false;
this.RadioWinsManu.Checked = true;
this.BoxWins.Enabled = true;
}
}
private void setDnsMode(AMMode mode)
{
if (mode == AMMode.Auto)
{
this.RadioDnsAuto.Checked = true;
this.RadioDnsManu.Checked = false;
this.BoxDns.Enabled = false;
this.BoxDns.Text = "";
}
if (mode == AMMode.Disable)
{
this.RadioDnsManu.Checked = false;
this.RadioDnsAuto.Checked = false;
this.BoxDns.Enabled = false;
this.BoxDns.Text = "";
}
if (mode == AMMode.Manu)
{
this.RadioDnsAuto.Checked = false;
this.RadioDnsManu.Checked = true;
this.BoxDns.Enabled = true;
}
}
private void setIpMode(AMMode mode)
{
if (mode == AMMode.Auto)
{
this.RadioIpAuto.Checked = true;
this.RadioIpManu.Checked = false;
this.BoxIpAdresse.Enabled = false;
this.BoxIpAdresse.Text = "";
this.BoxSubnet.Enabled = false;
this.BoxSubnet.Text = "";
this.BoxGateway.Enabled = false;
this.BoxGateway.Text = "";
}
if (mode == AMMode.Disable)
{
this.RadioIpManu.Checked = false;
this.RadioIpAuto.Checked = false;
this.BoxIpAdresse.Enabled = false;
this.BoxIpAdresse.Text = "";
this.BoxSubnet.Enabled = false;
this.BoxSubnet.Text = "";
this.BoxGateway.Enabled = false;
this.BoxGateway.Text = "";
}
if (mode == AMMode.Manu)
{
this.RadioIpAuto.Checked = false;
this.RadioIpManu.Checked = true;
this.BoxIpAdresse.Enabled = true;
this.BoxSubnet.Enabled = true;
this.BoxGateway.Enabled = true;
}
}
}
}