add settings dialog box

This commit is contained in:
Akash Mozumdar 2018-11-10 05:13:59 -05:00
parent 97d0528550
commit f351148b3d
14 changed files with 151 additions and 17 deletions

View File

@ -10,6 +10,7 @@ set(gui_SRCS
mainwindow.cpp mainwindow.cpp
misc.cpp misc.cpp
extenwindow.cpp extenwindow.cpp
setdialog.cpp
tests.cpp tests.cpp
host/host.cc host/host.cc
host/textthread.cc host/textthread.cc

View File

@ -5,7 +5,6 @@
#include "host.h" #include "host.h"
#include "const.h" #include "const.h"
#include "defs.h" #include "defs.h"
#include "text.h"
#include "../vnrhook/hijack/texthook.h" #include "../vnrhook/hijack/texthook.h"
namespace namespace

View File

@ -6,6 +6,7 @@
#include "common.h" #include "common.h"
#include "textthread.h" #include "textthread.h"
#include "text.h"
typedef std::function<void(DWORD)> ProcessEventCallback; typedef std::function<void(DWORD)> ProcessEventCallback;
typedef std::function<void(std::shared_ptr<TextThread>)> ThreadEventCallback; typedef std::function<void(std::shared_ptr<TextThread>)> ThreadEventCallback;
@ -30,12 +31,19 @@ namespace Host
void AddConsoleOutput(std::wstring text); void AddConsoleOutput(std::wstring text);
} }
inline UINT CURRENT_CODEPAGE = SHIFT_JIS; inline std::wstring StringToWideString(const std::string& text, UINT encoding = CP_UTF8)
inline std::wstring StringToWideString(const std::string& text, UINT encoding = CURRENT_CODEPAGE)
{ {
std::wstring ret(text.size() + 1, 0); std::wstring ret(text.size() + 1, 0);
ret.resize(MultiByteToWideChar(encoding, 0, text.c_str(), -1, ret.data(), ret.capacity()) - 1); if (int len = MultiByteToWideChar(encoding, 0, text.c_str(), -1, ret.data(), ret.capacity()))
{
ret.resize(len - 1);
return ret; return ret;
}
else
{
Host::AddConsoleOutput(INVALID_CODEPAGE);
return L"";
}
} }
// EOF // EOF

View File

@ -37,7 +37,7 @@ void TextThread::Push(const BYTE* data, int len)
LOCK(threadMutex); LOCK(threadMutex);
buffer += hp.type & USING_UNICODE buffer += hp.type & USING_UNICODE
? std::wstring((wchar_t*)data, len / 2) ? std::wstring((wchar_t*)data, len / 2)
: StringToWideString(std::string((char*)data, len), hp.codepage != 0 ? hp.codepage : CURRENT_CODEPAGE); : StringToWideString(std::string((char*)data, len), hp.codepage != 0 ? hp.codepage : defaultCodepage);
if (std::all_of(buffer.begin(), buffer.end(), [&](wchar_t c) { return repeatingChars.count(c) > 0; })) buffer.clear(); if (std::all_of(buffer.begin(), buffer.end(), [&](wchar_t c) { return repeatingChars.count(c) > 0; })) buffer.clear();
lastPushTime = GetTickCount(); lastPushTime = GetTickCount();
} }

View File

@ -16,6 +16,7 @@ public:
inline static int flushDelay = 400; // flush every 400ms by default inline static int flushDelay = 400; // flush every 400ms by default
inline static int maxBufferSize = 1000; inline static int maxBufferSize = 1000;
inline static int defaultCodepage = SHIFT_JIS;
inline static int threadCounter = 0; inline static int threadCounter = 0;
TextThread(ThreadParam tp, HookParam hp, std::wstring name); TextThread(ThreadParam tp, HookParam hp, std::wstring name);

View File

