commit 9bb6d807828c58a6aca38d279c4d1991db14447d Author: Dunestorm Date: Tue Mar 5 21:01:17 2019 +0000 Imported Version 1.0.0.0 diff --git a/TimedShutdown.sln b/TimedShutdown.sln new file mode 100644 index 0000000..0ae9497 --- /dev/null +++ b/TimedShutdown.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27130.2027 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TimedShutdown", "TimedShutdown\TimedShutdown.csproj", "{C1158C45-7666-4586-A6DA-CCC1EE95D457}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C1158C45-7666-4586-A6DA-CCC1EE95D457}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C1158C45-7666-4586-A6DA-CCC1EE95D457}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C1158C45-7666-4586-A6DA-CCC1EE95D457}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C1158C45-7666-4586-A6DA-CCC1EE95D457}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7E8B84F7-7D30-4B6A-9B9D-D9ED6C001280} + EndGlobalSection +EndGlobal diff --git a/TimedShutdown/App.config b/TimedShutdown/App.config new file mode 100644 index 0000000..731f6de --- /dev/null +++ b/TimedShutdown/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/TimedShutdown/App.xaml b/TimedShutdown/App.xaml new file mode 100644 index 0000000..84688d8 --- /dev/null +++ b/TimedShutdown/App.xaml @@ -0,0 +1,14 @@ + + + + + + + + + + + diff --git a/TimedShutdown/App.xaml.cs b/TimedShutdown/App.xaml.cs new file mode 100644 index 0000000..bf7ffa9 --- /dev/null +++ b/TimedShutdown/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace TimedShutdown +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/TimedShutdown/Bootstrapper.cs b/TimedShutdown/Bootstrapper.cs new file mode 100644 index 0000000..32b3599 --- /dev/null +++ b/TimedShutdown/Bootstrapper.cs @@ -0,0 +1,19 @@ +using System.Windows; +using Caliburn.Micro; +using TimedShutdown.ViewModels; + +namespace TimedShutdown +{ + public class Bootstrapper : BootstrapperBase + { + public Bootstrapper() + { + Initialize(); + } + + protected override void OnStartup(object sender, StartupEventArgs e) + { + DisplayRootViewFor(); + } + } +} diff --git a/TimedShutdown/Properties/AssemblyInfo.cs b/TimedShutdown/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..737b87e --- /dev/null +++ b/TimedShutdown/Properties/AssemblyInfo.cs @@ -0,0 +1,55 @@ +using System.Reflection; +using System.Resources; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Windows; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Timed Shutdown")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Fil Sapia")] +[assembly: AssemblyProduct("Timed Shutdown")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +//In order to begin building localizable applications, set +//CultureYouAreCodingWith in your .csproj file +//inside a . For example, if you are using US english +//in your source files, set the to en-US. Then uncomment +//the NeutralResourceLanguage attribute below. Update the "en-US" in +//the line below to match the UICulture setting in the project file. + +//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] + + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] + + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/TimedShutdown/Properties/Resources.Designer.cs b/TimedShutdown/Properties/Resources.Designer.cs new file mode 100644 index 0000000..2851e8c --- /dev/null +++ b/TimedShutdown/Properties/Resources.Designer.cs @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace TimedShutdown.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + public class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + public static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TimedShutdown.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + public static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized resource of type System.Drawing.Bitmap. + /// + public static System.Drawing.Bitmap ShutdownIcon { + get { + object obj = ResourceManager.GetObject("ShutdownIcon", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + } +} diff --git a/TimedShutdown/Properties/Resources.resx b/TimedShutdown/Properties/Resources.resx new file mode 100644 index 0000000..e776c94 --- /dev/null +++ b/TimedShutdown/Properties/Resources.resx @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + ..\Resources\ShutdownIcon.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + \ No newline at end of file diff --git a/TimedShutdown/Properties/Settings.Designer.cs b/TimedShutdown/Properties/Settings.Designer.cs new file mode 100644 index 0000000..c3c44b1 --- /dev/null +++ b/TimedShutdown/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace TimedShutdown.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/TimedShutdown/Properties/Settings.settings b/TimedShutdown/Properties/Settings.settings new file mode 100644 index 0000000..033d7a5 --- /dev/null +++ b/TimedShutdown/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/TimedShutdown/Resources/ShutdownIcon.png b/TimedShutdown/Resources/ShutdownIcon.png new file mode 100644 index 0000000..558ebde Binary files /dev/null and b/TimedShutdown/Resources/ShutdownIcon.png differ diff --git a/TimedShutdown/ShutdownIcon.ico b/TimedShutdown/ShutdownIcon.ico new file mode 100644 index 0000000..bd70651 Binary files /dev/null and b/TimedShutdown/ShutdownIcon.ico differ diff --git a/TimedShutdown/ShutdownManager.cs b/TimedShutdown/ShutdownManager.cs new file mode 100644 index 0000000..6915e61 --- /dev/null +++ b/TimedShutdown/ShutdownManager.cs @@ -0,0 +1,75 @@ +using System; +using System.Diagnostics; + +namespace TimedShutdown +{ + public class ShutdownManager + { + public enum Operation + { + Shutdown, + Reboot + } + + /// + /// Shuts down or reboots machine based on parameters. + /// + /// + /// + /// + public void ExecuteOperation(Operation shutdownoperation, int shutdownSeconds, bool forceShutdownState) + { + Func forceShutdown = ForceShutdown; + Func shutdownOperation = ShutdownOperation; + + ProcessStartInfo info = new ProcessStartInfo(); + Process proc = new Process(); + + info.FileName = "cmd.exe"; + info.Arguments = $"/c shutdown {ShutdownOperation(shutdownoperation)} -t" + + $" {shutdownSeconds}" + + $" {ForceShutdown(forceShutdownState)}"; + info.WindowStyle = ProcessWindowStyle.Hidden; + info.CreateNoWindow = true; + + proc.StartInfo = info; + proc.Start(); + } + + public void AbortOperation() + { + ProcessStartInfo info = new ProcessStartInfo(); + Process proc = new Process(); + + info.FileName = "cmd.exe"; + info.Arguments = $"/c shutdown -a"; + info.CreateNoWindow = true; + info.WindowStyle = ProcessWindowStyle.Hidden; + proc.StartInfo = info; + proc.Start(); + } + + #region Functions + + private string ShutdownOperation(Operation arg) + { + switch (arg) + { + case Operation.Shutdown: + return "-s"; + case Operation.Reboot: + return "-r"; + default: + return String.Empty; + } + } + + private string ForceShutdown(bool arg) + { + return "-f"; + } + + #endregion + + } +} diff --git a/TimedShutdown/TimedShutdown.csproj b/TimedShutdown/TimedShutdown.csproj new file mode 100644 index 0000000..cf4470d --- /dev/null +++ b/TimedShutdown/TimedShutdown.csproj @@ -0,0 +1,122 @@ + + + + + Debug + AnyCPU + {C1158C45-7666-4586-A6DA-CCC1EE95D457} + WinExe + TimedShutdown + TimedShutdown + v4.6.1 + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + ShutdownIcon.ico + + + + ..\packages\Caliburn.Micro.Core.3.2.0\lib\net45\Caliburn.Micro.dll + + + ..\packages\Caliburn.Micro.3.2.0\lib\net45\Caliburn.Micro.Platform.dll + + + ..\packages\Caliburn.Micro.3.2.0\lib\net45\Caliburn.Micro.Platform.Core.dll + + + + + + ..\packages\Caliburn.Micro.3.2.0\lib\net45\System.Windows.Interactivity.dll + + + + + + + + + 4.0 + + + + + + + + MSBuild:Compile + Designer + + + + + ShellRootView.xaml + + + App.xaml + Code + + + + Designer + MSBuild:Compile + + + + + Code + + + True + True + Resources.resx + + + True + Settings.settings + True + + + PublicResXFileCodeGenerator + Resources.Designer.cs + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + + + + + + + + + \ No newline at end of file diff --git a/TimedShutdown/ViewModels/ShellRootViewModel.cs b/TimedShutdown/ViewModels/ShellRootViewModel.cs new file mode 100644 index 0000000..4f52710 --- /dev/null +++ b/TimedShutdown/ViewModels/ShellRootViewModel.cs @@ -0,0 +1,134 @@ +using System; +using System.Windows.Media.Imaging; +using Caliburn.Micro; + +namespace TimedShutdown.ViewModels +{ + public class ShellRootViewModel : Screen + { + readonly ShutdownManager shutdownManager = new ShutdownManager(); + + #region Labels + + public string LabelWindowTitle { get; } = "Timed Shutdown"; + + public string LabelHoursLabel { get; } = "Hours:"; + + public string LabelMinutesLabel { get; } = "Minutes:"; + + public string LabelShutdownOptions { get; } = "Options:"; + + public string LabelShutdownOptionsForce { get; } = "Force Operation"; + + public string LabelShutdownTime { get; set; } = "Shutdown Time:"; + + public string LabelShutdown { get; } = "Shutdown"; + + public string LabelReboot { get; } = "Reboot"; + + public string LabelAbortShutdown { get; } = "Abort Shutdown"; + + #endregion + + #region Values + + private int _hours; + public int Hours + { + get { return _hours; } + set + { + if (value < 0) + { + value = 0; + } + else if (value > 23) + { + value = 23; + } + + _hours = value; + NotifyOfPropertyChange(() => Hours); + NotifyOfPropertyChange(() => ShutdownTime); + NotifyOfPropertyChange(() => ShutdownTimeSpan); + } + } + + private int _minutes; + public int Minutes + { + get { return _minutes; } + set + { + if (value < 0) + { + value = 0; + } + else if (value > 59) + { + value = 59; + } + + _minutes = value; + NotifyOfPropertyChange(() => Minutes); + NotifyOfPropertyChange(() => ShutdownTime); + NotifyOfPropertyChange(() => ShutdownTimeSpan); + } + } + + public int ShutdownTime => Minutes * 60 + Hours * 60 * 60; + + public TimeSpan ShutdownTimeSpan => TimeSpan.FromSeconds(ShutdownTime); + + public bool ShutdownOptionsForce { get; set; } + + #endregion + + #region Buttons + + public bool CanShutdown { get; set; } = true; + public void Shutdown() + { + shutdownManager.ExecuteOperation(ShutdownManager.Operation.Shutdown, ShutdownTime, false); + CanAbortShutdown = true; + CanReboot = false; + CanShutdown = false; + + NotifyOfPropertyChange(() => CanAbortShutdown); + NotifyOfPropertyChange(() => CanReboot); + NotifyOfPropertyChange(() => CanShutdown); + } + + public bool CanReboot { get; set; } = true; + public void Reboot() + { + shutdownManager.ExecuteOperation(ShutdownManager.Operation.Reboot, ShutdownTime, false); + CanAbortShutdown = true; + CanReboot = false; + CanShutdown = false; + + NotifyOfPropertyChange(() => CanAbortShutdown); + NotifyOfPropertyChange(() => CanReboot); + NotifyOfPropertyChange(() => CanShutdown); + } + + public bool CanAbortShutdown { get; set; } = true; + public void AbortShutdown() + { + // Reset values to defaults. + Minutes = 0; + Hours = 0; + + shutdownManager.AbortOperation(); + CanAbortShutdown = false; + CanReboot = true; + CanShutdown = true; + + NotifyOfPropertyChange(() => CanAbortShutdown); + NotifyOfPropertyChange(() => CanReboot); + NotifyOfPropertyChange(() => CanShutdown); + } + + #endregion + } +} diff --git a/TimedShutdown/Views/ShellRootView.xaml b/TimedShutdown/Views/ShellRootView.xaml new file mode 100644 index 0000000..99a0d5c --- /dev/null +++ b/TimedShutdown/Views/ShellRootView.xaml @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +