249 lines
8.0 KiB
C#
249 lines
8.0 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.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.Reflection;
|
|
|
|
namespace EthernetSwitcher
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
private static EthernetSwitcherService _service;
|
|
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
InitializeTrayIcon();
|
|
LoadNetworkInterfaces();
|
|
LoadSettings();
|
|
|
|
// Start the background service
|
|
_service = new EthernetSwitcherService();
|
|
_service.Start(trayIcon);
|
|
}
|
|
|
|
private void InitializeTrayIcon()
|
|
{
|
|
var contextMenu = new ContextMenuStrip();
|
|
|
|
// Add Mode submenu
|
|
var modeMenuItem = new ToolStripMenuItem("Режим");
|
|
foreach (EthernetMode mode in Enum.GetValues(typeof(EthernetMode)))
|
|
{
|
|
var modeItem = new ToolStripMenuItem(GetModeDisplayName(mode));
|
|
modeItem.Tag = mode;
|
|
modeItem.Click += (sender, e) => SetModeFromTray((EthernetMode)((ToolStripMenuItem)sender).Tag);
|
|
modeMenuItem.DropDownItems.Add(modeItem);
|
|
}
|
|
contextMenu.Items.Add(modeMenuItem);
|
|
|
|
contextMenu.Items.Add("Настройки", null, ShowSettings);
|
|
contextMenu.Items.Add("Выход", null, ExitApplication);
|
|
|
|
var networkIcon = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream("EthernetSwitcher.icon.ico"));
|
|
|
|
trayIcon = new NotifyIcon()
|
|
{
|
|
Icon = networkIcon,
|
|
Text = "Ethernet Switcher",
|
|
Visible = true,
|
|
ContextMenuStrip = contextMenu
|
|
};
|
|
|
|
trayIcon.Click += TrayIcon_Click;
|
|
|
|
UpdateModeMenuCheck();
|
|
}
|
|
|
|
private void ShowSettings(object sender, EventArgs e)
|
|
{
|
|
this.Show();
|
|
this.WindowState = FormWindowState.Normal;
|
|
this.BringToFront();
|
|
}
|
|
|
|
private void ExitApplication(object sender, EventArgs e)
|
|
{
|
|
trayIcon.Visible = false;
|
|
Application.Exit();
|
|
}
|
|
|
|
private void SetModeFromTray(EthernetMode mode)
|
|
{
|
|
var settings = CurrentSettings.Instance;
|
|
settings.Mode = mode;
|
|
CurrentSettings.Save();
|
|
|
|
// Update the service if needed
|
|
_service?.UpdateMode(mode);
|
|
|
|
// Update menu check marks
|
|
UpdateModeMenuCheck();
|
|
}
|
|
|
|
private string GetModeDisplayName(EthernetMode mode)
|
|
{
|
|
switch (mode)
|
|
{
|
|
case EthernetMode.Auto:
|
|
return "Авто";
|
|
case EthernetMode.Primary:
|
|
return "Основной";
|
|
case EthernetMode.Secondary:
|
|
return "Резервный";
|
|
case EthernetMode.Disabled:
|
|
return "Отключен";
|
|
default:
|
|
return mode.ToString();
|
|
}
|
|
}
|
|
|
|
private void UpdateModeMenuCheck()
|
|
{
|
|
if (trayIcon?.ContextMenuStrip == null) return;
|
|
|
|
var currentMode = CurrentSettings.Instance.Mode;
|
|
var modeMenuItem = trayIcon.ContextMenuStrip.Items[0] as ToolStripMenuItem;
|
|
if (modeMenuItem?.DropDownItems != null)
|
|
{
|
|
foreach (ToolStripMenuItem item in modeMenuItem.DropDownItems)
|
|
{
|
|
item.Checked = ((EthernetMode)item.Tag) == currentMode;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void LoadNetworkInterfaces()
|
|
{
|
|
var interfaces = CurrentSettings.GetNetworkInterfaces();
|
|
|
|
comboBoxPrimary.Items.Clear();
|
|
comboBoxSecondary.Items.Clear();
|
|
|
|
comboBoxPrimary.Items.AddRange(interfaces.ToArray());
|
|
comboBoxSecondary.Items.AddRange(interfaces.ToArray());
|
|
|
|
if (interfaces.Count > 0)
|
|
{
|
|
comboBoxPrimary.SelectedIndex = 0;
|
|
comboBoxSecondary.SelectedIndex = Math.Min(1, interfaces.Count - 1);
|
|
}
|
|
}
|
|
|
|
private void LoadSettings()
|
|
{
|
|
var settings = CurrentSettings.Instance;
|
|
|
|
comboBoxMode.SelectedItem = settings.Mode.ToString();
|
|
|
|
// Find the display names that match the saved clean names
|
|
var primaryDisplay = FindInterfaceDisplay(settings.PrimaryName);
|
|
var secondaryDisplay = FindInterfaceDisplay(settings.SecondaryName);
|
|
|
|
comboBoxPrimary.SelectedItem = primaryDisplay;
|
|
comboBoxSecondary.SelectedItem = secondaryDisplay;
|
|
comboBoxProtocol.SelectedItem = settings.Protocol.ToString().Replace("_", " + ");
|
|
trackBarInterval.Value = settings.CheckInterval;
|
|
|
|
// Update tray menu check marks
|
|
UpdateModeMenuCheck();
|
|
}
|
|
|
|
private string FindInterfaceDisplay(string cleanName)
|
|
{
|
|
foreach (var item in comboBoxPrimary.Items)
|
|
{
|
|
if (item.ToString().StartsWith(cleanName + " ("))
|
|
{
|
|
return item.ToString();
|
|
}
|
|
}
|
|
return cleanName; // Fallback if not found
|
|
}
|
|
|
|
private void SaveSettings()
|
|
{
|
|
var settings = CurrentSettings.Instance;
|
|
|
|
EthernetMode parsedMode;
|
|
if (Enum.TryParse<EthernetMode>(comboBoxMode.SelectedItem?.ToString(), out parsedMode))
|
|
{
|
|
settings.Mode = parsedMode;
|
|
}
|
|
|
|
settings.PrimaryName = CurrentSettings.GetCleanInterfaceName(comboBoxPrimary.SelectedItem?.ToString() ?? "");
|
|
settings.SecondaryName = CurrentSettings.GetCleanInterfaceName(comboBoxSecondary.SelectedItem?.ToString() ?? "");
|
|
|
|
var protocolString = comboBoxProtocol.SelectedItem?.ToString().Replace(" + ", "_") ?? "IPv4_IPv6";
|
|
IpProtocol parsedProtocol;
|
|
if (Enum.TryParse<IpProtocol>(protocolString, out parsedProtocol))
|
|
{
|
|
settings.Protocol = parsedProtocol;
|
|
}
|
|
|
|
settings.CheckInterval = trackBarInterval.Value;
|
|
|
|
CurrentSettings.Save();
|
|
}
|
|
|
|
private void TrackBarInterval_ValueChanged(object sender, EventArgs e)
|
|
{
|
|
label4.Text = $"Интервал проверки: {trackBarInterval.Value} сек";
|
|
}
|
|
|
|
protected override void OnVisibleChanged(EventArgs e)
|
|
{
|
|
base.OnVisibleChanged(e);
|
|
|
|
if (this.Visible)
|
|
{
|
|
LoadNetworkInterfaces();
|
|
LoadSettings();
|
|
}
|
|
}
|
|
|
|
private void TrayIcon_Click(object sender, EventArgs e)
|
|
{
|
|
var mouseEvent = e as MouseEventArgs;
|
|
if (mouseEvent != null && mouseEvent.Button == MouseButtons.Left)
|
|
{
|
|
this.Show();
|
|
this.WindowState = FormWindowState.Normal;
|
|
this.BringToFront();
|
|
}
|
|
}
|
|
|
|
protected override void OnResize(EventArgs e)
|
|
{
|
|
base.OnResize(e);
|
|
|
|
if (WindowState == FormWindowState.Minimized)
|
|
{
|
|
this.Hide();
|
|
}
|
|
}
|
|
|
|
protected override void OnFormClosing(FormClosingEventArgs e)
|
|
{
|
|
if (e.CloseReason == CloseReason.UserClosing)
|
|
{
|
|
SaveSettings();
|
|
e.Cancel = true;
|
|
this.Hide();
|
|
}
|
|
base.OnFormClosing(e);
|
|
}
|
|
|
|
protected override void OnFormClosed(FormClosedEventArgs e)
|
|
{
|
|
_service?.Stop();
|
|
trayIcon?.Dispose();
|
|
base.OnFormClosed(e);
|
|
}
|
|
}
|
|
} |