@ -2,6 +2,7 @@
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "text.h" #include "text.h"
#include "extenwindow.h" #include "extenwindow.h"
#include "setdialog.h"
#include "misc.h" #include "misc.h"
#include <QInputDialog> #include <QInputDialog>
@ -16,11 +17,10 @@ MainWindow::MainWindow(QWidget *parent) :
ttCombo = findChild<QComboBox*>("ttCombo"); ttCombo = findChild<QComboBox*>("ttCombo");
textOutput = findChild<QPlainTextEdit*>("textOutput"); textOutput = findChild<QPlainTextEdit*>("textOutput");
if (settings.contains(WINDOW)) this->setGeometry(settings.value(WINDOW).toRect()); if (settings.contains(WINDOW)) setGeometry(settings.value(WINDOW).toRect());
// TODO: add GUI for changing these
if (settings.contains(DEFAULT_CODEPAGE)) CURRENT_CODEPAGE = settings.value(DEFAULT_CODEPAGE).toInt();
if (settings.contains(FLUSH_DELAY)) TextThread::flushDelay = settings.value(FLUSH_DELAY).toInt(); if (settings.contains(FLUSH_DELAY)) TextThread::flushDelay = settings.value(FLUSH_DELAY).toInt();
if (settings.contains(MAX_BUFFER_SIZE)) TextThread::maxBufferSize = settings.value(MAX_BUFFER_SIZE).toInt(); if (settings.contains(MAX_BUFFER_SIZE)) TextThread::maxBufferSize = settings.value(MAX_BUFFER_SIZE).toInt();
if (settings.contains(DEFAULT_CODEPAGE)) TextThread::defaultCodepage = settings.value(DEFAULT_CODEPAGE).toInt();
qRegisterMetaType<std::shared_ptr<TextThread>>(); qRegisterMetaType<std::shared_ptr<TextThread>>();
@ -42,13 +42,9 @@ MainWindow::MainWindow(QWidget *parent) :
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
settings.setValue(WINDOW, this->geometry()); settings.setValue(WINDOW, geometry());
settings.setValue(DEFAULT_CODEPAGE, CURRENT_CODEPAGE);
settings.setValue(FLUSH_DELAY, TextThread::flushDelay);
settings.setValue(MAX_BUFFER_SIZE, TextThread::maxBufferSize);
settings.sync(); settings.sync();
delete ui; delete ui;
Host::Close(); Host::Close();
} }
@ -228,6 +224,11 @@ void MainWindow::on_saveButton_clicked()
file.write((hookList + "\r\n").toUtf8()); file.write((hookList + "\r\n").toUtf8());
} }
void MainWindow::on_setButton_clicked()
{
SetDialog(this).exec();
}
void MainWindow::on_extenButton_clicked() void MainWindow::on_extenButton_clicked()
{ {
extenWindow->activateWindow(); extenWindow->activateWindow();

View File

@ -41,6 +41,7 @@ private slots:
void on_unhookButton_clicked(); void on_unhookButton_clicked();
void on_hookButton_clicked(); void on_hookButton_clicked();
void on_saveButton_clicked(); void on_saveButton_clicked();
void on_setButton_clicked();
void on_extenButton_clicked(); void on_extenButton_clicked();
void on_ttCombo_activated(int index); void on_ttCombo_activated(int index);

View File

@ -101,6 +101,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QPushButton" name="setButton">
<property name="text">
<string>Settings</string>
</property>
</widget>
</item>
<item> <item>
<widget class="QPushButton" name="extenButton"> <widget class="QPushButton" name="extenButton">
<property name="text"> <property name="text">

View File

@ -5,5 +5,6 @@
#include <QVector> #include <QVector>
#include <QHash> #include <QHash>
#include <QMainWindow> #include <QMainWindow>
#include <QDialog>
#include <QFile> #include <QFile>
#include <QRegularExpression> #include <QRegularExpression>

40
GUI/setdialog.cpp Normal file
View File

@ -0,0 +1,40 @@
#include "setdialog.h"
#include "ui_setdialog.h"
#include "defs.h"
#include "host/host.h"
#include <QSettings>
SetDialog::SetDialog(QWidget* parent) :
QDialog(parent, Qt::WindowCloseButtonHint),
ui(new Ui::SetDialog)
{
ui->setupUi(this);
QFormLayout* layout = findChild<QFormLayout*>("layout");
auto addSpinBox = [&](QString label, int value)
{
auto spinbox = new QSpinBox(this);
spinbox->setMaximum(INT_MAX);
spinbox->setValue(value);
layout->insertRow(0, label, spinbox);
return spinbox;
};
flushDelay = addSpinBox(FLUSH_DELAY, TextThread::flushDelay);
maxBufferSize = addSpinBox(MAX_BUFFER_SIZE, TextThread::maxBufferSize);
defaultCodepage = addSpinBox(DEFAULT_CODEPAGE, TextThread::defaultCodepage);
}
SetDialog::~SetDialog()
{
delete ui;
}
void SetDialog::on_buttonBox_accepted()
{
QSettings settings(CONFIG_FILE, QSettings::IniFormat);
settings.setValue(FLUSH_DELAY, TextThread::flushDelay = flushDelay->value());
settings.setValue(MAX_BUFFER_SIZE, TextThread::maxBufferSize = maxBufferSize->value());
settings.setValue(DEFAULT_CODEPAGE, TextThread::defaultCodepage = defaultCodepage->value());
settings.sync();
}

30
GUI/setdialog.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef SETDIALOG_H
#define SETDIALOG_H
#include "qtcommon.h"
#include <QSpinBox>
namespace Ui
{
class SetDialog;
}
class SetDialog : public QDialog
{
Q_OBJECT
public:
explicit SetDialog(QWidget* parent = nullptr);
~SetDialog();
private slots:
void on_buttonBox_accepted();
private:
Ui::SetDialog* ui;
QSpinBox* flushDelay;
QSpinBox* maxBufferSize;
QSpinBox* defaultCodepage;
};
#endif // SETDIALOG_H

44
GUI/setdialog.ui Normal file
View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SetDialog</class>
<widget class="QDialog" name="SetDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>300</width>
<height>120</height>
</rect>
</property>
<property name="windowTitle">
<string>Settings</string>
</property>
<layout class="QFormLayout" name="layout">
<item row="0" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>SetDialog</receiver>
<slot>accept()</slot>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>SetDialog</receiver>
<slot>reject()</slot>
</connection>
</connections>
</ui>

View File

@ -27,8 +27,8 @@ constexpr auto EXTEN_SAVE_FILE = u8"Extensions.txt";
// Settings // Settings
constexpr auto WINDOW = u8"Window"; constexpr auto WINDOW = u8"Window";
constexpr auto DEFAULT_CODEPAGE = u8"Default_Codepage"; constexpr auto DEFAULT_CODEPAGE = u8"Default Codepage";
constexpr auto FLUSH_DELAY = u8"Flush_Delay"; constexpr auto FLUSH_DELAY = u8"Flush Delay";
constexpr auto MAX_BUFFER_SIZE = u8"Max_Buffer_Size"; constexpr auto MAX_BUFFER_SIZE = u8"Max Buffer Size";
// EOF // EOF

View File

@ -28,3 +28,4 @@ constexpr auto ARCHITECTURE_MISMATCH = L"Textractor: ERROR: architecture mismatc
constexpr auto INJECT_FAILED = L"Textractor: ERROR: couldn't inject"; constexpr auto INJECT_FAILED = L"Textractor: ERROR: couldn't inject";
constexpr auto INVALID_CODE = L"Textractor: invalid code"; constexpr auto INVALID_CODE = L"Textractor: invalid code";
constexpr auto NO_HOOKS = L"Textractor: no hooks detected"; constexpr auto NO_HOOKS = L"Textractor: no hooks detected";
constexpr auto INVALID_CODEPAGE = L"Textractor: ERROR: couldn't convert text (invalid codepage?)";