forked from blue/squawk
Some basic message painting
This commit is contained in:
parent
e1eea2f3a2
commit
e0ef1ef797
16 changed files with 296 additions and 44 deletions
|
@ -231,7 +231,7 @@ void Models::Account::toOfflineState()
|
|||
Item::toOfflineState();
|
||||
}
|
||||
|
||||
QString Models::Account::getAvatarPath()
|
||||
QString Models::Account::getAvatarPath() const
|
||||
{
|
||||
return avatarPath;
|
||||
}
|
||||
|
|
|
@ -57,7 +57,7 @@ namespace Models {
|
|||
QString getError() const;
|
||||
|
||||
void setAvatarPath(const QString& path);
|
||||
QString getAvatarPath();
|
||||
QString getAvatarPath() const;
|
||||
|
||||
void setAvailability(Shared::Availability p_avail);
|
||||
void setAvailability(unsigned int p_avail);
|
||||
|
|
|
@ -27,7 +27,7 @@ Models::Element::Element(Type p_type, const Models::Account* acc, const QString&
|
|||
avatarPath(),
|
||||
avatarState(Shared::Avatar::empty),
|
||||
account(acc),
|
||||
feed(new MessageFeed())
|
||||
feed(new MessageFeed(this))
|
||||
{
|
||||
connect(feed, &MessageFeed::requestArchive, this, &Element::requestArchive);
|
||||
|
||||
|
@ -149,3 +149,8 @@ void Models::Element::responseArchive(const std::list<Shared::Message> list)
|
|||
{
|
||||
feed->responseArchive(list);
|
||||
}
|
||||
|
||||
bool Models::Element::isRoom() const
|
||||
{
|
||||
return type != contact;
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ public:
|
|||
void changeMessage(const QString& id, const QMap<QString, QVariant>& data);
|
||||
unsigned int getMessagesCount() const;
|
||||
void responseArchive(const std::list<Shared::Message> list);
|
||||
bool isRoom() const;
|
||||
|
||||
signals:
|
||||
void requestArchive(const QString& before);
|
||||
|
|
|
@ -283,6 +283,15 @@ Shared::ConnectionState Models::Item::getAccountConnectionState() const
|
|||
return acc->getState();
|
||||
}
|
||||
|
||||
QString Models::Item::getAccountAvatarPath() const
|
||||
{
|
||||
const Account* acc = getParentAccount();
|
||||
if (acc == nullptr) {
|
||||
return "";
|
||||
}
|
||||
return acc->getAvatarPath();
|
||||
}
|
||||
|
||||
QString Models::Item::getDisplayedName() const
|
||||
{
|
||||
return name;
|
||||
|
|
|
@ -80,6 +80,7 @@ class Item : public QObject{
|
|||
QString getAccountName() const;
|
||||
QString getAccountJid() const;
|
||||
QString getAccountResource() const;
|
||||
QString getAccountAvatarPath() const;
|
||||
Shared::ConnectionState getAccountConnectionState() const;
|
||||
Shared::Availability getAccountAvailability() const;
|
||||
|
||||
|
|
|
@ -17,35 +17,39 @@
|
|||
*/
|
||||
|
||||
#include "messagefeed.h"
|
||||
#include "element.h"
|
||||
#include "room.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
const QHash<int, QByteArray> MessageFeed::roles = {
|
||||
const QHash<int, QByteArray> Models::MessageFeed::roles = {
|
||||
{Text, "text"},
|
||||
{Sender, "sender"},
|
||||
{Date, "date"},
|
||||
{DeliveryState, "deliveryState"},
|
||||
{Correction, "correction"},
|
||||
{SentByMe,"sentByMe"}
|
||||
{SentByMe,"sentByMe"},
|
||||
{Avatar, "avatar"}
|
||||
};
|
||||
|
||||
MessageFeed::MessageFeed(QObject* parent):
|
||||
Models::MessageFeed::MessageFeed(const Element* ri, QObject* parent):
|
||||
QAbstractListModel(parent),
|
||||
storage(),
|
||||
indexById(storage.get<id>()),
|
||||
indexByTime(storage.get<time>()),
|
||||
rosterItem(ri),
|
||||
syncState(incomplete)
|
||||
{
|
||||
}
|
||||
|
||||
MessageFeed::~MessageFeed()
|
||||
Models::MessageFeed::~MessageFeed()
|
||||
{
|
||||
for (Shared::Message* message : storage) {
|
||||
delete message;
|
||||
}
|
||||
}
|
||||
|
||||
void MessageFeed::addMessage(const Shared::Message& msg)
|
||||
void Models::MessageFeed::addMessage(const Shared::Message& msg)
|
||||
{
|
||||
QString id = msg.getId();
|
||||
StorageById::const_iterator itr = indexById.find(id);
|
||||
|
@ -67,15 +71,15 @@ void MessageFeed::addMessage(const Shared::Message& msg)
|
|||
endInsertRows();
|
||||
}
|
||||
|
||||
void MessageFeed::changeMessage(const QString& id, const Shared::Message& msg)
|
||||
void Models::MessageFeed::changeMessage(const QString& id, const Shared::Message& msg)
|
||||
{
|
||||
}
|
||||
|
||||
void MessageFeed::removeMessage(const QString& id)
|
||||
void Models::MessageFeed::removeMessage(const QString& id)
|
||||
{
|
||||
}
|
||||
|
||||
QVariant MessageFeed::data(const QModelIndex& index, int role) const
|
||||
QVariant Models::MessageFeed::data(const QModelIndex& index, int role) const
|
||||
{
|
||||
int i = index.row();
|
||||
QVariant answer;
|
||||
|
@ -90,7 +94,19 @@ QVariant MessageFeed::data(const QModelIndex& index, int role) const
|
|||
answer = msg->getBody();
|
||||
break;
|
||||
case Sender:
|
||||
answer = msg->getFrom();
|
||||
if (rosterItem->isRoom()) {
|
||||
if (sentByMe(*msg)) {
|
||||
answer = rosterItem->getDisplayedName();
|
||||
} else {
|
||||
answer = msg->getFromResource();
|
||||
}
|
||||
} else {
|
||||
if (sentByMe(*msg)) {
|
||||
answer = rosterItem->getAccountName();
|
||||
} else {
|
||||
answer = rosterItem->getDisplayedName();
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Date:
|
||||
answer = msg->getTime();
|
||||
|
@ -102,51 +118,55 @@ QVariant MessageFeed::data(const QModelIndex& index, int role) const
|
|||
answer = msg->getEdited();
|
||||
break;
|
||||
case SentByMe:
|
||||
answer = msg->getOutgoing();
|
||||
answer = sentByMe(*msg);
|
||||
break;
|
||||
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;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch (role) {
|
||||
case Qt::DisplayRole:
|
||||
case Text:
|
||||
answer = "loading...";
|
||||
break;
|
||||
default:
|
||||
answer = "";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return answer;
|
||||
}
|
||||
|
||||
int MessageFeed::rowCount(const QModelIndex& parent) const
|
||||
int Models::MessageFeed::rowCount(const QModelIndex& parent) const
|
||||
{
|
||||
int count = storage.size();
|
||||
if (syncState == syncing) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
return storage.size();
|
||||
}
|
||||
|
||||
unsigned int MessageFeed::unreadMessagesCount() const
|
||||
unsigned int Models::MessageFeed::unreadMessagesCount() const
|
||||
{
|
||||
return storage.size(); //let's say they are all new for now =)
|
||||
}
|
||||
|
||||
bool MessageFeed::canFetchMore(const QModelIndex& parent) const
|
||||
bool Models::MessageFeed::canFetchMore(const QModelIndex& parent) const
|
||||
{
|
||||
return syncState == incomplete;
|
||||
}
|
||||
|
||||
void MessageFeed::fetchMore(const QModelIndex& parent)
|
||||
void Models::MessageFeed::fetchMore(const QModelIndex& parent)
|
||||
{
|
||||
if (syncState == incomplete) {
|
||||
beginInsertRows(QModelIndex(), storage.size(), storage.size());
|
||||
syncState = syncing;
|
||||
endInsertRows();
|
||||
emit requestStateChange(true);
|
||||
|
||||
if (storage.size() == 0) {
|
||||
emit requestArchive("");
|
||||
|
@ -156,13 +176,11 @@ void MessageFeed::fetchMore(const QModelIndex& parent)
|
|||
}
|
||||
}
|
||||
|
||||
void MessageFeed::responseArchive(const std::list<Shared::Message> list)
|
||||
void Models::MessageFeed::responseArchive(const std::list<Shared::Message> list)
|
||||
{
|
||||
Storage::size_type size = storage.size();
|
||||
if (syncState == syncing) {
|
||||
beginRemoveRows(QModelIndex(), size, size);
|
||||
syncState = incomplete;
|
||||
endRemoveRows();
|
||||
emit requestStateChange(false);
|
||||
}
|
||||
|
||||
beginInsertRows(QModelIndex(), size, size + list.size() - 1);
|
||||
|
@ -173,7 +191,17 @@ void MessageFeed::responseArchive(const std::list<Shared::Message> list)
|
|||
endInsertRows();
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> MessageFeed::roleNames() const
|
||||
QHash<int, QByteArray> Models::MessageFeed::roleNames() const
|
||||
{
|
||||
return roles;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,13 +29,17 @@
|
|||
#include <boost/multi_index/mem_fun.hpp>
|
||||
|
||||
#include <shared/message.h>
|
||||
#include <shared/icons.h>
|
||||
|
||||
|
||||
namespace Models {
|
||||
class Element;
|
||||
|
||||
class MessageFeed : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
MessageFeed(QObject *parent = nullptr);
|
||||
MessageFeed(const Element* rosterItem, QObject *parent = nullptr);
|
||||
~MessageFeed();
|
||||
|
||||
void addMessage(const Shared::Message& msg);
|
||||
|
@ -55,6 +59,10 @@ public:
|
|||
|
||||
signals:
|
||||
void requestArchive(const QString& before);
|
||||
void requestStateChange(bool requesting);
|
||||
|
||||
protected:
|
||||
bool sentByMe(const Shared::Message& msg) const;
|
||||
|
||||
public:
|
||||
enum MessageRoles {
|
||||
|
@ -63,7 +71,8 @@ public:
|
|||
Date,
|
||||
DeliveryState,
|
||||
Correction,
|
||||
SentByMe
|
||||
SentByMe,
|
||||
Avatar
|
||||
};
|
||||
private:
|
||||
enum SyncState {
|
||||
|
@ -104,10 +113,11 @@ private:
|
|||
StorageById& indexById;
|
||||
StorageByTime& indexByTime;
|
||||
|
||||
const Element* rosterItem;
|
||||
SyncState syncState;
|
||||
|
||||
static const QHash<int, QByteArray> roles;
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
#endif // MESSAGEFEED_H
|
||||
|
|
|
@ -310,7 +310,12 @@ QString Models::Room::getParticipantIconPath(const QString& name) const
|
|||
{
|
||||
std::map<QString, Models::Participant*>::const_iterator itr = participants.find(name);
|
||||
if (itr == participants.end()) {
|
||||
return "";
|
||||
std::map<QString, QString>::const_iterator eitr = exParticipantAvatars.find(name);
|
||||
if (eitr != exParticipantAvatars.end()) {
|
||||
return eitr->second;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
return itr->second->getAvatarPath();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue