<commit_before>﻿using GlobalHotkeys;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.IO.Ports;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

namespace ComConsole
{
    public partial class MainForm : Form
    {
        private ComPort cPort;
        private List<GlobalHotkey> ghList = new List<GlobalHotkey>();

        public MainForm()
        {
            InitializeComponent();

            this.AddComPorts();
            this.AddBitrate();
            this.AddDataBits();
            this.AddStopBits();
            this.AddParity();
            this.AddHandshake();

            this.RevokePreviousSettings();

            this.cPort = new ComPort();
            this.cPort.OnStatusChanged += this.OnStatusChanged;
            this.cPort.OnDataRecieved += this.OnDataRecieved;
            this.FireOpen();

            this.DeserializeHotkeys();
            this.RenewAllHotkeys();
            this.FillListViewWithRenewedHotkeys();
        }

        private void FireOpen()
        {
            string port = comboBoxPort.Text.ToString();
            int rate = Convert.ToInt32(comboBoxRate.Text);
            int databits = Convert.ToInt32(comboBoxDataBits.Text);
            StopBits stopbits = (StopBits)Enum.Parse(typeof(StopBits), comboBoxStopBits.Text);
            Parity parity = (Parity)Enum.Parse(typeof(Parity), comboBoxParity.Text);
            Handshake handshake = (Handshake)Enum.Parse(typeof(Handshake), comboBoxHandshake.Text);

            this.cPort.Open(port, rate, parity, databits, stopbits, handshake);
        }

        private void SavePortInfo()
        {
            Properties.Settings.Default["port"] = this.comboBoxPort.Text.ToString();
            Properties.Settings.Default["rate"] = Convert.ToInt32(this.comboBoxRate.Text);
            Properties.Settings.Default["databits"] = Convert.ToInt32(this.comboBoxDataBits.Text);
            Properties.Settings.Default["stopbits"] = (StopBits)Enum.Parse(typeof(StopBits), this.comboBoxStopBits.Text);
            Properties.Settings.Default["parity"] = (Parity)Enum.Parse(typeof(Parity), this.comboBoxParity.Text);
            Properties.Settings.Default["handshake"] = (Handshake)Enum.Parse(typeof(Handshake), this.comboBoxHandshake.Text);

            Properties.Settings.Default.Save();
        }

        private void RevokePreviousSettings()
        {
            this.comboBoxPort.Text = Properties.Settings.Default.port;
            this.comboBoxRate.Text = Convert.ToString(Properties.Settings.Default.rate);
            this.comboBoxDataBits.Text = Convert.ToString(Properties.Settings.Default.databits);
            this.comboBoxStopBits.Text = Convert.ToString(Properties.Settings.Default.stopbits);
            this.comboBoxParity.Text = Convert.ToString(Properties.Settings.Default.parity);
            this.comboBoxHandshake.Text = Convert.ToString(Properties.Settings.Default.handshake);

            // append to text
            switch (Properties.Settings.Default.append) {
                case (int)AppendToText.CR:
                    this.radioButtonAppendCR.Checked = true; break;
                case (int)AppendToText.LF:
                    this.radioButtonAppendLF.Checked = true; break;
                case (int)AppendToText.CRLF:
                    this.radioButtonAppendCRLF.Checked = true; break;
                default:
                    this.radioButtonAppendNothing.Checked = true; break;
            }
        }


        #region Global hotkeys handling

        protected override void WndProc(ref Message m)
        {
            // we check if the message is about out hotkey
            var hotkeyInfo = HotkeyInfo.GetFromMessage(m);
            if (hotkeyInfo != null) {
                this.HandleHotkey(hotkeyInfo);
            }

            base.WndProc(ref m);
        }

