1 /*
2     Copyright (C) 2011  Lasath Fernando <kde@lasath.org>
3 
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Lesser General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the License, or (at your option) any later version.
8 
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Lesser General Public License for more details.
13 
14     You should have received a copy of the GNU Lesser General Public
15     License along with this library; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18 
19 
20 #ifndef MESSAGES_MODEL_H
21 #define MESSAGES_MODEL_H
22 
23 #include <QAbstractItemModel>
24 
25 #include <TelepathyQt/Types>
26 #include <TelepathyQt/ReceivedMessage>
27 
28 #include <KTp/message.h>
29 
30 class MessagesModel : public QAbstractListModel
31 {
32     Q_OBJECT
33     Q_ENUMS(MessageType)
34     Q_ENUMS(DeliveryStatus)
35     Q_PROPERTY(bool visibleToUser READ isVisibleToUser WRITE setVisibleToUser NOTIFY visibleToUserChanged)
36     Q_PROPERTY(int unreadCount READ unreadCount NOTIFY unreadCountChanged)
37     Q_PROPERTY(bool shouldStartOpened READ shouldStartOpened CONSTANT)
38 
39   public:
40     MessagesModel(const Tp::AccountPtr &account, QObject *parent = nullptr);
41     ~MessagesModel() override;
42 
43     enum Roles {
44         TextRole = Qt::UserRole, //String
45         TypeRole, //MessagesModel::MessageType (for now!)
46         TimeRole, //QDateTime
47         SenderIdRole, //string
48         SenderAliasRole, //string
49         SenderAvatarRole, //pixmap
50         DeliveryStatusRole, //MessagesModel::DeliveryStatus
51         DeliveryReportReceiveTimeRole, //QDateTime
52         PreviousMessageTypeRole, // allows for painting the messages as grouped
53         NextMessageTypeRole,
54     };
55 
56     enum MessageType {
57         MessageTypeIncoming,
58         MessageTypeOutgoing,
59         MessageTypeAction,
60         MessageTypeNotice
61     };
62 
63     enum DeliveryStatus {
64         DeliveryStatusUnknown,
65         DeliveryStatusDelivered,
66         DeliveryStatusRead, // implies DeliveryStatusDelivered
67         DeliveryStatusFailed
68     };
69 
70     QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;
71     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
72     int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
73 
74     Tp::TextChannelPtr textChannel() const;
75     void setTextChannel(const Tp::TextChannelPtr &channel);
76 
77     Tp::AccountPtr account() const;
78     void setAccount(const Tp::AccountPtr &account);
79 
80     /**
81      * Useful for offline history retrieving (as there's no text channel when offline)
82      */
83     void setContactData(const QString &contactId, const QString &contactAlias);
84 
85     bool isVisibleToUser() const;
86     void setVisibleToUser(bool visible);
87 
88     int  unreadCount() const;
89     void acknowledgeAllMessages();
90 
91     bool shouldStartOpened() const;
92 
93     QString lastMessage() const;
94     QDateTime lastMessageDateTime() const;
95 
96 
97   Q_SIGNALS:
98     void visibleToUserChanged(bool visible);
99     void unreadCountChanged(int unreadMesssagesCount);
100     void lastMessageChanged();
101 
102   public Q_SLOTS:
103     void fetchMoreHistory();
104     void sendNewMessage(const QString &message);
105 
106   private Q_SLOTS:
107     void onMessageReceived(const Tp::ReceivedMessage &message);
108     void onMessageSent(const Tp::Message &message, Tp::MessageSendingFlags flags, const QString &messageToken);
109     void onPendingMessageRemoved();
110     bool verifyPendingOperation(Tp::PendingOperation *op);
111     void onHistoryFetched(const QList<KTp::Message> &messages);
112 
113   private:
114     void setupChannelSignals(const Tp::TextChannelPtr &channel);
115     void removeChannelSignals(const Tp::TextChannelPtr &channel);
116 
117     class MessagesModelPrivate;
118     MessagesModelPrivate *d;
119 };
120 
121 #endif // CONVERSATION_MODEL_H
122