//  
//   Rebex Sample Code License
// 
//   Copyright 2026, 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 const string DefaultHostName = "test.rebex.net";
        public const string DefaultUserName = "demo";
        public const string DefaultPassword = "password";

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

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

        #region Properties

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

        private bool _enableTelnetLoginInfo;

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool EnableTelnetLoginInfo 
        {
            get { return _enableTelnetLoginInfo; }
            set 
            { 
                _enableTelnetLoginInfo = value;
                tbLogin.Enabled |= _enableTelnetLoginInfo;
                tbPassword.Enabled |= _enableTelnetLoginInfo;
            }
        }

        /// <summary>
        /// Gets the protocol type.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ProtocolType Protocol
        {
            get { return cbProtocol.SelectedIndex == 0 ? ProtocolType.Ssh : ProtocolType.Telnet; }
            set { cbProtocol.SelectedIndex = value == ProtocolType.Ssh ? 0 : 1; }
        }

        /// <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 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();

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

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

        /// <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.btnConnect = new System.Windows.Forms.Button();
            this.groupBox2 = 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.groupBox1 = 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.statusStrip = new System.Windows.Forms.StatusStrip();
            this.statusLabel = new System.Windows.Forms.ToolStripStatusLabel();
            this.openDialog = new System.Windows.Forms.OpenFileDialog();
            this.cbProtocol = new System.Windows.Forms.ComboBox();
            this.label0 = new System.Windows.Forms.Label();
            this.groupBox2.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.statusStrip.SuspendLayout();
            this.SuspendLayout();
            // 
            // btnConnect
            // 
            this.btnConnect.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnConnect.Location = new System.Drawing.Point(280, 288);
            this.btnConnect.Name = "btnConnect";
            this.btnConnect.Size = new System.Drawing.Size(73, 23);
            this.btnConnect.TabIndex = 23;
            this.btnConnect.Text = "Connect";
            this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.tbProxyPassword);
            this.groupBox2.Controls.Add(this.tbProxyLogin);
            this.groupBox2.Controls.Add(this.cbProxyType);
            this.groupBox2.Controls.Add(this.tbProxy);
            this.groupBox2.Controls.Add(this.label10);
            this.groupBox2.Controls.Add(this.label9);
            this.groupBox2.Controls.Add(this.label8);
            this.groupBox2.Controls.Add(this.tbProxyPort);
            this.groupBox2.Controls.Add(this.label6);
            this.groupBox2.Controls.Add(this.cbProxy);
            this.groupBox2.Controls.Add(this.label5);
            this.groupBox2.Controls.Add(this.label7);
            this.groupBox2.Location = new System.Drawing.Point(10, 148);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(342, 128);
            this.groupBox2.TabIndex = 22;
            this.groupBox2.TabStop = false;
            this.groupBox2.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:";
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.label0);
            this.groupBox1.Controls.Add(this.cbProtocol);
            this.groupBox1.Controls.Add(this.tbPort);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.tbHost);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.btnPrivateKey);
            this.groupBox1.Controls.Add(this.tbPrivateKey);
            this.groupBox1.Controls.Add(this.label11);
            this.groupBox1.Controls.Add(this.tbPassword);
            this.groupBox1.Controls.Add(this.label4);
            this.groupBox1.Controls.Add(this.tbLogin);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Location = new System.Drawing.Point(10, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(342, 128);
            this.groupBox1.TabIndex = 21;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Connection settings";
            // 
            // tbPort
            // 
            this.tbPort.Location = new System.Drawing.Point(248, 46);
            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, 46);
            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, 46);
            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, 46);
            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, 94);
            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, 94);
            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, 94);
            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, 70);
            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, 70);
            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, 70);
            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, 70);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(81, 23);
            this.label3.TabIndex = 14;
            this.label3.Text = "Login name:";
            // 
            // statusStrip
            // 
            this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.statusLabel});
            this.statusStrip.Location = new System.Drawing.Point(0, 321);
            this.statusStrip.Name = "statusStrip";
            this.statusStrip.Size = new System.Drawing.Size(362, 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 (*.*)|*.*";
            // 
            // cbProtocol
            // 
            this.cbProtocol.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbProtocol.Items.AddRange(new object[] {
            "SSH",
            "Telnet"});
            this.cbProtocol.Location = new System.Drawing.Point(88, 19);
            this.cbProtocol.Name = "cbProtocol";
            this.cbProtocol.Size = new System.Drawing.Size(88, 21);
            this.cbProtocol.TabIndex = 26;
            this.cbProtocol.SelectedIndexChanged += new System.EventHandler(this.cbProtocol_SelectedIndexChanged);
            // 
            // label0
            // 
            this.label0.Location = new System.Drawing.Point(7, 19);
            this.label0.Name = "label0";
            this.label0.Size = new System.Drawing.Size(81, 23);
            this.label0.TabIndex = 27;
            this.label0.Text = "Protocol:";
            // 
            // Connection
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(362, 343);
            this.Controls.Add(this.statusStrip);
            this.Controls.Add(this.btnConnect);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            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.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.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()
        {
            // 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 SetFormValues()
        {
            this.Protocol = (ProtocolType)Settings.Default.GetValue("Protocol", typeof(ProtocolType));
            this.HostName = Settings.Default.GetString("HostName", DefaultHostName);
            this.Port = Settings.Default.GetInt32("Port", this.Protocol == ProtocolType.Ssh ? Ssh.DefaultPort : Telnet.DefaultPort);
            this.LoginName = Settings.Default.GetString("Login", DefaultUserName);
            this.Password = Settings.Default.GetString("Password", DefaultPassword);
            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");
        }

        /// <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.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;
            if (Protocol == ProtocolType.Ssh)
            {
                isSsh = true;
                if (Port == Telnet.DefaultPort)
                    Port = Ssh.DefaultPort;
            }
            else
            {
                isSsh = false;
                if (Port == Ssh.DefaultPort)
                    Port = Telnet.DefaultPort;
            }

            tbLogin.Enabled = isSsh | _enableTelnetLoginInfo;
            tbPassword.Enabled = isSsh | _enableTelnetLoginInfo;
            tbPrivateKey.Enabled = isSsh;
            btnPrivateKey.Enabled = isSsh;
        }

        /// <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();
            }
        }

    }
}