Add manually check updates option

1. manually check updates
2. refactoring get http request to use async
3. remove some generic exception catch
4. remove unused code
This commit is contained in:
bao-qian
2016-05-09 02:32:47 +01:00
parent 373da78b7b
commit 6ac33b0568
13 changed files with 209 additions and 147 deletions

View File

@@ -64,7 +64,15 @@ namespace Wox
}
catch (Exception exception)
{
Log.Error(exception);
const string info = "Update.exe not found, not a Squirrel-installed app?";
if (exception.Message == info)
{
Log.Warn("info");
}
else
{
throw;
}
}
}

View File

@@ -78,6 +78,9 @@
<system:String x:Key="about">About</system:String>
<system:String x:Key="website">Website</system:String>
<system:String x:Key="version">Version</system:String>
<system:String x:Key="checkUpdates">Check Updates</system:String>
<system:String x:Key="newVersionTips">New Version {0} avaiable, please restart</system:String>
<system:String x:Key="releaseNotes">Release Notes:</system:String>
<system:String x:Key="about_activate_times">You have activated Wox {0} times</system:String>
<!--Action Keyword Setting Dialog-->

View File

@@ -340,27 +340,42 @@
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Text="{DynamicResource website}" />
<TextBlock Grid.Column="1" Grid.Row="0" HorizontalAlignment="Left" Cursor="Hand"
MouseUp="tbWebsite_MouseUp" x:Name="tbWebsite" Foreground="Blue"
Text="http://www.getwox.com" />
<TextBlock Grid.Column="0" Grid.Row="1" Text="{DynamicResource version}" />
<StackPanel Grid.Column="1" Grid.Row="1" Orientation="Horizontal">
<TextBlock HorizontalAlignment="Left" x:Name="tbVersion" Text="1.0.0" />
</StackPanel>
<TextBlock x:Name="tbActivatedTimes" Grid.Row="2" Grid.ColumnSpan="2"
<TextBlock x:Name="ActivatedTimes" Grid.Row="0" Grid.ColumnSpan="3"
Text="{DynamicResource about_activate_times}" />
<TextBlock Grid.Column="0" Grid.Row="1" Text="{DynamicResource website}" />
<TextBlock Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" >
<Hyperlink NavigateUri="https://github.com/Wox-launcher/Wox" RequestNavigate="OnRequestNavigate">
https://github.com/Wox-launcher/Wox
</Hyperlink>
</TextBlock>
<TextBlock Grid.Column="0" Grid.Row="2" Text="{DynamicResource version}" />
<TextBlock Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" x:Name="tbVersion" Text="1.0.0" />
<TextBlock Grid.Column="0" Grid.Row="3" Text="{DynamicResource releaseNotes}"></TextBlock>
<TextBlock Grid.Column="1" Grid.Row="3" HorizontalAlignment="Left" >
<Hyperlink NavigateUri="https://github.com/Wox-launcher/Wox/releases/latest" RequestNavigate="OnRequestNavigate">
https://github.com/Wox-launcher/Wox/releases/latest
</Hyperlink>
</TextBlock>
<Button Grid.Column="0" Grid.Row="4" Content="{DynamicResource checkUpdates}" HorizontalAlignment="Left" Margin="10 10 10 10"
Click="OnCheckUpdates"/>
<TextBlock Grid.Column="1" Grid.Row="4" Name="NewVersionTips" HorizontalAlignment="Left" Text="{DynamicResource newVersionTips}" Visibility="Hidden"/>
</Grid>
</TabItem>
</TabControl>

View File

