2018-11-01 14:07:42 -04:00
|
|
|
#include "extenwindow.h"
|
|
|
|
#include "ui_extenwindow.h"
|
2018-11-27 15:54:04 -05:00
|
|
|
#include "defs.h"
|
2018-12-18 12:14:54 -05:00
|
|
|
#include "text.h"
|
2018-11-04 04:00:14 -05:00
|
|
|
#include "types.h"
|
2018-11-27 15:54:04 -05:00
|
|
|
#include "misc.h"
|
|
|
|
#include <shared_mutex>
|
2018-12-21 14:18:43 -05:00
|
|
|
#include <QDragEnterEvent>
|
|
|
|
#include <QDropEvent>
|
2018-11-01 14:07:42 -04:00
|
|
|
#include <QFileDialog>
|
2018-11-01 18:47:19 -04:00
|
|
|
#include <QMimeData>
|
|
|
|
#include <QUrl>
|
2018-12-21 14:18:43 -05:00
|
|
|
#include <QLabel>
|
2018-11-01 14:07:42 -04:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct InfoForExtension
|
|
|
|
{
|
|
|
|
const char* name;
|
|
|
|
int64_t value;
|
|
|
|
InfoForExtension* next;
|
|
|
|
~InfoForExtension() { if (next) delete next; };
|
|
|
|
};
|
|
|
|
|
2018-11-01 14:46:37 -04:00
|
|
|
QHash<QString, wchar_t*(*)(const wchar_t*, const InfoForExtension*)> extensions;
|
|
|
|
QStringList extenNames;
|
2018-11-01 14:07:42 -04:00
|
|
|
std::shared_mutex extenMutex;
|
|
|
|
|
|
|
|
void Load(QString extenName)
|
|
|
|
{
|
2018-11-01 18:47:19 -04:00
|
|
|
if (extenName == ITH_DLL) return;
|
2018-11-01 14:07:42 -04:00
|
|
|
// Extension is dll and exports "OnNewSentence"
|
2018-12-13 08:37:37 -05:00
|
|
|
HMODULE module = GetModuleHandleW(S(extenName).c_str());
|
|
|
|
if (!module) module = LoadLibraryW(S(extenName).c_str());
|
2018-11-01 14:07:42 -04:00
|
|
|
if (!module) return;
|
|
|
|
FARPROC callback = GetProcAddress(module, "OnNewSentence");
|
|
|
|
if (!callback) return;
|
2018-11-01 14:46:37 -04:00
|
|
|
LOCK(extenMutex);
|
|
|
|
extensions[extenName] = (wchar_t*(*)(const wchar_t*, const InfoForExtension*))callback;
|
|
|
|
extenNames.push_back(extenName);
|
2018-11-01 14:07:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Unload(QString extenName)
|
|
|
|
{
|
2018-11-01 14:46:37 -04:00
|
|
|
LOCK(extenMutex);
|
|
|
|
extenNames.erase(std::remove(extenNames.begin(), extenNames.end(), extenName), extenNames.end());
|
2018-12-13 08:37:37 -05:00
|
|
|
FreeLibrary(GetModuleHandleW(S(extenName).c_str()));
|
2018-11-01 14:07:42 -04:00
|
|
|
}
|
2018-11-01 14:46:37 -04:00
|
|
|
|
|
|
|
void Reorder(QStringList extenNames)
|
|
|
|
{
|
|
|
|
LOCK(extenMutex);
|
|
|
|
::extenNames = extenNames;
|
|
|
|
}
|
2018-11-01 14:07:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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-01 14:46:37 -04:00
|
|
|
for (auto extenName : extenNames)
|
2018-11-01 14:07:42 -04:00
|
|
|
{
|
2018-11-01 14:46:37 -04:00
|
|
|
wchar_t* nextBuffer = extensions[extenName](sentenceBuffer, &miscInfoLinkedList);
|
2018-11-01 14:07:42 -04:00
|
|
|
if (nextBuffer == nullptr) { success = false; break; }
|
|
|
|
if (nextBuffer != sentenceBuffer) HeapFree(GetProcessHeap(), 0, sentenceBuffer);
|
|
|
|
sentenceBuffer = nextBuffer;
|
|
|
|
}
|
2018-12-13 08:37:37 -05:00
|
|
|
sentence = sentenceBuffer;
|
2018-11-01 14:07:42 -04:00
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, sentenceBuffer);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExtenWindow::ExtenWindow(QWidget* parent) :
|
2018-12-21 14:18:43 -05:00
|
|
|
QMainWindow(parent, Qt::WindowCloseButtonHint),
|
2018-11-01 14:07:42 -04:00
|
|
|
ui(new Ui::ExtenWindow)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2018-12-21 14:18:43 -05:00
|
|
|
ui->vboxLayout->addWidget(new QLabel(EXTEN_WINDOW_INSTRUCTIONS, this));
|
|
|
|
setWindowTitle(EXTENSIONS);
|
2018-11-01 14:07:42 -04:00
|
|
|
|
2018-12-14 23:14:30 -05:00
|
|
|
ui->extenList->installEventFilter(this);
|
2018-11-01 14:07:42 -04:00
|
|
|
|
2018-11-27 15:54:04 -05:00
|
|
|
for (auto extenName : QString(QAutoFile(EXTEN_SAVE_FILE, QIODevice::ReadOnly)->readAll()).split(">")) Load(extenName);
|
2018-11-01 14:07:42 -04:00
|
|
|
Sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
ExtenWindow::~ExtenWindow()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2018-11-01 18:47:19 -04:00
|
|
|
void ExtenWindow::Sync()
|
2018-11-01 14:07:42 -04:00
|
|
|
{
|
2018-12-14 23:14:30 -05:00
|
|
|
ui->extenList->clear();
|
2018-11-27 15:54:04 -05:00
|
|
|
QAutoFile extenSaveFile(EXTEN_SAVE_FILE, QIODevice::WriteOnly | QIODevice::Truncate);
|
2018-11-01 18:47:19 -04:00
|
|
|
std::shared_lock sharedLock(extenMutex);
|
|
|
|
for (auto extenName : extenNames)
|
|
|
|
{
|
2018-12-14 23:14:30 -05:00
|
|
|
ui->extenList->addItem(extenName);
|
2018-11-27 15:54:04 -05:00
|
|
|
extenSaveFile->write((extenName + ">").toUtf8());
|
2018-11-01 18:47:19 -04:00
|
|
|
}
|
2018-11-01 14:07:42 -04:00
|
|
|
}
|
|
|
|
|
2018-12-01 15:55:32 -05:00
|
|
|
void ExtenWindow::Add(QFileInfo extenFile)
|
2018-11-01 14:07:42 -04:00
|
|
|
{
|
2018-12-01 15:55:32 -05:00
|
|
|
if (extenFile.suffix() != "dll") return;
|
|
|
|
QFile::copy(extenFile.fileName(), extenFile.absoluteFilePath());
|
|
|
|
Load(extenFile.completeBaseName());
|
2018-11-01 14:07:42 -04:00
|
|
|
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-01 14:46:37 -04:00
|
|
|
QStringList extenNames;
|
2018-12-14 23:14:30 -05:00
|
|
|
for (int i = 0; i < ui->extenList->count(); ++i) extenNames.push_back(ui->extenList->item(i)->text());
|
2018-11-01 14:46:37 -04:00
|
|
|
Reorder(extenNames);
|
2018-11-01 14:07:42 -04:00
|
|
|
Sync();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-21 14:18:43 -05:00
|
|
|
void ExtenWindow::keyPressEvent(QKeyEvent* event)
|
|
|
|
{
|
|
|
|
if (event->key() == Qt::Key_Delete) if (auto extenName = ui->extenList->currentItem())
|
|
|
|
{
|
|
|
|
Unload(extenName->text());
|
|
|
|
Sync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 18:47:19 -04:00
|
|
|
void ExtenWindow::dragEnterEvent(QDragEnterEvent* event)
|
2018-11-01 14:07:42 -04:00
|
|
|
{
|
2018-11-01 18:47:19 -04:00
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExtenWindow::dropEvent(QDropEvent* event)
|
|
|
|
{
|
|
|
|
for (auto file : event->mimeData()->urls()) Add(file.toLocalFile());
|
|
|
|
}
|