Fix crash when trying to select Harmony command without configuration.
authorStephaneLenclud
Wed, 31 Aug 2016 16:06:47 +0200
changeset 262c4749a27966d
parent 261 e2729a990e8b
child 263 3ab73a2a72d8
Fix crash when trying to select Harmony command without configuration.
Consolidate Named event Trigger.
Server/Actions/ActionHarmonyCommand.cs
Server/FormSelectHarmonyCommand.cs
SharpLibEar/Event.cs
SharpLibEar/Manager.cs
     1.1 --- a/Server/Actions/ActionHarmonyCommand.cs	Tue Aug 30 21:14:18 2016 +0200
     1.2 +++ b/Server/Actions/ActionHarmonyCommand.cs	Wed Aug 31 16:06:47 2016 +0200
     1.3 @@ -8,6 +8,7 @@
     1.4  using System.Threading.Tasks;
     1.5  using System.Runtime.Serialization;
     1.6  using System.Windows.Forms;
     1.7 +using CodeProject.Dialog;
     1.8  
     1.9  namespace SharpDisplayManager
    1.10  {
    1.11 @@ -120,6 +121,12 @@
    1.12          /// <param name="e"></param>
    1.13          void ClickEventHandler(object sender, EventArgs e)
    1.14          {
    1.15 +            if (Program.HarmonyConfig == null)
    1.16 +            {
    1.17 +                ErrBox.Show("No Harmony Hub configuration!");
    1.18 +                return;
    1.19 +            }
    1.20 +
    1.21              FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand();
    1.22              DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg);
    1.23              if (res == DialogResult.OK)
     2.1 --- a/Server/FormSelectHarmonyCommand.cs	Tue Aug 30 21:14:18 2016 +0200
     2.2 +++ b/Server/FormSelectHarmonyCommand.cs	Wed Aug 31 16:06:47 2016 +0200
     2.3 @@ -8,6 +8,7 @@
     2.4  using System.Text;
     2.5  using System.Threading.Tasks;
     2.6  using System.Windows.Forms;
     2.7 +using CodeProject.Dialog;
     2.8  
     2.9  namespace SharpDisplayManager
    2.10  {
    2.11 @@ -28,7 +29,15 @@
    2.12          /// <param name="e"></param>
    2.13          private void FormSelectHarmonyCommand_Load(object sender, EventArgs e)
    2.14          {
    2.15 -            PopulateTreeViewHarmony(Program.HarmonyConfig);
    2.16 +            if (Program.HarmonyConfig != null)
    2.17 +            {
    2.18 +                PopulateTreeViewHarmony(Program.HarmonyConfig);
    2.19 +            }
    2.20 +            else
    2.21 +            {
    2.22 +                ErrBox.Show("No Harmony Hub configuration!");
    2.23 +                Close();
    2.24 +            }           
    2.25          }
    2.26  
    2.27          /// <summary>
     3.1 --- a/SharpLibEar/Event.cs	Tue Aug 30 21:14:18 2016 +0200
     3.2 +++ b/SharpLibEar/Event.cs	Wed Aug 31 16:06:47 2016 +0200
     3.3 @@ -23,6 +23,10 @@
     3.4          ]
     3.5          public bool Enabled { get; set; } = true;
     3.6  
     3.7 +
     3.8 +        /// <summary>
     3.9 +        /// TODO: Should the name property be moved to our EAR Object?
    3.10 +        /// </summary>
    3.11          [DataMember]
    3.12          [AttributeObjectProperty
    3.13              (
    3.14 @@ -33,6 +37,7 @@
    3.15          ]
    3.16          public string Name { get; set; } = "";
    3.17  
    3.18 +
    3.19          [DataMember]
    3.20          public List<Action> Actions = new List<Action>();
    3.21  
    3.22 @@ -41,6 +46,12 @@
    3.23          {
    3.24              base.DoConstruct();
    3.25  
    3.26 +            //Make sure our name is not null
    3.27 +            if (Name == null)
    3.28 +            {
    3.29 +                Name = "";
    3.30 +            }
    3.31 +
    3.32              // TODO: Construct properties too
    3.33              foreach (Action a in Actions)
    3.34              {
     4.1 --- a/SharpLibEar/Manager.cs	Tue Aug 30 21:14:18 2016 +0200
     4.2 +++ b/SharpLibEar/Manager.cs	Wed Aug 31 16:06:47 2016 +0200
     4.3 @@ -79,11 +79,13 @@
     4.4          {
     4.5              if (string.IsNullOrEmpty(aName))
     4.6              {
     4.7 -                //Just don't do that that would be silly
     4.8 +                //Just don't do that, that would be silly
     4.9                  return;
    4.10              }
    4.11 -            //Only trigger events matching the desired type
    4.12 -            foreach (Event e in Events.Where(e => e.Name.Equals(aName)))
    4.13 +            // Only trigger events matching the desired type
    4.14 +            // Doing some safety checks as well to prevent crashing if name was left null for some reason
    4.15 +            // This was the case when loading existing settings after event Name was introduced
    4.16 +            foreach (Event e in Events.Where(e => !string.IsNullOrEmpty(e.Name) && aName.Equals(e.Name)))
    4.17              {
    4.18                  await e.Trigger();
    4.19              }