<commit_before>﻿using AutoIt;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace Autoit_CiscoVPN
{
    class AutoLogin : AppConfig
    {
        int waitTime = 10;
        string messageBoxTitle = "CiscoVPN_Autoit MSG";
        Stopwatch stopwatch;

        #region Cisco_MainWindow handles
        string domainWindowTitle = "Cisco AnyConnect Secure Mobility Client";
        string domainTextbox = "[CLASS:Edit; INSTANCE:1]";
        string domainDropdown = "[CLASS:ComboBox; INSTANCE:1]";
        string connectButton = "[CLASS:Button; TEXT:Connect; INSTANCE:1]";
        string disconnectButton = "[CLASS:Button; TEXT:Disconnect; INSTANCE:1]";
        #endregion

        #region Cisco_loginWindow handles
        string loginWindowTitle = "Cisco AnyConnect | " + domainName;
        string groupDropdown = "[CLASS:ComboBox; INSTANCE:1]";
        string usernameTextbox = "[CLASS:Edit; INSTANCE:1]";
        string passwordTextbox = "[CLASS:Edit; INSTANCE:2]";
        string okButton = "[CLASS:Button; TEXT:OK; INSTANCE:1]";
        #endregion

        #region Cisco_TermsandCondition handles
        string termsandConditonWindowTitle = "Cisco AnyConnect";
        string acceptButton = "[CLASS:Button; TEXT:Accept; INSTANCE:1]";
        #endregion

        #region Common methods
        private void ShowErrorPopup(string errorMsg)
        {
            errorMsg += "\n\nScript will STOP now. Verify the error and restart the script.";
            DialogResult dialog = MessageBox.Show(errorMsg, messageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);

            if (dialog == DialogResult.OK)  //Close console window after clicking ok button
                Environment.Exit(0);
        }

        private void WaitForWindow(string windowID, int waitTime = 10)
        {
            AutoItX.AutoItSetOption("WinTitleMatchMode", 3);
            var handle = AutoItX.WinWaitActive(windowID, "", waitTime);

            if(windowID != termsandConditonWindowTitle) //Dont show errorpopup if there is no terms and condition popup
            {
                if (AutoItX.WinExists(windowID) == 0) //Autoit return 1 for success and 0 for failure
                    ShowErrorPopup($"'{windowID}' : unable to locate this window title");
            }
            
        }

        private void CheckControlVisibility(string windowTitle, string controlID)
        {
            var isControlVisible = AutoItX.ControlCommand(windowTitle, "", controlID, "IsVisible", "");

            if(controlID == disconnectButton)
            {
                if (Int16.Parse(isControlVisible) == 1) //Autoit will return 1 if the control is visible
                {
                    //AutoItX.ControlClick(domainWindowTitle, "", disconnectButton);   //Uncomment this line if you need disconnect vpn if its already connected
                    ShowErrorPopup($"Cisco VPN is ALREADY CONNECTED.");
                }
            }
            else
            {
                if (Int16.Parse(isControlVisible) == 0) //Autoit will return 1 if the control is visible
                    ShowErrorPopup($"CONTROL NOT VISIBLE\nwindowtitle={windowTitle}\ncontrolID={controlID}");
            }
        }

        private void WaitTillControlEnabled(string windowTitle, string controlID)
        {
            stopwatch = new Stopwatch();
            stopwatch.Start();

            //wait till the control enable and also wait upto the waittime declared
            while (Int16.Parse(AutoItX.ControlCommand(windowTitle, "", controlID, "IsEnabled", "")) == 0 && stopwatch.Elapsed.Seconds <= waitTime)
            {
                AutoItX.Sleep(100);
            }
            stopwatch.Stop();
            AutoItX.Sleep(500); //Added this sleep for stability
        }
        #endregion


        public AutoLogin()
        {<commit_msg>Script will auto disconnect if VPN is already connected<commit_after>﻿using AutoIt;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace Autoit_CiscoVPN
{
    class AutoLogin : AppConfig
    {
        int waitTime = 10;
        string messageBoxTitle = "CiscoVPN_Autoit MSG";
        Stopwatch stopwatch;

        #region Cisco_MainWindow handles
        string domainWindowTitle = "Cisco AnyConnect Secure Mobility Client";
        string domainTextbox = "[CLASS:Edit; INSTANCE:1]";
        string domainDropdown = "[CLASS:ComboBox; INSTANCE:1]";
        string connectButton = "[CLASS:Button; TEXT:Connect; INSTANCE:1]";
        string disconnectButton = "[CLASS:Button; TEXT:Disconnect; INSTANCE:1]";
        #endregion

        #region Cisco_loginWindow handles
        string loginWindowTitle = "Cisco AnyConnect | " + domainName;
        string groupDropdown = "[CLASS:ComboBox; INSTANCE:1]";
        string usernameTextbox = "[CLASS:Edit; INSTANCE:1]";
        string passwordTextbox = "[CLASS:Edit; INSTANCE:2]";
        string okButton = "[CLASS:Button; TEXT:OK; INSTANCE:1]";
        #endregion

        #region Cisco_TermsandCondition handles
        string termsandConditonWindowTitle = "Cisco AnyConnect";
        string acceptButton = "[CLASS:Button; TEXT:Accept; INSTANCE:1]";
        #endregion

        #region Common methods
        private void ShowErrorPopup(string errorMsg)
        {
            errorMsg += "\n\nScript will STOP now. Verify the error and restart the script.";
            DialogResult dialog = MessageBox.Show(errorMsg, messageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);

            if (dialog == DialogResult.OK)  //Close console window after clicking ok button
                Environment.Exit(0);
        }

        private void WaitForWindow(string windowID, int waitTime = 10)
        {
            AutoItX.AutoItSetOption("WinTitleMatchMode", 3);
            var handle = AutoItX.WinWaitActive(windowID, "", waitTime);

            if(windowID != termsandConditonWindowTitle) //Dont show errorpopup if there is no terms and condition popup
            {
                if (AutoItX.WinExists(windowID) == 0) //Autoit return 1 for success and 0 for failure
                    ShowErrorPopup($"'{windowID}' : unable to locate this window title");
            }
            
        }

        private void CheckControlVisibility(string windowTitle, string controlID)
        {
            var isControlVisible = AutoItX.ControlCommand(windowTitle, "", controlID, "IsVisible", "");

            if(controlID == disconnectButton)
            {
                if (Int16.Parse(isControlVisible) == 1) //Autoit will return 1 if the control is visible
                {
                    AutoItX.ControlClick(domainWindowTitle, "", disconnectButton); //Uncomment this line if you need disconnect vpn if its already connected
                    Environment.Exit(0); 
                    //ShowErrorPopup($"Cisco VPN is ALREADY CONNECTED.");
                }
            }
            else
            {
                if (Int16.Parse(isControlVisible) == 0) //Autoit will return 1 if the control is visible
                    ShowErrorPopup($"CONTROL NOT VISIBLE\nwindowtitle={windowTitle}\ncontrolID={controlID}");
            }
        }

        private void WaitTillControlEnabled(string windowTitle, string controlID)
        {
            stopwatch = new Stopwatch();
            stopwatch.Start();

            //wait till the control enable and also wait upto the waittime declared
            while (Int16.Parse(AutoItX.ControlCommand(windowTitle, "", controlID, "IsEnabled", "")) == 0 && stopwatch.Elapsed.Seconds <= waitTime)
            {
                AutoItX.Sleep(100);
            }
            stopwatch.Stop();
            AutoItX.Sleep(500); //Added this sleep for stability
        }
        #endregion


        public AutoLogin()
        {
{'fraction_non_alphanumeric': 0.09204487506374298, 'fraction_numerical': 0.009051504334523203, 'mean_word_length': 2.2714762301918263, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4093462', 'n_tokens_mistral': 2118, 'n_tokens_neox': 2011, 'n_words': 555}