using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace WoL_Manager { public partial class MainWindow : Form { private Host selectedHost; private int hostIndex; public MainWindow() { InitializeComponent(); } public void rebuildDataView() { Host[] hostList = Host.getHostList(); DataGridViewRowCollection rowsColl = DataGridHosts.Rows; if (hostList != null && hostList.Length > 0) { for (int t = 0; t < hostList.Length; t++) { object[] temp = new object[5]; temp[0] = hostList[t].getStatusIcon(); temp[1] = hostList[t].Name; temp[2] = hostList[t].IP.ToString(); temp[3] = hostList[t].getStatusString(); temp[4] = hostList[t]; if (rowsColl.Count > t) { rowsColl[t].SetValues(temp); } else { rowsColl.Insert(t, temp); } } if (rowsColl.Count > hostList.Length) { for (int t = hostList.Length; t < rowsColl.Count; t++) { rowsColl.RemoveAt(t); } } } else { rowsColl.Clear(); } DataGridViewSelectedRowCollection tempRows = DataGridHosts.SelectedRows; if (tempRows != null && tempRows.Count > 0) { hostIndex = tempRows[0].Index; DataGridViewCellCollection cells = tempRows[0].Cells; selectedHost = (Host)cells[4].Value; } } public void updateDataView() { DataGridViewRowCollection rowsColl = DataGridHosts.Rows; for (int t = 0; t < rowsColl.Count; t++) { DataGridViewCellCollection cells = rowsColl[t].Cells; Host host = (Host)cells[4].Value; if (host != null) { cells[0].Value = host.getStatusIcon(); cells[1].Value = host.Name; cells[2].Value = host.IP.ToString(); cells[3].Value = host.getStatusString(); } } } private void createPingThreads() { Host[] hostList = Host.getHostList(); for (int t = 0; t < hostList.Length; t++) { PingThread temp = new PingThread(hostList[t].IP, ManagerSettings.PingWaitTime, ManagerSettings.PingTimeOut, hostList[t]); hostList[t].pingThread = temp; Thread thread = new Thread(new ThreadStart(temp.threadFunction)); thread.Start(); } } public void deletePingThreads() { Host[] hostList = Host.getHostList(); for (int t = 0; t < hostList.Length; t++) { hostList[t].pingThread.stopThread(); hostList[t].pingThread = null; } } private void updatePingThreads() { Host[] hostList = Host.getHostList(); for (int t = 0; t < hostList.Length; t++) { if (hostList[t].pingThread == null) { PingThread temp = new PingThread(hostList[t].IP, ManagerSettings.PingWaitTime, ManagerSettings.PingTimeOut, hostList[t]); hostList[t].pingThread = temp; Thread thread = new Thread(new ThreadStart(temp.threadFunction)); thread.Start(); } } } private void MainWindow_Load(object sender, EventArgs e) { QuitMenuItem1.Click += new System.EventHandler(this.quitEventHandler); AboutMenuItem1.Click += new EventHandler(aboutEventHandler); AddMenuItem1.Click += new EventHandler(newHostEventHandler); DataGridHosts.SelectionChanged += new EventHandler(this.selectionEventHandler); DeleteMenuItem1.Click += new EventHandler(this.deleteEventHandler); EditMenuItem1.Click += new EventHandler(this.editHostEventHandler); WakeMenuItem1.Click += new EventHandler(wakeHostEventHandler); SettingsMenuItem1.Click += new EventHandler(settingsEventHandler); AddMenuItem2.Click +=new EventHandler(newHostEventHandler); EditMenuItem2.Click += new EventHandler(editHostEventHandler); DeleteMenuItem2.Click += new EventHandler(deleteEventHandler); WakeMenuItem2.Click += new EventHandler(wakeHostEventHandler); createPingThreads(); selectedHost = null; rebuildDataView(); this.Resize += new EventHandler(resizeEventHandler); TaskBarIcon.Click += new EventHandler(taskBarIconEventHandler); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainWindow)); Icon[] tempList = new Icon[4]; tempList[0] = new Icon((Icon)resources.GetObject("icon_gray"),new Size(24, 24)); tempList[1] = new Icon((Icon)resources.GetObject("icon_green"), new Size(24, 24)); tempList[2] = new Icon((Icon)resources.GetObject("icon_red"), new Size(24, 24)); tempList[3] = new Icon((Icon)resources.GetObject("icon_yellow"), new Size(24, 24)); Host.IconList = tempList; } void settingsEventHandler(object sender, EventArgs e) { SettingsWindow window = new SettingsWindow(); window.StartPosition = FormStartPosition.CenterParent; window.ShowDialog(this); window.Activate(); deletePingThreads(); updatePingThreads(); } private void taskBarIconEventHandler(object sender, EventArgs e) { this.WindowState = FormWindowState.Normal; this.Show(); this.Focus(); this.BringToFront(); } private int resizeState = 0; private void resizeEventHandler(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { resizeState = 1; this.ShowInTaskbar = false; } else if (resizeState == 1) { resizeState = 0; this.ShowInTaskbar = true; } } private void wakeHostEventHandler(object sender, EventArgs e) { if (selectedHost == null) return; selectedHost.wakeHost(); } private void quitEventHandler(object sender, EventArgs e) { this.Close(); } private void aboutEventHandler(object sender, EventArgs e) { AboutWindow window = new AboutWindow(); window.StartPosition = FormStartPosition.CenterParent; window.ShowDialog(this); window.Activate(); } private void newHostEventHandler(object sender, EventArgs e) { NewHostWindow window = new NewHostWindow(); window.StartPosition = FormStartPosition.CenterParent; window.ShowDialog(this); window.Activate(); rebuildDataView(); updatePingThreads(); } private void editHostEventHandler(object sender, EventArgs e) { if (selectedHost == null) { return; } EditHostWindow window = new EditHostWindow(selectedHost); window.StartPosition = FormStartPosition.CenterParent; window.ShowDialog(this); window.Activate(); updateDataView(); updatePingThreads(); } private void deleteEventHandler(object sender, EventArgs e) { if (selectedHost == null) { return; } DialogResult result = MessageBox.Show("Are you sure?", "Delete Host", MessageBoxButtons.OKCancel); if (result == DialogResult.OK) { Host.deleteHost(selectedHost); rebuildDataView(); } } private void selectionEventHandler(object sender, EventArgs e) { DataGridViewSelectedRowCollection tempRows = DataGridHosts.SelectedRows; if (tempRows != null && tempRows.Count > 0) { hostIndex = tempRows[0].Index; DataGridViewCellCollection cells = tempRows[0].Cells; selectedHost = (Host)cells[4].Value; WakeMenuItem1.Enabled = true; DeleteMenuItem1.Enabled = true; EditMenuItem1.Enabled = true; EditMenuItem2.Enabled = true; DeleteMenuItem2.Enabled = true; WakeMenuItem2.Enabled = true; Host host = (Host)cells[4].Value; if (host.Description == null) { host.Description = ""; } OutputTextBox.Text = String.Format(" Name:\t\t{0}\n IP-Address:\t{1}\n Broadcast:\t{2}\n MAC-Address:\t{3}\n===========================================\n Description:\n\n{4}", host.Name, host.IP.ToString(), host.Broadcast.ToString(), host.MAC, host.Description); } else { selectedHost = null; WakeMenuItem1.Enabled = false; DeleteMenuItem1.Enabled = false; EditMenuItem1.Enabled = false; EditMenuItem2.Enabled = false; DeleteMenuItem2.Enabled = false; WakeMenuItem2.Enabled = false; OutputTextBox.Text = ""; } } public static bool checkMAC(string mac) { if (mac.Length != 12) { return false; } else { for (int t = 0; t < mac.Length; t++) { if (Char.IsLetterOrDigit(mac[t])) { if (Char.IsLetter(mac[t])) { switch (mac[t]) { case 'A': break; case 'B': break; case 'C': break; case 'D': break; case 'E': break; case 'F': break; default: return false; } } } else { return false; } } } return true; } } }