diff options
Diffstat (limited to 'map')
-rw-r--r-- | map/CMakeLists.txt | 2 | ||||
-rw-r--r-- | map/map.cpp | 80 | ||||
-rw-r--r-- | map/map.h | 50 | ||||
-rw-r--r-- | map/mapmessage.cpp | 29 | ||||
-rw-r--r-- | map/mapmessage.h | 39 |
5 files changed, 200 insertions, 0 deletions
diff --git a/map/CMakeLists.txt b/map/CMakeLists.txt new file mode 100644 index 0000000..2e58fbe --- /dev/null +++ b/map/CMakeLists.txt @@ -0,0 +1,2 @@ +add_headers(map.h mapmessage.h) +add_sources(map.cpp mapmessage.cpp) diff --git a/map/map.cpp b/map/map.cpp new file mode 100644 index 0000000..eb3d5e0 --- /dev/null +++ b/map/map.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2019 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "message.h" +#include "messageengine.h" +#include "map.h" +#include "mapmessage.h" +#include "responsemessage.h" + +Map::Map (QUrl &url, QObject * parent) : + QObject(parent), + m_mloop(nullptr) +{ + m_mloop = new MessageEngine(url); + QObject::connect(m_mloop, &MessageEngine::connected, this, &Map::onConnected); + QObject::connect(m_mloop, &MessageEngine::disconnected, this, &Map::onDisconnected); + QObject::connect(m_mloop, &MessageEngine::messageReceived, this, &Map::onMessageReceived); +} + +Map::~Map() +{ + delete m_mloop; +} + +void Map::compose(QString recipient, QString message) +{ + MapMessage *tmsg = new MapMessage(); + QJsonObject parameter; + parameter.insert("recipient", recipient); + parameter.insert("message", message); + tmsg->createRequest("compose", parameter); + m_mloop->sendMessage(tmsg); + delete tmsg; +} + +void Map::onConnected() +{ + MapMessage *tmsg = new MapMessage(); + QJsonObject parameter; + parameter.insert("value", "notification"); + tmsg->createRequest("subscribe", parameter); + m_mloop->sendMessage(tmsg); + delete tmsg; +} + +void Map::onDisconnected() +{ + MapMessage *tmsg = new MapMessage(); + QJsonObject parameter; + parameter.insert("value", "notification"); + tmsg->createRequest("unsubscribe", parameter); + m_mloop->sendMessage(tmsg); + delete tmsg; +} + +void Map::onMessageReceived(MessageType type, Message *msg) +{ + if (type == MapEventMessage) { + MapMessage *tmsg = qobject_cast<MapMessage*>(msg); + + if (tmsg->isNotificationEvent()) { + emit notificationEvent(tmsg->eventData().toVariantMap()); + } + } + + msg->deleteLater(); +} diff --git a/map/map.h b/map/map.h new file mode 100644 index 0000000..e712b93 --- /dev/null +++ b/map/map.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2019 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MAP_H +#define MAP_H + +#include <QDebug> +#include <QObject> +#include <QJsonArray> +#include <QtQml/QQmlContext> +#include <QtQml/QQmlListProperty> + +#include "messageengine.h" + +class Map : public QObject +{ + Q_OBJECT + + public: + explicit Map(QUrl &url, QObject * parent = Q_NULLPTR); + virtual ~Map(); + + Q_INVOKABLE void compose(QString recipient, QString message); + + signals: + void notificationEvent(QVariantMap message); + + private: + MessageEngine *m_mloop; + + // slots + void onConnected(); + void onDisconnected(); + void onMessageReceived(MessageType, Message*); +}; + +#endif // MAP_H diff --git a/map/mapmessage.cpp b/map/mapmessage.cpp new file mode 100644 index 0000000..88957bc --- /dev/null +++ b/map/mapmessage.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2019 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <QJsonArray> +#include <QJsonDocument> +#include <QJsonObject> + +#include "mapmessage.h" + +bool MapMessage::createRequest(QString verb, QJsonObject parameter) +{ + if (!verbs.contains(verb)) + return false; + + return Message::createRequest("bluetooth-map", verb, parameter); +} diff --git a/map/mapmessage.h b/map/mapmessage.h new file mode 100644 index 0000000..4312519 --- /dev/null +++ b/map/mapmessage.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2019 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MAP_MESSAGE_H +#define MAP_MESSAGE_H + +#include "message.h" + +class MapMessage : public Message +{ + Q_OBJECT + public: + bool createRequest(QString verb, QJsonObject parameter); + bool isNotificationEvent() {return (this->eventName() == "notification"); }; + + private: + QStringList verbs { + "compose", + "message", + "list_messages", + "subscribe", + "unsubscribe", + }; +}; + +#endif // MAP_MESSAGE_H |