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

0001 // SPDX-FileCopyrightText: 2021 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 <QDateTime>
0009 #include <QObject>
0010 #include <QSqlDatabase>
0011 
0012 #include "qcorotask.h"
0013 
0014 #include "global.h"
0015 #include <contactphonenumbermapper.h>
0016 #include <database.h>
0017 
0018 class MessageModel;
0019 class ChannelHandler;
0020 
0021 class ChatListModel : public QAbstractListModel
0022 {
0023     Q_OBJECT
0024 
0025 public:
0026     ~ChatListModel();
0027 
0028     enum Role {
0029         DisplayNameRole = Qt::UserRole + 1,
0030         PhoneNumberListRole,
0031         UnreadMessagesRole,
0032         LastMessageRole,
0033         LastDateTimeRole,
0034         LastSentByMeRole,
0035         LastAttachmentRole,
0036         LastContactedRole,
0037         IsContactRole
0038     };
0039     Q_ENUM(Role)
0040 
0041     explicit ChatListModel(ChannelHandler &handler, QObject *parent = nullptr);
0042 
0043     QHash<int, QByteArray> roleNames() const override;
0044     QVariant data(const QModelIndex &index, int role) const override;
0045     int rowCount(const QModelIndex &parent = {}) const override;
0046 
0047     Q_INVOKABLE void fetchChatDetails(const PhoneNumberList &phoneNumberList, const bool sort = false);
0048     Q_INVOKABLE void startChat(const PhoneNumberList &phoneNumberList);
0049     Q_INVOKABLE void markChatAsRead(const PhoneNumberList &phoneNumberList);
0050     Q_INVOKABLE void restoreDefaults();
0051     Q_INVOKABLE void saveSettings();
0052     Q_INVOKABLE QString attachmentsFolder(const PhoneNumberList &phoneNumberList) const;
0053     Q_INVOKABLE void setCharacterLimit(const int &width);
0054 
0055     bool ready() const;
0056 
0057 public Q_SLOTS:
0058     void fetchChats(const PhoneNumberList &phoneNumberList);
0059     void deleteChat(const PhoneNumberList &phoneNumberList);
0060 
0061 Q_SIGNALS:
0062     void chatStarted(MessageModel *messageModel);
0063     void startingChatFaild(const QString &errorMessage);
0064 
0065     void readyChanged();
0066     void chatsFetched();
0067 
0068 private:
0069     QPair<Chat *, int> getChatIndex(const PhoneNumberList &phoneNumberList);
0070     QCoro::Task<void> fetchChatsInternal();
0071     QCoro::Task<void> fetchChatDetailsInternal(const PhoneNumberList &phoneNumberList, const bool sort = false);
0072 
0073     ChannelHandler &m_handler;
0074     QVector<Chat> m_chats;
0075     ContactPhoneNumberMapper &m_mapper;
0076     MessageModel *m_messageModel = nullptr;
0077     int m_characters = 15;
0078 };