view ServerMonitor/Helpers.cs @ 14:2db36ab759de

Add comments.
author Brad Greco <brad@bgreco.net>
date Mon, 22 Apr 2019 21:10:42 -0400
parents 052aa62cb42a
children 68d7834dc28e
line wrap: on
line source

using Renci.SshNet;
using Renci.SshNet.Common;
using ServerMonitorApp.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Windows.Forms;

namespace ServerMonitorApp
{
    static class Helpers
    {
        public static void FormatImageButton(Button button)
        {
            button.Image = new Bitmap(button.Image, button.Height - 8, button.Height - 8);
        }

        public static string GetAllMessages(this Exception ex)
        {
            return "\t" + (ex.InnerException == null ? ex.Message : ex.Message + Environment.NewLine + ex.InnerException.GetAllMessages());
        }

        public static string GetDisplayName(Type type)
        {
            return type?.GetAttribute<DisplayNameAttribute>()?.DisplayName ?? type?.Name ?? "null";
        }

        public static bool IsNullOrEmpty(this string aString)
        {
            return aString == null || aString.Trim() == string.Empty;
        }

        public static string ConvertNewlines(this string aString)
        {
            return aString.Replace("\r\n", "\n").Replace('\r', '\n');
        }

        public static T GetAttribute<T>(this Type type) where T : Attribute
        {
            return type.GetCustomAttributes(typeof(T), false).SingleOrDefault() as T;
        }

        public static Image GetImage(this CheckStatus checkStatus)
        {
            switch (checkStatus)
            {
                case CheckStatus.Error: return Resources.error;
                case CheckStatus.Warning: return Resources.warning;
                case CheckStatus.Information: return Resources.info;
                case CheckStatus.Success: return Resources.pass;
                case CheckStatus.Running: return Resources.run;
                case CheckStatus.Disabled: return Resources.disable;
                default: return null;
            }
        }

        public static Icon GetIcon(this CheckStatus checkStatus)
        {
            switch (checkStatus)
            {
                case CheckStatus.Error: return Resources.icon_error;
                case CheckStatus.Warning: return Resources.icon_warning;
                case CheckStatus.Information: return Resources.icon_info;
                default: return Resources.icon;
            }
        }

        public static bool In(this Enum value, params Enum[] values) {
            return values.Contains(value);
        }

        // https://stackoverflow.com/a/8345173
        /// <summary>
        /// Indicates whether any network connection is available.
        /// Filter connections below a specified speed, as well as 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.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
                    (ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
                    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;
        }
    }
}