From 3667243fc7c670a51dc8a1b6668082ea7b3e2804 Mon Sep 17 00:00:00 2001 From: Vincent Maas Date: Mon, 16 Aug 2021 13:59:01 +0200 Subject: [PATCH] fixed some issues, added check for updates --- ColorControl/App.config | 2 +- ColorControl/ColorControl.csproj | 5 +- ColorControl/Config.cs | 3 + ColorControl/LgDevice.cs | 6 +- ColorControl/LgService.cs | 2 +- ColorControl/MainForm.Designer.cs | 56 +++++++----- ColorControl/MainForm.cs | 111 +++++++++++++++++++----- ColorControl/MessageForms.cs | 4 +- ColorControl/Properties/AssemblyInfo.cs | 4 +- ColorControl/Utils.cs | 43 ++++++++- ColorControl/lgtv/LgTvConnection.cs | 12 ++- ColorControl/packages.config | 1 + 12 files changed, 193 insertions(+), 56 deletions(-) diff --git a/ColorControl/App.config b/ColorControl/App.config index 6dfbd73..1985185 100644 --- a/ColorControl/App.config +++ b/ColorControl/App.config @@ -7,7 +7,7 @@ - + diff --git a/ColorControl/ColorControl.csproj b/ColorControl/ColorControl.csproj index c4c19ba..49ba607 100644 --- a/ColorControl/ColorControl.csproj +++ b/ColorControl/ColorControl.csproj @@ -27,7 +27,7 @@ ColorControl Maassoft 0 - 4.0.0.0 + 4.1.0.0 false true true @@ -141,6 +141,9 @@ ..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll + + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.7\lib\net45\System.Net.Http.Formatting.dll + ..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll diff --git a/ColorControl/Config.cs b/ColorControl/Config.cs index 2372680..d993a69 100644 --- a/ColorControl/Config.cs +++ b/ColorControl/Config.cs @@ -8,6 +8,8 @@ public bool MinimizeToTray { get; set; } + public bool CheckForUpdates { get; set; } + public int DisplaySettingsDelay { get; set; } public string ScreenSaverShortcut { get; set; } @@ -26,6 +28,7 @@ FormWidth = 900; FormHeight = 600; MinimizeToTray = true; + CheckForUpdates = true; } } } diff --git a/ColorControl/LgDevice.cs b/ColorControl/LgDevice.cs index fbb92f6..eaeac89 100644 --- a/ColorControl/LgDevice.cs +++ b/ColorControl/LgDevice.cs @@ -223,13 +223,15 @@ namespace ColorControl { var settings = payload.settings; + Logger.Debug($"PictureSettingsChanged: {JsonConvert.SerializeObject(settings)}"); + if (settings.backlight != null) { - PictureSettings.Backlight = int.Parse(settings.backlight.Value); + PictureSettings.Backlight = Utils.ParseInt(settings.backlight.Value, PictureSettings.Backlight); } if (settings.contrast != null) { - PictureSettings.Contrast = int.Parse(settings.contrast.Value); + PictureSettings.Contrast = Utils.ParseInt(settings.contrast.Value, PictureSettings.Contrast); } } diff --git a/ColorControl/LgService.cs b/ColorControl/LgService.cs index ea0936d..25e7d43 100644 --- a/ColorControl/LgService.cs +++ b/ColorControl/LgService.cs @@ -277,7 +277,7 @@ namespace ColorControl private void SaveConfig() { - Config.PowerOnAfterStartup = Devices.Any(d => d.PowerOnAfterStartup); + Config.PowerOnAfterStartup = Devices?.Any(d => d.PowerOnAfterStartup) ?? false; var json = JsonConvert.SerializeObject(Config); File.WriteAllText(_configFilename, json); diff --git a/ColorControl/MainForm.Designer.cs b/ColorControl/MainForm.Designer.cs index 4ad9b1e..2f1bbd9 100644 --- a/ColorControl/MainForm.Designer.cs +++ b/ColorControl/MainForm.Designer.cs @@ -104,6 +104,8 @@ this.lvAmdPresets = new System.Windows.Forms.ListView(); this.tabLG = new System.Windows.Forms.TabPage(); this.scLgController = new System.Windows.Forms.SplitContainer(); + this.btnLgPresetEditTriggerConditions = new System.Windows.Forms.Button(); + this.edtLgPresetTriggerConditions = new System.Windows.Forms.TextBox(); this.lblLgPresetExcludedProcesses = new System.Windows.Forms.Label(); this.edtLgPresetExcludedProcesses = new System.Windows.Forms.TextBox(); this.lblLgPresetIncludedProcesses = new System.Windows.Forms.Label(); @@ -191,8 +193,7 @@ this.lbPlugins = new System.Windows.Forms.ListBox(); this.label7 = new System.Windows.Forms.Label(); this.lblInfo = new System.Windows.Forms.Label(); - this.edtLgPresetTriggerConditions = new System.Windows.Forms.TextBox(); - this.btnLgPresetEditTriggerConditions = new System.Windows.Forms.Button(); + this.chkCheckForUpdates = new System.Windows.Forms.CheckBox(); this.tcMain.SuspendLayout(); this.tabNVIDIA.SuspendLayout(); this.mnuNvPresets.SuspendLayout(); @@ -995,6 +996,27 @@ this.scLgController.SplitterDistance = 767; this.scLgController.TabIndex = 43; // + // btnLgPresetEditTriggerConditions + // + this.btnLgPresetEditTriggerConditions.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.btnLgPresetEditTriggerConditions.Location = new System.Drawing.Point(689, 317); + this.btnLgPresetEditTriggerConditions.Name = "btnLgPresetEditTriggerConditions"; + this.btnLgPresetEditTriggerConditions.Size = new System.Drawing.Size(75, 23); + this.btnLgPresetEditTriggerConditions.TabIndex = 54; + this.btnLgPresetEditTriggerConditions.Text = "Edit..."; + this.btnLgPresetEditTriggerConditions.UseVisualStyleBackColor = true; + this.btnLgPresetEditTriggerConditions.Click += new System.EventHandler(this.btnLgPresetEditTriggerConditions_Click); + // + // edtLgPresetTriggerConditions + // + this.edtLgPresetTriggerConditions.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.edtLgPresetTriggerConditions.Enabled = false; + this.edtLgPresetTriggerConditions.Location = new System.Drawing.Point(515, 319); + this.edtLgPresetTriggerConditions.Name = "edtLgPresetTriggerConditions"; + this.edtLgPresetTriggerConditions.ReadOnly = true; + this.edtLgPresetTriggerConditions.Size = new System.Drawing.Size(167, 20); + this.edtLgPresetTriggerConditions.TabIndex = 53; + // // lblLgPresetExcludedProcesses // this.lblLgPresetExcludedProcesses.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); @@ -1755,6 +1777,7 @@ // // grpGeneralOptions // + this.grpGeneralOptions.Controls.Add(this.chkCheckForUpdates); this.grpGeneralOptions.Controls.Add(this.chkMinimizeToSystemTray); this.grpGeneralOptions.Controls.Add(this.chkMinimizeOnClose); this.grpGeneralOptions.Controls.Add(this.chkStartMinimized); @@ -1931,26 +1954,16 @@ this.lblInfo.TabIndex = 1; this.lblInfo.Text = "Info"; // - // edtLgPresetTriggerConditions + // chkCheckForUpdates // - this.edtLgPresetTriggerConditions.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.edtLgPresetTriggerConditions.Enabled = false; - this.edtLgPresetTriggerConditions.Location = new System.Drawing.Point(515, 319); - this.edtLgPresetTriggerConditions.Name = "edtLgPresetTriggerConditions"; - this.edtLgPresetTriggerConditions.ReadOnly = true; - this.edtLgPresetTriggerConditions.Size = new System.Drawing.Size(167, 20); - this.edtLgPresetTriggerConditions.TabIndex = 53; - // - // btnLgPresetEditTriggerConditions - // - this.btnLgPresetEditTriggerConditions.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.btnLgPresetEditTriggerConditions.Location = new System.Drawing.Point(689, 317); - this.btnLgPresetEditTriggerConditions.Name = "btnLgPresetEditTriggerConditions"; - this.btnLgPresetEditTriggerConditions.Size = new System.Drawing.Size(75, 23); - this.btnLgPresetEditTriggerConditions.TabIndex = 54; - this.btnLgPresetEditTriggerConditions.Text = "Edit..."; - this.btnLgPresetEditTriggerConditions.UseVisualStyleBackColor = true; - this.btnLgPresetEditTriggerConditions.Click += new System.EventHandler(this.btnLgPresetEditTriggerConditions_Click); + this.chkCheckForUpdates.AutoSize = true; + this.chkCheckForUpdates.Location = new System.Drawing.Point(204, 42); + this.chkCheckForUpdates.Name = "chkCheckForUpdates"; + this.chkCheckForUpdates.Size = new System.Drawing.Size(177, 17); + this.chkCheckForUpdates.TabIndex = 6; + this.chkCheckForUpdates.Text = "Automatically check for updates"; + this.chkCheckForUpdates.UseVisualStyleBackColor = true; + this.chkCheckForUpdates.CheckedChanged += new System.EventHandler(this.chkCheckForUpdates_CheckedChanged); // // MainForm // @@ -2172,6 +2185,7 @@ private System.Windows.Forms.TextBox edtLgPresetIncludedProcesses; private System.Windows.Forms.Button btnLgPresetEditTriggerConditions; private System.Windows.Forms.TextBox edtLgPresetTriggerConditions; + private System.Windows.Forms.CheckBox chkCheckForUpdates; } } diff --git a/ColorControl/MainForm.cs b/ColorControl/MainForm.cs index 5227e04..8dac6a5 100644 --- a/ColorControl/MainForm.cs +++ b/ColorControl/MainForm.cs @@ -53,6 +53,9 @@ namespace ColorControl private AmdService _amdService; private bool _skipResize; + private FileVersionInfo _currentVersionInfo; + private bool _checkedForUpdates = false; + private string _updateHtmlUrl; public MainForm(AppContext appContext) { @@ -86,6 +89,7 @@ namespace ColorControl }; _trayIcon.MouseDoubleClick += trayIcon_MouseDoubleClick; _trayIcon.ContextMenu.Popup += trayIconContextMenu_Popup; + _trayIcon.BalloonTipClicked += trayIconBalloonTip_Clicked; chkStartAfterLogin.Checked = Utils.TaskExists(TS_TASKNAME, true); @@ -116,6 +120,7 @@ namespace ColorControl { try { + //throw new Exception("bla"); _nvService = new NvService(_dataDir); FillNvPresets(); @@ -194,6 +199,8 @@ namespace ColorControl private void AfterLgServiceRefreshDevices() { + FillLgDevices(); + if (StartUpParams.ExecuteLgPreset) { var _ = _lgService.ApplyPreset(StartUpParams.LgPresetName); @@ -221,7 +228,7 @@ namespace ColorControl private void InitInfo() { - var fileVersionInfo = FileVersionInfo.GetVersionInfo(Path.GetFileName(Application.ExecutablePath)); + _currentVersionInfo = FileVersionInfo.GetVersionInfo(Path.GetFileName(Application.ExecutablePath)); if (ApplicationDeployment.IsNetworkDeployed) { @@ -232,7 +239,7 @@ namespace ColorControl Text = Application.ProductName + " " + Application.ProductVersion; } - lblInfo.Text = Text + " - " + fileVersionInfo.LegalCopyright; + lblInfo.Text = Text + " - " + _currentVersionInfo.LegalCopyright; lbPlugins.Items.Add("lgtv.net by gr4b4z"); lbPlugins.Items.Add("Newtonsoft.Json by James Newton-King"); @@ -261,6 +268,14 @@ namespace ColorControl WindowState = FormWindowState.Normal; } + private void trayIconBalloonTip_Clicked(object sender, EventArgs e) + { + if (_updateHtmlUrl != null) + { + Utils.StartProcess(_updateHtmlUrl); + } + } + private void FillNvPresets() { Utils.InitListView(lvNvPresets, NvPreset.GetColumnNames()); @@ -611,6 +626,7 @@ namespace ColorControl chkStartMinimized.Checked = _config.StartMinimized; chkMinimizeOnClose.Checked = _config.MinimizeOnClose; chkMinimizeToSystemTray.Checked = _config.MinimizeToTray; + chkCheckForUpdates.Checked = _config.CheckForUpdates; edtDelayDisplaySettings.Value = _config.DisplaySettingsDelay; edtBlankScreenSaverShortcut.Text = _config.ScreenSaverShortcut; @@ -687,6 +703,9 @@ namespace ColorControl lblLgError.Text = "Error while initializing the LG-controller. You either don't have a LG TV or it is disabled."; lblLgError.Visible = true; } + + InitSelectedTab(); + CheckForUpdates(); } protected override void SetVisibleCore(bool value) @@ -1271,6 +1290,11 @@ namespace ColorControl } private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) + { + InitSelectedTab(); + } + + private void InitSelectedTab() { if (tcMain.SelectedTab == tabLG) { @@ -1292,29 +1316,16 @@ namespace ColorControl private void InitLgTab() { - if (cbxLgDevices.Items.Count == 0 && _lgService != null) + if (scLgController.Panel2.Controls.Count == 0) { - var devices = _lgService.Devices; - if (devices == null || !devices.Any()) - { - RefreshLgDevices(); - } - else - { - FillLgDevices(); - } - - if (scLgController.Panel2.Controls.Count == 0) - { - var rcPanel = new RemoteControlPanel(_lgService, _lgService.GetRemoteControlButtons()); - rcPanel.Parent = scLgController.Panel2; - rcPanel.Dock = DockStyle.Fill; - } - chkLgRemoteControlShow.Checked = _lgService.Config.ShowRemoteControl; - scLgController.Panel2Collapsed = !_lgService.Config.ShowRemoteControl; - - Utils.BuildComboBox(cbxLgPresetTrigger, PresetTriggerType.Resume, PresetTriggerType.Screensaver, PresetTriggerType.Shutdown, PresetTriggerType.Standby, PresetTriggerType.Startup); + var rcPanel = new RemoteControlPanel(_lgService, _lgService.GetRemoteControlButtons()); + rcPanel.Parent = scLgController.Panel2; + rcPanel.Dock = DockStyle.Fill; } + chkLgRemoteControlShow.Checked = _lgService.Config.ShowRemoteControl; + scLgController.Panel2Collapsed = !_lgService.Config.ShowRemoteControl; + + Utils.BuildComboBox(cbxLgPresetTrigger, PresetTriggerType.Resume, PresetTriggerType.Screensaver, PresetTriggerType.Shutdown, PresetTriggerType.Standby, PresetTriggerType.Startup); } private void InitOptionsTab() @@ -2039,6 +2050,52 @@ Do you want to continue?"; { ApplyNvPresetOnStartup(); ApplyAmdPresetOnStartup(); + if (_trayIcon.Visible) + { + CheckForUpdates(); + } + } + + private void CheckForUpdates() + { + if (!_config.CheckForUpdates || _checkedForUpdates || Debugger.IsAttached) + { + return; + } + + _checkedForUpdates = true; + + var _ = Utils.GetRestJsonAsync("https://api.github.com/repos/maassoft/colorcontrol/releases/latest", InitHandleCheckForUpdates); + } + + private void InitHandleCheckForUpdates(dynamic latest) + { + BeginInvoke(new Action(HandleCheckForUpdates), new[] { latest }); + } + + private void HandleCheckForUpdates(dynamic latest) + { + if (latest?.tag_name == null) + { + return; + } + + var currentVersion = Application.ProductVersion; + + var newVersion = latest.tag_name.Value.Substring(1); + if (newVersion.CompareTo(currentVersion) > 0) + { + _updateHtmlUrl = latest.html_url.Value; + + if (_trayIcon.Visible) + { + _trayIcon.ShowBalloonTip(5000, "Update available", $"Version {newVersion} is available. Click to open the GitHub page", ToolTipIcon.Info); + } + else + { + MessageForms.InfoOk($"New version {newVersion} is available. Click on the Help-button to open the GitHub page.", "Update available", "https://github.com/Maassoft/ColorControl/releases/tag/v4.0.0.0"); + } + } } private void ApplyNvPresetOnStartup(int attempts = 5) { @@ -2749,5 +2806,13 @@ Do you want to continue?" edtLgPresetTriggerConditions.Tag = (PresetConditionType)values.First().Value; edtLgPresetTriggerConditions.Text = Utils.GetDescriptions((int)value).Join(", "); } + + private void chkCheckForUpdates_CheckedChanged(object sender, EventArgs e) + { + if (_initialized) + { + _config.CheckForUpdates = chkCheckForUpdates.Checked; + } + } } } \ No newline at end of file diff --git a/ColorControl/MessageForms.cs b/ColorControl/MessageForms.cs index 95d78eb..c4153a0 100644 --- a/ColorControl/MessageForms.cs +++ b/ColorControl/MessageForms.cs @@ -38,9 +38,9 @@ namespace ColorControl MessageBox.Show(text, MainForm?.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } - public static void InfoOk(string text) + public static void InfoOk(string text, string title = null, string url = null) { - MessageBox.Show(text, MainForm?.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBox.Show(text, title ?? MainForm?.Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, 0, url); } public static DialogResult QuestionYesNo(string text) diff --git a/ColorControl/Properties/AssemblyInfo.cs b/ColorControl/Properties/AssemblyInfo.cs index 683c75c..2d9b0ff 100644 --- a/ColorControl/Properties/AssemblyInfo.cs +++ b/ColorControl/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("4.0.0.0")] -[assembly: AssemblyFileVersion("4.0.0.0")] +[assembly: AssemblyVersion("4.1.0.0")] +[assembly: AssemblyFileVersion("4.1.0.0")] diff --git a/ColorControl/Utils.cs b/ColorControl/Utils.cs index fdee71b..f1f10ae 100644 --- a/ColorControl/Utils.cs +++ b/ColorControl/Utils.cs @@ -12,13 +12,13 @@ using System.Drawing; using System.Globalization; using System.IO; using System.Linq; +using System.Net.Http; using System.Reflection; using System.Runtime.InteropServices; using System.Security.Principal; using System.Threading; using System.Threading.Tasks; using System.Web.Script.Serialization; -using System.Windows; using System.Windows.Forms; using Windows.Devices.Enumeration; using Windows.Devices.Enumeration.Pnp; @@ -630,6 +630,11 @@ namespace ColorControl public static int SetComboBoxEnumIndex(ComboBox comboBox, int value) { + if (comboBox.Items.Count == 0) + { + return -1; + } + var index = 0; for (var i = 0; i < comboBox.Items.Count; i++) @@ -926,6 +931,42 @@ namespace ColorControl } target.AddRange(words); } + + public static int ParseInt(string value, int def = 0) + { + if (int.TryParse(value, out var result)) + { + return result; + } + + return def; + } + + public static async Task GetRestJsonAsync(string url, Action callBack = null) + { + var httpClient = new HttpClient(); + httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd("request"); + + var response = await httpClient.GetAsync(url); + if (response.IsSuccessStatusCode) + { + var result = await response.Content.ReadAsAsync(); + + if (callBack != null) + { + callBack(result); + } + + return result; + } + + if (callBack != null) + { + callBack(null); + } + + return null; + } } public class EnumComboBoxItem diff --git a/ColorControl/lgtv/LgTvConnection.cs b/ColorControl/lgtv/LgTvConnection.cs index db26586..ec672c1 100644 --- a/ColorControl/lgtv/LgTvConnection.cs +++ b/ColorControl/lgtv/LgTvConnection.cs @@ -205,7 +205,14 @@ namespace LgTv if (_callbacks.TryGetValue(id, out Func callback)) { - callback(obj.payload); + try + { + callback(obj.payload); + } + catch (Exception callbackException) + { + Logger.Error($"Connection_MessageReceived: the callback threw an exception: {callbackException.ToLogString()}"); + } } } } @@ -230,8 +237,9 @@ namespace LgTv { try { - if (taskCompletion.Task?.Status == TaskStatus.Running) + if (taskCompletion.Task.Status != TaskStatus.RanToCompletion) { + Logger.Debug("taskCompletion.Task.Status: " + taskCompletion.Task.Status); taskCompletion.SetException(new Exception(ex.Message)); } } diff --git a/ColorControl/packages.config b/ColorControl/packages.config index 967ac52..8adeeb0 100644 --- a/ColorControl/packages.config +++ b/ColorControl/packages.config @@ -1,5 +1,6 @@  +