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
273 lines (249 loc) · 10.5 KB
/
UpdateDialog.cs
File metadata and controls
273 lines (249 loc) · 10.5 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
using System;
using System.IO;
using System.Net;
using System.Windows.Forms;
using System.ComponentModel;
using PluginCore.Localization;
using PluginCore.Managers;
using PluginCore.Helpers;
using PluginCore.Controls;
using PluginCore;
namespace FlashDevelop.Dialogs
{
public class UpdateDialog : SmartForm
{
private UpdateInfo updateInfo = null;
private System.Windows.Forms.Label infoLabel;
private System.Windows.Forms.Button closeButton;
private System.Windows.Forms.Button downloadButton;
private System.ComponentModel.BackgroundWorker worker;
private String URL = DistroConfig.DISTRIBUTION_VERSION;
private static Boolean silentCheck = false;
public UpdateDialog()
{
this.Owner = Globals.MainForm;
this.Font = Globals.Settings.DefaultFont;
this.FormGuid = "4d5fdc1c-2698-46e9-b22d-fa9a42ba8d26";
this.InitializeComponent();
this.ApplyLocalizedTexts();
this.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>
private void InitializeComponent()
{
this.downloadButton = new System.Windows.Forms.ButtonEx();
this.infoLabel = new System.Windows.Forms.Label();
this.closeButton = new System.Windows.Forms.ButtonEx();
this.SuspendLayout();
//
// downloadButton
//
this.downloadButton.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
this.downloadButton.Enabled = false;
this.downloadButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.downloadButton.Location = new System.Drawing.Point(72, 69);
this.downloadButton.Name = "downloadButton";
this.downloadButton.Size = new System.Drawing.Size(116, 23);
this.downloadButton.TabIndex = 2;
this.downloadButton.Text = "&Download Update";
this.downloadButton.UseVisualStyleBackColor = true;
this.downloadButton.Click += new System.EventHandler(this.DownloadButtonClick);
//
// infoLabel
//
this.infoLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
this.infoLabel.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.infoLabel.Location = new System.Drawing.Point(13, 13);
this.infoLabel.Name = "infoLabel";
this.infoLabel.Size = new System.Drawing.Size(327, 44);
this.infoLabel.TabIndex = 0;
this.infoLabel.Text = "There is a newer version of FlashDevelop available.\r\n\r\nYour version: *.*.* / Server version: *.*.*";
this.infoLabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// closeButton
//
this.closeButton.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
this.closeButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.closeButton.Location = new System.Drawing.Point(194, 69);
this.closeButton.Name = "closeButton";
this.closeButton.Size = new System.Drawing.Size(84, 23);
this.closeButton.TabIndex = 1;
this.closeButton.Text = "&Close";
this.closeButton.UseVisualStyleBackColor = true;
this.closeButton.Click += new System.EventHandler(this.CloseButtonClick);
//
// UpdateDialog
//
this.CancelButton = this.closeButton;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(353, 105);
this.Controls.Add(this.closeButton);
this.Controls.Add(this.downloadButton);
this.Controls.Add(this.infoLabel);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "UpdateDialog";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = " Update Check";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.DialogClosed);
this.ResumeLayout(false);
}
#endregion
#region Methods And Event Handlers
/// <summary>
/// Applies the localized texts to the form
/// </summary>
private void ApplyLocalizedTexts()
{
this.Text = " " + TextHelper.GetString("Title.UpdateDialog");
this.infoLabel.Text = TextHelper.GetString("Info.CheckingUpdates");
this.downloadButton.Text = TextHelper.GetString("Label.DownloadUpdate");
this.closeButton.Text = TextHelper.GetString("Label.Close");
}
/// <summary>
/// Downloads the new flashdevelop release
/// </summary>
private void DownloadButtonClick(Object sender, EventArgs e)
{
try
{
String address = this.updateInfo.DownloadUrl;
ProcessHelper.StartAsync(address);
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
}
this.Close();
}
/// <summary>
/// When the form is closed cancel the update check
/// </summary>
private void DialogClosed(Object sender, FormClosedEventArgs e)
{
if (this.worker.IsBusy)
{
this.worker.CancelAsync();
}
}
/// <summary>
/// Closes the dialog when user clicks buttons
/// </summary>
private void CloseButtonClick(Object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// Startups the update check
/// </summary>
private void InitializeUpdating()
{
this.worker = new BackgroundWorker();
this.worker.DoWork += new DoWorkEventHandler(this.WorkerDoWork);
this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.WorkerCompleted);
this.worker.WorkerSupportsCancellation = true;
this.worker.RunWorkerAsync();
}
/// <summary>
/// Does the actual work on background
/// </summary>
private void WorkerDoWork(Object sender, DoWorkEventArgs e)
{
try
{
WebRequest request = WebRequest.Create(URL);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
String version = reader.ReadLine(); // Read version
String download = reader.ReadLine(); // Read download
String product = Application.ProductName; // Internal version
Int32 lenght = DistroConfig.DISTRIBUTION_NAME.Length + 1;
String current = product.Substring(lenght, product.IndexOfOrdinal(" for") - lenght);
stream.Close(); response.Close(); // Close all resources
e.Result = new UpdateInfo(current, version, download);
}
catch (Exception ex)
{
ErrorManager.AddToLog("Update check failed.", ex);
e.Result = null;
}
}
/// <summary>
/// Handles the finish of the update check
/// </summary>
private void WorkerCompleted(Object sender, RunWorkerCompletedEventArgs e)
{
this.updateInfo = (UpdateInfo)e.Result;
if (this.updateInfo == null)
{
String info = TextHelper.GetString("Info.UpdateCheckFailed");
String formatted = String.Format(info, "\n\n");
this.infoLabel.Text = formatted;
if (silentCheck) this.Close();
}
else if (this.updateInfo.NeedsUpdate)
{
this.downloadButton.Enabled = true;
String info = TextHelper.GetString("Info.UpdateAvailable");
String formatted = String.Format(info, "\n\n", this.updateInfo.UserVersion, this.updateInfo.ServerVersion);
this.infoLabel.Text = formatted;
if (silentCheck) this.ShowDialog();
}
else
{
String info = TextHelper.GetString("Info.NoUpdateAvailable");
this.infoLabel.Text = info;
if (silentCheck) this.Close();
}
}
/// <summary>
/// Shows the update dialog
/// </summary>
public static void Show(Boolean silent)
{
silentCheck = silent;
using (UpdateDialog updateDialog = new UpdateDialog())
if (!silentCheck) updateDialog.ShowDialog();
}
#endregion
}
#region UpdateInfo
public class UpdateInfo
{
public String UserVersion = null;
public String ServerVersion = null;
public String DownloadUrl = "http://www.flashdevelop.org/community/viewforum.php?f=11";
public Boolean NeedsUpdate = false;
public UpdateInfo(String userVersion, String serverVersion, String downloadUrl)
{
this.UserVersion = userVersion;
this.ServerVersion = serverVersion;
this.DownloadUrl = downloadUrl;
this.ParseNeedsUpdate();
}
/// <summary>
/// Parses the needs update value from the version strings
/// </summary>
private void ParseNeedsUpdate()
{
try
{
Int32 result = String.Compare(this.UserVersion, this.ServerVersion, true);
this.NeedsUpdate = (result < 0);
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
}
}
}
#endregion
}