private void exportToolStripButton_Click(object sender, EventArgs e) { try { if (lvwPresets.SelectedItems.Count > 0) { if (saveTemplateFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { List <AgentPresetConfig> list = new List <AgentPresetConfig>(); foreach (ListViewItem lvi in lvwPresets.SelectedItems) { if (lvi.Tag is AgentPresetConfig) { AgentPresetConfig preset = (AgentPresetConfig)lvi.Tag; list.Add(preset); } } AgentPresetConfig.SavePresetsToFile(saveTemplateFileDialog.FileName, list); } } } catch (Exception ex) { MessageBox.Show("Export failed!\r\n" + ex.Message, "Export templates", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
private void LoadPresets() { List <AgentPresetConfig> allPresets = AgentPresetConfig.GetAllPresets(); lvwPresets.Groups.Clear(); foreach (string agentType in (from p in allPresets group p by p.AgentClassName into g select g.Key).OrderBy(p1 => p1)) { lvwPresets.Groups.Add(new ListViewGroup(agentType)); } lvwPresets.Items.Clear(); foreach (AgentPresetConfig preset in (from p in allPresets orderby p.Description select p)) { ListViewItem 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.SetGroupState(QuickMon.Controls.ListViewGroupState.Collapsible); SetTitleSaved(); }
private void cmdOK_Click(object sender, EventArgs e) { if (lvwPresets.SelectedItems.Count > 0) { SelectedPreset = (AgentPresetConfig)lvwPresets.SelectedItems[0].Tag; DialogResult = System.Windows.Forms.DialogResult.OK; Close(); } }
private void LoadSelectedPreset(AgentPresetConfig agentPresetConfig) { if (!isLoadingEditPreset) { isLoadingEditPreset = true; cboAgentType.SelectedItem = (from RegisteredAgent a in cboAgentType.Items where a.ClassName.EndsWith(agentPresetConfig.AgentClassName) select a).FirstOrDefault(); txtDescription.Text = agentPresetConfig.Description; txtConfig.Text = XmlFormattingUtils.NormalizeXML(agentPresetConfig.Config); txtConfig.SelectionStart = 0; txtConfig.DoCaretVisible(); isLoadingEditPreset = false; } }
private void resetToolStripButton_Click(object sender, EventArgs e) { try { DialogResult r = MessageBox.Show("This will reset all templates to the installed ones! This cannot be undone once completed.\r\nAre you sure you want to continue?", "Import", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (r == System.Windows.Forms.DialogResult.Yes) { AgentPresetConfig.ResetAllPresets(); LoadPresets(); } } catch (Exception ex) { MessageBox.Show("Reset failed!\r\n" + ex.Message, "Reset templates", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
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); } } } }
private void LoadPresetAgents() { lvwAgentType.Items.Clear(); ListViewItem lvi; ListViewGroup generalGroup = (from ListViewGroup gr in lvwAgentType.Groups where gr.Header.ToLower() == "general" select gr).FirstOrDefault(); foreach (AgentPresetConfig preset in (from p in AgentPresetConfig.GetAllPresets() orderby p.Description select p)) { try { RegisteredAgent ar = (from a in RegisteredAgentCache.Agents where ((selectingCollectors && a.IsCollector) || (!selectingCollectors && a.IsNotifier)) && a.ClassName.EndsWith(preset.AgentClassName) orderby a.Name select a).FirstOrDefault(); if (ar != null) { ListViewGroup agentGroup = (from ListViewGroup gr in lvwAgentType.Groups where gr.Header.ToLower() == ar.CategoryName.ToLower() select gr).FirstOrDefault(); if (agentGroup == null) { agentGroup = generalGroup; } lvi = new ListViewItem(preset.Description); string details = preset.Config; lvi.ImageIndex = 0; lvi.Group = agentGroup; lvi.SubItems.Add(details); lvi.Tag = preset; lvwAgentType.Items.Add(lvi); } } catch { } } }
private void lvwAgentType_SelectedIndexChanged(object sender, EventArgs e) { cmdOK.Enabled = lvwAgentType.SelectedItems.Count == 1; bool presets = false; try { if (lvwAgentType.SelectedItems.Count > 0 && lvwAgentType.SelectedItems[0].Tag is RegisteredAgent) { RegisteredAgent ra = (RegisteredAgent)lvwAgentType.SelectedItems[0].Tag; List <AgentPresetConfig> existingPresets = AgentPresetConfig.GetPresetsForClass(ra.Name); presets = (existingPresets != null && existingPresets.Count > 0); } } catch { } optSelectPreset.Enabled = presets; if (optSelectPreset.Checked && !presets) { optShowConfigEditor.Checked = true; } }
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); }
private void importToolStripButton_Click(object sender, EventArgs e) { try { DialogResult r = MessageBox.Show("Do you want any similar named (description) templates that are imported to overwrite existing templates?", "Import", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (r == System.Windows.Forms.DialogResult.Cancel) { return; } if (openFileDialogOpen.ShowDialog() == System.Windows.Forms.DialogResult.OK) { int importCount = 0; foreach (string fileName in openFileDialogOpen.FileNames) { List <AgentPresetConfig> newPresets = AgentPresetConfig.ReadPresetsFromFile(fileName); foreach (string agentType in (from p in newPresets group p by p.AgentClassName into g select g.Key)) { if ((from ListViewGroup gr in lvwPresets.Groups where gr.Header == agentType select gr).Count() == 0) { lvwPresets.Groups.Add(new ListViewGroup(agentType)); } } foreach (AgentPresetConfig preset in newPresets) { ListViewItem lvi = (from ListViewItem lv in lvwPresets.Items where lv.Tag is AgentPresetConfig && ((AgentPresetConfig)lv.Tag).AgentClassName == preset.AgentClassName && lv.Text.ToLower() == preset.Description.ToLower() select lv).FirstOrDefault(); if (lvi == null) { 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); importCount++; } else if (r == System.Windows.Forms.DialogResult.Yes) { lvi.Group = (from ListViewGroup gr in lvwPresets.Groups where gr.Header == preset.AgentClassName select gr).FirstOrDefault(); lvi.Tag = preset; importCount++; } } } //SetTitleChanged(); SaveChanges(); MessageBox.Show(string.Format("{0} item(s) imported", importCount), "Import", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch (Exception ex) { MessageBox.Show("Import failed!\r\n" + ex.Message, "Save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }