From eb1421c143be52f4b2090d7829d3c37f850e2c61 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Sat, 5 Jun 2021 16:29:51 -0600 Subject: [PATCH] fix style issues and use blank icons --- GUI/CMakeLists.txt | 7 +-- GUI/attachprocessdialog.cpp | 48 ++++++++++++++++++ GUI/attachprocessdialog.h | 17 +++++++ GUI/attachprocessdialog.ui | 41 ++++++++++++++++ GUI/attachtoprocessdialog.cpp | 92 ----------------------------------- GUI/attachtoprocessdialog.h | 51 ------------------- GUI/attachtoprocessdialog.ui | 52 -------------------- GUI/extenwindow.h | 5 -- GUI/mainwindow.cpp | 52 ++++++-------------- GUI/utils/windowshelpers.cpp | 30 ------------ GUI/utils/windowshelpers.h | 9 ---- 11 files changed, 121 insertions(+), 283 deletions(-) create mode 100644 GUI/attachprocessdialog.cpp create mode 100644 GUI/attachprocessdialog.h create mode 100644 GUI/attachprocessdialog.ui delete mode 100644 GUI/attachtoprocessdialog.cpp delete mode 100644 GUI/attachtoprocessdialog.h delete mode 100644 GUI/attachtoprocessdialog.ui delete mode 100644 GUI/utils/windowshelpers.cpp delete mode 100644 GUI/utils/windowshelpers.h diff --git a/GUI/CMakeLists.txt b/GUI/CMakeLists.txt index c8e17de..54eafae 100644 --- a/GUI/CMakeLists.txt +++ b/GUI/CMakeLists.txt @@ -2,20 +2,15 @@ include(QtUtils) msvc_registry_search() find_qt5(Core Widgets WinExtras) -set(CMAKE_AUTOUIC ON) -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) - add_executable(Textractor WIN32 main.cpp mainwindow.cpp extenwindow.cpp - attachtoprocessdialog.cpp + attachprocessdialog.cpp host/exception.cpp host/host.cpp host/textthread.cpp host/hookcode.cpp - utils/windowshelpers.cpp Textractor.rc Textractor.ico ) diff --git a/GUI/attachprocessdialog.cpp b/GUI/attachprocessdialog.cpp new file mode 100644 index 0000000..830d9ed --- /dev/null +++ b/GUI/attachprocessdialog.cpp @@ -0,0 +1,48 @@ +#include "attachprocessdialog.h" +#include + +extern const char* SELECT_PROCESS; +extern const char* ATTACH_INFO; + +AttachProcessDialog::AttachProcessDialog(QWidget *parent, std::vector> processIcons) : + QDialog(parent, Qt::WindowCloseButtonHint), + model(this) +{ + ui.setupUi(this); + setWindowTitle(SELECT_PROCESS); + ui.label->setText(ATTACH_INFO); + ui.processIdEdit->setValidator(new QIntValidator(0, INT_MAX, this)); + ui.processList->setModel(&model); + + QPixmap transparent(100, 100); + transparent.fill(QColor::fromRgba(0)); + for (const auto& [process, icon] : processIcons) + { + auto item = new QStandardItem(icon ? QIcon(QtWin::fromHICON(icon)) : transparent, process); + item->setEditable(false); + model.appendRow(item); + } + + connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); + connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); + connect(ui.processList, &QListView::clicked, [this](QModelIndex index) + { + selectedProcess = model.item(index.row())->text(); + }); + connect(ui.processList, &QListView::doubleClicked, [this](QModelIndex index) + { + selectedProcess = model.item(index.row())->text(); + accept(); + }); + connect(ui.processIdEdit, &QLineEdit::returnPressed, [this] + { + selectedProcess = ui.processIdEdit->text(); + accept(); + }); + +} + +QString AttachProcessDialog::SelectedProcess() +{ + return selectedProcess.isEmpty() ? ui.processIdEdit->text() : selectedProcess; +} diff --git a/GUI/attachprocessdialog.h b/GUI/attachprocessdialog.h new file mode 100644 index 0000000..9d17eb6 --- /dev/null +++ b/GUI/attachprocessdialog.h @@ -0,0 +1,17 @@ +#pragma once + +#include "qtcommon.h" +#include "ui_attachprocessdialog.h" +#include + +class AttachProcessDialog : public QDialog +{ +public: + explicit AttachProcessDialog(QWidget *parent, std::vector> processIcons); + QString SelectedProcess(); + +private: + Ui::AttachProcessDialog ui; + QStandardItemModel model; + QString selectedProcess; +}; diff --git a/GUI/attachprocessdialog.ui b/GUI/attachprocessdialog.ui new file mode 100644 index 0000000..ece348a --- /dev/null +++ b/GUI/attachprocessdialog.ui @@ -0,0 +1,41 @@ + + + AttachProcessDialog + + + Qt::WindowModal + + + + 0 + 0 + 800 + 400 + + + + + + + + + + Process id + + + + + + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + diff --git a/GUI/attachtoprocessdialog.cpp b/GUI/attachtoprocessdialog.cpp deleted file mode 100644 index c85b9d4..0000000 --- a/GUI/attachtoprocessdialog.cpp +++ /dev/null @@ -1,92 +0,0 @@ -#include "attachtoprocessdialog.h" -#include "ui_attachtoprocessdialog.h" -#include "utils/windowshelpers.h" - -#include -#include -#include - -namespace -{ - QString GetNameProcessFromIndex(const QModelIndex &index, const QVector>& data) - { - const int row = index.row(); - return data[row].first; - } -} - -AttachToProcessDialog::AttachToProcessDialog(QWidget *parent) : - QDialog(parent, Qt::WindowCloseButtonHint), - ui(new Ui::AttachToProcessDialog), - model(new QStandardItemModel(this)) -{ - ui->setupUi(this); - - const QIntValidator* validator = new QIntValidator(0, INT_MAX, this); - ui->lineEdit->setValidator(validator); -} - -AttachToProcessDialog::~AttachToProcessDialog() -{ - delete ui; -} - -void AttachToProcessDialog::setLabelText(const QString& text) -{ - ui->label->setText(text); -} - -void AttachToProcessDialog::setData(QVector>&& newData) -{ - data = std::move(newData); - selectedProcess.clear(); - std::sort(data.begin(), data.end(), [](const auto& left, const auto& right) { - return left.first < right.first; - }); - model->clear(); - for (const auto& [process, hIcon] : data) - { - QIcon icon = WindowsHepers::CreateQIconFromHIcon(hIcon); - auto* item = new QStandardItem(icon, process); - item->setEditable(false); - model->appendRow(item); - } - ui->listView->setModel(model); -} - -QString AttachToProcessDialog::getSelectedData() -{ - return selectedProcess.isEmpty() ? ui->lineEdit->text() : selectedProcess; -} - -void AttachToProcessDialog::on_buttonBox_accepted() -{ - accept(); -} - -void AttachToProcessDialog::on_buttonBox_rejected() -{ - reject(); -} - -void AttachToProcessDialog::on_listView_doubleClicked(const QModelIndex& index) -{ - selectedProcess = GetNameProcessFromIndex(index, data); - accept(); -} - -void AttachToProcessDialog::on_lineEdit_returnPressed() -{ - selectedProcess = ui->lineEdit->text(); - accept(); -} - -void AttachToProcessDialog::on_listView_clicked(const QModelIndex &index) -{ - selectedProcess = GetNameProcessFromIndex(index, data); -} - -void AttachToProcessDialog::on_lineEdit_editingFinished() -{ - selectedProcess = ui->lineEdit->text(); -} diff --git a/GUI/attachtoprocessdialog.h b/GUI/attachtoprocessdialog.h deleted file mode 100644 index 3dde6e5..0000000 --- a/GUI/attachtoprocessdialog.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef ATTACHTOPROCESSDIALOG_H -#define ATTACHTOPROCESSDIALOG_H - -#include -#include -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE -namespace Ui { -class AttachToProcessDialog; -} -QT_END_NAMESPACE - -class QStandardItemModel; -class QModelIndex; - -class AttachToProcessDialog : public QDialog -{ - Q_OBJECT - -public: - explicit AttachToProcessDialog(QWidget *parent = nullptr); - void setData(QVector>&& newData); - void setLabelText(const QString& text); - QString getSelectedData(); - ~AttachToProcessDialog(); - -private slots: - void on_buttonBox_accepted(); - - void on_buttonBox_rejected(); - - void on_listView_doubleClicked(const QModelIndex &index); - - void on_lineEdit_returnPressed(); - - void on_listView_clicked(const QModelIndex &index); - - void on_lineEdit_editingFinished(); - -private: - Ui::AttachToProcessDialog* ui; - QStandardItemModel* model; - QString selectedProcess; - QVector> data; -}; - -#endif // ATTACHTOPROCESSDIALOG_H \ No newline at end of file diff --git a/GUI/attachtoprocessdialog.ui b/GUI/attachtoprocessdialog.ui deleted file mode 100644 index 9f3c231..0000000 --- a/GUI/attachtoprocessdialog.ui +++ /dev/null @@ -1,52 +0,0 @@ - - - AttachToProcessDialog - - - Qt::WindowModal - - - - 0 - 0 - 813 - 426 - - - - Form - - - - - - - - TextLabel - - - - - - - PID - - - - - - - - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - - - - - diff --git a/GUI/extenwindow.h b/GUI/extenwindow.h index 7561092..0cc3d40 100644 --- a/GUI/extenwindow.h +++ b/GUI/extenwindow.h @@ -2,11 +2,6 @@ #include "qtcommon.h" -namespace Ui -{ - class ExtenWindow; -} - struct InfoForExtension { const char* name; diff --git a/GUI/mainwindow.cpp b/GUI/mainwindow.cpp index 9880919..a8b9615 100644 --- a/GUI/mainwindow.cpp +++ b/GUI/mainwindow.cpp @@ -5,9 +5,7 @@ #include "extenwindow.h" #include "host/host.h" #include "host/hookcode.h" -#include "attachtoprocessdialog.h" -#include "utils/windowshelpers.h" - +#include "attachprocessdialog.h" #include #include #include @@ -32,7 +30,6 @@ extern const char* SETTINGS; extern const char* EXTENSIONS; extern const char* FONT; extern const char* SELECT_PROCESS; -extern const char* ATTACH_INFO; extern const char* SELECT_PROCESS_INFO; extern const char* FROM_COMPUTER; extern const char* PROCESSES; @@ -83,7 +80,6 @@ namespace Ui::MainWindow ui; std::atomic selectedProcessId = 0; ExtenWindow* extenWindow = nullptr; - AttachToProcessDialog* attachDialog = nullptr; std::unordered_set alreadyAttached; bool autoAttach = false, autoAttachSavedOnly = true; bool showSystemProcesses = false; @@ -157,50 +153,31 @@ namespace } void AttachProcess() - { - auto processes = GetAllProcesses(); + { QMultiHash processesMap; - QVector> dialogData; - dialogData.reserve(processes.size()); - for (auto [processId, processName] : processes) + std::vector> processIcons; + for (auto [processId, processName] : GetAllProcesses()) { if (processName && (showSystemProcesses || processName->find(L":\\Windows\\") == std::string::npos)) { - const auto& value = processName.value(); - const QFileInfo& fileInfo = QFileInfo(S(value)); - const QString& fileName = fileInfo.fileName(); + QString fileName = QFileInfo(S(processName.value())).fileName(); if (!processesMap.contains(fileName)) { - const auto icon = WindowsHepers::GetIconHandlerFromExe(value.c_str()); - dialogData.push_back({fileName, icon}); + HICON bigIcon, smallIcon; + ExtractIconExW(processName->c_str(), 0, &bigIcon, &smallIcon, 1); + processIcons.push_back({ fileName, bigIcon ? bigIcon : smallIcon }); } processesMap.insert(fileName, processId); } } - dialogData.shrink_to_fit(); - processes.clear(); + std::sort(processIcons.begin(), processIcons.end()); - attachDialog->setWindowTitle(SELECT_PROCESS); - attachDialog->setLabelText(ATTACH_INFO); - - attachDialog->setData(std::move(dialogData)); - const bool hasChosenData = attachDialog->exec() != 0; - - if (hasChosenData) + AttachProcessDialog attachProcessDialog(This, processIcons); + if (attachProcessDialog.exec()) { - const QString& process = attachDialog->getSelectedData(); - const int pid = process.toInt(nullptr, 0); - if (pid) - { - Host::InjectProcess(pid); - } - else - { - for (const auto& processId : processesMap.values(process)) - { - Host::InjectProcess(processId); - } - } + QString process = attachProcessDialog.SelectedProcess(); + if (int processId = process.toInt(nullptr, 0)) Host::InjectProcess(processId); + else for (int processId : processesMap.values(process)) Host::InjectProcess(processId); } } @@ -636,7 +613,6 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) This = this; ui.setupUi(this); extenWindow = new ExtenWindow(this); - attachDialog = new AttachToProcessDialog(this); for (auto [text, slot] : Array{ { ATTACH, AttachProcess }, { LAUNCH, LaunchProcess }, diff --git a/GUI/utils/windowshelpers.cpp b/GUI/utils/windowshelpers.cpp deleted file mode 100644 index 2d657d9..0000000 --- a/GUI/utils/windowshelpers.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "windowshelpers.h" - -#include -#include -#include -#include -#include - -#include - -namespace WindowsHepers { - HICON GetIconHandlerFromExe(const wchar_t* const filePath) - { - HICON bigIcon; - HICON smallIcon; - ExtractIconEx(filePath, 0, &bigIcon, &smallIcon, 1); - return bigIcon != 0 ? bigIcon : smallIcon; - } - - QIcon CreateQIconFromHIcon(const HICON hIcon) - { - if (hIcon) - { - const QPixmap& pixmap = QtWin::fromHICON(hIcon); - return QIcon(pixmap); - } - const QStyle* style = QApplication::style(); - return style->standardIcon(QStyle::SP_ComputerIcon); - } -} diff --git a/GUI/utils/windowshelpers.h b/GUI/utils/windowshelpers.h deleted file mode 100644 index 405d4fe..0000000 --- a/GUI/utils/windowshelpers.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include -class QIcon; -namespace WindowsHepers -{ - HICON GetIconHandlerFromExe(const wchar_t* const filePath); - QIcon CreateQIconFromHIcon(const HICON hIcon); -}