From 8b6543592405db54f18bb31dbcad421265192aff Mon Sep 17 00:00:00 2001 From: Kamil118 Date: Sat, 22 Aug 2026 14:46:48 +0200 Subject: [PATCH] Updated Licensing, Improved error messaging, added confirmation for actions scheduled less than 15s in advance, Added restart option. --- GoToSleep/GoToSleep.Designer.cs | 23 ++++-- GoToSleep/GoToSleep.cs | 21 ++++- GoToSleep/PowerDownData.cs | 10 +++ GoToSleep/SystemPowerManager.cs | 60 +++++++++++++- LICENSE.txt | 2 +- THIRD-PARTY-NOTICES.txt | 142 ++++++++++++++++++++++++++++++++ 6 files changed, 246 insertions(+), 12 deletions(-) create mode 100644 THIRD-PARTY-NOTICES.txt diff --git a/GoToSleep/GoToSleep.Designer.cs b/GoToSleep/GoToSleep.Designer.cs index 07ce602..8920b5e 100644 --- a/GoToSleep/GoToSleep.Designer.cs +++ b/GoToSleep/GoToSleep.Designer.cs @@ -36,6 +36,7 @@ textBox1 = new TextBox(); tableLayoutPanel2 = new TableLayoutPanel(); flowLayoutPanel1 = new FlowLayoutPanel(); + button1 = new Button(); tableLayoutPanel2.SuspendLayout(); flowLayoutPanel1.SuspendLayout(); SuspendLayout(); @@ -53,7 +54,7 @@ // // shutdownButton // - shutdownButton.Location = new Point(185, 3); + shutdownButton.Location = new Point(263, 3); shutdownButton.Margin = new Padding(3, 3, 0, 3); shutdownButton.Name = "shutdownButton"; shutdownButton.Size = new Size(75, 23); @@ -64,7 +65,7 @@ // // suspendButton // - suspendButton.Location = new Point(104, 3); + suspendButton.Location = new Point(182, 3); suspendButton.Name = "suspendButton"; suspendButton.Size = new Size(75, 23); suspendButton.TabIndex = 2; @@ -74,7 +75,7 @@ // // hibernateButton // - hibernateButton.Location = new Point(23, 3); + hibernateButton.Location = new Point(101, 3); hibernateButton.Name = "hibernateButton"; hibernateButton.Size = new Size(75, 23); hibernateButton.TabIndex = 3; @@ -114,12 +115,23 @@ flowLayoutPanel1.Controls.Add(shutdownButton); flowLayoutPanel1.Controls.Add(suspendButton); flowLayoutPanel1.Controls.Add(hibernateButton); + flowLayoutPanel1.Controls.Add(button1); flowLayoutPanel1.FlowDirection = FlowDirection.RightToLeft; - flowLayoutPanel1.Location = new Point(221, 33); + flowLayoutPanel1.Location = new Point(143, 33); flowLayoutPanel1.Name = "flowLayoutPanel1"; - flowLayoutPanel1.Size = new Size(260, 41); + flowLayoutPanel1.Size = new Size(338, 41); flowLayoutPanel1.TabIndex = 7; // + // button1 + // + button1.Location = new Point(20, 3); + button1.Name = "button1"; + button1.Size = new Size(75, 23); + button1.TabIndex = 4; + button1.Text = "Restart"; + button1.UseVisualStyleBackColor = true; + button1.Click += restart_Click; + // // GoToSleep // AutoScaleDimensions = new SizeF(7F, 15F); @@ -145,5 +157,6 @@ private TextBox textBox1; private TableLayoutPanel tableLayoutPanel2; private FlowLayoutPanel flowLayoutPanel1; + private Button button1; } } diff --git a/GoToSleep/GoToSleep.cs b/GoToSleep/GoToSleep.cs index e06fec1..dac282a 100644 --- a/GoToSleep/GoToSleep.cs +++ b/GoToSleep/GoToSleep.cs @@ -41,8 +41,9 @@ namespace GoToSleep { manager.shutdown(getTimeString()); } - catch (Exception ex) { - MessageBox.Show(ex.Message); + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -54,7 +55,7 @@ namespace GoToSleep } catch (Exception ex) { - MessageBox.Show(ex.Message); + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -66,7 +67,19 @@ namespace GoToSleep } catch (Exception ex) { - MessageBox.Show(ex.Message); + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void restart_Click(object sender, EventArgs e) + { + try + { + manager.restart(getTimeString()); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } diff --git a/GoToSleep/PowerDownData.cs b/GoToSleep/PowerDownData.cs index cc77ff6..8abd908 100644 --- a/GoToSleep/PowerDownData.cs +++ b/GoToSleep/PowerDownData.cs @@ -38,6 +38,16 @@ namespace GoToSleep Process.Start(psi); }); } + + public static PowerDownData Restart(DateTime when) + { + return new PowerDownData(PowerDownType.Shutdown, when, () => { + var psi = new ProcessStartInfo("shutdown", "/r /t 0"); + psi.CreateNoWindow = true; + psi.UseShellExecute = false; + Process.Start(psi); + }); + } public static PowerDownData Suspend(DateTime when) { return new PowerDownData(PowerDownType.Suspend, when, () => { diff --git a/GoToSleep/SystemPowerManager.cs b/GoToSleep/SystemPowerManager.cs index ab54219..2ec3153 100644 --- a/GoToSleep/SystemPowerManager.cs +++ b/GoToSleep/SystemPowerManager.cs @@ -13,11 +13,14 @@ namespace GoToSleep { internal class SystemPowerManager { + private static int TIMER_WARNING_SECONDS = 15; + + [Serializable] class DatetimeStringException : Exception { public DatetimeStringException(string message) : base(message) { } - public DatetimeStringException(Exception e) : base("Conversion failed.", e) { } + public DatetimeStringException(Exception e) : base("Conversion from string to time failed.", e) { } } [Serializable] @@ -62,7 +65,6 @@ namespace GoToSleep try { timeString = prepareTimeString(timeString); - //var tmp = parser.Parse(timeString); List? results = null; @@ -77,6 +79,7 @@ namespace GoToSleep if (results == null || results.Count == 0) { + //try converting using english as fallback results = DateTimeRecognizer.RecognizeDateTime(timeString, Culture.English); if (results == null || results.Count == 0) { @@ -104,6 +107,21 @@ namespace GoToSleep return result; } + bool confirmDate(DateTime target, string action) + { + if (target <= DateTime.Now.AddSeconds(TIMER_WARNING_SECONDS)) + { + //the number will be always off by a fraction of a second, so round it up + if (MessageBox.Show($"Are you sure you want to schedule {action} in just {Math.Ceiling((target - DateTime.Now).TotalSeconds)} seconds?", "Confirm", MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes) + { + return true; + } + return false; + } + //warning threshold not met + return true; + } + public void shutdown(string timeString) { DateTime target; @@ -116,6 +134,11 @@ namespace GoToSleep throw; } + if(!confirmDate(target,"shutdown")) + { + return; + } + using (var progressPopup = new CountdownForm(PowerDownData.Shutdown(target))) { progressPopup.ShowDialog(parentForm); @@ -134,6 +157,11 @@ namespace GoToSleep throw; } + if (!confirmDate(target, "suspension")) + { + return; + } + using (var progressPopup = new CountdownForm(PowerDownData.Suspend(target))) { progressPopup.ShowDialog(parentForm); @@ -152,10 +180,38 @@ namespace GoToSleep throw; } + if (!confirmDate(target, "hibernation")) + { + return; + } + using (var progressPopup = new CountdownForm(PowerDownData.Hibernate(target))) { progressPopup.ShowDialog(parentForm); } } + + public void restart(string timeString) + { + DateTime target; + try + { + target = getTimeOffsetFromString(timeString); + } + catch (Exception) + { + throw; + } + + if (!confirmDate(target, "restart")) + { + return; + } + + using (var progressPopup = new CountdownForm(PowerDownData.Restart(target))) + { + progressPopup.ShowDialog(parentForm); + } + } } } diff --git a/LICENSE.txt b/LICENSE.txt index 8aa2645..23005e5 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,6 +1,6 @@ MIT License -Copyright (c) [year] [fullname] +Copyright (c) 2026 Kamil118 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/THIRD-PARTY-NOTICES.txt b/THIRD-PARTY-NOTICES.txt new file mode 100644 index 0000000..17c7d3d --- /dev/null +++ b/THIRD-PARTY-NOTICES.txt @@ -0,0 +1,142 @@ +License notice for Microsoft.Recognizers.Text (v1.8.13) +------------------------------------ + +https://github.com/Microsoft/Recognizers-Text + +Copyright © Microsoft Corporation. All rights reserved. + +Licensed under MIT + +Available at https://licenses.nuget.org/MIT + + +License notice for Microsoft.Recognizers.Text.DateTime (v1.8.13) +------------------------------------ + +https://github.com/Microsoft/Recognizers-Text + +Copyright © Microsoft Corporation. All rights reserved. + +Licensed under MIT + +Available at https://licenses.nuget.org/MIT + + +License notice for Microsoft.Extensions.Caching.Abstractions (v2.2.0) +------------------------------------ + +https://github.com/aspnet/Extensions at 9bc79b2f25a3724376d7af19617c33749a30ea3a + +https://asp.net/ + +Copyright © Microsoft Corporation. All rights reserved. + +License available at https://raw.githubusercontent.com/aspnet/AspNetCore/2.0.0/LICENSE.txt + + +License notice for Microsoft.Extensions.Caching.Memory (v2.2.0) +------------------------------------ + +https://github.com/aspnet/Extensions at 9bc79b2f25a3724376d7af19617c33749a30ea3a + +https://asp.net/ + +Copyright © Microsoft Corporation. All rights reserved. + +License available at https://raw.githubusercontent.com/aspnet/AspNetCore/2.0.0/LICENSE.txt + + +License notice for Microsoft.Extensions.DependencyInjection.Abstractions (v2.2.0) +------------------------------------ + +https://github.com/aspnet/Extensions at 9bc79b2f25a3724376d7af19617c33749a30ea3a + +https://asp.net/ + +Copyright © Microsoft Corporation. All rights reserved. + +License available at https://raw.githubusercontent.com/aspnet/AspNetCore/2.0.0/LICENSE.txt + + +License notice for Microsoft.Extensions.Options (v2.2.0) +------------------------------------ + +https://github.com/aspnet/Extensions at 9bc79b2f25a3724376d7af19617c33749a30ea3a + +https://asp.net/ + +Copyright © Microsoft Corporation. All rights reserved. + +License available at https://raw.githubusercontent.com/aspnet/AspNetCore/2.0.0/LICENSE.txt + + +License notice for Microsoft.Extensions.Primitives (v2.2.0) +------------------------------------ + +https://github.com/aspnet/Extensions at 9bc79b2f25a3724376d7af19617c33749a30ea3a + +https://asp.net/ + +Copyright © Microsoft Corporation. All rights reserved. + +License available at https://raw.githubusercontent.com/aspnet/AspNetCore/2.0.0/LICENSE.txt + + +License notice for Microsoft.Recognizers.Definitions (v1.8.13) +------------------------------------ + +https://github.com/Microsoft/Recognizers-Text + +Copyright © Microsoft Corporation. All rights reserved. + +Licensed under MIT + +Available at https://licenses.nuget.org/MIT + + +License notice for Microsoft.Recognizers.Text.DataTypes.TimexExpression (v1.8.13) +------------------------------------ + +https://github.com/Microsoft/Recognizers-Text + +Copyright © Microsoft Corporation. All rights reserved. + +Licensed under MIT + +Available at https://licenses.nuget.org/MIT + + +License notice for Microsoft.Recognizers.Text.Number (v1.8.13) +------------------------------------ + +https://github.com/Microsoft/Recognizers-Text + +Copyright © Microsoft Corporation. All rights reserved. + +Licensed under MIT + +Available at https://licenses.nuget.org/MIT + + +License notice for Microsoft.Recognizers.Text.NumberWithUnit (v1.8.13) +------------------------------------ + +https://github.com/Microsoft/Recognizers-Text + +Copyright © Microsoft Corporation. All rights reserved. + +Licensed under MIT + +Available at https://licenses.nuget.org/MIT + + +License notice for System.ComponentModel.Annotations (v4.5.0) +------------------------------------ + +https://dot.net/ + +Copyright © Microsoft Corporation. All rights reserved. + +License available at https://github.com/dotnet/corefx/blob/master/LICENSE.TXT + +