forked from TechieGuy12/PlexServerAutoUpdater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
267 lines (244 loc) · 7.68 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
using System;
using System.Collections.Generic;
using System.Drawing;
using static System.Environment;
using System.Threading;
using System.Threading.Tasks;
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;
#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()
{
ToBeClosed = false;
cts = new CancellationTokenSource();
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)
{
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();
}
catch (Exception ex)
{
txtUpdateStatus.Text += $"ERROR: {ex.Message}{NewLine}";
Log.Write(ex);
}
finally
{
if (cts != null)
{
cts.Dispose();
}
Initialize();
}
}
/// <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);
}
#endregion
#region Private Functions
/// <summary>
/// Initializes the values on the form.
/// </summary>
private void Initialize()
{
try
{
Log.Write("Initializing the Plex media server object.");
server = new MediaServer();
if (server == null)
{
Log.Write(
"The Plex media server object could not be initialized. Setting the flag to close the application.");
ToBeClosed = true;
return;
}
server.UpdateMessage +=
new MediaServer.UpdateMessageHandler(ServerUpdateMessage);
lblInstalledVersion.Text = server.CurrentVersion.ToString();
lblLatestVersion.Text = server.LatestVersion.ToString();
if (server.LatestVersion > server.CurrentVersion)
{
btnUpdate.Visible = true;
btnCancel.Visible = false;
btnExit.Enabled = true;
}
else
{
btnUpdate.Visible = false;
btnCancel.Visible = false;
btnExit.Enabled = true;
}
}
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;
}
}
#endregion
}
}