File: scripttool.cpp

package info (click to toggle)
openterface-qt 0.1.0%2Bds-1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 1,444 kB
  • sloc: cpp: 9,552; sh: 127; python: 57; ansic: 4; makefile: 4
file content (208 lines) | stat: -rw-r--r-- 7,379 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* ========================================================================== *
*                                                                            *
*    This file is part of the Openterface Mini KVM App QT version            *
*                                                                            *
*    Copyright (C) 2024   <info@openterface.com>                             *
*                                                                            *
*    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 version 3.                                 *
*                                                                            *
*    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/>.    *
*                                                                            *
* ========================================================================== *
*/ 

#include "scripttool.h"
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
#include <QCoreApplication>
#include <QShortcut>
#include <QDebug>
#include <thread>
#include "../scripts/Lexer.h"
#include "../scripts/Parser.h"
// #include "../scripts/semanticAnalyzer.h"
#include "../scripts/KeyboardMouse.h"

Q_DECLARE_LOGGING_CATEGORY(log_script)

ScriptTool::ScriptTool(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("Autohotkey Script Tool"));
    setFixedSize(640, 480);

    filePathEdit = new QLineEdit(this);
    filePathEdit->setPlaceholderText(tr("Select autohotkey.ahk file..."));
    filePathEdit->setReadOnly(true);

    selectButton = new QPushButton(tr("Browse"), this);
    runButton = new QPushButton(tr("Run Script"), this);
    runButton->setEnabled(false);

    saveButton = new QPushButton(tr("Save Script"), this);
    saveButton->setEnabled(false);

    scriptEdit = new QTextEdit(this);
    scriptEdit->setReadOnly(true);
    scriptEdit->setFont(QFont("Courier", 10));
    scriptEdit->setLineWrapMode(QTextEdit::NoWrap);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    
    QHBoxLayout *fileLayout = new QHBoxLayout();
    fileLayout->addWidget(filePathEdit);
    fileLayout->addWidget(selectButton);
    
    // Add the cancel button
    QPushButton *cancelButton = new QPushButton(tr("Cancel"), this);
    
    // Arrange buttons horizontally
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->addWidget(selectButton);
    buttonLayout->addWidget(runButton);
    buttonLayout->addWidget(saveButton);
    buttonLayout->addWidget(cancelButton);

    mainLayout->addLayout(fileLayout);
    mainLayout->addWidget(scriptEdit);
    mainLayout->addLayout(buttonLayout);

    connect(selectButton, &QPushButton::clicked, this, &ScriptTool::selectFile);
    connect(runButton, &QPushButton::clicked, this, &ScriptTool::runScript);
    connect(saveButton, &QPushButton::clicked, this, &ScriptTool::saveScript);
    connect(cancelButton, &QPushButton::clicked, this, &ScriptTool::close);

}

ScriptTool::~ScriptTool()
{
}

void ScriptTool::selectFile()
{
    QString appPath = QCoreApplication::applicationDirPath();
    
    QString filePath = QFileDialog::getOpenFileName(this,
        tr("Select autohotkey File"),
        appPath,
        tr("Autohotkey Files (*.ahk);;All Files (*)"));

    if (!filePath.isEmpty()) {
        filePathEdit->setText(filePath);
        runButton->setEnabled(true);

        QFile file(filePath);
        if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream in(&file);
            fileContents = in.readAll();
            file.close();
            lexer.setSource(fileContents.toStdString());
            tokens = lexer.tokenize();
            QString styledText = highlightTokens(tokens);

            // qDebug(log_script) << "Token Type:" << static_cast<int>(token.type) << "Value:" << tokenText;
            
            scriptEdit->setText(styledText);
            scriptEdit->setReadOnly(false);
            saveButton->setEnabled(true);
        } else {
            QMessageBox::warning(this, tr("Error"), tr("Could not open file for reading."));
        }
    }
}

void ScriptTool::runScript()
{
    QString filePath = filePathEdit->text();
    if (filePath.isEmpty()) {
        QMessageBox::warning(this, tr("Error"), tr("Please select a payload file first."));
        return;
    }

    lexer.setSource(scriptEdit->toPlainText().toStdString());
    tokens = lexer.tokenize();

    Parser parser(tokens);
    std::shared_ptr<ASTNode> syntaxTree = parser.parse();
    // qDebug(log_script) << "synctaxTree: " << syntaxTree.get();

    // Emit the signal with the syntaxTree
    emit syntaxTreeReady(syntaxTree);

    // QMessageBox::information(this, tr("Script Execution"), 
    //     tr("Script execution will be implemented here.\nSelected file: %1").arg(filePath));
}

void ScriptTool::processAST(ASTNode* node)
{
    if (!node) return;

    
}

void ScriptTool::saveScript()
{
    QString filePath = filePathEdit->text();
    if (!filePath.isEmpty()) {
        QFile file(filePath);
        if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            QTextStream out(&file);
            out << scriptEdit->toPlainText();
            file.close();
            QMessageBox::information(this, tr("Success"), tr("Script saved successfully."));
        } else {
            QMessageBox::warning(this, tr("Error"), tr("Could not save file."));
        }
    }
}

QString ScriptTool::highlightTokens(const std::vector<Token>& tokens) {
    QString styledText;
    for (const auto& token : tokens) {
        QString tokenText = QString::fromStdString(token.value);
        QString color;
        if (tokenText == "\\n") {
            tokenText = tokenText.replace("\\n", "<br>");
        }
        switch (token.type) {
            case AHKTokenType::KEYWORD:
                color = "green";
                break;
            case AHKTokenType::FUNCTION:
                color = "blue";
                break;
            case AHKTokenType::VARIABLE:
                color = "white";
                break;
            case AHKTokenType::INTEGER:
            case AHKTokenType::FLOAT:
                color = "DarkGoldenRod";
                break;
            case AHKTokenType::COMMAND:
                color = "purple";
                break;
            case AHKTokenType::COMMENT:
                color = "grey";
                break;
            default:
                if (QGuiApplication::palette().color(QPalette::Window).lightness() < 128) {
                    color = "white";
                } else {
                    color = "black";
                }
                break;
        }
        styledText += QString("<span style='color:%1;'>%2</span>").arg(color, tokenText);
    }
    return styledText;
}