-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationWindowViewModel.cs
More file actions
513 lines (459 loc) · 16.1 KB
/
ConfigurationWindowViewModel.cs
File metadata and controls
513 lines (459 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Security.Cryptography.X509Certificates;
namespace TaskPilot
{
/// <summary>
/// ViewModel für das Konfigurationsfenster
/// </summary>
public class ConfigurationWindowViewModel : INotifyPropertyChanged
{
private ObservableCollection<ConfigurableProcess> _availableProcesses;
private List<ConfigurableProcess> _allProcesses;
private string _filterText = string.Empty;
private List<MonitoredProgram> _originalMonitoredPrograms;
private int _serverPort;
private bool _serverEnabled;
private bool _httpsEnabled;
private string _certificateThumbprint = string.Empty;
private string _certificateDisplayLabel = string.Empty;
private string _securityPassword = "admin";
public event PropertyChangedEventHandler? PropertyChanged;
public ObservableCollection<ConfigurableProcess> AvailableProcesses
{
get => _availableProcesses;
set
{
if (_availableProcesses != value)
{
_availableProcesses = value;
OnPropertyChanged();
}
}
}
public string FilterText
{
get => _filterText;
set
{
if (_filterText != value)
{
_filterText = value;
OnPropertyChanged();
ApplyFilter();
}
}
}
public int ServerPort
{
get => _serverPort;
set
{
if (_serverPort != value)
{
_serverPort = value;
OnPropertyChanged();
}
}
}
public bool ServerEnabled
{
get => _serverEnabled;
set
{
if (_serverEnabled != value)
{
_serverEnabled = value;
OnPropertyChanged();
}
}
}
public bool HttpsEnabled
{
get => _httpsEnabled;
set
{
if (_httpsEnabled != value)
{
_httpsEnabled = value;
OnPropertyChanged();
}
}
}
public string CertificateThumbprint
{
get => _certificateThumbprint;
set
{
if (_certificateThumbprint != value)
{
_certificateThumbprint = value;
OnPropertyChanged();
UpdateCertificateDisplayLabel();
}
}
}
public string CertificateDisplayLabel
{
get => _certificateDisplayLabel;
set
{
if (_certificateDisplayLabel != value)
{
_certificateDisplayLabel = value;
OnPropertyChanged();
}
}
}
public string SecurityPassword
{
get => _securityPassword;
set
{
if (_securityPassword != value)
{
_securityPassword = value;
OnPropertyChanged();
}
}
}
public ConfigurationWindowViewModel(List<MonitoredProgram> monitoredPrograms, IniConfigReader.ServerSettings serverSettings)
{
_availableProcesses = new ObservableCollection<ConfigurableProcess>();
_allProcesses = new List<ConfigurableProcess>();
_originalMonitoredPrograms = new List<MonitoredProgram>(monitoredPrograms);
_serverPort = serverSettings.Port;
_serverEnabled = serverSettings.Enabled;
_httpsEnabled = serverSettings.HttpsEnabled;
_certificateThumbprint = serverSettings.CertificateThumbprint;
_securityPassword = serverSettings.Password;
UpdateCertificateDisplayLabel();
LoadAvailableProcesses();
}
private void LoadAvailableProcesses()
{
// Lade nur Programme aus der INI-Datei (nicht laufende Prozesse)
_allProcesses.Clear();
foreach (var program in _originalMonitoredPrograms)
{
_allProcesses.Add(new ConfigurableProcess
{
ProcessName = program.ProcessName,
DisplayName = program.DisplayName,
IsSelected = program.IsSelected, // Lese IsSelected aus der INI
Description = program.Description,
AutoRestart = program.AutoRestart,
StartCommand = program.StartCommand
});
}
// Sortiere nach DisplayName
_allProcesses = _allProcesses.OrderBy(p => p.DisplayName).ToList();
ApplyFilter();
}
private void ApplyFilter()
{
List<ConfigurableProcess> source;
if (string.IsNullOrWhiteSpace(_filterText))
{
source = _allProcesses;
}
else
{
source = FilterProcesses(_filterText);
}
// Sortiere nach DisplayName
source = source.OrderBy(p => p.DisplayName).ToList();
_availableProcesses.Clear();
foreach (var process in source)
{
_availableProcesses.Add(process);
}
}
private List<ConfigurableProcess> FilterProcesses(string filterText)
{
// Prüfe ob es ein Wildcard-Pattern ist
if (filterText.Contains("*") || filterText.Contains("?"))
{
return FilterWithWildcards(filterText);
}
else
{
// Normale Teilstring-Suche (Standard)
var filterLower = filterText.ToLowerInvariant();
return _allProcesses
.Where(p => p.DisplayName.ToLowerInvariant().Contains(filterLower) ||
p.ProcessName.ToLowerInvariant().Contains(filterLower))
.ToList();
}
}
private List<ConfigurableProcess> FilterWithWildcards(string pattern)
{
try
{
// Konvertiere das Wildcard-Pattern zu Regex
var regexPattern = WildcardToRegex(pattern);
var regex = new Regex(regexPattern, RegexOptions.IgnoreCase);
return _allProcesses
.Where(p => regex.IsMatch(p.DisplayName) || regex.IsMatch(p.ProcessName))
.ToList();
}
catch
{
// Falls das Regex-Pattern ungültig ist, fall zurück auf normale Suche
var filterLower = pattern.ToLowerInvariant();
return _allProcesses
.Where(p => p.DisplayName.ToLowerInvariant().Contains(filterLower) ||
p.ProcessName.ToLowerInvariant().Contains(filterLower))
.ToList();
}
}
private string WildcardToRegex(string pattern)
{
// Escape alle Regex-Sonderzeichen außer * und ?
var escaped = Regex.Escape(pattern);
// Ersetze die escapten Wildcard-Zeichen mit Regex-Äquivalenten
var regex = escaped
.Replace(@"\*", ".*") // * = beliebig viele Zeichen
.Replace(@"\?", "."); // ? = genau ein Zeichen
// Anchore das Pattern um exakte Übereinstimmung zu ermöglichen
return "^" + regex + "$";
}
public List<MonitoredProgram> GetSelectedPrograms()
{
return _allProcesses
.Where(p => p.IsSelected)
.Select(p => new MonitoredProgram
{
ProcessName = p.ProcessName,
DisplayName = p.DisplayName,
Description = p.Description,
AutoRestart = p.AutoRestart,
StartCommand = p.StartCommand
})
.ToList();
}
public List<MonitoredProgram> GetAllPrograms()
{
return _allProcesses
.Select(p => new MonitoredProgram
{
ProcessName = p.ProcessName,
DisplayName = p.DisplayName,
Description = p.Description,
AutoRestart = p.AutoRestart,
StartCommand = p.StartCommand,
IsSelected = p.IsSelected
})
.ToList();
}
public List<MonitoredProgram> GetDeselectedPrograms()
{
return _allProcesses
.Where(p => !p.IsSelected)
.Select(p => new MonitoredProgram
{
ProcessName = p.ProcessName,
DisplayName = p.DisplayName,
Description = p.Description,
AutoRestart = p.AutoRestart,
StartCommand = p.StartCommand
})
.ToList();
}
public void SelectAllFiltered()
{
foreach (var process in _availableProcesses)
{
process.IsSelected = true;
}
}
public void DeselectAll()
{
foreach (var process in _allProcesses)
{
process.IsSelected = false;
}
}
public IniConfigReader.ServerSettings GetServerSettings()
{
var normalizedThumbprint = (_certificateThumbprint ?? string.Empty)
.Replace(" ", string.Empty)
.Trim();
return new IniConfigReader.ServerSettings
{
Port = _serverPort,
Enabled = _serverEnabled,
HttpsEnabled = _httpsEnabled,
CertificateThumbprint = normalizedThumbprint,
Password = _securityPassword
};
}
private void UpdateCertificateDisplayLabel()
{
try
{
var thumb = (_certificateThumbprint ?? string.Empty).Replace(" ", string.Empty);
if (string.IsNullOrWhiteSpace(thumb))
{
CertificateDisplayLabel = "Kein Zertifikat ausgewählt";
return;
}
// Versuche CurrentUser, dann LocalMachine
var (cert, storeText) = CertificateLookup.FindByThumbprintWithStore(thumb);
if (cert == null)
{
CertificateDisplayLabel = "Zertifikat nicht gefunden";
return;
}
var displayName = !string.IsNullOrWhiteSpace(cert.FriendlyName)
? cert.FriendlyName
: CertificateLookup.ExtractCn(cert.Subject);
CertificateDisplayLabel = $"{storeText} — {displayName}";
}
catch
{
CertificateDisplayLabel = string.Empty;
}
}
private static class CertificateLookup
{
public static (X509Certificate2? cert, string storeText) FindByThumbprintWithStore(string thumbprint)
{
var normalized = (thumbprint ?? string.Empty).Replace(" ", string.Empty).ToUpperInvariant();
var cert = FindInStore(StoreLocation.CurrentUser, out var storeTextCU);
if (cert != null) return (cert, storeTextCU);
cert = FindInStore(StoreLocation.LocalMachine, out var storeTextLM);
if (cert != null) return (cert, storeTextLM);
return (null, string.Empty);
X509Certificate2? FindInStore(StoreLocation location, out string storeText)
{
storeText = location == StoreLocation.CurrentUser ? "CurrentUser\\My" : "LocalMachine\\My";
try
{
using var store = new X509Store(StoreName.My, location);
store.Open(OpenFlags.ReadOnly);
var match = store.Certificates
.Find(X509FindType.FindByThumbprint, normalized, validOnly: false)
.OfType<X509Certificate2>()
.FirstOrDefault();
return match;
}
catch
{
return null;
}
}
}
public static string ExtractCn(string subject)
{
if (string.IsNullOrWhiteSpace(subject)) return string.Empty;
foreach (var part in subject.Split(','))
{
var p = part.Trim();
if (p.StartsWith("CN=", StringComparison.OrdinalIgnoreCase))
{
return p.Substring(3).Trim();
}
}
return subject;
}
}
public void RemoveProcess(ConfigurableProcess process)
{
// Entferne aus der Liste
_allProcesses.Remove(process);
_availableProcesses.Remove(process);
}
public void RefreshProcesses()
{
LoadAvailableProcesses();
FilterText = string.Empty;
}
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
/// <summary>
/// Repräsentiert einen konfigurierbaren Prozess
/// </summary>
public class ConfigurableProcess : INotifyPropertyChanged
{
private bool _isSelected;
private string _displayName = string.Empty;
private string _description = string.Empty;
private bool _autoRestart;
private string _startCommand = string.Empty;
public string ProcessName { get; set; } = string.Empty;
public string DisplayName
{
get => _displayName;
set
{
if (_displayName != value)
{
_displayName = value;
OnPropertyChanged();
}
}
}
public string Description
{
get => _description;
set
{
if (_description != value)
{
_description = value;
OnPropertyChanged();
}
}
}
public bool AutoRestart
{
get => _autoRestart;
set
{
if (_autoRestart != value)
{
_autoRestart = value;
OnPropertyChanged();
}
}
}
public string StartCommand
{
get => _startCommand;
set
{
if (_startCommand != value)
{
_startCommand = value;
OnPropertyChanged();
}
}
}
public bool IsSelected
{
get => _isSelected;
set
{
if (_isSelected != value)
{
_isSelected = value;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}