From e64e836525fc93b03888f8ecebf3e3f74c58f347 Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Fri, 1 Oct 2021 18:04:22 +1300 Subject: [PATCH] Message display working This is a first working message display, but doesn't ignore messages seen already. This needs fixing. --- DisplayMagician/MessageItem.cs | 2 +- DisplayMagician/Program.cs | 72 ++++++++++++++++++++- DisplayMagician/Properties/AssemblyInfo.cs | 4 +- DisplayMagician/UIForms/StartMessageForm.cs | 27 +++++++- 4 files changed, 97 insertions(+), 8 deletions(-) diff --git a/DisplayMagician/MessageItem.cs b/DisplayMagician/MessageItem.cs index 70b1041..a89e5b5 100644 --- a/DisplayMagician/MessageItem.cs +++ b/DisplayMagician/MessageItem.cs @@ -12,7 +12,7 @@ namespace DisplayMagician { get; set; } public string MessageMode - { get; set; } = "RTF"; + { get; set; } = "txt"; public string MinVersion { get; set; } diff --git a/DisplayMagician/Program.cs b/DisplayMagician/Program.cs index 8924dce..b630575 100644 --- a/DisplayMagician/Program.cs +++ b/DisplayMagician/Program.cs @@ -985,14 +985,82 @@ namespace DisplayMagician { return; } - StartMessageForm myMessageWindow = new StartMessageForm(); + foreach (MessageItem message in messageIndex) { + // Firstly, check that the version is correct + Version myAppVersion = Assembly.GetEntryAssembly().GetName().Version; + if (!string.IsNullOrWhiteSpace(message.MinVersion)) + { + Version minVersion; + if (!(Version.TryParse(message.MinVersion,out minVersion))) + { + logger.Error($"Program/ShowMessages: Couldn't show message \"{message.HeadingText}\" (#{message.Id}) as we couldn't parse the minversion string."); + continue; + } + + if (!(myAppVersion >= minVersion)) + { + logger.Debug($"Program/ShowMessages: Message is for version >= {minVersion} and this is version {myAppVersion} so not showing message."); + continue; + } + } + if (!string.IsNullOrWhiteSpace(message.MaxVersion)) + { + Version maxVersion; + if (!(Version.TryParse(message.MaxVersion, out maxVersion))) + { + logger.Error($"Program/ShowMessages: Couldn't show message \"{message.HeadingText}\" (#{message.Id}) as we couldn't parse the maxversion string."); + continue; + } + + if (!(myAppVersion <= maxVersion)) + { + logger.Debug($"Program/ShowMessages: Message is for version <= {maxVersion} and this is version {myAppVersion} so not showing message."); + continue; + } + } + + // Secondly check if the dates are such that we should show this + if (!string.IsNullOrWhiteSpace(message.StartDate)) + { + // Convert datestring to a datetime + DateTime startTime; + if (!DateTime.TryParse(message.StartDate,out startTime)) + { + logger.Error($"Program/ShowMessages: Couldn't show message \"{message.HeadingText}\" (#{message.Id}) as we couldn't parse the start date."); + continue; + } + + if (!(DateTime.Now >= startTime)) + { + logger.Debug($"Program/ShowMessages: Message start date for \"{message.HeadingText}\" (#{message.Id}) not yet reached so not ready to show message."); + continue; + } + } + if (!string.IsNullOrWhiteSpace(message.EndDate)) + { + // Convert datestring to a datetime + DateTime endTime; + if (!DateTime.TryParse(message.EndDate, out endTime)) + { + logger.Error($"Program/ShowMessages: Couldn't show message \"{message.HeadingText}\" (#{message.Id}) as we couldn't parse the end date."); + continue; + } + + if (!(DateTime.Now <= endTime)) + { + logger.Debug($"Program/ShowMessages: Message end date for \"{message.HeadingText}\" (#{message.Id}) past so not showing message as it's too old."); + continue; + } + } + + StartMessageForm myMessageWindow = new StartMessageForm(); myMessageWindow.MessageMode = message.MessageMode; myMessageWindow.URL = message.Url; myMessageWindow.HeadingText = message.HeadingText; myMessageWindow.ButtonText = message.ButtonText; - myMessageWindow.Show(); + myMessageWindow.ShowDialog(); } } diff --git a/DisplayMagician/Properties/AssemblyInfo.cs b/DisplayMagician/Properties/AssemblyInfo.cs index 8d6a780..2e2d6c2 100644 --- a/DisplayMagician/Properties/AssemblyInfo.cs +++ b/DisplayMagician/Properties/AssemblyInfo.cs @@ -26,8 +26,8 @@ using System.Resources; [assembly: Guid("e4ceaf5e-ad01-4695-b179-31168eb74c48")] // Version information -[assembly: AssemblyVersion("2.0.0.10")] -[assembly: AssemblyFileVersion("2.0.0.10")] +[assembly: AssemblyVersion("2.0.0.15")] +[assembly: AssemblyFileVersion("2.0.0.15")] [assembly: NeutralResourcesLanguageAttribute( "en" )] [assembly: CLSCompliant(true)] diff --git a/DisplayMagician/UIForms/StartMessageForm.cs b/DisplayMagician/UIForms/StartMessageForm.cs index 673ad8a..a04f6c9 100644 --- a/DisplayMagician/UIForms/StartMessageForm.cs +++ b/DisplayMagician/UIForms/StartMessageForm.cs @@ -77,10 +77,16 @@ namespace DisplayMagician.UIForms { if (File.Exists(Filename)) { - if (MessageMode == "RTF") + if (MessageMode == "rtf") { + rtb_message.Show(); rtb_message.LoadFile(Filename, RichTextBoxStreamType.RichText); } + else if (MessageMode == "txt") + { + rtb_message.Show(); + rtb_message.LoadFile(Filename, RichTextBoxStreamType.PlainText); + } else { logger.Error($"StartMessageForm/StartMessageForm_Load: Message from file {Filename} is in an unsupported MessageMode: {MessageMode}"); @@ -109,7 +115,7 @@ namespace DisplayMagician.UIForms } // If we get here, then the URL is good. See if we can access the URL supplied WebClient client = new WebClient(); - if (MessageMode == "RTF") + if (MessageMode == "rtf") { try { @@ -117,6 +123,7 @@ namespace DisplayMagician.UIForms MemoryStream theMemStream = new MemoryStream(); theMemStream.Write(byteArray, 0, byteArray.Length); theMemStream.Position = 0; + rtb_message.Show(); rtb_message.LoadFile(theMemStream, RichTextBoxStreamType.RichText); } catch (Exception ex) @@ -124,8 +131,22 @@ namespace DisplayMagician.UIForms logger.Error(ex, $"StartMessageForm/StartMessageForm_Load: Exception while trying to load the URL supplied (\"{URL}\") into the RichTextBox message object (RTF Mode)"); this.Close(); return; + } + } + else if (MessageMode == "txt") + { + try + { + string textToShow = client.DownloadString(URL); + rtb_message.Show(); + rtb_message.Text = textToShow; + } + catch (Exception ex) + { + logger.Error(ex, $"StartMessageForm/StartMessageForm_Load: Exception while trying to load the URL supplied (\"{URL}\") into the RichTextBox message object (TXT Mode)"); + this.Close(); + return; } - } else {