Skip to content

Commit 535204a

Browse files
committed
Feature: Error message dialog
1 parent 887617a commit 535204a

20 files changed

+359
-86
lines changed

Source/NETworkManager.Models/HostsFileEditor/HostsFileEditor.cs

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,18 @@ private static HostsFileEntryModifyResult EnableEntry(HostsFileEntry entry)
188188
return HostsFileEntryModifyResult.BackupError;
189189
}
190190

191-
// Replace the entry in the hosts file
192-
var hostsFileLines = File.ReadAllLines(HostsFilePath).ToList();
191+
// Enable the entry in the hosts file
192+
List<string> hostsFileLines = [];
193+
194+
try
195+
{
196+
hostsFileLines = File.ReadAllLines(HostsFilePath).ToList();
197+
}
198+
catch (Exception ex)
199+
{
200+
Log.Error($"EnableEntry - Failed to read hosts file: {HostsFilePath}", ex);
201+
return HostsFileEntryModifyResult.ReadError;
202+
}
193203

194204
bool entryFound = false;
195205

@@ -199,7 +209,15 @@ private static HostsFileEntryModifyResult EnableEntry(HostsFileEntry entry)
199209
{
200210
entryFound = true;
201211

202-
hostsFileLines[i] = entry.Line.TrimStart('#', ' ');
212+
hostsFileLines.RemoveAt(i);
213+
hostsFileLines.Insert(i, CreateEntryLine(new HostsFileEntry
214+
{
215+
IsEnabled = true,
216+
IPAddress = entry.IPAddress,
217+
Hostname = entry.Hostname,
218+
Comment = entry.Comment,
219+
Line = entry.Line
220+
}));
203221

204222
break;
205223
}
@@ -250,8 +268,18 @@ private static HostsFileEntryModifyResult DisableEntry(HostsFileEntry entry)
250268
return HostsFileEntryModifyResult.BackupError;
251269
}
252270

253-
// Replace the entry in the hosts file
254-
var hostsFileLines = File.ReadAllLines(HostsFilePath).ToList();
271+
// Disable the entry in the hosts file
272+
List<string> hostsFileLines = [];
273+
274+
try
275+
{
276+
hostsFileLines = File.ReadAllLines(HostsFilePath).ToList();
277+
}
278+
catch (Exception ex)
279+
{
280+
Log.Error($"DisableEntry - Failed to read hosts file: {HostsFilePath}", ex);
281+
return HostsFileEntryModifyResult.ReadError;
282+
}
255283

256284
bool entryFound = false;
257285

@@ -261,7 +289,15 @@ private static HostsFileEntryModifyResult DisableEntry(HostsFileEntry entry)
261289
{
262290
entryFound = true;
263291

264-
hostsFileLines[i] = "# " + entry.Line;
292+
hostsFileLines.RemoveAt(i);
293+
hostsFileLines.Insert(i, CreateEntryLine(new HostsFileEntry
294+
{
295+
IsEnabled = false,
296+
IPAddress = entry.IPAddress,
297+
Hostname = entry.Hostname,
298+
Comment = entry.Comment,
299+
Line = entry.Line
300+
}));
265301

266302
break;
267303
}
@@ -311,8 +347,18 @@ private static HostsFileEntryModifyResult AddEntry(HostsFileEntry entry)
311347
return HostsFileEntryModifyResult.BackupError;
312348
}
313349

314-
// Add the entry to the hosts file
315-
var hostsFileLines = File.ReadAllLines(HostsFilePath).ToList();
350+
// Add the entry to the hosts file
351+
List<string> hostsFileLines = [];
352+
353+
try
354+
{
355+
hostsFileLines = File.ReadAllLines(HostsFilePath).ToList();
356+
}
357+
catch (Exception ex)
358+
{
359+
Log.Error($"AddEntry - Failed to read hosts file: {HostsFilePath}", ex);
360+
return HostsFileEntryModifyResult.ReadError;
361+
}
316362

317363
hostsFileLines.Add(CreateEntryLine(entry));
318364

