File indexing completed on 2024-05-12 05:39:50

0001 /***************************************************************************
0002  *  Copyright (C) 2020 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #ifndef INSTANTMESSAGINGMODEL_H
0021 #define INSTANTMESSAGINGMODEL_H
0022 
0023 #include "data/chatroom.h"
0024 #include <QAbstractListModel>
0025 #include <QPointer>
0026 #include <core_global.h>
0027 
0028 #include <memory>
0029 
0030 class DiceRoller;
0031 class PlayerModel;
0032 namespace InstantMessaging
0033 {
0034 class MessageInterface;
0035 class CORE_EXPORT InstantMessagingModel : public QAbstractListModel
0036 {
0037     Q_OBJECT
0038     Q_PROPERTY(QString localId READ localId WRITE setLocalId NOTIFY localIdChanged)
0039     Q_PROPERTY(bool unread READ unread NOTIFY unreadChanged)
0040 public:
0041     enum Role
0042     {
0043         TitleRole= Qt::UserRole + 1,
0044         IdRole,
0045         TypeRole,
0046         ChatRole,
0047         HasUnreadMessageRole,
0048         ClosableRole,
0049         RecipiantCountRole
0050     };
0051 
0052     explicit InstantMessagingModel(DiceRoller* diceRoller, PlayerModel* playerModel, QObject* parent= nullptr);
0053     virtual ~InstantMessagingModel();
0054 
0055     int rowCount(const QModelIndex& parent= QModelIndex()) const override;
0056     QVariant data(const QModelIndex& index, int role= Qt::DisplayRole) const override;
0057 
0058     QHash<int, QByteArray> roleNames() const override;
0059 
0060     ChatRoom* globalChatRoom() const;
0061     QString localId() const;
0062     bool unread() const;
0063 
0064 public slots:
0065     void insertGlobalChatroom(const QString& title, const QString& uuid= QString());
0066     void insertIndividualChatroom(const QString& playerId, const QString& playerName);
0067     void insertExtraChatroom(const QString& title, const QStringList& playerIds, bool remote,
0068                              const QString& uuid= QString());
0069     void setLocalId(const QString& id);
0070     void addMessageIntoChatroom(InstantMessaging::MessageInterface*, ChatRoom::ChatRoomType type, const QString& uuid);
0071     void removePlayer(const QString& id);
0072     void setDiceParser(DiceRoller* diceParser);
0073 
0074 signals:
0075     void chatRoomCreated(InstantMessaging::ChatRoom*, bool remote= false);
0076     void localIdChanged(QString);
0077     void unreadChanged();
0078 
0079 private:
0080     void addChatRoom(ChatRoom* room, bool remote= false);
0081 
0082 private:
0083     std::vector<std::unique_ptr<ChatRoom>> m_chats;
0084     QPointer<PlayerModel> m_personModel;
0085     QString m_localId;
0086     QPointer<DiceRoller> m_diceParser;
0087 };
0088 } // namespace InstantMessaging
0089 Q_DECLARE_METATYPE(InstantMessaging::InstantMessagingModel*)
0090 #endif // INSTANTMESSAGINGMODEL_H