Now notifications have actions! Some more usefull functions to roster model

This commit is contained in:
Blue 2022-04-24 18:52:29 +03:00
parent 3916aec358
commit e58213b294
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
15 changed files with 205 additions and 30 deletions

View file

@ -155,6 +155,16 @@ void Models::Contact::removePresence(const QString& name)
}
}
Models::Presence * Models::Contact::getPresence(const QString& name)
{
QMap<QString, Presence*>::iterator itr = presences.find(name);
if (itr == presences.end()) {
return nullptr;
} else {
return itr.value();
}
}
void Models::Contact::refresh()
{
QDateTime lastActivity;

View file

@ -51,6 +51,7 @@ public:
void addPresence(const QString& name, const QMap<QString, QVariant>& data);
void removePresence(const QString& name);
Presence* getPresence(const QString& name);
QString getContactName() const;
QString getStatus() const;

View file

@ -134,6 +134,11 @@ unsigned int Models::Element::getMessagesCount() const
return feed->unreadMessagesCount();
}
bool Models::Element::markMessageAsRead(const QString& id) const
{
return feed->markMessageAsRead(id);
}
void Models::Element::addMessage(const Shared::Message& data)
{
feed->addMessage(data);

View file

@ -42,6 +42,7 @@ public:
void addMessage(const Shared::Message& data);
void changeMessage(const QString& id, const QMap<QString, QVariant>& data);
unsigned int getMessagesCount() const;
bool markMessageAsRead(const QString& id) const;
void responseArchive(const std::list<Shared::Message> list, bool last);
bool isRoom() const;
void fileProgress(const QString& messageId, qreal value, bool up);

View file

@ -264,6 +264,16 @@ void Models::Room::removeParticipant(const QString& p_name)
}
}
Models::Participant * Models::Room::getParticipant(const QString& p_name)
{
std::map<QString, Participant*>::const_iterator itr = participants.find(p_name);
if (itr == participants.end()) {
return nullptr;
} else {
return itr->second;
}
}
void Models::Room::handleParticipantUpdate(std::map<QString, Participant*>::const_iterator itr, const QMap<QString, QVariant>& data)
{
Participant* part = itr->second;

View file

@ -58,6 +58,7 @@ public:
void addParticipant(const QString& name, const QMap<QString, QVariant>& data);
void changeParticipant(const QString& name, const QMap<QString, QVariant>& data);
void removeParticipant(const QString& name);
Participant* getParticipant(const QString& name);
void toOfflineState() override;
QString getDisplayedName() const override;

View file

@ -549,8 +549,8 @@ void Models::Roster::removeGroup(const QString& account, const QString& name)
void Models::Roster::changeContact(const QString& account, const QString& jid, const QMap<QString, QVariant>& data)
{
Element* el = getElement({account, jid});
if (el != NULL) {
Element* el = getElement(ElId(account, jid));
if (el != nullptr) {
for (QMap<QString, QVariant>::const_iterator itr = data.begin(), end = data.end(); itr != end; ++itr) {
el->update(itr.key(), itr.value());
}
@ -559,8 +559,8 @@ void Models::Roster::changeContact(const QString& account, const QString& jid, c
void Models::Roster::changeMessage(const QString& account, const QString& jid, const QString& id, const QMap<QString, QVariant>& data)
{
Element* el = getElement({account, jid});
if (el != NULL) {
Element* el = getElement(ElId(account, jid));
if (el != nullptr) {
el->changeMessage(id, data);
} else {
qDebug() << "A request to change a message of the contact " << jid << " in the account " << account << " but it wasn't found";
@ -707,8 +707,8 @@ void Models::Roster::removePresence(const QString& account, const QString& jid,
void Models::Roster::addMessage(const QString& account, const Shared::Message& data)
{
Element* el = getElement({account, data.getPenPalJid()});
if (el != NULL) {
Element* el = getElement(ElId(account, data.getPenPalJid()));
if (el != nullptr) {
el->addMessage(data);
}
}
@ -948,9 +948,18 @@ const Models::Element * Models::Roster::getElementConst(const Models::Roster::El
}
}
return NULL;
return nullptr;
}
bool Models::Roster::markMessageAsRead(const Models::Roster::ElId& elementId, const QString& messageId)
{
const Element* el = getElementConst(elementId);
if (el != nullptr) {
return el->markMessageAsRead(messageId);
} else {
return false;
}
}
QModelIndex Models::Roster::getAccountIndex(const QString& name)
{
@ -968,7 +977,7 @@ QModelIndex Models::Roster::getGroupIndex(const QString& account, const QString&
if (itr == accounts.end()) {
return QModelIndex();
} else {
std::map<ElId, Group*>::const_iterator gItr = groups.find({account, name});
std::map<ElId, Group*>::const_iterator gItr = groups.find(ElId(account, name));
if (gItr == groups.end()) {
return QModelIndex();
} else {
@ -978,6 +987,48 @@ QModelIndex Models::Roster::getGroupIndex(const QString& account, const QString&
}
}
QModelIndex Models::Roster::getContactIndex(const QString& account, const QString& jid, const QString& resource)
{
std::map<QString, Account*>::const_iterator itr = accounts.find(account);
if (itr == accounts.end()) {
return QModelIndex();
} else {
Account* acc = itr->second;
QModelIndex accIndex = index(acc->row(), 0, QModelIndex());
std::map<ElId, Contact*>::const_iterator cItr = contacts.find(ElId(account, jid));
if (cItr != contacts.end()) {
QModelIndex contactIndex = index(acc->getContact(jid), 0, accIndex);
if (resource.size() == 0) {
return contactIndex;
} else {
Presence* pres = cItr->second->getPresence(resource);
if (pres != nullptr) {
return index(pres->row(), 0, contactIndex);
} else {
return contactIndex;
}
}
} else {
std::map<ElId, Room*>::const_iterator rItr = rooms.find(ElId(account, jid));
if (rItr != rooms.end()) {
QModelIndex roomIndex = index(rItr->second->row(), 0, accIndex);
if (resource.size() == 0) {
return roomIndex;
} else {
Participant* part = rItr->second->getParticipant(resource);
if (part != nullptr) {
return index(part->row(), 0, roomIndex);
} else {
return roomIndex;
}
}
} else {
return QModelIndex();
}
}
}
}
void Models::Roster::onElementRequestArchive(const QString& before)
{
Element* el = static_cast<Element*>(sender());
@ -988,7 +1039,7 @@ void Models::Roster::responseArchive(const QString& account, const QString& jid,
{
ElId id(account, jid);
Element* el = getElement(id);
if (el != NULL) {
if (el != nullptr) {
el->responseArchive(list, last);
}
}
@ -996,8 +1047,8 @@ void Models::Roster::responseArchive(const QString& account, const QString& jid,
void Models::Roster::fileProgress(const std::list<Shared::MessageInfo>& msgs, qreal value, bool up)
{
for (const Shared::MessageInfo& info : msgs) {
Element* el = getElement({info.account, info.jid});
if (el != NULL) {
Element* el = getElement(ElId(info.account, info.jid));
if (el != nullptr) {
el->fileProgress(info.messageId, value, up);
}
}
@ -1006,8 +1057,8 @@ void Models::Roster::fileProgress(const std::list<Shared::MessageInfo>& msgs, qr
void Models::Roster::fileComplete(const std::list<Shared::MessageInfo>& msgs, bool up)
{
for (const Shared::MessageInfo& info : msgs) {
Element* el = getElement({info.account, info.jid});
if (el != NULL) {
Element* el = getElement(ElId(info.account, info.jid));
if (el != nullptr) {
el->fileComplete(info.messageId, up);
}
}
@ -1016,8 +1067,8 @@ void Models::Roster::fileComplete(const std::list<Shared::MessageInfo>& msgs, bo
void Models::Roster::fileError(const std::list<Shared::MessageInfo>& msgs, const QString& err, bool up)
{
for (const Shared::MessageInfo& info : msgs) {
Element* el = getElement({info.account, info.jid});
if (el != NULL) {
Element* el = getElement(ElId(info.account, info.jid));
if (el != nullptr) {
el->fileError(info.messageId, err, up);
}
}
@ -1031,7 +1082,7 @@ Models::Element * Models::Roster::getElement(const Models::Roster::ElId& id)
Models::Item::Type Models::Roster::getContactType(const Models::Roster::ElId& id) const
{
const Models::Element* el = getElementConst(id);
if (el == NULL) {
if (el == nullptr) {
return Item::root;
}

View file

@ -88,6 +88,8 @@ public:
const Account* getAccountConst(const QString& name) const;
QModelIndex getAccountIndex(const QString& name);
QModelIndex getGroupIndex(const QString& account, const QString& name);
QModelIndex getContactIndex(const QString& account, const QString& jid, const QString& resource = "");
bool markMessageAsRead(const ElId& elementId, const QString& messageId);
void responseArchive(const QString& account, const QString& jid, const std::list<Shared::Message>& list, bool last);
void fileProgress(const std::list<Shared::MessageInfo>& msgs, qreal value, bool up);
@ -115,7 +117,7 @@ private slots:
void onChildMoved();
void onElementRequestArchive(const QString& before);
void recalculateUnreadMessages();
private:
Item* root;
std::map<QString, Account*> accounts;

View file

@ -656,3 +656,8 @@ Models::Roster::ElId Squawk::currentConversationId() const
}
}
void Squawk::select(QModelIndex index)
{
m_ui->roster->scrollTo(index, QAbstractItemView::EnsureVisible);
m_ui->roster->selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
}

View file

@ -90,6 +90,7 @@ public slots:
void writeSettings();
void stateChanged(Shared::Availability state);
void responseVCard(const QString& jid, const Shared::VCard& card);
void select(QModelIndex index);
private:
QScopedPointer<Ui::Squawk> m_ui;

View file

@ -318,12 +318,7 @@ QVariant Models::MessageFeed::data(const QModelIndex& index, int role) const
case Bulk: {
FeedItem item;
item.id = msg->getId();
std::set<QString>::const_iterator umi = unreadMessages->find(item.id);
if (umi != unreadMessages->end()) {
unreadMessages->erase(umi);
emit unreadMessagesCountChanged();
}
markMessageAsRead(item.id);
item.sentByMe = sentByMe(*msg);
item.date = msg->getTime();
@ -373,6 +368,17 @@ int Models::MessageFeed::rowCount(const QModelIndex& parent) const
return storage.size();
}
bool Models::MessageFeed::markMessageAsRead(const QString& id) const
{
std::set<QString>::const_iterator umi = unreadMessages->find(id);
if (umi != unreadMessages->end()) {
unreadMessages->erase(umi);
emit unreadMessagesCountChanged();
return true;
}
return false;
}
unsigned int Models::MessageFeed::unreadMessagesCount() const
{
return unreadMessages->size();

View file

@ -72,6 +72,7 @@ public:
void reportLocalPathInvalid(const QString& messageId);
unsigned int unreadMessagesCount() const;
bool markMessageAsRead(const QString& id) const;
void fileProgress(const QString& messageId, qreal value, bool up);
void fileError(const QString& messageId, const QString& error, bool up);
void fileComplete(const QString& messageId, bool up);