#region License // Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved. // This source code is made available under the terms of the GNU Lesser General Public License v3.0 // https://github.com/paulkohler/minisqlquery/blob/master/LICENSE #endregion using System; using System.Windows.Forms; using MiniSqlQuery.Core; using MiniSqlQuery.Properties; namespace MiniSqlQuery.PlugIns.ConnectionStringsManager { /// The db connections form. public partial class DbConnectionsForm : Form { /// The _host window. private readonly IHostWindow _hostWindow; /// The _services. private readonly IApplicationServices _services; /// The _settings. private readonly IApplicationSettings _settings; /// The _definition list. private DbConnectionDefinitionList _definitionList; private bool _loaded; private bool _dirty; /// Initializes a new instance of the class. public DbConnectionsForm() { InitializeComponent(); toolStripButtonAdd.Image = ImageResource.database_add; toolStripButtonCopyAsNew.Image = ImageResource.database_add; toolStripButtonEditConnStr.Image = ImageResource.database_edit; toolStripButtonDelete.Image = ImageResource.database_delete; Icon = ImageResource.disconnect_icon; } /// Initializes a new instance of the class. /// The services. /// The host window. /// The settings. public DbConnectionsForm(IApplicationServices services, IHostWindow hostWindow, IApplicationSettings settings) : this() { _services = services; _hostWindow = hostWindow; _settings = settings; } /// The load connection definitions. /// private static DbConnectionDefinitionList LoadConnectionDefinitions() { return DbConnectionDefinitionList.FromXml(Utility.LoadConnections()); } /// The add to list. /// The definition. private void AddToList(DbConnectionDefinition definition) { if (!lstConnections.Items.Contains(definition)) { lstConnections.Items.Add(definition); SetDirty(); } } /// The db connections form_ form closing. /// The sender. /// The e. private void DbConnectionsForm_FormClosing(object sender, FormClosingEventArgs e) { if (_dirty) { DialogResult saveFile = _hostWindow.DisplayMessageBox( this, Resources.The_connection_details_have_changed__do_you_want_to_save, Resources.Save_Changes, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 0, null, null); switch (saveFile) { case DialogResult.Yes: SaveConnectionDefinitions(_definitionList); break; case DialogResult.Cancel: e.Cancel = true; break; } } } /// The db connections form_ shown. /// The sender. /// The e. private void DbConnectionsForm_Shown(object sender, EventArgs e) { _definitionList = LoadConnectionDefinitions(); UpdateListView(); _loaded = true; } /// The manage definition. /// The definition. private void ManageDefinition(DbConnectionDefinition definition) { ConnectionStringBuilderForm frm; string oldName = null; if (definition == null) { frm = new ConnectionStringBuilderForm(_hostWindow, _services); // new blank form } else { oldName = definition.Name; frm = new ConnectionStringBuilderForm(_hostWindow, definition, _services); } frm.ShowDialog(this); if (frm.DialogResult == DialogResult.OK) { SetDirty(); if (lstConnections.Items.Contains(frm.ConnectionDefinition) && definition != null) { if (definition.Name != oldName) { // want the list text to update due to name change UpdateListView(); lstConnections.SelectedItem = definition; } else { UpdateDetailsPanel(lstConnections.SelectedItem as DbConnectionDefinition); } } else { _definitionList.AddDefinition(frm.ConnectionDefinition); AddToList(frm.ConnectionDefinition); lstConnections.SelectedItem = frm.ConnectionDefinition; } } } /// The remove from list. /// The definition. private void RemoveFromList(DbConnectionDefinition definition) { if (lstConnections.Items.Contains(definition)) { lstConnections.Items.Remove(definition); SetDirty(); } } /// The save connection definitions. /// The data. private void SaveConnectionDefinitions(DbConnectionDefinitionList data) { _settings.SetConnectionDefinitions(data); Utility.SaveConnections(data); _dirty = false; } /// The update details panel. /// The definition. private void UpdateDetailsPanel(DbConnectionDefinition definition) { if (definition != null) { txtName.Text = definition.Name; txtProvider.Text = definition.ProviderName; txtConn.Text = definition.ConnectionString; txtComment.Text = definition.Comment; } else { txtName.Clear(); txtProvider.Clear(); txtConn.Clear(); txtComment.Clear(); } } /// The update list view. private void UpdateListView() { if (_definitionList.Definitions != null && _definitionList.Definitions.Length > 0) { lstConnections.Items.Clear(); lstConnections.Items.AddRange(_definitionList.Definitions); lstConnections.SelectedItem = _definitionList.Definitions[0]; } } /// The lst connections_ double click. /// The sender. /// The e. private void lstConnections_DoubleClick(object sender, EventArgs e) { DbConnectionDefinition definition = lstConnections.SelectedItem as DbConnectionDefinition; if (definition != null) { ManageDefinition(definition); } } /// The lst connections_ selected value changed. /// The sender. /// The e. private void lstConnections_SelectedValueChanged(object sender, EventArgs e) { DbConnectionDefinition definition = lstConnections.SelectedItem as DbConnectionDefinition; UpdateDetailsPanel(definition); } /// The tool strip button add_ click. /// The sender. /// The e. private void toolStripButtonAdd_Click(object sender, EventArgs e) { ManageDefinition(null); } /// The tool strip button cancel_ click. /// The sender. /// The e. private void toolStripButtonCancel_Click(object sender, EventArgs e) { Close(); } /// The tool strip button copy as new_ click. /// The sender. /// The e. private void toolStripButtonCopyAsNew_Click(object sender, EventArgs e) { DbConnectionDefinition definition = lstConnections.SelectedItem as DbConnectionDefinition; if (definition != null) { DbConnectionDefinition newDefinition = DbConnectionDefinition.FromXml(definition.ToXml()); newDefinition.Name = "Copy of " + newDefinition.Name; ManageDefinition(newDefinition); } } /// The tool strip button delete_ click. /// The sender. /// The e. private void toolStripButtonDelete_Click(object sender, EventArgs e) { DbConnectionDefinition definition = lstConnections.SelectedItem as DbConnectionDefinition; if (definition != null) { int newIndex = Math.Max(lstConnections.SelectedIndex - 1, 0); _definitionList.RemoveDefinition(definition); RemoveFromList(definition); if (lstConnections.Items.Count > 0) { lstConnections.SelectedIndex = newIndex; } } } /// The tool strip button edit conn str_ click. /// The sender. /// The e. private void toolStripButtonEditConnStr_Click(object sender, EventArgs e) { DbConnectionDefinition definition = lstConnections.SelectedItem as DbConnectionDefinition; if (definition != null) { ManageDefinition(definition); } } /// The tool strip button ok_ click. /// The sender. /// The e. private void toolStripButtonOk_Click(object sender, EventArgs e) { SaveConnectionDefinitions(_definitionList); Close(); } /// The tool strip button test_ click. /// The sender. /// The e. private void toolStripButtonTest_Click(object sender, EventArgs e) { // do a standalone raw connection test DbConnectionDefinition definition = lstConnections.SelectedItem as DbConnectionDefinition; if (definition != null) { Exception exp = QueryRunner.TestDbConnection(definition.ProviderName, definition.ConnectionString); if (exp == null) { string msg = string.Format("Connected to '{0}' successfully.", definition.Name); _hostWindow.DisplaySimpleMessageBox(this, msg, "Connection Successful"); } else { string msg = string.Format("Failed connecting to '{0}'.{1}{2}", definition.Name, Environment.NewLine, exp.Message); _hostWindow.DisplaySimpleMessageBox(this, msg, "Connection Failed"); } } } private void DbConnectionsForm_Load(object sender, EventArgs e) { } private void SetDirty() { if (!_loaded) { return; } if (!_dirty) { _dirty = true; Text += "*"; } } } }