[Settings]Show No network error when trying to update (#21500)

This commit is contained in:
Laszlo Nemeth
2022-11-15 15:11:44 +01:00
committed by GitHub
parent a357d62ff5
commit 8c6b447a33
3 changed files with 80 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Abstractions;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.Json;
@@ -159,6 +160,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
private bool _isNewVersionDownloading;
private bool _isNewVersionChecked;
private bool _isNoNetwork;
private bool _settingsBackupRestoreMessageVisible;
private string _settingsBackupMessage;
@@ -592,6 +594,14 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public bool IsNoNetwork
{
get
{
return _isNoNetwork;
}
}
public bool SettingsBackupRestoreMessageVisible
{
get
@@ -737,6 +747,23 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
// callback function to launch the URL to check for updates.
private void CheckForUpdatesClick()
{
// check if network is available
bool isNetAvailable = IsNetworkAvailable();
// check if the state changed
bool prevState = _isNoNetwork;
_isNoNetwork = !isNetAvailable;
if (prevState != _isNoNetwork)
{
NotifyPropertyChanged(nameof(IsNoNetwork));
}
if (!isNetAvailable)
{
_isNewVersionDownloading = false;
return;
}
RefreshUpdatingState();
IsNewVersionDownloading = string.IsNullOrEmpty(UpdatingSettingsConfig.DownloadedInstallerFilename);
NotifyPropertyChanged(nameof(IsDownloadAllowed));
@@ -886,5 +913,48 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
NotifyPropertyChanged(nameof(IsDownloadAllowed));
}
}
/// <summary>
/// Indicates whether any network connection is available
/// Filter virtual network cards.
/// </summary>
/// <returns>
/// <c>true</c> if a network connection is available; otherwise, <c>false</c>.
/// </returns>
public static bool IsNetworkAvailable()
{
if (!NetworkInterface.GetIsNetworkAvailable())
{
return false;
}
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
// discard because of standard reasons
if ((ni.OperationalStatus != OperationalStatus.Up) ||
(ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
(ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
{
continue;
}
// discard virtual cards (virtual box, virtual pc, etc.)
if (ni.Description.Contains("virtual", StringComparison.OrdinalIgnoreCase) ||
ni.Name.Contains("virtual", StringComparison.OrdinalIgnoreCase))
{
continue;
}
// discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
{
continue;
}
return true;
}
return false;
}
}
}