Skip to content

Commit fbd053f

Browse files
author
Sébastien Geiser
committed
Update Notification
1 parent 7395717 commit fbd053f

File tree

8 files changed

+98
-35
lines changed

8 files changed

+98
-35
lines changed

CSharpRegexTools4Npp/CSharpRegexTools4Npp.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,20 @@
6969
<HintPath>..\packages\Costura.Fody.4.1.0\lib\net40\Costura.dll</HintPath>
7070
</Reference>
7171
<Reference Include="Microsoft.CSharp" />
72+
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
73+
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
74+
</Reference>
7275
<Reference Include="PresentationCore" />
7376
<Reference Include="PresentationFramework" />
7477
<Reference Include="System" />
7578
<Reference Include="System.Configuration" />
7679
<Reference Include="System.Data" />
7780
<Reference Include="System.Drawing" />
7881
<Reference Include="System.IO.Compression" />
82+
<Reference Include="System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
83+
<SpecificVersion>False</SpecificVersion>
84+
<HintPath>..\..\..\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll</HintPath>
85+
</Reference>
7986
<Reference Include="System.Runtime.Serialization" />
8087
<Reference Include="System.ServiceModel" />
8188
<Reference Include="System.Transactions" />

CSharpRegexTools4Npp/Main.cs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
using CSharpRegexTools4Npp.PluginInfrastructure;
2+
using Newtonsoft.Json.Linq;
23
using RegexDialog;
34
using System;
5+
using System.Diagnostics;
46
using System.Drawing;
57
using System.Linq;
8+
using System.Net;
9+
using System.Net.Http;
610
using System.Reflection;
711
using System.Runtime.InteropServices;
812
using System.Windows;
@@ -84,21 +88,71 @@ internal static void SetToolBarIcon()
8488
Marshal.FreeHGlobal(pTbIcons);
8589
}
8690

