Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions Rapr/DSEForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public partial class DSEForm : Form
private Status currentStatus = Status.Normal;
private bool statusRequiresReview;
private ToolStripMenuItem themeMenu;
private ToolStripMenuItem removeGhostDevicesToolStripMenuItem;

private static readonly IUpdateManager UpdateManager = CreateUpdateManager();

Expand Down Expand Up @@ -243,6 +244,7 @@ private void UpdateDriverStore(IDriverStore driverStore)
this.buttonSelectUnusedDrivers.Enabled = driverStore.SupportDeviceNameColumn;
this.ctxMenuSelectOldDrivers.Enabled = driverStore.SupportDeviceNameColumn;
this.ctxMenuSelectUnusedDrivers.Enabled = driverStore.SupportDeviceNameColumn;
this.removeGhostDevicesToolStripMenuItem.Enabled = driverStore.Type == DriverStoreType.Online;

switch (driverStore.Type)
{
Expand Down Expand Up @@ -415,6 +417,12 @@ private void SetupOptionsMenu()
}

this.optionsStripMenuItem.DropDownItems.Add(this.themeMenu);

this.removeGhostDevicesToolStripMenuItem = new ToolStripMenuItem(Language.ResourceManager.GetString("Menu_File_Remove_Ghost_Devices", Language.Culture));
this.removeGhostDevicesToolStripMenuItem.Click += this.RemoveGhostDevicesToolStripMenuItem_Click;
this.fileToolStripMenuItem.DropDownItems.Insert(
this.fileToolStripMenuItem.DropDownItems.IndexOf(this.toolStripSeparator4),
this.removeGhostDevicesToolStripMenuItem);
}

private void ApplyTheme(IThemeColors palette)
Expand Down Expand Up @@ -1404,6 +1412,96 @@ private void ExportAllDriverListToolStripMenuItem_Click(object sender, EventArgs
ExportDriverList(this.lstDriverStoreEntries.Objects);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Any failure is logged and shown to the user instead of ending the app.")]
private async void RemoveGhostDevicesToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.driverStore.Type != DriverStoreType.Online)
{
return;
}

List<GhostDevice> ghostDevices;
try
{
this.StartOperation();
this.ShowStatus(Status.Normal, Language.ResourceManager.GetString("Status_Scanning_Ghost_Devices", Language.Culture));
ghostDevices = await Task.Run(() => ConfigManager.GetGhostDevices()).ConfigureAwait(true);
}
catch (Exception ex)
{
this.ShowStatus(Status.Error, ex.Message, ex.ToString(), true);
return;
}
finally
{
this.EndOperation();
}

if (ghostDevices.Count == 0)
{
this.ShowStatus(Status.Normal, Language.ResourceManager.GetString("Message_No_Ghost_Devices", Language.Culture), usePopup: true);
return;
}

List<GhostDevice> selected;
using (var form = new GhostDevicesForm(ghostDevices) { RightToLeft = this.RightToLeft, RightToLeftLayout = this.RightToLeftLayout })
{
if (form.ShowDialog(this) != DialogResult.OK)
{
this.UpdateCheckedItemSize();
return;
}

selected = form.SelectedDevices;
}

if (selected.Count == 0
|| DialogResult.OK != this.ShowMessageBox(
string.Format(Language.ResourceManager.GetString("Message_Confirm_Remove_Ghost_Devices", Language.Culture), selected.Count),
Language.Message_Title_Warning,
MessageBoxButtons.OKCancel,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button2))
{
this.UpdateCheckedItemSize();
return;
}

