Esempio n. 1
0
        private void llblExportConfigAsTemplate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (editingNotifierEntry != null && editingNotifierEntry.Notifier != null && txtName.Text.Trim().Length > 0)
            {
                if (MessageBox.Show("Are you sure you want to export the current config as a template?\r\nThe notifier entry name will be used as name for the template.", "Export", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    try
                    {
                        AgentPresetConfig newPreset = new AgentPresetConfig();
                        newPreset.AgentClassName = editingNotifierEntry.NotifierRegistrationName;
                        newPreset.Description    = txtName.Text;
                        newPreset.Config         = editingNotifierEntry.InitialConfiguration;
                        List <AgentPresetConfig> allExistingPresets = AgentPresetConfig.GetAllPresets();

                        if ((from p in allExistingPresets
                             where p.AgentClassName == newPreset.AgentClassName && p.Description == newPreset.Description
                             select p).Count() > 0)
                        {
                            if (MessageBox.Show("A template with the name '" + newPreset.Description + "' already exist!\r\nDo you want to replace it?", "Export", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                            {
                                return;
                            }
                            else
                            {
                                AgentPresetConfig existingEntry = (from p in allExistingPresets
                                                                   where p.AgentClassName == newPreset.AgentClassName && p.Description == newPreset.Description
                                                                   select p).FirstOrDefault();
                                existingEntry.Config = newPreset.Config;
                            }
                        }
                        else
                        {
                            allExistingPresets.Add(newPreset);
                        }
                        AgentPresetConfig.SaveAllPresetsToFile(allExistingPresets);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Export", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
Esempio n. 2
0
        private bool SaveChanges()
        {
            bool success = false;

            try
            {
                //first add currently edited record to list
                if (templateChange)
                {
                    #region Validation
                    if (cboAgentType.SelectedIndex == -1)
                    {
                        MessageBox.Show("You must specify an agent type!", "Save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        cboAgentType.Focus();
                        return(false);
                    }
                    else if (txtDescription.Text.Trim().Length == 0)
                    {
                        MessageBox.Show("You must specify a description!", "Save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        txtDescription.Focus();
                        return(false);
                    }
                    else if (txtConfig.Text.Trim().Length == 0)
                    {
                        MessageBox.Show("You must specify some config details!", "Save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        txtConfig.Focus();
                        return(false);
                    }
                    try
                    {
                        System.Xml.XmlDocument testDoc = new System.Xml.XmlDocument();
                        testDoc.LoadXml(txtConfig.Text);
                    }
                    catch (Exception exValidation)
                    {
                        MessageBox.Show("You must specify a valid config string!\r\n" + exValidation.Message, "Save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        txtConfig.Focus();
                        return(false);
                    }
                    #endregion

                    ListViewItem lvi = (from ListViewItem lv in lvwPresets.Items
                                        where lv.Tag is AgentPresetConfig &&
                                        ((AgentPresetConfig)lv.Tag).AgentClassName == ((RegisteredAgent)cboAgentType.SelectedItem).Name &&
                                        lv.Text.ToLower() == txtDescription.Text.ToLower()
                                        select lv).FirstOrDefault();
                    if (lvi != null)
                    {
                        AgentPresetConfig preset = (AgentPresetConfig)lvi.Tag;
                        preset.AgentClassName = ((RegisteredAgent)cboAgentType.SelectedItem).Name;
                        lvi.Group             = (from ListViewGroup gr in lvwPresets.Groups
                                                 where gr.Header == preset.AgentClassName
                                                 select gr).FirstOrDefault();
                        preset.Config = txtConfig.Text;
                    }
                    else
                    {
                        AgentPresetConfig preset = new AgentPresetConfig();
                        preset.Description    = txtDescription.Text;
                        preset.AgentClassName = ((RegisteredAgent)cboAgentType.SelectedItem).Name;
                        preset.Config         = txtConfig.Text;

                        lvi            = new ListViewItem(preset.Description);
                        lvi.ImageIndex = 0;
                        lvi.Group      = (from ListViewGroup gr in lvwPresets.Groups
                                          where gr.Header == preset.AgentClassName
                                          select gr).FirstOrDefault();
                        lvi.Tag = preset;
                        lvwPresets.Items.Add(lvi);
                        lvwPresets.SelectedItems.Clear();
                        lvi.Selected = true;
                    }
                }
                //Second save all Presets to file
                List <AgentPresetConfig> list = new List <AgentPresetConfig>();
                foreach (ListViewItem lvi in lvwPresets.Items)
                {
                    if (lvi.Tag is AgentPresetConfig)
                    {
                        list.Add((AgentPresetConfig)lvi.Tag);
                    }
                }
                AgentPresetConfig.SaveAllPresetsToFile(list);

                SetTitleSaved();
                success = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Save failed!\r\n" + ex.Message, "Save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return(success);
        }