File indexing completed on 2024-05-05 16:58:38

0001 // SPDX-FileCopyrightText: 2023 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
0003 // SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 #pragma once
0006 
0007 #include "neochatroom.h"
0008 
0009 #include <QAbstractListModel>
0010 #include <QPointer>
0011 #include <QRectF>
0012 
0013 namespace Quotient
0014 {
0015 class RoomMessageEvent;
0016 }
0017 
0018 struct LiveLocationData {
0019     QString eventId;
0020     QString senderId;
0021     QJsonObject beaconInfo;
0022     QJsonObject beacon;
0023 };
0024 bool operator<(const LiveLocationData &lhs, const LiveLocationData &rhs);
0025 
0026 /** Accumulates live location beacon events in a given room
0027  *  and provides the last known state for one or more live location beacons.
0028  */
0029 class LiveLocationsModel : public QAbstractListModel
0030 {
0031     Q_OBJECT
0032     Q_PROPERTY(NeoChatRoom *room MEMBER m_room NOTIFY roomChanged)
0033     /** The event id of the beacon start event, ie. the one all suspequent
0034      *  events use to relate to the same beacon.
0035      *  If this is set only this specific beacon will be coverd by this model,
0036      *  if it is empty, all beacons in the room will be covered.
0037      */
0038     Q_PROPERTY(QString eventId MEMBER m_eventId NOTIFY eventIdChanged)
0039 
0040     /** Bounding box of all live location beacons covered by this model. */
0041     Q_PROPERTY(QRectF boundingBox READ boundingBox NOTIFY boundingBoxChanged)
0042 
0043 public:
0044     explicit LiveLocationsModel(QObject *parent = nullptr);
0045 
0046     enum Roles {
0047         LatitudeRole, /**< Latest latitude of a live locaction beacon. */
0048         LongitudeRole, /**< Latest longitude of a live locaction beacon. */
0049         AssetRole, /**< Type of location event, e.g. self pin of the user location. */
0050         AuthorRole, /**< The author of the event. */
0051         IsLiveRole, /**< Boolean that indicates whether a live location beacon is still live. */
0052     };
0053     Q_ENUM(Roles)
0054 
0055     int rowCount(const QModelIndex &parent = {}) const override;
0056     QVariant data(const QModelIndex &index, int roleName) const override;
0057     QHash<int, QByteArray> roleNames() const override;
0058 
0059     QRectF boundingBox() const;
0060 
0061 Q_SIGNALS:
0062     void roomChanged();
0063     void eventIdChanged();
0064     void boundingBoxChanged();
0065 
0066 private:
0067     void addEvent(const Quotient::RoomEvent *event);
0068     void updateLocationData(LiveLocationData &&data);
0069 
0070     QPointer<NeoChatRoom> m_room;
0071     QString m_eventId;
0072 
0073     QList<LiveLocationData> m_locations;
0074 };