2018-11-01 14:07:42 -04:00
|
|
|
#include "extenwindow.h"
|
|
|
|
#include "ui_extenwindow.h"
|
2019-02-16 00:25:19 -05:00
|
|
|
#include <concrt.h>
|
2019-08-29 13:46:21 -04:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QFileDialog>
|
2018-12-21 14:18:43 -05:00
|
|
|
#include <QDragEnterEvent>
|
|
|
|
#include <QDropEvent>
|
2018-11-01 18:47:19 -04:00
|
|
|
#include <QMimeData>
|
|
|
|
#include <QUrl>
|
2018-11-01 14:07:42 -04:00
|
|
|
|
2019-02-27 11:33:17 -05:00
|
|
|
extern const char* EXTENSIONS;
|
2019-08-29 13:46:21 -04:00
|
|
|
extern const char* ADD_EXTENSION;
|
2021-03-08 08:46:34 -07:00
|
|
|
extern const char* REMOVE_EXTENSION;
|
2019-06-04 23:14:46 -04:00
|
|
|
extern const char* INVALID_EXTENSION;
|
|
|
|
extern const char* CONFIRM_EXTENSION_OVERWRITE;
|
|
|
|
extern const char* EXTENSION_WRITE_ERROR;
|
2019-02-27 11:33:17 -05:00
|
|
|
extern const char* EXTEN_WINDOW_INSTRUCTIONS;
|
|
|
|
|
2018-11-01 14:07:42 -04:00
|
|
|
namespace
|
|
|
|
{
|
2020-03-16 02:56:04 -06:00
|
|
|
constexpr auto EXTEN_SAVE_FILE = u8"SavedExtensions.txt";
|
2021-01-30 16:42:29 -07:00
|
|
|
constexpr auto DEFAULT_EXTENSIONS = u8"Remove Repeated Characters>Remove Repeated Phrases>Regex Filter>Copy to Clipboard>Bing Translate>Extra Window>Extra Newlines>Styler";
|
2020-03-16 02:56:04 -06:00
|
|
|
|
2019-02-09 00:30:38 -05:00
|
|
|
struct Extension
|
2018-11-01 14:07:42 -04:00
|
|
|
{
|
2019-02-09 00:30:38 -05:00
|
|
|
std::wstring name;
|
2019-08-29 13:46:21 -04:00
|
|
|
wchar_t* (*callback)(wchar_t*, const InfoForExtension*);
|
2018-11-01 14:07:42 -04:00
|
|
|
};
|
|
|
|
|
2020-03-16 02:56:04 -06:00
|
|
|
Ui::ExtenWindow ui;
|
2019-02-16 00:25:19 -05:00
|
|
|
concurrency::reader_writer_lock extenMutex;
|
2019-02-09 00:30:38 -05:00
|
|
|
std::vector<Extension> extensions;
|
2020-03-16 02:56:04 -06:00
|
|
|
ExtenWindow* This = nullptr;
|
2018-11-01 14:07:42 -04:00
|
|
|
|
2019-06-04 23:14:46 -04:00
|
|
|
bool Load(QString extenName)
|
2018-11-01 14:07:42 -04:00
|
|
|
{
|
2020-09-10 07:18:17 -06:00
|
|
|
if (extenName.endsWith(".dll")) extenName.chop(4);
|
|
|
|
if (extenName.endsWith(".xdll")) extenName.chop(5);
|
|
|
|
if (!QFile::exists(extenName + ".xdll")) QFile::copy(extenName + ".dll", extenName + ".xdll");
|
|
|
|
// Extension must export "OnNewSentence"
|
|
|
|
if (QTextFile(extenName + ".xdll", QIODevice::ReadOnly).readAll().contains("OnNewSentence"))
|
2019-01-11 17:14:49 -05:00
|
|
|
{
|
2020-09-10 07:18:17 -06:00
|
|
|
if (HMODULE module = LoadLibraryW(S(extenName + ".xdll").c_str()))
|
2019-06-04 23:14:46 -04:00
|
|
|
{
|
2019-09-16 17:14:59 -04:00
|
|
|
if (auto callback = (decltype(Extension::callback))GetProcAddress(module, "OnNewSentence"))
|
2019-08-29 13:46:21 -04:00
|
|
|
{
|
|
|
|
std::scoped_lock writeLock(extenMutex);
|
|
|
|
extensions.push_back({ S(extenName), callback });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
FreeLibrary(module);
|
2019-06-04 23:14:46 -04:00
|
|
|
}
|
2019-01-11 17:14:49 -05:00
|
|
|
}
|
2019-06-04 23:14:46 -04:00
|
|
|
return false;
|
2018-11-01 14:07:42 -04:00
|
|
|
}
|
|
|
|
|
2019-02-09 00:30:38 -05:00
|
|
|
void Unload(int index)
|
2018-11-01 14:07:42 -04:00
|
|
|
{
|
2019-01-09 22:35:01 -05:00
|
|
|
std::scoped_lock writeLock(extenMutex);
|
2020-09-10 07:18:17 -06:00
|
|
|
FreeLibrary(GetModuleHandleW((extensions.at(index).name + L".xdll").c_str()));
|
2019-02-09 00:30:38 -05:00
|
|
|
extensions.erase(extensions.begin() + index);
|
2018-11-01 14:07:42 -04:00
|
|
|
}
|
2018-11-01 14:46:37 -04:00
|
|
|
|
|
|
|
void Reorder(QStringList extenNames)
|
|
|
|
{
|
2019-01-09 22:35:01 -05:00
|
|
|
std::scoped_lock writeLock(extenMutex);
|
2019-02-09 00:30:38 -05:00
|
|
|
std::vector<Extension> extensions;
|
2019-02-13 18:23:42 -05:00
|
|
|
for (auto extenName : extenNames)
|
2020-01-18 23:25:57 -07:00
|
|
|
extensions.push_back(*std::find_if(::extensions.begin(), ::extensions.end(), [&](Extension extension) { return extension.name == S(extenName); }));
|
2019-02-09 00:30:38 -05:00
|
|
|
::extensions = extensions;
|
2018-11-01 14:46:37 -04:00
|
|
|
}
|
2020-03-16 02:56:04 -06:00
|
|
|
|
|
|
|
void Sync()
|
|
|
|
{
|
|
|
|
ui.extenList->clear();
|
|
|
|
QTextFile extenSaveFile(EXTEN_SAVE_FILE, QIODevice::WriteOnly | QIODevice::Truncate);
|
|
|
|
concurrency::reader_writer_lock::scoped_lock_read readLock(extenMutex);
|
|
|
|
for (auto extension : extensions)
|
|
|
|
{
|
|
|
|
ui.extenList->addItem(S(extension.name));
|
|
|
|
extenSaveFile.write((S(extension.name) + ">").toUtf8());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Add(QFileInfo extenFile)
|
|
|
|
{
|
2020-09-10 07:18:17 -06:00
|
|
|
if (extenFile.suffix() == "dll" || extenFile.suffix() == "xdll")
|
2020-03-16 02:56:04 -06:00
|
|
|
{
|
|
|
|
if (extenFile.absolutePath() != QDir::currentPath())
|
|
|
|
{
|
|
|
|
if (QFile::exists(extenFile.fileName()) && QMessageBox::question(This, EXTENSIONS, CONFIRM_EXTENSION_OVERWRITE) == QMessageBox::Yes) QFile::remove(extenFile.fileName());
|
|
|
|
if (!QFile::copy(extenFile.absoluteFilePath(), extenFile.fileName())) QMessageBox::warning(This, EXTENSIONS, EXTENSION_WRITE_ERROR);
|
|
|
|
}
|
2020-09-10 07:18:17 -06:00
|
|
|
if (Load(extenFile.fileName())) return Sync();
|
2020-03-16 02:56:04 -06:00
|
|
|
}
|
|
|
|
QMessageBox::information(This, EXTENSIONS, QString(INVALID_EXTENSION).arg(extenFile.fileName()));
|
|
|
|
}
|
|
|
|
|
2021-03-08 08:46:34 -07:00
|
|
|
void Delete()
|
2020-03-16 02:56:04 -06:00
|
|
|
{
|
2021-03-08 08:46:34 -07:00
|
|
|
if (ui.extenList->currentItem())
|
|
|
|
{
|
|
|
|
Unload(ui.extenList->currentIndex().row());
|
|
|
|
Sync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContextMenu(QPoint point)
|
|
|
|
{
|
|
|
|
QAction addExtension(ADD_EXTENSION), removeExtension(REMOVE_EXTENSION);
|
|
|
|
if (auto action = QMenu::exec({ &addExtension, &removeExtension }, ui.extenList->mapToGlobal(point), nullptr, This))
|
|
|
|
if (action == &removeExtension) Delete();
|
2021-03-09 21:32:56 -07:00
|
|
|
else if (QString extenFile = QFileDialog::getOpenFileName(This, ADD_EXTENSION, ".", EXTENSIONS + QString(" (*.xdll);;Libraries (*.dll)")); !extenFile.isEmpty()) Add(extenFile);
|
2020-03-16 02:56:04 -06:00
|
|
|
}
|
2018-11-01 14:07:42 -04:00
|
|
|
}
|
|
|
|
|
2019-09-04 12:29:48 -04:00
|
|
|
bool DispatchSentenceToExtensions(std::wstring& sentence, const InfoForExtension* sentenceInfo)
|
2018-11-01 14:07:42 -04:00
|
|
|
{
|
2019-02-18 20:57:10 -05:00
|
|
|
wchar_t* sentenceBuffer = (wchar_t*)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, (sentence.size() + 1) * sizeof(wchar_t));
|
2018-11-01 14:07:42 -04:00
|
|
|
wcscpy_s(sentenceBuffer, sentence.size() + 1, sentence.c_str());
|
2019-02-16 00:25:19 -05:00
|
|
|
concurrency::reader_writer_lock::scoped_lock_read readLock(extenMutex);
|
2019-02-09 00:30:38 -05:00
|
|
|
for (const auto& extension : extensions)
|
2021-03-08 08:46:34 -07:00
|
|
|
if (!*(sentenceBuffer = extension.callback(sentenceBuffer, sentenceInfo))) break;
|
2018-12-13 08:37:37 -05:00
|
|
|
sentence = sentenceBuffer;
|
2018-11-01 14:07:42 -04:00
|
|
|
HeapFree(GetProcessHeap(), 0, sentenceBuffer);
|
2019-02-18 20:57:10 -05:00
|
|
|
return !sentence.empty();
|
2018-11-01 14:07:42 -04:00
|
|
|
}
|
|
|
|
|
2019-06-21 01:29:48 -04:00
|
|
|
void CleanupExtensions()
|
|
|
|
{
|
|
|
|
std::scoped_lock writeLock(extenMutex);
|
2020-09-10 07:18:17 -06:00
|
|
|
for (auto extension : extensions) FreeLibrary(GetModuleHandleW((extension.name + L".xdll").c_str()));
|
2019-06-21 01:29:48 -04:00
|
|
|
extensions.clear();
|
|
|
|
}
|
|
|
|
|
2021-01-30 12:07:37 -07:00
|
|
|
ExtenWindow::ExtenWindow(QWidget* parent) : QMainWindow(parent, Qt::WindowCloseButtonHint)
|
2018-11-01 14:07:42 -04:00
|
|
|
{
|
2020-03-16 02:56:04 -06:00
|
|
|
This = this;
|
|
|
|
ui.setupUi(this);
|
|
|
|
ui.vboxLayout->addWidget(new QLabel(EXTEN_WINDOW_INSTRUCTIONS, this));
|
2018-12-21 14:18:43 -05:00
|
|
|
setWindowTitle(EXTENSIONS);
|
2018-11-01 14:07:42 -04:00
|
|
|
|
2021-03-08 08:46:34 -07:00
|
|
|
connect(ui.extenList, &QListWidget::customContextMenuRequested, ContextMenu);
|
2020-03-16 02:56:04 -06:00
|
|
|
ui.extenList->installEventFilter(this);
|
2018-11-01 14:07:42 -04:00
|
|
|
|
2019-01-25 22:49:50 -05:00
|
|
|
if (!QFile::exists(EXTEN_SAVE_FILE)) QTextFile(EXTEN_SAVE_FILE, QIODevice::WriteOnly).write(DEFAULT_EXTENSIONS);
|
2019-01-10 21:47:16 -05:00
|
|
|
for (auto extenName : QString(QTextFile(EXTEN_SAVE_FILE, QIODevice::ReadOnly).readAll()).split(">")) Load(extenName);
|
2018-11-01 14:07:42 -04:00
|
|
|
Sync();
|
|
|
|
}
|
|
|
|
|
2018-12-28 12:14:56 -05:00
|
|
|
bool ExtenWindow::eventFilter(QObject* target, QEvent* event)
|
|
|
|
{
|
2021-03-08 08:46:34 -07:00
|
|
|
// https://stackoverflow.com/questions/1224432/how-do-i-respond-to-an-internal-drag-and-drop-operation-using-a-qlistwidget/1528215
|
2018-11-01 14:07:42 -04:00
|
|
|
if (event->type() == QEvent::ChildRemoved)
|
|
|
|
{
|
2018-11-01 14:46:37 -04:00
|
|
|
QStringList extenNames;
|
2020-03-16 02:56:04 -06: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();
|
|
|
|
}
|
2018-12-28 12:14:56 -05:00
|
|
|
return false;
|
2018-11-01 14:07:42 -04:00
|
|
|
}
|
|
|
|
|
2018-12-21 14:18:43 -05:00
|
|
|
void ExtenWindow::keyPressEvent(QKeyEvent* event)
|
|
|
|
{
|
2021-03-08 08:46:34 -07:00
|
|
|
if (event->key() == Qt::Key_Delete) Delete();
|
2018-12-21 14:18:43 -05:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-12-28 12:14:56 -05:00
|
|
|
void ExtenWindow::dropEvent(QDropEvent* event)
|
|
|
|
{
|
2019-08-29 13:46:21 -04:00
|
|
|
for (auto file : event->mimeData()->urls()) Add(file.toLocalFile());
|
2018-11-01 18:47:19 -04:00
|
|
|
}
|