1
0
forked from blue/squawk
squawk/ui/models/messagefeed.cpp

345 lines
11 KiB
C++
Raw Normal View History

/*
* 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"
2020-08-20 21:32:30 +00:00
#include "element.h"
#include "room.h"
#include <QDebug>
2020-08-20 21:32:30 +00:00
const QHash<int, QByteArray> Models::MessageFeed::roles = {
2020-08-12 16:55:01 +00:00
{Text, "text"},
{Sender, "sender"},
{Date, "date"},
{DeliveryState, "deliveryState"},
{Correction, "correction"},
2020-08-20 21:32:30 +00:00
{SentByMe,"sentByMe"},
{Avatar, "avatar"},
{Attach, "attach"},
{Id, "id"},
{Bulk, "bulk"}
2020-08-12 16:55:01 +00:00
};
2020-08-20 21:32:30 +00:00
Models::MessageFeed::MessageFeed(const Element* ri, QObject* parent):
QAbstractListModel(parent),
storage(),
indexById(storage.get<id>()),
indexByTime(storage.get<time>()),
2020-08-20 21:32:30 +00:00
rosterItem(ri),
syncState(incomplete),
uploads(),
downloads()
{
}
2020-08-20 21:32:30 +00:00
Models::MessageFeed::~MessageFeed()
{
for (Shared::Message* message : storage) {
delete message;
}
}
2020-08-20 21:32:30 +00:00
void Models::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();
}
2020-08-20 21:32:30 +00:00
void Models::MessageFeed::changeMessage(const QString& id, const Shared::Message& msg)
{
}
2020-08-20 21:32:30 +00:00
void Models::MessageFeed::removeMessage(const QString& id)
{
}
2020-08-20 21:32:30 +00:00
QVariant Models::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) {
case Qt::DisplayRole:
case Text: {
QString body = msg->getBody();
if (body != msg->getOutOfBandUrl()) {
answer = body;
}
}
2020-08-12 16:55:01 +00:00
break;
case Sender:
if (sentByMe(*msg)) {
answer = rosterItem->getAccountName();
2020-08-20 21:32:30 +00:00
} else {
if (rosterItem->isRoom()) {
answer = msg->getFromResource();
2020-08-20 21:32:30 +00:00
} else {
answer = rosterItem->getDisplayedName();
}
}
2020-08-12 16:55:01 +00:00
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:
2020-08-20 21:32:30 +00:00
answer = sentByMe(*msg);
2020-08-12 16:55:01 +00:00
break;
2020-08-20 21:32:30 +00:00
case Avatar: {
QString path;
if (sentByMe(*msg)) {
path = rosterItem->getAccountAvatarPath();
} else if (!rosterItem->isRoom()) {
if (rosterItem->getAvatarState() != Shared::Avatar::empty) {
path = rosterItem->getAvatarPath();
}
} else {
const Room* room = static_cast<const Room*>(rosterItem);
path = room->getParticipantIconPath(msg->getFromResource());
}
if (path.size() == 0) {
answer = Shared::iconPath("user", true);
} else {
answer = path;
}
}
break;
case Attach:
answer.setValue(fillAttach(*msg));
2020-08-12 16:55:01 +00:00
break;
case Id:
answer.setValue(msg->getId());
break;
case Bulk: {
FeedItem item;
item.id = msg->getId();
item.sentByMe = sentByMe(*msg);
item.date = msg->getTime();
item.state = msg->getState();
item.correction = msg->getEdited();
QString body = msg->getBody();
if (body != msg->getOutOfBandUrl()) {
item.text = body;
}
item.avatar.clear();
if (item.sentByMe) {
item.sender = rosterItem->getAccountName();
item.avatar = rosterItem->getAccountAvatarPath();
} else {
if (rosterItem->isRoom()) {
item.sender = msg->getFromResource();
const Room* room = static_cast<const Room*>(rosterItem);
item.avatar = room->getParticipantIconPath(msg->getFromResource());
} else {
item.sender = rosterItem->getDisplayedName();
if (rosterItem->getAvatarState() != Shared::Avatar::empty) {
item.avatar = rosterItem->getAvatarPath();
}
}
}
if (item.avatar.size() == 0) {
item.avatar = Shared::iconPath("user", true);
}
item.attach = fillAttach(*msg);
answer.setValue(item);
}
break;
2020-08-12 16:55:01 +00:00
default:
break;
}
}
return answer;
}
2020-08-20 21:32:30 +00:00
int Models::MessageFeed::rowCount(const QModelIndex& parent) const
{
2020-08-20 21:32:30 +00:00
return storage.size();
}
2020-08-20 21:32:30 +00:00
unsigned int Models::MessageFeed::unreadMessagesCount() const
{
return storage.size(); //let's say they are all new for now =)
}
2020-08-20 21:32:30 +00:00
bool Models::MessageFeed::canFetchMore(const QModelIndex& parent) const
{
return syncState == incomplete;
}
2020-08-20 21:32:30 +00:00
void Models::MessageFeed::fetchMore(const QModelIndex& parent)
{
if (syncState == incomplete) {
syncState = syncing;
2020-08-20 21:32:30 +00:00
emit requestStateChange(true);
if (storage.size() == 0) {
emit requestArchive("");
} else {
2020-08-12 16:55:01 +00:00
emit requestArchive((*indexByTime.rbegin())->getId());
}
}
}
void Models::MessageFeed::responseArchive(const std::list<Shared::Message> list, bool last)
{
2020-08-12 16:55:01 +00:00
Storage::size_type size = storage.size();
2020-08-12 16:55:01 +00:00
beginInsertRows(QModelIndex(), size, size + list.size() - 1);
for (const Shared::Message& msg : list) {
Shared::Message* copy = new Shared::Message(msg);
storage.insert(copy);
}
endInsertRows();
if (syncState == syncing) {
if (last) {
syncState = complete;
} else {
syncState = incomplete;
}
emit requestStateChange(false);
}
}
2020-08-20 21:32:30 +00:00
QHash<int, QByteArray> Models::MessageFeed::roleNames() const
2020-08-12 16:55:01 +00:00
{
return roles;
}
2020-08-20 21:32:30 +00:00
bool Models::MessageFeed::sentByMe(const Shared::Message& msg) const
{
if (rosterItem->isRoom()) {
const Room* room = static_cast<const Room*>(rosterItem);
return room->getNick().toLower() == msg.getFromResource().toLower();
} else {
return msg.getOutgoing();
}
}
Models::Attachment Models::MessageFeed::fillAttach(const Shared::Message& msg) const
{
::Models::Attachment att;
att.localPath = msg.getAttachPath();
att.remotePath = msg.getOutOfBandUrl();
if (att.remotePath.size() == 0) {
if (att.localPath.size() == 0) {
att.state = none;
} else {
Progress::const_iterator itr = uploads.find(msg.getId());
if (itr == uploads.end()) {
att.state = local;
} else {
att.state = uploading;
att.progress = itr->second;
}
}
} else {
if (att.localPath.size() == 0) {
Progress::const_iterator itr = downloads.find(msg.getId());
if (itr == downloads.end()) {
att.state = remote;
} else {
att.state = downloading;
att.progress = itr->second;
}
} else {
att.state = ready;
}
}
return att;
}
void Models::MessageFeed::downloadAttachment(const QString& messageId)
{
2021-02-27 12:21:27 +00:00
QModelIndex ind = modelIndexById(messageId);
if (ind.isValid()) {
std::pair<Progress::iterator, bool> progressPair = downloads.insert(std::make_pair(messageId, 0));
if (progressPair.second) { //Only to take action if we weren't already downloading it
2021-02-27 12:21:27 +00:00
Shared::Message* msg = static_cast<Shared::Message*>(ind.internalPointer());
emit dataChanged(ind, ind);
emit fileLocalPathRequest(messageId, msg->getOutOfBandUrl());
} else {
qDebug() << "Attachment download for message with id" << messageId << "is already in progress, skipping";
}
} else {
qDebug() << "An attempt to download an attachment for the message that doesn't exist. ID:" << messageId;
}
}
void Models::MessageFeed::uploadAttachment(const QString& messageId)
{
qDebug() << "request to upload attachment of the message" << messageId;
}
2021-02-27 12:21:27 +00:00
void Models::MessageFeed::fileProgress(const QString& messageId, qreal value)
{
Progress::iterator itr = downloads.find(messageId);
if (itr != downloads.end()) {
itr->second = value;
QModelIndex ind = modelIndexById(messageId);
emit dataChanged(ind, ind);
}
}
QModelIndex Models::MessageFeed::modelIndexById(const QString& id) const
{
StorageById::const_iterator itr = indexById.find(id);
if (itr != indexById.end()) {
Shared::Message* msg = *itr;
StorageByTime::const_iterator tItr = indexByTime.upper_bound(msg->getTime());
int position = indexByTime.rank(tItr);
return createIndex(position, 0, msg);
} else {
return QModelIndex();
}
}