diff --git a/harbour-berail.pro b/harbour-berail.pro index 2cc44af..dd28bbf 100644 --- a/harbour-berail.pro +++ b/harbour-berail.pro @@ -80,6 +80,7 @@ RESOURCES += \ qml/resources/resources.qrc HEADERS += \ + src/connectiontracker.h \ src/logger.h \ src/os.h \ src/api.h \ @@ -105,6 +106,7 @@ HEADERS += \ src/models/stopabstract.h SOURCES += src/harbour-berail.cpp \ + src/connectiontracker.cpp \ src/logger.cpp \ src/os.cpp \ src/api.cpp \ diff --git a/qml/components/TripDelegate.qml b/qml/components/TripDelegate.qml index d5ecbad..b2d32f5 100644 --- a/qml/components/TripDelegate.qml +++ b/qml/components/TripDelegate.qml @@ -34,6 +34,7 @@ ListItem { anchors.fill: parent z: 1 onClicked: _showVias == true? _showVias = false: _showVias = true + onPressAndHold: connectionTracker.toggleTracked(model.id) enabled: vias.count() > 0 } diff --git a/qml/harbour-berail.qml b/qml/harbour-berail.qml index 0801ff9..7671b15 100644 --- a/qml/harbour-berail.qml +++ b/qml/harbour-berail.qml @@ -16,11 +16,13 @@ */ import QtQuick 2.2 +import QtQml.Models 2.2 import Sailfish.Silica 1.0 import Nemo.Configuration 1.0 import Nemo.DBus 2.0 import Harbour.BeRail.API 1.0 import Harbour.BeRail.SFOS 1.0 +import Harbour.BeRail.ConnectionTracker 1.0 import "pages" import "components" @@ -73,6 +75,21 @@ ApplicationWindow onErrorOccurred: sfos.createToaster(errorText, "icon-s-high-importance", "x-harbour-berail") } + ConnectionTracker { + id: connectionTracker + api: api + } + + Connections { + target: connectionTracker + onConnectionTracked: { + sfos.createToaster("Added to tracked routes", "icon-s-new", "x-harbour-berail") + } + onConnectionUntracked: { + sfos.createToaster("Deleted from tracked routes", "icon-m-input-remove", "x-harbour-berail") + } + } + DBusInterface { bus: DBus.SystemBus service: "net.connman" diff --git a/src/connectiontracker.cpp b/src/connectiontracker.cpp new file mode 100644 index 0000000..a3248f9 --- /dev/null +++ b/src/connectiontracker.cpp @@ -0,0 +1,30 @@ +#include + +#include "connectiontracker.h" + +#include "models/vialistmodel.h" +#include "api.h" +#include "os.h" + +ConnectionTracker::ConnectionTracker(QObject *parent) + : QObject(parent), m_connections() +{ + +} + + +void ConnectionTracker::toggleTracked(int i) { + auto connection = m_api->connections()->connectionList()[i]; + + auto uuid = connection->uuid(); + qDebug() << "toggle tracking of" << connection->from()->station()->name() << " to " << connection->to()->station()->name() + << "with uuid" << uuid; + + if(m_connections.contains(uuid)) { + m_connections.remove(uuid); + emit connectionUntracked(uuid); + } else { + m_connections.insert(uuid, connection); + emit connectionTracked(uuid, connection); + } +} diff --git a/src/connectiontracker.h b/src/connectiontracker.h new file mode 100644 index 0000000..ef94bdf --- /dev/null +++ b/src/connectiontracker.h @@ -0,0 +1,34 @@ +#ifndef CONNECTIONTRACKER_H +#define CONNECTIONTRACKER_H + +#include +#include +#include + +#include "models/connection.h" +#include "models/vialistmodel.h" + +#include "os.h" +#include "api.h" + +class ConnectionTracker: public QObject { + Q_OBJECT + Q_PROPERTY(API *api MEMBER m_api) +public: + ConnectionTracker(QObject *parent = nullptr); + Q_INVOKABLE void toggleTracked(int i); + + void setApi(API *api) { this->m_api = api; } + API *api() { return this->m_api; } + +signals: + void connectionTracked(QUuid, QSharedPointer); + void connectionUntracked(QUuid); + +private: + QMap> m_connections; + API *m_api; + OS sfos; +}; + +#endif // CONNECTIONTRACKER_H diff --git a/src/harbour-berail.cpp b/src/harbour-berail.cpp index 4970d73..22c9953 100644 --- a/src/harbour-berail.cpp +++ b/src/harbour-berail.cpp @@ -29,6 +29,7 @@ #include "logger.h" #include "os.h" #include "api.h" +#include "connectiontracker.h" #include "models/station.h" // Add toString() method to all custom method @@ -71,6 +72,7 @@ int main(int argc, char *argv[]) qmlRegisterUncreatableType("Harbour.BeRail.Models", 1, 0, "IRail", "read only"); qmlRegisterType("Harbour.BeRail.API", 1, 0, "API"); qmlRegisterType("Harbour.BeRail.SFOS", 1, 0, "SFOS"); + qmlRegisterType("Harbour.BeRail.ConnectionTracker", 1, 0, "ConnectionTracker"); // Start the application. view->setSource(SailfishApp::pathTo("qml/harbour-berail.qml")); diff --git a/src/models/connection.cpp b/src/models/connection.cpp index 0b3058a..ed0b588 100644 --- a/src/models/connection.cpp +++ b/src/models/connection.cpp @@ -16,6 +16,9 @@ */ #include "connection.h" +#include +#include + Connection::Connection(int id, Stop* fromStation, Stop* toStation, QString fromVehicleId, QString toVehicleId, Disturbances* alerts, Remarks* remarks, IRail::Occupancy occupancy, int duration, ViaListModel* vias, QDateTime timestamp) { this->setId(id); @@ -29,6 +32,8 @@ Connection::Connection(int id, Stop* fromStation, Stop* toStation, QString fromV this->setDuration(duration); this->setVias(vias); this->setTimestamp(timestamp); + + computeUuid(); } /********************* @@ -152,3 +157,34 @@ void Connection::setTimestamp(const QDateTime &value) m_timestamp = value; emit this->timestampChanged(); } + +QUuid Connection::uuid() const +{ + return m_uuid; +} + +void Connection::computeUuid() +{ + QCryptographicHash hash(QCryptographicHash::Sha3_512); + + // Scheduled departure time, station and vehicle uniquely identify starting point + hash.addData(from()->scheduledDepartureTime().toString(Qt::ISODate).toUtf8()); + hash.addData(from()->station()->name().toUtf8()); + hash.addData(m_fromVehicleId.toUtf8()); + + auto viaModel = vias(); + foreach(Via *via, viaModel->viaList()) { + qDebug() << "via" << via->stop()->station()->name(); + hash.addData("via"); + hash.addData(via->stop()->station()->name().toUtf8()); + hash.addData(via->vehicleId().toUtf8()); + hash.addData(via->arrivalTime().toString(Qt::ISODate).toUtf8()); + } + + hash.addData("to"); + hash.addData(to()->station()->name().toUtf8()); + hash.addData(to()->scheduledArrivalTime().toString(Qt::ISODate).toUtf8()); + + auto bytes = hash.result(); + m_uuid = QUuid::fromRfc4122(bytes.left(16)); // XXX: abuse of RFC4122 +} diff --git a/src/models/connection.h b/src/models/connection.h index 47b1d5d..d1ae1cd 100644 --- a/src/models/connection.h +++ b/src/models/connection.h @@ -20,6 +20,7 @@ #include #include #include +#include #include "stop.h" #include "disturbances.h" #include "remarks.h" @@ -37,6 +38,7 @@ class Connection: public QObject Q_PROPERTY(int duration READ duration WRITE setDuration NOTIFY durationChanged) Q_PROPERTY(ViaListModel* vias READ vias WRITE setVias NOTIFY viasChanged) Q_PROPERTY(QDateTime timestamp READ timestamp WRITE setTimestamp NOTIFY timestampChanged) + Q_PROPERTY(QUuid uuid READ uuid) public: explicit Connection(int id, @@ -75,6 +77,7 @@ class Connection: public QObject void setVias(ViaListModel *vias); QDateTime timestamp() const; void setTimestamp(const QDateTime &value); + QUuid uuid() const; signals: void idChanged(); @@ -89,6 +92,8 @@ class Connection: public QObject void timestampChanged(); private: + void computeUuid(); + int m_id; Stop* m_from; Stop* m_to; @@ -100,6 +105,7 @@ class Connection: public QObject int m_duration; ViaListModel* m_vias; QDateTime m_timestamp; + QUuid m_uuid; }; #endif // CONNECTION_H