feat: prettify model event handling logic

This commit is contained in:
saturneric 2024-11-25 22:42:13 +01:00
parent b70d73ce96
commit 664d3cff16
3 changed files with 160 additions and 110 deletions

View File

@ -94,6 +94,23 @@
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 MLogInfo(const QString& s) { GFModuleLogInfo(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;
}
inline auto FormatStringHelper(const QString& format,
const std::string& arg) -> QString {
return format.arg(QString::fromStdString(arg));
}
template <typename T>
auto FormatStringHelper(const QString& format, T arg) -> QString {
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>
auto FormatStringHelper(const QString& format, T arg, Args... args) -> QString {
return FormatStringHelper(format.arg(arg), args...);
@ -135,6 +163,13 @@ inline auto FormatStringHelper(const QString& format) -> QString {
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>
auto FormatString(const QString& format, Args... args) -> QString {
return FormatStringHelper(format, args...);

View File

@ -44,3 +44,23 @@
return QMapToGFModuleMetaDataList( \
{{"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

View File

@ -30,13 +30,13 @@
#include <QtCore>
#include "GFModuleCommonUtils.hpp"
#include "GFModuleDefine.h"
#include "VKSInterface.h"
GF_MODULE_API_DEFINE("com.bktus.gpgfrontend.module.key_server_sync",
"KeyServerSync", "1.0.0",
"Sync Information From Trusted Key Server.", "Saturneric")
GF_MODULE_API_DEFINE_V2("com.bktus.gpgfrontend.module.key_server_sync",
"KeyServerSync", "1.0.0",
"Sync Information From Trusted Key Server.",
"Saturneric")
auto GFRegisterModule() -> int {
LOG_DEBUG("key server sync module registering");
@ -51,122 +51,117 @@ auto GFActiveModule() -> int {
return 0;
}
EXECUTE_MODULE() {
FLOG_DEBUG("key server sync module executing, event id: %1",
event["event_id"]);
REGISTER_EVENT_HANDLER(
REQUEST_GET_PUBLIC_KEY_BY_FINGERPRINT, [](const MEvent& event) -> int {
if (event["fingerprint"].isEmpty())
CB_ERR(event, -1, "fingerprint is empty");
if (event["event_id"] == "REQUEST_GET_PUBLIC_KEY_BY_FINGERPRINT") {
if (event["fingerprint"].isEmpty())
CB_ERR(event, -1, "fingerprint is empty");
QByteArray fingerprint = event["fingerprint"].toLatin1();
FLOG_DEBUG("try to get key info of fingerprint: %1", fingerprint);
QByteArray fingerprint = event["fingerprint"].toLatin1();
FLOG_DEBUG("try to get key info of fingerprint: %1", fingerprint);
auto* vks = new VKSInterface();
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved,
QThread::currentThread(), [event](const QString& key) {
// callback
CB(event, GFGetModuleID(),
{
{"ret", QString::number(0)},
{"key_data", key},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
auto* vks = new VKSInterface();
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved,
QThread::currentThread(), [event](const QString& key) {
// callback
CB(event, GFGetModuleID(),
{
{"ret", QString::number(0)},
{"key_data", key},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
QObject::connect(vks, &VKSInterface::SignalErrorOccurred,
QThread::currentThread(),
[event](const QString& error, const QString& data) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(-1)},
{"error_msg", error},
{"reply_data", data},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
vks->GetByFingerprint(fingerprint);
return 0;
});
QObject::connect(vks, &VKSInterface::SignalErrorOccurred,
QThread::currentThread(),
[event](const QString& error, const QString& data) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(-1)},
{"error_msg", error},
{"reply_data", data},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
vks->GetByFingerprint(fingerprint);
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");
return 0;
}
QByteArray key_id = event["key_id"].toLatin1();
FLOG_DEBUG("try to get key info of key id: %1", key_id);
if (event["event_id"] == "REQUEST_GET_PUBLIC_KEY_BY_KEY_ID") {
if (event["key_id"].isEmpty()) CB_ERR(event, -1, "key_id is empty");
auto* vks = new VKSInterface();
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved,
QThread::currentThread(), [event](const QString& key) {
// callback
CB(event, GFGetModuleID(),
{
{"ret", QString::number(0)},
{"key_data", key},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
QObject::connect(vks, &VKSInterface::SignalErrorOccurred,
QThread::currentThread(),
[event](const QString& error, const QString& data) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(-1)},
{"error_msg", error},
{"reply_data", data},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
vks->GetByKeyId(key_id);
QByteArray key_id = event["key_id"].toLatin1();
FLOG_DEBUG("try to get key info of key id: %1", key_id);
return 0;
});
auto* vks = new VKSInterface();
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved,
QThread::currentThread(), [event](const QString& key) {
// callback
CB(event, GFGetModuleID(),
{
{"ret", QString::number(0)},
{"key_data", key},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
QObject::connect(vks, &VKSInterface::SignalErrorOccurred,
QThread::currentThread(),
[event](const QString& error, const QString& data) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(-1)},
{"error_msg", error},
{"reply_data", data},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
vks->GetByKeyId(key_id);
REGISTER_EVENT_HANDLER(
REQUEST_UPLOAD_PUBLIC_KEY, [](const MEvent& event) -> int {
if (event["key_text"].isEmpty()) CB_ERR(event, -1, "key_text is empty");
return 0;
}
QByteArray key_text = event["key_text"].toLatin1();
FLOG_DEBUG("try to get key info of key id: %1", key_text);
if (event["event_id"] == "REQUEST_UPLOAD_PUBLIC_KEY") {
if (event["key_text"].isEmpty()) CB_ERR(event, -1, "key_text is empty");
QByteArray key_text = event["key_text"].toLatin1();
FLOG_DEBUG("try to get key info of key id: %1", key_text);
auto* vks = new VKSInterface();
QObject::connect(
vks, &VKSInterface::SignalKeyUploaded, QThread::currentThread(),
[event](const QString& fpr, const QJsonObject& status,
const QString& token) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(0)},
{"fingerprint", fpr},
{"status", QString::fromUtf8(QJsonDocument(status).toJson())},
{"token", token},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
QObject::connect(vks, &VKSInterface::SignalErrorOccurred,
QThread::currentThread(),
[event](const QString& error, const QString& data) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(-1)},
{"error_msg", error},
{"reply_data", data},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
vks->UploadKey(key_text);
return 0;
}
CB_ERR(event, -1, "the type of this event is not supported");
}
END_EXECUTE_MODULE()
auto* vks = new VKSInterface();
QObject::connect(
vks, &VKSInterface::SignalKeyUploaded, QThread::currentThread(),
[event](const QString& fpr, const QJsonObject& status,
const QString& token) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(0)},
{"fingerprint", fpr},
{"status",
QString::fromUtf8(QJsonDocument(status).toJson())},
{"token", token},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
QObject::connect(vks, &VKSInterface::SignalErrorOccurred,
QThread::currentThread(),
[event](const QString& error, const QString& data) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(-1)},
{"error_msg", error},
{"reply_data", data},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
vks->UploadKey(key_text);
return 0;
});
auto GFDeactivateModule() -> int { return 0; }