[WIP] Partial startup message support

Adding ability to provide messages during bootup. This is specifically to allow update messages.
This commit is contained in:
Terry MacDonald 2021-09-30 20:47:52 +13:00
parent 9eafe478fc
commit f6177fe493
3 changed files with 81 additions and 7 deletions

View File

@ -643,6 +643,9 @@ namespace DisplayMagician {
// Check for updates // Check for updates
CheckForUpdates(); 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 // Run the program with normal startup
AppMainForm = new MainForm(); AppMainForm = new MainForm();
Application.Run(AppMainForm); Application.Run(AppMainForm);

View File

@ -40,11 +40,12 @@ namespace DisplayMagician.UIForms
this.lbl_profile_shown.BackColor = System.Drawing.Color.Black; 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.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.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.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.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 // rtb_message
// //
@ -83,7 +84,7 @@ namespace DisplayMagician.UIForms
this.Name = "StartMessageForm"; this.Name = "StartMessageForm";
this.ShowIcon = false; this.ShowIcon = false;
this.ShowInTaskbar = false; this.ShowInTaskbar = false;
this.Text = "DisplayMagician - Startup Message"; this.Text = "DisplayMagician - Message";
this.Load += new System.EventHandler(this.StartMessageForm_Load); this.Load += new System.EventHandler(this.StartMessageForm_Load);
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();

View File

@ -8,13 +8,17 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using System.IO; using System.IO;
using System.Net;
namespace DisplayMagician.UIForms namespace DisplayMagician.UIForms
{ {
public partial class StartMessageForm : Form public partial class StartMessageForm : Form
{ {
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public string Filename; public string Filename;
public string URL;
public string Heading;
public StartMessageForm() public StartMessageForm()
{ {
@ -28,10 +32,76 @@ namespace DisplayMagician.UIForms
private void StartMessageForm_Load(object sender, EventArgs e) 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;
} }
} }
} }