using Pulsar.Common.Enums; using Pulsar.Common.Messages; using Pulsar.Common.Models; using Pulsar.Server.Forms.DarkMode; using Pulsar.Server.Helper; using Pulsar.Server.Messages; using Pulsar.Server.Networking; using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace Pulsar.Server.Forms { public partial class FrmStartupManager : Form { /// /// The client which can be used for the startup manager. /// private readonly Client _connectClient; /// /// The message handler for handling the communication with the client. /// private readonly StartupManagerHandler _startupManagerHandler; /// /// Holds the opened startup manager form for each client. /// private static readonly Dictionary OpenedForms = new Dictionary(); /// /// Creates a new startup manager form for the client or gets the current open form, if there exists one already. /// /// The client used for the startup manager form. /// /// Returns a new startup manager form for the client if there is none currently open, otherwise creates a new one. /// public static FrmStartupManager CreateNewOrGetExisting(Client client) { if (OpenedForms.ContainsKey(client)) { return OpenedForms[client]; } FrmStartupManager f = new FrmStartupManager(client); f.Disposed += (sender, args) => OpenedForms.Remove(client); OpenedForms.Add(client, f); return f; } /// /// Initializes a new instance of the class using the given client. /// /// The client used for the remote desktop form. public FrmStartupManager(Client client) { _connectClient = client; _startupManagerHandler = new StartupManagerHandler(client); RegisterMessageHandler(); InitializeComponent(); DarkModeManager.ApplyDarkMode(this); ScreenCaptureHider.ScreenCaptureHider.Apply(this.Handle); } /// /// Registers the startup manager message handler for client communication. /// private void RegisterMessageHandler() { _connectClient.ClientState += ClientDisconnected; _startupManagerHandler.ProgressChanged += StartupItemsChanged; MessageHandler.Register(_startupManagerHandler); } /// /// Unregisters the startup manager message handler. /// private void UnregisterMessageHandler() { MessageHandler.Unregister(_startupManagerHandler); _startupManagerHandler.ProgressChanged -= StartupItemsChanged; _connectClient.ClientState -= ClientDisconnected; } /// /// Called whenever a client disconnects. /// /// The client which disconnected. /// True if the client connected, false if disconnected private void ClientDisconnected(Client client, bool connected) { if (!connected) { this.Invoke((MethodInvoker)this.Close); } } private void FrmStartupManager_Load(object sender, EventArgs e) { this.Text = WindowHelper.GetWindowTitle("Startup Manager", _connectClient); AddGroups(); _startupManagerHandler.RefreshStartupItems(); } private void FrmStartupManager_FormClosing(object sender, FormClosingEventArgs e) { UnregisterMessageHandler(); } /// /// Adds all supported startup types as ListView groups. /// private void AddGroups() { lstStartupItems.Groups.Add( new ListViewGroup("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run") { Tag = StartupType.LocalMachineRun }); lstStartupItems.Groups.Add( new ListViewGroup("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce") { Tag = StartupType.LocalMachineRunOnce }); lstStartupItems.Groups.Add( new ListViewGroup("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run") { Tag = StartupType.CurrentUserRun }); lstStartupItems.Groups.Add( new ListViewGroup("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce") { Tag = StartupType.CurrentUserRunOnce }); lstStartupItems.Groups.Add( new ListViewGroup("HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run") { Tag = StartupType.LocalMachineRunX86 }); lstStartupItems.Groups.Add( new ListViewGroup("HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce") { Tag = StartupType.LocalMachineRunOnceX86 }); lstStartupItems.Groups.Add(new ListViewGroup("%APPDATA%\\Microsoft\\Windows\\Start Menu\\Programs\\Startup") { Tag = StartupType.StartMenu }); } /// /// Called whenever a startup item changed. /// /// The message handler which raised the event. /// The current startup items of the client. private void StartupItemsChanged(object sender, List startupItems) { lstStartupItems.Items.Clear(); foreach (var item in startupItems) { var i = lstStartupItems.Groups.Cast().First(x => (StartupType)x.Tag == item.Type); ListViewItem lvi = new ListViewItem(new[] { item.Name, item.Path }) { Group = i, Tag = item }; lstStartupItems.Items.Add(lvi); } } private void addEntryToolStripMenuItem_Click(object sender, EventArgs e) { using (var frm = new FrmStartupAdd()) { if (frm.ShowDialog() == DialogResult.OK) { _startupManagerHandler.AddStartupItem(frm.StartupItem); _startupManagerHandler.RefreshStartupItems(); } } } private void removeEntryToolStripMenuItem_Click(object sender, EventArgs e) { bool modified = false; foreach (ListViewItem item in lstStartupItems.SelectedItems) { _startupManagerHandler.RemoveStartupItem((StartupItem)item.Tag); modified = true; } if (modified) { _startupManagerHandler.RefreshStartupItems(); } } } }