-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
ButtplugAboutControl.xaml.cs
239 lines (210 loc) · 8.81 KB
/
ButtplugAboutControl.xaml.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
using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using Buttplug.Core;
using JetBrains.Annotations;
using Newtonsoft.Json.Linq;
using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;
using TabControl = System.Windows.Controls.TabControl;
namespace Buttplug.Components.Controls
{
/// <summary>
/// Interaction logic for ButtplugAboutControl.xaml
/// </summary>
public partial class ButtplugAboutControl
{
private string _gitHash;
private string _buildType;
private uint _clickCounter;
private ButtplugConfig _config;
private string _appName;
public event EventHandler AboutImageClickedABunch;
public ButtplugAboutControl()
{
InitializeComponent();
}
public void InitializeVersion()
{
AboutVersionNumber.Text = Assembly.GetEntryAssembly().GetName().Version.ToString();
var longVer = System.Diagnostics.FileVersionInfo.GetVersionInfo(Application.ResourceAssembly.Location)
.ProductVersion;
if (longVer.Length > 0)
{
AboutVersionNumber.Text = longVer;
}
// AssemblyInformationalVersion("1.0.0.0-dev")
var pos = longVer.IndexOf('-');
if (pos > 0)
{
_buildType = longVer.Substring(pos);
}
AboutGitVersion.Text = string.Empty;
var attribute = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyGitVersion), false)[0];
if (attribute != null && ((AssemblyGitVersion)attribute).Value.Length > 0)
{
_gitHash = ((AssemblyGitVersion)attribute).Value;
AboutGitVersion.Text = _gitHash;
AboutGitVersion.MouseDown += GithubRequestNavigate;
}
}
public string GetAboutVersion()
{
return AboutVersionNumber.Text + " " + _gitHash;
}
private void GithubRequestNavigate(object aObj, MouseButtonEventArgs aEvent)
{
System.Diagnostics.Process.Start(
new Uri($"http://github.com/metafetish/buttplug-csharp/commit/{_gitHash}").AbsoluteUri);
}
private void PatreonRequestNavigate(object aObj, MouseButtonEventArgs aEvent)
{
System.Diagnostics.Process.Start(new Uri("http://patreon.com/qdot").AbsoluteUri);
}
private void Hyperlink_RequestNavigate(object aSender, System.Windows.Navigation.RequestNavigateEventArgs aEvent)
{
System.Diagnostics.Process.Start(aEvent.Uri.AbsoluteUri);
}
private void IconImage_Click(object aSender, RoutedEventArgs aEvent)
{
_clickCounter += 1;
if (_clickCounter < 5)
{
return;
}
IconImage.MouseDown -= IconImage_Click;
AboutImageClickedABunch?.Invoke(this, aEvent);
}
private void LicenseHyperlink_Click(object aSender, RoutedEventArgs aEvent)
{
new LicenseView().Show();
}
public void CheckUpdate([NotNull] ButtplugConfig config, [NotNull] string appName)
{
if (Application.Current == null)
{
// Avoid test harness
return;
}
_config = config;
_appName = appName;
var check = _config.GetValue(appName + ".updateCheck");
if (check == null)
{
var result = System.Windows.MessageBox.Show("Do you want Buttplug to automatically check for updates on start?", "Update Checking", MessageBoxButton.YesNo, MessageBoxImage.Asterisk);
check = result == MessageBoxResult.Yes ? "true" : "false";
_config.SetValue(appName + ".updateCheck", check);
}
AutoUpdateCheck.IsChecked = bool.Parse(check);
UpdateGroup.Visibility = Visibility.Visible;
if (!bool.Parse(check))
{
return;
}
CheckForUpdates_Click(this, null);
}
private void AutoUpdateCheck_Checked(object sender, RoutedEventArgs e)
{
if (_config != null && _appName != null && _appName.Length > 0)
{
_config.SetValue(_appName + ".updateCheck", true.ToString());
}
}
private void AutoUpdateCheck_Unchecked(object sender, RoutedEventArgs e)
{
if (_config != null && _appName != null && _appName.Length > 0)
{
_config.SetValue(_appName + ".updateCheck", false.ToString());
}
}
private void CheckForUpdates_Click(object sender, RoutedEventArgs e)
{
if (_config == null || _appName == null || _appName.Length <= 0)
{
return;
}
UpdateCheckStatus.Inlines.Clear();
UpdateCheckStatus.Text = "Checking for updates... (" + DateTime.Now.ToString() + ")";
try
{
string html = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://updates.buttplug.io/updates.json");
request.AutomaticDecompression = DecompressionMethods.GZip;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
var json = JObject.Parse(html);
var latest = json[_appName]["version"]?.ToString();
if (latest == null)
{
return;
}
// Why isn't this just using the AssemblyVersion?!?!
var dashPosition = AboutVersionNumber.Text.IndexOf('-');
string numericVer;
numericVer = dashPosition >= 0 ? AboutVersionNumber.Text.Substring(0, dashPosition) : AboutVersionNumber.Text;
var cVer = Version.Parse(numericVer);
var lVer = Version.Parse(latest);
// Reverse this sign to test
if (cVer.CompareTo(lVer) < 0)
{
// Update available
UpdateCheckStatus.Text = "Update Available! (" + DateTime.Now.ToString() + ")";
Hyperlink hyperlink = null;
try
{
var location = json[_appName]["location"]?.ToString();
if (location != null)
{
hyperlink = new Hyperlink(new Run(location))
{
NavigateUri = new Uri(location),
};
hyperlink.RequestNavigate += Hyperlink_RequestNavigate;
UpdateCheckStatus.Text += "\n";
UpdateCheckStatus.Inlines.Add(hyperlink);
}
}
catch
{
// noop - there was an update, we just don't know where
}
if (MessageBox.Show("A new buttplug update is available! Would you like to go to the update site?",
"Buttplug Update",
MessageBoxButton.YesNo,
MessageBoxImage.Asterisk) == MessageBoxResult.Yes)
{
hyperlink?.DoClick();
}
try
{
((TabControl)((TabItem)Parent).Parent).SelectedItem = Parent;
UpdateCheckStatus.Focus();
}
catch
{
// noop - things went bang
}
}
else
{
UpdateCheckStatus.Text = "No new updates! (" + DateTime.Now.ToString() + ")";
}
}
catch (Exception ex)
{
UpdateCheckStatus.Text = "Error encountered whilst checking for updates! (" + DateTime.Now.ToString() + ")";
new ButtplugLogManager().GetLogger(GetType()).LogException(ex);
}
}
}
}