comparison ServerMonitor/Helpers.cs @ 17:68d7834dc28e

More comments.
author Brad Greco <brad@bgreco.net>
date Sat, 25 May 2019 15:14:26 -0400
parents 052aa62cb42a
children 7645122aa7a9
comparison
equal deleted inserted replaced
16:7626b099aefd 17:68d7834dc28e
1 using Renci.SshNet; 1 using ServerMonitorApp.Properties;
2 using Renci.SshNet.Common;
3 using ServerMonitorApp.Properties;
4 using System; 2 using System;
5 using System.Collections.Generic;
6 using System.ComponentModel; 3 using System.ComponentModel;
7 using System.Drawing; 4 using System.Drawing;
8 using System.Linq; 5 using System.Linq;
9 using System.Net.NetworkInformation; 6 using System.Net.NetworkInformation;
10 using System.Text;
11 using System.Windows.Forms; 7 using System.Windows.Forms;
12 8
13 namespace ServerMonitorApp 9 namespace ServerMonitorApp
14 { 10 {
15 static class Helpers 11 static class Helpers
16 { 12 {
13 /// <summary>Resizes the image on a button to fit inside the button's dimensions.</summary>
14 /// <param name="button">A button containing an image.</param>
17 public static void FormatImageButton(Button button) 15 public static void FormatImageButton(Button button)
18 { 16 {
19 button.Image = new Bitmap(button.Image, button.Height - 8, button.Height - 8); 17 button.Image = new Bitmap(button.Image, button.Height - 8, button.Height - 8);
20 } 18 }
21 19
20 /// <summary>Recursively collects the messages for an exception and its inner exceptions.</summary>
21 /// <param name="ex">The parent exception.</param>
22 /// <returns>A string containing the messages for the exception and its inner exceptions.</returns>
22 public static string GetAllMessages(this Exception ex) 23 public static string GetAllMessages(this Exception ex)
23 { 24 {
24 return "\t" + (ex.InnerException == null ? ex.Message : ex.Message + Environment.NewLine + ex.InnerException.GetAllMessages()); 25 return "\t" + (ex.InnerException == null ? ex.Message : ex.Message + Environment.NewLine + ex.InnerException.GetAllMessages());
25 } 26 }
26 27
28 /// <summary>Gets the value of the DisplayNameAttribute for a type.</summary>
29 /// <param name="type">The type to get the display name of.</param>
30 /// <returns>The type's display name, or the type name if no display name is defined.</returns>
27 public static string GetDisplayName(Type type) 31 public static string GetDisplayName(Type type)
28 { 32 {
29 return type?.GetAttribute<DisplayNameAttribute>()?.DisplayName ?? type?.Name ?? "null"; 33 return type?.GetAttribute<DisplayNameAttribute>()?.DisplayName ?? type?.Name ?? "null";
30 } 34 }
31 35
36 /// <summary>Checks whether a string is null, an empty string, or only contains white space.</summary>
37 /// <param name="aString">The string to test.</param>
38 /// <returns>True if the string is null, an empty string, or only contains white space. False otherwise.</returns>
32 public static bool IsNullOrEmpty(this string aString) 39 public static bool IsNullOrEmpty(this string aString)
33 { 40 {
34 return aString == null || aString.Trim() == string.Empty; 41 return aString == null || aString.Trim() == string.Empty;
35 } 42 }
36 43
44 /// <summary>Converts all newlines in a string to unix format.</summary>
45 /// <param name="aString">The string to convert.</param>
46 /// <returns>The string with all newlines converted to unix format.</returns>
37 public static string ConvertNewlines(this string aString) 47 public static string ConvertNewlines(this string aString)
38 { 48 {
39 return aString.Replace("\r\n", "\n").Replace('\r', '\n'); 49 return aString.Replace("\r\n", "\n").Replace('\r', '\n');
40 } 50 }
41 51
52 /// <summary>Gets an attribute on a class.</summary>
53 /// <typeparam name="T">The type of the attribute to return.</typeparam>
54 /// <param name="type">The type of the class the attribute is on.</param>
55 /// <returns>The attribute, or null if the attribute does not exist on the class.</returns>
42 public static T GetAttribute<T>(this Type type) where T : Attribute 56 public static T GetAttribute<T>(this Type type) where T : Attribute
43 { 57 {
44 return type.GetCustomAttributes(typeof(T), false).SingleOrDefault() as T; 58 return type.GetCustomAttributes(typeof(T), false).SingleOrDefault() as T;
45 } 59 }
46 60
61 /// <summary>Gets an image associated with a check status for use in the UI.</summary>
62 /// <param name="checkStatus">The check status.</param>
63 /// <returns>The image associated with the check status.</returns>
47 public static Image GetImage(this CheckStatus checkStatus) 64 public static Image GetImage(this CheckStatus checkStatus)
48 { 65 {
49 switch (checkStatus) 66 switch (checkStatus)
50 { 67 {
51 case CheckStatus.Error: return Resources.error; 68 case CheckStatus.Error: return Resources.error;
56 case CheckStatus.Disabled: return Resources.disable; 73 case CheckStatus.Disabled: return Resources.disable;
57 default: return null; 74 default: return null;
58 } 75 }
59 } 76 }
60 77
78 /// <summary>Gets a program icon associated with a check status.</summary>
79 /// <param name="checkStatus">The check status.</param>
80 /// <returns>The program icon associated with the check status.</returns>
61 public static Icon GetIcon(this CheckStatus checkStatus) 81 public static Icon GetIcon(this CheckStatus checkStatus)
62 { 82 {
63 switch (checkStatus) 83 switch (checkStatus)
64 { 84 {
65 case CheckStatus.Error: return Resources.icon_error; 85 case CheckStatus.Error: return Resources.icon_error;
67 case CheckStatus.Information: return Resources.icon_info; 87 case CheckStatus.Information: return Resources.icon_info;
68 default: return Resources.icon; 88 default: return Resources.icon;
69 } 89 }
70 } 90 }
71 91
92 /// <summary>Returns whether an enum is in a list of values.</summary>
93 /// <param name="value">The value to check.</param>
94 /// <param name="values">The list of possible values.</param>
95 /// <returns>True if the value was in the list, false otherwise.</returns>
72 public static bool In(this Enum value, params Enum[] values) { 96 public static bool In(this Enum value, params Enum[] values) {
73 return values.Contains(value); 97 return values.Contains(value);
74 } 98 }
75 99
76 // https://stackoverflow.com/a/8345173 100 // https://stackoverflow.com/a/8345173