try
{
this.StartOperation();
this.ShowStatus(Status.Normal, Language.ResourceManager.GetString("Status_Removing_Ghost_Devices", Language.Culture));
var failed = await Task.Run(() => selected.Where(device => !ConfigManager.TryRemoveGhostDevice(device.InstanceId)).ToList()).ConfigureAwait(true);

if (failed.Count == 0)
{
this.ShowStatus(
Status.Success,
string.Format(Language.ResourceManager.GetString("Message_Remove_Ghost_Devices_Success", Language.Culture), selected.Count),
usePopup: true);
}
else
{
string message = string.Format(Language.ResourceManager.GetString("Message_Remove_Ghost_Devices_Error", Language.Culture), failed.Count, selected.Count);
this.ShowStatus(
Status.Error,
$"{message}{Environment.NewLine}{string.Join(Environment.NewLine, failed)}",
usePopup: true);
}
}
catch (Exception ex)
{
this.ShowStatus(Status.Error, ex.Message, ex.ToString(), true);
}
finally
{
this.EndOperation();
}

// Packages that only served the removed devices now show up as unused.
await this.PopulateUIWithDriverStoreEntries().ConfigureAwait(true);
}

private void RunAsAdminToolStripMenuItem_Click(object sender, EventArgs e)
{
DSEFormHelper.RunAsAdministrator();
Expand Down Expand Up @@ -1691,6 +1789,7 @@ private void StartOperation()
this.buttonSelectUnusedDrivers.Enabled = false;
this.buttonExportAllDrivers.Enabled = false;
this.chooseDriverStoreToolStripMenuItem.Enabled = false;
this.removeGhostDevicesToolStripMenuItem.Enabled = false;
this.exportAllDriverListToolStripMenuItem.Enabled = false;
this.languageToolStripMenuItem.Enabled = false;
this.optionsStripMenuItem.Enabled = false;
Expand All @@ -1713,6 +1812,7 @@ private void EndOperation()
this.buttonSelectUnusedDrivers.Enabled = this.driverStore.SupportDeviceNameColumn;
this.buttonExportAllDrivers.Enabled = this.driverStore.SupportExportAllDrivers && this.lstDriverStoreEntries.Objects != null;
this.chooseDriverStoreToolStripMenuItem.Enabled = true;
this.removeGhostDevicesToolStripMenuItem.Enabled = this.driverStore.Type == DriverStoreType.Online;
this.exportAllDriverListToolStripMenuItem.Enabled = this.lstDriverStoreEntries.Objects != null;
this.languageToolStripMenuItem.Enabled = true;
this.optionsStripMenuItem.Enabled = true;
Expand Down
99 changes: 99 additions & 0 deletions Rapr/GhostDevicesForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

using Rapr.Lang;
using Rapr.Utils;

namespace Rapr
{
/// <summary>
/// Lists ghost devices with checkboxes so the user picks which ones to remove.
/// Built in code because its texts have no generated Language properties yet.
/// </summary>
public class GhostDevicesForm : Form
{
private readonly ListView listView;
private readonly Button buttonRemove;

public GhostDevicesForm(IEnumerable<GhostDevice> devices)
{
this.Text = GetString("GhostDevices_Title");
this.Font = SystemFonts.MessageBoxFont;
this.ClientSize = new Size(760, 440);
this.MinimumSize = new Size(480, 300);
this.StartPosition = FormStartPosition.CenterParent;
this.MinimizeBox = false;
this.ShowInTaskbar = false;
this.ShowIcon = false;

var layout = new TableLayoutPanel { Dock = DockStyle.Fill, ColumnCount = 1, RowCount = 3, Padding = new Padding(8) };
// A fixed-width column lets the description wrap and keeps the buttons inside the window.
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
layout.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));

var description = new Label
{
AutoSize = true,
Anchor = AnchorStyles.Left | AnchorStyles.Right,
Margin = new Padding(3, 3, 3, 8),
Text = GetString("GhostDevices_Description"),
};

this.listView = new ListView
{
Dock = DockStyle.Fill,
View = View.Details,
CheckBoxes = true,
FullRowSelect = true,
HideSelection = false,
};
this.listView.Columns.Add(GetString("GhostDevices_Column_Name"));
this.listView.Columns.Add(GetString("GhostDevices_Column_Class"));
this.listView.Columns.Add(GetString("GhostDevices_Column_InstanceId"));

foreach (var device in devices.OrderBy(d => d.ClassName).ThenBy(d => d.Name))
{
this.listView.Items.Add(new ListViewItem(new[] { device.Name ?? device.InstanceId, device.ClassName, device.InstanceId })
{
Tag = device,
Checked = device.SelectedByDefault,
});
}

// Column widths need the window handle, so size them once the form is loaded.
this.Load += (sender, e) => this.listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
this.listView.ItemChecked += (sender, e) => this.buttonRemove.Enabled = this.listView.CheckedItems.Count > 0;

this.buttonRemove = new Button
{
AutoSize = true,
DialogResult = DialogResult.OK,
Text = GetString("GhostDevices_Remove"),
Enabled = this.listView.CheckedItems.Count > 0,
};
var buttonCancel = new Button { AutoSize = true, DialogResult = DialogResult.Cancel, Text = Language.Button_Cancel };

var buttons = new FlowLayoutPanel { AutoSize = true, Anchor = AnchorStyles.Right, FlowDirection = FlowDirection.LeftToRight };
buttons.Controls.Add(this.buttonRemove);
buttons.Controls.Add(buttonCancel);

layout.Controls.Add(description, 0, 0);
layout.Controls.Add(this.listView, 0, 1);
layout.Controls.Add(buttons, 0, 2);
this.Controls.Add(layout);

this.AcceptButton = this.buttonRemove;
this.CancelButton = buttonCancel;
DriverStoreTheme.Attach(this);
}

public List<GhostDevice> SelectedDevices =>
this.listView.CheckedItems.Cast<ListViewItem>().Select(item => (GhostDevice)item.Tag).ToList();

private static string GetString(string name) => Language.ResourceManager.GetString(name, Language.Culture) ?? name;
}
}
39 changes: 39 additions & 0 deletions Rapr/Lang/Language.resx
Original file line number Diff line number Diff line change
Expand Up @@ -621,4 +621,43 @@ Continue selecting drivers?</value>
<data name="Theme_Dark" xml:space="preserve">
<value>Dark</value>
</data>
<data name="Menu_File_Remove_Ghost_Devices" xml:space="preserve">
<value>Remove &amp;Ghost Devices...</value>
</data>
<data name="GhostDevices_Title" xml:space="preserve">
<value>Remove Ghost Devices</value>
</data>
<data name="GhostDevices_Description" xml:space="preserve">
<value>These devices are installed but not connected. Removing one deletes its device entry; Windows installs it again if the device is connected later. Software-created devices (ROOT, SWD, HTREE, ...) are not selected by default because some of them are needed even while hidden.</value>
</data>
<data name="GhostDevices_Column_Name" xml:space="preserve">
<value>Device</value>
</data>
<data name="GhostDevices_Column_Class" xml:space="preserve">
<value>Class</value>
</data>
<data name="GhostDevices_Column_InstanceId" xml:space="preserve">
<value>Instance ID</value>
</data>
<data name="GhostDevices_Remove" xml:space="preserve">
<value>&amp;Remove</value>
</data>
<data name="Status_Scanning_Ghost_Devices" xml:space="preserve">
<value>Scanning for ghost devices...</value>
</data>
<data name="Status_Removing_Ghost_Devices" xml:space="preserve">
<value>Removing ghost devices...</value>
</data>
<data name="Message_No_Ghost_Devices" xml:space="preserve">
<value>No ghost devices found.</value>
</data>
<data name="Message_Confirm_Remove_Ghost_Devices" xml:space="preserve">
<value>Remove {0} ghost device(s)? This cannot be undone.</value>
</data>
<data name="Message_Remove_Ghost_Devices_Success" xml:space="preserve">
<value>{0} ghost device(s) removed.</value>
</data>
<data name="Message_Remove_Ghost_Devices_Error" xml:space="preserve">
<value>{0} of {1} ghost device(s) could not be removed:</value>
</data>
</root>
Loading