//  
//   Rebex Sample Code License
// 
//   Copyright 2025, REBEX CR s.r.o.
//   All rights reserved.
//   https://www.rebex.net/
// 
//   Permission to use, copy, modify, and/or distribute this software for any
//   purpose with or without fee is hereby granted.
// 
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//   OTHER DEALINGS IN THE SOFTWARE.
// 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Xml;

using Rebex.Net;

namespace Rebex.Samples
{
    /// <summary>
    /// The Connection class provides user interface for setting the ssh connection settings.
    /// </summary>
    public class Connection : Form
    {
        public const int DefaultProxyPort = 3128;

        public enum ProtocolType
        {
            Ssh = 0,
            Telnet = 1,
            SerialPort = 2,
        }

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        private StatusStrip statusStrip;
        private ToolStripStatusLabel statusLabel;
        private System.Windows.Forms.OpenFileDialog openDialog;
        private FlowLayoutPanel flowLayoutPanel1;
        private Panel panel1;
        private Label label0;
        private ComboBox cbProtocol;
        private GroupBox gbPort;
        private GroupBox gbProxy;
        private TextBox tbProxyPassword;
        private TextBox tbProxyLogin;
        private ComboBox cbProxyType;
        private TextBox tbProxy;
        private Label label10;
        private Label label9;
        private Label label8;
        private TextBox tbProxyPort;
        private Label label6;
        private CheckBox cbProxy;
        private Label label5;
        private Label label7;
        private GroupBox gbConnection;
        private TextBox tbPort;
        private Label label2;
        private TextBox tbHost;
        private Label label1;
        private Button btnPrivateKey;
        private TextBox tbPrivateKey;
        private Label label11;
        private TextBox tbPassword;
        private Label label4;
        private TextBox tbLogin;
        private Label label3;
        private Button btnConnect;
        private TextBox tbBaudRate;
        private Label label12;
        private TextBox tbPortName;
        private Label label13;

        #region Properties

        /// <summary>
        /// Gets the value indicating whether the form was confirmed.
        /// </summary>
        public bool OK
        {
            get { return _ok; }
        }
        private bool _ok = false;

        /// <summary>
        /// Gets the protocol type.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ProtocolType Protocol
        {
            get 
            {				
                return (ProtocolType)cbProtocol.SelectedIndex; 
            
            }
            set 
            {
                int index = (int)value;
                if (index >= cbProtocol.Items.Count) index = 0;
                cbProtocol.SelectedIndex = index;
            }
        }

        /// <summary>
        /// Gets the host name.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string HostName
        {
            get { return tbHost.Text; }
            set { tbHost.Text = value; }
        }

        /// <summary>
        /// Gets the port of the host.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int Port
        {
            get
            {
                int port = 0;

                try { port = Int32.Parse(tbPort.Text); }
                catch { }

                return port;
            }
            set { tbPort.Text = value.ToString(); }
        }

        /// <summary>
        /// Gets the port name.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string PortName
        {
            get { return tbPortName.Text; }
            set { tbPortName.Text = value; }
        }

        /// <summary>
        /// Gets the baud rate.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int BaudRate
        {
            get
            {
                int baudRate = 0;

                try { baudRate = Int32.Parse(tbBaudRate.Text); }
                catch { }

                return baudRate;
            }
            set { tbBaudRate.Text = value.ToString(); }
        }

        /// <summary>
        /// Gets the login name.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string LoginName
        {
            get { return tbLogin.Text; }
            set { tbLogin.Text = value; }
        }

        /// <summary>
        /// Gets the password.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string Password
        {
            get { return tbPassword.Text; }
            set { tbPassword.Text = value; }
        }

        /// <summary>
        /// Gets the file location of the private key.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string PrivateKey
        {
            get { return tbPrivateKey.Text; }
            set { tbPrivateKey.Text = value; }
        }

        /// <summary>
        /// Gets the value indicating whether the proxy is enabled.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool ProxyEnabled
        {
            set
            {
                if (value)
                {
                    tbProxy.Enabled = true;
                    tbProxyPort.Enabled = true;
                    cbProxy.Checked = true;
                    cbProxyType.Enabled = true;
                    tbProxyLogin.Enabled = true;
                    tbProxyPassword.Enabled = true;
                }
                else
                {
                    tbProxy.Enabled = false;
                    tbProxyPort.Enabled = false;
                    cbProxy.Checked = false;
                    cbProxyType.Enabled = false;
                    tbProxyLogin.Enabled = false;
                    tbProxyPassword.Enabled = false;
                }
            }

            get { return cbProxy.Checked; }
        }

