From c469898024d6ae2179818221daa249d04042a084 Mon Sep 17 00:00:00 2001 From: torum <37834607+torum@users.noreply.github.com> Date: Sat, 31 Dec 2022 23:54:55 +0900 Subject: [PATCH] v3.0.17 --- .../MPDCtrl/ViewModels/Classes/Profile.cs | 15 ++ .../MPDCtrl/ViewModels/MainViewModel.cs | 133 ++++++++++++++++-- 2 files changed, 138 insertions(+), 10 deletions(-) diff --git a/MPDCtrl-Desktop/MPDCtrl/ViewModels/Classes/Profile.cs b/MPDCtrl-Desktop/MPDCtrl/ViewModels/Classes/Profile.cs index a2792e5..2ac138e 100644 --- a/MPDCtrl-Desktop/MPDCtrl/ViewModels/Classes/Profile.cs +++ b/MPDCtrl-Desktop/MPDCtrl/ViewModels/Classes/Profile.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; using MPDCtrl.Common; @@ -26,6 +27,20 @@ public string Host } } + private IPAddress _hostIpAddress; + public IPAddress HostIpAddress + { + get { return _hostIpAddress; } + set + { + if (_hostIpAddress == value) + return; + + _hostIpAddress = value; + NotifyPropertyChanged(nameof(HostIpAddress)); + } + } + private int _port; public int Port { diff --git a/MPDCtrl-Desktop/MPDCtrl/ViewModels/MainViewModel.cs b/MPDCtrl-Desktop/MPDCtrl/ViewModels/MainViewModel.cs index 9db3084..d4b14ff 100644 --- a/MPDCtrl-Desktop/MPDCtrl/ViewModels/MainViewModel.cs +++ b/MPDCtrl-Desktop/MPDCtrl/ViewModels/MainViewModel.cs @@ -19,6 +19,7 @@ using System.Windows.Media; using System.Xml; using System.Xml.Linq; +using Windows.Networking; namespace MPDCtrl.ViewModels { @@ -40,7 +41,8 @@ namespace MPDCtrl.ViewModels /// Add Search option "Exact" or "Contain". /// /// Version history: - /// v3.0.16 + /// v3.0.17 Allow hostname input removing ip address input restriction. + /// v3.0.16 Store release. /// v3.0.15.2 tweaked the light theme (mainly setting > connection profile combobox and checkbox). /// v3.0.15.1 tweaked the light theme (mainly sliders). /// v3.0.15 MS Store release. @@ -124,7 +126,7 @@ public class MainViewModel : ViewModelBase const string _appName = "MPDCtrl"; // Application version - const string _appVer = "v3.0.16.0"; + const string _appVer = "v3.0.17.0"; public static string AppVer { @@ -2377,6 +2379,7 @@ public string Host SetError(nameof(Host), MPDCtrl.Properties.Resources.Settings_ErrorHostMustBeSpecified); } + /* else if (value == "localhost") { _host = "127.0.0.1"; @@ -2398,11 +2401,27 @@ public string Host SetError(nameof(Host), MPDCtrl.Properties.Resources.Settings_ErrorHostInvalidAddressFormat); } } + */ NotifyPropertyChanged(nameof(Host)); } } + private IPAddress _hostIpAddress; + public IPAddress HostIpAddress + { + get { return _hostIpAddress; } + set + { + if (_hostIpAddress == value) + return; + + _hostIpAddress = value; + + NotifyPropertyChanged(nameof(HostIpAddress)); + } + } + private int _port = 6600; public string Port { @@ -4244,10 +4263,34 @@ public void OnWindowClosing(object sender, CancelEventArgs e) #region == Methods == - private void Start(string host, int port) + private async void Start(string host, int port) { + HostIpAddress = null; + try + { + var addresses = await Dns.GetHostAddressesAsync(host); + if (addresses.Count() > 0) + { + HostIpAddress = addresses[0]; + } + else + { + // TODO:: + ConnectionStatusMessage = "Error: Could not retrive IP Address from the hostname."; + StatusBarMessage = "Error: Could not retrive IP Address from the hostname."; + return; + } + } + catch (Exception ex) + { + // TODO:: + ConnectionStatusMessage = "Error: Could not retrive IP Address from the hostname."; + StatusBarMessage = "Error: Could not retrive IP Address from the hostname."; + return; + } + // Start MPD connection. - Task.Run(() => _mpc.MpdIdleConnect(host, port)); + _=Task.Run(() => _mpc.MpdIdleConnect(HostIpAddress.ToString(), port)); } private void UpdateButtonStatus() @@ -7207,12 +7250,13 @@ public async void ChangeConnectionProfileCommand_Execute(object obj) // Validate Host input. if (Host == "") { - SetError(nameof(Host), "Error: Host must be epecified."); //TODO: translate + SetError(nameof(Host), "Error: Host must be specified."); //TODO: translate NotifyPropertyChanged(nameof(Host)); return; } else { + /* if (Host == "localhost") { Host = "127.0.0.1"; @@ -7234,11 +7278,41 @@ public async void ChangeConnectionProfileCommand_Execute(object obj) return; } + */ + } + + HostIpAddress = null; + try + { + var addresses = await Dns.GetHostAddressesAsync(Host); + if (addresses.Count() > 0) + { + HostIpAddress = addresses[0]; + } + else + { + //TODO: translate. + SetError(nameof(Host), "Error: Could not retrive IP Address from the hostname."); + // TODO:: + ConnectionStatusMessage = "Error: Could not retrive IP Address from the hostname."; + StatusBarMessage = "Error: Could not retrive IP Address from the hostname."; + return; + } + } + catch(Exception ex) + { + //TODO: translate. + SetError(nameof(Host), "Error: Could not retrive IP Address from the hostname. (SocketException)"); + // TODO:: + ConnectionStatusMessage = "Error: Could not retrive IP Address from the hostname."; + StatusBarMessage = "Error: Could not retrive IP Address from the hostname."; + return; } if (_port == 0) { - SetError(nameof(Port), "Error: Port must be epecified."); //TODO: translate. + //TODO: translate. + SetError(nameof(Port), "Error: Port must be specified."); return; } @@ -7309,8 +7383,9 @@ public async void ChangeConnectionProfileCommand_Execute(object obj) IsConnecting = true; - /// - ConnectionResult r = await _mpc.MpdIdleConnect(_host, _port); + if (HostIpAddress == null) return; + //ConnectionResult r = await _mpc.MpdIdleConnect(_host, _port); + ConnectionResult r = await _mpc.MpdIdleConnect(HostIpAddress.ToString(), _port); if (r.IsSuccess) { @@ -7323,6 +7398,7 @@ public async void ChangeConnectionProfileCommand_Execute(object obj) { Name = _host + ":" + _port.ToString(), Host = _host, + HostIpAddress = _hostIpAddress, Port = _port, Password = _password, IsDefault = true @@ -7337,6 +7413,7 @@ public async void ChangeConnectionProfileCommand_Execute(object obj) else { SelectedProfile.Host = _host; + SelectedProfile.HostIpAddress = _hostIpAddress; SelectedProfile.Port = _port; SelectedProfile.Password = _password; @@ -7439,19 +7516,55 @@ private async void ChangeConnection(Profile prof) IsAlbumArtVisible = false; AlbumArt = _albumArtDefault; + HostIpAddress = null; + // TODO: more? }); _volume = prof.Volume; NotifyPropertyChanged(nameof(Volume)); + Host = prof.Host; + + HostIpAddress = null; + + try + { + var addresses = await Dns.GetHostAddressesAsync(Host); + if (addresses.Count() > 0) + { + HostIpAddress = addresses[0]; + } + else + { + SetError(nameof(Host), "Error: Could not retrive IP Address from the hostname."); //TODO: translate. + + // TODO::::::: + ConnectionStatusMessage = "Error: Could not retrive IP Address from the hostname."; + StatusBarMessage = "Error: Could not retrive IP Address from the hostname."; + return; + } + } + catch (Exception ex) + { + SetError(nameof(Host), "Error: Could not retrive IP Address from the hostname. (SocketException)"); //TODO: translate. + + // TODO::::::: + ConnectionStatusMessage = "Error: Could not retrive IP Address from the hostname."; + StatusBarMessage = "Error: Could not retrive IP Address from the hostname."; + + return; + } + _port = prof.Port; Password = prof.Password; IsConnecting = true; - /// - ConnectionResult r = await _mpc.MpdIdleConnect(_host, _port); + + if (HostIpAddress == null) return; + //ConnectionResult r = await _mpc.MpdIdleConnect(_host, _port); + ConnectionResult r = await _mpc.MpdIdleConnect(HostIpAddress.ToString(), _port); if (r.IsSuccess) {