File indexing completed on 2024-05-05 05:28:47

0001 // SPDX-FileCopyrightText: 2020 Jonah BrĂ¼chert <jbb@kaidan.im>
0002 //
0003 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 
0005 #pragma once
0006 
0007 #include <QAbstractListModel>
0008 #include <QObject>
0009 
0010 #include <KPeople/PersonData>
0011 
0012 #include <phonenumberlist.h>
0013 
0014 #include "database.h"
0015 
0016 #include "qcorotask.h"
0017 
0018 struct Person {
0019     Q_GADGET
0020     Q_PROPERTY(QString phoneNumber MEMBER m_phoneNumber)
0021     Q_PROPERTY(QString name MEMBER m_name)
0022 public:
0023     QString m_phoneNumber;
0024     QString m_name;
0025 };
0026 Q_DECLARE_METATYPE(Person)
0027 
0028 class ChannelHandler;
0029 
0030 class MessageModel : public QAbstractListModel
0031 {
0032     Q_OBJECT
0033     Q_PROPERTY(PhoneNumberList phoneNumberList READ phoneNumberList NOTIFY phoneNumberListChanged)
0034     Q_PROPERTY(QString sendingNumber READ sendingNumber CONSTANT)
0035     Q_PROPERTY(QVector<Person> people READ people NOTIFY peopleChanged)
0036     Q_PROPERTY(QString attachmentsFolder READ attachmentsFolder CONSTANT)
0037 
0038 public:
0039     enum Role {
0040         TextRole = Qt::UserRole + 1,
0041         DateRole,
0042         TimeRole,
0043         SentByMeRole,
0044         ReadRole,
0045         DeliveryStateRole,
0046         IdRole,
0047         AttachmentsRole,
0048         SmilRole,
0049         FromNumberRole,
0050         MessageIdRole,
0051         DeliveryReportRole,
0052         ReadReportRole,
0053         PendingDownloadRole,
0054         ContentLocationRole,
0055         ExpiresRole,
0056         ExpiresDateTimeRole,
0057         SizeRole,
0058         TapbacksRole
0059     };
0060     Q_ENUM(Role)
0061 
0062     enum DeliveryState {
0063         Unknown = MessageState::Unknown,
0064         Pending = MessageState::Pending,
0065         Sent = MessageState::Sent,
0066         Failed = MessageState::Failed,
0067         Received = MessageState::Received
0068     };
0069     Q_ENUM(DeliveryState)
0070 
0071     explicit MessageModel(ChannelHandler &handler, const PhoneNumberList &phoneNumberList, QObject *parent = nullptr);
0072 
0073     QHash<int, QByteArray> roleNames() const override;
0074     QVariant data(const QModelIndex &index, int role) const override;
0075     int rowCount(const QModelIndex &index = {}) const override;
0076 
0077     QVector<Person> people() const;
0078 
0079     PhoneNumberList phoneNumberList() const;
0080 
0081     QString sendingNumber() const;
0082 
0083     QString attachmentsFolder() const;
0084 
0085     Q_INVOKABLE QVariant fileInfo(const QUrl &path);
0086 
0087     Q_INVOKABLE void fetchAllMessages();
0088 
0089     /**
0090      * @brief adds a tapack reaction to a previously sent or recieved message,
0091      * and updates the model and database
0092      * @param id
0093      * @param tapback
0094      * @param isRemoved
0095      */
0096     Q_INVOKABLE void sendTapback(const QString &id, const QString &tapback, const bool &isRemoved);
0097 
0098     /**
0099      * @brief sends a message with the specified text,
0100      * and adds it to the model and database by calling addMessage(const QString&)
0101      * @param text
0102      */
0103     Q_INVOKABLE void sendMessage(const QString &text, const QStringList &files, const qint64 &totalSize);
0104 
0105     /**
0106      * @brief marks a message as read by calling the respective database function
0107      */
0108     Q_INVOKABLE void markMessageRead(const int id);
0109 
0110     /**
0111      * @brief downloads the contents of an MMS message
0112      */
0113     Q_INVOKABLE void downloadMessage(const QString &id, const QString &url, const QDateTime &expires);
0114 
0115     /**
0116      * @brief permanently deletes a message and any attachments
0117      */
0118     Q_INVOKABLE void deleteMessage(const QString &id, const int index, const QStringList &files);
0119 
0120     /**
0121      * @brief saves selected attachments to downloads folder
0122      * @param list
0123      */
0124     Q_INVOKABLE void saveAttachments(const QStringList &attachments);
0125 
0126     /**
0127      * @brief excludes set phone number from message notifications
0128      * @param list
0129      */
0130     Q_INVOKABLE void disableNotifications(const PhoneNumberList &phoneNumberList);
0131 
0132 private:
0133     QPair<Message *, int> getMessageIndex(const QString &id);
0134     void updateMessageState(const QString &id, MessageState state, const bool temp = false);
0135 
0136     ChannelHandler &m_handler;
0137     std::vector<Message> m_messages;
0138 
0139     // properties
0140     PhoneNumberList m_phoneNumberList;
0141     QVector<Person> m_peopleData;
0142 
0143 private Q_SLOTS:
0144     QCoro::Task<void> fetchMessages(const QString &id, const int limit = 0);
0145     QCoro::Task<void> fetchUpdatedMessage(const QString &id);
0146     void messageAdded(const QString &numbers, const QString &id);
0147     void messageUpdated(const QString &numbers, const QString &id);
0148 
0149 Q_SIGNALS:
0150     void phoneNumberListChanged();
0151     void peopleChanged();
0152     void messagesFetched();
0153 };