11using System ;
2+ using System . Collections . Generic ;
23using System . Collections . ObjectModel ;
4+ using System . Diagnostics . Contracts ;
35using System . Linq ;
46using System . Reactive ;
7+ using System . Threading ;
58using Avalonia . Media ;
9+ using MsBox . Avalonia ;
10+ using MsBox . Avalonia . Enums ;
611using OpenSSHALib . Lib ;
712using OpenSSHALib . Models ;
813using ReactiveUI ;
@@ -13,37 +18,96 @@ namespace OpenSSHA_GUI.ViewModels;
1318
1419public class UploadToServerViewModel : ViewModelBase , IValidatableViewModel
1520{
21+ public UploadToServerViewModel ( ) : this ( [ ] ) { }
1622 public UploadToServerViewModel ( ObservableCollection < SshPublicKey > keys )
1723 {
1824 Keys = keys ;
1925 SelectedPublicKey = Keys . First ( ) ;
20- UploadAction = ReactiveCommand . CreateFromTask < Unit , UploadToServerViewModel ? > (
26+ UploadAction = ReactiveCommand . CreateFromTask < Unit , UploadToServerViewModel > (
2127 async e =>
2228 {
29+ string messageBoxText ;
30+ var messageBoxIcon = Icon . Success ;
31+ if ( Hostname is "" || User is "" || Password is "" ) messageBoxIcon = Icon . Error ;
32+ if ( ServerCommunicator . TryOpenSshConnection ( Hostname , User , Password , out var client , out var errorMessage ) )
33+ {
34+ try
35+ {
36+ messageBoxText = await client . PutKeyToServer ( SelectedPublicKey ) ;
37+ }
38+ catch ( Exception exception )
39+ {
40+ messageBoxText = exception . Message ;
41+ messageBoxIcon = Icon . Error ;
42+ }
43+ }
44+ else
45+ {
46+ messageBoxText = errorMessage ;
47+ messageBoxIcon = Icon . Error ;
48+ }
49+
50+ var messageBox = MessageBoxManager . GetMessageBoxStandard ( $ "Upload result of key { SelectedPublicKey . Filename } ", messageBoxText , ButtonEnum . Ok , messageBoxIcon ) ;
51+ await messageBox . ShowAsync ( ) ;
2352 return this ;
2453 } ) ;
2554 TestConnection = ReactiveCommand . CreateFromTask < Unit , Unit > ( async e =>
2655 {
56+ var toolTipMessage = "Missing host, user or password!" ;
57+ if ( Hostname is "" || User is "" || Password is "" ) goto Failed ;
2758 if ( ServerCommunicator . TestConnection ( Hostname , User , Password , out var message ) )
2859 {
2960 StatusButtonBackground = Brushes . LimeGreen ;
3061 StatusButtonText = "Status: success" ;
3162 StatusButtonToolTip = $ "Connection successfully established for ssh://{ User } @{ Hostname } ";
63+ ConnectionSuccessful = true ;
64+ EvaluateEnabledState ( ) ;
65+ return e ;
3266 }
33- else
34- {
35- StatusButtonBackground = Brushes . IndianRed ;
36- StatusButtonText = "Status: failed" ;
37- StatusButtonToolTip = message ;
38- }
67+ toolTipMessage = message ;
68+ Failed :
69+ StatusButtonBackground = Brushes . IndianRed ;
70+ StatusButtonText = "Status: failed" ;
71+ StatusButtonToolTip = toolTipMessage ;
72+ Hostname = "" ;
73+ User = "" ;
74+ Password = "" ;
75+ EvaluateEnabledState ( ) ;
76+ return e ;
77+ } ) ;
78+ ResetCommand = ReactiveCommand . Create < Unit , Unit > ( e =>
79+ {
80+ Hostname = "" ;
81+ User = "" ;
82+ Password = "" ;
83+ ConnectionSuccessful = false ;
84+ SelectedPublicKey = Keys . First ( ) ;
85+ StatusButtonText = "Status: unknown" ;
86+ StatusButtonToolTip = "Status not yet tested!" ;
87+ StatusButtonBackground = Brushes . Gray ;
88+ EvaluateEnabledState ( ) ;
3989 return e ;
4090 } ) ;
4191 }
4292
4393 public string Hostname { get ; set ; } = "" ;
4494 public string User { get ; set ; } = "" ;
4595 public string Password { get ; set ; } = "" ;
96+ private bool ConnectionSuccessful { get ; set ; } = false ;
97+
98+
99+ private bool _uploadButtonEnabled = false ;
100+ public bool UploadButtonEnabled
101+ {
102+ get => _uploadButtonEnabled ;
103+ set => this . RaiseAndSetIfChanged ( ref _uploadButtonEnabled , value ) ;
104+ }
46105
106+ private void EvaluateEnabledState ( )
107+ {
108+ UploadButtonEnabled = Hostname is not "" && User is not "" && Password is not "" && ConnectionSuccessful ;
109+ }
110+
47111 public ReactiveCommand < Unit , Unit > TestConnection { get ; }
48112
49113 private string _statusButtonToolTip = "Status not yet tested" ;
@@ -67,8 +131,9 @@ public IBrush StatusButtonBackground
67131 set => this . RaiseAndSetIfChanged ( ref _statusButtonBackground , value ) ;
68132 }
69133
70- public SshPublicKey SelectedPublicKey { get ; }
134+ public SshPublicKey SelectedPublicKey { get ; set ; }
71135 public ObservableCollection < SshPublicKey > Keys { get ; }
72136 public ValidationContext ValidationContext { get ; } = new ( ) ;
73137 public ReactiveCommand < Unit , UploadToServerViewModel > UploadAction { get ; }
138+ public ReactiveCommand < Unit , Unit > ResetCommand { get ; }
74139}
0 commit comments