-
-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathUpdateChecker.cs
More file actions
191 lines (163 loc) · 6.89 KB
/
UpdateChecker.cs
File metadata and controls
191 lines (163 loc) · 6.89 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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using RestSharp;
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinDynamicDesktop
{
[Flags]
public enum RestartFlags
{
/// <summary>No restart restrictions</summary>
NONE = 0,
/// <summary>Do not restart if process terminates due to unhandled exception</summary>
RESTART_NO_CRASH = 1,
/// <summary>Do not restart if process terminates due to application not responding</summary>
RESTART_NO_HANG = 2,
/// <summary>Do not restart if process terminates due to installation of update</summary>
RESTART_NO_PATCH = 4,
/// <summary>Do not restart if process terminates due to computer restart as result of an update</summary>
RESTART_NO_REBOOT = 8
}
class UpdateChecker
{
private static readonly Func<string, string> _ = Localization.GetTranslation;
private static ToolStripMenuItem menuItem;
public static void Initialize()
{
if (UwpDesktop.IsRunningAsUwp())
{
return;
}
AppContext.notifyIcon.BalloonTipClicked += OnBalloonTipClicked;
TryCheckAuto();
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
internal static extern uint RegisterApplicationRestart(string pwzCommandline, uint dwFlags);
public static ToolStripItem[] GetMenuItems()
{
if (!UwpDesktop.IsRunningAsUwp())
{
menuItem = new ToolStripMenuItem(_("Check for &updates once a week"), null, OnAutoUpdateItemClick);
menuItem.Checked = JsonConfig.settings.autoUpdateCheck;
return new ToolStripItem[]
{
menuItem,
new ToolStripSeparator()
};
}
else
{
return Array.Empty<ToolStripItem>();
}
}
public static string GetCurrentVersion()
{
return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
private static async Task<string> GetLatestVersion()
{
var client = new RestClient("https://api.github.com");
var request = new RestRequest("repos/t1m0thyj/WinDynamicDesktop/releases/latest");
var response = await client.ExecuteAsync<GitHubApiData>(request);
return response.IsSuccessful ? response.Data.tag_name.Substring(1) : null;
}
private static bool IsUpdateAvailable(string currentVersion, string latestVersion)
{
Version current = new Version(currentVersion);
Version latest = new Version(latestVersion);
return (latest > current);
}
public static async Task CheckManual()
{
string currentVersion = GetCurrentVersion();
string latestVersion = await GetLatestVersion();
if (latestVersion == null)
{
MessageDialog.ShowWarning(_("WinDynamicDesktop could not connect to the Internet to check for " +
"updates."), _("Error"));
}
else if (IsUpdateAvailable(currentVersion, latestVersion))
{
DialogResult result = MessageDialog.ShowQuestion(string.Format(_("There is a newer version of " +
"WinDynamicDesktop available. Do you want to download the update now?\n\nCurrent Version: {0}\n" +
"Latest Version: {1}"), currentVersion, latestVersion), _("Update Available"));
if (result == DialogResult.Yes)
{
UwpDesktop.GetHelper().OpenUpdateLink();
}
}
else
{
MessageDialog.ShowInfo(_("You already have the latest version of WinDynamicDesktop installed."),
_("Up To Date"));
}
}
private static async Task CheckAuto()
{
string currentVersion = GetCurrentVersion();
string latestVersion = await GetLatestVersion();
if (latestVersion == null)
{
return;
}
else if (IsUpdateAvailable(currentVersion, latestVersion))
{
AppContext.ShowPopup(string.Format(_("WinDynamicDesktop {0} is available. Click here to download it."),
latestVersion), _("Update Available"));
}
JsonConfig.settings.lastUpdateCheckTime = DateTime.Now.ToString(CultureInfo.InvariantCulture);
}
public static void TryCheckAuto(bool forceIfEnabled = false)
{
PreserveTempDlls();
if (UwpDesktop.IsRunningAsUwp() || JsonConfig.settings.autoUpdateCheck == false)
{
return;
}
if (JsonConfig.settings.lastUpdateCheckTime != null && !forceIfEnabled)
{
DateTime lastUpdateCheck = DateTime.Parse(JsonConfig.settings.lastUpdateCheckTime,
CultureInfo.InvariantCulture);
TimeSpan timeDiff = new TimeSpan(DateTime.Now.Ticks - lastUpdateCheck.Ticks);
if (timeDiff.Days < 7)
{
return;
}
}
Task.Run(CheckAuto);
}
private static void PreserveTempDlls()
{
// Update access time for extracted DLL files to prevent Storage Sense from deleting them
string tempLibPath = Path.Combine(Path.GetTempPath(), ".net", Application.ProductName);
if (Directory.Exists(tempLibPath))
{
foreach (string filePath in Directory.EnumerateFiles(tempLibPath, "*.dll", SearchOption.AllDirectories))
{
File.SetLastAccessTime(filePath, DateTime.Now);
}
}
}
private static void OnAutoUpdateItemClick(object sender, EventArgs e)
{
bool isEnabled = JsonConfig.settings.autoUpdateCheck ^ true;
JsonConfig.settings.autoUpdateCheck = isEnabled;
menuItem.Checked = isEnabled;
TryCheckAuto(true);
}
private static void OnBalloonTipClicked(object sender, EventArgs e)
{
if (AppContext.notifyIcon.BalloonTipTitle == _("Update Available"))
{
UwpDesktop.GetHelper().OpenUpdateLink();
}
}
}
}