Textractor_test/GUI/extenwindow.cpp

149 lines
4.1 KiB
C++
Raw Normal View History

#include "extenwindow.h"
#include "ui_extenwindow.h"
2018-11-04 15:13:51 +08:00
#include "text.h"
#include "defs.h"
2018-11-04 17:00:14 +08:00
#include "types.h"
#include "misc.h"
#include <shared_mutex>
#include <QFileDialog>
2018-11-02 06:47:19 +08:00
#include <QMimeData>
#include <QUrl>
namespace
{
struct InfoForExtension
{
const char* name;
int64_t value;
InfoForExtension* next;
~InfoForExtension() { if (next) delete next; };
};
2018-11-02 02:46:37 +08:00
QHash<QString, wchar_t*(*)(const wchar_t*, const InfoForExtension*)> extensions;
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"
HMODULE module = GetModuleHandleW(extenName.toStdWString().c_str());
if (!module) module = LoadLibraryW(extenName.toStdWString().c_str());
if (!module) return;
FARPROC callback = GetProcAddress(module, "OnNewSentence");
if (!callback) return;
2018-11-02 02:46:37 +08:00
LOCK(extenMutex);
extensions[extenName] = (wchar_t*(*)(const wchar_t*, const InfoForExtension*))callback;
extenNames.push_back(extenName);
}
void Unload(QString extenName)
{
2018-11-02 02:46:37 +08:00
LOCK(extenMutex);
extenNames.erase(std::remove(extenNames.begin(), extenNames.end(), extenName), extenNames.end());
FreeLibrary(GetModuleHandleW(extenName.toStdWString().c_str()));
}
2018-11-02 02:46:37 +08:00
void Reorder(QStringList extenNames)
{
LOCK(extenMutex);
::extenNames = extenNames;
}
}
bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<std::string, int64_t> miscInfo)
{
bool success = true;
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;
for (auto& i : miscInfo) miscInfoTraverser = miscInfoTraverser->next = new InfoForExtension{ i.first.c_str(), i.second, nullptr };
std::shared_lock sharedLock(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 == nullptr) { success = false; break; }
if (nextBuffer != sentenceBuffer) HeapFree(GetProcessHeap(), 0, sentenceBuffer);
sentenceBuffer = nextBuffer;
}
sentence = std::wstring(sentenceBuffer);
HeapFree(GetProcessHeap(), 0, sentenceBuffer);
return success;
}
ExtenWindow::ExtenWindow(QWidget* parent) :
QMainWindow(parent),
ui(new Ui::ExtenWindow)
{
ui->setupUi(this);
extenList = findChild<QListWidget*>("extenList");
extenList->installEventFilter(this);
for (auto extenName : QString(QAutoFile(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()
{
2018-11-02 06:47:19 +08:00
extenList->clear();
QAutoFile extenSaveFile(EXTEN_SAVE_FILE, QIODevice::WriteOnly | QIODevice::Truncate);
2018-11-02 06:47:19 +08:00
std::shared_lock sharedLock(extenMutex);
for (auto extenName : extenNames)
{
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();
}
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 < extenList->count(); ++i) extenNames.push_back(extenList->item(i)->text());
Reorder(extenNames);
Sync();
}
return false;
}
2018-11-02 06:47:19 +08:00
void ExtenWindow::dragEnterEvent(QDragEnterEvent* event)
{
2018-11-02 06:47:19 +08:00
event->acceptProposedAction();
}
void ExtenWindow::dropEvent(QDropEvent* event)
{
for (auto file : event->mimeData()->urls()) Add(file.toLocalFile());
}
void ExtenWindow::on_addButton_clicked()
{
2018-11-04 15:13:51 +08:00
Add(QFileDialog::getOpenFileName(this, SELECT_EXTENSION, "C:\\", EXTENSIONS));
2018-11-02 06:47:19 +08:00
}
void ExtenWindow::on_rmvButton_clicked()
{
if (auto extenName = extenList->currentItem()) Unload(extenName->text());
Sync();
}