Skip to content

Commit b4e29b9

Browse files
committed
Catch exception if putty.exe not found
1 parent a96bfc7 commit b4e29b9

6 files changed

Lines changed: 85 additions & 31 deletions

File tree

Source/NETworkManager/Controls/PuTTYControl.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:Converter="clr-namespace:NETworkManager.Converters"
77
xmlns:WindowsForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
8+
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
9+
Dialog:DialogParticipation.Register="{Binding}"
810
mc:Ignorable="d" Loaded="UserControl_Loaded">
911
<UserControl.Resources>
1012
<Converter:BooleanReverseToVisibilityConverter x:Key="BooleanReverseToVisibilityConverter" />

Source/NETworkManager/Controls/PuTTYControl.xaml.cs

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using NETworkManager.Utilities;
1111
using NETworkManager.Models.PuTTY;
1212
using System.Windows.Input;
13+
using MahApps.Metro.Controls.Dialogs;
14+
using NETworkManager.Models.Settings;
1315

1416
namespace NETworkManager.Controls
1517
{
@@ -26,14 +28,15 @@ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName
2628

2729
#region Variables
2830
private bool _initialized = false;
31+
private IDialogCoordinator dialogCoordinator;
2932

30-
private PuTTYSessionInfo _puTTYSessionInfo;
33+
private Models.PuTTY.PuTTYSessionInfo _puTTYSessionInfo;
3134

3235
Process PuTTYProcess = null;
3336
IntPtr AppWin;
3437

3538
DispatcherTimer resizeTimer = new DispatcherTimer();
36-
39+
3740
private bool _connected = true;
3841
public bool Connected
3942
{
@@ -50,11 +53,13 @@ public bool Connected
5053
#endregion
5154

5255
#region Constructor, load
53-
public PuTTYControl(PuTTYSessionInfo info)
56+
public PuTTYControl(Models.PuTTY.PuTTYSessionInfo info)
5457
{
5558
InitializeComponent();
5659
DataContext = this;
5760

61+
dialogCoordinator = DialogCoordinator.Instance;
62+
5863
_puTTYSessionInfo = info;
5964

6065
resizeTimer.Tick += ResizeTimer_Tick;
@@ -92,39 +97,55 @@ private void ReconnectAction()
9297
#endregion
9398

9499
#region Methods
95-
private void Connect()
100+
private async void Connect()
96101
{
102+
103+
97104
ProcessStartInfo info = new ProcessStartInfo
98105
{
99106
FileName = _puTTYSessionInfo.PuTTYLocation,
100107
Arguments = PuTTY.BuildCommandLine(_puTTYSessionInfo)
101108
};
102109

103-
PuTTYProcess = Process.Start(info);
110+
try
111+
{
112+
PuTTYProcess = Process.Start(info);
104113

105-
PuTTYProcess.EnableRaisingEvents = true;
106-
PuTTYProcess.Exited += PuTTYProcess_Exited;
114+
PuTTYProcess.EnableRaisingEvents = true;
115+
PuTTYProcess.Exited += PuTTYProcess_Exited;
107116

108-
PuTTYProcess.WaitForInputIdle();
109-
110-
// Embed putty window into panel, remove border etc.
111-
AppWin = PuTTYProcess.MainWindowHandle;
117+
PuTTYProcess.WaitForInputIdle();
112118

113-
NativeMethods.SetParent(AppWin, puTTYHost.Handle);
119+
// Embed putty window into panel, remove border etc.
120+
AppWin = PuTTYProcess.MainWindowHandle;
114121

115-
// Show window before set style and resize
116-
NativeMethods.ShowWindow(AppWin, NativeMethods.WindowShowStyle.Maximize);
122+
NativeMethods.SetParent(AppWin, puTTYHost.Handle);
117123

118-
// Remove border etc.
119-
int style = (int)NativeMethods.GetWindowLong(AppWin, NativeMethods.GWL_STYLE);
120-
style &= ~(NativeMethods.WS_BORDER | NativeMethods.WS_THICKFRAME);
121-
NativeMethods.SetWindowLongPtr(AppWin, NativeMethods.GWL_STYLE, new IntPtr(style));
124+
// Show window before set style and resize
125+
NativeMethods.ShowWindow(AppWin, NativeMethods.WindowShowStyle.Maximize);
122126

123-
// Resize embedded application & refresh
124-
if (PuTTYProcess != null)
125-
ResizeEmbeddedPuTTY();
127+
// Remove border etc.
128+
int style = (int)NativeMethods.GetWindowLong(AppWin, NativeMethods.GWL_STYLE);
129+
style &= ~(NativeMethods.WS_BORDER | NativeMethods.WS_THICKFRAME);
130+
NativeMethods.SetWindowLongPtr(AppWin, NativeMethods.GWL_STYLE, new IntPtr(style));
126131

127-
Connected = true;
132+
// Resize embedded application & refresh
133+
if (PuTTYProcess != null)
134+
ResizeEmbeddedPuTTY();
135+
136+
Connected = true;
137+
}
138+
catch (Exception ex)
139+
{
140+
MetroDialogSettings settings = AppearanceManager.MetroDialog;
141+
settings.AffirmativeButtonText = Application.Current.Resources["String_Button_OK"] as string;
142+
143+
ConfigurationManager.Current.FixAirspace = true;
144+
145+
await dialogCoordinator.ShowMessageAsync(this, Application.Current.Resources["String_Header_Error"] as string, ex.Message, MessageDialogStyle.Affirmative, settings);
146+
147+
ConfigurationManager.Current.FixAirspace = false;
148+
}
128149
}
129150

130151
private void PuTTYProcess_Exited(object sender, EventArgs e)

Source/NETworkManager/ViewModels/PuTTYSettingsViewModel.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
using NETworkManager.Models.Settings;
1+
using MahApps.Metro.Controls.Dialogs;
2+
using NETworkManager.Models.Settings;
23
using NETworkManager.Utilities;
4+
using System;
35
using System.Diagnostics;
6+
using System.IO;
7+
using System.Windows;
48
using System.Windows.Input;
59

610
namespace NETworkManager.ViewModels
@@ -9,7 +13,8 @@ public class PuTTYSettingsViewModel : ViewModelBase
913
{
1014
#region Variables
1115
private const string ApplicationFileExtensionFilter = "Application (*.exe)|*.exe";
12-
16+
private IDialogCoordinator dialogCoordinator;
17+
1318
private bool _isLoading = true;
1419

1520
private string _puTTYLocation;
@@ -150,8 +155,10 @@ public int RloginPort
150155
#endregion
151156

152157
#region Contructor, load settings
153-
public PuTTYSettingsViewModel()
158+
public PuTTYSettingsViewModel(IDialogCoordinator instance)
154159
{
160+
dialogCoordinator = instance;
161+
155162
LoadSettings();
156163

157164
_isLoading = false;
@@ -160,6 +167,7 @@ public PuTTYSettingsViewModel()
160167
private void LoadSettings()
161168
{
162169
PuTTYLocation = SettingsManager.Current.PuTTY_PuTTYLocation;
170+
IsPuTTYConfigured = File.Exists(PuTTYLocation);
163171
SerialLine = SettingsManager.Current.PuTTY_SerialLine;
164172
PuTTYProfile = SettingsManager.Current.PuTTY_Profile;
165173
SSHPort = SettingsManager.Current.PuTTY_SSHPort;
@@ -193,7 +201,24 @@ public ICommand ConfigurePuTTYCommand
193201

194202
private void ConfigurePuTTYAction()
195203
{
196-
Process.Start(SettingsManager.Current.PuTTY_PuTTYLocation);
204+
ConfigurePuTTY();
205+
}
206+
#endregion
207+
208+
#region Methods
209+
private async void ConfigurePuTTY()
210+
{
211+
try
212+
{
213+
Process.Start(SettingsManager.Current.PuTTY_PuTTYLocation);
214+
}
215+
catch (Exception ex)
216+
{
217+
MetroDialogSettings settings = AppearanceManager.MetroDialog;
218+
settings.AffirmativeButtonText = Application.Current.Resources["String_Button_OK"] as string;
219+
220+
await dialogCoordinator.ShowMessageAsync(this, Application.Current.Resources["String_Header_Error"] as string, ex.Message, MessageDialogStyle.Affirmative, settings);
221+
}
197222
}
198223
#endregion
199224
}

Source/NETworkManager/ViewModels/PuTTYViewModel.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,10 @@ private async void EditGroupAction(object group)
437437
#region Methods
438438
private void CheckIfPuTTYConfigured()
439439
{
440-
IsPuTTYConfigured = File.Exists(SettingsManager.Current.PuTTY_PuTTYLocation);
440+
if (!string.IsNullOrEmpty(SettingsManager.Current.PuTTY_PuTTYLocation))
441+
IsPuTTYConfigured = File.Exists(SettingsManager.Current.PuTTY_PuTTYLocation);
442+
else
443+
IsPuTTYConfigured = false;
441444
}
442445

443446
private void ConnectSession(Models.PuTTY.PuTTYSessionInfo sessionInfo, string Header = null)

Source/NETworkManager/Views/PuTTYSettingsView.xaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
7-
xmlns:Validator="clr-namespace:NETworkManager.Validators"
7+
xmlns:Validator="clr-namespace:NETworkManager.Validators"
8+
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
9+
Dialog:DialogParticipation.Register="{Binding}"
810
mc:Ignorable="d">
911
<StackPanel>
1012
<TextBlock Text="{DynamicResource String_Header_PuTTY}" Style="{StaticResource HeaderTextBlock}" />
@@ -18,7 +20,7 @@
1820
</Binding>
1921
</TextBox.Text>
2022
</TextBox>
21-
<Button Content="{DynamicResource String_Button_ConfigurePuTTY}" Command="{Binding ConfigurePuTTYCommand}" IsEnabled="{Binding IsPuTTYConfigured}" HorizontalAlignment="Left" Style="{StaticResource DefaultButton}" Margin="0,0,0,10" />
23+
<Button Content="{DynamicResource String_Button_ConfigurePuTTY}" Command="{Binding ConfigurePuTTYCommand}" IsEnabled="{Binding IsPuTTYConfigured}" Style="{StaticResource DefaultButton}" HorizontalAlignment="Left" Margin="0,0,0,10" />
2224
<TextBlock Text="{DynamicResource String_PuTTYProfile}" Style="{DynamicResource DefaultTextBlock}" VerticalAlignment="Center" Margin="0,0,0,10" />
2325
<TextBox Width="250" Text="{Binding PuTTYProfile}" HorizontalAlignment="Left" Style="{StaticResource DefaultTextBox}" Margin="0,0,0,10" />
2426
<TextBlock Text="{DynamicResource String_DefaultSSHPort}" Style="{DynamicResource DefaultTextBlock}" VerticalAlignment="Center" Margin="0,0,0,10" />

Source/NETworkManager/Views/PuTTYSettingsView.xaml.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using NETworkManager.ViewModels;
1+
using MahApps.Metro.Controls.Dialogs;
2+
using NETworkManager.ViewModels;
23
using System.Windows.Controls;
34

45
namespace NETworkManager.Views
56
{
67
public partial class PuTTYSettingsView : UserControl
78
{
8-
PuTTYSettingsViewModel viewModel = new PuTTYSettingsViewModel();
9+
PuTTYSettingsViewModel viewModel = new PuTTYSettingsViewModel(DialogCoordinator.Instance);
910

1011
public PuTTYSettingsView()
1112
{

0 commit comments

Comments
 (0)