<commit_before># This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

require 'gui_main'
require 'gui_conf_event'
require 'gui_bug_event'
require 'gui'
require 'yaml'
require 'thread'
require 'fileutils'

include GUI

# Class containing event code for the main GUI window.
class GUIMainEvent < GUIMain

    # Initializes Events for the main GUI window.
    def initialize
        super
        
        log(1, "Program Started")
        log(0, "Initialization started")

        log(0, "Checking directories")
        FileUtils.mkdir "logs" unless `ls`.match(/logs/)
        FileUtils.mkdir "bugs" unless `ls`.match(/bugs/)

        # Events
        
        # Close button in top corner.
        evt_close()                         {close_prog()}

        # "File" menu.
        evt_menu(ID_NEW)                    {new_script()}
        evt_menu(ID_OPEN)                   {open_script()}
        evt_menu(ID_SAVE)                   {save_script()}
        evt_menu(ID_SAVEAS)                 {saveas_script()}
        evt_menu(ID_EXIT)                   {close_prog()}

        # "Run" menu.
        evt_menu(@run_run)                  {run_script()}
        evt_menu(@run_dryrun)               {run_script(-1)}
        evt_menu(@run_run_from)             {run_script(1)}

        # "Tools" menu.
        evt_menu(@tools_conf)               {display_conf_window()}

        # "Help" menu.
        evt_menu(ID_ABOUT)                  {about()}
        evt_menu(@help_bug)                 {report_bug}

        # Other widgets.
        evt_choice(@event_choice)           {event_type_selected()}
        evt_choice(@command_choice)         {event_subtype_selected()}

        evt_button(ID_ADD)                  {add_event()}
        evt_button(@insert_btn)             {add_event(true)}

        evt_button(ID_UP)                   {move_event(-1)}
        evt_button(ID_DOWN)                 {move_event(1)}
        evt_button(ID_DELETE)               {delete_event()}
        evt_button(@edit_btn)               {edit_event()}\

        evt_listbox_dclick(@script_display) {copy_event()}

        evt_button(ID_STOP)                 {stop_run()}
        evt_button(@play_btn)               {continue_run()}
        evt_button(@pause_btn)              {pause_run()}
          
        log(0, "Initialization complete")
    end

    # Writes a log message to the screen on the log tab.
    #
    # *<i>level</i>: An integer, either 0, 1, or 2 which correspond respectivly
    #                "low", "normal", and "high" priority messages.
    # *<i>message</i>: A log message as a string.
    def log(level, message)
        levels = ["Low", "Normal", "High"]
        @log_text.set_insertion_point_end
        @log_text.write_text("#{Time.now}: Urgency level #{levels[level]} -- #{message}\n\r")
    end

    # Closes the program, prompting the user to save first.
    # Called when either the
    def close_prog()
        log(1, "Program closing")
        new_script()
        log(0, "Writing log to file")
        File.open("logs/#{Time.now.month}-#{Time.now.day}-#{Time.now.year}.log", 'w') unless File.exist?(".logs/#{Time.now.month}-#{Time.now.day}-#{Time.now.year}.log")
        File.open("logs/#{Time.now.month}-#{Time.now.day}-#{Time.now.year}.log", 'a') {|log_out| log_out.write(@log_text.get_value)}
        self.destroy
    end

    # Creates a new script, first prompting user to save the current script.
    def new_script()
        log(0, "Checking save status")
        if $script.modified
            log(0, "Launching save prompt")
            prompt = MessageDialog.new(self, "Would you like to save?",
                "UNSAVED CHANGES!", YES_NO)
            if prompt.show_modal == ID_YES
                log(0, "Save prompt accepted")
                save_script()
            end
        end
        $script = Script.new
        populate_list(@script_display)
        log(1, "Blank script created")
    end

    # Opens a previously saved script
    def open_script()
        log(1, "Opening load dialog")
        dlg = FileDialog.new(self, "Choose a file", Dir.getwd(), "", "Pport Script (*.scp)|*.scp", OPEN)
        if dlg.show_modal() == ID_OK
            log(0, "File selected")
            file = dlg.get_path
            begin
                File.open(file, 'r') {|fin| $script = YAML.load(fin)}
                log(0, "Script loaded")
                File.open("#{file}.atr", 'r') {|fin| $attr = YAML.load(fin)}
                log(0, "Attributes loaded")
            rescue
                log(2, "Exception handled in open_script()")
            end
        end
        populate_list(@script_display)
    end

    # Saves the current script.
    def save_script()
        if $attr[:name]
            log(1, "Saving script to #{$attr[:name]}")
            File.open($attr[:name], 'w') {|fout| YAML.dump($script, fout)}
            log(0, "Saving script")
            File.open("#{$attr[:name]}.atr", 'w') {|fout| YAML.dump($attr, fout)}
            log(0, "Saving attributes")
            $script.modified = false
        else
            saveas_script()
        end
    end

    # Allows the user to select a directory and filename to save their
    # script to.
    def saveas_script()
        log(1, "Opening save dialog")
        dlg = FileDialog.new(self, "Save file as...", Dir.getwd(), "", "Pport Script (*.scp)|*.scp", SAVE)
        if dlg.show_modal() == Wx::ID_OK
            log(0, "File selected")<commit_msg>All data now saved to 1 file instead of 2<commit_after># This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