@@ -357,7 +403,17 @@ public static HostsFileEntryModifyResult EditEntry(HostsFileEntry entry, HostsFi
357403
}
358404

359405
// Replace the entry from the hosts file
360-
var hostsFileLines = File.ReadAllLines(HostsFilePath).ToList();
406+
List<string> hostsFileLines = [];
407+
408+
try
409+
{
410+
hostsFileLines = File.ReadAllLines(HostsFilePath).ToList();
411+
}
412+
catch (Exception ex)
413+
{
414+
Log.Error($"EditEntry - Failed to read hosts file: {HostsFilePath}", ex);
415+
return HostsFileEntryModifyResult.ReadError;
416+
}
361417

362418
bool entryFound = false;
363419

@@ -419,7 +475,17 @@ private static HostsFileEntryModifyResult DeleteEntry(HostsFileEntry entry)
419475
}
420476

421477
// Remove the entry from the hosts file
422-
var hostsFileLines = File.ReadAllLines(HostsFilePath).ToList();
478+
List<string> hostsFileLines = [];
479+
480+
try
481+
{
482+
hostsFileLines = File.ReadAllLines(HostsFilePath).ToList();
483+
}
484+
catch (Exception ex)
485+
{
486+
Log.Error($"DeleteEntry - Failed to read hosts file: {HostsFilePath}", ex);
487+
return HostsFileEntryModifyResult.ReadError;
488+
}
423489

424490
bool entryFound = false;
425491

Source/NETworkManager.Models/HostsFileEditor/HostsFileEntryModifyResult.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public enum HostsFileEntryModifyResult
1515
/// </summary>
1616
NotFound,
1717

18+
/// <summary>
19+
/// An error occurred while reading the hosts file.
20+
/// </summary>
21+
ReadError,
22+
1823
/// <summary>
1924
/// An error occurred while writing to the hosts file.
2025
/// </summary>

Source/NETworkManager/ProfileDialogManager.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,9 @@ public static Task ShowDeleteProfileDialog(Window parentWindow, IProfileManagerM
583583

584584
viewModel.OnProfileManagerDialogClose();
585585
},
586-
profiles.Count == 1
587-
? Strings.DeleteProfileMessage
588-
: Strings.DeleteProfilesMessage);
586+
profiles.Count == 1 ? Strings.DeleteProfileMessage : Strings.DeleteProfilesMessage,
587+
Strings.Delete
588+
);
589589

590590
childWindow.Title = Strings.DeleteProfile;
591591

@@ -684,7 +684,10 @@ public static Task ShowDeleteGroupDialog(Window parentWindow, IProfileManagerMin
684684
Settings.ConfigurationManager.Current.IsChildWindowOpen = false;
685685

686686
viewModel.OnProfileManagerDialogClose();
687-
}, Strings.DeleteGroupMessage);
687+
},
688+
Strings.DeleteGroupMessage,
689+
Strings.Delete
690+
);
688691

689692
childWindow.Title = Strings.DeleteGroup;
690693

Source/NETworkManager/ViewModels/AWSSessionManagerSettingsViewModel.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
using System;
1+
using MahApps.Metro.Controls.Dialogs;
2+
using MahApps.Metro.SimpleChildWindow;
3+
using NETworkManager.Localization.Resources;
4+
using NETworkManager.Models.AWS;
5+
using NETworkManager.Settings;
6+
using NETworkManager.Utilities;
7+
using NETworkManager.Views;
8+
using System;
29
using System.ComponentModel;
310
using System.Diagnostics;
411
using System.IO;
512
using System.Threading.Tasks;
613
using System.Windows;
714
using System.Windows.Data;
815
using System.Windows.Input;
9-
using MahApps.Metro.Controls.Dialogs;
10-
using MahApps.Metro.SimpleChildWindow;
11-
using NETworkManager.Localization.Resources;
12-
using NETworkManager.Models.AWS;
13-
using NETworkManager.Settings;
14-
using NETworkManager.Utilities;
15-
using NETworkManager.Views;
1616

