# Kill Process on Disconnect Feature - Implementation Summary ## Overview This document describes the implementation of the "Kill Process on Disconnect" feature for the Pianobar Helper application. ## Feature Requirements 1. **Kill Process Checkbox (`chkkillpro`)**: A checkbox in the Settings form that allows users to enable/disable the kill-on-disconnect behavior 2. **Gating Mechanism**: The checkbox must be disabled (non-interactive) unless the "Enable Profile Credentials and Custom Config Generation" checkbox (`chkOverrideCredentials`) is checked 3. **Disconnect Behavior**: When the user clicks the Disconnect button in the main application: - If `chkkillpro` is checked: Pianobar process is immediately killed via its PID - If `chkkillpro` is unchecked: Pianobar process is detached (continues running in background) ## Implementation Details ### 1. Data Model (`ProfileSettings`) **File**: `ProfileManager.cs` Added `KillProcessOnDisconnect` boolean property to `ProfileSettings`: ```csharp public class ProfileSettings { // ... other properties ... public bool KillProcessOnDisconnect { get; set; } } ``` This property is saved/loaded with each profile in `profiles.json`. ### 2. Process Management (`PianobarManager`) **File**: `PianobarManager.cs` Implemented two disconnect methods: #### `Disconnect()` - Detach mode - Cleans up streams and event handlers - Sets `IsConnected = false` - Leaves the Pianobar process running in the background #### `DisconnectAndKill()` - Kill mode - Checks if the process exists and is not exited - Calls `Process.Kill()` to terminate immediately - Reports the PID that was killed - Cleans up and sets `IsConnected = false` ### 3. Settings UI (`SettingsForm`) **File**: `Settings.cs` and `Settings.Designer.cs` #### Control Initialization - `chkkillpro` checkbox starts **disabled** (set in Designer) - All credential-related fields start disabled: - `txtConfigFilePath` - `btnBrowseConfigFile` - `txtPianobarUser` - `txtPianobarPass` - `chkkillpro` #### Event Handling **`chkOverrideCredentials_CheckedChanged`**: - Wired in Designer (line 352) - Calls `ToggleCredentialFields(checked)` method **`ToggleCredentialFields(bool enabled)`**: ```csharp private void ToggleCredentialFields(bool enabled) { if (txtConfigFilePath != null) txtConfigFilePath.Enabled = enabled; if (btnBrowseConfigFile != null) btnBrowseConfigFile.Enabled = enabled; if (txtPianobarUser != null) txtPianobarUser.Enabled = enabled; if (txtPianobarPass != null) txtPianobarPass.Enabled = enabled; if (chkkillpro != null) chkkillpro.Enabled = enabled; // <-- Critical: Kill checkbox is gated here } ``` #### Profile Save/Load - `LoadSettingsToControls()` loads `KillProcessOnDisconnect` into `chkkillpro` - `SaveSettingsFromControls()` saves `chkkillpro.Checked` to profile settings - When saving, the profile is set as active via `SetActiveProfile()` ### 4. Main Application Logic (`Main.cs`) **File**: `Main.cs` **`btnDisconnect_Click` handler**: ```csharp public void btnDisconnect_Click(object? sender, EventArgs e) { if (!_pianobarManager.IsConnected) { MessageBox.Show("Not connected to Pianobar.", "Not Connected", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (statusLabelRight != null) statusLabelRight.Text = "Disconnecting..."; // Check if we should kill the process or just detach var activeProfile = _profileManager.GetActiveProfile(); bool killProcess = activeProfile?.Settings.KillProcessOnDisconnect ?? false; if (killProcess) { _pianobarManager.DisconnectAndKill(); // <-- Terminates process } else { _pianobarManager.Disconnect(); // <-- Detaches, leaves running } _outputManager?.Stop(); } ``` ### 5. Output Folder Browse Button **File**: `Settings.cs` and `Settings.Designer.cs` **`output_folder_browse_button`** (ID as requested): - Wired in Designer to `output_folder_browse_button_Click` (line 237) - Opens `SaveFileDialog` to allow user to select output file location - Default filename: `now_playing.txt` - Selected path is saved to `output_folder_text_box` (maps to `TextFilePath` in settings) ## User Workflow ### Enabling Kill-on-Disconnect 1. Open **File ? Settings** 2. Navigate to **"Pianobar Config & Auth"** tab 3. Check **"Enable Profile Credentials and Custom Config Generation"** (`chkOverrideCredentials`) - This enables all credential fields **and** the kill process checkbox 4. Check **"Kill process"** (`chkkillpro`) 5. Click **Save** - Profile is updated and set as active - Setting persists for future sessions ### Using the Feature - **Connect**: Click Connect button (Pianobar starts in background, PID shown in status) - **Disconnect with Kill**: If `chkkillpro` was checked: - Pianobar process is immediately terminated by PID - Status shows "Pianobar process killed (PID: XXXX)" - **Disconnect without Kill**: If `chkkillpro` was unchecked: - Pianobar process continues running - Application detaches and closes streams - Status shows "Disconnected from Pianobar (process detached)" ### Disabling Override - If user unchecks `chkOverrideCredentials`: - Kill process checkbox becomes disabled and cannot be checked - All credential fields are disabled - User cannot enable kill-on-disconnect without also enabling custom config generation ## Testing Checklist ? **Build Status**: Project builds successfully without errors ? **Designer Wiring**: All event handlers properly wired in Designer files: - `chkOverrideCredentials.CheckedChanged` ? `chkOverrideCredentials_CheckedChanged` - `output_folder_browse_button.Click` ? `output_folder_browse_button_Click` - `chkkillpro` exists and is added to `pianobar_override_tab_page` ? **Initial State**: All credential fields and `chkkillpro` start disabled ? **Gating Logic**: `ToggleCredentialFields()` enables/disables `chkkillpro` based on override checkbox ? **Profile Persistence**: `KillProcessOnDisconnect` saved/loaded with profile ? **Disconnect Logic**: Main form branches to kill or detach based on profile setting ? **Process Management**: `DisconnectAndKill()` terminates by PID; `Disconnect()` detaches only ## Files Modified 1. **ProfileManager.cs** - Added `KillProcessOnDisconnect` to data model 2. **PianobarManager.cs** - Added `DisconnectAndKill()` method 3. **Settings.cs** - Load/save logic and gating via `ToggleCredentialFields()` 4. **Settings.Designer.cs** - Set initial `Enabled = false` for credential fields and `chkkillpro` 5. **Main.cs** - Disconnect logic branches based on profile flag ## Summary The kill-on-disconnect feature is fully implemented and integrated with the existing profile system. The checkbox is properly gated by the override credentials checkbox, ensuring users cannot accidentally enable the feature without understanding its context within custom config generation. The feature provides clean process termination when desired while preserving the option to keep Pianobar running in the background.