        /// <summary>
        /// Gets the proxy server name.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string ProxyHostName
        {
            set { tbProxy.Text = value; }
            get { return tbProxy.Text; }
        }

        /// <summary>
        /// Gets the port of the proxy server.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int ProxyPort
        {
            set { tbProxyPort.Text = value.ToString(); }
            get
            {
                int proxyPort = 0;

                try { proxyPort = Int32.Parse(tbProxyPort.Text); }
                catch { }

                return proxyPort;
            }
        }

        /// <summary>
        /// Gets the proxy login name.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string ProxyLoginName
        {
            set { tbProxyLogin.Text = value; }
            get { return tbProxyLogin.Text; }
        }

        /// <summary>
        /// Gets the proxy password.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string ProxyPassword
        {
            set { tbProxyPassword.Text = value; }
            get { return tbProxyPassword.Text; }
        }

        /// <summary>
        /// Gets the proxy type.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ProxyType ProxyType
        {
            get
            {
                switch (cbProxyType.SelectedIndex)
                {
                    case 1:
                        return ProxyType.Socks4a;
                    case 2:
                        return ProxyType.Socks5;
                    case 3:
                        return ProxyType.HttpConnect;
                    default:
                        return ProxyType.Socks4;
                }
            }
            set
            {
                switch (value)
                {
                    case ProxyType.Socks4a:
                        cbProxyType.SelectedIndex = 1; break;
                    case ProxyType.Socks5:
                        cbProxyType.SelectedIndex = 2; break;
                    case ProxyType.HttpConnect:
                        cbProxyType.SelectedIndex = 3; break;
                    default:
                        cbProxyType.SelectedIndex = 0; break;
                }
            }
        }

        #endregion

