diff --git a/GoToSleep/PowerDownData.cs b/GoToSleep/PowerDownData.cs index 8abd908..7ae8b05 100644 --- a/GoToSleep/PowerDownData.cs +++ b/GoToSleep/PowerDownData.cs @@ -11,24 +11,30 @@ namespace GoToSleep { Shutdown, Suspend, - Hibernate + Hibernate, + Restart }; public PowerDownType actionType { get; } public DateTime when { get; } - public delegate void Action(); - public Action onCountdownEnd { get; } + // Private constructor to enforce actionType and Action consistency private PowerDownData(PowerDownType _type, DateTime _when, Action action) { actionType = _type; when = _when; onCountdownEnd = action; } + + /// + /// Creates PowerDownData for Shutdown action scheduled at specified time + /// + /// The date and time when the action is supposed to trigger + /// A new PowerDownData configured for Shutdown public static PowerDownData Shutdown(DateTime when) { return new PowerDownData(PowerDownType.Shutdown, when, () => { @@ -39,21 +45,38 @@ namespace GoToSleep }); } + /// + /// Creates PowerDownData for Restart action scheduled at specified time + /// + /// The date and time when the action is supposed to trigger + /// A new PowerDownData configured for Restart public static PowerDownData Restart(DateTime when) { - return new PowerDownData(PowerDownType.Shutdown, when, () => { + return new PowerDownData(PowerDownType.Restart, when, () => { var psi = new ProcessStartInfo("shutdown", "/r /t 0"); psi.CreateNoWindow = true; psi.UseShellExecute = false; Process.Start(psi); }); } + + /// + /// Creates PowerDownData for Suspend action scheduled at specified time + /// + /// The date and time when the action is supposed to trigger + /// A new PowerDownData configured for Suspend public static PowerDownData Suspend(DateTime when) { return new PowerDownData(PowerDownType.Suspend, when, () => { Application.SetSuspendState(PowerState.Suspend, false, false); }); } + + /// + /// Creates PowerDownData for Hibernate action scheduled at specified time + /// + /// The date and time when the action is supposed to trigger + /// A new PowerDownData configured for Hibernate public static PowerDownData Hibernate(DateTime when) { return new PowerDownData(PowerDownType.Hibernate, when, () => { diff --git a/GoToSleep/SystemPowerManager.cs b/GoToSleep/SystemPowerManager.cs index 1eb57b4..46503c3 100644 --- a/GoToSleep/SystemPowerManager.cs +++ b/GoToSleep/SystemPowerManager.cs @@ -13,16 +13,32 @@ namespace GoToSleep { internal class SystemPowerManager { - private static int TIMER_WARNING_SECONDS = 15; + /// + /// Static value specifying the minimum delay (in seconds) of action execution without a warning. + /// + /// + /// If user specifies time that's delayed by less than this number of seconds, ask for confirmation. + /// Exists to protect user from typos or interpreter errors that would schedule an action + /// that executes before user can notice the mistake and cancel it. + /// + /// Selected unilaterally by a committee of 1 developers. + private const int TIMER_WARNING_SECONDS = 15; + /// + /// Base exception for when the string can't be converted into a valid object. + /// [Serializable] - class DatetimeStringException : Exception + abstract class DatetimeStringException : Exception { public DatetimeStringException(string message) : base(message) { } public DatetimeStringException(Exception e) : base("Conversion from string to time failed.", e) { } } + /// + /// The exception that is thrown when the time string is converted into a object that references + /// a point of time in the past. + /// [Serializable] class DateTimeBeforeException : DatetimeStringException { @@ -31,12 +47,20 @@ namespace GoToSleep } } + /// + /// The exception that's thrown when the time string couldn't be converted into any . + /// + [Serializable] class ParseFailedException : DatetimeStringException { public ParseFailedException(Exception e) : base(e) { } } + /// + /// Initializes a new instance of the class with a reference to the main window. + /// + /// main window instance. public SystemPowerManager(GoToSleep pf) { parentForm = pf; @@ -45,6 +69,12 @@ namespace GoToSleep CountdownForm? progressPopup; + + /// + /// Prepares an input time string to improve compatibility with Microsoft.Recognizers.Text + /// + /// Time string to be preprocessed + /// Time string modified for better compatibility with Microsoft.Recognizers.Text parser string prepareTimeString(string s) { // make sure there are spaces between numbers and units, for the parser @@ -61,6 +91,14 @@ namespace GoToSleep return s; } + /// + /// Converts natural language time string to Datetime object + /// + /// String containing natural language time definition + /// new Datetime object referencing the time specified in + /// Thrown when general parsing failure + /// Thrown when the input string references point of time in the past + DateTime getTimeOffsetFromString(string timeString) { DateTime result; @@ -109,6 +147,15 @@ namespace GoToSleep return result; } + /// + /// Prompts user to confirm the scheduling of action if it's scheduled to happen less than + /// in the future. + /// + /// The Datetime the action is scheduled to execute. + /// The name of action that is about to be scheduled. + /// if the date doesn't meet warning requirements, or the user confirmed the intent. + /// if the user clicked cancel or closed the prompt window. + bool confirmDate(DateTime target, string action) { if (target <= DateTime.Now.AddSeconds(TIMER_WARNING_SECONDS)) @@ -124,6 +171,10 @@ namespace GoToSleep return true; } + /// + /// Displays a new window for scheduled shutdown action. + /// + /// Time string refrencing when the action should be executed. public void shutdown(string timeString) { DateTime target; @@ -145,6 +196,10 @@ namespace GoToSleep progressPopup.Show(); } + /// + /// Displays a new window for scheduled suspend action. + /// + /// Time string refrencing when the action should be executed. public void suspend(string timeString) { DateTime target; @@ -166,6 +221,10 @@ namespace GoToSleep progressPopup.Show(); } + /// + /// Displays a new window for scheduled hibernate action. + /// + /// Time string refrencing when the action should be executed. public void hibernate(string timeString) { DateTime target; @@ -187,6 +246,10 @@ namespace GoToSleep progressPopup.Show(); } + /// + /// Displays a new window for scheduled restart action. + /// + /// Time string refrencing when the action should be executed. public void restart(string timeString) { DateTime target;