1+ /*
2+ MIT License
3+
4+ Copyright (c) Léo Corporation
5+
6+ Permission is hereby granted, free of charge, to any person obtaining a copy
7+ of this software and associated documentation files (the "Software"), to deal
8+ in the Software without restriction, including without limitation the rights
9+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ copies of the Software, and to permit persons to whom the Software is
11+ furnished to do so, subject to the following conditions:
12+
13+ The above copyright notice and this permission notice shall be included in all
14+ copies or substantial portions of the Software.
15+
16+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ SOFTWARE.
23+ */
24+ using InternetTest . Commands ;
25+ using InternetTest . Helpers ;
26+ using InternetTest . Models ;
27+ using System . Collections . ObjectModel ;
28+ using System . Net . NetworkInformation ;
29+ using System . Windows ;
30+ using System . Windows . Input ;
31+
32+ namespace InternetTest . ViewModels . Windows ;
33+
34+ public class AdapterDetailsWindowViewModel : ViewModelBase
35+ {
36+ private readonly NetworkAdapter _networkAdapter ;
37+
38+ private string ? _name ;
39+ public string Name { get => _name ?? string . Empty ; set { _name = value ; OnPropertyChanged ( nameof ( Name ) ) ; } }
40+ public ObservableCollection < GridItemViewModel > Details { get ; } = [ ] ;
41+ public ObservableCollection < GridItemViewModel > Cat2 { get ; } = [ ] ;
42+ public ObservableCollection < GridItemViewModel > Cat3 { get ; } = [ ] ;
43+
44+ public ICommand CopyCommand { get ; }
45+ public AdapterDetailsWindowViewModel ( NetworkAdapter networkAdapter )
46+ {
47+ Name = networkAdapter . Name ;
48+ Details = [
49+ new ( Properties . Resources . DataConsumption , $ "{ StorageUnitHelper . GetStorageUnit ( networkAdapter . BytesReceived + networkAdapter . BytesSent ) . Item2 : 0.00} { StorageUnitHelper . UnitToString ( StorageUnitHelper . GetStorageUnit ( networkAdapter . BytesReceived + networkAdapter . BytesSent ) . Item1 ) } ", 0 , 0 ) ,
50+ new ( Properties . Resources . InterfaceType , NetworkAdapter . GetInterfaceTypeName ( networkAdapter . NetworkInterfaceType ) , 1 , 0 ) ,
51+ new ( Properties . Resources . Status , networkAdapter . Status switch {
52+ OperationalStatus . Up => Properties . Resources . ConnectedS ,
53+ OperationalStatus . Down => Properties . Resources . Disconnected ,
54+ _ => networkAdapter . Status . ToString ( )
55+ } , 0 , 1 ) ,
56+ new ( Properties . Resources . IpVersion , networkAdapter . IpVersion ?? Properties . Resources . Unknown , 1 , 1 ) ,
57+ new ( Properties . Resources . Speed , $ "{ StorageUnitHelper . GetStorageUnit ( networkAdapter . Speed ) . Item2 : 0.00} { StorageUnitHelper . UnitToString ( StorageUnitHelper . GetStorageUnit ( networkAdapter . Speed ) . Item1 ) } /s", 2 , 0 ) ,
58+ ] ;
59+
60+ Cat2 = [
61+ new ( Properties . Resources . DNSSuffix , networkAdapter . DnsSuffix , 0 , 0 ) ,
62+ new ( Properties . Resources . MTU , networkAdapter . Mtu . ToString ( ) , 1 , 0 ) ,
63+ new ( Properties . Resources . DnsEnabled , BoolToString ( networkAdapter . DnsEnabled ) , 0 , 1 ) ,
64+ new ( Properties . Resources . DnsDynamicConfigured , BoolToString ( networkAdapter . IsDynamicDnsEnabled ) , 1 , 1 ) ,
65+ new ( Properties . Resources . Multicast , BoolToString ( networkAdapter . SupportsMulticast ) , 2 , 0 )
66+ ] ;
67+
68+ Cat3 = [
69+ new ( Properties . Resources . TotalBytesReceived , $ "{ StorageUnitHelper . GetStorageUnit ( networkAdapter . BytesReceived ) . Item2 : 0.00} { StorageUnitHelper . UnitToString ( StorageUnitHelper . GetStorageUnit ( networkAdapter . BytesReceived ) . Item1 ) } ", 0 , 0 ) ,
70+ new ( Properties . Resources . TotalBytesSent , $ "{ StorageUnitHelper . GetStorageUnit ( networkAdapter . BytesSent ) . Item2 : 0.00} { StorageUnitHelper . UnitToString ( StorageUnitHelper . GetStorageUnit ( networkAdapter . BytesSent ) . Item1 ) } ", 1 , 0 ) ,
71+ new ( Properties . Resources . IncomingPacketsDiscarded , networkAdapter . IncomingPacketsDiscarded . ToString ( ) , 2 , 0 ) ,
72+ new ( Properties . Resources . IncomingPacketsWithErrors , networkAdapter . IncomingPacketsWithErrors . ToString ( ) , 3 , 0 ) ,
73+ new ( Properties . Resources . IncomingUnknownProtocolPackets , networkAdapter . IncomingUnknownProtocolPackets . ToString ( ) , 4 , 0 ) ,
74+ new ( Properties . Resources . NonUnicastPacketsReceived , networkAdapter . NonUnicastPacketsReceived . ToString ( ) , 5 , 0 ) ,
75+ new ( Properties . Resources . NonUnicastPacketsSent , networkAdapter . NonUnicastPacketsSent . ToString ( ) , 0 , 1 ) ,
76+ new ( Properties . Resources . OutgoingPacketsDiscarded , networkAdapter . OutgoingPacketsDiscarded . ToString ( ) , 1 , 1 ) ,
77+ new ( Properties . Resources . OutgoingPacketsWithErrors , networkAdapter . OutgoingPacketsWithErrors . ToString ( ) , 2 , 1 ) ,
78+ new ( Properties . Resources . OutputQueueLength , networkAdapter . OutputQueueLength . ToString ( ) , 3 , 1 ) ,
79+ new ( Properties . Resources . UnicastPacketsReceived , networkAdapter . UnicastPacketsReceived . ToString ( ) , 4 , 1 ) ,
80+ new ( Properties . Resources . UnicastPacketsSent , networkAdapter . UnicastPacketsSent . ToString ( ) , 5 , 1 )
81+ ] ;
82+
83+ CopyCommand = new RelayCommand ( Copy ) ;
84+ _networkAdapter = networkAdapter ;
85+ }
86+
87+ private void Copy ( object ? o )
88+ {
89+ Clipboard . SetDataObject ( _networkAdapter . ToFormattedString ( ) ) ;
90+ }
91+
92+ private string BoolToString ( bool b )
93+ {
94+ return b ? Properties . Resources . Yes : Properties . Resources . No ;
95+ }
96+ }
97+
98+ public class GridItemViewModel : ViewModelBase
99+ {
100+ private string ? _title ;
101+ public string ? Title
102+ {
103+ get => _title ;
104+ set { _title = value ; OnPropertyChanged ( nameof ( Title ) ) ; }
105+ }
106+ private string ? _value ;
107+ public string ? Value
108+ {
109+ get => _value ;
110+ set { _value = value ; OnPropertyChanged ( nameof ( Value ) ) ; }
111+ }
112+
113+ private int _gridColumn ;
114+ public int GridColumn
115+ {
116+ get => _gridColumn ;
117+ set { _gridColumn = value ; OnPropertyChanged ( nameof ( GridColumn ) ) ; }
118+ }
119+
120+ private int _gridRow ;
121+ public int GridRow
122+ {
123+ get => _gridRow ;
124+ set { _gridRow = value ; OnPropertyChanged ( nameof ( GridRow ) ) ; }
125+ }
126+
127+ public GridItemViewModel ( string title , string value , int row , int col )
128+ {
129+ Title = title ;
130+ Value = value ;
131+ GridRow = row ;
132+ GridColumn = col ;
133+ }
134+ }
0 commit comments