add drag'n'drop for extensions

This commit is contained in:
Akash Mozumdar 2018-11-01 18:47:19 -04:00
parent 4b82d545bf
commit ee0a8c4887
4 changed files with 47 additions and 21 deletions

View File

@ -1,7 +1,10 @@
#include "extenwindow.h" #include "extenwindow.h"
#include "ui_extenwindow.h" #include "ui_extenwindow.h"
#include "defs.h"
#include "types.h" #include "types.h"
#include <QFileDialog> #include <QFileDialog>
#include <QMimeData>
#include <QUrl>
namespace namespace
{ {
@ -19,6 +22,7 @@ namespace
void Load(QString extenName) void Load(QString extenName)
{ {
if (extenName == ITH_DLL) return;
// Extension is dll and exports "OnNewSentence" // Extension is dll and exports "OnNewSentence"
HMODULE module = GetModuleHandleW(extenName.toStdWString().c_str()); HMODULE module = GetModuleHandleW(extenName.toStdWString().c_str());
if (!module) module = LoadLibraryW(extenName.toStdWString().c_str()); if (!module) module = LoadLibraryW(extenName.toStdWString().c_str());
@ -91,19 +95,25 @@ ExtenWindow::~ExtenWindow()
delete ui; delete ui;
} }
void ExtenWindow::on_addButton_clicked() void ExtenWindow::Sync()
{ {
QString extenFileName = QFileDialog::getOpenFileName(this, "Select Extension", "C:\\", "Extensions (*.dll)"); extenList->clear();
if (!extenFileName.size()) return; extenSaveFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
QString extenName = extenFileName.mid(extenFileName.lastIndexOf("/") + 1); std::shared_lock sharedLock(extenMutex);
QFile::copy(extenFileName, extenName); for (auto extenName : extenNames)
Load(extenName.left(extenName.lastIndexOf(".dll"))); {
Sync(); extenList->addItem(extenName);
extenSaveFile.write((extenName + ">").toUtf8());
}
extenSaveFile.close();
} }
void ExtenWindow::on_rmvButton_clicked() void ExtenWindow::Add(QString fileName)
{ {
if (auto extenName = extenList->currentItem()) Unload(extenName->text()); if (!fileName.endsWith(".dll")) return;
QString extenName = fileName.mid(fileName.lastIndexOf("/") + 1);
QFile::copy(fileName, extenName);
Load(extenName.split(".dll")[0]);
Sync(); Sync();
} }
@ -120,15 +130,23 @@ bool ExtenWindow::eventFilter(QObject* target, QEvent* event)
return false; return false;
} }
void ExtenWindow::Sync() void ExtenWindow::dragEnterEvent(QDragEnterEvent* event)
{ {
extenList->clear(); event->acceptProposedAction();
extenSaveFile.open(QIODevice::WriteOnly | QIODevice::Truncate); }
std::shared_lock sharedLock(extenMutex);
for (auto extenName : extenNames) void ExtenWindow::dropEvent(QDropEvent* event)
{ {
extenList->addItem(extenName); for (auto file : event->mimeData()->urls()) Add(file.toLocalFile());
extenSaveFile.write((extenName + ">").toUtf8());
} }
extenSaveFile.close();
void ExtenWindow::on_addButton_clicked()
{
Add(QFileDialog::getOpenFileName(this, "Select Extension", "C:\\", "Extensions (*.dll)"));
}
void ExtenWindow::on_rmvButton_clicked()
{
if (auto extenName = extenList->currentItem()) Unload(extenName->text());
Sync();
} }

View File

@ -4,6 +4,8 @@
#include "qtcommon.h" #include "qtcommon.h"
#include <shared_mutex> #include <shared_mutex>
#include <QListWidget> #include <QListWidget>
#include <QDragEnterEvent>
#include <QDropEvent>
namespace Ui namespace Ui
{ {
@ -25,8 +27,11 @@ private slots:
void on_rmvButton_clicked(); void on_rmvButton_clicked();
private: private:
bool eventFilter(QObject* target, QEvent* event); void Add(QString fileName);
void Sync(); void Sync();
bool eventFilter(QObject* target, QEvent* event);
void dragEnterEvent(QDragEnterEvent* event);
void dropEvent(QDropEvent* event);
Ui::ExtenWindow* ui; Ui::ExtenWindow* ui;
QFile extenSaveFile = QFile("Extensions.txt"); QFile extenSaveFile = QFile("Extensions.txt");

View File

@ -10,6 +10,9 @@
<height>300</height> <height>300</height>
</rect> </rect>
</property> </property>
<property name="acceptDrops">
<bool>true</bool>
</property>
<property name="windowTitle"> <property name="windowTitle">
<string>Extensions</string> <string>Extensions</string>
</property> </property>

View File

@ -3,7 +3,7 @@
// vnrhook/defs.h // vnrhook/defs.h
// 8/23/2013 jichi // 8/23/2013 jichi
#define ITH_DLL L"vnrhook.dll" #define ITH_DLL L"vnrhook"
// Pipes // Pipes