Zzz – Blame information for rev 5

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.ComponentModel;
3 using System.Drawing;
4 using System.Runtime.InteropServices;
5 using System.Threading;
6 using System.Windows.Forms;
7 using InTheHand.Net.Bluetooth;
8 using InTheHand.Net.Sockets;
9 using Serilog;
10 using Zzz.Clients;
11 using Zzz.Properties;
12 using Zzz.Utilities;
13  
14 namespace Zzz
15 {
16 public partial class SettingsForm : Form
17 {
18 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
19  
20 private BindingSource _bluetoothWatchListBindingSource;
21  
22 private readonly MqttClient _mqttClient;
23  
24 private readonly Configuration.Configuration _configuration;
25  
26 private BindingSource _windowsWatchListBindingSource;
27  
28 private BluetoothClient _bluetoothClient;
29  
30 private BluetoothComponent _bluetoothComponent;
31  
32 private ScheduledContinuation MqttRestartContinuation;
33  
34 private CancellationTokenSource CancellationTokenSource;
35  
36 private CancellationToken CancellationToken;
37  
38 #endregion
39  
40 #region Constructors, Destructors and Finalizers
41  
42 public SettingsForm()
43 {
44 InitializeComponent();
45  
46 CancellationTokenSource = new CancellationTokenSource();
47 CancellationToken = CancellationTokenSource.Token;
48  
49 MqttRestartContinuation = new ScheduledContinuation();
50  
51 try
52 {
53 _bluetoothClient = new BluetoothClient();
54 _bluetoothComponent = new BluetoothComponent(_bluetoothClient);
55  
56 _bluetoothComponent.DiscoverDevicesProgress += LocalComponent_DiscoverDevicesProgress;
57 _bluetoothComponent.DiscoverDevicesComplete += LocalComponent_DiscoverDevicesComplete;
58 }
59 catch(Exception ex)
60 {
61 Log.Warning(ex, $"Bluetooth stack is not supported");
62 }
63 }
64  
65 public SettingsForm(MqttClient mqttClient, Configuration.Configuration configuration) : this()
66 {
67 _mqttClient = mqttClient;
68 _configuration = configuration;
69 }
70  
71 /// <summary>
72 /// Clean up any resources being used.
73 /// </summary>
74 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
75 protected override void Dispose(bool disposing)
76 {
77 if (disposing && components != null)
78 {
79 components.Dispose();
80 }
81  
82 if (_bluetoothComponent != null)
83 {
84 _bluetoothComponent.DiscoverDevicesProgress -= LocalComponent_DiscoverDevicesProgress;
85 _bluetoothComponent.DiscoverDevicesComplete -= LocalComponent_DiscoverDevicesComplete;
86 }
87  
88 _mqttClient.MqttConnectionFailed -= MqttClient_MqttConnectionFailed;
89 _mqttClient.MqttConnectionSucceeded -= MqttClient_MqttConnectionSucceeded;
90 _mqttClient.MqttDisconnected -= MqttClient_MqttDisconnected;
91 _mqttClient.MqttSubscribeFailed -= MqttClient_MqttSubscribeFailed;
92 _mqttClient.MqttSubscribeSucceeded -= MqttClient_MqttSubscribeSucceeded;
93  
94 _bluetoothComponent?.Dispose();
95 _bluetoothComponent = null;
96  
97 _bluetoothClient?.Dispose();
98 _bluetoothClient = null;
99  
100 base.Dispose(disposing);
101 }
102  
103 #endregion
104  
105 #region Event Handlers
106  
107 private void MqttSettings_Changed(object sender, EventArgs e)
108 {
109 MqttRestartContinuation.Schedule(TimeSpan.FromSeconds(1), async () =>
110 {
111 switch (_configuration.MqttEnable)
112 {
113 case false:
114 await _mqttClient.Stop();
115 break;
116 default:
117 await _mqttClient.Restart();
118 break;
119 }
120 }, CancellationToken);
121  
122 }
123  
124 private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
125 {
126 CancellationTokenSource.Cancel();
127 }
128  
129 private void SettingsForm_Load(object sender, EventArgs e)
130 {
2 office 131 Utilities.WindowState.FormTracker.Track(this);
132  
1 office 133 pictureBox1.BackColor = _mqttClient.Connected ? Color.Green : Color.Red;
134 pictureBox2.BackColor = _mqttClient.Subscribed ? Color.Green : Color.Red;
135  
136 _mqttClient.MqttConnectionFailed += MqttClient_MqttConnectionFailed;
137 _mqttClient.MqttConnectionSucceeded += MqttClient_MqttConnectionSucceeded;
138 _mqttClient.MqttDisconnected += MqttClient_MqttDisconnected;
139 _mqttClient.MqttSubscribeFailed += MqttClient_MqttSubscribeFailed;
140 _mqttClient.MqttSubscribeSucceeded += MqttClient_MqttSubscribeSucceeded;
141  
142 // Bind the bluetooth watch list binding source.
143 _bluetoothWatchListBindingSource = new BindingSource(_configuration.BluetoothWatchList, ""); ;
144 listBox1.DataSource = _bluetoothWatchListBindingSource;
145  
146 // Bind the windows watch list binding source.
147 _windowsWatchListBindingSource = new BindingSource(_configuration.WindowsWatchList, "");
148 listBox2.DataSource = _windowsWatchListBindingSource;
149  
150 // Bind the rest of the settings.
151 checkBox1.DataBindings.Add(nameof(checkBox1.Checked), _configuration, nameof(_configuration.LaunchOnBoot), true, DataSourceUpdateMode.OnPropertyChanged);
152 trackBar2.DataBindings.Add(nameof(trackBar2.Value), _configuration, nameof(_configuration.Timeout), true, DataSourceUpdateMode.OnPropertyChanged);
153 numericUpDown3.DataBindings.Add(nameof(numericUpDown3.Value), _configuration, nameof(_configuration.Timeout), true, DataSourceUpdateMode.OnPropertyChanged);
154 numericUpDown5.DataBindings.Add(nameof(numericUpDown5.Value), _configuration, nameof(_configuration.HibernateTimeout), true, DataSourceUpdateMode.OnPropertyChanged);
155 comboBox3.DataBindings.Add(nameof(comboBox3.Text), _configuration, nameof(_configuration.Action), true, DataSourceUpdateMode.OnPropertyChanged);
156 comboBox1.DataBindings.Add(nameof(comboBox1.Text), _configuration, nameof(_configuration.ActionClick), true, DataSourceUpdateMode.OnPropertyChanged);
157 comboBox2.DataBindings.Add(nameof(comboBox2.Text), _configuration, nameof(_configuration.ActionDoubleClick), true, DataSourceUpdateMode.OnPropertyChanged);
158 trackBar4.DataBindings.Add(nameof(trackBar4.Value), _configuration, nameof(_configuration.ClickActionDelay), true, DataSourceUpdateMode.OnPropertyChanged);
159 checkBox2.DataBindings.Add(nameof(checkBox2.Checked), _configuration, nameof(_configuration.MonitorMouse), true, DataSourceUpdateMode.OnPropertyChanged);
160 checkBox3.DataBindings.Add(nameof(checkBox3.Checked), _configuration, nameof(_configuration.MonitorKeyboard), true, DataSourceUpdateMode.OnPropertyChanged);
161 checkBox5.DataBindings.Add(nameof(checkBox5.Checked), _configuration, nameof(_configuration.MonitorBluetooth), true, DataSourceUpdateMode.OnPropertyChanged);
162 checkBox6.DataBindings.Add(nameof(checkBox6.Checked), _configuration, nameof(_configuration.MonitorWindows), true, DataSourceUpdateMode.OnPropertyChanged);
163 trackBar1.DataBindings.Add(nameof(trackBar1.Value), _configuration, nameof(_configuration.MouseMoveTolerance), true, DataSourceUpdateMode.OnPropertyChanged);
164 numericUpDown1.DataBindings.Add(nameof(numericUpDown1.Value), _configuration, nameof(_configuration.MouseMoveTolerance), true, DataSourceUpdateMode.OnPropertyChanged);
165 trackBar3.DataBindings.Add(nameof(trackBar3.Value), _configuration, nameof(_configuration.BluetoothScanInterval), true, DataSourceUpdateMode.OnPropertyChanged);
166 numericUpDown4.DataBindings.Add(nameof(numericUpDown4.Value), _configuration, nameof(_configuration.BluetoothScanInterval), true, DataSourceUpdateMode.OnPropertyChanged);
167 checkBox4.DataBindings.Add(nameof(checkBox4.Checked), _configuration, nameof(_configuration.MqttEnable), true, DataSourceUpdateMode.OnPropertyChanged);
168 textBox1.DataBindings.Add(nameof(textBox1.Text), _configuration, nameof(_configuration.MqttServer), true, DataSourceUpdateMode.OnPropertyChanged);
169 numericUpDown2.DataBindings.Add(nameof(numericUpDown2.Value), _configuration, nameof(_configuration.MqttPort), true, DataSourceUpdateMode.OnPropertyChanged);
170 textBox2.DataBindings.Add(nameof(textBox2.Text), _configuration, nameof(_configuration.MqttUsername), true, DataSourceUpdateMode.OnPropertyChanged);
171 textBox3.DataBindings.Add(nameof(textBox3.Text), _configuration, nameof(_configuration.MqttPassword), true, DataSourceUpdateMode.OnPropertyChanged);
172 textBox4.DataBindings.Add(nameof(textBox4.Text), _configuration, nameof(_configuration.MqttTopic), true, DataSourceUpdateMode.OnPropertyChanged);
173 }
174  
5 office 175 private void MqttClient_MqttSubscribeSucceeded(object sender, MQTTnet.Client.MqttClientSubscribeResultCode e)
1 office 176 {
177 pictureBox2.BackColor = Color.Green;
178 }
179  
5 office 180 private void MqttClient_MqttSubscribeFailed(object sender, MQTTnet.Client.MqttClientSubscribeResultCode e)
1 office 181 {
182 pictureBox2.BackColor = Color.Red;
183 }
184  
185 private void MqttClient_MqttDisconnected(object sender, EventArgs e)
186 {
187 pictureBox1.BackColor = Color.Red;
188 pictureBox2.BackColor = Color.Red;
189 }
190  
191 private void MqttClient_MqttConnectionSucceeded(object sender, EventArgs e)
192 {
193 pictureBox1.BackColor = Color.Green;
194 }
195  
196 private void MqttClient_MqttConnectionFailed(object sender, EventArgs e)
197 {
198 pictureBox1.BackColor = Color.Red;
199 pictureBox2.BackColor = Color.Red;
200 }
201  
202 private void Button2_Click(object sender, EventArgs e)
203 {
204 foreach (var item in listBox1.SelectedItems)
205 {
206 _configuration.BluetoothWatchList.Remove((string) item);
207 }
208  
209 _bluetoothWatchListBindingSource.ResetBindings(true);
210 }
211  
212 private void Button1_Click(object sender, EventArgs e)
213 {
214 if (string.IsNullOrEmpty(textBox5.Text))
215 {
216 return;
217 }
218  
219 if (_configuration.BluetoothWatchList.Contains(textBox5.Text))
220 {
221 return;
222 }
223  
224 _configuration.BluetoothWatchList.Add(textBox5.Text);
225  
226 _bluetoothWatchListBindingSource.ResetBindings(true);
227 }
228  
229 private void Button3_Click(object sender, EventArgs e)
230 {
231 if (!BluetoothRadio.IsSupported)
232 {
233 Log.Information("Bluetooth radio is not supported.");
234 return;
235 }
236  
237 _bluetoothComponent.DiscoverDevicesAsync(255, true, true, true, true, null);
238  
239 Log.Information("Bluetooth scan started.");
240 }
241  
242 private void LocalComponent_DiscoverDevicesComplete(object sender, DiscoverDevicesEventArgs e)
243 {
244 Log.Information("Bluetooth scan finished.");
245  
246 _bluetoothWatchListBindingSource.ResetBindings(true);
247 }
248  
249 private void LocalComponent_DiscoverDevicesProgress(object sender, DiscoverDevicesEventArgs e)
250 {
251 foreach (var deviceInfo in e.Devices)
252 {
253 if (!_configuration.BluetoothWatchList.Contains(deviceInfo.DeviceName))
254 {
255 _configuration.BluetoothWatchList.Add(deviceInfo.DeviceName);
256  
257 _bluetoothWatchListBindingSource.ResetBindings(true);
258 }
259 }
260 }
261  
262 private void Button5_Click(object sender, EventArgs e)
263 {
264 foreach (var item in listBox2.SelectedItems)
265 {
266 _configuration.WindowsWatchList.Remove((string) item);
267 }
268  
269  
270 _windowsWatchListBindingSource.ResetBindings(true);
271 }
272  
273 private void Button4_Click(object sender, EventArgs e)
274 {
275 foreach (var item in listBox3.SelectedItems)
276 {
277 if (_configuration.WindowsWatchList.Contains((string) item))
278 {
279 continue;
280 }
281  
282 _configuration.WindowsWatchList.Add((string) item);
283 }
284  
285 _windowsWatchListBindingSource.ResetBindings(true);
286 }
287  
288 private void Button6_Click(object sender, EventArgs e)
289 {
290 listBox3.Items.Clear();
291  
292 foreach (var handle in Helpers.FindWindows((wnd, param) => true))
293 {
294 var title = Helpers.GetWindowTitle(handle);
295 if (string.IsNullOrEmpty(title))
296 {
297 continue;
298 }
299  
300 listBox3.Items.Add(title);
301 }
302 }
303  
304 private void SettingsForm_ResizeBegin(object sender, EventArgs e)
305 {
306 SuspendLayout();
307 }
308  
309 private void SettingsForm_ResizeEnd(object sender, EventArgs e)
310 {
311 ResumeLayout(true);
312 }
313  
314 #endregion
315 }
316 }