1- using System ;
1+ using Microsoft . Win32 . TaskScheduler ;
2+ using System ;
23using System . Windows . Forms ;
34
45namespace WindowsShutdownTimer
56{
67 public partial class Options : Form
78 {
9+ #region Main
10+
811 /// <summary>
912 /// The parent form object.
1013 /// </summary>
@@ -26,45 +29,97 @@ public Options(TimerForm parent)
2629
2730 last_shutdown_label_desc . Text = "Last Attempted Shutdown: " ;
2831
29- if ( timerWindow . TimerExists ( TimerForm . DEFAULT_TASK_NAME ) )
32+ if ( timerWindow . TimerExists ( TimerForm . DEFAULT_TASK_NAME ) )
3033 {
31- if ( timerWindow . TimerRunning ( TimerForm . DEFAULT_TASK_NAME , DateTime . Now ) )
34+ if ( timerWindow . TimerRunning ( TimerForm . DEFAULT_TASK_NAME , DateTime . Now ) )
3235 last_shutdown_label . Text = "Pending " + "(" + Convert . ToString ( Properties . Settings . Default . ShutdownTimer ) + ")" ;
3336 else
3437 {
35- // If the timer was never run, display the time accordingly.
36- if ( timerWindow . GetLastRunTime ( TimerForm . DEFAULT_TASK_NAME ) == timerWindow . SetDefaultDateTime ( new DateTime ( ) ) )
37- last_shutdown_label . Text = "N/A" ;
38- else
38+ try
3939 {
40- // Windows 7 doesn't have the last run time so I need to do something else .
41- if ( timerWindow . GetLastRunTime ( TimerForm . DEFAULT_TASK_NAME ) == default ( DateTime ) )
42- last_shutdown_label . Text = Convert . ToString ( Properties . Settings . Default . ShutdownTimer ) ;
40+ // If the timer was never run, display the time accordingly .
41+ if ( GetLastRunTime ( TimerForm . DEFAULT_TASK_NAME ) == timerWindow . SetDefaultDateTime ( new DateTime ( ) ) )
42+ last_shutdown_label . Text = "N/A" ;
4343 else
44- last_shutdown_label . Text = Convert . ToString ( timerWindow . GetLastRunTime ( TimerForm . DEFAULT_TASK_NAME ) ) ;
44+ {
45+ // Windows 7 doesn't have the last run time so I need to do something else.
46+ if ( GetLastRunTime ( TimerForm . DEFAULT_TASK_NAME ) == default ( DateTime ) )
47+ last_shutdown_label . Text = Convert . ToString ( Properties . Settings . Default . ShutdownTimer ) ;
48+ else
49+ last_shutdown_label . Text = Convert . ToString ( GetLastRunTime ( TimerForm . DEFAULT_TASK_NAME ) ) ;
50+ }
51+ }
52+ catch ( NoTimerExists )
53+ {
54+ last_shutdown_label . Text = "N/A" ;
4555 }
4656 }
4757 }
4858 else
4959 last_shutdown_label . Text = "N/A" ;
60+
61+ timerWindow . BringFormForward ( ) ;
62+ }
63+
64+ #endregion
65+
66+ #region Helper Functions
67+
68+ /// <summary>
69+ /// Gets the last time a scheduled task was run (successfully or not).
70+ /// </summary>
71+ /// <param name="taskName">The name of the task that will be searched.</param>
72+ /// <returns>Returns the DateTime of when the scheduled even last fired.</returns>
73+ public DateTime GetLastRunTime ( string taskName )
74+ {
75+ if ( Properties . Settings . Default . ShowShutdownNotification )
76+ return Properties . Settings . Default . ShutdownTimer ;
77+
78+ else
79+ {
80+ using ( TaskService ts = new TaskService ( ) )
81+ {
82+ // If the task exists, return the last run time.
83+ if ( timerWindow . TimerExists ( taskName ) )
84+ {
85+ Task task = ts . GetTask ( taskName ) ;
86+ return task . LastRunTime ;
87+ }
88+
89+ else
90+ throw new NoTimerExists ( "The timer doesn't exist in the task scheduler." ) ;
91+ }
92+ }
5093 }
5194
95+ #endregion
96+
97+ #region Event Handlers
98+
5299 /// <summary>
53100 /// When the form loads, set the check boxes according to the user's prior settings (if any).
54101 /// </summary>
55102 /// <param name="sender"></param>
56103 /// <param name="e"></param>
57104 private void Options_Load ( object sender , EventArgs e )
58105 {
106+ // First check box
59107 if ( Properties . Settings . Default . MinimizeToSysTray )
60108 minimize_to_sys_tray . Checked = true ;
61109 else
62110 minimize_to_sys_tray . Checked = false ;
63111
112+ // Second check box
64113 if ( Properties . Settings . Default . LClickOpenSysTray )
65114 left_click_open_sys_tray . Checked = true ;
66115 else
67116 left_click_open_sys_tray . Checked = false ;
117+
118+ // Third check box
119+ if ( Properties . Settings . Default . ShowShutdownNotification )
120+ show_shutdown_notification . Checked = true ;
121+ else
122+ show_shutdown_notification . Checked = false ;
68123 }
69124
70125 /// <summary>
@@ -73,26 +128,44 @@ private void Options_Load(object sender, EventArgs e)
73128 /// <param name="sender"></param>
74129 /// <param name="e"></param>
75130 public void save_options_button_Click ( object sender , EventArgs e )
76- {
131+ {
77132 // Minimize program to system tray rather than to the taskbar.
78133 if ( minimize_to_sys_tray . Checked )
79134 Properties . Settings . Default . MinimizeToSysTray = true ;
80135 else
81136 Properties . Settings . Default . MinimizeToSysTray = false ;
82-
137+
83138 // Single left click to re-show program when clicking icon in system tray.
84139 if ( left_click_open_sys_tray . Checked )
85140 Properties . Settings . Default . LClickOpenSysTray = true ;
86141 else
87142 Properties . Settings . Default . LClickOpenSysTray = false ;
88143
144+ // Display the shutdown notification from windows alerting the user of an impending shutdown.
145+ if ( show_shutdown_notification . Checked && ! Properties . Settings . Default . ShowShutdownNotification )
146+ {
147+ DialogResult confirm = MessageBox . Show ( "Are you sure you want to show the shutdown notifications? This may result in the " +
148+ "timer being inaccurate and you not being shown the correct time remaining. In addition, it also has to create/remove a " +
149+ "shutdown timer each time you load the program in order to check if one exists. So that might result in a message indicating " +
150+ "that a shutdown was stopped but it was only a check. I honestly don't recommend enabling this option. Proceed with caution!"
151+ , "WARNING - Are you sure?" , MessageBoxButtons . YesNoCancel , MessageBoxIcon . Warning ) ;
152+
153+ if ( confirm == DialogResult . Yes )
154+ Properties . Settings . Default . ShowShutdownNotification = true ;
155+ else
156+ return ;
157+ }
158+ else
159+ Properties . Settings . Default . ShowShutdownNotification = false ;
160+
89161 Properties . Settings . Default . Save ( ) ;
90- //timerWindow.ApplyUserSettings();
91162
92163 // Close the options form and redisplay the parent.
93164 ActiveForm . Close ( ) ;
94165 timerWindow . Enabled = true ;
95166 timerWindow . Visible = true ;
167+
168+ timerWindow . BringFormForward ( ) ;
96169 }
97170
98171 /// <summary>
@@ -103,6 +176,7 @@ public void save_options_button_Click(object sender, EventArgs e)
103176 private void Options_FormClosing ( object sender , FormClosingEventArgs e )
104177 {
105178 timerWindow . Enabled = true ;
179+ timerWindow . BringFormForward ( ) ;
106180 }
107181
108182 /// <summary>
@@ -123,9 +197,9 @@ public void check_update_button_Click(object sender, EventArgs e)
123197 Array curV = currentVersion . Split ( '.' ) ;
124198
125199 // Note: The user's version number should never be higher than the release version. So I don't even consider that case.
126- for ( int i = 0 ; i < 4 ; i ++ )
200+ for ( int i = 0 ; i < 4 ; i ++ )
127201 {
128- if ( Convert . ToInt32 ( webV . GetValue ( i ) ) > Convert . ToInt32 ( curV . GetValue ( i ) ) )
202+ if ( Convert . ToInt32 ( webV . GetValue ( i ) ) > Convert . ToInt32 ( curV . GetValue ( i ) ) )
129203 {
130204 DialogResult result = MessageBox . Show ( "The current version is: " + currentVersion + " and the newest version is " + webVersion + ". Would you " +
131205 "like to download the newest version?" , "New Version Found!" , MessageBoxButtons . YesNoCancel , MessageBoxIcon . Question ) ;
@@ -168,5 +242,7 @@ public void check_update_button_Click(object sender, EventArgs e)
168242
169243 MessageBox . Show ( "The current version is: " + currentVersion + " and it is up to date!" , "No New Update!" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
170244 }
245+
246+ #endregion
171247 }
172248}
0 commit comments