From 52efc2b1a4e5cd14d5795b4730e3748ccb697cf7 Mon Sep 17 00:00:00 2001 From: blue Date: Tue, 31 Dec 2019 21:14:12 +0300 Subject: [PATCH] now we have avatars in muc chats --- ui/models/room.cpp | 23 +++++++++++++++++++++++ ui/models/room.h | 6 ++++++ ui/models/roster.cpp | 4 ++-- ui/models/roster.h | 2 +- ui/squawk.cpp | 14 ++++++++++---- ui/utils/messageline.cpp | 18 ++++++++++++------ ui/widgets/room.cpp | 38 ++++++++++++++++++++++++++++++++++++++ ui/widgets/room.h | 2 ++ 8 files changed, 94 insertions(+), 13 deletions(-) diff --git a/ui/models/room.cpp b/ui/models/room.cpp index 72eafd2..55e64ca 100644 --- a/ui/models/room.cpp +++ b/ui/models/room.cpp @@ -276,6 +276,7 @@ void Models::Room::addParticipant(const QString& p_name, const QMapsetName(p_name); participants.insert(std::make_pair(p_name, part)); appendChild(part); + emit participantJoined(*part); } } @@ -299,6 +300,7 @@ void Models::Room::removeParticipant(const QString& p_name) participants.erase(itr); removeChild(p->row()); p->deleteLater(); + emit participantLeft(p_name); } } @@ -373,3 +375,24 @@ void Models::Room::setAvatarState(unsigned int p_state) qDebug() << "An attempt to set invalid avatar state" << p_state << "to the room" << jid << ", skipping"; } } + +std::map Models::Room::getParticipants() const +{ + std::map result; + + for (std::pair pair : participants) { + result.emplace(pair.first, *(pair.second)); + } + + return result; +} + +QString Models::Room::getParticipantIconPath(const QString& name) const +{ + std::map::const_iterator itr = participants.find(name); + if (itr == participants.end()) { + return ""; + } + + return itr->second->getAvatarPath(); +} diff --git a/ui/models/room.h b/ui/models/room.h index 3a6ebee..4c3b9d9 100644 --- a/ui/models/room.h +++ b/ui/models/room.h @@ -71,6 +71,12 @@ public: QString getDisplayedName() const override; Shared::Avatar getAvatarState() const; QString getAvatarPath() const; + std::map getParticipants() const; + QString getParticipantIconPath(const QString& name) const; + +signals: + void participantJoined(const Participant& participant); + void participantLeft(const QString& name); private: void handleParticipantUpdate(std::map::const_iterator itr, const QMap& data); diff --git a/ui/models/roster.cpp b/ui/models/roster.cpp index 63bd290..44b4ac8 100644 --- a/ui/models/roster.cpp +++ b/ui/models/roster.cpp @@ -949,7 +949,7 @@ bool Models::Roster::groupHasContact(const QString& account, const QString& grou } } -QString Models::Roster::getContactIconPath(const QString& account, const QString& jid) +QString Models::Roster::getContactIconPath(const QString& account, const QString& jid, const QString& resource) { ElId id(account, jid); std::multimap::const_iterator cItr = contacts.find(id); @@ -959,7 +959,7 @@ QString Models::Roster::getContactIconPath(const QString& account, const QString if (rItr == rooms.end()) { qDebug() << "An attempt to get an icon path of non existing contact" << account << ":" << jid << ", returning empty value"; } else { - //path = rItr->second->getRoomName(); + path = rItr->second->getParticipantIconPath(resource); } } else { if (cItr->second->getAvatarState() != Shared::Avatar::empty) { diff --git a/ui/models/roster.h b/ui/models/roster.h index a2ffdc2..542d0b7 100644 --- a/ui/models/roster.h +++ b/ui/models/roster.h @@ -73,7 +73,7 @@ public: std::deque groupList(const QString& account) const; bool groupHasContact(const QString& account, const QString& group, const QString& contactJID) const; - QString getContactIconPath(const QString& account, const QString& jid); + QString getContactIconPath(const QString& account, const QString& jid, const QString& resource); Account* getAccount(const QString& name); QModelIndex getAccountIndex(const QString& name); QModelIndex getGroupIndex(const QString& account, const QString& name); diff --git a/ui/squawk.cpp b/ui/squawk.cpp index 915c42c..0f2ce5d 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -230,7 +230,6 @@ void Squawk::addGroup(const QString& account, const QString& name) settings.beginGroup(account); if (settings.value("expanded", false).toBool()) { QModelIndex ind = rosterModel.getAccountIndex(account); - qDebug() << "expanding account " << ind.data(); m_ui->roster->expand(ind); if (settings.value(name + "/expanded", false).toBool()) { m_ui->roster->expand(rosterModel.getGroupIndex(account, name)); @@ -497,21 +496,28 @@ void Squawk::accountMessage(const QString& account, const Shared::Message& data) void Squawk::notify(const QString& account, const Shared::Message& msg) { QString name = QString(rosterModel.getContactName(account, msg.getPenPalJid())); - QString path = QString(rosterModel.getContactIconPath(account, msg.getPenPalJid())); + QString path = QString(rosterModel.getContactIconPath(account, msg.getPenPalJid(), msg.getPenPalResource())); QVariantList args; args << QString(QCoreApplication::applicationName()); args << QVariant(QVariant::UInt); //TODO some normal id if (path.size() > 0) { args << path; } else { - args << QString("mail-message"); + args << QString("mail-message"); //TODO should here better be unknown user icon? } if (msg.getType() == Shared::Message::groupChat) { args << msg.getFromResource() + " from " + name; } else { args << name; } - args << QString(msg.getBody()); + + QString body(msg.getBody()); + QString oob(msg.getOutOfBandUrl()); + if (body == oob) { + body = tr("Attached file"); + } + + args << body; args << QStringList(); args << QVariantMap(); args << 3000; diff --git a/ui/utils/messageline.cpp b/ui/utils/messageline.cpp index 013d94c..ba95259 100644 --- a/ui/utils/messageline.cpp +++ b/ui/utils/messageline.cpp @@ -74,6 +74,10 @@ MessageLine::Position MessageLine::message(const Shared::Message& msg, bool forc outgoing = true; } else { sender = msg.getFromResource(); + std::map::iterator aItr = palAvatars.find(sender); + if (aItr != palAvatars.end()) { + aPath = aItr->second; + } outgoing = false; } } else { @@ -111,16 +115,18 @@ MessageLine::Position MessageLine::message(const Shared::Message& msg, bool forc if (outgoing) { myMessages.insert(std::make_pair(id, message)); } else { + QString senderId; if (room) { - + senderId = sender; } else { QString jid = msg.getFromJid(); - std::map::iterator pItr = palMessages.find(jid); - if (pItr == palMessages.end()) { - pItr = palMessages.insert(std::make_pair(jid, Index())).first; - } - pItr->second.insert(std::make_pair(id, message)); } + + std::map::iterator pItr = palMessages.find(senderId); + if (pItr == palMessages.end()) { + pItr = palMessages.insert(std::make_pair(senderId, Index())).first; + } + pItr->second.insert(std::make_pair(id, message)); } messageIndex.insert(std::make_pair(id, message)); unsigned long index = std::distance(messageOrder.begin(), result.first); //need to make with binary indexed tree diff --git a/ui/widgets/room.cpp b/ui/widgets/room.cpp index 49c422c..f495432 100644 --- a/ui/widgets/room.cpp +++ b/ui/widgets/room.cpp @@ -28,6 +28,16 @@ Room::Room(Models::Account* acc, Models::Room* p_room, QWidget* parent): setAvatar(room->getAvatarPath()); connect(room, &Models::Room::childChanged, this, &Room::onRoomChanged); + connect(room, &Models::Room::participantJoined, this, &Room::onParticipantJoined); + connect(room, &Models::Room::participantLeft, this, &Room::onParticipantLeft); + + std::map members = room->getParticipants(); + for (std::pair pair : members) { + QString aPath = pair.second.getAvatarPath(); + if (aPath.size() > 0) { + line->setPalAvatar(pair.first, aPath); + } + } } Room::~Room() @@ -62,6 +72,34 @@ void Room::onRoomChanged(Models::Item* item, int row, int col) case 6: setStatus(room->getSubject()); break; + case 8: + setAvatar(room->getAvatarPath()); + break; + } + } else { + switch (col) { + case 7: { + Models::Participant* mem = static_cast(item); + QString aPath = mem->getAvatarPath(); + if (aPath.size() > 0) { + line->setPalAvatar(mem->getName(), aPath); + } else { + line->dropPalAvatar(mem->getName()); + } + } } } } + +void Room::onParticipantJoined(const Models::Participant& participant) +{ + QString aPath = participant.getAvatarPath(); + if (aPath.size() > 0) { + line->setPalAvatar(participant.getName(), aPath); + } +} + +void Room::onParticipantLeft(const QString& name) +{ + line->dropPalAvatar(name); +} diff --git a/ui/widgets/room.h b/ui/widgets/room.h index bf08615..2cf7831 100644 --- a/ui/widgets/room.h +++ b/ui/widgets/room.h @@ -36,6 +36,8 @@ public: protected slots: void onRoomChanged(Models::Item* item, int row, int col); + void onParticipantJoined(const Models::Participant& participant); + void onParticipantLeft(const QString& name); protected: void handleSendMessage(const QString & text) override;