/// <summary> /// Creates or updates a Crm connection /// </summary> /// <param name="isCreation">Indicates if it is a connection creation</param> /// <param name="connectionToUpdate">Details of the connection to update</param> /// <param name="connectionFile">List of connections where the connection is edited from</param> /// <returns>Created or updated connection</returns> public ConnectionDetail EditConnection(bool isCreation, ConnectionDetail connectionToUpdate, ConnectionFile connectionFile = null) { var cForm = new ConnectionWizard2(connectionToUpdate) { StartPosition = FormStartPosition.CenterParent }; if (cForm.ShowDialog(innerAppForm) == DialogResult.OK) { if (isCreation) { if (connectionFile == null) { if (ConnectionManager.Instance.ConnectionsList.Connections.FirstOrDefault( d => d.ConnectionId == cForm.CrmConnectionDetail.ConnectionId) == null) { ConnectionManager.Instance.ConnectionsList.Connections.Add(cForm.CrmConnectionDetail); } } else { var connections = CrmConnections.LoadFromFile(connectionFile.Path); if (connections.Connections.FirstOrDefault( d => d.ConnectionId == cForm.CrmConnectionDetail.ConnectionId) == null) { connections.Connections.Add(cForm.CrmConnectionDetail); } connections.SerializeToFile(connectionFile.Path); } } else { if (connectionFile == null) { ConnectionManager.Instance.ConnectionsList.Connections .Where(x => x.ConnectionId == cForm.CrmConnectionDetail.ConnectionId) .ToList() .ForEach(x => x.UpdateAfterEdit(cForm.CrmConnectionDetail)); } else { var connections = CrmConnections.LoadFromFile(connectionFile.Path); foreach (ConnectionDetail detail in connections.Connections) { if (detail.ConnectionId == cForm.CrmConnectionDetail.ConnectionId) { detail.UpdateAfterEdit(cForm.CrmConnectionDetail); } } connections.SerializeToFile(connectionFile.Path); } } return(cForm.CrmConnectionDetail); } return(null); }
private void btnOk_Click(object sender, EventArgs e) { try { var newCc = CrmConnections.LoadFromFile(txtFilePath.Text); OpenedFile = new ConnectionFile(newCc) { Path = txtFilePath.Text, Name = newCc.Name, LastUsed = DateTime.Now }; if (ConnectionsList.Instance.Files.Any(f => f.Name == OpenedFile.Name)) { int cloneId = 1; string newName = OpenedFile.Name ?? "New File"; while (ConnectionsList.Instance.Files.FirstOrDefault(f => f.Name == newName) != null) { var rule = new System.Text.RegularExpressions.Regex(".* \\(" + cloneId + "\\)$"); if (rule.IsMatch(newName)) { cloneId++; newName = $"{OpenedFile?.Name?.Replace($" ({cloneId - 1})", "") ?? "New File"} ({cloneId})"; } else { newName = $"{newName} ({cloneId})"; } } OpenedFile.Name = newName; MessageBox.Show(this, $"A connection file with this name already exists!\n\nIt has been renamed to '{newName}'", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } ConnectionManager.ConfigurationFile = txtFilePath.Text; ConnectionsList.Instance.Files.First(f => f.Path == txtFilePath.Text).Name = OpenedFile.Name; ConnectionManager.Instance.LoadConnectionsList(); ConnectionsList.Instance.Save(); OpenedFilePath = txtFilePath.Text; DialogResult = DialogResult.OK; Close(); } catch (Exception error) { MessageBox.Show(this, "It seems something went wrong when loading your file: " + error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Adds the ToolStripMenuItems representing connections to the /// ToolStripDropDownButton /// </summary> /// <param name="btn">ToolStripDropDownButton where to add connections</param> private void AddActionsList(ToolStripDropDownButton btn) { var list = new List <ToolStripItem>(); int filesCount = ConnectionsList.Instance.Files.Count; if (filesCount == 0) { var defaultFilePath = Path.Combine(new FileInfo(ConnectionsList.ConnectionsListFilePath).DirectoryName, "ConnectionsList.Default.xml"); CrmConnections cc = new CrmConnections("Default"); cc.SerializeToFile(defaultFilePath); ConnectionsList.Instance.Files.Add(new ConnectionFile(cc) { Path = defaultFilePath, LastUsed = DateTime.Now }); ConnectionsList.Instance.Save(); } foreach (var file in ConnectionsList.Instance.Files) { var connections = CrmConnections.LoadFromFile(file.Path); connections.Connections.Sort(); var fileItem = new ToolStripMenuItem(file.Name); fileItem.Tag = file; if (!mergeConnectionFiles && filesCount > 1) { list.Add(fileItem); } foreach (var cDetail in connections.Connections) { ToolStripMenuItem item = new ToolStripMenuItem(); item.Text = cDetail.ConnectionName; item.Tag = cDetail; if (cDetail.UseOnline) { item.Image = RessourceManager.GetImage( "McTools.Xrm.Connection.WinForms.Resources.CRMOnlineLive_16.png"); } else if (cDetail.UseOsdp) { item.Image = RessourceManager.GetImage( "McTools.Xrm.Connection.WinForms.Resources.CRMOnlineLive_16.png"); } else if (cDetail.UseIfd) { item.Image = RessourceManager.GetImage( "McTools.Xrm.Connection.WinForms.Resources.server_key.png"); } else { item.Image = RessourceManager.GetImage( "McTools.Xrm.Connection.WinForms.Resources.server.png"); } BuildActionItems(item); if (!mergeConnectionFiles && filesCount > 1) { fileItem.DropDownItems.Add(item); } else { list.Add(item); } } if (!mergeConnectionFiles && filesCount > 1) { if (fileItem.DropDownItems.Count > 0) { fileItem.DropDownItems.Add(new ToolStripSeparator()); } } var newConnectionItem = new ToolStripMenuItem(); newConnectionItem.Text = "Create new connection"; newConnectionItem.Image = (Image)resources.GetObject("server_add"); newConnectionItem.Click += newConnectionItem_Click; if (!mergeConnectionFiles && filesCount > 1) { fileItem.DropDownItems.Add(newConnectionItem); } } if (mergeConnectionFiles || filesCount == 1) { if (list.Count > 0) { list.Add(new ToolStripSeparator()); } var newConnectionItem = new ToolStripMenuItem(); newConnectionItem.Text = "Create new connection"; newConnectionItem.Image = (Image)resources.GetObject("server_add"); newConnectionItem.Click += newConnectionItem_Click; list.Add(newConnectionItem); } if (InvokeRequired) { Invoke(new Action(() => { btn.DropDownItems.Clear(); btn.DropDownItems.AddRange(list.ToArray()); })); } else { btn.DropDownItems.Clear(); btn.DropDownItems.AddRange(list.ToArray()); } }