]> git.zarvox.org Git - imoo.git/commitdiff
ribbon: dispatch network event to ribbon manager
authorDrew Fisher <drew.m.fisher@gmail.com>
Thu, 24 Apr 2014 05:34:43 +0000 (22:34 -0700)
committerDrew Fisher <drew.m.fisher@gmail.com>
Thu, 24 Apr 2014 05:34:43 +0000 (22:34 -0700)
ribbon/main.cpp
ribbon/ribbonmanager.cpp
ribbon/ribbonmanager.h
ribbon/ribbonserver.cpp
ribbon/ribbonserver.h

index 1e183d8f00a92c984e946e50c94285e3e0323db2..3103aff74528d76cef418a34cac3ebc3de6dba88 100644 (file)
@@ -32,12 +32,14 @@ int main(int argc, char** argv)
 
        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();
 }
index 1f373848304ed7ff1773e313914766be2977fd50..373d3e74e9f49f601e1be483186d263c22b7a451 100644 (file)
@@ -290,6 +290,12 @@ void RibbonManager::received_im_msg(PurpleAccount *account, char *sender, char *
        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");
index 0773bfd25eca78f553d6cc1738def9a5c0e0075d..7d9de966afe32627c1b3bba48a69845498c205c8 100644 (file)
@@ -23,6 +23,7 @@ public:
        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:
index d14ac3d6ad19f7854dff42b9a5f85586d4dcb3c7..e3bbc8a2213db4d72f153116a9e8b7a2846f23d7 100644 (file)
@@ -7,6 +7,17 @@
 #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)
 {
@@ -63,8 +74,27 @@ void RibbonServer::onMessageReceived(RibbonSocket* r, QByteArray buf)
                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";
        }
 }
 
index f1b32bf453749b358ff376b40979d906eb674708..17143c46087ca8cd118baece24b3ff97d3451e2e 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <QObject>
 #include <QList>
+#include <QVariantMap>
 #include <QtNetwork/QHostAddress>
 
 // forward declarations
@@ -21,6 +22,7 @@ public slots:
        void sendEventToClient(QString sender, QString method, QVariantMap ctx);
 signals:
        void newConnection();
+       void messageReceived(QString destination, QString method, QVariantMap ctx);
 private:
        QTcpServer* server;
        QHostAddress _addr;