-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlPanel.cs
More file actions
337 lines (296 loc) · 10.7 KB
/
ControlPanel.cs
File metadata and controls
337 lines (296 loc) · 10.7 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using Microsoft.Win32;
using System.Runtime.InteropServices;
using QRCoder;
namespace AutoLogout
{
public partial class ControlPanel : Form
{
// UAC shield
private const int BCM_SETSHIELD = 0x160C;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
private readonly CountdownTimer parent;
private readonly Label usedTimeIndicator;
private readonly NumericUpDown dailylimitPicker;
private readonly NumericUpDown todaylimitPicker;
private readonly DateTimePicker waketimePicker;
private readonly DateTimePicker sleeptimePicker;
private readonly CheckBox autostartCheckBox;
private readonly bool autostartEnabled = false;
private readonly Button DeauthButton;
private readonly Button SaveButton;
public ControlPanel(CountdownTimer parent)
{
this.parent = parent;
Text = "AutoLogout Settings";
Icon = new Icon("Resources/icon-light.ico");
FormBorderStyle = FormBorderStyle.FixedDialog;
ShowInTaskbar = true;
StartPosition = FormStartPosition.CenterScreen;
MinimizeBox = false;
MaximizeBox = false;
BackColor = Color.White;
Width = 360;
Height = 400;
AutoScaleMode = AutoScaleMode.Dpi;
AutoScaleDimensions = new(96F, 96F);
using (RegistryKey? key = Registry.LocalMachine.OpenSubKey(
@"Software\Microsoft\Windows\CurrentVersion\Run"))
{
string regValue = (string)(key?.GetValue("AutoLogout") ?? "");
if (regValue.Contains(Common.exePath)) autostartEnabled = true;
}
TableLayoutPanel mainTable = new()
{
Dock = DockStyle.Top,
AutoSize = true,
ColumnCount = 1,
RowCount = 2,
};
TableLayoutPanel table = new()
{
AutoSize = true,
ColumnCount = 2,
RowCount = 2,
Padding = new Padding(10),
};
FlowLayoutPanel optionsPanel = new()
{
AutoSize = true,
};
FlowLayoutPanel buttonPanel = new()
{
Dock = DockStyle.Bottom,
AutoSize = true,
BackColor = SystemColors.Control,
Padding = new Padding(8),
};
Label usedTimeLabel = new()
{
Dock = DockStyle.Fill,
Text = "Time used today:",
AutoSize = true,
Padding = new Padding { Bottom = 12 },
};
usedTimeIndicator = new()
{
Dock = DockStyle.Fill,
Text = "Loading...",
AutoSize = true,
};
Label dailylimitLabel = new()
{
Dock = DockStyle.Fill,
Text = "Daily time limit (minutes):",
AutoSize = true,
};
dailylimitPicker = new()
{
Minimum = -1,
Maximum = 1440, // 24 hours in minutes
};
Label todaylimitLabel = new()
{
Dock = DockStyle.Fill,
Text = "Today's time limit (minutes):",
AutoSize = true,
};
todaylimitPicker = new()
{
Minimum = -1,
Maximum = 1440, // 24 hours in minutes
};
Label waketimeLabel = new()
{
Text = "Wake time:",
AutoSize = true,
};
waketimePicker = new()
{
Format = DateTimePickerFormat.Time,
ShowUpDown = true,
Width = 150,
};
Label sleeptimeLabel = new()
{
Text = "Sleep time:",
AutoSize = true,
};
sleeptimePicker = new()
{
Format = DateTimePickerFormat.Time,
ShowUpDown = true,
Width = 150,
};
Label autostartLabel = new()
{
Text = "Start timer on log in:",
AutoSize = true,
};
autostartCheckBox = new()
{
Checked = autostartEnabled,
};
autostartCheckBox.CheckedChanged += autostartCheckBox_Checked;
table.Controls.Add(usedTimeLabel);
table.Controls.Add(usedTimeIndicator);
table.Controls.Add(dailylimitLabel);
table.Controls.Add(dailylimitPicker);
table.Controls.Add(todaylimitLabel);
table.Controls.Add(todaylimitPicker);
table.Controls.Add(waketimeLabel);
table.Controls.Add(waketimePicker);
table.Controls.Add(sleeptimeLabel);
table.Controls.Add(sleeptimePicker);
table.Controls.Add(autostartLabel);
table.Controls.Add(autostartCheckBox);
Button AuthButton = new() { Text = "Connect to your phone", AutoSize = true };
AuthButton.Click += AuthButton_Click;
DeauthButton = new() { Text = "Sign out all devices", AutoSize = true };
DeauthButton.Click += DeauthButton_Click;
Button ChangePasswordButton = new() { Text = "Change password", AutoSize = true };
ChangePasswordButton.Click += ChangePasswordButton_Click;
Button RemoveAccountButton = new() { Text = "Remove AutoLogout from this account", AutoSize = true };
RemoveAccountButton.Click += RemoveAccountButton_Click;
SaveButton = new() { Text = "Save", DialogResult = DialogResult.OK, AutoSize = true, FlatStyle = FlatStyle.System };
Button CancelButton = new() { Text = "Cancel", DialogResult = DialogResult.Cancel, AutoSize = true };
SaveButton.Click += SaveButton_Click;
optionsPanel.Controls.Add(AuthButton);
optionsPanel.Controls.Add(DeauthButton);
optionsPanel.Controls.Add(ChangePasswordButton);
optionsPanel.Controls.Add(RemoveAccountButton);
buttonPanel.Controls.Add(SaveButton);
buttonPanel.Controls.Add(CancelButton);
mainTable.Controls.Add(table);
mainTable.Controls.Add(optionsPanel);
Controls.Add(mainTable);
Controls.Add(buttonPanel);
AcceptButton = SaveButton;
base.CancelButton = CancelButton;
// Initialize controls with current state
OnStateChanged();
// Subscribe to future state changes
parent.state.Changed += OnStateChanged;
}
private void OnStateChanged()
{
// Update all controls that are affected by the state
// If the event came from another thread, send it to the correct thread
if (InvokeRequired)
{
Invoke(OnStateChanged);
return;
}
TimeSpan timeSpan = TimeSpan.FromSeconds(parent.state.usedTime);
string timeString = string.Format("{0:D2}:{1:D2}:{2:D2}", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
usedTimeIndicator.Text = timeString;
dailylimitPicker.Value = parent.state.dailyTimeLimit >= 0 ? parent.state.dailyTimeLimit / 60 : -1;
todaylimitPicker.Value = parent.state.todayTimeLimit >= 0 ? parent.state.todayTimeLimit / 60 : -1;
TimeOnly waketime = parent.state.waketime;
DateTime wakeDateTime = DateTime.Today.Add(waketime.ToTimeSpan());
waketimePicker.Value = wakeDateTime;
TimeOnly bedtime = parent.state.bedtime;
DateTime sleepDateTime = DateTime.Today.Add(bedtime.ToTimeSpan());
sleeptimePicker.Value = sleepDateTime;
DeauthButton.Enabled = parent.state.OnlineMode;
}
private void autostartCheckBox_Checked(object? sender, EventArgs e)
{
if(autostartCheckBox.Checked != autostartEnabled)
// To show the shield
SendMessage(SaveButton.Handle, BCM_SETSHIELD, IntPtr.Zero, new IntPtr(1));
else
// To hide the shield
SendMessage(SaveButton.Handle, BCM_SETSHIELD, IntPtr.Zero, new IntPtr(0));
}
private void AuthButton_Click(object? sender, EventArgs e)
{
if (!parent.state.OnlineMode)
{
parent.state.OnlineMode = true;
Task.Run(parent.state.Sync);
parent.state.TriggerStateChanged();
}
// Generate a QR code for the user to scan with their phone
string qrContent = $"https://autologout.yiays.com/app/addAccount?uuid={parent.state.uuid}";
using var qrGenerator = new QRCodeGenerator();
using var qrData = qrGenerator.CreateQrCode(qrContent, QRCodeGenerator.ECCLevel.Q);
using var qrCode = new QRCode(qrData);
using var qrBitmap = qrCode.GetGraphic(20);
// Show QR in a simple dialog
Form qrForm = new Form
{
Text = "Scan this QR code with your phone",
ClientSize = new Size(600, 600),
FormBorderStyle = FormBorderStyle.SizableToolWindow,
StartPosition = FormStartPosition.CenterParent
};
PictureBox pb = new PictureBox
{
Dock = DockStyle.Fill,
Image = new Bitmap(qrBitmap),
SizeMode = PictureBoxSizeMode.Zoom
};
qrForm.Controls.Add(pb);
qrForm.ShowDialog(this);
}
private void DeauthButton_Click(object? sender, EventArgs e)
{
if (MessageBox.Show(
"Are you sure you want delete all your online data and sign out all devices?",
"AutoLogout", MessageBoxButtons.YesNo, MessageBoxIcon.Warning
) == DialogResult.Yes)
{
Task.Run(parent.state.Deauth);
}
}
private void ChangePasswordButton_Click(object? sender, EventArgs e)
{
if (parent.state.NewPassword())
MessageBox.Show(
"Password changed successfully.", "AutoLogout", MessageBoxButtons.OK, MessageBoxIcon.Information
);
}
private async void RemoveAccountButton_Click(object? sender, EventArgs e)
{
if (MessageBox.Show(
"This disables AutoLogout for this account, any other accounts are unaffected. " +
"You can reactivate AutoLogout anytime by opening it again." +
"\nDo you want to continue?",
"AutoLogout", MessageBoxButtons.YesNo, MessageBoxIcon.Warning
) == DialogResult.Yes)
{
if (parent.state.OnlineMode)
await parent.state.Deauth();
State.ClearRegistry();
parent.state.ExitIntent = true;
Application.Exit();
}
}
private void SaveButton_Click(object? sender, EventArgs e)
{
parent.state.dailyTimeLimit = (int)(dailylimitPicker.Value >= 0 ? dailylimitPicker.Value * 60 : -1);
parent.state.todayTimeLimit = (int)(todaylimitPicker.Value >= 0 ? todaylimitPicker.Value * 60 : -1);
// Give parents some time to correct their mistake if they set the time limit too low
if (parent.state.remainingTime == 0)
parent.state.todayTimeLimit = parent.state.usedTime + 30;
parent.state.waketime = new(waketimePicker.Value.Hour, waketimePicker.Value.Minute);
parent.state.bedtime = new(sleeptimePicker.Value.Hour, sleeptimePicker.Value.Minute);
parent.state.SaveToRegistry();
parent.state.TriggerStateChanged();
if (autostartCheckBox.Checked != autostartEnabled)
{
if (autostartCheckBox.Checked)
Common.Relaunch("--register");
else
Common.Relaunch("--unregister");
}
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
parent.state.Changed -= OnStateChanged;
base.OnFormClosed(e);
parent.controlPanel = null;
}
}
}