From 49b85d150c2fb0d084667c7d6be37a53ea722f53 Mon Sep 17 00:00:00 2001 From: AT <14300910+theClueless@users.noreply.github.com> Date: Tue, 7 Jan 2020 02:34:46 +0200 Subject: [PATCH] initial work, added github to setting, change update manage from static created log folder prop for log class --- Wox.Core/Updater.cs | 21 +++++---- Wox.Infrastructure/Http/Http.cs | 10 +++++ Wox.Infrastructure/Image/ImageLoader.cs | 2 +- Wox.Infrastructure/Wox.cs | 1 - Wox/App.config | 12 +++++ Wox/App.xaml.cs | 7 +-- Wox/Properties/Settings.Designer.cs | 11 ++++- Wox/Properties/Settings.settings | 12 ++--- Wox/ReportWindow.xaml.cs | 2 +- Wox/SettingWindow.xaml.cs | 45 ++----------------- Wox/Settings.cs | 28 ++++++++++++ Wox/ViewModel/SettingWindowViewModel.cs | 59 +++++++++++++++++++++++-- Wox/Wox.csproj | 1 + 13 files changed, 146 insertions(+), 65 deletions(-) create mode 100644 Wox/Settings.cs diff --git a/Wox.Core/Updater.cs b/Wox.Core/Updater.cs index d2461baf4a..971995f5ee 100644 --- a/Wox.Core/Updater.cs +++ b/Wox.Core/Updater.cs @@ -16,18 +16,23 @@ using Wox.Infrastructure.Logger; namespace Wox.Core { - public static class Updater + public class Updater { - private static readonly Internationalization Translater = InternationalizationManager.Instance; + public string GitHubRepository { get; } - public static async Task UpdateApp() + public Updater(string gitHubRepository) + { + GitHubRepository = gitHubRepository; + } + + public async Task UpdateApp() { UpdateManager m; UpdateInfo u; try { - m = await GitHubUpdateManager(Constant.Repository); + m = await GitHubUpdateManager(GitHubRepository); } catch (Exception e) when (e is HttpRequestException || e is WebException || e is SocketException) { @@ -66,8 +71,8 @@ namespace Wox.Core await m.ApplyReleases(u); await m.CreateUninstallerRegistryEntry(); - var newVersionTips = Translater.GetTranslation("newVersionTips"); - newVersionTips = string.Format(newVersionTips, fr.Version); + var newVersionTips = this.NewVersinoTips(fr.Version.ToString()); + MessageBox.Show(newVersionTips); Log.Info($"|Updater.UpdateApp|Update success:{newVersionTips}"); } @@ -90,7 +95,7 @@ namespace Wox.Core } /// https://github.com/Squirrel/Squirrel.Windows/blob/master/src/Squirrel/UpdateManager.Factory.cs - private static async Task GitHubUpdateManager(string repository) + private async Task GitHubUpdateManager(string repository) { var uri = new Uri(repository); var api = $"https://api.github.com/repos{uri.AbsolutePath}/releases"; @@ -109,7 +114,7 @@ namespace Wox.Core return manager; } - public static string NewVersinoTips(string version) + public string NewVersinoTips(string version) { var translater = InternationalizationManager.Instance; var tips = string.Format(translater.GetTranslation("newVersionTips"), version); diff --git a/Wox.Infrastructure/Http/Http.cs b/Wox.Infrastructure/Http/Http.cs index d79f3481fc..0c8597a5d4 100644 --- a/Wox.Infrastructure/Http/Http.cs +++ b/Wox.Infrastructure/Http/Http.cs @@ -13,6 +13,16 @@ namespace Wox.Infrastructure.Http { private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko"; + static Http() + { + // need to be added so it would work on a win10 machine + ServicePointManager.Expect100Continue = true; + ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls + | SecurityProtocolType.Tls11 + | SecurityProtocolType.Tls12 + | SecurityProtocolType.Ssl3; + } + public static HttpProxy Proxy { private get; set; } public static IWebProxy WebProxy() { diff --git a/Wox.Infrastructure/Image/ImageLoader.cs b/Wox.Infrastructure/Image/ImageLoader.cs index 528900ce7c..d1a0a74faf 100644 --- a/Wox.Infrastructure/Image/ImageLoader.cs +++ b/Wox.Infrastructure/Image/ImageLoader.cs @@ -164,7 +164,7 @@ namespace Wox.Infrastructure.Image } catch (System.Exception e) { - Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path}", e); + // Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path}", e); type = ImageType.Error; image = ImageCache[Constant.ErrorIcon]; ImageCache[path] = image; diff --git a/Wox.Infrastructure/Wox.cs b/Wox.Infrastructure/Wox.cs index fbab671edc..396ee0bb1a 100644 --- a/Wox.Infrastructure/Wox.cs +++ b/Wox.Infrastructure/Wox.cs @@ -29,7 +29,6 @@ namespace Wox.Infrastructure public static readonly string DataDirectory = DetermineDataDirectory(); public static readonly string PluginsDirectory = Path.Combine(DataDirectory, Plugins); public static readonly string PreinstalledDirectory = Path.Combine(ProgramDirectory, Plugins); - public const string Repository = "https://github.com/Wox-launcher/Wox"; public const string Issue = "https://github.com/Wox-launcher/Wox/issues/new"; public static readonly string Version = FileVersionInfo.GetVersionInfo(Assembly.Location.NonNull()).ProductVersion; diff --git a/Wox/App.config b/Wox/App.config index 0fa0fe8bd8..aef034f761 100644 --- a/Wox/App.config +++ b/Wox/App.config @@ -1,7 +1,19 @@  + + +
+ + + + + + https://github.com/Wox-launcher/Wox + + + \ No newline at end of file diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index 9436df4758..6d6716a866 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -25,6 +25,7 @@ namespace Wox private Settings _settings; private MainViewModel _mainVM; private SettingWindowViewModel _settingsVM; + private readonly Updater _updater = new Updater(Wox.Properties.Settings.Default.GithubRepo); [STAThread] public static void Main() @@ -50,7 +51,7 @@ namespace Wox ImageLoader.Initialize(); - _settingsVM = new SettingWindowViewModel(); + _settingsVM = new SettingWindowViewModel(_updater); _settings = _settingsVM.Settings; Alphabet.Initialize(_settings); @@ -111,12 +112,12 @@ namespace Wox var timer = new Timer(1000 * 60 * 60 * 5); timer.Elapsed += async (s, e) => { - await Updater.UpdateApp(); + await _updater.UpdateApp(); }; timer.Start(); // check updates on startup - await Updater.UpdateApp(); + await _updater.UpdateApp(); } }); } diff --git a/Wox/Properties/Settings.Designer.cs b/Wox/Properties/Settings.Designer.cs index 7a4226349e..a61339f5e7 100644 --- a/Wox/Properties/Settings.Designer.cs +++ b/Wox/Properties/Settings.Designer.cs @@ -12,7 +12,7 @@ namespace Wox.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.3.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -22,5 +22,14 @@ namespace Wox.Properties { return defaultInstance; } } + + [global::System.Configuration.ApplicationScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("https://github.com/Wox-launcher/Wox")] + public string GithubRepo { + get { + return ((string)(this["GithubRepo"])); + } + } } } diff --git a/Wox/Properties/Settings.settings b/Wox/Properties/Settings.settings index a585a6308c..1fc52390be 100644 --- a/Wox/Properties/Settings.settings +++ b/Wox/Properties/Settings.settings @@ -1,7 +1,9 @@  - - - - - + + + + + https://github.com/Wox-launcher/Wox + + \ No newline at end of file diff --git a/Wox/ReportWindow.xaml.cs b/Wox/ReportWindow.xaml.cs index 38509a728f..ac7e986fda 100644 --- a/Wox/ReportWindow.xaml.cs +++ b/Wox/ReportWindow.xaml.cs @@ -23,7 +23,7 @@ namespace Wox private void SetException(Exception exception) { - string path = Path.Combine(Constant.DataDirectory, Log.DirectoryName, Constant.Version); + string path = Log.CurrentLogDirectory; var directory = new DirectoryInfo(path); var log = directory.GetFiles().OrderByDescending(f => f.LastWriteTime).First(); diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index c041d7ddd3..67d67351eb 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -260,53 +260,16 @@ namespace Wox #region Proxy private void OnTestProxyClick(object sender, RoutedEventArgs e) - { - if (string.IsNullOrEmpty(_settings.Proxy.Server)) - { - MessageBox.Show(InternationalizationManager.Instance.GetTranslation("serverCantBeEmpty")); - return; - } - if (_settings.Proxy.Port <= 0) - { - MessageBox.Show(InternationalizationManager.Instance.GetTranslation("portCantBeEmpty")); - return; - } - - HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Infrastructure.Constant.Repository); - if (string.IsNullOrEmpty(_settings.Proxy.UserName) || string.IsNullOrEmpty(_settings.Proxy.Password)) - { - request.Proxy = new WebProxy(_settings.Proxy.Server, _settings.Proxy.Port); - } - else - { - request.Proxy = new WebProxy(_settings.Proxy.Server, _settings.Proxy.Port) - { - Credentials = new NetworkCredential(_settings.Proxy.UserName, _settings.Proxy.Password) - }; - } - try - { - var response = (HttpWebResponse)request.GetResponse(); - if (response.StatusCode == HttpStatusCode.OK) - { - MessageBox.Show(InternationalizationManager.Instance.GetTranslation("proxyIsCorrect")); - } - else - { - MessageBox.Show(InternationalizationManager.Instance.GetTranslation("proxyConnectFailed")); - } - } - catch - { - MessageBox.Show(InternationalizationManager.Instance.GetTranslation("proxyConnectFailed")); - } + { // TODO: change to command + var msg = _viewModel.TestProxy(); + MessageBox.Show(msg); // TODO: add message box service } #endregion private async void OnCheckUpdates(object sender, RoutedEventArgs e) { - await Updater.UpdateApp(); + _viewModel.UpdateApp(); // TODO: change to command } private void OnRequestNavigate(object sender, RequestNavigateEventArgs e) diff --git a/Wox/Settings.cs b/Wox/Settings.cs new file mode 100644 index 0000000000..b45d4a5a17 --- /dev/null +++ b/Wox/Settings.cs @@ -0,0 +1,28 @@ +namespace Wox.Properties { + + + // This class allows you to handle specific events on the settings class: + // The SettingChanging event is raised before a setting's value is changed. + // The PropertyChanged event is raised after a setting's value is changed. + // The SettingsLoaded event is raised after the setting values are loaded. + // The SettingsSaving event is raised before the setting values are saved. + internal sealed partial class Settings { + + public Settings() { + // // To add event handlers for saving and changing settings, uncomment the lines below: + // + // this.SettingChanging += this.SettingChangingEventHandler; + // + // this.SettingsSaving += this.SettingsSavingEventHandler; + // + } + + private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e) { + // Add code to handle the SettingChangingEvent event here. + } + + private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) { + // Add code to handle the SettingsSaving event here. + } + } +} diff --git a/Wox/ViewModel/SettingWindowViewModel.cs b/Wox/ViewModel/SettingWindowViewModel.cs index 67b8d7af06..5c418a8bdf 100644 --- a/Wox/ViewModel/SettingWindowViewModel.cs +++ b/Wox/ViewModel/SettingWindowViewModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Media; @@ -20,10 +21,12 @@ namespace Wox.ViewModel { public class SettingWindowViewModel : BaseModel { + private readonly Updater _updater; private readonly WoxJsonStorage _storage; - public SettingWindowViewModel() + public SettingWindowViewModel(Updater updater) { + _updater = updater; _storage = new WoxJsonStorage(); Settings = _storage.Load(); Settings.PropertyChanged += (s, e) => @@ -39,6 +42,10 @@ namespace Wox.ViewModel public Settings Settings { get; set; } + public async void UpdateApp() + { + await _updater.UpdateApp(); + } public void Save() { @@ -88,6 +95,50 @@ namespace Wox.ViewModel public List Languages => _translater.LoadAvailableLanguages(); public IEnumerable MaxResultsRange => Enumerable.Range(2, 16); + public string TestProxy() + { + var proxyServer = Settings.Proxy.Server; + var proxyUserName = Settings.Proxy.UserName; + if (string.IsNullOrEmpty(proxyServer)) + { + return InternationalizationManager.Instance.GetTranslation("serverCantBeEmpty"); + } + if (Settings.Proxy.Port <= 0) + { + return InternationalizationManager.Instance.GetTranslation("portCantBeEmpty"); + } + + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_updater.GitHubRepository); + + if (string.IsNullOrEmpty(proxyUserName) || string.IsNullOrEmpty(Settings.Proxy.Password)) + { + request.Proxy = new WebProxy(proxyServer, Settings.Proxy.Port); + } + else + { + request.Proxy = new WebProxy(proxyServer, Settings.Proxy.Port) + { + Credentials = new NetworkCredential(proxyUserName, Settings.Proxy.Password) + }; + } + try + { + var response = (HttpWebResponse)request.GetResponse(); + if (response.StatusCode == HttpStatusCode.OK) + { + return InternationalizationManager.Instance.GetTranslation("proxyIsCorrect"); + } + else + { + return InternationalizationManager.Instance.GetTranslation("proxyConnectFailed"); + } + } + catch + { + return InternationalizationManager.Instance.GetTranslation("proxyConnectFailed"); + } + } + #endregion #region plugin @@ -220,7 +271,7 @@ namespace Wox.ViewModel }, new Result { - Title = $"Open Source: {Constant.Repository}", + Title = $"Open Source: {_updater.GitHubRepository}", SubTitle = "Please star it!" } }; @@ -330,8 +381,8 @@ namespace Wox.ViewModel #region about - public static string Github => Constant.Repository; - public static string ReleaseNotes => @"https://github.com/Wox-launcher/Wox/releases/latest"; + public string Github => _updater.GitHubRepository; + public string ReleaseNotes => _updater.GitHubRepository + @"/releases/latest"; public static string Version => Constant.Version; public string ActivatedTimes => string.Format(_translater.GetTranslation("about_activate_times"), Settings.ActivateTimes); #endregion diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index 4a4dc62bfc..77b7e16d00 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -165,6 +165,7 @@ ResultListBox.xaml +