        private void HandleKeyBindAdd()
        {
            int globalHotkeyId;
           
            var key = (Keys)Enum.Parse(typeof(Keys), textBoxKey.Text.ToUpper());
            // check if mod keys are checked
            var mod = Modifiers.NoMod;
            if (this.checkBoxAlt.Checked) { mod = mod | Modifiers.Alt; }
            if (this.checkBoxCtlr.Checked) { mod = mod | Modifiers.Ctrl; }
            if (this.checkBoxShift.Checked) { mod = mod | Modifiers.Shift; }
            if (this.checkBoxWinKey.Checked) { mod = mod | Modifiers.Win; }

            try {
                GlobalHotkey gh = new GlobalHotkey(mod, key, this, true);
                globalHotkeyId = gh.Id;
                gh.command = this.textBoxCommand.Text;
                this.ghList.Add(gh);
            } catch (GlobalHotkeyException e) {
                MessageBox.Show(e.Message);
                return;
            } 

            // when shortcut registered successfully
            ListViewItem listItem = new ListViewItem(String.Format("{0} {1}", mod, key));
            listItem.SubItems.Add(this.textBoxCommand.Text);
            listItem.SubItems.Add(globalHotkeyId.ToString());

            this.listView1.Items.Add(listItem);
        }

        private void HandleKeyBindRemove()
        {
            // it was checked if anything is selected
            // and we know for sure only one item can be selected 
            int globalHotkeyId = Int32.Parse(this.listView1.SelectedItems[0].SubItems[2].Text);
            foreach (GlobalHotkey gh in this.ghList) {
                if (gh.Id.Equals(globalHotkeyId)) {
                    gh.Dispose();
                }
            }
            this.listView1.SelectedItems[0].Remove();
        }

        private void HandleHotkey(HotkeyInfo hotkeyInfo)
        {
            richTextBox1.AppendText(string.Format("{0} : Hotkey Proc! {1}, {2}{3}", DateTime.Now.ToString("hh:MM:ss.fff"),
                                           hotkeyInfo.Key, hotkeyInfo.Modifiers, Environment.NewLine));
        }<commit_msg>Hotkey press is now beeing processed and sent through the port<commit_after>﻿using GlobalHotkeys;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.IO.Ports;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

namespace ComConsole
{
    public partial class MainForm : Form
    {
        private ComPort cPort;
        private List<GlobalHotkey> ghList = new List<GlobalHotkey>();

        public MainForm()
        {
            InitializeComponent();

            this.AddComPorts();
            this.AddBitrate();
            this.AddDataBits();
            this.AddStopBits();
            this.AddParity();
            this.AddHandshake();

            this.RevokePreviousSettings();

            this.cPort = new ComPort();
            this.cPort.OnStatusChanged += this.OnStatusChanged;
            this.cPort.OnDataRecieved += this.OnDataRecieved;
            this.FireOpen();

            this.DeserializeHotkeys();
            this.RenewAllHotkeys();
            this.FillListViewWithRenewedHotkeys();
        }

        private void FireOpen()
        {
            string port = comboBoxPort.Text.ToString();
            int rate = Convert.ToInt32(comboBoxRate.Text);
            int databits = Convert.ToInt32(comboBoxDataBits.Text);
            StopBits stopbits = (StopBits)Enum.Parse(typeof(StopBits), comboBoxStopBits.Text);
            Parity parity = (Parity)Enum.Parse(typeof(Parity), comboBoxParity.Text);
            Handshake handshake = (Handshake)Enum.Parse(typeof(Handshake), comboBoxHandshake.Text);

            this.cPort.Open(port, rate, parity, databits, stopbits, handshake);
        }

        private void SavePortInfo()
        {
            Properties.Settings.Default["port"] = this.comboBoxPort.Text.ToString();
            Properties.Settings.Default["rate"] = Convert.ToInt32(this.comboBoxRate.Text);
            Properties.Settings.Default["databits"] = Convert.ToInt32(this.comboBoxDataBits.Text);
            Properties.Settings.Default["stopbits"] = (StopBits)Enum.Parse(typeof(StopBits), this.comboBoxStopBits.Text);
            Properties.Settings.Default["parity"] = (Parity)Enum.Parse(typeof(Parity), this.comboBoxParity.Text);
            Properties.Settings.Default["handshake"] = (Handshake)Enum.Parse(typeof(Handshake), this.comboBoxHandshake.Text);

            Properties.Settings.Default.Save();
        }

