2020-08-11 22:49:51 +00:00
|
|
|
/*
|
|
|
|
* Squawk messenger.
|
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "messagefeed.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
2020-08-12 16:55:01 +00:00
|
|
|
const QHash<int, QByteArray> MessageFeed::roles = {
|
|
|
|
{Text, "text"},
|
|
|
|
{Sender, "sender"},
|
|
|
|
{Date, "date"},
|
|
|
|
{DeliveryState, "deliveryState"},
|
|
|
|
{Correction, "correction"},
|
|
|
|
{SentByMe,"sentByMe"}
|
|
|
|
};
|
|
|
|
|
2020-08-11 22:49:51 +00:00
|
|
|
MessageFeed::MessageFeed(QObject* parent):
|
|
|
|
QAbstractListModel(parent),
|
|
|
|
storage(),
|
|
|
|
indexById(storage.get<id>()),
|
|
|
|
indexByTime(storage.get<time>()),
|
|
|
|
syncState(incomplete)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageFeed::~MessageFeed()
|
|
|
|
{
|
|
|
|
for (Shared::Message* message : storage) {
|
|
|
|
delete message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageFeed::addMessage(const Shared::Message& msg)
|
|
|
|
{
|
|
|
|
QString id = msg.getId();
|
|
|
|
StorageById::const_iterator itr = indexById.find(id);
|
|
|
|
if (itr != indexById.end()) {
|
|
|
|
qDebug() << "received more then one message with the same id, skipping yet the new one";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Shared::Message* copy = new Shared::Message(msg);
|
|
|
|
StorageByTime::const_iterator tItr = indexByTime.upper_bound(msg.getTime());
|
|
|
|
int position;
|
|
|
|
if (tItr == indexByTime.end()) {
|
|
|
|
position = storage.size();
|
|
|
|
} else {
|
|
|
|
position = indexByTime.rank(tItr);
|
|
|
|
}
|
|
|
|
beginInsertRows(QModelIndex(), position, position);
|
|
|
|
storage.insert(copy);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageFeed::changeMessage(const QString& id, const Shared::Message& msg)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageFeed::removeMessage(const QString& id)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant MessageFeed::data(const QModelIndex& index, int role) const
|
|
|
|
{
|
|
|
|
int i = index.row();
|
|
|
|
QVariant answer;
|
2020-08-12 16:55:01 +00:00
|
|
|
|
|
|
|
StorageByTime::const_iterator itr = indexByTime.nth(i);
|
|
|
|
if (itr != indexByTime.end()) {
|
|
|
|
const Shared::Message* msg = *itr;
|
|
|
|
|
|
|
|
switch (role) {
|
2020-08-15 21:48:28 +00:00
|
|
|
case Qt::DisplayRole:
|
2020-08-12 16:55:01 +00:00
|
|
|
case Text:
|
|
|
|
answer = msg->getBody();
|
|
|
|
break;
|
|
|
|
case Sender:
|
|
|
|
answer = msg->getFrom();
|
|
|
|
break;
|
|
|
|
case Date:
|
|
|
|
answer = msg->getTime();
|
|
|
|
break;
|
|
|
|
case DeliveryState:
|
|
|
|
answer = static_cast<unsigned int>(msg->getState());
|
|
|
|
break;
|
|
|
|
case Correction:
|
|
|
|
answer = msg->getEdited();
|
|
|
|
break;
|
|
|
|
case SentByMe:
|
|
|
|
answer = msg->getOutgoing();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (role) {
|
2020-08-15 21:48:28 +00:00
|
|
|
case Qt::DisplayRole:
|
2020-08-12 16:55:01 +00:00
|
|
|
case Text:
|
|
|
|
answer = "loading...";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
answer = "";
|
|
|
|
break;
|
2020-08-11 22:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MessageFeed::rowCount(const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
int count = storage.size();
|
|
|
|
if (syncState == syncing) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int MessageFeed::unreadMessagesCount() const
|
|
|
|
{
|
|
|
|
return storage.size(); //let's say they are all new for now =)
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MessageFeed::canFetchMore(const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
return syncState == incomplete;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageFeed::fetchMore(const QModelIndex& parent)
|
|
|
|
{
|
|
|
|
if (syncState == incomplete) {
|
2020-08-12 16:55:01 +00:00
|
|
|
beginInsertRows(QModelIndex(), storage.size(), storage.size());
|
2020-08-11 22:49:51 +00:00
|
|
|
syncState = syncing;
|
|
|
|
endInsertRows();
|
|
|
|
|
|
|
|
if (storage.size() == 0) {
|
|
|
|
emit requestArchive("");
|
|
|
|
} else {
|
2020-08-12 16:55:01 +00:00
|
|
|
emit requestArchive((*indexByTime.rbegin())->getId());
|
2020-08-11 22:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageFeed::responseArchive(const std::list<Shared::Message> list)
|
|
|
|
{
|
2020-08-12 16:55:01 +00:00
|
|
|
Storage::size_type size = storage.size();
|
2020-08-11 22:49:51 +00:00
|
|
|
if (syncState == syncing) {
|
2020-08-12 16:55:01 +00:00
|
|
|
beginRemoveRows(QModelIndex(), size, size);
|
2020-08-11 22:49:51 +00:00
|
|
|
syncState = incomplete;
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:55:01 +00:00
|
|
|
beginInsertRows(QModelIndex(), size, size + list.size() - 1);
|
2020-08-11 22:49:51 +00:00
|
|
|
for (const Shared::Message& msg : list) {
|
|
|
|
Shared::Message* copy = new Shared::Message(msg);
|
|
|
|
storage.insert(copy);
|
|
|
|
}
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:55:01 +00:00
|
|
|
QHash<int, QByteArray> MessageFeed::roleNames() const
|
|
|
|
{
|
|
|
|
return roles;
|
|
|
|
}
|