Skip to content

Commit 3e38697

Browse files
committed
Update
1 parent 1a485af commit 3e38697

File tree

8 files changed

+126
-46
lines changed

8 files changed

+126
-46
lines changed

Source/NETworkManager/NETworkManager.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@
312312
<Compile Include="StatusWindow.xaml.cs">
313313
<DependentUpon>StatusWindow.xaml</DependentUpon>
314314
</Compile>
315+
<Compile Include="Utilities\CustomCommand.cs" />
315316
<Compile Include="Utilities\CustomCommandInfo.cs" />
316317
<Compile Include="Utilities\CommonMethods.cs" />
317318
<Compile Include="Utilities\ArrayHelper.cs" />
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Windows;
4+
5+
namespace NETworkManager.Utilities
6+
{
7+
public static class CustomCommand
8+
{
9+
public static void Run(CustomCommandInfo info)
10+
{
11+
try
12+
{
13+
if (string.IsNullOrEmpty(info.Arguments))
14+
Process.Start(info.FilePath);
15+
else
16+
Process.Start(info.FilePath, info.Arguments);
17+
}
18+
catch (Exception ex)
19+
{
20+
MessageBox.Show(ex.Message, "Error in Utilities.CustomCommand.Run()", MessageBoxButton.OK, MessageBoxImage.Error);
21+
}
22+
}
23+
}
24+
}
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
1-
namespace NETworkManager.Utilities
1+
using System;
2+
3+
namespace NETworkManager.Utilities
24
{
3-
public class CustomCommandInfo
5+
public class CustomCommandInfo : ICloneable
46
{
7+
public Guid ID { get; set; }
8+
59
public string Name { get; set; }
610
public string FilePath { get; set; }
711
public string Arguments { get; set; }
812

913
public CustomCommandInfo()
1014
{
11-
15+
ID = Guid.NewGuid();
1216
}
1317

14-
public CustomCommandInfo(string name, string filePath)
18+
public CustomCommandInfo(Guid id,string name, string filePath)
1519
{
20+
ID = id;
1621
Name = name;
1722
FilePath = filePath;
1823
}
1924

20-
public CustomCommandInfo(string name, string filePath, string arguments) : this(name, filePath)
25+
public CustomCommandInfo(Guid id, string name, string filePath, string arguments) : this(id, name, filePath)
2126
{
2227
Arguments = arguments;
2328
}
29+
30+
public object Clone()
31+
{
32+
return MemberwiseClone();
33+
}
2434
}
2535
}

Source/NETworkManager/ViewModels/CredentialViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public CredentialViewModel(Action<CredentialViewModel> saveCommand, Action<Crede
138138

139139
_isEdited = isEdited;
140140

141+
// Create new --> GUID
141142
_credentialInfo = credentialInfo ?? new CredentialInfo();
142143

143144
Id = _credentialInfo.ID;

Source/NETworkManager/ViewModels/CustomCommandViewModel.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ public class CustomCommandViewModel : ViewModelBase
1212

1313
public ICommand CancelCommand { get; }
1414

15+
private Guid _id;
16+
public Guid ID
17+
{
18+
get => _id;
19+
set
20+
{
21+
if (_id == value)
22+
return;
23+
24+
_id = value;
25+
OnPropertyChanged();
26+
}
27+
}
28+
1529
private string _name;
1630
public string Name
1731
{
@@ -67,8 +81,7 @@ public string Arguments
6781
}
6882

6983
private readonly CustomCommandInfo _info;
70-
71-
84+
7285
private bool _infoChanged;
7386
public bool InfoChanged
7487
{
@@ -106,8 +119,10 @@ public CustomCommandViewModel(Action<CustomCommandViewModel> saveCommand, Action
106119

107120
_isEdited = isEdited;
108121

122+
// Create new --> GUID
109123
_info = info ?? new CustomCommandInfo();
110124

125+
ID = _info.ID;
111126
Name = _info.Name;
112127
FilePath = _info.FilePath;
113128
Arguments = _info.Arguments;

Source/NETworkManager/ViewModels/IPScannerSettingsViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ public async void AddCustomCommand()
385385
{
386386
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);
387387

388-
SettingsManager.Current.IPScanner_CustomCommands.Add(new CustomCommandInfo(instance.Name, instance.FilePath, instance.Arguments));
388+
SettingsManager.Current.IPScanner_CustomCommands.Add(new CustomCommandInfo(instance.ID ,instance.Name, instance.FilePath, instance.Arguments));
389389
}, instance =>
390390
{
391391
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);
@@ -411,7 +411,7 @@ public async void EditCustomCommand()
411411
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);
412412

413413
SettingsManager.Current.IPScanner_CustomCommands.Remove(SelectedCustomCommand);
414-
SettingsManager.Current.IPScanner_CustomCommands.Add(new CustomCommandInfo(instance.Name, instance.FilePath, instance.Arguments));
414+
SettingsManager.Current.IPScanner_CustomCommands.Add(new CustomCommandInfo(instance.ID, instance.Name, instance.FilePath, instance.Arguments));
415415
}, instance =>
416416
{
417417
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);