        private void RevokePreviousSettings()
        {
            this.comboBoxPort.Text = Properties.Settings.Default.port;
            this.comboBoxRate.Text = Convert.ToString(Properties.Settings.Default.rate);
            this.comboBoxDataBits.Text = Convert.ToString(Properties.Settings.Default.databits);
            this.comboBoxStopBits.Text = Convert.ToString(Properties.Settings.Default.stopbits);
            this.comboBoxParity.Text = Convert.ToString(Properties.Settings.Default.parity);
            this.comboBoxHandshake.Text = Convert.ToString(Properties.Settings.Default.handshake);

            // append to text
            switch (Properties.Settings.Default.append) {
                case (int)AppendToText.CR:
                    this.radioButtonAppendCR.Checked = true; break;
                case (int)AppendToText.LF:
                    this.radioButtonAppendLF.Checked = true; break;
                case (int)AppendToText.CRLF:
                    this.radioButtonAppendCRLF.Checked = true; break;
                default:
                    this.radioButtonAppendNothing.Checked = true; break;
            }
        }


        #region Global hotkeys handling

        protected override void WndProc(ref Message m)
        {
            // we check if the message is about out hotkey
            var hotkeyInfo = HotkeyInfo.GetFromMessage(m);
            if (hotkeyInfo != null) {
                string command = null;
                foreach (GlobalHotkey gh in this.ghList) {
                    if (gh.Key.Equals((int)hotkeyInfo.Key)) {
                        command = gh.command;
                    }
                }
                this.HandleHotkey(hotkeyInfo, command);
            }

            base.WndProc(ref m);
        }

        private void HandleKeyBindAdd()
        {
            int globalHotkeyId;
           
            var key = (Keys)Enum.Parse(typeof(Keys), textBoxKey.Text.ToUpper());
            // check if mod keys are checked
            var mod = Modifiers.NoMod;
            if (this.checkBoxAlt.Checked) { mod = mod | Modifiers.Alt; }
            if (this.checkBoxCtlr.Checked) { mod = mod | Modifiers.Ctrl; }
            if (this.checkBoxShift.Checked) { mod = mod | Modifiers.Shift; }
            if (this.checkBoxWinKey.Checked) { mod = mod | Modifiers.Win; }

            try {
                GlobalHotkey gh = new GlobalHotkey(mod, key, this, true);
                globalHotkeyId = gh.Id;
                gh.command = this.textBoxCommand.Text;
                this.ghList.Add(gh);
            } catch (GlobalHotkeyException e) {
                MessageBox.Show(e.Message);
                return;
            } 

            // when shortcut registered successfully
            ListViewItem listItem = new ListViewItem(String.Format("{0} {1}", mod, key));
            listItem.SubItems.Add(this.textBoxCommand.Text);
            listItem.SubItems.Add(globalHotkeyId.ToString());

            this.listView1.Items.Add(listItem);
        }

        private void HandleKeyBindRemove()
        {
            // it was checked if anything is selected
            // and we know for sure only one item can be selected 
            int globalHotkeyId = Int32.Parse(this.listView1.SelectedItems[0].SubItems[2].Text);
            foreach (GlobalHotkey gh in this.ghList) {
                if (gh.Id.Equals(globalHotkeyId)) {
                    gh.Dispose();
                }
            }
            this.listView1.SelectedItems[0].Remove();
        }

        private void HandleHotkey(HotkeyInfo hotkeyInfo, string command)
        {
            if (command == null) return;
            this.cPort.Send(command);
            this.richTextBox1.AppendText(String.Format("[S] {0} \n", command));
        }
{'fraction_non_alphanumeric': 0.1030327868852459, 'fraction_numerical': 0.003524590163934426, 'mean_word_length': 1.9963163064833005, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '622115', 'n_tokens_mistral': 3364, 'n_tokens_neox': 3190, 'n_words': 593}