Add Ordner-Überwachung Tab in Config-Tool

This commit is contained in:
administrator 2026-04-19 21:46:52 +02:00
parent 92e2767f76
commit 10ef84a73c

View file

@ -20,7 +20,7 @@ public class MainForm : Form
private CheckBox chkBindAll = null!;
// ── Grids ─────────────────────────────────────────────────────
private DataGridView gridProfiles = null!, gridAccounts = null!;
private DataGridView gridProfiles = null!, gridAccounts = null!, gridFolders = null!;
// ── Filter ────────────────────────────────────────────────────
private TextBox txtGlobalAllowed = null!, txtGlobalBlocked = null!;
@ -57,6 +57,7 @@ public class MainForm : Form
var tabs = new TabControl { Dock = DockStyle.Fill };
tabs.TabPages.Add(BuildProfilesTab());
tabs.TabPages.Add(BuildAccountsTab());
tabs.TabPages.Add(BuildFoldersTab());
tabs.TabPages.Add(BuildFilterTab());
tabs.TabPages.Add(BuildApiTab());
tabs.TabPages.Add(BuildGeneralTab());
@ -177,6 +178,61 @@ public class MainForm : Form
return tab;
}
// ── Tab: Ordner-Überwachung ────────────────────────────────────
private TabPage BuildFoldersTab()
{
var tab = new TabPage("Ordner-Überwachung");
tab.Controls.Add(new Label
{
Text = "PDFs in diesen Ordnern werden automatisch erkannt und gedruckt.",
Left = Pad, Top = Pad, Width = 1060, Height = 18, ForeColor = Color.DimGray
});
gridFolders = new DataGridView
{
Left = Pad, Top = 32, Width = 1060, Height = 500,
Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom,
AllowUserToAddRows = true, AllowUserToDeleteRows = false,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None,
ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize,
EditMode = DataGridViewEditMode.EditOnEnter,
SelectionMode = DataGridViewSelectionMode.FullRowSelect,
ScrollBars = ScrollBars.Both
};
gridFolders.Columns.Add(new DataGridViewTextBoxColumn { Name = "FName", HeaderText = "Name", Width = 120 });
gridFolders.Columns.Add(new DataGridViewTextBoxColumn { Name = "FPath", HeaderText = "Pfad", Width = 400 });
gridFolders.Columns.Add(new DataGridViewButtonColumn { Name = "FBrowse", HeaderText = "", Text = "…", Width = 30, UseColumnTextForButtonValue = true });
gridFolders.Columns.Add(new DataGridViewCheckBoxColumn { Name = "FSubfolders",HeaderText = "Unterordner", Width = 90 });
gridFolders.Columns.Add(new DataGridViewCheckBoxColumn { Name = "FDelete", HeaderText = "Nach Druck löschen",Width = 120 });
var colFProfile = new DataGridViewComboBoxColumn { Name = "FProfile", HeaderText = "Drucker-Profil", FlatStyle = FlatStyle.Flat, Width = 180 };
colFProfile.Items.Add("");
gridFolders.Columns.Add(colFProfile);
gridFolders.DataError += (_, e) => e.ThrowException = false;
gridFolders.CurrentCellDirtyStateChanged += (_, _) =>
{
if (gridFolders.IsCurrentCellDirty) gridFolders.CommitEdit(DataGridViewDataErrorContexts.Commit);
};
// "…"-Button → Ordner auswählen
gridFolders.CellClick += (_, e) =>
{
if (e.RowIndex < 0 || gridFolders.Columns[e.ColumnIndex].Name != "FBrowse") return;
using var d = new FolderBrowserDialog { Description = "Ordner wählen" };
var current = gridFolders.Rows[e.RowIndex].Cells["FPath"].Value?.ToString();
if (!string.IsNullOrEmpty(current) && Directory.Exists(current)) d.SelectedPath = current;
if (d.ShowDialog() == DialogResult.OK)
gridFolders.Rows[e.RowIndex].Cells["FPath"].Value = d.SelectedPath;
};
gridProfiles.CellValueChanged += (_, _) => RefreshProfileDropdowns();
AttachContextMenu(gridFolders);
tab.Controls.Add(gridFolders);
return tab;
}
// ── Tab: Filter ───────────────────────────────────────────────
private TabPage BuildFilterTab()
{
@ -259,7 +315,7 @@ public class MainForm : Form
return tab;
}
// ── Tab: Über ───────────────────────────────────────────────────
// ── Tab: Über ─────────────────────────────────────────────────
private TabPage BuildAboutTab()
{
var tab = new TabPage("Über");
@ -359,21 +415,26 @@ public class MainForm : Form
.Select(r => r.Cells["PName"].Value?.ToString() ?? "")
.Where(n => n.Length > 0).ToList();
var col = (DataGridViewComboBoxColumn)gridAccounts.Columns["Profile"];
var current = col.Items.Cast<object>().Select(o => o.ToString()).ToList();
// nur hinzufügen was noch nicht drin ist, nichts entfernen (bestehende Werte bleiben gültig)
col.Items.Clear();
col.Items.Add("");
foreach (var n in names) col.Items.Add(n);
// bestehende Zellwerte die nicht mehr in der Liste sind trotzdem erhalten
// Postfächer
var colA = (DataGridViewComboBoxColumn)gridAccounts.Columns["Profile"];
colA.Items.Clear(); colA.Items.Add("");
foreach (var n in names) colA.Items.Add(n);
foreach (DataGridViewRow row in gridAccounts.Rows)
{
if (row.IsNewRow) continue;
var val = row.Cells["Profile"].Value?.ToString() ?? "";
if (!string.IsNullOrEmpty(val) && !col.Items.Contains(val))
col.Items.Add(val);
if (!string.IsNullOrEmpty(val) && !colA.Items.Contains(val)) colA.Items.Add(val);
}
// Ordner-Überwachung
var colF = (DataGridViewComboBoxColumn)gridFolders.Columns["FProfile"];
colF.Items.Clear(); colF.Items.Add("");
foreach (var n in names) colF.Items.Add(n);
foreach (DataGridViewRow row in gridFolders.Rows)
{
if (row.IsNewRow) continue;
var val = row.Cells["FProfile"].Value?.ToString() ?? "";
if (!string.IsNullOrEmpty(val) && !colF.Items.Contains(val)) colF.Items.Add(val);
}
}
@ -414,7 +475,6 @@ public class MainForm : Form
txtSumatraPath.Text = mp["SumatraPath"]?.ToString() ?? "";
txtTempDir.Text = mp["TempDirectory"]?.ToString() ?? "";
// Globale Filter
txtGlobalAllowed.Text = string.Join(Environment.NewLine,
(mp["AllowedSenders"] as JArray ?? new JArray()).Select(t => t.ToString()));
txtGlobalBlocked.Text = string.Join(Environment.NewLine,
@ -480,6 +540,24 @@ public class MainForm : Form
profName);
}
// Ordner-Überwachung
gridFolders.Rows.Clear();
foreach (var f in mp["FolderWatchers"] as JArray ?? new JArray())
{
var profName = f["PrinterProfileName"]?.ToString() ?? "";
var profCol = (DataGridViewComboBoxColumn)gridFolders.Columns["FProfile"];
if (!string.IsNullOrEmpty(profName) && !profCol.Items.Contains(profName))
profCol.Items.Add(profName);
gridFolders.Rows.Add(
f["Name"]?.ToString() ?? "",
f["Path"]?.ToString() ?? "",
"…",
f["IncludeSubfolders"]?.Value<bool>() ?? false,
f["DeleteAfterPrint"]?.Value<bool>() ?? true,
profName);
}
SetStatus($"Geladen: {txtConfigPath.Text}", Color.DarkGreen);
}
catch (Exception ex) { SetStatus($"Fehler: {ex.Message}", Color.Red); }
@ -489,6 +567,7 @@ public class MainForm : Form
{
gridProfiles.EndEdit();
gridAccounts.EndEdit();
gridFolders.EndEdit();
var path = txtConfigPath.Text;
var root = File.Exists(path) ? JObject.Parse(File.ReadAllText(path)) : new JObject();
@ -526,6 +605,20 @@ public class MainForm : Form
});
}
var folders = new JArray();
foreach (DataGridViewRow r in gridFolders.Rows)
{
if (r.IsNewRow) continue;
folders.Add(new JObject
{
["Name"] = r.Cells["FName"].Value?.ToString() ?? "",
["Path"] = r.Cells["FPath"].Value?.ToString() ?? "",
["IncludeSubfolders"] = r.Cells["FSubfolders"].Value is true,
["DeleteAfterPrint"] = r.Cells["FDelete"].Value is true,
["PrinterProfileName"] = r.Cells["FProfile"].Value?.ToString() ?? ""
});
}
root["MailPrint"] = new JObject
{
["PollIntervalSeconds"] = int.TryParse(txtInterval.Text, out int iv) ? iv : 60,
@ -539,6 +632,7 @@ public class MainForm : Form
["BlockedSenders"] = ToJArray(txtGlobalBlocked.Text, multiline: true),
["PrinterProfiles"] = profiles,
["Accounts"] = accounts,
["FolderWatchers"] = folders,
["WebApi"] = new JObject
{
["Port"] = int.TryParse(txtApiPort.Text, out int ap) ? ap : 5100,
@ -559,7 +653,6 @@ public class MainForm : Form
catch (Exception ex) { SetStatus($"Fehler: {ex.Message}", Color.Red); }
}
// Komma- oder Zeilengetrennte Liste → JArray
private static JArray ToJArray(string? input, bool multiline = false)
{
if (string.IsNullOrWhiteSpace(input)) return new JArray();