-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFreshness.cs
142 lines (125 loc) · 5.84 KB
/
Freshness.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
/////////////////////////////////////////////////////////////////////////////////
// CodeLab for Paint.NET
// Portions Copyright ©2007-2020 BoltBait. All Rights Reserved.
// Portions Copyright ©Microsoft Corporation. All Rights Reserved.
//
// THE CODELAB DEVELOPERS MAKE NO WARRANTY OF ANY KIND REGARDING THE CODE. THEY
// SPECIFICALLY DISCLAIM ANY WARRANTY OF FITNESS FOR ANY PARTICULAR PURPOSE OR
// ANY OTHER WARRANTY. THE CODELAB DEVELOPERS DISCLAIM ALL LIABILITY RELATING
// TO THE USE OF THIS CODE. NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR
// OTHERWISE, TO ANY INTELLECTUAL PROPERTY RIGHTS IS GRANTED HEREIN.
//
// Latest distribution: https://www.BoltBait.com/pdn/codelab
/////////////////////////////////////////////////////////////////////////////////
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PdnCodeLab
{
internal static class Freshness
{
private const string ThisVersion = CodeLab.Version;
private const string WebUpdateFile = "https://www.boltbait.com/versions.txt"; // The web site to check for updates
private const string ThisApplication = "1"; // in the WebUpadteFile, CodeLab is application #1
// format of the versions.txt file: application number;current version;URL to download current version
// for example: 1;2.13;https://boltbait.com/pdn/CodeLab/CodeLab213.zip
// each application on its own line
private static string updateURL;
private static string updateVER;
private static bool needToShowNotification = false;
private static readonly HttpClient httpClient = new HttpClient();
internal static void DisplayUpdateNotification()
{
if (needToShowNotification)
{
needToShowNotification = false;
if (FlexibleMessageBox.Show("An update to CodeLab is available.\n\nWould you like to download CodeLab v" + updateVER + "?\n\n(This will not close your current CodeLab session.)", "CodeLab Updater", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
UIUtil.LaunchUrl(null, updateURL);
}
}
}
internal static async Task GoCheckForUpdates(bool silentMode, bool force)
{
updateURL = string.Empty;
updateVER = string.Empty;
needToShowNotification = false;
if (!force)
{
// only check for updates every 7 days
if (Math.Abs((Settings.LatestUpdateCheck - DateTime.Today).TotalDays) < 7)
{
return; // not time yet
}
}
UpdateStatus updateStatus = UpdateStatus.Unknown;
HttpResponseMessage response = null;
Random r = new Random(); // defeat any cache by appending a random number to the URL
try
{
response = await httpClient.GetAsync($"{WebUpdateFile}?r={r.Next(int.MaxValue)}");
}
catch
{
}
if (response != null && response.IsSuccessStatusCode)
{
Settings.LatestUpdateCheck = DateTime.Now;
string content = await response.Content.ReadAsStringAsync();
foreach (string line in content.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
{
string[] data = line.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (data.Length >= 2 && data[0] == ThisApplication)
{
if (Version.TryParse(data[1], out Version remoteVersion) &&
Version.TryParse(ThisVersion, out Version localVersion))
{
if (remoteVersion > localVersion)
{
updateStatus = UpdateStatus.UpdateAvailable;
updateVER = data[1];
updateURL = data[2];
}
else
{
updateStatus = UpdateStatus.UpToDate;
}
}
break;
}
}
}
response?.Dispose();
if (silentMode)
{
needToShowNotification = updateStatus == UpdateStatus.UpdateAvailable;
}
else
{
switch (updateStatus)
{
case UpdateStatus.Unknown:
FlexibleMessageBox.Show("I'm not sure if you are up-to-date.\n\nI was not able to reach the update website.\n\nTry again later.", "CodeLab Updater", MessageBoxButtons.OK, MessageBoxIcon.Warning);
break;
case UpdateStatus.UpToDate:
FlexibleMessageBox.Show("You are up-to-date!", "CodeLab Updater", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case UpdateStatus.UpdateAvailable:
if (FlexibleMessageBox.Show("An update to CodeLab is available.\n\nWould you like to download CodeLab v" + updateVER + "?\n\n(This will not close your current CodeLab session.)", "CodeLab Updater", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
UIUtil.LaunchUrl(null, updateURL);
}
break;
}
}
}
private enum UpdateStatus
{
Unknown,
UpToDate,
UpdateAvailable
}
}
}