require 'gui_main'
require 'gui_conf_event'
require 'gui_bug_event'
require 'gui'
require 'yaml'
require 'thread'
require 'fileutils'

include GUI

# Class containing event code for the main GUI window.
class GUIMainEvent < GUIMain

    # Initializes Events for the main GUI window.
    def initialize
        super
        
        log(1, "Program Started")
        log(0, "Initialization started")

        log(0, "Checking directories")
        FileUtils.mkdir "logs" unless `ls`.match(/logs/)
        FileUtils.mkdir "bugs" unless `ls`.match(/bugs/)

        # Events
        
        # Close button in top corner.
        evt_close()                         {close_prog()}

        # "File" menu.
        evt_menu(ID_NEW)                    {new_script()}
        evt_menu(ID_OPEN)                   {open_script()}
        evt_menu(ID_SAVE)                   {save_script()}
        evt_menu(ID_SAVEAS)                 {saveas_script()}
        evt_menu(ID_EXIT)                   {close_prog()}

        # "Run" menu.
        evt_menu(@run_run)                  {run_script()}
        evt_menu(@run_dryrun)               {run_script(-1)}
        evt_menu(@run_run_from)             {run_script(1)}

        # "Tools" menu.
        evt_menu(@tools_conf)               {display_conf_window()}

        # "Help" menu.
        evt_menu(ID_ABOUT)                  {about()}
        evt_menu(@help_bug)                 {report_bug}

        # Other widgets.
        evt_choice(@event_choice)           {event_type_selected()}
        evt_choice(@command_choice)         {event_subtype_selected()}

        evt_button(ID_ADD)                  {add_event()}
        evt_button(@insert_btn)             {add_event(true)}

        evt_button(ID_UP)                   {move_event(-1)}
        evt_button(ID_DOWN)                 {move_event(1)}
        evt_button(ID_DELETE)               {delete_event()}
        evt_button(@edit_btn)               {edit_event()}\

        evt_listbox_dclick(@script_display) {copy_event()}

        evt_button(ID_STOP)                 {stop_run()}
        evt_button(@play_btn)               {continue_run()}
        evt_button(@pause_btn)              {pause_run()}
          
        log(0, "Initialization complete")
    end

    # Writes a log message to the screen on the log tab.
    #
    # *<i>level</i>: An integer, either 0, 1, or 2 which correspond respectivly
    #                "low", "normal", and "high" priority messages.
    # *<i>message</i>: A log message as a string.
    def log(level, message)
        levels = ["Low", "Normal", "High"]
        @log_text.set_insertion_point_end
        @log_text.write_text("#{Time.now}: Urgency level #{levels[level]} -- #{message}\n\r")
    end

    # Closes the program, prompting the user to save first.
    # Called when either the
    def close_prog()
        log(1, "Program closing")
        new_script()
        log(0, "Writing log to file")
        File.open("logs/#{Time.now.month}-#{Time.now.day}-#{Time.now.year}.log", 'w') unless File.exist?(".logs/#{Time.now.month}-#{Time.now.day}-#{Time.now.year}.log")
        File.open("logs/#{Time.now.month}-#{Time.now.day}-#{Time.now.year}.log", 'a') {|log_out| log_out.write(@log_text.get_value)}
        self.destroy
    end

    # Creates a new script, first prompting user to save the current script.
    def new_script()
        log(0, "Checking save status")
        if $script.modified
            log(0, "Launching save prompt")
            prompt = MessageDialog.new(self, "Would you like to save?",
                "UNSAVED CHANGES!", YES_NO)
            if prompt.show_modal == ID_YES
                log(0, "Save prompt accepted")
                save_script()
            end
        end
        $script = Script.new
        populate_list(@script_display)
        log(1, "Blank script created")
    end

    # Opens a previously saved script
    def open_script()
        log(1, "Opening load dialog")
        dlg = FileDialog.new(self, "Choose a file", Dir.getwd(), "", "Pport Script (*.scp)|*.scp", OPEN)
        if dlg.show_modal() == ID_OK
            log(0, "File selected")
            file = dlg.get_path
            begin
                File.open(file, 'r') do |fin|
                data = YAML.load(fin)
                $script = data[0]
                $attr = data[1]
            end
                log(0, "Script loaded")
            rescue
                log(2, "Exception handled in open_script()")
            end
        end
        populate_list(@script_display)
    end

    # Saves the current script.
    def save_script()
        if $attr[:name]
            log(1, "Saving script to #{$attr[:name]}")
            File.open($attr[:name], 'w') {|fout| YAML.dump([$script, $attr], fout)}
            log(0, "Script saved")
            $script.modified = false
        else
            saveas_script()
        end
    end

    # Allows the user to select a directory and filename to save their
    # script to.
    def saveas_script()
        log(1, "Opening save dialog")
        dlg = FileDialog.new(self, "Save file as...", Dir.getwd(), "", "Pport Script (*.scp)|*.scp", SAVE)
        if dlg.show_modal() == Wx::ID_OK
            log(0, "File selected")
{'fraction_non_alphanumeric': 0.11911727827468026, 'fraction_numerical': 0.004848282203460671, 'mean_word_length': 1.9657907783837383, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1962864', 'n_tokens_mistral': 3617, 'n_tokens_neox': 3579, 'n_words': 969}