ChatModel.hpp 4.52 KB
Newer Older
Ronan's avatar
Ronan committed
1 2
/*
 * ChatModel.hpp
Wescoeur's avatar
Wescoeur committed
3
 * Copyright (C) 2017-2018  Belledonne Communications, Grenoble, France
Ronan's avatar
Ronan committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *  Created on: February 2, 2017
 *      Author: Ronan Abhamon
 */

23 24 25
#ifndef CHAT_MODEL_H_
#define CHAT_MODEL_H_

26
#include <linphone++/linphone.hh>
Ronan's avatar
Ronan committed
27
#include <QAbstractListModel>
28

Ronan's avatar
Ronan committed
29
// =============================================================================
30
// Fetch all N messages of a ChatRoom.
Ronan's avatar
Ronan committed
31
// =============================================================================
32

33 34
class CoreHandlers;

35
class ChatModel : public QAbstractListModel {
36 37
  class MessageHandlers;

38 39 40
  Q_OBJECT;

public:
Ronan's avatar
Ronan committed
41 42 43 44 45
  enum Roles {
    ChatEntry = Qt::DisplayRole,
    SectionDate
  };

46
  enum EntryType {
Ronan's avatar
Ronan committed
47
    GenericEntry,
48 49 50 51 52
    MessageEntry,
    CallEntry
  };
  Q_ENUM(EntryType);

Ronan's avatar
Ronan committed
53
  enum CallStatus {
54 55 56
    CallStatusDeclined = int(linphone::Call::Status::Declined),
    CallStatusMissed = int(linphone::Call::Status::Missed),
    CallStatusSuccess = int(linphone::Call::Status::Success)
Ronan's avatar
Ronan committed
57 58 59
  };
  Q_ENUM(CallStatus);

60
  enum MessageStatus {
61 62 63 64 65 66 67 68
    MessageStatusDelivered = int(linphone::ChatMessage::State::Delivered),
    MessageStatusDeliveredToUser = int(linphone::ChatMessage::State::DeliveredToUser),
    MessageStatusDisplayed = int(linphone::ChatMessage::State::Displayed),
    MessageStatusFileTransferDone = int(linphone::ChatMessage::State::FileTransferDone),
    MessageStatusFileTransferError = int(linphone::ChatMessage::State::FileTransferError),
    MessageStatusIdle = int(linphone::ChatMessage::State::Idle),
    MessageStatusInProgress = int(linphone::ChatMessage::State::InProgress),
    MessageStatusNotDelivered = int(linphone::ChatMessage::State::NotDelivered)
69
  };
70
  Q_ENUM(MessageStatus);
71

72
  ChatModel (const QString &peerAddress, const QString &localAddress);
73
  ~ChatModel ();
74

75
  int rowCount (const QModelIndex &index = QModelIndex()) const override;
76

77 78
  QHash<int, QByteArray> roleNames () const override;
  QVariant data (const QModelIndex &index, int role) const override;
Ronan's avatar
Ronan committed
79

80
  bool removeRow (int row, const QModelIndex &parent = QModelIndex());
81
  bool removeRows (int row, int count, const QModelIndex &parent = QModelIndex()) override;
82

83 84
  QString getPeerAddress () const;
  QString getLocalAddress () const;
85

86 87
  bool getIsRemoteComposing () const;

88 89 90 91
  void removeEntry (int id);
  void removeAllEntries ();

  void sendMessage (const QString &message);
92

93 94
  void resendMessage (int id);

95 96
  void sendFileMessage (const QString &path);

97
  void downloadFile (int id);
98 99 100 101 102 103
  void openFile (int id, bool showDirectory = false);
  void openFileDirectory (int id) {
    openFile(id, true);
  }

  bool fileWasDownloaded (int id);
104

105 106
  void compose ();

107
  void resetMessageCount ();
108

Ronan's avatar
Ronan committed
109
signals:
110 111
  bool isRemoteComposingChanged (bool status);

112
  void allEntriesRemoved ();
113
  void lastEntryRemoved ();
Ronan's avatar
Ronan committed
114

115 116
  void messageSent (const std::shared_ptr<linphone::ChatMessage> &message);
  void messageReceived (const std::shared_ptr<linphone::ChatMessage> &message);
Ronan's avatar
Ronan committed
117

118
  void messageCountReset ();
119

Ronan's avatar
Ronan committed
120
private:
121
  typedef QPair<QVariantMap, std::shared_ptr<void>> ChatEntryData;
122

123
  void setSipAddresses (const QString &peerAddress, const QString &localAddress);
124

125 126
  const ChatEntryData getFileMessageEntry (int id);

Ronan's avatar
Ronan committed
127
  void removeEntry (ChatEntryData &entry);
128

129
  void insertCall (const std::shared_ptr<linphone::CallLog> &callLog);
130 131
  void insertMessageAtEnd (const std::shared_ptr<linphone::ChatMessage> &message);

132
  void handleCallStateChanged (const std::shared_ptr<linphone::Call> &call, linphone::Call::State state);
Ronan's avatar
Ronan committed
133
  void handleIsComposingChanged (const std::shared_ptr<linphone::ChatRoom> &chatRoom);
134 135
  void handleMessageReceived (const std::shared_ptr<linphone::ChatMessage> &message);

136 137
  bool mIsRemoteComposing = false;

138
  mutable QList<ChatEntryData> mEntries;
139
  std::shared_ptr<linphone::ChatRoom> mChatRoom;
140

141 142
  std::shared_ptr<CoreHandlers> mCoreHandlers;
  std::shared_ptr<MessageHandlers> mMessageHandlers;
143 144 145
};

#endif // CHAT_MODEL_H_