forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateDialog.cs
More file actions
260 lines (240 loc) · 9.45 KB
/
Copy pathUpdateDialog.cs
File metadata and controls
260 lines (240 loc) · 9.45 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
using System;
using System.IO;
using System.Windows.Forms;
using System.Net.Http;
using System.Text.RegularExpressions;
using FlashDevelop.Settings;
using PluginCore.Localization;
using PluginCore.Managers;
using PluginCore.Helpers;
using PluginCore.Controls;
using PluginCore;
namespace FlashDevelop.Dialogs
{
public class UpdateDialog : SmartForm
{
const string URL = DistroConfig.DISTRIBUTION_VERSION;
readonly HttpClient httpClient = new HttpClient();
UpdateInfo updateInfo;
Label infoLabel;
Button closeButton;
Button downloadButton;
static bool silentCheck;
public UpdateDialog()
{
Owner = Globals.MainForm;
Font = PluginBase.Settings.DefaultFont;
FormGuid = "4d5fdc1c-2698-46e9-b22d-fa9a42ba8d26";
InitializeComponent();
ApplyLocalizedTexts();
InitializeUpdating();
}
#region Windows Form Designer Generated Code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent()
{
downloadButton = new ButtonEx();
infoLabel = new Label();
closeButton = new ButtonEx();
SuspendLayout();
//
// downloadButton
//
downloadButton.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right;
downloadButton.Enabled = false;
downloadButton.FlatStyle = FlatStyle.System;
downloadButton.Location = new System.Drawing.Point(72, 69);
downloadButton.Name = "downloadButton";
downloadButton.Size = new System.Drawing.Size(116, 23);
downloadButton.TabIndex = 2;
downloadButton.Text = "&Download Update";
downloadButton.UseVisualStyleBackColor = true;
downloadButton.Click += DownloadButtonClick;
//
// infoLabel
//
infoLabel.Anchor = (AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right;
infoLabel.FlatStyle = FlatStyle.System;
infoLabel.Location = new System.Drawing.Point(13, 13);
infoLabel.Name = "infoLabel";
infoLabel.Size = new System.Drawing.Size(327, 44);
infoLabel.TabIndex = 0;
infoLabel.Text = "There is a newer version of FlashDevelop available.\r\n\r\nYour version: *.*.* / Server version: *.*.*";
infoLabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// closeButton
//
closeButton.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right;
closeButton.FlatStyle = FlatStyle.System;
closeButton.Location = new System.Drawing.Point(194, 69);
closeButton.Name = "closeButton";
closeButton.Size = new System.Drawing.Size(84, 23);
closeButton.TabIndex = 1;
closeButton.Text = "&Close";
closeButton.UseVisualStyleBackColor = true;
closeButton.Click += CloseButtonClick;
//
// UpdateDialog
//
CancelButton = closeButton;
AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new System.Drawing.Size(353, 105);
Controls.Add(closeButton);
Controls.Add(downloadButton);
Controls.Add(infoLabel);
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
Name = "UpdateDialog";
ShowInTaskbar = false;
StartPosition = FormStartPosition.CenterParent;
Text = " Update Check";
FormClosed += DialogClosed;
ResumeLayout(false);
}
#endregion
#region Methods And Event Handlers
/// <summary>
/// Applies the localized texts to the form
/// </summary>
void ApplyLocalizedTexts()
{
Text = " " + TextHelper.GetString("Title.UpdateDialog");
infoLabel.Text = TextHelper.GetString("Info.CheckingUpdates");
downloadButton.Text = TextHelper.GetString("Label.DownloadUpdate");
closeButton.Text = TextHelper.GetString("Label.Close");
}
/// <summary>
/// Downloads the new flashdevelop release
/// </summary>
void DownloadButtonClick(object sender, EventArgs e)
{
try
{
ProcessHelper.StartAsync(updateInfo.DownloadUrl);
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
}
Close();
}
/// <summary>
/// When the form is closed cancel the update check
/// </summary>
void DialogClosed(object sender, FormClosedEventArgs e) => httpClient.Dispose();
/// <summary>
/// Closes the dialog when user clicks buttons
/// </summary>
void CloseButtonClick(object sender, EventArgs e) => Close();
/// <summary>
/// Startups the update check
/// </summary>
async void InitializeUpdating()
{
try
{
switch (((SettingObject)PluginBase.MainForm.Settings).AutomaticallyCheckUpdatesFor)
{
case UpdateType.StableRelease:
{
var response = await httpClient.GetAsync(URL);
using var stream = await response.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream);
var version = await reader.ReadLineAsync(); // Read version
var download = await reader.ReadLineAsync(); // Read download
var product = Application.ProductName; // Internal version
var length = DistroConfig.DISTRIBUTION_NAME.Length + 1;
var current = product.Substring(length, product.IndexOfOrdinal(" for") - length);
updateInfo = new UpdateInfo(current, version, download);
OnLoadComplete();
break;
}
case UpdateType.PreviewRelease:
{
var response = await httpClient.GetAsync(DistroConfig.DISTRIBUTION_DEV_VERSION);
using var stream = await response.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream);
var version = await reader.ReadLineAsync(); // Read version
version = Regex.Match(version, @"\d.*\d").Value;
var product = Application.ProductName; // Internal version
var length = DistroConfig.DISTRIBUTION_NAME.Length + 1;
var current = product.Substring(length, product.IndexOfOrdinal(" for") - length);
updateInfo = new UpdateInfo(current, version, DistroConfig.DISTRIBUTION_DEV_BUILD);
OnLoadComplete();
break;
}
}
}
catch (Exception ex)
{
ErrorManager.AddToLog("Update check failed.", ex);
}
}
void OnLoadComplete()
{
if (updateInfo is null)
{
var info = TextHelper.GetString("Info.UpdateCheckFailed");
infoLabel.Text = string.Format(info, "\n\n");
if (silentCheck) Close();
}
else if (updateInfo.NeedsUpdate)
{
downloadButton.Enabled = true;
var info = TextHelper.GetString("Info.UpdateAvailable");
infoLabel.Text = string.Format(info, "\n\n", updateInfo.UserVersion, updateInfo.ServerVersion);
if (silentCheck) ShowDialog();
}
else
{
infoLabel.Text = TextHelper.GetString("Info.NoUpdateAvailable");
if (silentCheck) Close();
}
}
/// <summary>
/// Shows the update dialog
/// </summary>
public static void Show(bool silent)
{
silentCheck = silent;
using var dialog = new UpdateDialog();
if (!silentCheck) dialog.ShowDialog();
}
#endregion
}
#region UpdateInfo
public class UpdateInfo
{
public string UserVersion;
public string ServerVersion;
public string DownloadUrl = "http://www.flashdevelop.org/community/viewforum.php?f=11";
public bool NeedsUpdate;
public UpdateInfo(string userVersion, string serverVersion, string downloadUrl)
{
UserVersion = userVersion;
ServerVersion = serverVersion;
DownloadUrl = downloadUrl;
ParseNeedsUpdate();
}
/// <summary>
/// Parses the needs update value from the version strings
/// </summary>
void ParseNeedsUpdate()
{
try
{
NeedsUpdate = string.Compare(UserVersion, ServerVersion, true) < 0;
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
}
}
}
#endregion
}