Source/NETworkManager/ViewModels/IPScannerViewModel.cs

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using NETworkManager.Views;
2323
using NETworkManager.Models.EventSystem;
2424
using NETworkManager.Enum;
25+
using System.Text.RegularExpressions;
2526

2627
namespace NETworkManager.ViewModels
2728
{
@@ -257,6 +258,8 @@ public DateTime? EndTime
257258
}
258259
}
259260

261+
public IEnumerable<CustomCommandInfo> CustomCommands => SettingsManager.Current.IPScanner_CustomCommands;
262+
260263
private bool _expandStatistics;
261264
public bool ExpandStatistics
262265
{
@@ -365,6 +368,64 @@ private void PerformDNSLookupHostnameAction()
365368
EventSystem.RedirectDataToApplication(ApplicationViewManager.Name.DNSLookup, SelectedHostResult.Hostname);
366369
}
367370

371+
public ICommand CustomCommandCommand => new RelayCommand(CustomCommandAction);
372+
373+
private void CustomCommandAction(object guid)
374+
{
375+
Debug.WriteLine(guid.ToString());
376+
377+
if (guid is Guid id)
378+
{
379+
CustomCommandInfo info = (CustomCommandInfo)CustomCommands.FirstOrDefault(x => x.ID == id).Clone();
380+
381+
if (info == null)
382+
return; // ToDo: Log and error message
383+
384+
// Replace vars
385+
string hostname = !string.IsNullOrEmpty(SelectedHostResult.Hostname) ? SelectedHostResult.Hostname.TrimEnd('.') : "";
386+
string ipAddress = SelectedHostResult.PingInfo.IPAddress.ToString();
387+
388+
info.FilePath = Regex.Replace(info.FilePath, "\\$\\$hostname\\$\\$", hostname, RegexOptions.IgnoreCase);
389+
info.FilePath = Regex.Replace(info.FilePath, "\\$\\$ipaddress\\$\\$", ipAddress , RegexOptions.IgnoreCase);
390+
info.Arguments = Regex.Replace(info.Arguments, "\\$\\$hostname\\$\\$", hostname, RegexOptions.IgnoreCase);
391+
info.Arguments = Regex.Replace(info.Arguments, "\\$\\$ipaddress\\$\\$", ipAddress, RegexOptions.IgnoreCase);
392+
393+
CustomCommand.Run(info);
394+
}
395+
}
396+
397+
public ICommand AddProfileSelectedHostCommand => new RelayCommand(p => AddProfileSelectedHostAction());
398+
private async void AddProfileSelectedHostAction()
399+
{
400+
ProfileInfo profileInfo = new ProfileInfo()
401+
{
402+
Name = string.IsNullOrEmpty(SelectedHostResult.Hostname) ? SelectedHostResult.PingInfo.IPAddress.ToString() : SelectedHostResult.Hostname.TrimEnd('.'),
403+
Host = SelectedHostResult.PingInfo.IPAddress.ToString()
404+
};
405+
406+
var customDialog = new CustomDialog
407+
{
408+
Title = Resources.Localization.Strings.AddProfile
409+
};
410+
411+
var profileViewModel = new ProfileViewModel(instance =>
412+
{
413+
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);
414+
415+
ProfileManager.AddProfile(instance);
416+
}, instance =>
417+
{
418+
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);
419+
}, ProfileManager.GetGroups(), ProfileEditMode.Add, profileInfo);
420+
421+
customDialog.Content = new ProfileDialog
422+
{
423+
DataContext = profileViewModel
424+
};
425+
426+
await _dialogCoordinator.ShowMetroDialogAsync(this, customDialog);
427+
}
428+
368429
public ICommand CopySelectedIPAddressCommand => new RelayCommand(p => CopySelectedIPAddressAction());
369430

