/// <summary> /// Click Event handler for the Wake Selection button. /// </summary> /// <param name="sender">Reference to the WakeSelectionButton object.</param> /// <param name="e">Event specific data.</param> private void WakeSelectionButton_Click(object sender, EventArgs e) { StringBuilder WakeErrors = new StringBuilder(); Cursor.Current = Cursors.WaitCursor; // Wake all selected hosts. foreach (ListViewItem SelectedItem in this.HostsListView.SelectedItems) { WolHost SelectedHost = SelectedItem.Tag as WolHost; try { WakeHost(SelectedHost); } catch (Exception Ex) { WakeErrors.AppendFormat("{0}: {1}\n", SelectedHost.Name, Ex.Message); } } Cursor.Current = null; // If any hosts caused an exception, report the exception messages. if (WakeErrors.Length > 0) { MessageBox.Show(WakeErrors.ToString(), "Host Wake Request", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void HostContextMenuStrip_Opening(object sender, CancelEventArgs e) { WolHost SelectedHost = null; this.AwakenHostMenuItem.DropDownItems.Clear(); if (this.HostsListView.SelectedItems.Count == 1) { SelectedHost = this.HostsListView.SelectedItems[0].Tag as WolHost; } if (SelectedHost != null && SelectedHost.Networks.Count > 1) { foreach (WolHostNetwork CurrentNetwork in SelectedHost.Networks) { ToolStripMenuItem NewMenuItem = new ToolStripMenuItem(); NewMenuItem.Text = CurrentNetwork.Name; NewMenuItem.Tag = CurrentNetwork.NetworkId; NewMenuItem.Click += AwakenHostSubMenuItem_Click; if (CurrentNetwork.NetworkId == SelectedHost.DefaultNetwork) { NewMenuItem.Font = new Font(this.AwakenHostMenuItem.Font, FontStyle.Bold); } this.AwakenHostMenuItem.DropDownItems.Add(NewMenuItem); } } }
/// <summary> /// /// </summary> public SecureOnPromptForm(WolHost host) { InitializeComponent(); _host = host; InitialiseUI(); }
/// <summary> /// Updates an item in the Hosts ListView with the details for the given WolHost using the /// property options configured in the config file. /// </summary> /// <param name="item">The ListViewItem to update.</param> /// <param name="host">The host details used to refresh the ListViewItem.</param> private void UpdateHostListViewItem(ListViewItem item, WolHost host) { item.ImageIndex = GetMachineIconIndex(host.RequireSecureOn); item.SubItems[0].Text = GetProperty <WolHost, string>(host, Settings.Default.HostViewItemFirstLineProperty); item.SubItems[1].Text = GetProperty <WolHost, string>(host, Settings.Default.HostViewItemSecondLineProperty); item.SubItems[2].Text = GetProperty <WolHost, string>(host, Settings.Default.HostViewItemThirdLineProperty); item.Tag = host; }
/// <summary> /// Creates a new instance of the form for amending the properties of a given WolHost object. /// </summary> /// <param name="action">The amendment mode in which the form will be operating.</param> /// <param name="host"></param> public AmendHost(ActionMode action, WolHost host) { InitializeComponent(); _action = action; _host = host; InitialiseUI(); }
/// <summary> /// Click Event handler for dynamic sub-items attached to the Awaken Host menu item. /// </summary> /// <param name="sender">Reference to the dynamically created menu item.</param> /// <param name="e">Event specific data.</param> private void AwakenHostSubMenuItem_Click(object sender, EventArgs e) { if (this.HostsListView.SelectedItems.Count == 1) { ToolStripMenuItem SourceMenuItem = sender as ToolStripMenuItem; WolHost HostToWake = this.HostsListView.SelectedItems[0].Tag as WolHost; WakeHost(HostToWake, (Guid)SourceMenuItem.Tag); } }
/// <summary> /// Click Event handler for the Amend Host Menu Item. /// </summary> /// <param name="sender">Reference to the AmendHostMenuItem object.</param> /// <param name="e">Event specific data.</param> private void AmendHostMenuItem_Click(object sender, EventArgs e) { if (HostsListView.SelectedItems.Count > 0) { WolHost SelectedHost = this.HostsListView.SelectedItems[0].Tag as WolHost; if (SelectedHost != null) { AmendWolHost(SelectedHost); } } }
/// <summary> /// Adds a new host to the Host ListView using the property options configured in the config file. /// </summary> /// <param name="host">The host to add.</param> /// <returns>The ListViewItem added.</returns> private ListViewItem AddHostListViewItem(WolHost host) { ListViewItem NewHostItem = new ListViewItem(); NewHostItem.Text = GetProperty <WolHost, string>(host, Settings.Default.HostViewItemFirstLineProperty) ?? String.Empty; NewHostItem.SubItems.AddRange(new string[] { GetProperty <WolHost, string>(host, Settings.Default.HostViewItemSecondLineProperty) ?? String.Empty, GetProperty <WolHost, string>(host, Settings.Default.HostViewItemThirdLineProperty) ?? String.Empty }); NewHostItem.ImageIndex = GetMachineIconIndex(host.RequireSecureOn); NewHostItem.Tag = host; return(this.HostsListView.Items.Add(NewHostItem)); }
/// <summary> /// Click Event handler for the Add Host Menu Item. /// </summary> /// <param name="sender">Reference to the AddHostMenuItem object.</param> /// <param name="e">Event specific data.</param> private void AddHostMenuItem_Click(object sender, EventArgs e) { WolHost NewHost = new WolHost(); using (AmendHost AmendHostForm = new AmendHost(ActionMode.Add, NewHost)) { if (AmendHostForm.ShowDialog(this) == DialogResult.OK) { this.Cursor = Cursors.WaitCursor; _hostList.Items.Add(NewHost); AddHostListViewItem(NewHost); this.Cursor = null; } } }
/// <summary> /// Private method to handle then atomic amendment of a Wake on LAN host. /// </summary> /// <param name="hostToAmend">The Wake on LAN which is to be amended.</param> private void AmendWolHost(WolHost hostToAmend) { WolHost ClonedHost = hostToAmend.Clone(); using (AmendHost AmendHostForm = new AmendHost(ActionMode.Amend, ClonedHost)) { if (AmendHostForm.ShowDialog(this) == DialogResult.OK) { this.Cursor = Cursors.WaitCursor; _hostList.Items.Remove(hostToAmend); _hostList.Items.Add(ClonedHost); UpdateHostListViewItem(this.HostsListView.SelectedItems[0], ClonedHost); this.Cursor = null; } } }
/// <summary> /// Double Click Event handler for the Hosts ListView. /// </summary> /// <param name="sender">Reference to the HostsListView object.</param> /// <param name="e">Event specific data.</param> private void HostsListView_DoubleClick(object sender, EventArgs e) { if (this.HostsListView.SelectedItems.Count == 1) { WolHost SelectedHost = this.HostsListView.SelectedItems[0].Tag as WolHost; if (SelectedHost != null) { switch ((HostDoubleClickAction)Enum.Parse(typeof(HostDoubleClickAction), Settings.Default.HostViewItemDoubleClickAction)) { case HostDoubleClickAction.ShowProperties: AmendWolHost(SelectedHost); break; case HostDoubleClickAction.Wake: WakeHost(SelectedHost); break; } } } }
/// <summary> /// Wakes a host, handling any required SecureOn password prompting. /// </summary> /// <param name="hostToWake">The WolHost object describing the host to wake.</param> /// <param name="networkToWake"></param> private void WakeHost(WolHost hostToWake, Guid networkId) { if (hostToWake != null) { // Retrieve the network details for waking the host. WolHostNetwork Network = hostToWake.Networks[networkId]; if (Network == null) { throw new InvalidOperationException("Host cannot be woken as no suitable network has been defined."); } IPEndPoint HostEndpoint = null; if (Network.Locality == WolHostNetwork.NetworkLocality.Remote) { try { IPAddress[] ResolvedAddresses = Dns.GetHostAddresses(Network.Address); if (ResolvedAddresses != null && ResolvedAddresses.Length > 0) { HostEndpoint = new IPEndPoint(ResolvedAddresses[0], Network.Port); } } catch (System.Net.Sockets.SocketException) { HostEndpoint = null; } // Check that a valid endpoint has been prepared. A remote host is not valid for awakening // if the endpoint is not valid by this point - falling back to local would not be any use. if (HostEndpoint == null) { // TODO: Tidy this scenario throw new InvalidOperationException("Invalid host address for remote host."); } } if (hostToWake.RequireSecureOn == false) { if (Network.Locality == WolHostNetwork.NetworkLocality.Remote) { WakeOnLan.WakeMachine(hostToWake.MachineAddress, HostEndpoint, Network.SubnetMask); } else { WakeOnLan.WakeMachine(hostToWake.MachineAddress); } } else { // Check to see whether the SecureOn Password needs to be prompted. if (hostToWake.SecureOnPassword == null || hostToWake.SecureOnPassword == PhysicalAddress.None) { using (SecureOnPromptForm SecureOnPrompt = new SecureOnPromptForm(hostToWake)) { if (SecureOnPrompt.ShowDialog() == DialogResult.OK) { // Use the supplied SecureOn Password. if (Network.Locality == WolHostNetwork.NetworkLocality.Remote) { WakeOnLan.WakeMachine(hostToWake.MachineAddress, SecureOnPrompt.SecureOnPassword, HostEndpoint, Network.SubnetMask); } else { WakeOnLan.WakeMachine(hostToWake.MachineAddress, SecureOnPrompt.SecureOnPassword); } } } } else { // Use the stored SecureOn Password. if (Network.Locality == WolHostNetwork.NetworkLocality.Remote) { WakeOnLan.WakeMachine(hostToWake.MachineAddress, hostToWake.SecureOnPassword, HostEndpoint, Network.SubnetMask); } else { WakeOnLan.WakeMachine(hostToWake.MachineAddress, hostToWake.SecureOnPassword); } } } } }
/// <summary> /// Wakes a host, handling any required SecureOn password prompting. /// </summary> /// <param name="hostToWake">The WolHost object describing the host to wake.</param> private void WakeHost(WolHost hostToWake) { WakeHost(hostToWake, hostToWake.DefaultNetwork); }