1717
namespace NETworkManager.ViewModels;
1818

@@ -213,7 +213,7 @@ private void BrowseFileAction()
213213
Filter = GlobalStaticConfiguration.ApplicationFileExtensionFilter
214214
};
215215

216-
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
216+
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
217217
ApplicationFilePath = openFileDialog.FileName;
218218
}
219219

@@ -290,15 +290,16 @@ private Task DeleteAWSProfile()
290290
childWindow.IsOpen = false;
291291
ConfigurationManager.Current.IsChildWindowOpen = false;
292292
},
293-
Strings.DeleteAWSProfileMessage
294-
);
293+
Strings.DeleteAWSProfileMessage,
294+
Strings.Delete
295+
);
295296

296297
childWindow.Title = Strings.DeleteAWSProfile;
297-
298+
298299
childWindow.DataContext = childWindowViewModel;
299-
300+
300301
ConfigurationManager.Current.IsChildWindowOpen = true;
301-
302+
302303
return (Application.Current.MainWindow as MainWindow).ShowChildWindowAsync(childWindow);
303304
}
304305

Source/NETworkManager/ViewModels/DNSLookupSettingsViewModel.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DnsClient;
22
using MahApps.Metro.Controls.Dialogs;
3+
using MahApps.Metro.SimpleChildWindow;
34
using NETworkManager.Localization.Resources;
45
using NETworkManager.Models.Network;
56
using NETworkManager.Settings;
@@ -13,7 +14,6 @@
1314
using System.Windows;
1415
using System.Windows.Data;
1516
using System.Windows.Input;
16-
using MahApps.Metro.SimpleChildWindow;
1717

1818
namespace NETworkManager.ViewModels;
1919

@@ -372,15 +372,16 @@ private Task DeleteDNSServer()
372372
childWindow.IsOpen = false;
373373
ConfigurationManager.Current.IsChildWindowOpen = false;
374374
},
375-
Strings.DeleteDNSServerMessage
376-
);
375+
Strings.DeleteDNSServerMessage,
376+
Strings.Delete
377+
);
377378

378379
childWindow.Title = Strings.DeleteDNSServer;
379-
380+
380381
childWindow.DataContext = childWindowViewModel;
381-
382+
382383
ConfigurationManager.Current.IsChildWindowOpen = true;
383-
384+
384385
return (Application.Current.MainWindow as MainWindow).ShowChildWindowAsync(childWindow);
385386
}
386387

Source/NETworkManager/ViewModels/HostsFileEditorViewModel.cs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,10 @@ private async Task EnableEntryAction()
271271
{
272272
IsModifying = true;
273273

274-
await HostsFileEditor.EnableEntryAsync(SelectedResult);
274+
var result = await HostsFileEditor.EnableEntryAsync(SelectedResult);
275+
276+
if (result != HostsFileEntryModifyResult.Success)
277+
await ShowErrorMessageAsync(result);
275278

276279
IsModifying = false;
277280
}
@@ -282,7 +285,10 @@ private async Task DisableEntryAction()
282285
{
283286
IsModifying = true;
284287

285-
await HostsFileEditor.DisableEntryAsync(SelectedResult);
288+
var result = await HostsFileEditor.DisableEntryAsync(SelectedResult);
289+
290+
if (result != HostsFileEntryModifyResult.Success)
291+
await ShowErrorMessageAsync(result);
286292

287293
IsModifying = false;
288294
}
@@ -300,14 +306,17 @@ private async Task AddEntryAction()
300306
childWindow.IsOpen = false;
301307
ConfigurationManager.Current.IsChildWindowOpen = false;
302308