370431
private void CopySelectedIPAddressAction()
@@ -421,39 +482,6 @@ private void CopySelectedStatusAction()
421482
CommonMethods.SetClipboard(LocalizationManager.TranslateIPStatus(SelectedHostResult.PingInfo.Status));
422483
}
423484

424-
public ICommand AddProfileSelectedHostCommand => new RelayCommand(p => AddProfileSelectedHostAction());
425-
426-
private async void AddProfileSelectedHostAction()
427-
{
428-
ProfileInfo profileInfo = new ProfileInfo()
429-
{
430-
Name = string.IsNullOrEmpty(SelectedHostResult.Hostname) ? SelectedHostResult.PingInfo.IPAddress.ToString() : SelectedHostResult.Hostname.TrimEnd('.'),
431-
Host = SelectedHostResult.PingInfo.IPAddress.ToString()
432-
};
433-
434-
var customDialog = new CustomDialog
435-
{
436-
Title = Resources.Localization.Strings.AddProfile
437-
};
438-
439-
var profileViewModel = new ProfileViewModel(instance =>
440-
{
441-
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);
442-
443-
ProfileManager.AddProfile(instance);
444-
}, instance =>
445-
{
446-
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);
447-
}, ProfileManager.GetGroups(), ProfileEditMode.Add, profileInfo);
448-
449-
customDialog.Content = new ProfileDialog
450-
{
451-
DataContext = profileViewModel
452-
};
453-
454-
await _dialogCoordinator.ShowMetroDialogAsync(this, customDialog);
455-
}
456-
457485
public ICommand ExportCommand => new RelayCommand(p => ExportAction());
458486

459487
private void ExportAction()

Source/NETworkManager/Views/IPScannerView.xaml.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
2-
using System.Diagnostics;
32
using System.Windows.Controls;
43
using MahApps.Metro.Controls.Dialogs;
4+
using NETworkManager.Utilities;
55
using NETworkManager.ViewModels;
66

77
namespace NETworkManager.Views
@@ -63,9 +63,10 @@ private void ContextMenu_Opened(object sender, System.Windows.RoutedEventArgs e)
6363
((MenuItem)menu.Items[index]).Items.Clear();
6464

6565
// Add items to custom commands
66-
foreach (string str in new string[] { "Internet Explorer","Google Chrome", "PowerShell" })
67-
((MenuItem)menu.Items[index]).Items.Add(new MenuItem { Header = str });
68-
66+
foreach (CustomCommandInfo info in _viewModel.CustomCommands)
67+
{
68+
((MenuItem)menu.Items[index]).Items.Add(new MenuItem { Header = info.Name, Command = _viewModel.CustomCommandCommand, CommandParameter = info.ID });
69+
}
6970
}
7071
}
7172
}

0 commit comments

Comments
 (0)