From 85f28ff9d58f24c2f3cc95f9165f765fb0827373 Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Thu, 30 Sep 2021 20:47:52 +1300 Subject: [PATCH] [WIP] Partial startup message support Adding ability to provide messages during bootup. This is specifically to allow update messages. --- DisplayMagician/Program.cs | 3 + .../UIForms/StartMessageForm.Designer.cs | 9 ++- DisplayMagician/UIForms/StartMessageForm.cs | 76 ++++++++++++++++++- 3 files changed, 81 insertions(+), 7 deletions(-) diff --git a/DisplayMagician/Program.cs b/DisplayMagician/Program.cs index 368fd44..17509d3 100644 --- a/DisplayMagician/Program.cs +++ b/DisplayMagician/Program.cs @@ -643,6 +643,9 @@ namespace DisplayMagician { // Check for updates CheckForUpdates(); + // Check if it's an upgrade from DisplayMagician v1 to v2 + // and if it is then explain what the user needs to do. + // Run the program with normal startup AppMainForm = new MainForm(); Application.Run(AppMainForm); diff --git a/DisplayMagician/UIForms/StartMessageForm.Designer.cs b/DisplayMagician/UIForms/StartMessageForm.Designer.cs index 51ef984..c13bb3c 100644 --- a/DisplayMagician/UIForms/StartMessageForm.Designer.cs +++ b/DisplayMagician/UIForms/StartMessageForm.Designer.cs @@ -40,11 +40,12 @@ namespace DisplayMagician.UIForms this.lbl_profile_shown.BackColor = System.Drawing.Color.Black; this.lbl_profile_shown.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lbl_profile_shown.ForeColor = System.Drawing.Color.White; - this.lbl_profile_shown.Location = new System.Drawing.Point(478, 18); + this.lbl_profile_shown.Location = new System.Drawing.Point(437, 19); this.lbl_profile_shown.Name = "lbl_profile_shown"; - this.lbl_profile_shown.Size = new System.Drawing.Size(318, 29); + this.lbl_profile_shown.Size = new System.Drawing.Size(400, 29); this.lbl_profile_shown.TabIndex = 20; - this.lbl_profile_shown.Text = "Important Upgrade Message"; + this.lbl_profile_shown.Text = "Important DisplayMagician Message"; + this.lbl_profile_shown.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // rtb_message // @@ -83,7 +84,7 @@ namespace DisplayMagician.UIForms this.Name = "StartMessageForm"; this.ShowIcon = false; this.ShowInTaskbar = false; - this.Text = "DisplayMagician - Startup Message"; + this.Text = "DisplayMagician - Message"; this.Load += new System.EventHandler(this.StartMessageForm_Load); this.ResumeLayout(false); this.PerformLayout(); diff --git a/DisplayMagician/UIForms/StartMessageForm.cs b/DisplayMagician/UIForms/StartMessageForm.cs index 787aaeb..48411ff 100644 --- a/DisplayMagician/UIForms/StartMessageForm.cs +++ b/DisplayMagician/UIForms/StartMessageForm.cs @@ -8,13 +8,17 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; +using System.Net; namespace DisplayMagician.UIForms { public partial class StartMessageForm : Form { + private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); public string Filename; + public string URL; + public string Heading; public StartMessageForm() { @@ -28,10 +32,76 @@ namespace DisplayMagician.UIForms private void StartMessageForm_Load(object sender, EventArgs e) { - if (File.Exists(Filename)) + string FullPath; + + // check if we're in Filename mode or URL mode + if (!String.IsNullOrWhiteSpace(Filename)) { - rtb_message.LoadFile(Filename, RichTextBoxStreamType.RichText); - } + // We're in filename mode + // Figure out the full path of the filename + try + { + FullPath = Path.Combine(Application.StartupPath, Filename); + } + catch (Exception ex) + { + logger.Error(ex, $"StartMessageForm/StartMessageForm_Load: Filename supplied (\"{Filename}\") is not within the Application startup path (\"{Application.StartupPath}\")"); + return; + } + + // Try to load the Filename if it's supplied + try + { + if (File.Exists(Filename)) + { + rtb_message.LoadFile(Filename, RichTextBoxStreamType.RichText); + } + else + { + logger.Error($"StartMessageForm/StartMessageForm_Load: Couldn't find the Filename supplied (\"{Filename}\") and load it into the RichTextBox message object"); + } + } + catch (Exception ex) + { + logger.Error(ex, $"StartMessageForm/StartMessageForm_Load: Exception while trying to load the Filename supplied (\"{Filename}\") into the RichTextBox message object"); + return; + } + } + else + { + // We're in URL mode + // See if the URL supplied is valid + if (!IsURLValid(URL)) + { + logger.Error($"StartMessageForm/StartMessageForm_Load: URL {URL} pointing to the RTF file is invalid!"); + return; + } + // If we get here, then the URL is good. See if we can access the URL supplied + WebClient client = new WebClient(); + try + { + byte[] byteArray = client.DownloadData(URL); + MemoryStream theMemStream = new MemoryStream(); + theMemStream.Write(byteArray, 0, byteArray.Length); + rtb_message.LoadFile(theMemStream, RichTextBoxStreamType.RichText); + } + catch (Exception ex) + { + logger.Error(ex, $"StartMessageForm/StartMessageForm_Load: Exception while trying to load the URL supplied (\"{Filename}\") into the RichTextBox message object"); + return; + } + + } + } + + public static bool IsURLValid(string url) + { + Uri uriResult; + bool tryCreateResult = Uri.TryCreate(url, UriKind.Absolute, out uriResult); + if (tryCreateResult == true && uriResult != null) + return true; + else + return false; } } }