From 0dcc738efd725d984f2e12b097c099cba95ad39c Mon Sep 17 00:00:00 2001 From: Jonas Rapp Date: Sun, 16 Jun 2024 11:27:51 +0200 Subject: [PATCH] When interfaces breaks, be more gentle --- XrmToolBox/New/NewForm.Community.cs | 63 ++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/XrmToolBox/New/NewForm.Community.cs b/XrmToolBox/New/NewForm.Community.cs index f06e720c..5050422c 100644 --- a/XrmToolBox/New/NewForm.Community.cs +++ b/XrmToolBox/New/NewForm.Community.cs @@ -24,26 +24,17 @@ private void displayXrmToolBoxHelpToolStripMenuItem_Click(object sender, System. private void donateDollarPluginMenuItem_Click(object sender, System.EventArgs e) { - if (((PluginForm)dpMain.ActiveContent).Control is IPayPalPlugin payPal) - { - Donate("EN", "USD", payPal.EmailAccount, payPal.DonationDescription); - } + DonatePlugin("USD"); } private void donateEuroPluginMenuItem_Click(object sender, System.EventArgs e) { - if (((PluginForm)dpMain.ActiveContent).Control is IPayPalPlugin payPal) - { - Donate("EN", "EUR", payPal.EmailAccount, payPal.DonationDescription); - } + DonatePlugin("EUR"); } private void donateGbpPluginMenuItem_Click(object sender, System.EventArgs e) { - if (((PluginForm)dpMain.ActiveContent).Control is IPayPalPlugin payPal) - { - Donate("EN", "GBP", payPal.EmailAccount, payPal.DonationDescription); - } + DonatePlugin("GBP"); } private void donateInEuroToolStripMenuItem_Click(object sender, System.EventArgs e) @@ -86,7 +77,8 @@ private string GetAdditionalInfo() private void githubPluginMenuItem_Click(object sender, System.EventArgs e) { var url = GetGithubBaseUrl("issues/new"); - + if (string.IsNullOrEmpty(url)) + return; var additionalInfo = GetAdditionalInfo(); if (currentContent is PluginForm pf) @@ -107,7 +99,10 @@ private void GithubXrmToolBoxMenuItem_Click(object sender, System.EventArgs e) private void HelpSelectedPluginToolStripMenuItem_Click(object sender, System.EventArgs e) { - Process.Start(GetHelpUrl()); + var url = GetHelpUrl(); + if (string.IsNullOrEmpty(url)) + return; + Process.Start(url); } private void helpToolStripMenuItem_Click(object sender, System.EventArgs e) @@ -224,16 +219,37 @@ private void Donate(string language, string currency, string emailAccount, strin Process.Start(url); } + private void DonatePlugin(string currency) + { + if (dpMain.ActiveContent is PluginForm pluginForm && pluginForm.Control is IPayPalPlugin payPal) + { + Donate("EN", currency, payPal.EmailAccount, payPal.DonationDescription); + } + else + { + MessageBox.Show("There is internal issue, cannot find the PayPal account.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + } + private string GetGithubBaseUrl(string page) { - var plugin = (IGitHubPlugin)((PluginForm)dpMain.ActiveContent).Control; - return $"https://github.com/{plugin.UserName}/{plugin.RepositoryName}/{page}"; + if (dpMain.ActiveContent is PluginForm plugin && + plugin.Control is IGitHubPlugin gitplugin) + { + return $"https://github.com/{gitplugin.UserName}/{gitplugin.RepositoryName}/{page}"; + } + MessageBox.Show("There is internal issue, cannot find the GitHub repository.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return null; } private string GetHelpUrl() { - var plugin = (IHelpPlugin)((PluginForm)dpMain.ActiveContent).Control; - return plugin.HelpUrl; + if (dpMain.ActiveContent is PluginForm pluginForm && pluginForm.Control is IHelpPlugin helpPlugin) + { + return helpPlugin.HelpUrl; + } + MessageBox.Show("There is internal issue, cannot find the Help URL.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return null; } private void tsmiAbout_Click(object sender, System.EventArgs e) @@ -246,8 +262,15 @@ private void tsmiAbout_Click(object sender, System.EventArgs e) private void tsmiAboutSelectedPlugin_Click(object sender, System.EventArgs e) { - var plugin = (IAboutPlugin)((PluginForm)dpMain.ActiveContent).Control; - plugin.ShowAboutDialog(); + if (dpMain.ActiveContent is PluginForm plugin && + plugin.Control is IAboutPlugin aboutPlugin) + { + aboutPlugin.ShowAboutDialog(); + } + else + { + MessageBox.Show("There is internal issue, cannot find the About window.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } } } } \ No newline at end of file