Better way to store expanded elements in roster, several clean ups, translations

This commit is contained in:
Blue 2022-08-17 19:25:35 +03:00
parent 7e9eed2075
commit d162494ec8
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
18 changed files with 874 additions and 252 deletions

View file

@ -75,6 +75,11 @@ void Models::Element::update(const QString& field, const QVariant& value)
}
}
QString Models::Element::getId() const
{
return jid;
}
QString Models::Element::getAvatarPath() const
{
return avatarPath;

View file

@ -38,6 +38,7 @@ public:
QString getAvatarPath() const;
virtual void update(const QString& field, const QVariant& value);
virtual QString getId() const override;
void addMessage(const Shared::Message& data);
void changeMessage(const QString& id, const QMap<QString, QVariant>& data);

View file

@ -181,6 +181,11 @@ QString Models::Item::getName() const
return name;
}
QString Models::Item::getId() const
{
return name;
}
QVariant Models::Item::data(int column) const
{
if (column != 0) {

View file

@ -65,6 +65,7 @@ class Item : public QObject{
virtual void appendChild(Item *child);
virtual void removeChild(int index);
virtual QString getDisplayedName() const;
virtual QString getId() const;
QString getName() const;
void setName(const QString& name);

View file

@ -75,6 +75,11 @@ QString Models::Reference::getDisplayedName() const
return original->getDisplayedName();
}
QString Models::Reference::getId() const
{
return original->getId();
}
Models::Item * Models::Reference::dereference()
{
return original;

View file

@ -38,6 +38,8 @@ public:
QString getDisplayedName() const override;
void appendChild(Models::Item * child) override;
void removeChild(int index) override;
QString getId() const override;
Item* dereference();
const Item* dereferenceConst() const;

View file

@ -76,7 +76,6 @@ private:
private:
bool autoJoin;
bool joined;
QString jid;
QString nick;
QString subject;
std::map<QString, Participant*> participants;

View file

@ -52,6 +52,8 @@ void Models::Roster::addAccount(const QMap<QString, QVariant>& data)
root->appendChild(acc);
accounts.insert(std::make_pair(acc->getName(), acc));
accountsModel->addAccount(acc);
emit addedElement({acc->getId()});
}
QVariant Models::Roster::data (const QModelIndex& index, int role) const
@ -433,6 +435,9 @@ void Models::Roster::addGroup(const QString& account, const QString& name)
Group* group = new Group({{"name", name}});
groups.insert(std::make_pair(id, group));
acc->appendChild(group);
emit addedElement({acc->getId(), group->getId()});
} else {
qDebug() << "An attempt to add group " << name << " to non existing account " << account << ", skipping";
}
@ -470,6 +475,7 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons
}
}
std::list<QString> path = {acc->getId()};
if (group == "") {
if (acc->getContact(jid) != -1) {
qDebug() << "An attempt to add a contact" << jid << "to the ungrouped contact set of account" << account << "for the second time, skipping";
@ -486,6 +492,7 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons
}
parent = itr->second;
path.push_back(parent->getId());
if (parent->getContact(jid) != -1) {
qDebug() << "An attempt to add a contact" << jid << "to the group" << group << "for the second time, skipping";
@ -502,11 +509,14 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons
}
}
path.push_back(contact->getId());
if (ref == 0) {
ref = new Reference(contact);
}
parent->appendChild(ref);
emit addedElement(path);
}
void Models::Roster::removeGroup(const QString& account, const QString& name)
@ -694,6 +704,7 @@ void Models::Roster::addPresence(const QString& account, const QString& jid, con
if (itr != contacts.end()) {
itr->second->addPresence(name, data);
}
}
void Models::Roster::removePresence(const QString& account, const QString& jid, const QString& name)
@ -809,6 +820,8 @@ void Models::Roster::addRoom(const QString& account, const QString jid, const QM
connect(room, &Room::unreadMessagesCountChanged, this, &Roster::recalculateUnreadMessages);
rooms.insert(std::make_pair(id, room));
acc->appendChild(room);
emit addedElement({acc->getId(), room->getId()});
}
void Models::Roster::changeRoom(const QString& account, const QString jid, const QMap<QString, QVariant>& data)
@ -961,7 +974,7 @@ bool Models::Roster::markMessageAsRead(const Models::Roster::ElId& elementId, co
}
}
QModelIndex Models::Roster::getAccountIndex(const QString& name)
QModelIndex Models::Roster::getAccountIndex(const QString& name) const
{
std::map<QString, Account*>::const_iterator itr = accounts.find(name);
if (itr == accounts.end()) {
@ -971,7 +984,7 @@ QModelIndex Models::Roster::getAccountIndex(const QString& name)
}
}
QModelIndex Models::Roster::getGroupIndex(const QString& account, const QString& name)
QModelIndex Models::Roster::getGroupIndex(const QString& account, const QString& name) const
{
std::map<QString, Account*>::const_iterator itr = accounts.find(account);
if (itr == accounts.end()) {
@ -987,7 +1000,7 @@ QModelIndex Models::Roster::getGroupIndex(const QString& account, const QString&
}
}
QModelIndex Models::Roster::getContactIndex(const QString& account, const QString& jid, const QString& resource)
QModelIndex Models::Roster::getContactIndex(const QString& account, const QString& jid, const QString& resource) const
{
std::map<QString, Account*>::const_iterator itr = accounts.find(account);
if (itr == accounts.end()) {
@ -1113,3 +1126,66 @@ void Models::Roster::recalculateUnreadMessages()
}
emit unreadMessagesCountChanged(count);
}
std::list<QString> Models::Roster::getItemPath(const QModelIndex& index) const
{
std::list<QString> result;
if (index.isValid() && index.model() == this) {
Item* item = static_cast<Item*>(index.internalPointer());
while (item->type != Item::root) {
result.push_front(item->getId());
item = item->parentItem();
}
}
return result;
}
QModelIndex Models::Roster::getIndexByPath(const std::list<QString>& path) const
{
if (path.empty())
return QModelIndex();
QModelIndex current;
for (const QString& hop : path) {
int rows = rowCount(current);
bool found = false;
for (int i = 0; i < rows; ++i) {
QModelIndex el = index(i, 0, current);
Item* item = static_cast<Item*>(el.internalPointer());
if (item->getId() == hop) {
found = true;
current = el;
break;
}
}
if (!found)
break;
}
return current; //this way I will return the last matching model index, may be it's logically incorrect
// std::list<QString>::const_iterator pathItr = path.begin();
// QString accName = *pathItr;
// QModelIndex accIndex = getAccountIndex(accName);
// if (path.size() == 1)
// return accIndex;
//
// if (!accIndex.isValid())
// return QModelIndex();
//
// ++pathItr;
// ElId id{accName, *pathItr};
// QModelIndex contactIndex = getContactIndex(id.account, id.name);
// if (!contactIndex.isValid())
// contactIndex = getGroupIndex(id.account, id.name);
//
// if (path.size() == 2)
// return contactIndex;
//
// if (!contactIndex.isValid())
// return QModelIndex();
//
// ++pathItr;
}

View file

@ -22,6 +22,7 @@
#include <qabstractitemmodel.h>
#include <deque>
#include <map>
#include <list>
#include <QVector>
#include "shared/message.h"
@ -86,9 +87,12 @@ public:
QString getContactIconPath(const QString& account, const QString& jid, const QString& resource) const;
Account* getAccount(const QString& name);
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 = "");
QModelIndex getAccountIndex(const QString& name) const;
QModelIndex getGroupIndex(const QString& account, const QString& name) const;
QModelIndex getContactIndex(const QString& account, const QString& jid, const QString& resource = "") const;
QModelIndex getIndexByPath(const std::list<QString>& path) const;
std::list<QString> getItemPath(const QModelIndex& index) const;
bool markMessageAsRead(const ElId& elementId, const QString& messageId);
void responseArchive(const QString& account, const QString& jid, const std::list<Shared::Message>& list, bool last);
@ -104,6 +108,7 @@ signals:
void unreadMessagesCountChanged(int count);
void unnoticedMessage(const QString& account, const Shared::Message& msg);
void localPathInvalid(const QString& path);
void addedElement(const std::list<QString>& path); //emits only on addition of Account, Contact, Room or Group. Presence and Participant are ignored
private slots:
void onAccountDataChanged(const QModelIndex& tl, const QModelIndex& br, const QVector<int>& roles);