-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
MainForm.cs
419 lines (386 loc) · 13.9 KB
/
MainForm.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System;
using System.Collections.Generic;
using System.Drawing;
using static System.Environment;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
namespace TE.Plex
{
/// <summary>
/// The Plex Media Server Updater main form.
/// </summary>
public partial class MainForm : Form
{
#region Private Variables
/// <summary>
/// The media server object.
/// </summary>
private MediaServer _server = null;
/// <summary>
/// The cancellation token source.
/// </summary>
private CancellationTokenSource _cts = null;
/// <summary>
/// The wait timer.
/// </summary>
private System.Timers.Timer _timer = null;
#endregion
#region Properties
/// <summary>
/// Gets a flag indicating if the form is to be closed.
/// </summary>
public bool ToBeClosed { get; private set; }
#endregion
#region Constructors
/// <summary>
/// Initializes the form.
/// </summary>
public MainForm()
{
InitializeComponent();
Initialize();
}
#endregion
#region Events
/// <summary>
/// Cancels the Plex update..
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// Event-related arguments.
/// </param>
void BtnCancelClick(object sender, EventArgs e)
{
_cts?.Cancel();
}
/// <summary>
/// Close the form.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// Event-related arguments.
/// </param>
private void btnExit_Click(object sender, EventArgs e)
{
Log.Write("Closing the application.");
Close();
}
/// <summary>
/// Performs the Plex Media Server update.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// Event-related arguments.
/// </param>
void BtnUpdateClick(object sender, EventArgs e)
{
if (CheckIfCanUpdate())
{
PerformUpdate();
}
}
/// <summary>
/// Enables or disables the controls and timers on the form.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// Event-related arguments.
/// </param>
private void chkWait_CheckedChanged(object sender, EventArgs e)
{
lblCheckEveryLabel.Enabled = chkWait.Checked;
lblCheckSecondsLabel.Enabled = chkWait.Checked;
numSeconds.Enabled = chkWait.Checked;
if (_timer != null)
{
_timer.Enabled = chkWait.Checked;
}
}
/// <summary>
/// The form is shown.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// Arguments associated with the event.
/// </param>
private void MainForm_Shown(object sender, EventArgs e)
{
// If the form is to be closed, then close the form.
if (ToBeClosed)
{
Log.Write("Closing the application.");
Close();
}
}
/// <summary>
/// The messages from the update execution.
/// </summary>
/// <param name="sender">
/// The sender object.
/// </param>
/// <param name="message">
/// The message to display on the form.
/// </param>
private void ServerUpdateMessage(object sender, string message)
{
txtUpdateStatus.Text += $"{message}{NewLine}";
Log.Write(message);
}
/// <summary>
/// The timer has elapsed so check the play count to see if the server
/// is in use.
/// </summary>
/// <param name="source">
/// The sender.
/// </param>
/// <param name="e"
/// Elapsed event arguments.
/// ></param>
private void OnTimedEvent(object sender, ElapsedEventArgs e)
{
if (_server == null)
{
_timer.Enabled = false;
return;
}
if (CheckIfCanUpdate())
{
PerformUpdate();
}
}
#endregion
#region Private Functions
/// <summary>
/// Checks to see if the server can be updated at this time.
/// </summary>
private bool CheckIfCanUpdate()
{
if (_server == null)
{
txtUpdateStatus.Text += "The server was not specified. Cannot perform the update.";
Log.Write("The server was not specified. Cannot perform the update.");
_timer.Enabled = false;
return false;
}
int playCount = _server.GetPlayCount();
int inProgressRecordingCount = _server.GetInProgressRecordingCount();
// No item is currently being played
if (playCount == 0 && inProgressRecordingCount == 0)
{
txtUpdateStatus.Text += "The server is not in use continuing to perform the update.";
Log.Write("The server is not in use continuing to perform the update.");
lblPlayCount.Text = playCount.ToString();
lblInProgressRecordingCount.Text = inProgressRecordingCount.ToString();
btnUpdate.Enabled = true;
_timer.Enabled = false;
return true;
}
// At least one item is being played
else if (playCount > 0 || inProgressRecordingCount > 0)
{
lblPlayCount.Text = playCount.ToString();
lblInProgressRecordingCount.Text = inProgressRecordingCount.ToString();
if (chkWait.Checked)
{
txtUpdateStatus.Text += "Waiting for the server to be free has been enabled. Server update can begin. Waiting for all media and/or in progress recordings to be stopped.";
Log.Write("The server is in use. Waiting for all media and/or in progress recordings to be stopped before performing the update.");
btnUpdate.Enabled = false;
_timer.Interval =
Convert.ToDouble(Math.Abs(numSeconds.Value) * 1000);
_timer.Enabled = true;
return false;
}
else if (!chkWait.Checked && inProgressRecordingCount > 0)
{
txtUpdateStatus.Text += "The wait option has been disabled, but you cannot update the server while there is a recording in progress. Waiting for all in progress recordings to be stopped.";
Log.Write("The server is in use. Waiting for all media and/or in progress recordings to be stopped before performing the update.");
btnUpdate.Enabled = false;
_timer.Interval =
Convert.ToDouble(Math.Abs(numSeconds.Value) * 1000);
_timer.Enabled = true;
return false;
}
else
{
txtUpdateStatus.Text += "The wait option has been disabled. You can go ahead and update the server.";
Log.Write("The wait option has been disabled. You can go ahead and update the server.");
btnUpdate.Enabled = true;
_timer.Enabled = false;
return true;
}
}
// Could not determine how many items are being played
else
{
txtUpdateStatus.Text += "The server in use status could not be determined. The server can be updated if you wish.";
Log.Write("The server in use status could not be determined. The server can be updated if you wish.");
lblPlayCount.Text = "Unknown";
lblInProgressRecordingCount.Text = "Unknown";
btnUpdate.Enabled = true;
_timer.Enabled = false;
return true;
}
}
/// <summary>
/// Initializes the values on the form.
/// </summary>
private void Initialize()
{
try
{
ToBeClosed = false;
_cts = new CancellationTokenSource();
Log.Write("Initializing the timer object.");
_timer = new System.Timers.Timer();
_timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
_timer.Enabled = false;
_timer.Interval = Convert.ToDouble(numSeconds.Value * 1000);
Log.Write("Initializing the Plex media server object.");
_server = new MediaServer(ServerUpdateMessage);
if (_server == null)
{
Log.Write(
"The Plex media server object could not be initialized. Setting the flag to close the application.");
ToBeClosed = true;
return;
}
lblInstalledVersion.Text = _server.CurrentVersion.ToString();
lblLatestVersion.Text = _server.LatestVersion.ToString();
if (_server.LatestVersion > _server.CurrentVersion)
{
btnUpdate.Visible = true;
btnCancel.Visible = false;
btnExit.Enabled = true;
CheckIfCanUpdate();
}
else
{
btnUpdate.Visible = false;
btnCancel.Visible = false;
btnExit.Enabled = true;
if (_server.GetPlayCount() >= 0 || _server.GetInProgressRecordingCount() >= 0)
{
lblPlayCount.Text = _server.PlayCount.ToString();
lblInProgressRecordingCount.Text = _server.InProgressRecordingCount.ToString();
}
else
{
lblPlayCount.Text = "Unknown";
lblInProgressRecordingCount.Text = "Unknown";
}
}
}
catch (LocalSystem.Msi.MSIException ex)
{
MessageBox.Show(
$"MSI exception: {ex.Message}",
"Plex Updater Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
Log.Write(ex);
ToBeClosed = true;
}
catch (AppNotInstalledException ex)
{
MessageBox.Show(
"The Plex Server application is not installed.",
"Plex Updater Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
Log.Write(ex);
ToBeClosed = true;
}
catch (ServiceNotInstalledException ex)
{
MessageBox.Show(
"The Plex service is not installed.",
"Plex Updater Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
Log.Write(ex);
ToBeClosed = true;
}
catch (PlexDataFolderNotFoundException ex)
{
MessageBox.Show(
"The Plex data folder could not be found.",
"Plex Updater Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
Log.Write(ex);
ToBeClosed = true;
}
catch (LocalSystem.WindowsUserSidNotFound ex)
{
MessageBox.Show(
"The SID for the Plex service user could not be found.",
"Plex Updater Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
Log.Write(ex);
ToBeClosed = true;
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Plex Updater Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
Log.Write(ex);
ToBeClosed = true;
}
}
/// <summary>
/// Perform the server update.
/// </summary>
private void PerformUpdate()
{
try
{
btnUpdate.Enabled = false;
btnCancel.Visible = false;
btnExit.Enabled = false;
if (_cts == null)
{
_cts = new CancellationTokenSource();
}
CancellationToken ct = _cts.Token;
Task plexUpdate = Task.Factory.StartNew(() =>
{
// Throw an exception if already cancelled
ct.ThrowIfCancellationRequested();
_server.Update();
}, _cts.Token);
plexUpdate.Wait();
if (!_server.IsRunning())
{
Log.Write("The Plex server was not started successfully.");
}
}
catch (Exception ex)
{
txtUpdateStatus.Text += $"ERROR: {ex.Message}{NewLine}";
Log.Write(ex);
}
finally
{
_cts?.Dispose();
Initialize();
}
}
#endregion
}
}