diff options
author | Javier <dev.git@javispedro.com> | 2015-03-31 02:38:50 +0200 |
---|---|---|
committer | Javier <dev.git@javispedro.com> | 2015-03-31 02:38:50 +0200 |
commit | 631af0284a651f1698e4796c2e39aabb83a3bfca (patch) | |
tree | c7a2e0f5a898c4a68aa9482757718364ba93f83f /saltoqd/cardmanager.cpp | |
parent | 1a72dfb10bb0189e842c754b3550795538a79493 (diff) | |
download | saltoq-631af0284a651f1698e4796c2e39aabb83a3bfca.tar.gz saltoq-631af0284a651f1698e4796c2e39aabb83a3bfca.zip |
parse incoming card events
Diffstat (limited to 'saltoqd/cardmanager.cpp')
-rw-r--r-- | saltoqd/cardmanager.cpp | 97 |
1 files changed, 85 insertions, 12 deletions
diff --git a/saltoqd/cardmanager.cpp b/saltoqd/cardmanager.cpp index 5418104..1bf8dd4 100644 --- a/saltoqd/cardmanager.cpp +++ b/saltoqd/cardmanager.cpp @@ -1,3 +1,4 @@ +#include <QtCore/QRegularExpression> #include "cardmanager.h" Card::Card(const QString &id, QObject *parent) : @@ -153,6 +154,11 @@ Card* CardDeck::cardAt(int position) const return _deck.at(position); } +Card* CardDeck::cardAt(const QString &id) const +{ + return _ids.value(id); +} + void CardDeck::appendCard(Card *card) { insertCard(_deck.size(), card); @@ -215,7 +221,33 @@ CardManager::CardManager(FmsManager *fms, ToqManager *toq) : void CardManager::handleMessage(const ToqConnection::Message &msg) { - + QPair<CardDeck*, QString> p = unpackMessage(msg.payload); + CardDeck *deck = p.first; + if (!deck) { + return; + } + QHash<QString,QString> dict = unpackDictionary(p.second); + qDebug() << dict; + QString id = dict["id"]; + Card *card = deck->cardAt(id); + if (card) { + QString event = dict["event"]; + if (event == "open") { + card->setOpen(true); + } else if (event == "closed") { + card->setOpen(false); + } else if (event == "visible") { + card->setVisible(true); + } else if (event == "invisible") { + card->setVisible(false); + } else if (event == "selected") { + emit card->optionSelected(dict["eventdata"]); + } else { + qWarning() << "Unknown card event" << event; + } + } else { + qWarning() << "Card" << id << "not found in deck" << deck->package(); + } } void CardManager::installDeck(CardDeck *deck) @@ -246,7 +278,7 @@ QString CardManager::generateCardDescription(const QString &verb, Card *card) co { QString desc = verb + " { "; desc += QString("id = \"%1\", ").arg(card->id()); - desc += QString("version = 2, "); + desc += QString("version = 2, cardevents = \"true\", "); if (!card->header().isEmpty()) { desc += QString("header = \"%1\", ").arg(card->header()); } @@ -257,7 +289,7 @@ QString CardManager::generateCardDescription(const QString &verb, Card *card) co desc += QString("time = \"%1\", ").arg(card->dateTime().toMSecsSinceEpoch()); } if (!card->info().isEmpty()) { - desc += QString("title = \"%1\", ").arg(card->info()); + desc += QString("info = \"%1\", ").arg(card->info()); } if (!card->isVibrate()) { desc += QString("suppressvibe = \"true\", "); @@ -269,6 +301,47 @@ QString CardManager::generateCardDescription(const QString &verb, Card *card) co return desc; } +QByteArray CardManager::packMessage(CardDeck *deck, const QString &msg) const +{ + QByteArray data; + QByteArray target = QString("%1/%2").arg(deck->package()).arg(deck->application()).toUtf8(); + QByteArray payload = msg.toUtf8(); + + target.append('\0'); // Null terminated + + data.reserve(1 + target.size() + payload.size()); + data.append(char(target.size())); + data.append(target); + data.append(payload); + return data; +} + +QPair<CardDeck*,QString> CardManager::unpackMessage(const QByteArray &data) const +{ + CardDeck *deck = 0; + QString payload; + int id_size = data.at(0); + QStringList id = QString::fromUtf8(data.mid(1, id_size)).split('/'); + deck = _decks.value(id.at(0)); + payload = QString::fromUtf8(data.mid(1 + id_size)); + if (!deck) { + qWarning() << "Deck with id" << id[0] << "not found"; + } + return QPair<CardDeck*, QString>(deck, payload); +} + +QHash<QString,QString> CardManager::unpackDictionary(const QString &data) const +{ + QHash<QString,QString> dict; + const QRegularExpression re("(\\w+) = \"([^\"]*)\""); + QRegularExpressionMatchIterator it = re.globalMatch(data); + while (it.hasNext()) { + QRegularExpressionMatch m = it.next(); + dict.insert(m.captured(1), m.captured(2)); + } + return dict; +} + void CardManager::refreshDeck(CardDeck *deck) { Q_ASSERT(deck); @@ -282,13 +355,6 @@ void CardManager::refreshDeck(CardDeck *deck) data += generateCardDescription("NotifyCard", card); } -#if 0 - qDebug() << data; - data = QLatin1String( - "NotifyCard { id = \"jackpal.androidterm\", version = 2, app = \"Terminal\", title = \"Terminal\", time = \"1427073495379\", cardevents = \"true\", divider = \"true\", icon = \"fms:/jackpal.androidterm.img\", detail = { \"Ses de terminal en ejecucin\" } }\n" - "NotifyCard { id = \"com.android.vending\", version = 2, app = \"Google Play Store\", title = \"Simulador Chat\", time = \"1427073139463\", divider = \"true\", icon = \"fms:/com.android.vending.img\", detail = { \"Instalada correctamente\" } }\n"); -#endif - qDebug() << data; QString cardsFile = QString("/packages/%1/cards.dat").arg(deck->package()); @@ -317,8 +383,12 @@ void CardManager::handleDeckNeedsRefresh() void CardManager::handleCardAdded(Card *card) { CardDeck *deck = static_cast<CardDeck*>(sender()); + qDebug() << "Card added"; if (_toq->isConnected()) { - + QString cmd = generateCardDescription("PopupCard", card); + QByteArray payload = packMessage(deck, cmd); + qDebug() << cmd; + _toq->sendMessage(ToqConnection::AppMessagingEndpoint, ToqConnection::AppMessagingEndpoint + 1, 0x8000, payload); } } @@ -326,6 +396,9 @@ void CardManager::handleCardRemoved(Card *card) { CardDeck *deck = static_cast<CardDeck*>(sender()); if (_toq->isConnected()) { - + QString cmd = QString("DeleteCard { id = \"%1\" }\n").arg(card->id()); + QByteArray payload = packMessage(deck, cmd); + qDebug() << cmd; + _toq->sendMessage(ToqConnection::AppMessagingEndpoint, ToqConnection::AppMessagingEndpoint + 1, 0x8000, payload); } } |