[PT Run] System plugin: Add IP and MAC (#17023)

* first test and changes

* last changes

* last changes

* last changes

* improve exception

* fix spellings

* spell fixes

* search improvements, installer, tests, code cleanup

* remove left-over

* update dev docs

* fix spelling

* update namings

* improve scoring

* spell checker

* dev docs

* update images

* update expect.txt for this pr
This commit is contained in:
Heiko
2022-03-21 13:37:51 +01:00
committed by GitHub
parent 42ba008323
commit 4c067bb728
16 changed files with 1193 additions and 168 deletions

View File

@@ -0,0 +1,205 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.NetworkInformation;
using System.Windows;
using System.Windows.Interop;
using Microsoft.PowerToys.Run.Plugin.System.Properties;
using Wox.Infrastructure;
using Wox.Plugin;
using Wox.Plugin.Common.Win32;
using Wox.Plugin.Logger;
namespace Microsoft.PowerToys.Run.Plugin.System.Components
{
/// <summary>
/// This class holds all available results
/// </summary>
internal static class Commands
{
internal const int EWXLOGOFF = 0x00000000;
internal const int EWXSHUTDOWN = 0x00000001;
internal const int EWXREBOOT = 0x00000002;
internal const int EWXFORCE = 0x00000004;
internal const int EWXPOWEROFF = 0x00000008;
internal const int EWXFORCEIFHUNG = 0x00000010;
/// <summary>
/// Returns a list with all system command results
/// </summary>
/// <param name="isUefi">Value indicating if the system is booted in uefi mode</param>
/// <param name="iconTheme">The current theme to use for the icons</param>
/// <param name="culture">The culture to use for the result's title and sub title</param>
/// <param name="confirmCommands">A value indicating if the user should confirm the system commands</param>
/// <returns>A list of all results</returns>
internal static List<Result> GetSystemCommands(bool isUefi, string iconTheme, CultureInfo culture, bool confirmCommands)
{
var results = new List<Result>();
results.AddRange(new[]
{
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_shutdown_computer", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_shutdown_computer_description", culture),
IcoPath = $"Images\\shutdown.{iconTheme}.png",
Action = c =>
{
return ResultHelper.ExecuteCommand(confirmCommands, Resources.Microsoft_plugin_sys_shutdown_computer_confirmation, () => Helper.OpenInShell("shutdown", "/s /hybrid /t 0"));
},
},
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_restart_computer", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_restart_computer_description", culture),
IcoPath = $"Images\\restart.{iconTheme}.png",
Action = c =>
{
return ResultHelper.ExecuteCommand(confirmCommands, Resources.Microsoft_plugin_sys_restart_computer_confirmation, () => Helper.OpenInShell("shutdown", "/r /t 0"));
},
},
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_sign_out", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_sign_out_description", culture),
IcoPath = $"Images\\logoff.{iconTheme}.png",
Action = c =>
{
return ResultHelper.ExecuteCommand(confirmCommands, Resources.Microsoft_plugin_sys_sign_out_confirmation, () => NativeMethods.ExitWindowsEx(EWXLOGOFF, 0));
},
},
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_lock", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_lock_description", culture),
IcoPath = $"Images\\lock.{iconTheme}.png",
Action = c =>
{
return ResultHelper.ExecuteCommand(confirmCommands, Resources.Microsoft_plugin_sys_lock_confirmation, () => NativeMethods.LockWorkStation());
},
},
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_sleep", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_sleep_description", culture),
IcoPath = $"Images\\sleep.{iconTheme}.png",
Action = c =>
{
return ResultHelper.ExecuteCommand(confirmCommands, Resources.Microsoft_plugin_sys_sleep_confirmation, () => NativeMethods.SetSuspendState(false, true, true));
},
},
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_hibernate", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_hibernate_description", culture),
IcoPath = $"Images\\sleep.{iconTheme}.png", // Icon change needed
Action = c =>
{
return ResultHelper.ExecuteCommand(confirmCommands, Resources.Microsoft_plugin_sys_hibernate_confirmation, () => NativeMethods.SetSuspendState(true, true, true));
},
},
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_emptyrecyclebin", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_emptyrecyclebin_description", culture),
IcoPath = $"Images\\recyclebin.{iconTheme}.png",
Action = c =>
{
// http://www.pinvoke.net/default.aspx/shell32/SHEmptyRecycleBin.html
// FYI, couldn't find documentation for this but if the recycle bin is already empty, it will return -2147418113 (0x8000FFFF (E_UNEXPECTED))
// 0 for nothing
var result = NativeMethods.SHEmptyRecycleBin(new WindowInteropHelper(Application.Current.MainWindow).Handle, 0);
if (result != (uint)HRESULT.S_OK && result != 0x8000FFFF)
{
var name = "Plugin: " + Resources.Microsoft_plugin_sys_plugin_name;
var message = $"Error emptying recycle bin, error code: {result}\n" +
"please refer to https://msdn.microsoft.com/en-us/library/windows/desktop/aa378137";
Log.Error(message, typeof(Commands));
_ = MessageBox.Show(message, name);
}
return true;
},
},
});
// UEFI command/result. It is only available on systems booted in UEFI mode.
if (isUefi)
{
results.Add(new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_uefi", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_uefi_description", culture),
IcoPath = $"Images\\firmwareSettings.{iconTheme}.png",
Action = c =>
{
return ResultHelper.ExecuteCommand(confirmCommands, Resources.Microsoft_plugin_sys_uefi_confirmation, () => Helper.OpenInShell("shutdown", "/r /fw /t 0", null, true));
},
});
}
return results;
}
/// <summary>
/// Returns a list of all ip and mac results
/// </summary>
/// <param name="iconTheme">The theme to use for the icons</param>
/// <param name="culture">The culture to use for the result's title and sub title</param>
/// <returns>The list of available results</returns>
internal static List<Result> GetNetworkConnectionResults(string iconTheme, CultureInfo culture)
{
var results = new List<Result>();
var interfaces = NetworkInterface.GetAllNetworkInterfaces().Where(x => x.NetworkInterfaceType != NetworkInterfaceType.Loopback && x.GetPhysicalAddress() != null);
foreach (NetworkInterface i in interfaces)
{
NetworkConnectionProperties intInfo = new NetworkConnectionProperties(i);
if (!string.IsNullOrEmpty(intInfo.IPv4))
{
results.Add(new Result()
{
Title = intInfo.IPv4,
SubTitle = string.Format(CultureInfo.InvariantCulture, Resources.ResourceManager.GetString("Microsoft_plugin_sys_ip4_description", culture), intInfo.ConnectionName) + " - " + Resources.ResourceManager.GetString("Microsoft_plugin_sys_SubTitle_CopyHint", culture),
IcoPath = $"Images\\networkAdapter.{iconTheme}.png",
ToolTipData = new ToolTipData(Resources.Microsoft_plugin_sys_ConnectionDetails, intInfo.GetConnectionDetails()),
ContextData = new SystemPluginContext { Type = ResultContextType.NetworkAdapterInfo, Data = intInfo.GetConnectionDetails() },
Action = _ => ResultHelper.CopyToClipBoard(intInfo.IPv4),
});
}
if (!string.IsNullOrEmpty(intInfo.IPv6Primary))
{
results.Add(new Result()
{
Title = intInfo.IPv6Primary,
SubTitle = string.Format(CultureInfo.InvariantCulture, Resources.ResourceManager.GetString("Microsoft_plugin_sys_ip6_description", culture), intInfo.ConnectionName) + " - " + Resources.ResourceManager.GetString("Microsoft_plugin_sys_SubTitle_CopyHint", culture),
IcoPath = $"Images\\networkAdapter.{iconTheme}.png",
ToolTipData = new ToolTipData(Resources.Microsoft_plugin_sys_ConnectionDetails, intInfo.GetConnectionDetails()),
ContextData = new SystemPluginContext { Type = ResultContextType.NetworkAdapterInfo, Data = intInfo.GetConnectionDetails() },
Action = _ => ResultHelper.CopyToClipBoard(intInfo.IPv6Primary),
});
}
if (!string.IsNullOrEmpty(intInfo.PhysicalAddress))
{
results.Add(new Result()
{
Title = intInfo.PhysicalAddress,
SubTitle = string.Format(CultureInfo.InvariantCulture, Resources.ResourceManager.GetString("Microsoft_plugin_sys_mac_description", culture), intInfo.Adapter, intInfo.ConnectionName) + " - " + Resources.ResourceManager.GetString("Microsoft_plugin_sys_SubTitle_CopyHint", culture),
IcoPath = $"Images\\networkAdapter.{iconTheme}.png",
ToolTipData = new ToolTipData(Resources.Microsoft_plugin_sys_AdapterDetails, intInfo.GetAdapterDetails()),
ContextData = new SystemPluginContext { Type = ResultContextType.NetworkAdapterInfo, Data = intInfo.GetAdapterDetails() },
Action = _ => ResultHelper.CopyToClipBoard(intInfo.PhysicalAddress),
});
}
}
return results;
}
}
}

