2018-11-02 02:07:42 +08:00
|
|
|
#include "extenwindow.h"
|
|
|
|
#include "ui_extenwindow.h"
|
2018-11-28 04:54:04 +08:00
|
|
|
#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"
|
2018-11-28 04:54:04 +08:00
|
|
|
#include "misc.h"
|
2019-02-16 13:25:19 +08:00
|
|
|
#include <concrt.h>
|
2018-12-22 03:18:43 +08:00
|
|
|
#include <QDragEnterEvent>
|
|
|
|
#include <QDropEvent>
|
2018-11-02 06:47:19 +08:00
|
|
|
#include <QMimeData>
|
|
|
|
#include <QUrl>
|
2018-12-22 03:18:43 +08:00
|
|
|
#include <QLabel>
|
2018-11-02 02:07:42 +08:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2019-02-09 13:30:38 +08:00
|
|
|
|
|
|
|
struct Extension
|
2018-11-02 02:07:42 +08:00
|
|
|
{
|
2019-02-09 13:30:38 +08:00
|
|
|
std::wstring name;
|
|
|
|
wchar_t*(*callback)(wchar_t*, const InfoForExtension*);
|
2018-11-02 02:07:42 +08:00
|
|
|
};
|
|
|
|
|
2019-02-16 13:25:19 +08:00
|
|
|
concurrency::reader_writer_lock extenMutex;
|
2019-02-09 13:30:38 +08:00
|
|
|
std::vector<Extension> extensions;
|
2018-11-02 02:07:42 +08:00
|
|
|
|
|
|
|
void Load(QString extenName)
|
|
|
|
{
|
2018-11-02 06:47:19 +08:00
|
|
|
if (extenName == ITH_DLL) return;
|
2018-11-02 02:07:42 +08:00
|
|
|
// Extension is dll and exports "OnNewSentence"
|
2019-02-09 13:30:38 +08:00
|
|
|
if (auto callback = (decltype(Extension::callback))GetProcAddress(LoadLibraryOnce(S(extenName)), "OnNewSentence"))
|
2019-01-12 06:14:49 +08:00
|
|
|
{
|
|
|
|
std::scoped_lock writeLock(extenMutex);
|
2019-02-09 13:30:38 +08:00
|
|
|
extensions.push_back({ S(extenName), callback });
|
2019-01-12 06:14:49 +08:00
|
|
|
}
|
2018-11-02 02:07:42 +08:00
|
|
|
}
|
|
|
|
|
2019-02-09 13:30:38 +08:00
|
|
|
void Unload(int index)
|
2018-11-02 02:07:42 +08:00
|
|
|
{
|
2019-01-10 11:35:01 +08:00
|
|
|
std::scoped_lock writeLock(extenMutex);
|
2019-02-09 13:30:38 +08:00
|
|
|
FreeLibrary(GetModuleHandleW(extensions.at(index).name.c_str()));
|
|
|
|
extensions.erase(extensions.begin() + index);
|
2018-11-02 02:07:42 +08:00
|
|
|
}
|
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);
|
2019-02-09 13:30:38 +08:00
|
|
|
std::vector<Extension> extensions;
|
2019-02-14 07:23:42 +08:00
|
|
|
for (auto extenName : extenNames)
|
2019-02-09 13:30:38 +08:00
|
|
|
extensions.push_back(*std::find_if(::extensions.begin(), ::extensions.end(), [&](auto extension) { return extension.name == S(extenName); }));
|
|
|
|
::extensions = extensions;
|
2018-11-02 02:46:37 +08:00
|
|
|
}
|
2018-11-02 02:07:42 +08:00
|
|
|
}
|
|
|
|
|
2019-02-09 13:30:38 +08:00
|
|
|
bool DispatchSentenceToExtensions(std::wstring& sentence, const InfoForExtension* miscInfo)
|
2018-11-02 02:07:42 +08:00
|
|
|
{
|
2019-02-19 09:57:10 +08:00
|
|
|
wchar_t* sentenceBuffer = (wchar_t*)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, (sentence.size() + 1) * sizeof(wchar_t));
|
2018-11-02 02:07:42 +08:00
|
|
|
wcscpy_s(sentenceBuffer, sentence.size() + 1, sentence.c_str());
|
2019-02-16 13:25:19 +08:00
|
|
|
concurrency::reader_writer_lock::scoped_lock_read readLock(extenMutex);
|
2019-02-09 13:30:38 +08:00
|
|
|
for (const auto& extension : extensions)
|
2019-02-19 09:57:10 +08:00
|
|
|
if (*(sentenceBuffer = extension.callback(sentenceBuffer, miscInfo)) == L'\0') break;
|
2018-12-13 21:37:37 +08:00
|
|
|
sentence = sentenceBuffer;
|
2018-11-02 02:07:42 +08:00
|
|
|
HeapFree(GetProcessHeap(), 0, sentenceBuffer);
|
2019-02-19 09:57:10 +08:00
|
|
|
return !sentence.empty();
|
2018-11-02 02:07:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ExtenWindow::ExtenWindow(QWidget* parent) :
|
2018-12-22 03:18:43 +08:00
|
|
|
QMainWindow(parent, Qt::WindowCloseButtonHint),
|
2018-11-02 02:07:42 +08:00
|
|
|
ui(new Ui::ExtenWindow)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2019-01-26 11:49:50 +08:00
|
|
|
|
2018-12-22 03:18:43 +08:00
|
|
|
ui->vboxLayout->addWidget(new QLabel(EXTEN_WINDOW_INSTRUCTIONS, this));
|
|
|
|
setWindowTitle(EXTENSIONS);
|
2018-11-02 02:07:42 +08:00
|
|
|
|
2018-12-15 12:14:30 +08:00
|
|
|
ui->extenList->installEventFilter(this);
|
2018-11-02 02:07:42 +08:00
|
|
|
|
2019-01-26 11:49:50 +08:00
|
|
|
if (!QFile::exists(EXTEN_SAVE_FILE)) QTextFile(EXTEN_SAVE_FILE, QIODevice::WriteOnly).write(DEFAULT_EXTENSIONS);
|
2019-01-11 10:47:16 +08:00
|
|
|
for (auto extenName : QString(QTextFile(EXTEN_SAVE_FILE, QIODevice::ReadOnly).readAll()).split(">")) Load(extenName);
|
2018-11-02 02:07:42 +08:00
|
|
|
Sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
ExtenWindow::~ExtenWindow()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2018-11-02 06:47:19 +08:00
|
|
|
void ExtenWindow::Sync()
|
2018-11-02 02:07:42 +08:00
|
|
|
{
|
2018-12-15 12:14:30 +08:00
|
|
|
ui->extenList->clear();
|
2019-01-11 10:47:16 +08:00
|
|
|
QTextFile extenSaveFile(EXTEN_SAVE_FILE, QIODevice::WriteOnly | QIODevice::Truncate);
|
2019-02-16 13:25:19 +08:00
|
|
|
concurrency::reader_writer_lock::scoped_lock_read readLock(extenMutex);
|
2019-02-09 13:30:38 +08:00
|
|
|
for (auto extension : extensions)
|
2018-11-02 06:47:19 +08:00
|
|
|
{
|
2019-02-09 13:30:38 +08:00
|
|
|
ui->extenList->addItem(S(extension.name));
|
|
|
|
extenSaveFile.write((S(extension.name) + ">").toUtf8());
|
2018-11-02 06:47:19 +08:00
|
|
|
}
|
2018-11-02 02:07:42 +08:00
|
|
|
}
|
|
|
|
|
2018-12-02 04:55:32 +08:00
|
|
|
void ExtenWindow::Add(QFileInfo extenFile)
|
2018-11-02 02:07:42 +08:00
|
|
|
{
|
2018-12-02 04:55:32 +08:00
|
|
|
if (extenFile.suffix() != "dll") return;
|
2019-02-14 07:23:42 +08:00
|
|
|
QFile::copy(extenFile.absoluteFilePath(), extenFile.fileName());
|
2018-12-02 04:55:32 +08:00
|
|
|
Load(extenFile.completeBaseName());
|
2018-11-02 02:07:42 +08:00
|
|
|
Sync();
|
|
|
|
}
|
|
|
|
|
2018-12-29 01:14:56 +08:00
|
|
|
bool ExtenWindow::eventFilter(QObject* target, QEvent* event)
|
|
|
|
{
|
2018-11-02 02:07:42 +08:00
|
|
|
// 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;
|
2018-12-15 12:14:30 +08:00
|
|
|
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);
|
2018-11-02 02:07:42 +08:00
|
|
|
Sync();
|
|
|
|
}
|
2018-12-29 01:14:56 +08:00
|
|
|
return false;
|
2018-11-02 02:07:42 +08:00
|
|
|
}
|
|
|
|
|
2018-12-22 03:18:43 +08:00
|
|
|
void ExtenWindow::keyPressEvent(QKeyEvent* event)
|
|
|
|
{
|
2019-02-09 13:30:38 +08:00
|
|
|
if (event->key() == Qt::Key_Delete && ui->extenList->currentItem() != nullptr)
|
2018-12-22 03:18:43 +08:00
|
|
|
{
|
2019-02-09 13:30:38 +08:00
|
|
|
Unload(ui->extenList->currentIndex().row());
|
2018-12-22 03:18:43 +08:00
|
|
|
Sync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-02 06:47:19 +08:00
|
|
|
void ExtenWindow::dragEnterEvent(QDragEnterEvent* event)
|
2018-11-02 02:07:42 +08:00
|
|
|
{
|
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());
|
|
|
|
}
|