diff options
author | Raquel Medina <raquel.medina@konsulko.com> | 2020-03-18 23:56:31 +0100 |
---|---|---|
committer | Raquel Medina <raquel.medina@konsulko.com> | 2020-03-23 14:02:28 +0100 |
commit | 0ed292d3ccf93c889734960676a321d1166d3f66 (patch) | |
tree | b8d64d1685d2f4bf599ab3867a0d3ff557ff0479 /pbap | |
parent | 5c750385d02116a92fa4c120ccc26abb8267bc97 (diff) |
rework message hierarchy
Rework message hierarchy with the final objective
of splitting libqtappfw into several libraries.
This commit carries the following changes:
- Simplify message hierarchy, keeping abstract
Message class, adding specialization for call and
event messages, keeping ResponseMessage, and
removing all module specific specializations.
- Add MessageFactory class to create message
objects.
- Change messages life cycle: using smart pointers
and removing QObject from message hierarchy (a
Message is not a QObject anymore and thus
'deleteLater()' is not available).
- Adapt all modules to use new message hierarchy.
- Keep ResponseMessage original constructor to
avoid breaking TaskManager.
- Message constructors have been kept public, but
will go private on a follow-up patch (once
TaskManager class has been modified to use new
MessageFactory).
Bug-AGL: SPEC-3112
Signed-off-by: Raquel Medina <raquel.medina@konsulko.com>
Change-Id: I3a7a6325209ddeca2293f1ac745371861a947bfb
Diffstat (limited to 'pbap')
-rw-r--r-- | pbap/CMakeLists.txt | 4 | ||||
-rw-r--r-- | pbap/pbap.cpp | 123 | ||||
-rw-r--r-- | pbap/pbap.h | 5 |
3 files changed, 80 insertions, 52 deletions
diff --git a/pbap/CMakeLists.txt b/pbap/CMakeLists.txt index 99edd7d..441280f 100644 --- a/pbap/CMakeLists.txt +++ b/pbap/CMakeLists.txt @@ -1,2 +1,2 @@ -add_headers(pbap.h pbapmessage.h) -add_sources(pbap.cpp pbapmessage.cpp) +add_headers(pbap.h) +add_sources(pbap.cpp) diff --git a/pbap/pbap.cpp b/pbap/pbap.cpp index 7b7829f..a0b84f4 100644 --- a/pbap/pbap.cpp +++ b/pbap/pbap.cpp @@ -19,9 +19,10 @@ #include <QMimeDatabase> #include <QtQml/QQmlEngine> -#include "message.h" -#include "pbapmessage.h" +#include "callmessage.h" +#include "eventmessage.h" #include "responsemessage.h" +#include "messagefactory.h" #include "messageengine.h" #include "pbap.h" @@ -102,50 +103,62 @@ Pbap::~Pbap() void Pbap::importContacts(int max_entries) { - PbapMessage *tmsg = new PbapMessage(); + std::unique_ptr<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call); + if (!msg) + return; + + CallMessage* pmsg = static_cast<CallMessage*>(msg.get()); QJsonObject parameter; - tmsg->createRequest("import", parameter); - m_mloop->sendMessage(tmsg); - delete tmsg; + pmsg->createRequest("bluetooth-pbap", "import", parameter); + m_mloop->sendMessage(std::move(msg)); } void Pbap::refreshContacts(int max_entries) { - PbapMessage *tmsg = new PbapMessage(); + std::unique_ptr<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call); + if (!msg) + return; + + CallMessage* pmsg = static_cast<CallMessage*>(msg.get()); QJsonObject parameter; - tmsg->createRequest("contacts", parameter); - m_mloop->sendMessage(tmsg); - delete tmsg; + pmsg->createRequest("bluetooth-pbap", "contacts", parameter); + m_mloop->sendMessage(std::move(msg)); } void Pbap::refreshCalls(int max_entries) { - PbapMessage *tmsg = new PbapMessage(); + std::unique_ptr<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call); + if (!msg) + return; + + CallMessage* pmsg = static_cast<CallMessage*>(msg.get()); QJsonObject parameter; parameter.insert("list", "cch"); if (max_entries >= 0) parameter.insert("max_entries", max_entries); - tmsg->createRequest("history", parameter); - m_mloop->sendMessage(tmsg); - delete tmsg; + pmsg->createRequest("bluetooth-pbap", "history", parameter); + m_mloop->sendMessage(std::move(msg)); } void Pbap::search(QString number) { - PbapMessage *tmsg = new PbapMessage(); + std::unique_ptr<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call); + if (!msg) + return; + + CallMessage* pmsg = static_cast<CallMessage*>(msg.get()); QJsonObject parameter; if (!number.isEmpty()) parameter.insert("number", number); parameter.insert("max_entries", 1); - tmsg->createRequest("search", parameter); - m_mloop->sendMessage(tmsg); - delete tmsg; + pmsg->createRequest("bluetooth-pbap", "search", parameter); + m_mloop->sendMessage(std::move(msg)); } bool compareContactPtr(QObject *a, QObject *b) @@ -253,55 +266,71 @@ void Pbap::sendSearchResults(QJsonArray results) void Pbap::onConnected() { QStringListIterator eventIterator(events); - PbapMessage *tmsg; while (eventIterator.hasNext()) { - tmsg = new PbapMessage(); + std::unique_ptr<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call); + if (!msg) + return; + + CallMessage* pmsg = static_cast<CallMessage*>(msg.get()); QJsonObject parameter; parameter.insert("value", eventIterator.next()); - tmsg->createRequest("subscribe", parameter); - m_mloop->sendMessage(tmsg); - delete tmsg; + pmsg->createRequest("bluetooth-pbap", "subscribe", parameter); + m_mloop->sendMessage(std::move(msg)); } } void Pbap::onDisconnected() { QStringListIterator eventIterator(events); - PbapMessage *tmsg; while (eventIterator.hasNext()) { - tmsg = new PbapMessage(); + std::unique_ptr<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call); + if (!msg) + return; + + CallMessage* pmsg = static_cast<CallMessage*>(msg.get()); QJsonObject parameter; parameter.insert("value", eventIterator.next()); - tmsg->createRequest("unsubscribe", parameter); - m_mloop->sendMessage(tmsg); - delete tmsg; + pmsg->createRequest("bluetooth-pbap", "unsubscribe", parameter); + m_mloop->sendMessage(std::move(msg)); } } -void Pbap::onMessageReceived(MessageType type, Message *msg) +void Pbap::onMessageReceived(std::shared_ptr<Message> msg) { - if (msg->isEvent() && type == MessageType::PbapEventMessage) { - PbapMessage *tmsg = qobject_cast<PbapMessage*>(msg); - - if (tmsg->isStatusEvent()) { - emit statusChanged(tmsg->connected()); - if (tmsg->connected() == true) { + if (!msg) + return; + + if (msg->isEvent()) { + std::shared_ptr<EventMessage> emsg = std::static_pointer_cast<EventMessage>(msg); + QString ename = emsg->eventName(); + QString eapi = emsg->eventApi(); + QVariantMap data = emsg->eventData().toVariantMap(); + + if (eapi != "pbap") + return; + + bool connected = data.find("connected").value().toBool(); + if (ename == "status") { + emit statusChanged(connected); + if (connected) refreshContacts(-1); } - } - } else if (msg->isReply() && type == MessageType::ResponseRequestMessage) { - ResponseMessage *tmsg = qobject_cast<ResponseMessage*>(msg); - - if (tmsg->requestVerb() == "contacts" || tmsg->requestVerb() == "import") { - updateContacts(tmsg->replyData().value("vcards").toArray()); - } else if (tmsg->requestVerb() == "history") { - updateCalls(tmsg->replyData().value("vcards").toArray()); - } else if (tmsg->requestVerb() == "search") { - sendSearchResults(tmsg->replyData().value("results").toArray()); + } else if (msg->isReply()) { + std::shared_ptr<ResponseMessage> rmsg = std::static_pointer_cast<ResponseMessage>(msg); + QString api = rmsg->requestApi(); + if (api != "pbap") + return; + + QString verb = rmsg->requestVerb(); + QJsonObject data = rmsg->replyData(); + if (verb == "contacts" || verb == "import") { + updateContacts(data.value("vcards").toArray()); + } else if (verb == "history") { + updateCalls(data.value("vcards").toArray()); + } else if (verb == "search") { + sendSearchResults(data.value("results").toArray()); } } - - msg->deleteLater(); } diff --git a/pbap/pbap.h b/pbap/pbap.h index a89bf2b..4deb9c6 100644 --- a/pbap/pbap.h +++ b/pbap/pbap.h @@ -17,6 +17,7 @@ #ifndef PBAP_H #define PBAP_H +#include <memory> #include <QObject> #include <QJsonArray> #include <QtQml/QQmlContext> @@ -25,8 +26,6 @@ class MessageEngine; class Message; -enum class MessageType; - class PhoneNumber : public QObject { Q_OBJECT @@ -169,7 +168,7 @@ class Pbap : public QObject // slots void onConnected(); void onDisconnected(); - void onMessageReceived(MessageType, Message*); + void onMessageReceived(std::shared_ptr<Message>); const QStringList events { "status", |