View File

@@ -0,0 +1,305 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using Microsoft.PowerToys.Run.Plugin.System.Properties;
namespace Microsoft.PowerToys.Run.Plugin.System.Components
{
/// <summary>
/// This class represents the informations for a network connection/interface
/// </summary>
internal class NetworkConnectionProperties
{
/// <summary>
/// Gets the name of the adapter
/// </summary>
internal string Adapter { get; private set; }
/// <summary>
/// Gets the physical address (MAC) of the adapter
/// </summary>
internal string PhysicalAddress { get; private set; }
/// <summary>
/// Gets a value indicating the interface type
/// </summary>
internal NetworkInterfaceType Type { get; private set; }
/// <summary>
/// Gets the speed of the adapter as unformatted value (Static information form the adapter device)
/// </summary>
internal long Speed { get; private set; }
/// <summary>
/// Gets a value indicating the operational state of the adapter
/// </summary>
internal OperationalStatus State { get; private set; }
/// <summary>
/// Gets the name of the network connection
/// </summary>
internal string ConnectionName { get; private set; }
/// <summary>
/// Gets a string with the suffix of the connection
/// </summary>
internal string Suffix { get; private set; }
/// <summary>
/// Gets the IPv4 address
/// </summary>
internal string IPv4 { get; private set; }
/// <summary>
/// Gets the IPv4 subnet mask
/// </summary>
internal string IPv4Mask { get; private set; }
/// <summary>
/// Gets the primarily used IPv6 address
/// </summary>
internal string IPv6Primary { get; private set; }
/// <summary>
/// Gets the global IPv6 address
/// </summary>
internal string IPv6Global { get; private set; }
/// <summary>
/// Gets the temporary IPv6 address
/// </summary>
internal string IPv6Temporary { get; private set; }
/// <summary>
/// Gets the link local IPv6 address
/// </summary>
internal string IPv6LinkLocal { get; private set; }
/// <summary>
/// Gets the site local IPv6 address
/// </summary>
internal string IPv6SiteLocal { get; private set; }
/// <summary>
/// Gets the unique local IPv6 address
/// </summary>
internal string IPv6UniqueLocal { get; private set; }
/// <summary>
/// Gets the list of gateway IPs as string
/// </summary>
internal List<string> Gateways { get; private set; } = new List<string>();
/// <summary>
/// Gets the list of DHCP server IPs as string
/// </summary>
internal List<string> DhcpServers { get; private set; } = new List<string>();
/// <summary>
/// Gets the list of DNS server IPs as string
/// </summary>
internal List<string> DnsServers { get; private set; } = new List<string>();
/// <summary>
/// Gets the list of WINS server IPs as string
/// </summary>
internal List<string> WinsServers { get; private set; } = new List<string>();
/// <summary>
/// Initializes a new instance of the <see cref="NetworkConnectionProperties"/> class.
/// </summary>
/// <param name="networkInterface">Network interface of the connection</param>
internal NetworkConnectionProperties(NetworkInterface networkInterface)
{
// Setting adapter properties
Adapter = networkInterface.Description;
PhysicalAddress = networkInterface.GetPhysicalAddress().ToString();
Type = networkInterface.NetworkInterfaceType;
Speed = networkInterface.Speed;
State = networkInterface.OperationalStatus;
// Connection properties
ConnectionName = networkInterface.Name;
if (State == OperationalStatus.Up)
{
Suffix = networkInterface.GetIPProperties().DnsSuffix;
SetIpProperties(networkInterface.GetIPProperties());
}
}
/// <summary>
/// Gets a formatted string with the adapter details
/// </summary>
/// <returns>String with the details</returns>
internal string GetAdapterDetails()
{
return $"{Resources.Microsoft_plugin_sys_AdapterName}: {Adapter}" +
$"\n{Resources.Microsoft_plugin_sys_PhysicalAddress}: {PhysicalAddress}" +
$"\n{Resources.Microsoft_plugin_sys_Speed}: {GetFormattedSpeedValue(Speed)}" +
$"\n{Resources.Microsoft_plugin_sys_Type}: {GetAdapterTypeAsString(Type)}" +
$"\n{Resources.Microsoft_plugin_sys_State}: " + (State == OperationalStatus.Up ? Resources.Microsoft_plugin_sys_Connected : Resources.Microsoft_plugin_sys_Disconnected) +
$"\n{Resources.Microsoft_plugin_sys_ConnectionName}: {ConnectionName}";
}
/// <summary>
/// Returns a formatted string with the connection details
/// </summary>
/// <returns>String with the details</returns>
internal string GetConnectionDetails()
{
return $"{Resources.Microsoft_plugin_sys_ConnectionName}: {ConnectionName}" +
$"\n{Resources.Microsoft_plugin_sys_State}: " + (State == OperationalStatus.Up ? Resources.Microsoft_plugin_sys_Connected : Resources.Microsoft_plugin_sys_Disconnected) +
$"\n{Resources.Microsoft_plugin_sys_Type}: {GetAdapterTypeAsString(Type)}" +
$"\n{Resources.Microsoft_plugin_sys_Suffix}: {Suffix}" +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Ip4Address}: ", IPv4) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Ip4SubnetMask}: ", IPv4Mask) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Ip6Address}:\n\t", IPv6Global) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Ip6Temp}:\n\t", IPv6Temporary) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Ip6Link}:\n\t", IPv6LinkLocal) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Ip6Site}:\n\t", IPv6SiteLocal) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Ip6Unique}:\n\t", IPv6UniqueLocal) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Gateways}:\n\t", Gateways) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Dhcp}:\n\t", DhcpServers) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Dns}:\n\t", DnsServers) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Wins}:\n\t", WinsServers) +
$"\n\n{Resources.Microsoft_plugin_sys_AdapterName}: {Adapter}" +
$"\n{Resources.Microsoft_plugin_sys_PhysicalAddress}: {PhysicalAddress}" +
$"\n{Resources.Microsoft_plugin_sys_Speed}: {GetFormattedSpeedValue(Speed)}";
}
/// <summary>
/// Set the ip address properties of the <see cref="NetworkConnectionProperties"/> instance.
/// </summary>
/// <param name="properties">Element of the type <see cref="IPInterfaceProperties"/>.</param>
private void SetIpProperties(IPInterfaceProperties properties)
{
var ipList = properties.UnicastAddresses;
foreach (var ip in ipList)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
{
IPv4 = ip.Address.ToString();
IPv4Mask = ip.IPv4Mask.ToString();
}
else if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
{
if (string.IsNullOrEmpty(IPv6Primary))
{
IPv6Primary = ip.Address.ToString();
}
if (ip.Address.IsIPv6LinkLocal)
{
IPv6LinkLocal = ip.Address.ToString();
}
else if (ip.Address.IsIPv6SiteLocal)
{
IPv6SiteLocal = ip.Address.ToString();
}
else if (ip.Address.IsIPv6UniqueLocal)
{
IPv6UniqueLocal = ip.Address.ToString();
}
else if (ip.SuffixOrigin == SuffixOrigin.Random)
{
IPv6Temporary = ip.Address.ToString();
}
else
{
IPv6Global = ip.Address.ToString();
}
}
}
foreach (var ip in properties.GatewayAddresses)
{
Gateways.Add(ip.Address.ToString());
}
foreach (var ip in properties.DhcpServerAddresses)
{
DhcpServers.Add(ip.ToString());
}
foreach (var ip in properties.DnsAddresses)
{
DnsServers.Add(ip.ToString());
}
foreach (var ip in properties.WinsServersAddresses)
{
WinsServers.Add(ip.ToString());
}
}
/// <summary>
/// Gets the interface type as string
/// </summary>
/// <param name="type">The type to convert</param>
/// <returns>A string indicating the interface type</returns>
private string GetAdapterTypeAsString(NetworkInterfaceType type)
{
switch (type)
{
case NetworkInterfaceType.Wman:
case NetworkInterfaceType.Wwanpp:
case NetworkInterfaceType.Wwanpp2:
return Resources.Microsoft_plugin_sys_MobileBroadband;
case NetworkInterfaceType.Wireless80211:
return Resources.Microsoft_plugin_sys_WirelessLan;
case NetworkInterfaceType.Loopback:
return Resources.Microsoft_plugin_sys_Loopback;
case NetworkInterfaceType.Tunnel:
return Resources.Microsoft_plugin_sys_TunnelConnection;
case NetworkInterfaceType.Unknown:
return Resources.Microsoft_plugin_sys_Unknown;
default:
return Resources.Microsoft_plugin_sys_Cable;
}
}
/// <summary>
/// Gets the speed as formatted text value
/// </summary>
/// <param name="speed">The adapter speed as <see langword="long"/>.</param>
/// <returns>A formatted string like `100 MB/s`</returns>
private static string GetFormattedSpeedValue(long speed)
{
return (speed >= 1000000000) ? string.Format(CultureInfo.InvariantCulture, Resources.Microsoft_plugin_sys_Gbps, speed / 1000000000) : string.Format(CultureInfo.InvariantCulture, Resources.Microsoft_plugin_sys_Mbps, speed / 1000000);
}
/// <summary>
/// Returns IP info or an empty string
/// </summary>
/// <param name="title">Descriptive header for the information.</param>
/// <param name="property">IP value as <see cref="string"/> or <see cref="List{String}"/>.</param>
/// <returns>Formatted string or an empty string.</returns>
/// <exception cref="ArgumentException">If the parameter <paramref name="property"/> is not of the type <see cref="string"/> or <see cref="List{String}"/>.</exception>
private static string CreateIpInfoForDetailsText(string title, dynamic property)
{
if (property is string)
{
return $"\n{title}{property}";
}
else if (property is List<string> list)
{
return list.Count == 0 ? string.Empty : $"\n{title}{string.Join("\n\t", property)}";
}
else if (property is null)
{
return string.Empty;
}
else
{
throw new ArgumentException($"'{property}' is not of type 'string' or 'List<string>'.", nameof(property));
}
}
}
}