@@ -4,6 +4,8 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
@@ -11,16 +13,21 @@ using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Microsoft.Win32;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NHotkey;
using NHotkey.Wpf;
using Squirrel;
using Wox.Core.Plugin;
using Wox.Core.Resource;
using Wox.Core.Updater;
using Wox.Core.UserSettings;
using Wox.Helper;
using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.Http;
using Wox.Infrastructure.Image;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
using Wox.ViewModel;
using Application = System.Windows.Forms.Application;
@@ -56,7 +63,7 @@ namespace Wox
_settings.ProxyEnabled = ToggleProxy.IsChecked ?? false;
}
private void Setting_Loaded(object sender, RoutedEventArgs ev)
private async void Setting_Loaded(object sender, RoutedEventArgs ev)
{
#region General
cbHideWhenDeactive.Checked += (o, e) =>
@@ -136,11 +143,10 @@ namespace Wox
#endregion
#region About
tbVersion.Text = UpdaterManager.Instance.CurrentVersion.ToString();
string activateTimes = string.Format(InternationalizationManager.Instance.GetTranslation("about_activate_times"),
_settings.ActivateTimes);
tbActivatedTimes.Text = activateTimes;
string activateTimes = string.Format(
InternationalizationManager.Instance.GetTranslation("about_activate_times"), _settings.ActivateTimes);
ActivatedTimes.Text = activateTimes;
tbVersion.Text = Infrastructure.Wox.Version;
#endregion
@@ -806,7 +812,7 @@ namespace Wox
private void tbWebsite_MouseUp(object sender, MouseButtonEventArgs e)
{
Process.Start("http://www.getwox.com");
Process.Start(Infrastructure.Wox.Github);
}
#endregion
@@ -820,5 +826,76 @@ namespace Wox
}
}
private async void OnCheckUpdates(object sender, RoutedEventArgs e)
{
try
{
var version = await NewVersion();
if (!string.IsNullOrEmpty(version))
{
var newVersion = NumericVersion(version);
var oldVersion = NumericVersion(Infrastructure.Wox.Version);
if (newVersion > oldVersion)
{
NewVersionTips.Text = string.Format(NewVersionTips.Text, version);
NewVersionTips.Visibility = Visibility.Visible;
using (var updater = await UpdateManager.GitHubUpdateManager(Infrastructure.Wox.Github, prerelease: true))
{
// todo 5/9 the return value of UpdateApp() is NULL, fucking useless!
await updater.UpdateApp();
}
}
}
}
catch (Exception ex)
{
Log.Error(ex);
}
}
private async Task<string> NewVersion()
{
const string githubAPI = @"https://api.github.com/repos/wox-launcher/wox/releases/latest";
var response = await HttpRequest.Get(githubAPI, HttpProxy.Instance);
if (!string.IsNullOrEmpty(response))
{
JContainer json;
try
{
json = (JContainer) JsonConvert.DeserializeObject(response);
}
catch (JsonSerializationException e)
{
Log.Error(e);
return string.Empty;
}
var version = json?["tag_name"]?.ToString();
if (!string.IsNullOrEmpty(version))
{
return version;
}
else
{
return string.Empty;
}
}
else
{
return string.Empty;
}
}
private
int NumericVersion(string version)
{
var newVersion = version.Replace("v", ".").Replace(".", "").Replace("*", "");
return int.Parse(newVersion);
}
private void OnRequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
}
}

View File

@@ -215,10 +215,6 @@
<SubType>Designer</SubType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Page Include="WoxUpdate.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Resource Include="Images\update.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
@@ -432,8 +428,9 @@
cd "$(TargetDir)Plugins" &amp; del /s /q Pinyin4Net.dll
cd "$(TargetDir)" &amp; del /s /q *.xml
cd "$(TargetDir)Installer" &amp; del /s /q *
)
</PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
<PropertyGroup>
<PreBuildEvent>
@@ -446,7 +443,7 @@
</Target>
<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="myAssemblyInfo"/>
<Output TaskParameter="Assemblies" ItemName="myAssemblyInfo" />
</GetAssemblyIdentity>
<Exec Command="nuget pack $(SolutionDir)Deploy\wox.nuspec -Version %(myAssemblyInfo.Version) -Properties Configuration=Release -OutputDirectory $(TargetDir) -BasePath $(TargetDir)" />
<Exec Command="squirrel --releasify $(TargetDir)Wox.%(myAssemblyInfo.Version).nupkg --releaseDir $(TargetDir)Installer --no-msi" />