        /// <summary>
        /// Initialize this class.
        /// </summary>
        public Connection()
        {
            InitializeComponent();
            cbProtocol.Items.RemoveAt(2);

            // set default values
            tbPort.Text = Ssh.DefaultPort.ToString();
            tbProxyPort.Text = DefaultProxyPort.ToString();

            // set form values according to the config file
            LoadConfig();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.statusStrip = new System.Windows.Forms.StatusStrip();
            this.statusLabel = new System.Windows.Forms.ToolStripStatusLabel();
            this.openDialog = new System.Windows.Forms.OpenFileDialog();
            this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
            this.panel1 = new System.Windows.Forms.Panel();
            this.label0 = new System.Windows.Forms.Label();
            this.cbProtocol = new System.Windows.Forms.ComboBox();
            this.gbPort = new System.Windows.Forms.GroupBox();
            this.gbProxy = new System.Windows.Forms.GroupBox();
            this.tbProxyPassword = new System.Windows.Forms.TextBox();
            this.tbProxyLogin = new System.Windows.Forms.TextBox();
            this.cbProxyType = new System.Windows.Forms.ComboBox();
            this.tbProxy = new System.Windows.Forms.TextBox();
            this.label10 = new System.Windows.Forms.Label();
            this.label9 = new System.Windows.Forms.Label();
            this.label8 = new System.Windows.Forms.Label();
            this.tbProxyPort = new System.Windows.Forms.TextBox();
            this.label6 = new System.Windows.Forms.Label();
            this.cbProxy = new System.Windows.Forms.CheckBox();
            this.label5 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.gbConnection = new System.Windows.Forms.GroupBox();
            this.tbPort = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.tbHost = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.btnPrivateKey = new System.Windows.Forms.Button();
            this.tbPrivateKey = new System.Windows.Forms.TextBox();
            this.label11 = new System.Windows.Forms.Label();
            this.tbPassword = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.tbLogin = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.btnConnect = new System.Windows.Forms.Button();
            this.tbBaudRate = new System.Windows.Forms.TextBox();
            this.label12 = new System.Windows.Forms.Label();
            this.tbPortName = new System.Windows.Forms.TextBox();
            this.label13 = new System.Windows.Forms.Label();
            this.flowLayoutPanel1.SuspendLayout();
            this.panel1.SuspendLayout();
            this.gbPort.SuspendLayout();
            this.gbProxy.SuspendLayout();
            this.gbConnection.SuspendLayout();
            this.statusStrip.SuspendLayout();
            this.SuspendLayout();
            // 
            // statusStrip
            // 
            this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.statusLabel});
            this.statusStrip.Location = new System.Drawing.Point(0, 375);
            this.statusStrip.Name = "statusStrip";
            this.statusStrip.Size = new System.Drawing.Size(351, 22);
            this.statusStrip.SizingGrip = false;
            this.statusStrip.TabIndex = 24;
            // 
            // statusLabel
            // 
            this.statusLabel.Name = "statusLabel";
            this.statusLabel.Size = new System.Drawing.Size(90, 21);
            // 
            // openDialog
            // 
            this.openDialog.Filter = "Private key (*.pri)|*.pri|Putty private key (*.ppk)|*.ppk|All files (*.*)|*.*";
            // 
            // flowLayoutPanel1
            // 
            this.flowLayoutPanel1.AutoSize = true;
            this.flowLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.flowLayoutPanel1.Controls.Add(this.panel1);
            this.flowLayoutPanel1.Controls.Add(this.gbConnection);
            this.flowLayoutPanel1.Controls.Add(this.gbProxy);
            this.flowLayoutPanel1.Controls.Add(this.gbPort);
            this.flowLayoutPanel1.Controls.Add(this.btnConnect);
            this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.flowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;
            this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.flowLayoutPanel1.MaximumSize = new System.Drawing.Size(351, 0);
            this.flowLayoutPanel1.MinimumSize = new System.Drawing.Size(351, 0);
            this.flowLayoutPanel1.Name = "flowLayoutPanel1";
            this.flowLayoutPanel1.Size = new System.Drawing.Size(351, 375);
            this.flowLayoutPanel1.TabIndex = 30;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.label0);
            this.panel1.Controls.Add(this.cbProtocol);
            this.panel1.Location = new System.Drawing.Point(6, 3);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(342, 35);
            this.panel1.TabIndex = 0;
            // 
            // label0
            // 
            this.label0.Location = new System.Drawing.Point(6, 12);
            this.label0.Name = "label0";
            this.label0.Size = new System.Drawing.Size(81, 18);
            this.label0.TabIndex = 29;
            this.label0.Text = "Protocol:";
            // 
            // cbProtocol
            // 
            this.cbProtocol.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbProtocol.Items.AddRange(new object[] {
            "SSH",
            "Telnet",
            "Serial Port"});
            this.cbProtocol.Location = new System.Drawing.Point(88, 9);
            this.cbProtocol.Name = "cbProtocol";
            this.cbProtocol.Size = new System.Drawing.Size(88, 21);
            this.cbProtocol.TabIndex = 28;
            this.cbProtocol.SelectedIndexChanged += new System.EventHandler(this.cbProtocol_SelectedIndexChanged);
            // 
            // gbPort
            // 
            this.gbPort.Controls.Add(this.tbBaudRate);
            this.gbPort.Controls.Add(this.label12);
            this.gbPort.Controls.Add(this.tbPortName);
            this.gbPort.Controls.Add(this.label13);
            this.gbPort.Location = new System.Drawing.Point(6, 288);
            this.gbPort.Name = "gbPort";
            this.gbPort.Size = new System.Drawing.Size(342, 50);
            this.gbPort.TabIndex = 1;
            this.gbPort.TabStop = false;
            this.gbPort.Text = "Port settings";
            // 
            // gbProxy
            // 
            this.gbProxy.Controls.Add(this.tbProxyPassword);
            this.gbProxy.Controls.Add(this.tbProxyLogin);
            this.gbProxy.Controls.Add(this.cbProxyType);
            this.gbProxy.Controls.Add(this.tbProxy);
            this.gbProxy.Controls.Add(this.label10);
            this.gbProxy.Controls.Add(this.label9);
            this.gbProxy.Controls.Add(this.label8);
            this.gbProxy.Controls.Add(this.tbProxyPort);
            this.gbProxy.Controls.Add(this.label6);
            this.gbProxy.Controls.Add(this.cbProxy);
            this.gbProxy.Controls.Add(this.label5);
            this.gbProxy.Controls.Add(this.label7);
            this.gbProxy.Location = new System.Drawing.Point(6, 154);
            this.gbProxy.Name = "gbProxy";
            this.gbProxy.Size = new System.Drawing.Size(342, 128);
            this.gbProxy.TabIndex = 3;
            this.gbProxy.TabStop = false;
            this.gbProxy.Text = "Proxy settings";
            // 
            // tbProxyPassword
            // 
            this.tbProxyPassword.Location = new System.Drawing.Point(88, 96);
            this.tbProxyPassword.Name = "tbProxyPassword";
            this.tbProxyPassword.PasswordChar = '*';
            this.tbProxyPassword.Size = new System.Drawing.Size(88, 20);
            this.tbProxyPassword.TabIndex = 29;
            // 
            // tbProxyLogin
            // 
            this.tbProxyLogin.Location = new System.Drawing.Point(88, 72);
            this.tbProxyLogin.Name = "tbProxyLogin";
            this.tbProxyLogin.Size = new System.Drawing.Size(88, 20);
            this.tbProxyLogin.TabIndex = 27;
            // 
            // cbProxyType
            // 
            this.cbProxyType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbProxyType.Items.AddRange(new object[] {
            "Socks4",
            "Socks4a",
            "Socks5",
            "HttpConnect"});
            this.cbProxyType.Location = new System.Drawing.Point(88, 48);
            this.cbProxyType.Name = "cbProxyType";
            this.cbProxyType.Size = new System.Drawing.Size(88, 21);
            this.cbProxyType.TabIndex = 24;
            // 
            // tbProxy
            // 
            this.tbProxy.Location = new System.Drawing.Point(88, 24);
            this.tbProxy.Name = "tbProxy";
            this.tbProxy.Size = new System.Drawing.Size(88, 20);
            this.tbProxy.TabIndex = 20;
            // 
            // label10
            // 
            this.label10.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.label10.ForeColor = System.Drawing.SystemColors.ControlDarkDark;
            this.label10.Location = new System.Drawing.Point(192, 72);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(133, 48);
            this.label10.TabIndex = 30;
            this.label10.Text = "Leave login/password fields empty when not needed.";
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(13, 96);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(75, 23);
            this.label9.TabIndex = 28;
            this.label9.Text = "Password:";
            // 
            // label8
            // 
            this.label8.Location = new System.Drawing.Point(13, 72);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(75, 23);
            this.label8.TabIndex = 26;
            this.label8.Text = "Login name:";
            // 
            // tbProxyPort
            // 
            this.tbProxyPort.Location = new System.Drawing.Point(248, 24);
            this.tbProxyPort.Name = "tbProxyPort";
            this.tbProxyPort.Size = new System.Drawing.Size(88, 20);
            this.tbProxyPort.TabIndex = 23;
            // 
            // label6
            // 
            this.label6.Location = new System.Drawing.Point(192, 24);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(46, 23);
            this.label6.TabIndex = 22;
            this.label6.Text = "Port:";
            // 
            // cbProxy
            // 
            this.cbProxy.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.cbProxy.Location = new System.Drawing.Point(248, 48);
            this.cbProxy.Name = "cbProxy";
            this.cbProxy.Size = new System.Drawing.Size(73, 24);
            this.cbProxy.TabIndex = 22;
            this.cbProxy.Text = "Use proxy";
            this.cbProxy.Click += new System.EventHandler(this.cbProxy_Click);
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(13, 24);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(75, 23);
            this.label5.TabIndex = 19;
            this.label5.Text = "Proxy:";
            // 
            // label7
            // 
            this.label7.Location = new System.Drawing.Point(13, 48);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(75, 23);
            this.label7.TabIndex = 25;
            this.label7.Text = "Proxy type:";
            // 
            // gbConnection
            // 
            this.gbConnection.Controls.Add(this.tbPort);
            this.gbConnection.Controls.Add(this.label2);
            this.gbConnection.Controls.Add(this.tbHost);
            this.gbConnection.Controls.Add(this.label1);
            this.gbConnection.Controls.Add(this.btnPrivateKey);
            this.gbConnection.Controls.Add(this.tbPrivateKey);
            this.gbConnection.Controls.Add(this.label11);
            this.gbConnection.Controls.Add(this.tbPassword);
            this.gbConnection.Controls.Add(this.label4);
            this.gbConnection.Controls.Add(this.tbLogin);
            this.gbConnection.Controls.Add(this.label3);
            this.gbConnection.Location = new System.Drawing.Point(6, 44);
            this.gbConnection.Name = "gbConnection";
            this.gbConnection.Size = new System.Drawing.Size(342, 104);
            this.gbConnection.TabIndex = 2;
            this.gbConnection.TabStop = false;
            this.gbConnection.Text = "Connection settings";
            // 
            // tbPort
            // 
            this.tbPort.Location = new System.Drawing.Point(248, 24);
            this.tbPort.Name = "tbPort";
            this.tbPort.Size = new System.Drawing.Size(88, 20);
            this.tbPort.TabIndex = 13;
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(184, 24);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(64, 23);
            this.label2.TabIndex = 12;
            this.label2.Text = "Port:";
            // 
            // tbHost
            // 
            this.tbHost.Location = new System.Drawing.Point(88, 24);
            this.tbHost.Name = "tbHost";
            this.tbHost.Size = new System.Drawing.Size(88, 20);
            this.tbHost.TabIndex = 11;
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(7, 24);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(81, 23);
            this.label1.TabIndex = 10;
            this.label1.Text = "Host name:";
            // 
            // btnPrivateKey
            // 
            this.btnPrivateKey.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnPrivateKey.Location = new System.Drawing.Point(294, 72);
            this.btnPrivateKey.Name = "btnPrivateKey";
            this.btnPrivateKey.Size = new System.Drawing.Size(42, 20);
            this.btnPrivateKey.TabIndex = 20;
            this.btnPrivateKey.Text = "...";
            this.btnPrivateKey.Click += new System.EventHandler(this.btnPrivateKey_Click);
            // 
            // tbPrivateKey
            // 
            this.tbPrivateKey.Location = new System.Drawing.Point(88, 72);
            this.tbPrivateKey.Name = "tbPrivateKey";
            this.tbPrivateKey.Size = new System.Drawing.Size(200, 20);
            this.tbPrivateKey.TabIndex = 19;
            // 
            // label11
            // 
            this.label11.Location = new System.Drawing.Point(7, 72);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(81, 23);
            this.label11.TabIndex = 18;
            this.label11.Text = "Private key:";
            // 
            // tbPassword
            // 
            this.tbPassword.Location = new System.Drawing.Point(248, 48);
            this.tbPassword.Name = "tbPassword";
            this.tbPassword.PasswordChar = '*';
            this.tbPassword.Size = new System.Drawing.Size(88, 20);
            this.tbPassword.TabIndex = 17;
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(184, 48);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(64, 23);
            this.label4.TabIndex = 16;
            this.label4.Text = "Password:";
            // 
            // tbLogin
            // 
            this.tbLogin.Location = new System.Drawing.Point(88, 48);
            this.tbLogin.Name = "tbLogin";
            this.tbLogin.Size = new System.Drawing.Size(88, 20);
            this.tbLogin.TabIndex = 15;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(7, 48);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(81, 23);
            this.label3.TabIndex = 14;
            this.label3.Text = "Login name:";
            // 
            // btnConnect
            // 
            this.btnConnect.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnConnect.Location = new System.Drawing.Point(275, 344);
            this.btnConnect.Name = "btnConnect";
            this.btnConnect.Size = new System.Drawing.Size(73, 23);
            this.btnConnect.TabIndex = 4;
            this.btnConnect.Text = "Connect";
            this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
            // 
            // tbBaudRate
            // 
            this.tbBaudRate.Location = new System.Drawing.Point(248, 19);
            this.tbBaudRate.Name = "tbBaudRate";
            this.tbBaudRate.Size = new System.Drawing.Size(88, 20);
            this.tbBaudRate.TabIndex = 17;
            this.tbBaudRate.Text = "9600";
            // 
            // label12
            // 
            this.label12.Location = new System.Drawing.Point(184, 19);
            this.label12.Name = "label12";
            this.label12.Size = new System.Drawing.Size(64, 23);
            this.label12.TabIndex = 16;
            this.label12.Text = "Baud rate:";
            // 
            // tbPortName
            // 
            this.tbPortName.Location = new System.Drawing.Point(88, 19);
            this.tbPortName.Name = "tbPortName";
            this.tbPortName.Size = new System.Drawing.Size(88, 20);
            this.tbPortName.TabIndex = 15;
            // 
            // label13
            // 
            this.label13.Location = new System.Drawing.Point(7, 19);
            this.label13.Name = "label13";
            this.label13.Size = new System.Drawing.Size(81, 23);
            this.label13.TabIndex = 14;
            this.label13.Text = "Port name:";
            // 
            // Connection
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.AutoSize = true;
            this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.ClientSize = new System.Drawing.Size(351, 397);
            this.Controls.Add(this.flowLayoutPanel1);
            this.Controls.Add(this.statusStrip);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.KeyPreview = true;
            this.Name = "Connection";
            this.ShowInTaskbar = false;
            this.Text = "Connection";
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Connection_KeyDown);
            this.flowLayoutPanel1.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.gbPort.ResumeLayout(false);
            this.gbPort.PerformLayout();
            this.gbProxy.ResumeLayout(false);
            this.gbProxy.PerformLayout();
            this.gbConnection.ResumeLayout(false);
            this.gbConnection.PerformLayout();
            this.statusStrip.ResumeLayout(false);
            this.statusStrip.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        /// <summary>
        /// Validates the from values.
        /// </summary>
        /// <returns>true (everything is ok)</returns>
        public bool ValidateForm()
        {
            if (Protocol == ProtocolType.SerialPort)
            {
                // port
                if (tbPortName.Text == null || tbPortName.Text.Length <= 0)
                {
                    statusLabel.Text = "Port name is a required field";
                    return false;
                }

                // baud rate
                if (this.BaudRate <= 0)
                {
                    statusLabel.Text = "Baud rate must be greater than 0";
                    return false;
                }
            }
            else
            {
                // host
                if (tbHost.Text == null || tbHost.Text.Length <= 0)
                {
                    statusLabel.Text = "Host name is a required field";
                    return false;
                }

                // port
                if (tbPort.Text == null || tbPort.Text.Length <= 0)
                {
                    statusLabel.Text = "Host port is a required field";
                    return false;
                }

                // port
                if (this.Port <= 0)
                {
                    statusLabel.Text = "Host port must be greater than 0";
                    return false;
                }

                // login
                if (tbLogin.Text == null || tbLogin.Text.Length <= 0)
                {
                    statusLabel.Text = "Host login is a required field";
                    return false;
                }

                // password
                if ((tbPassword.Text == null || tbPassword.Text.Length <= 0) &&
                    (tbPrivateKey.Text == null || tbPrivateKey.Text.Length <= 0))
                {
                    statusLabel.Text = "Host password is a required field";
                    return false;
                }

                // ----------------
                // proxy check-outs
                // ----------------
                if (cbProxy.Checked)
                {
                    // proxy host
                    if (tbProxy.Text == null || tbProxy.Text.Length <= 0)
                    {
                        statusLabel.Text = "Proxy host is a required field when proxy is enabled";
                        return false;
                    }

                    // proxy port
                    if (tbProxyPort.Text == null || tbProxyPort.Text.Length <= 0)
                    {
                        statusLabel.Text = "Proxy port is a required field when proxy is enabled";
                        return false;
                    }

                    // proxy port
                    if (this.ProxyPort <= 0)
                    {
                        statusLabel.Text = "Proxy port must be number greater than 0";
                        return false;
                    }
                }
            }

            return true;
        }

        /// <summary>
        /// Sets the form values according to the config file.
        /// </summary>
        private void LoadConfig()
        {
            this.Protocol = (ProtocolType)Settings.Default.GetValue("Protocol", typeof(ProtocolType));
            this.HostName = Settings.Default.GetString("HostName");
            this.Port = Settings.Default.GetInt32("Port", this.Protocol == ProtocolType.Ssh ? Ssh.DefaultPort : Telnet.DefaultPort);
            this.LoginName = Settings.Default.GetString("Login");
            this.Password = Settings.Default.GetString("Password");
            this.PrivateKey = Settings.Default.GetString("PrivateKey");
            this.ProxyEnabled = Settings.Default.GetBoolean("ProxyEnabled", false);
            this.ProxyHostName = Settings.Default.GetString("ProxyHostName");
            this.ProxyPort = Settings.Default.GetInt32("ProxyPort", DefaultProxyPort);
            this.ProxyType = (ProxyType)Settings.Default.GetValue("ProxyType", typeof(ProxyType));
            this.ProxyLoginName = Settings.Default.GetString("ProxyLogin");
            this.ProxyPassword = Settings.Default.GetString("ProxyPassword");
            this.PortName = Settings.Default.GetString("PortName");
            this.BaudRate = Settings.Default.GetInt32("BaudRate", 9600);
        }

        /// <summary>
        /// Save the form values into the config file.
        /// </summary>
        private void SaveConfig()
        {
            Settings.Default.SetValue("Protocol", this.Protocol);
            Settings.Default.SetValue("HostName", this.HostName);
            Settings.Default.SetValue("Port", this.Port);
            Settings.Default.SetValue("Login", this.LoginName);
            Settings.Default.SetValue("Password", this.Password);
            Settings.Default.SetValue("PrivateKey", this.PrivateKey);
            Settings.Default.SetValue("ProxyEnabled", this.ProxyEnabled);
            Settings.Default.SetValue("ProxyHostName", this.ProxyHostName);
            Settings.Default.SetValue("ProxyPort", this.ProxyPort);
            Settings.Default.SetValue("ProxyType", this.ProxyType);
            Settings.Default.SetValue("ProxyLogin", this.ProxyLoginName);
            Settings.Default.SetValue("ProxyPassword", this.ProxyPassword);
            Settings.Default.SetValue("PortName", this.PortName);
            Settings.Default.SetValue("BaudRate", this.BaudRate);

            Settings.Default.Save();
        }

        /// <summary>
        /// Handles the button 'Connect' click event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConnect_Click(object sender, System.EventArgs e)
        {
            // if the form is valid
            if (ValidateForm())
            {
                // save the config and submit the form
                _ok = true;
                SaveConfig();
                this.Close();
            }
        }

        /// <summary>
        /// Handles the checkbox 'Proxy' click event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbProxy_Click(object sender, System.EventArgs e)
        {
            // get the checkbox by casting from sender object
            CheckBox cb = sender as CheckBox;

            // if sender wasn't checkbox, return
            if (cb == null)
                return;

            // enable / disable Proxy section according to check box state
            if (cb.Checked)
            {
                tbProxy.Enabled = true;
                tbProxyPort.Enabled = true;
                cbProxy.Checked = true;
                cbProxyType.Enabled = true;
                tbProxyLogin.Enabled = true;
                tbProxyPassword.Enabled = true;
            }
            else
            {
                tbProxy.Enabled = false;
                tbProxyPort.Enabled = false;
                cbProxy.Checked = false;
                cbProxyType.Enabled = false;
                tbProxyLogin.Enabled = false;
                tbProxyPassword.Enabled = false;
            }
        }

        /// <summary>
        /// Handles the button 'PrivateKey' click event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrivateKey_Click(object sender, System.EventArgs e)
        {
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                tbPrivateKey.Text = openDialog.FileName;
            }
        }

        /// <summary>
        /// Handles 'Protocol' selection change.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbProtocol_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool isSsh = false;
            bool isSerial = false;
            switch (Protocol)
            {
                case ProtocolType.Ssh:
                    isSsh = true;
                    if (Port == Telnet.DefaultPort)
                        Port = Ssh.DefaultPort;
                    break;
                case ProtocolType.Telnet:
                    if (Port == Ssh.DefaultPort)
                        Port = Telnet.DefaultPort;
                    break;
                case ProtocolType.SerialPort:
                    isSerial = true;
                    break;
            }

            tbLogin.Enabled = isSsh;
            tbPassword.Enabled = isSsh;
            tbPrivateKey.Enabled = isSsh;
            btnPrivateKey.Enabled = isSsh;

            gbPort.Visible= isSerial;
            gbConnection.Visible = !isSerial;
            gbProxy.Visible = !isSerial;			
        }

        /// <summary>
        /// Handles the form keydown event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Connection_KeyDown(object sender, KeyEventArgs e)
        {
            // when 'enter' is pressed the form is submited
            if (e.KeyCode == Keys.Enter)
            {
                if (ValidateForm())
                {
                    _ok = true;
                    SaveConfig();
                    this.Close();
                }
            }

            // when 'esc' is pressed the form is closed without submiting
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }
    }
}