feat: prettify model event handling logic
This commit is contained in:
parent
b70d73ce96
commit
664d3cff16
@ -94,6 +94,23 @@
|
|||||||
return ret; \
|
return ret; \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define DEFINE_EXECUTE_API_USING_STANDARD_EVEN_HANDLE_MODEL \
|
||||||
|
EXECUTE_MODULE() { \
|
||||||
|
auto event_id = event["event_id"]; \
|
||||||
|
auto it = _gr_module_event_handlers.find(event_id); \
|
||||||
|
if (it != _gr_module_event_handlers.end()) { \
|
||||||
|
return it.value()(event); \
|
||||||
|
} \
|
||||||
|
CB_ERR(event, -1, QString("unsupported event id: %1").arg(event_id)); \
|
||||||
|
} \
|
||||||
|
END_EXECUTE_MODULE()
|
||||||
|
|
||||||
|
#define REGISTER_EVENT_HANDLER(event_id, handler) \
|
||||||
|
static const bool _gv_register_event_handler_by_id_##event_id = [] { \
|
||||||
|
_gr_module_event_handlers[#event_id] = handler; \
|
||||||
|
return true; \
|
||||||
|
}();
|
||||||
|
|
||||||
inline void MLogDebug(const QString& s) { GFModuleLogDebug(s.toUtf8()); }
|
inline void MLogDebug(const QString& s) { GFModuleLogDebug(s.toUtf8()); }
|
||||||
inline void MLogInfo(const QString& s) { GFModuleLogInfo(s.toUtf8()); }
|
inline void MLogInfo(const QString& s) { GFModuleLogInfo(s.toUtf8()); }
|
||||||
inline void MLogWarn(const QString& s) { GFModuleLogWarn(s.toUtf8()); }
|
inline void MLogWarn(const QString& s) { GFModuleLogWarn(s.toUtf8()); }
|
||||||
@ -121,11 +138,22 @@ inline auto UnStrDup(const char* s) -> QString {
|
|||||||
return q_s;
|
return q_s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline auto FormatStringHelper(const QString& format,
|
||||||
|
const std::string& arg) -> QString {
|
||||||
|
return format.arg(QString::fromStdString(arg));
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
auto FormatStringHelper(const QString& format, T arg) -> QString {
|
auto FormatStringHelper(const QString& format, T arg) -> QString {
|
||||||
return format.arg(arg);
|
return format.arg(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
auto FormatStringHelper(const QString& format, const std::string& arg,
|
||||||
|
Args... args) -> QString {
|
||||||
|
return FormatStringHelper(format.arg(QString::fromStdString(arg)), args...);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T, typename... Args>
|
template <typename T, typename... Args>
|
||||||
auto FormatStringHelper(const QString& format, T arg, Args... args) -> QString {
|
auto FormatStringHelper(const QString& format, T arg, Args... args) -> QString {
|
||||||
return FormatStringHelper(format.arg(arg), args...);
|
return FormatStringHelper(format.arg(arg), args...);
|
||||||
@ -135,6 +163,13 @@ inline auto FormatStringHelper(const QString& format) -> QString {
|
|||||||
return format;
|
return format;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline auto FormatStringHelper(const T& format) ->
|
||||||
|
typename std::enable_if<std::is_same<T, std::string>::value,
|
||||||
|
QString>::type {
|
||||||
|
return FormatStringHelper(QString::fromStdString(format));
|
||||||
|
}
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
auto FormatString(const QString& format, Args... args) -> QString {
|
auto FormatString(const QString& format, Args... args) -> QString {
|
||||||
return FormatStringHelper(format, args...);
|
return FormatStringHelper(format, args...);
|
||||||
|
@ -44,3 +44,23 @@
|
|||||||
return QMapToGFModuleMetaDataList( \
|
return QMapToGFModuleMetaDataList( \
|
||||||
{{"Name", (name)}, {"Description", (desc)}, {"Author", (author)}}); \
|
{{"Name", (name)}, {"Description", (desc)}, {"Author", (author)}}); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define GF_MODULE_API_DEFINE_V2(id, name, ver, desc, author) \
|
||||||
|
auto GFGetModuleGFSDKVersion() -> const char* { \
|
||||||
|
return DUP(GF_SDK_VERSION_STR); \
|
||||||
|
} \
|
||||||
|
auto GFGetModuleQtEnvVersion() -> const char* { \
|
||||||
|
return DUP(QT_VERSION_STR); \
|
||||||
|
} \
|
||||||
|
auto GFGetModuleID() -> const char* { return DUP((id)); } \
|
||||||
|
auto GFGetModuleVersion() -> const char* { return DUP((ver)); } \
|
||||||
|
auto GFGetModuleMetaData() -> GFModuleMetaData* { \
|
||||||
|
return QMapToGFModuleMetaDataList( \
|
||||||
|
{{"Name", (name)}, {"Description", (desc)}, {"Author", (author)}}); \
|
||||||
|
} \
|
||||||
|
using MEvent = QMap<QString, QString>; \
|
||||||
|
using EventHandler = std::function<int(const MEvent&)>; \
|
||||||
|
static QMap<QString, EventHandler> Module##nameEventHandlers; \
|
||||||
|
static QMap<QString, EventHandler>& _gr_module_event_handlers = \
|
||||||
|
Module##nameEventHandlers; \
|
||||||
|
DEFINE_EXECUTE_API_USING_STANDARD_EVEN_HANDLE_MODEL
|
@ -30,13 +30,13 @@
|
|||||||
|
|
||||||
#include <QtCore>
|
#include <QtCore>
|
||||||
|
|
||||||
#include "GFModuleCommonUtils.hpp"
|
|
||||||
#include "GFModuleDefine.h"
|
#include "GFModuleDefine.h"
|
||||||
#include "VKSInterface.h"
|
#include "VKSInterface.h"
|
||||||
|
|
||||||
GF_MODULE_API_DEFINE("com.bktus.gpgfrontend.module.key_server_sync",
|
GF_MODULE_API_DEFINE_V2("com.bktus.gpgfrontend.module.key_server_sync",
|
||||||
"KeyServerSync", "1.0.0",
|
"KeyServerSync", "1.0.0",
|
||||||
"Sync Information From Trusted Key Server.", "Saturneric")
|
"Sync Information From Trusted Key Server.",
|
||||||
|
"Saturneric")
|
||||||
|
|
||||||
auto GFRegisterModule() -> int {
|
auto GFRegisterModule() -> int {
|
||||||
LOG_DEBUG("key server sync module registering");
|
LOG_DEBUG("key server sync module registering");
|
||||||
@ -51,11 +51,8 @@ auto GFActiveModule() -> int {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
EXECUTE_MODULE() {
|
REGISTER_EVENT_HANDLER(
|
||||||
FLOG_DEBUG("key server sync module executing, event id: %1",
|
REQUEST_GET_PUBLIC_KEY_BY_FINGERPRINT, [](const MEvent& event) -> int {
|
||||||
event["event_id"]);
|
|
||||||
|
|
||||||
if (event["event_id"] == "REQUEST_GET_PUBLIC_KEY_BY_FINGERPRINT") {
|
|
||||||
if (event["fingerprint"].isEmpty())
|
if (event["fingerprint"].isEmpty())
|
||||||
CB_ERR(event, -1, "fingerprint is empty");
|
CB_ERR(event, -1, "fingerprint is empty");
|
||||||
|
|
||||||
@ -88,11 +85,11 @@ EXECUTE_MODULE() {
|
|||||||
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
|
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
|
||||||
&VKSInterface::deleteLater);
|
&VKSInterface::deleteLater);
|
||||||
vks->GetByFingerprint(fingerprint);
|
vks->GetByFingerprint(fingerprint);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
});
|
||||||
|
|
||||||
if (event["event_id"] == "REQUEST_GET_PUBLIC_KEY_BY_KEY_ID") {
|
REGISTER_EVENT_HANDLER(
|
||||||
|
REQUEST_GET_PUBLIC_KEY_BY_KEY_ID, [](const MEvent& event) -> int {
|
||||||
if (event["key_id"].isEmpty()) CB_ERR(event, -1, "key_id is empty");
|
if (event["key_id"].isEmpty()) CB_ERR(event, -1, "key_id is empty");
|
||||||
|
|
||||||
QByteArray key_id = event["key_id"].toLatin1();
|
QByteArray key_id = event["key_id"].toLatin1();
|
||||||
@ -125,9 +122,10 @@ EXECUTE_MODULE() {
|
|||||||
vks->GetByKeyId(key_id);
|
vks->GetByKeyId(key_id);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
});
|
||||||
|
|
||||||
if (event["event_id"] == "REQUEST_UPLOAD_PUBLIC_KEY") {
|
REGISTER_EVENT_HANDLER(
|
||||||
|
REQUEST_UPLOAD_PUBLIC_KEY, [](const MEvent& event) -> int {
|
||||||
if (event["key_text"].isEmpty()) CB_ERR(event, -1, "key_text is empty");
|
if (event["key_text"].isEmpty()) CB_ERR(event, -1, "key_text is empty");
|
||||||
|
|
||||||
QByteArray key_text = event["key_text"].toLatin1();
|
QByteArray key_text = event["key_text"].toLatin1();
|
||||||
@ -142,7 +140,8 @@ EXECUTE_MODULE() {
|
|||||||
{
|
{
|
||||||
{"ret", QString::number(0)},
|
{"ret", QString::number(0)},
|
||||||
{"fingerprint", fpr},
|
{"fingerprint", fpr},
|
||||||
{"status", QString::fromUtf8(QJsonDocument(status).toJson())},
|
{"status",
|
||||||
|
QString::fromUtf8(QJsonDocument(status).toJson())},
|
||||||
{"token", token},
|
{"token", token},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -162,11 +161,7 @@ EXECUTE_MODULE() {
|
|||||||
&VKSInterface::deleteLater);
|
&VKSInterface::deleteLater);
|
||||||
vks->UploadKey(key_text);
|
vks->UploadKey(key_text);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
});
|
||||||
|
|
||||||
CB_ERR(event, -1, "the type of this event is not supported");
|
|
||||||
}
|
|
||||||
END_EXECUTE_MODULE()
|
|
||||||
|
|
||||||
auto GFDeactivateModule() -> int { return 0; }
|
auto GFDeactivateModule() -> int { return 0; }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user