-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathucCountdown.cs
303 lines (258 loc) · 10.3 KB
/
ucCountdown.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
using System;
using System.Windows.Forms;
using System.IO;
using Business.Entities;
using System.Diagnostics;
using System.Net.Mail;
using System.Net;
using System.Threading.Tasks;
using System.Threading;
namespace MultipleCountdown
{
public partial class ucCountdown : UserControl
{
int UpdateRemainingTimeInterval;
DateTime RemainingTimeLastUpdated;
bool sendNotificationEmailWhenComplete;
public CountdownStructure CountdownEssentials { get; private set; }
public void SetCountdownEssentials(CountdownStructure cd)
{
CountdownEssentials = cd;
InitializeCountdown();
}
public void ReadSettingsValues()
{
UpdateRemainingTimeInterval = Properties.Settings.Default.UpdateRemainingTimeIntervalInSeconds;
sendNotificationEmailWhenComplete = Properties.Settings.Default.SendNotificationEmailWhenComplete;
}
public ucCountdown()
{
InitializeComponent();
btnStartStop.Text = "Start";
RefreshEndTimeLabelText(true);
}
public ucCountdown(CountdownStructure countdownEssentials)
: this()
{
SetCountdownEssentials(countdownEssentials);
}
void InitializeCountdown()
{
lblTitle.Text = CountdownEssentials.Title;
SetTextBoxValuesFromSeconds();
if (CountdownEssentials.IsInProgress)
{
startTimer();
}
}
private void SetTextBoxValuesFromSeconds()
{
double Days = CountdownEssentials.remainingTime.Days;
double Hours = CountdownEssentials.remainingTime.Hours;
double Minutes = CountdownEssentials.remainingTime.Minutes;
double Seconds = CountdownEssentials.remainingTime.Seconds;
if (Days == 0) tbDay.Text = string.Empty;
else tbDay.Text = Days.ToString();
if(Hours == 0 && Days == 0) tbHour.Text = string.Empty;
else tbHour.Text = Hours.ToString();
if (Minutes == 0 && Hours == 0 && Days == 0) tbMinute.Text = string.Empty;
else tbMinute.Text = Minutes.ToString();
if (Seconds == 0 && Minutes == 0 && Hours == 0 && Days == 0) tbSecond.Text = string.Empty;
else tbSecond.Text = Seconds.ToString();
}
private double GetSecondsFromTextBoxes()
{
int day, hour, minute, second;
if (int.TryParse(tbDay.Text.Trim(), out day) == false) day = 0;
if (int.TryParse(tbHour.Text.Trim(), out hour) == false) hour = 0;
if (int.TryParse(tbMinute.Text.Trim(), out minute) == false) minute = 0;
if (int.TryParse(tbSecond.Text.Trim(), out second) == false) second = 0;
return GetTotalSeconds(day, hour, minute, second);
}
private double GetTotalSeconds(int day, int hour, int minute, int second)
{
return (day * 24 * 60 * 60) + (hour * 60 * 60) + (minute * 60) + (second);
}
private void btnStartStop_Click(object sender, EventArgs e)
{
switchTimer();
startSyncInParent();
}
private void toggleTextboxEditable(bool editable)
{
tbDay.Enabled = editable;
tbHour.Enabled = editable;
tbMinute.Enabled = editable;
tbSecond.Enabled = editable;
}
void startTimer()
{
CountdownEssentials.IsInProgress = true;
CountdownEssentials.SetTotalSeconds(GetSecondsFromTextBoxes());
timer1.Enabled = true;
RemainingTimeLastUpdated = DateTime.Now;
btnStartStop.Text = "Stop";
RefreshEndTimeLabelText();
toggleTextboxEditable(false);
}
void stopTimer()
{
CountdownEssentials.IsInProgress = false;
timer1.Enabled = false;
btnStartStop.Text = "Start";
RefreshEndTimeLabelText(true);
toggleTextboxEditable(true);
}
void switchTimer()
{
if (timer1.Enabled)
{
stopTimer();
}
else
{
startTimer();
}
}
private void btnClose_Click(object sender, EventArgs e)
{
CountdownEssentials.IsDeleted = true;
((Countdown)Parent.Parent).UserControlClosed(this);
startSyncInParent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (CountdownEssentials.remainingTime.TickingSeconds > 0)
{
CountdownEssentials.remainingTime.TickingSeconds--;
if((DateTime.Now - RemainingTimeLastUpdated).TotalSeconds >= UpdateRemainingTimeInterval)
{
CountdownEssentials.UpdateRemainingTime();
RemainingTimeLastUpdated = DateTime.Now;
}
SetTextBoxValuesFromSeconds();
}
else
{
switchTimer();
if (sendNotificationEmailWhenComplete)
{
SendEmail();
}
try
{
string filePath = Path.Combine(Application.ExecutablePath.Remove(Application.ExecutablePath.LastIndexOf("\\")), "notification_sound.mp3");
NAudio.Wave.IWavePlayer waveOutDevice = new NAudio.Wave.WaveOut();
NAudio.Wave.AudioFileReader audioFileReader = new NAudio.Wave.AudioFileReader(filePath);
waveOutDevice.Init(audioFileReader);
waveOutDevice.Play();
}catch(Exception ex)
{
//TODO: Log it
}
AlertForm alert = new AlertForm(CountdownEssentials.Title);
alert.Top = 0;
alert.Left = 0;
alert.TopMost = true;
alert.ShowDialog();
}
}
async void SendEmail()
{
string smtpAddress = Properties.Settings.Default.EmailSmtpServer;
int portNumber = Properties.Settings.Default.EmailSmtpPortNumber;
bool enableSSL = Properties.Settings.Default.EmailSmtpRequiresSSL;
string emailFrom = Properties.Settings.Default.EmailFromAddress;
string password = Properties.Settings.Default.EmailFromPassword;
string emailTo = Properties.Settings.Default.EmailToAddress;
string subject = string.Format("Countdown Finished - {0}", CountdownEssentials.Title);
string body = string.Format("Your countdown with title '{0}' has finished. <br/>End time: {1}", CountdownEssentials.Title, CountdownEssentials.EndTimeUtc.ToLocalTime().ToString("dd MMMM yyyy - HH:mm:ss"));
try
{
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
// Can set to false, if you are sending pure text.
mail.IsBodyHtml = true;
//mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
//mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
bool result = await sendMailAsync(smtp, mail);
if(result == true)
{
//TODO: Probably we'll do nothing here
}else
{
//TODO: There was an error but we'll probably just do nothing here too
}
}
}
}
catch(Exception ex)
{
//TODO: Logging logging
}
}
async Task<bool> sendMailAsync(SmtpClient smtp, MailMessage mail)
{
try
{
smtp.Send(mail);
return true;
}
catch (Exception ex)
{
//TODO: Logging logging
return false;
}
}
void startSyncInParent()
{
CountdownEssentials.UpdatedNow();
//trigger synchronization
if (Parent != null && Parent.Parent != null)
{
((Countdown)Parent.Parent).StartAutoSync();
}
}
private void btnChangeRemainingTime_Click(object sender, EventArgs e)
{
AlterCountdownTime alterForm = new AlterCountdownTime(this);
alterForm.ShowDialog();
}
void RefreshEndTimeLabelText(bool clear = false)
{
string labelText = clear == false ?
string.Format("End Time: {0}", CountdownEssentials.EndTimeUtc.ToLocalTime().ToString("dd MMMM yyyy HH:mm:ss")) :
string.Empty;
lblEndTime.Text = labelText;
}
public void ChangeTime(int day, int hour, int minute, int second)
{
double totalSeconds = GetTotalSeconds(day, hour, minute, second);
CountdownEssentials.SetTotalSeconds(totalSeconds);
CountdownEssentials.UpdatedNow();
RefreshEndTimeLabelText();
}
public void AddTime(int day, int hour, int minute, int second)
{
double totalSeconds = GetTotalSeconds(day, hour, minute, second);
CountdownEssentials.SetTotalSeconds(CountdownEssentials.remainingTime.TickingSeconds + totalSeconds);
CountdownEssentials.UpdatedNow();
RefreshEndTimeLabelText();
}
public void ReduceTime(int day, int hour, int minute, int second)
{
double totalSeconds = GetTotalSeconds(day, hour, minute, second);
CountdownEssentials.SetTotalSeconds(CountdownEssentials.remainingTime.TickingSeconds - totalSeconds);
CountdownEssentials.UpdatedNow();
RefreshEndTimeLabelText();
}
}
}