View File

@@ -0,0 +1,78 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using Microsoft.PowerToys.Run.Plugin.System.Properties;
using Wox.Infrastructure;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Microsoft.PowerToys.Run.Plugin.System.Components
{
internal static class ResultHelper
{
internal static bool ExecuteCommand(bool confirm, string confirmationMessage, Action command)
{
if (confirm)
{
MessageBoxResult messageBoxResult = MessageBox.Show(
confirmationMessage,
Resources.Microsoft_plugin_sys_confirmation,
MessageBoxButton.YesNo,
MessageBoxImage.Warning);
if (messageBoxResult == MessageBoxResult.No)
{
return false;
}
}
command();
return true;
}
internal static bool CopyToClipBoard(in string text)
{
try
{
Clipboard.Clear();
Clipboard.SetText(text);
return true;
}
catch (Exception exception)
{
Log.Exception("Can't copy to clipboard", exception, typeof(ResultHelper));
return false;
}
}
internal static List<ContextMenuResult> GetContextMenuForResult(Result result)
{
var contextMenu = new List<ContextMenuResult>();
if (!(result?.ContextData is SystemPluginContext contextData))
{
return contextMenu;
}
if (contextData.Type == ResultContextType.NetworkAdapterInfo)
{
contextMenu.Add(new ContextMenuResult()
{
AcceleratorKey = Key.C,
AcceleratorModifiers = ModifierKeys.Control,
FontFamily = "Segoe MDL2 Assets",
Glyph = "\xE8C8", // E8C8 => Symbol: Copy
Title = Resources.Microsoft_plugin_sys_CopyDetails,
Action = _ => ResultHelper.CopyToClipBoard(contextData.Data),
});
}
return contextMenu;
}
}
}

View File

@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.PowerToys.Run.Plugin.System.Components
{
internal class SystemPluginContext
{
/// <summary>
/// Gets or sets the type of the result
/// </summary>
public ResultContextType Type { get; set; }
/// <summary>
/// Gets or sets the context data for the command/results
/// </summary>
public string Data { get; set; }
}
internal enum ResultContextType
{
Command, // Reserved for later usage
NetworkAdapterInfo,
}
}