303-
await HostsFileEditor.AddEntryAsync(new HostsFileEntry
309+
var result = await HostsFileEditor.AddEntryAsync(new HostsFileEntry
304310
{
305311
IsEnabled = instance.IsEnabled,
306312
IPAddress = instance.IPAddress,
307313
Hostname = instance.Hostname,
308314
Comment = instance.Comment
309315
});
310316

317+
if (result != HostsFileEntryModifyResult.Success)
318+
await ShowErrorMessageAsync(result);
319+
311320
IsModifying = false;
312321
}, _ =>
313322
{
@@ -326,6 +335,34 @@ await HostsFileEditor.AddEntryAsync(new HostsFileEntry
326335
await (Application.Current.MainWindow as MainWindow).ShowChildWindowAsync(childWindow);
327336
}
328337

338+
private async Task ShowErrorMessageAsync(HostsFileEntryModifyResult result)
339+
{
340+
string message = result switch
341+
{
342+
HostsFileEntryModifyResult.ReadError => Strings.HostsFileReadErrorMessage,
343+
HostsFileEntryModifyResult.WriteError => Strings.HostsFileWriteErrorMessage,
344+
HostsFileEntryModifyResult.NotFound => Strings.HostsFileEntryNotFoundMessage,
345+
HostsFileEntryModifyResult.BackupError => Strings.HostsFileBackupErrorMessage,
346+
_ => Strings.UnkownError
347+
};
348+
349+
var childWindow = new OKMessageChildWindow();
350+
351+
var childWindowViewModel = new OKMessageViewModel(_ =>
352+
{
353+
childWindow.IsOpen = false;
354+
ConfigurationManager.Current.IsChildWindowOpen = false;
355+
}, message);
356+
357+
childWindow.Title = Strings.Error;
358+
359+
childWindow.DataContext = childWindowViewModel;
360+
361+
ConfigurationManager.Current.IsChildWindowOpen = true;
362+
363+
await (Application.Current.MainWindow as MainWindow).ShowChildWindowAsync(childWindow);
364+
}
365+
329366
public ICommand EditEntryCommand => new RelayCommand(_ => EditEntryAction().ConfigureAwait(false), ModifyEntry_CanExecute);
330367

331368
private async Task EditEntryAction()
@@ -339,14 +376,17 @@ private async Task EditEntryAction()
339376
childWindow.IsOpen = false;
340377
ConfigurationManager.Current.IsChildWindowOpen = false;
341378

342-
await HostsFileEditor.EditEntryAsync(instance.Entry, new HostsFileEntry
379+
var result = await HostsFileEditor.EditEntryAsync(instance.Entry, new HostsFileEntry
343380
{
344381
IsEnabled = instance.IsEnabled,
345382
IPAddress = instance.IPAddress,
346383
Hostname = instance.Hostname,
347384
Comment = instance.Comment
348385
});
349386

387+
if (result != HostsFileEntryModifyResult.Success)
388+
await ShowErrorMessageAsync(result);
389+
350390
IsModifying = false;
351391
}, _ =>
352392
{
@@ -378,7 +418,10 @@ private async Task DeleteEntryAction()
378418
childWindow.IsOpen = false;
379419
ConfigurationManager.Current.IsChildWindowOpen = false;
380420

381-
await HostsFileEditor.DeleteEntryAsync(SelectedResult);
421+
var result = await HostsFileEditor.DeleteEntryAsync(SelectedResult);
422+
423+
if (result != HostsFileEntryModifyResult.Success)
424+
await ShowErrorMessageAsync(result);
382425

383426
IsModifying = false;
384427
}, _ =>
@@ -387,7 +430,10 @@ private async Task DeleteEntryAction()
387430
ConfigurationManager.Current.IsChildWindowOpen = false;
388431

389432
IsModifying = false;
390-
}, string.Format(Strings.DeleteHostsFileEntryMessage, SelectedResult.IPAddress, SelectedResult.Hostname, string.IsNullOrEmpty(SelectedResult.Comment) ? "" : $"# {SelectedResult.Comment}"));
433+
},
434+
string.Format(Strings.DeleteHostsFileEntryMessage, SelectedResult.IPAddress, SelectedResult.Hostname, string.IsNullOrEmpty(SelectedResult.Comment) ? "" : $"# {SelectedResult.Comment}"),
435+
Strings.Delete
436+
);
391437

392438
childWindow.Title = Strings.DeleteEntry;
393439

0 commit comments

Comments
 (0)