91+
private static async void CheckUpdates(RegExToolDialog dialog)
92+
{
93+
double hoursFromLastCheck = Math.Abs((DateTime.Now - Config.Instance.LastUpdateCheck).TotalHours);
94+
95+
//if (hoursFromLastCheck > 8)
96+
if (true)
97+
{
98+
ServicePointManager.ServerCertificateValidationCallback += (_, __, ___, ____) => true;
99+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
100+
101+
try
102+
{
103+
HttpClient client = new HttpClient();
104+
client.DefaultRequestHeaders.Add("Accept-Language", "en-US;q=0.5,en;q=0.3");
105+
client.DefaultRequestHeaders.Add("User-Agent", "C# App");
106+
107+
var response = await client.GetAsync("https://api.github.com/repos/codingseb/CSharpRegexTools4Npp/releases/latest").ConfigureAwait(true);
108+
109+
string responseText = await response.Content.ReadAsStringAsync();
110+
111+
var jsonResult = JObject.Parse(responseText);
112+
113+
int[] latestVersion = jsonResult["name"].ToString().Split('.').Select(digit => int.Parse(digit.Trim())).ToArray();
114+
int[] currentVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString().Split('.').Select(digit => int.Parse(digit.Trim())).ToArray();
115+
116+
Debug.WriteLine($"{latestVersion} - {currentVersion}");
117+
118+
for(int i = 0; i < latestVersion.Length && i < currentVersion.Length;i++)
119+
{
120+
if(latestVersion[i] > currentVersion[i])
121+
{
122+
Config.Instance.UpdateAvailable = true;
123+
Config.Instance.UpdateURL = "https://github.com/codingseb/CSharpRegexTools4Npp/releases";
124+
break;
125+
}
126+
else if(latestVersion[i] < currentVersion[i])
127+
{
128+
break;
129+
}
130+
}
131+
}
132+
catch
133+
{ }
134+
}
135+
}
136+
87137
public static void ShowTheDialog()
88138
{
89139
try
90140
{
91141
AppDomain.CurrentDomain.SetupInformation.PrivateBinPath = @"plugins\CSharpRegexTools4Npp";
92142

93-
IntPtr hWnd = FindWindow(null, "C# Regex Tools - " + Assembly.GetCallingAssembly().GetName().Version.ToString());
143+
IntPtr hWnd = FindWindow(null, "C# Regex Tools - " + Assembly.GetExecutingAssembly().GetName().Version.ToString());
94144

95145
if (hWnd.ToInt64() > 0)
96146
{
97147
SetForegroundWindow(hWnd);
98148
}
99149
else
100150
{
101-
var dialog = new RegExToolDialog
151+
RegExToolDialog dialog = null;
152+
153+
RegExToolDialog.InitIsOK = () => CheckUpdates(dialog);
154+
155+
dialog = new RegExToolDialog
102156
{
103157
GetText = () => BNpp.Text,
104158

@@ -157,7 +211,7 @@ public static void ShowTheDialog()
157211
result = false;
158212
}
159213

160-
hWnd = FindWindow(null, "C# Regex Tool - " + Assembly.GetCallingAssembly().GetName().Version.ToString());
214+
hWnd = FindWindow(null, "C# Regex Tool - " + Assembly.GetExecutingAssembly().GetName().Version.ToString());
161215
if (hWnd.ToInt64() > 0)
162216
{
163217
SetForegroundWindow(hWnd);

CSharpRegexTools4Npp/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
<packages>
33
<package id="Costura.Fody" version="4.1.0" targetFramework="net46" developmentDependency="true" />
44
<package id="Fody" version="6.3.0" targetFramework="net46" developmentDependency="true" />
5+
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net47" />
56
</packages>

RegexDialog/RegExToolDialog.xaml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@
180180
<MenuItem Header="_Help and about on Github" Click="Help_MenuItem_Click"/>
181181
</MenuItem>
182182
</Menu>
183-
<Border DockPanel.Dock="Top">
183+
<DockPanel DockPanel.Dock="Top">
184+
<TextBlock DockPanel.Dock="Right"
185+
Visibility="{Binding UpdateAvailable, Source={x:Static local:Config.Instance}, Converter={CustomBoolToVisibilityConverter}}"
186+
VerticalAlignment="Center"><Hyperlink NavigateUri="https://github.com/codingseb/CSharpRegexTools4Npp/releases"
187+
RequestNavigate="Hyperlink_RequestNavigate">A new version of C# Regex Tools is available</Hyperlink></TextBlock>
184188
<WrapPanel >
185189
<WrapPanel.Resources>
186190
<Style BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" TargetType="Button" />
@@ -194,18 +198,18 @@
194198
<Button x:Name="ShowMatchesButton" Click="ShowMatchesButton_Click" >
195199
<StackPanel Orientation="Horizontal">
196200
<Image Source="{StaticResource MatchPicture}"
197-
Width="16"
198-
Height="16"
199-
ToolTip="Matches"
200-
/>
201+
Width="16"
202+
Height="16"
203+
ToolTip="Matches"
204+
/>
201205
</StackPanel>
202206
</Button>
203207
<Button x:Name="SelectAllButton" Click="SelectAllButton_Click" IsEnabled="{Binding TextSourceOn, Converter={converters:ExpressionEvalConverter Expression='binding == RegexTextSource.CurrentTab || binding == RegexTextSource.CurrentSelection'}}">
204208
<StackPanel Orientation="Horizontal">
205209
<Image Source="{StaticResource SelectPicture}"
206-
Width="16"
207-
Height="16"
208-
ToolTip="Select All Matches"/>
210+
Width="16"
211+
Height="16"
212+
ToolTip="Select All Matches"/>
209213
</StackPanel>
210214
</Button>
211215
<Button x:Name="ExtractMatchesButton" Click="ExtractMatchesButton_Click">
@@ -225,7 +229,7 @@
225229
</StackPanel>
226230
</Button>
227231
</WrapPanel>
228-
</Border>
232+
</DockPanel>
229233
<Grid Margin="0,5,0,0">
230234
<Grid.ColumnDefinitions>
231235
<ColumnDefinition x:Name="FirstColumn" />

RegexDialog/RegExToolDialog.xaml.cs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
44
using Microsoft.Win32;
55
using Newtonsoft.Json;
6+
using Newtonsoft.Json.Linq;
67
using Ookii.Dialogs.Wpf;
78
using System;
89
using System.Collections.Generic;
910
using System.Collections.ObjectModel;
1011
using System.Diagnostics;
1112
using System.IO;
1213
using System.Linq;
14+
using System.Net;
1315
using System.Net.Http;
1416
using System.Reflection;
1517
using System.Text;
@@ -108,6 +110,8 @@ private string InjectInReplaceScript(string replaceScript)
108110
public delegate bool TryOpenDelegate(string fileName, bool onlyIfAlreadyOpen);
109111
public delegate void SetPositionDelegate(int index, int length);
110112

113+
public static Action InitIsOK { get; set; }
114+
111115
/// <summary>
112116
/// Fonction de récupération du texte à utiliser comme input pour l'expression régulière
113117
/// public delegate string GetTextDelegate()
@@ -309,28 +313,7 @@ private void Init()
309313
// Set Treeview Matches Result base contextMenu
310314
MatchResultsTreeView.ContextMenu = MatchResultsTreeView.Resources["cmMatchResultsMenu"] as ContextMenu;
311315

312-
CheckUpdates();
313-
}
314-
315-
private async void CheckUpdates()
316-
{
317-
double hoursFromLastCheck = Math.Abs((DateTime.Now - Config.Instance.LastUpdateCheck).TotalHours);
318-
319-
//if (hoursFromLastCheck > 8)
320-
if (true)
321-
{
322-
try
323-
{
324-
HttpClient client = new HttpClient();
325-
326-
var response = await client.GetAsync("https://github.com/codingseb/CSharpRegexTools4Npp/releases");
327-
328-
string responseText = await response.Content.ReadAsStringAsync();
329-
330-
331-
}
332-
catch { }
333-
}
316+
InitIsOK?.Invoke();
334317
}
335318

336319
private void BuildRegexOptionsCheckBoxs()

RegexDialog/RegexDialog.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
<StartupObject />
4646
</PropertyGroup>
4747
<ItemGroup>
48+
<Reference Include="CodingSeb.Converters, Version=1.0.3.0, Culture=neutral, processorArchitecture=MSIL">
49+
<HintPath>..\packages\CodingSeb.Converters.1.0.3\lib\net45\CodingSeb.Converters.dll</HintPath>
50+
</Reference>
51+
<Reference Include="CodingSeb.ExpressionEvaluator, Version=1.4.16.0, Culture=neutral, processorArchitecture=MSIL">
52+
<HintPath>..\packages\CodingSeb.ExpressionEvaluator.1.4.16\lib\net45\CodingSeb.ExpressionEvaluator.dll</HintPath>
53+
</Reference>
4854
<Reference Include="CodingSeb.Layouts, Version=1.0.1.0, Culture=neutral, processorArchitecture=MSIL">
4955
<HintPath>..\packages\CodingSeb.Layouts.1.0.1\lib\net47\CodingSeb.Layouts.dll</HintPath>
5056
</Reference>

RegexDialog/Utils/Config.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace RegexDialog
99
{
10-
internal class Config : NotifyPropertyChangedBaseClass
10+
public class Config : NotifyPropertyChangedBaseClass
1111
{
1212
#region Json singleton
1313

@@ -66,6 +66,12 @@ private void Init()
6666
{
6767
}
6868

69+
[JsonIgnore]
70+
public bool UpdateAvailable { get; set; }
71+
72+
[JsonIgnore]
73+
public string UpdateURL { get; set; }
74+
6975
public string RegexEditorText { get; set; }
7076
public string ReplaceEditorText { get; set; }
7177
public string CSharpTextSourceEditorText { get; set; }

RegexDialog/packages.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="AvalonEdit" version="6.0.1" targetFramework="net46" />
4+
<package id="CodingSeb.Converters" version="1.0.3" targetFramework="net47" />
5+
<package id="CodingSeb.ExpressionEvaluator" version="1.4.16" targetFramework="net47" />
46
<package id="CodingSeb.Layouts" version="1.0.1" targetFramework="net47" />
57
<package id="Costura.Fody" version="4.1.0" targetFramework="net47" />
68
<package id="CS-Script.bin" version="3.30.5.1" targetFramework="net46" />

0 commit comments

Comments
 (0)