From a1a3ea825275671b105f5c0417e25a3c0a59ed25 Mon Sep 17 00:00:00 2001 From: Javier Date: Sat, 28 Mar 2015 23:09:44 +0100 Subject: implement storagemanager --- saltoqd/commmanager.cpp | 49 ++++++++++++++--- saltoqd/storagemanager.cpp | 127 +++++++++++++++++++++++++++++++++++++++++++-- saltoqd/storagemanager.h | 14 +++-- saltoqd/toqmanager.cpp | 2 +- 4 files changed, 175 insertions(+), 17 deletions(-) diff --git a/saltoqd/commmanager.cpp b/saltoqd/commmanager.cpp index 271749a..dc17e7d 100644 --- a/saltoqd/commmanager.cpp +++ b/saltoqd/commmanager.cpp @@ -1,7 +1,10 @@ +#include #include "commmanager.h" using namespace CommHistory; +static const int RECORD_LIMIT = 20; + CommManager::CommManager(StorageManager *storage, ToqManager *toq) : QObject(toq), _toq(toq), _storage(storage), _calls(new CallModel(this)), @@ -12,10 +15,10 @@ CommManager::CommManager(StorageManager *storage, ToqManager *toq) : _calls->setQueryMode(EventModel::AsyncQuery); _calls->setSorting(CallModel::SortByTime); _calls->setResolveContacts(true); - _calls->setLimit(20); + _calls->setLimit(RECORD_LIMIT); _convs->setQueryMode(EventModel::AsyncQuery); - _convs->setLimit(20); + _convs->setLimit(RECORD_LIMIT); _refreshTimer->setSingleShot(true); _refreshTimer->setInterval(1000); @@ -58,13 +61,16 @@ void CommManager::refresh() int rows = _calls->rowCount(); for (int i = 0; i < rows; i++) { Event e = _calls->event(i); - qDebug() << "Got call" << e.toString(); QDateTime dt = e.startTime(); QJsonObject obj; - obj.insert("CommsType", QLatin1String("Call")); - obj.insert("ReceivedTime", qint64(dt.toTime_t())); - obj.insert("CallerId", e.contactName()); - obj.insert("ItemId", e.id()); + obj.insert("Name", e.contactName()); + obj.insert("ContactId", QJsonValue()); + QJsonArray records; + QJsonObject record; + record.insert("CommsType", QLatin1String("Call")); + record.insert("ReceivedTime", qint64(dt.toTime_t())); + record.insert("CallerId", e.contactName()); + record.insert("ItemId", e.id()); QJsonObject details; details.insert("Duration", e.startTime().secsTo(e.endTime())); details.insert("PhoneType", QLatin1String("Other")); // TODO @@ -80,7 +86,9 @@ void CommManager::refresh() break; } details.insert("IsMissedCall", e.isMissedCall()); - obj.insert("CommsDetails", details); + record.insert("CommsDetails", details); + records.append(record); + obj.insert("CommsRecords", records); events.insert(dt, obj); } @@ -90,4 +98,29 @@ void CommManager::refresh() Group g = _convs->group(_convs->index(i, 0)); qDebug() << "Chat" << g.contactName(); } + + QJsonArray records; + + int i = 0; + auto it = events.begin(); + while (it != events.end()) { + QJsonObject record; + record.insert("RecordId", i); + record.insert("RecordPayload", it.value()); + records.append(record); + + ++it; + if (++i >= RECORD_LIMIT) break; + } + + + QString storeName("Phub.Phone.RecentComms"); + QJsonObject store; + store.insert("Name", storeName); + store.insert("Records", records); + + QJsonObject root; + root.insert("DataStore", store); + + _storage->updateStore(storeName, root); } diff --git a/saltoqd/storagemanager.cpp b/saltoqd/storagemanager.cpp index c2b9808..fe53cd6 100644 --- a/saltoqd/storagemanager.cpp +++ b/saltoqd/storagemanager.cpp @@ -1,7 +1,14 @@ +#include +#include "obexconnection.h" #include "storagemanager.h" -StorageManager::StorageManager(ToqManager *toq) : - QObject(toq), _toq(toq) +static QString generate_send_name(const QString &store, int checksum) +{ + return QString("%1_%2.jsn").arg(store).arg(checksum); +} + +StorageManager::StorageManager(ObexConnection *obex, ToqManager *toq) : + QObject(toq), _obex(obex), _toq(toq) { _toq->setEndpointListener(ToqConnection::StorageServiceEndpoint, this); } @@ -9,9 +16,121 @@ StorageManager::StorageManager(ToqManager *toq) : void StorageManager::handleMessage(const ToqConnection::Message &msg) { Q_ASSERT(msg.destination == ToqConnection::StorageServiceEndpoint); - switch (msg.type) { // TODO + switch (msg.type) { + case 0: + handleGetStoreStatusMessage(msg); + break; + case 1: + handleGetStoreMessage(msg); + break; default: - qWarning() << "Unknown message" << msg.type; + qWarning() << "Unknown message type" << msg.type; break; } } + +void StorageManager::handleGetStoreStatusMessage(const ToqConnection::Message &msg) +{ + QString storeName = msg.payload.object()["store"].toString(); + QJsonObject reply; + if (storeName.contains('*')) { + QJsonArray results; + for (auto it = _stores.begin(); it != _stores.end(); ++it) { + QJsonObject result; + result.insert("store", it.key()); + result.insert("sequence", qint64(it.value().checksum)); + results.append(result); + } + reply.insert("result", int(0)); + reply.insert("description", QString("Found all Stores")); + reply.insert("stores", results); + } else if (_stores.contains(storeName)) { + const Store &store = _stores[storeName]; + QJsonArray results; + QJsonObject result; + result.insert("store", storeName); + result.insert("sequence", qint64(store.checksum)); + results.append(result); + reply.insert("result", int(0)); + reply.insert("description", QString("Found the store %1").arg(storeName)); + reply.insert("stores", results); + } else { + qWarning() << "Store" << storeName << "not found!"; + reply.insert("result", int(-1)); + reply.insert("description", QString("Store %1 not found").arg(storeName)); + reply.insert("stores", QJsonArray()); + } + + _toq->sendReply(msg, 0x4000, reply); +} + +void StorageManager::handleGetStoreMessage(const ToqConnection::Message &msg) +{ + QString storeName = msg.payload.object()["store"].toString(); + QJsonObject reply; + if (_stores.contains(storeName)) { + const Store &store = _stores[storeName]; + reply.insert("result", int(0)); + reply.insert("description", QLatin1String("Update store request received")); + reply.insert("store", storeName); + reply.insert("sequence", qint64(store.checksum)); + reply.insert("transfer_mode", QLatin1String("OPP")); + QJsonObject opp; + QString filename = generate_send_name(storeName, store.checksum); + opp.insert("filename", filename); + opp.insert("checksum", qint64(store.checksum)); + reply.insert("OPP", opp); + _toq->sendReply(msg, 0x4001, reply); + + ObexTransfer *transfer = _obex->put(filename, store.contents); + transfer->setParent(this); + connect(transfer, &ObexTransfer::finished, + this, &StorageManager::handleObexFinished); + connect(transfer, &ObexTransfer::error, + this, &StorageManager::handleObexError); + } else { + reply.insert("result", int(-1)); + reply.insert("description", QString("Store %1 not found").arg(storeName)); + _toq->sendReply(msg, 0x4001, reply); + } +} + +void StorageManager::updateStore(const QString &storeName, const QJsonObject &json) +{ + Store& store = _stores[storeName]; + QJsonDocument doc(json); + QByteArray data = doc.toJson(QJsonDocument::Compact); + + qDebug() << "Store" << storeName << ":" << QString::fromUtf8(data); + + quint32 checksum = ToqConnection::checksum(data); + if (store.checksum != checksum) { + store.contents = data; + store.checksum = checksum; + + // Send updated store message + if (_toq->isConnected()) { + QJsonObject msg; + msg.insert("store", storeName); + msg.insert("sequence", qint64(checksum)); + _toq->sendMessage(ToqConnection::StorageServiceEndpoint, + ToqConnection::StorageServiceEndpoint + 1, + 0x8000, msg); + } + } +} + +void StorageManager::handleObexFinished() +{ + ObexTransfer *transfer = static_cast(sender()); + disconnect(transfer, 0, this, 0); + transfer->deleteLater(); +} + +void StorageManager::handleObexError(int resp) +{ + ObexTransfer *transfer = static_cast(sender()); + qWarning() << "Failure while updating store:" << resp; + disconnect(transfer, 0, this, 0); + transfer->deleteLater(); +} diff --git a/saltoqd/storagemanager.h b/saltoqd/storagemanager.h index fa97a8d..6e04956 100644 --- a/saltoqd/storagemanager.h +++ b/saltoqd/storagemanager.h @@ -7,23 +7,29 @@ class StorageManager : public QObject, public ToqManager::EndpointHandler { Q_OBJECT public: - explicit StorageManager(ToqManager *toq); + explicit StorageManager(ObexConnection *obex, ToqManager *toq); void handleMessage(const ToqConnection::Message &msg) Q_DECL_OVERRIDE; - void updateStore(const QString &id, const QByteArray &data); + void updateStore(const QString &store, const QJsonObject &json); private: + void handleGetStoreStatusMessage(const ToqConnection::Message &msg); void handleGetStoreMessage(const ToqConnection::Message &msg); struct Store { - QByteArray data; + QByteArray contents; quint32 checksum; }; +private slots: + void handleObexFinished(); + void handleObexError(int resp); + private: + ObexConnection *_obex; ToqManager *_toq; - QHash _stores; + QMap _stores; }; #endif // STORAGEMANAGER_H diff --git a/saltoqd/toqmanager.cpp b/saltoqd/toqmanager.cpp index 5db15c9..27331da 100644 --- a/saltoqd/toqmanager.cpp +++ b/saltoqd/toqmanager.cpp @@ -18,7 +18,7 @@ ToqManager::ToqManager(const QBluetoothAddress &address, QObject *parent) : _versionManager(new VersionManager(this)), _systemManager(new SystemManager(this)), _fmsManager(new FmsManager(_obex, this)), - _storageManager(new StorageManager(this)), + _storageManager(new StorageManager(_obex, this)), _musicManager(new MusicManager(this)), _commManager(new CommManager(_storageManager, this)), _voiceCallManager(new VoiceCallManager(this)), -- cgit v1.2.3