feat: add ActivityLogForm with color-coded DataGridView
This commit is contained in:
95
src/SpamGuard/Tray/ActivityLogForm.cs
Normal file
95
src/SpamGuard/Tray/ActivityLogForm.cs
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
namespace SpamGuard.Tray;
|
||||||
|
|
||||||
|
using SpamGuard.Models;
|
||||||
|
using SpamGuard.Services;
|
||||||
|
|
||||||
|
public sealed class ActivityLogForm : Form
|
||||||
|
{
|
||||||
|
private readonly ActivityLog _activityLog;
|
||||||
|
private readonly DataGridView _grid;
|
||||||
|
|
||||||
|
public ActivityLogForm(ActivityLog activityLog)
|
||||||
|
{
|
||||||
|
_activityLog = activityLog;
|
||||||
|
|
||||||
|
Text = "SpamGuard - Activity Log";
|
||||||
|
Size = new System.Drawing.Size(800, 500);
|
||||||
|
StartPosition = FormStartPosition.CenterScreen;
|
||||||
|
Icon = IconGenerator.Green;
|
||||||
|
|
||||||
|
_grid = new DataGridView
|
||||||
|
{
|
||||||
|
Dock = DockStyle.Fill,
|
||||||
|
ReadOnly = true,
|
||||||
|
AllowUserToAddRows = false,
|
||||||
|
AllowUserToDeleteRows = false,
|
||||||
|
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
|
||||||
|
SelectionMode = DataGridViewSelectionMode.FullRowSelect,
|
||||||
|
BackgroundColor = System.Drawing.SystemColors.Window,
|
||||||
|
BorderStyle = BorderStyle.None
|
||||||
|
};
|
||||||
|
|
||||||
|
_grid.Columns.Add("Time", "Time");
|
||||||
|
_grid.Columns.Add("From", "From");
|
||||||
|
_grid.Columns.Add("Subject", "Subject");
|
||||||
|
_grid.Columns.Add("Verdict", "Verdict");
|
||||||
|
_grid.Columns.Add("Confidence", "Confidence");
|
||||||
|
_grid.Columns.Add("Reason", "Reason");
|
||||||
|
|
||||||
|
_grid.Columns["Time"]!.Width = 70;
|
||||||
|
_grid.Columns["From"]!.Width = 150;
|
||||||
|
_grid.Columns["Verdict"]!.Width = 80;
|
||||||
|
_grid.Columns["Confidence"]!.Width = 80;
|
||||||
|
|
||||||
|
Controls.Add(_grid);
|
||||||
|
|
||||||
|
RefreshData();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RefreshData()
|
||||||
|
{
|
||||||
|
if (InvokeRequired)
|
||||||
|
{
|
||||||
|
Invoke(RefreshData);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var entries = _activityLog.GetRecent(200);
|
||||||
|
_grid.Rows.Clear();
|
||||||
|
|
||||||
|
foreach (var entry in entries)
|
||||||
|
{
|
||||||
|
var rowIndex = _grid.Rows.Add(
|
||||||
|
entry.Timestamp.ToLocalTime().ToString("HH:mm:ss"),
|
||||||
|
entry.Sender,
|
||||||
|
entry.Subject,
|
||||||
|
entry.Verdict.ToString(),
|
||||||
|
entry.Confidence?.ToString("P0") ?? "--",
|
||||||
|
entry.Reason ?? ""
|
||||||
|
);
|
||||||
|
|
||||||
|
var row = _grid.Rows[rowIndex];
|
||||||
|
row.DefaultCellStyle.ForeColor = entry.Verdict switch
|
||||||
|
{
|
||||||
|
Verdict.Spam => System.Drawing.Color.Red,
|
||||||
|
Verdict.Trusted => System.Drawing.Color.Green,
|
||||||
|
Verdict.Uncertain => System.Drawing.Color.Orange,
|
||||||
|
Verdict.Error => System.Drawing.Color.Gray,
|
||||||
|
_ => System.Drawing.SystemColors.ControlText
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnFormClosing(FormClosingEventArgs e)
|
||||||
|
{
|
||||||
|
if (e.CloseReason == CloseReason.UserClosing)
|
||||||
|
{
|
||||||
|
e.Cancel = true;
|
||||||
|
Hide();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
base.OnFormClosing(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user