Textractor_test/GUI/extenwindow.cpp

146 lines
3.9 KiB
C++
Raw Normal View History

#include "extenwindow.h"
#include "ui_extenwindow.h"
#include "defs.h"
2018-12-19 01:14:54 +08:00
#include "text.h"
2018-11-04 17:00:14 +08:00
#include "types.h"
#include "misc.h"
#include <QDragEnterEvent>
#include <QDropEvent>
2018-11-02 06:47:19 +08:00
#include <QMimeData>
#include <QUrl>
#include <QLabel>
namespace
{
struct InfoForExtension
{
const char* name;
int64_t value;
InfoForExtension* next;
~InfoForExtension() { if (next) delete next; };
};
2019-01-24 02:59:34 +08:00
QHash<QString, wchar_t*(*)(wchar_t*, const InfoForExtension*)> extensions;
2018-11-02 02:46:37 +08:00
QStringList extenNames;
std::shared_mutex extenMutex;
void Load(QString extenName)
{
2018-11-02 06:47:19 +08:00
if (extenName == ITH_DLL) return;
// Extension is dll and exports "OnNewSentence"
if (FARPROC callback = GetProcAddress(LoadLibraryOnce(S(extenName)), "OnNewSentence"))
{
std::scoped_lock writeLock(extenMutex);
2019-01-24 02:59:34 +08:00
extensions[extenName] = (wchar_t*(*)(wchar_t*, const InfoForExtension*))callback;
extenNames.push_back(extenName);
}
}
void Unload(QString extenName)
{
2019-01-10 11:35:01 +08:00
std::scoped_lock writeLock(extenMutex);
2018-11-02 02:46:37 +08:00
extenNames.erase(std::remove(extenNames.begin(), extenNames.end(), extenName), extenNames.end());
FreeLibrary(GetModuleHandleW(S(extenName).c_str()));
}
2018-11-02 02:46:37 +08:00
void Reorder(QStringList extenNames)
{
2019-01-10 11:35:01 +08:00
std::scoped_lock writeLock(extenMutex);
2018-11-02 02:46:37 +08:00
::extenNames = extenNames;
}
}
2019-01-02 06:50:22 +08:00
bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<const char*, int64_t> miscInfo)
{
wchar_t* sentenceBuffer = (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentence.size() + 1) * sizeof(wchar_t));
wcscpy_s(sentenceBuffer, sentence.size() + 1, sentence.c_str());
InfoForExtension miscInfoLinkedList{ "", 0, nullptr };
InfoForExtension* miscInfoTraverser = &miscInfoLinkedList;
2019-01-02 06:50:22 +08:00
for (auto[name, value] : miscInfo) miscInfoTraverser = miscInfoTraverser->next = new InfoForExtension{ name, value, nullptr };
2019-01-10 11:35:01 +08:00
std::shared_lock readLock(extenMutex);
2018-11-02 02:46:37 +08:00
for (auto extenName : extenNames)
{
2018-11-02 02:46:37 +08:00
wchar_t* nextBuffer = extensions[extenName](sentenceBuffer, &miscInfoLinkedList);
if (nextBuffer != sentenceBuffer) HeapFree(GetProcessHeap(), 0, sentenceBuffer);
2019-01-02 06:50:22 +08:00
if (nextBuffer == nullptr) return false;
sentenceBuffer = nextBuffer;
}
sentence = sentenceBuffer;
HeapFree(GetProcessHeap(), 0, sentenceBuffer);
2019-01-02 06:50:22 +08:00
return true;
}
ExtenWindow::ExtenWindow(QWidget* parent) :
QMainWindow(parent, Qt::WindowCloseButtonHint),
ui(new Ui::ExtenWindow)
{
ui->setupUi(this);
ui->vboxLayout->addWidget(new QLabel(EXTEN_WINDOW_INSTRUCTIONS, this));
setWindowTitle(EXTENSIONS);
ui->extenList->installEventFilter(this);
for (auto extenName : QString(QTextFile(EXTEN_SAVE_FILE, QIODevice::ReadOnly).readAll()).split(">")) Load(extenName);
Sync();
}
ExtenWindow::~ExtenWindow()
{
delete ui;
}
2018-11-02 06:47:19 +08:00
void ExtenWindow::Sync()
{
ui->extenList->clear();
QTextFile extenSaveFile(EXTEN_SAVE_FILE, QIODevice::WriteOnly | QIODevice::Truncate);
2019-01-10 11:35:01 +08:00
std::shared_lock readLock(extenMutex);
2018-11-02 06:47:19 +08:00
for (auto extenName : extenNames)
{
ui->extenList->addItem(extenName);
extenSaveFile.write((extenName + ">").toUtf8());
2018-11-02 06:47:19 +08:00
}
}
void ExtenWindow::Add(QFileInfo extenFile)
{
if (extenFile.suffix() != "dll") return;
QFile::copy(extenFile.fileName(), extenFile.absoluteFilePath());
Load(extenFile.completeBaseName());
Sync();
}
2018-12-29 01:14:56 +08:00
bool ExtenWindow::eventFilter(QObject* target, QEvent* event)
{
// See https://stackoverflow.com/questions/1224432/how-do-i-respond-to-an-internal-drag-and-drop-operation-using-a-qlistwidget/1528215
if (event->type() == QEvent::ChildRemoved)
{
2018-11-02 02:46:37 +08:00
QStringList extenNames;
for (int i = 0; i < ui->extenList->count(); ++i) extenNames.push_back(ui->extenList->item(i)->text());
2018-11-02 02:46:37 +08:00
Reorder(extenNames);
Sync();
}
2018-12-29 01:14:56 +08:00
return false;
}
void ExtenWindow::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_Delete) if (auto extenName = ui->extenList->currentItem())
{
Unload(extenName->text());
Sync();
}
}
2018-11-02 06:47:19 +08:00
void ExtenWindow::dragEnterEvent(QDragEnterEvent* event)
{
2018-11-02 06:47:19 +08:00
event->acceptProposedAction();
}
2018-12-29 01:14:56 +08:00
void ExtenWindow::dropEvent(QDropEvent* event)
{
2018-11-02 06:47:19 +08:00
for (auto file : event->mimeData()->urls()) Add(file.toLocalFile());
}