QObject::connect(b, SIGNAL(clicked()),
purple, SLOT(sign_in_user()));
+
RibbonServer* server = new RibbonServer();
server->listen(QHostAddress::Any, 8888);
QObject::connect(purple, SIGNAL(event(QString,QString,QVariantMap)),
server, SLOT(sendEventToClient(QString,QString,QVariantMap)));
-
+ QObject::connect(server, SIGNAL(messageReceived(QString,QString,QVariantMap)),
+ purple, SLOT(handleExternalEvent(QString,QString,QVariantMap)));
return app->exec();
}
qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss") << sender << purple_conversation_get_name(conv) << message;
}
+void RibbonManager::handleExternalEvent(QString destination, QString method, QVariantMap context)
+{
+ // TODO: handle external event
+ qDebug() << "TODO: do something with" << destination << "." << method << "(" << context << ")";
+}
+
void RibbonManager::sign_in_user(void)
{
QFile file("account.txt");
void received_im_msg(PurpleAccount* account, char* sender, char* message, PurpleConversation* conv, PurpleMessageFlags flags);
public slots:
+ void handleExternalEvent(QString destination, QString method, QVariantMap context);
void sign_in_user(void);
signals:
#include <QJsonObject>
#include <QDebug>
+// Helper functions for sanitizing external events
+static bool hasStringMember(QVariantMap m, QString memberName)
+{
+ return m.contains(memberName) && m[memberName].type() == QVariant::String;
+}
+
+static bool hasMapMember(QVariantMap m, QString memberName)
+{
+ return m.contains(memberName) && m[memberName].type() == QVariant::Map;
+}
+
RibbonServer::RibbonServer(QObject* parent) :
QObject(parent)
{
qWarning() << buf.constData();
qWarning() << "hexencoded:" << buf.toHex().constData();
r->abort();
+ return;
+ }
+ qDebug() << "json document:" << json;
+ QJsonObject json_object = json.object();
+ if (json_object.isEmpty()) {
+ qWarning() << "Json document was not an object";
+ r->abort();
+ return;
+ }
+ QVariantMap m = json_object.toVariantMap();
+ // verify that m contains:
+ // - a string `destination`,
+ // - a string `method`, and
+ // - a map `params`
+ // If so, emit a signal with those items.
+ if (hasStringMember(m, "destination")
+ && hasStringMember(m, "method")
+ && hasMapMember(m, "params")) {
+ emit messageReceived(m["destination"].toString(), m["method"].toString(), m["params"].toMap());
} else {
- qDebug() << "json document:" << json;
+ qWarning() << "json did not validate, not emitting signal";
}
}
#include <QObject>
#include <QList>
+#include <QVariantMap>
#include <QtNetwork/QHostAddress>
// forward declarations
void sendEventToClient(QString sender, QString method, QVariantMap ctx);
signals:
void newConnection();
+ void messageReceived(QString destination, QString method, QVariantMap ctx);
private:
QTcpServer* server;
QHostAddress _addr;