Add R2Task class

This commit is contained in:
Florian Märkl 2018-07-22 15:04:13 +02:00
parent e4aec07f01
commit e0c7e625e4
5 changed files with 96 additions and 32 deletions

View File

@ -7,6 +7,7 @@
#include "utils/TempConfig.h"
#include "utils/Configuration.h"
#include "utils/AsyncTask.h"
#include "utils/R2Task.h"
#include "Cutter.h"
#include "sdb.h"
@ -194,20 +195,18 @@ QJsonDocument CutterCore::cmdj(const QString &str)
QString CutterCore::cmdTask(const QString &str)
{
RCoreTask *task = startTask(str);
joinTask(task);
QString ret = QString::fromUtf8(task->res);
deleteTask(task);
return ret;
R2Task task(str);
task.startTask();
task.joinTask();
return task.getResult();
}
QJsonDocument CutterCore::cmdjTask(const QString &str)
{
RCoreTask *task = startTask(str);
joinTask(task);
QJsonDocument ret = parseJson(task->res, str);
deleteTask(task);
return ret;
R2Task task(str);
task.startTask();
task.joinTask();
return parseJson(task.getResultRaw(), str);
}
QJsonDocument CutterCore::parseJson(const char *res, const QString &cmd)
@ -1749,22 +1748,6 @@ QList<QString> CutterCore::getColorThemes()
return r;
}
RCoreTask *CutterCore::startTask(const QString &cmd)
{
RCoreTask *task = r_core_task_new (core_, true, cmd.toLocal8Bit().constData(), nullptr, nullptr);
r_core_task_enqueue(core_, task);
return task;
}
void CutterCore::joinTask(RCoreTask *task)
{
r_core_task_join(core_, nullptr, task);
}
void CutterCore::deleteTask(RCoreTask *task)
{
r_core_task_del(core_, task->id);
}
void CutterCore::setCutterPlugins(QList<CutterPlugin*> plugins)
{
this->plugins = plugins;

View File

@ -524,10 +524,6 @@ public:
void triggerAsmOptionsChanged();
void triggerGraphOptionsChanged();
RCoreTask *startTask(const QString &cmd);
void joinTask(RCoreTask *task);
void deleteTask(RCoreTask *task);
void setCutterPlugins(QList<CutterPlugin*> plugins);
QList<CutterPlugin*> getCutterPlugins();

View File

@ -171,7 +171,8 @@ SOURCES += \
widgets/BacktraceWidget.cpp \
dialogs/OpenFileDialog.cpp \
utils/CommandTask.cpp \
utils/ProgressIndicator.cpp
utils/ProgressIndicator.cpp \
utils/R2Task.cpp
HEADERS += \
Cutter.h \
@ -257,7 +258,8 @@ HEADERS += \
utils/FunctionsTask.h \
utils/CommandTask.h \
utils/ProgressIndicator.h \
plugins/CutterPlugin.h
plugins/CutterPlugin.h \
utils/R2Task.h
FORMS += \
dialogs/AboutDialog.ui \

51
src/utils/R2Task.cpp Normal file
View File

@ -0,0 +1,51 @@
#include "R2Task.h"
R2Task::R2Task(const QString &cmd)
{
task = r_core_task_new(Core()->core(),
true,
cmd.toLocal8Bit().constData(),
static_cast<RCoreTaskCallback>(&R2Task::taskFinishedCallback),
this);
}
R2Task::~R2Task()
{
r_core_task_del(Core()->core(), task->id);
}
void R2Task::taskFinishedCallback(void *user, char *)
{
reinterpret_cast<R2Task *>(user)->taskFinished();
}
void R2Task::taskFinished()
{
emit finished();
}
void R2Task::startTask()
{
r_core_task_enqueue(Core()->core(), task);
}
void R2Task::breakTask()
{
r_core_task_break(Core()->core(), task->id);
}
void R2Task::joinTask()
{
r_core_task_join(Core()->core(), nullptr, task);
}
QString R2Task::getResult()
{
return QString::fromUtf8(task->res);
}
const char *R2Task::getResultRaw()
{
return task->res;
}

32
src/utils/R2Task.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef R2TASK_H
#define R2TASK_H
#include "Cutter.h"
class R2Task: public QObject
{
Q_OBJECT
private:
RCoreTask *task;
static void taskFinishedCallback(void *user, char *);
void taskFinished();
public:
explicit R2Task(const QString &cmd);
~R2Task();
void startTask();
void breakTask();
void joinTask();
QString getResult();
const char *getResultRaw();
signals:
void finished();
};
#endif // R2TASK_H