1
0
forked from blue/squawk

first prototype

This commit is contained in:
Blue 2020-04-18 02:17:47 +03:00
parent 494afcf2b5
commit 83a2e6af85
13 changed files with 365 additions and 244 deletions

View File

@ -17,6 +17,8 @@
*/ */
#include "account.h" #include "account.h"
#include "contact.h"
#include "reference.h"
#include <QDebug> #include <QDebug>
Models::Account::Account(const QMap<QString, QVariant>& data, Models::Item* parentItem): Models::Account::Account(const QMap<QString, QVariant>& data, Models::Item* parentItem):
@ -267,4 +269,3 @@ void Models::Account::setPasswordType(unsigned int pt)
{ {
setPasswordType(Shared::Global::fromInt<Shared::AccountPassword>(pt)); setPasswordType(Shared::Global::fromInt<Shared::AccountPassword>(pt));
} }

View File

@ -28,6 +28,9 @@
#include <QIcon> #include <QIcon>
namespace Models { namespace Models {
class Contact;
class Reference;
class Account : public Item { class Account : public Item {
Q_OBJECT Q_OBJECT
public: public:

View File

@ -17,10 +17,11 @@
*/ */
#include "contact.h" #include "contact.h"
#include "account.h"
#include <QDebug> #include <QDebug>
Models::Contact::Contact(const QString& p_jid ,const QMap<QString, QVariant> &data, Item *parentItem): Models::Contact::Contact(const Account* acc, const QString& p_jid ,const QMap<QString, QVariant> &data, Item *parentItem):
Item(Item::contact, data, parentItem), Item(Item::contact, data, parentItem),
jid(p_jid), jid(p_jid),
availability(Shared::Availability::offline), availability(Shared::Availability::offline),
@ -30,7 +31,8 @@ Models::Contact::Contact(const QString& p_jid ,const QMap<QString, QVariant> &da
messages(), messages(),
childMessages(0), childMessages(0),
status(), status(),
avatarPath() avatarPath(),
account(acc)
{ {
QMap<QString, QVariant>::const_iterator itr = data.find("state"); QMap<QString, QVariant>::const_iterator itr = data.find("state");
if (itr != data.end()) { if (itr != data.end()) {
@ -368,7 +370,8 @@ Models::Contact::Contact(const Models::Contact& other):
state(other.state), state(other.state),
presences(), presences(),
messages(other.messages), messages(other.messages),
childMessages(0) childMessages(0),
account(other.account)
{ {
for (const Presence* pres : other.presences) { for (const Presence* pres : other.presences) {
Presence* pCopy = new Presence(*pres); Presence* pCopy = new Presence(*pres);
@ -415,3 +418,9 @@ void Models::Contact::setAvatarState(unsigned int p_state)
qDebug() << "An attempt to set invalid avatar state" << p_state << "to the contact" << jid << ", skipping"; qDebug() << "An attempt to set invalid avatar state" << p_state << "to the contact" << jid << ", skipping";
} }
} }
const Models::Account * Models::Contact::getParentAccount() const
{
return account;
}

View File

@ -31,13 +31,14 @@
#include <deque> #include <deque>
namespace Models { namespace Models {
class Account;
class Contact : public Item class Contact : public Item
{ {
Q_OBJECT Q_OBJECT
public: public:
typedef std::deque<Shared::Message> Messages; typedef std::deque<Shared::Message> Messages;
Contact(const QString& p_jid, const QMap<QString, QVariant> &data, Item *parentItem = 0); Contact(const Account* acc, const QString& p_jid, const QMap<QString, QVariant> &data, Item *parentItem = 0);
Contact(const Contact& other); Contact(const Contact& other);
~Contact(); ~Contact();
@ -72,6 +73,7 @@ public:
protected: protected:
void _removeChild(int index) override; void _removeChild(int index) override;
bool columnInvolvedInDisplay(int col) override; bool columnInvolvedInDisplay(int col) override;
const Account* getParentAccount() const override;
protected slots: protected slots:
void refresh(); void refresh();
@ -98,6 +100,7 @@ private:
unsigned int childMessages; unsigned int childMessages;
QString status; QString status;
QString avatarPath; QString avatarPath;
const Account* account;
}; };
} }

View File

@ -18,6 +18,7 @@
#include "group.h" #include "group.h"
#include "contact.h" #include "contact.h"
#include "reference.h"
Models::Group::Group(const QMap<QString, QVariant>& data, Models::Item* parentItem): Models::Group::Group(const QMap<QString, QVariant>& data, Models::Item* parentItem):
Item(group, data, parentItem), Item(group, data, parentItem),
@ -103,16 +104,3 @@ unsigned int Models::Group::getOnlineContacts() const
return amount; return amount;
} }
bool Models::Group::hasContact(const QString& jid) const
{
for (Models::Item* item : childItems) {
if (item->type == Item::contact) {
const Contact* cnt = static_cast<const Contact*>(item);
if (cnt->getJid() == jid) {
return true;
}
}
}
return false;
}

View File

@ -23,6 +23,8 @@
namespace Models { namespace Models {
class Contact;
class Group : public Models::Item class Group : public Models::Item
{ {
Q_OBJECT Q_OBJECT
@ -36,8 +38,6 @@ public:
unsigned int getUnreadMessages() const; unsigned int getUnreadMessages() const;
unsigned int getOnlineContacts() const; unsigned int getOnlineContacts() const;
bool hasContact(const QString& jid) const;
protected: protected:
void _removeChild(int index) override; void _removeChild(int index) override;

View File

@ -18,6 +18,8 @@
#include "item.h" #include "item.h"
#include "account.h" #include "account.h"
#include "reference.h"
#include "contact.h"
#include <QDebug> #include <QDebug>
@ -46,11 +48,16 @@ Models::Item::Item(const Models::Item& other):
Models::Item::~Item() Models::Item::~Item()
{ {
std::deque<Item*>::const_iterator itr = childItems.begin(); for (Reference* ref : references) {
std::deque<Item*>::const_iterator end = childItems.end(); Item* parent = ref->parentItem();
if (parent != nullptr) {
parent->removeChild(ref->row());
}
delete ref;
}
for (;itr != end; ++itr) { for (Item* child : childItems) {
delete (*itr); delete child;
} }
} }
@ -67,18 +74,26 @@ void Models::Item::appendChild(Models::Item* child)
bool moving = false; bool moving = false;
int newRow = 0; int newRow = 0;
std::deque<Item*>::const_iterator before = childItems.begin(); std::deque<Item*>::const_iterator before = childItems.begin();
Type ct = child->type;
if (ct == reference) {
ct = static_cast<Reference*>(child)->dereference()->type;
}
while (before != childItems.end()) { while (before != childItems.end()) {
Item* bfr = *before; Item* bfr = *before;
if (bfr->type > child->type) { Type bt = bfr->type;
if (bt == reference) {
bt = static_cast<Reference*>(bfr)->dereference()->type;
}
if (bt > ct) {
break; break;
} else if (bfr->type == child->type && bfr->getDisplayedName() > child->getDisplayedName()) { } else if (bt == ct && bfr->getDisplayedName() > child->getDisplayedName()) {
break; break;
} }
newRow++; newRow++;
before++; before++;
} }
if (child->parent != 0) { if (child->parent != nullptr) {
int oldRow = child->row(); int oldRow = child->row();
moving = true; moving = true;
emit childIsAboutToBeMoved(child->parent, oldRow, oldRow, this, newRow); emit childIsAboutToBeMoved(child->parent, oldRow, oldRow, this, newRow);
@ -116,7 +131,7 @@ int Models::Item::childCount() const
int Models::Item::row() const int Models::Item::row() const
{ {
if (parent != 0) { if (parent != nullptr) {
std::deque<Item*>::const_iterator itr = parent->childItems.begin(); std::deque<Item*>::const_iterator itr = parent->childItems.begin();
std::deque<Item*>::const_iterator end = parent->childItems.end(); std::deque<Item*>::const_iterator end = parent->childItems.end();
@ -127,7 +142,7 @@ int Models::Item::row() const
} }
} }
return 0; //TODO not sure how it helps, i copy-pasted it from the example return -1; //TODO not sure how it helps
} }
Models::Item * Models::Item::parentItem() Models::Item * Models::Item::parentItem()
@ -178,13 +193,13 @@ void Models::Item::_removeChild(int index)
QObject::disconnect(child, &Item::childMoved, this, &Item::childMoved); QObject::disconnect(child, &Item::childMoved, this, &Item::childMoved);
childItems.erase(childItems.begin() + index); childItems.erase(childItems.begin() + index);
child->parent = 0; child->parent = nullptr;
} }
void Models::Item::changed(int col) void Models::Item::changed(int col)
{ {
if (parent != 0) { if (parent != nullptr) {
emit childChanged(this, row(), col); emit childChanged(this, row(), col);
} }
} }
@ -197,21 +212,21 @@ void Models::Item::toOfflineState()
} }
} }
const Models::Item * Models::Item::getParentAccount() const const Models::Account * Models::Item::getParentAccount() const
{ {
const Item* p = this; const Item* p = this;
while (p != 0 && p->type != Item::account) { while (p != nullptr && p->type != Item::account) {
p = p->parentItemConst(); p = p->parentItemConst();
} }
return p; return static_cast<const Account*>(p);
} }
QString Models::Item::getAccountJid() const QString Models::Item::getAccountJid() const
{ {
const Account* acc = static_cast<const Account*>(getParentAccount()); const Account* acc = getParentAccount();
if (acc == 0) { if (acc == nullptr) {
return ""; return "";
} }
return acc->getLogin() + "@" + acc->getServer(); return acc->getLogin() + "@" + acc->getServer();
@ -219,8 +234,8 @@ QString Models::Item::getAccountJid() const
QString Models::Item::getAccountResource() const QString Models::Item::getAccountResource() const
{ {
const Account* acc = static_cast<const Account*>(getParentAccount()); const Account* acc = getParentAccount();
if (acc == 0) { if (acc == nullptr) {
return ""; return "";
} }
return acc->getResource(); return acc->getResource();
@ -228,8 +243,8 @@ QString Models::Item::getAccountResource() const
QString Models::Item::getAccountName() const QString Models::Item::getAccountName() const
{ {
const Account* acc = static_cast<const Account*>(getParentAccount()); const Account* acc = getParentAccount();
if (acc == 0) { if (acc == nullptr) {
return ""; return "";
} }
return acc->getName(); return acc->getName();
@ -237,8 +252,8 @@ QString Models::Item::getAccountName() const
Shared::Availability Models::Item::getAccountAvailability() const Shared::Availability Models::Item::getAccountAvailability() const
{ {
const Account* acc = static_cast<const Account*>(getParentAccount()); const Account* acc = getParentAccount();
if (acc == 0) { if (acc == nullptr) {
return Shared::Availability::offline; return Shared::Availability::offline;
} }
return acc->getAvailability(); return acc->getAvailability();
@ -246,8 +261,8 @@ Shared::Availability Models::Item::getAccountAvailability() const
Shared::ConnectionState Models::Item::getAccountConnectionState() const Shared::ConnectionState Models::Item::getAccountConnectionState() const
{ {
const Account* acc = static_cast<const Account*>(getParentAccount()); const Account* acc = getParentAccount();
if (acc == 0) { if (acc == nullptr) {
return Shared::ConnectionState::disconnected; return Shared::ConnectionState::disconnected;
} }
return acc->getState(); return acc->getState();
@ -261,15 +276,24 @@ QString Models::Item::getDisplayedName() const
void Models::Item::onChildChanged(Models::Item* item, int row, int col) void Models::Item::onChildChanged(Models::Item* item, int row, int col)
{ {
Item* parent = item->parentItem(); Item* parent = item->parentItem();
if (parent != 0 && parent == this) { if (parent != nullptr && parent == this) {
if (item->columnInvolvedInDisplay(col)) { if (item->columnInvolvedInDisplay(col)) {
int newRow = 0; int newRow = 0;
std::deque<Item*>::const_iterator before = childItems.begin(); std::deque<Item*>::const_iterator before = childItems.begin();
Type ct = item->type;
if (ct == reference) {
ct = static_cast<Reference*>(item)->dereference()->type;
}
while (before != childItems.end()) { while (before != childItems.end()) {
Item* bfr = *before; Item* bfr = *before;
if (bfr->type > item->type) { Type bt = bfr->type;
if (bt == reference) {
bt = static_cast<Reference*>(bfr)->dereference()->type;
}
if (bt > ct) {
break; break;
} else if (bfr->type == item->type && bfr->getDisplayedName() > item->getDisplayedName()) { } else if (bt == ct && bfr->getDisplayedName() > item->getDisplayedName()) {
break; break;
} }
newRow++; newRow++;
@ -293,3 +317,42 @@ bool Models::Item::columnInvolvedInDisplay(int col)
{ {
return col == 0; return col == 0;
} }
void Models::Item::addReference(Models::Reference* ref)
{
references.insert(ref);
}
void Models::Item::removeReference(Models::Reference* ref)
{
std::set<Reference*>::const_iterator itr = references.find(ref);
if (itr != references.end()) {
references.erase(itr);
}
}
int Models::Item::getContact(const QString& jid) const
{
int index = -1;
for (std::deque<Item*>::size_type i = 0; i < childItems.size(); ++i) {
const Models::Item* item = childItems[i];
const Contact* cnt = nullptr;
if (item->type == Item::reference) {
item = static_cast<const Reference*>(item)->dereferenceConst();
}
if (item->type == Item::contact) {
cnt = static_cast<const Contact*>(item);
if (cnt->getJid() == jid) {
index = i;
break;
}
}
}
return index;
}
std::set<Models::Reference *>::size_type Models::Item::referencesCount() const
{
return references.size();
}

View File

@ -24,11 +24,14 @@
#include <QVariant> #include <QVariant>
#include <deque> #include <deque>
#include <set>
#include "shared/enums.h" #include "shared/enums.h"
namespace Models { namespace Models {
class Reference; class Reference;
class Contact;
class Account;
class Item : public QObject{ class Item : public QObject{
friend class Reference; friend class Reference;
@ -82,11 +85,17 @@ class Item : public QObject{
const Type type; const Type type;
int getContact(const QString& jid) const;
std::set<Reference*>::size_type referencesCount() const;
void addReference(Reference* ref);
void removeReference(Reference* ref);
protected: protected:
virtual void changed(int col); virtual void changed(int col);
virtual void _removeChild(int index); virtual void _removeChild(int index);
virtual bool columnInvolvedInDisplay(int col); virtual bool columnInvolvedInDisplay(int col);
const Item* getParentAccount() const; virtual const Account* getParentAccount() const;
protected slots: protected slots:
void onChildChanged(Models::Item* item, int row, int col); void onChildChanged(Models::Item* item, int row, int col);
@ -95,7 +104,7 @@ class Item : public QObject{
QString name; QString name;
std::deque<Item*> childItems; std::deque<Item*> childItems;
Item* parent; Item* parent;
std::deque<Item*> references; std::set<Reference*> references;
protected slots: protected slots:
virtual void toOfflineState(); virtual void toOfflineState();

View File

@ -17,39 +17,37 @@
*/ */
#include "reference.h" #include "reference.h"
#include <QDebug>
using namespace Models; using namespace Models;
Models::Reference::Reference(Models::Item* original, Models::Item* parent): Models::Reference::Reference(Models::Item* original, Models::Item* parent):
Models::Item(reference, {}, parent), Models::Item(reference, {}, parent),
original(original) original(original),
ax(-1),
bx(-1),
cx(-1),
c(false)
{ {
original->references.push_back(this); connect(original, &Item::childChanged, this, &Reference::onChildChanged);
connect(original, &Item::childIsAboutToBeInserted, this, &Reference::onChildIsAboutToBeInserted);
connect(original, &Item::childInserted, this, &Reference::onChildInserted);
connect(original, &Item::childIsAboutToBeRemoved, this, &Reference::onChildIsAboutToBeRemoved);
connect(original, &Item::childRemoved, this, &Reference::onChildRemoved);
connect(original, &Item::childIsAboutToBeMoved, this, &Reference::onChildIsAboutToBeMoved);
connect(original, &Item::childMoved, this, &Reference::onChildMoved);
connect(original, &Item::childChanged, this, &Item::childChanged); original->addReference(this);
connect(original, &Item::childIsAboutToBeInserted, this, &Item::childIsAboutToBeInserted);
connect(original, &Item::childInserted, this, &Item::childInserted);
connect(original, &Item::childIsAboutToBeRemoved, this, &Item::childIsAboutToBeRemoved);
connect(original, &Item::childRemoved, this, &Item::childRemoved);
connect(original, &Item::childIsAboutToBeMoved, this, &Item::childIsAboutToBeMoved);
connect(original, &Item::childMoved, this, &Item::childMoved);
} }
Models::Reference::~Reference() Models::Reference::~Reference()
{ {
disconnect(original, &Item::childIsAboutToBeInserted, this, &Item::childIsAboutToBeInserted); disconnect(original, &Item::childIsAboutToBeInserted, this, &Reference::onChildIsAboutToBeInserted);
disconnect(original, &Item::childInserted, this, &Item::childInserted); disconnect(original, &Item::childInserted, this, &Reference::onChildInserted);
disconnect(original, &Item::childIsAboutToBeRemoved, this, &Item::childIsAboutToBeRemoved); disconnect(original, &Item::childIsAboutToBeRemoved, this, &Reference::onChildIsAboutToBeRemoved);
disconnect(original, &Item::childRemoved, this, &Item::childRemoved); disconnect(original, &Item::childRemoved, this, &Reference::onChildRemoved);
disconnect(original, &Item::childIsAboutToBeMoved, this, &Item::childIsAboutToBeMoved); disconnect(original, &Item::childIsAboutToBeMoved, this, &Reference::onChildIsAboutToBeMoved);
disconnect(original, &Item::childMoved, this, &Item::childMoved); disconnect(original, &Item::childMoved, this, &Reference::onChildMoved);
for (std::deque<Item*>::const_iterator itr = original->references.begin(), end = original->references.end(); itr != end; itr++) {
if (*itr == this) {
original->references.erase(itr);
break;
}
}
} }
int Models::Reference::columnCount() const int Models::Reference::columnCount() const
@ -91,3 +89,79 @@ void Models::Reference::toOfflineState()
{ {
original->toOfflineState(); original->toOfflineState();
} }
void Models::Reference::onChildChanged(Models::Item* item, int row, int col)
{
if (item == original) {
emit childChanged(this, row, col);
}
}
void Models::Reference::onChildIsAboutToBeInserted(Models::Item* parent, int first, int last)
{
if (parent == original) {
ax = first;
bx = last;
}
}
void Models::Reference::onChildInserted()
{
if (ax != -1 && bx != -1) {
for (int i = ax; i <= bx; ++i) {
Reference* ref = new Reference(original->child(i));
Item::appendChild(ref);
}
ax = -1;
bx = -1;
}
}
void Models::Reference::onChildIsAboutToBeRemoved(Models::Item* parent, int first, int last)
{
if (parent == original) {
for (int i = first; i <= last; ++i) {
Reference* ref = static_cast<Reference*>(childItems[i]);
Item* orig = original->child(i);
orig->removeReference(ref);
Item::removeChild(i);
delete ref;
}
}
}
void Models::Reference::onChildRemoved()
{
}
void Models::Reference::onChildIsAboutToBeMoved(Models::Item* source, int first, int last, Models::Item* destination, int newIndex)
{
if (destination == original) {
if (source != original) {
c = true;
ax = first;
bx = last;
cx = newIndex;
emit childIsAboutToBeMoved(source, first, last, destination, newIndex);
} else {
ax = newIndex;
bx = newIndex + last - first + 1;
}
}
}
void Models::Reference::onChildMoved()
{
if (c) {
std::deque<Item*>::const_iterator beg = childItems.begin() + ax;
std::deque<Item*>::const_iterator end = childItems.begin() + bx + 1;
std::deque<Item*>::const_iterator tgt = childItems.begin() + cx;
std::deque<Item*> temp;
temp.insert(temp.end(), beg, end);
childItems.erase(beg, end);
childItems.insert(tgt, temp.begin(), temp.end());
emit childMoved();
} else {
onChildInserted();
}
}

View File

@ -30,7 +30,7 @@ class Reference : public Models::Item
{ {
Q_OBJECT Q_OBJECT
public: public:
Reference(Models::Item* original, Models::Item* parent); Reference(Models::Item* original, Models::Item* parent = 0);
~Reference(); ~Reference();
int columnCount() const override; int columnCount() const override;
@ -44,9 +44,21 @@ public:
protected slots: protected slots:
void toOfflineState() override; void toOfflineState() override;
private slots:
void onChildChanged(Models::Item* item, int row, int col);
void onChildIsAboutToBeInserted(Item* parent, int first, int last);
void onChildInserted();
void onChildIsAboutToBeRemoved(Item* parent, int first, int last);
void onChildRemoved();
void onChildIsAboutToBeMoved(Item* source, int first, int last, Item* destination, int newIndex);
void onChildMoved();
private: private:
Models::Item* original; Models::Item* original;
int ax;
int bx;
int cx;
bool c;
}; };
} }

View File

@ -62,6 +62,9 @@ QVariant Models::Roster::data (const QModelIndex& index, int role) const
QVariant result; QVariant result;
Item *item = static_cast<Item*>(index.internalPointer()); Item *item = static_cast<Item*>(index.internalPointer());
if (item->type == Item::reference) {
item = static_cast<Reference*>(item)->dereference();
}
switch (role) { switch (role) {
case Qt::DisplayRole: case Qt::DisplayRole:
{ {
@ -430,7 +433,8 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons
{ {
Item* parent; Item* parent;
Account* acc; Account* acc;
Contact* sample = 0; Contact* contact;
Reference* ref = 0;
ElId id(account, jid); ElId id(account, jid);
{ {
@ -442,13 +446,18 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons
acc = itr->second; acc = itr->second;
} }
for (std::multimap<ElId, Contact*>::iterator itr = contacts.lower_bound(id), eItr = contacts.upper_bound(id); itr != eItr; ++itr) { {
sample = itr->second; //need to find if this contact is already added somewhere std::map<ElId, Contact*>::iterator itr = contacts.find(id);
break; //so one iteration is enough if (itr == contacts.end()) {
contact = new Contact(acc, jid, data);
contacts.insert(std::make_pair(id, contact));
} else {
contact = itr->second;
}
} }
if (group == "") { //this means this contact is already added somewhere and there is no sense to add it ungrouped if (group == "") {
if (sample != 0) { 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"; qDebug() << "An attempt to add a contact" << jid << "to the ungrouped contact set of account" << account << "for the second time, skipping";
return; return;
} else { } else {
@ -464,41 +473,26 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons
parent = itr->second; parent = itr->second;
for (int i = 0; i < parent->childCount(); ++i) { //checking if the contact is already added to that group if (parent->getContact(jid) != -1) {
Item* item = parent->child(i); qDebug() << "An attempt to add a contact" << jid << "to the group" << group << "for the second time, skipping";
if (item->type == Item::contact) { return;
Contact* ca = static_cast<Contact*>(item);
if (ca->getJid() == jid) {
qDebug() << "An attempt to add a contact" << jid << "to the group" << group << "for the second time, skipping";
return;
}
}
} }
for (int i = 0; i < acc->childCount(); ++i) { //checking if that contact is among ugrouped int refIndex = acc->getContact(jid);
Item* item = acc->child(i); if (refIndex != -1) { //checking if that contact is among ugrouped
if (item->type == Item::contact) { qDebug() << "An attempt to add a already existing contact " << jid
Contact* ca = static_cast<Contact*>(item); << " to the group " << group
if (ca->getJid() == jid) { << ", contact will be moved from ungrouped contacts of " << account;
qDebug() << "An attempt to add a already existing contact " << jid ref = static_cast<Reference*>(acc->child(refIndex));
<< " to the group " << group acc->removeChild(refIndex);
<< ", contact will be moved from ungrouped contacts of " << account;
parent->appendChild(ca);
return;
}
}
} }
} }
Contact* contact;
if (sample == 0) { if (ref == 0) {
contact = new Contact(jid, data); ref = new Reference(contact);
} else {
contact = sample->copy();
} }
contacts.insert(std::make_pair(id, contact)); parent->appendChild(ref);
parent->appendChild(contact);
} }
void Models::Roster::removeGroup(const QString& account, const QString& name) void Models::Roster::removeGroup(const QString& account, const QString& name)
@ -515,33 +509,24 @@ void Models::Roster::removeGroup(const QString& account, const QString& name)
parent->removeChild(row); parent->removeChild(row);
std::deque<Contact*> toInsert; std::deque<Reference*> toInsert;
for (int i = 0; item->childCount() > 0; ++i) { for (int i = 0; item->childCount() > 0; ++i) {
Contact* cont = static_cast<Contact*>(item->child(0)); Reference* ref = static_cast<Reference*>(item->child(0));
item->removeChild(0); item->removeChild(0);
std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound({account, cont->getJid()}); Contact* cont = static_cast<Contact*>(ref->dereference());
std::multimap<ElId, Contact*>::iterator cEnd = contacts.upper_bound({account, cont->getJid()}); if (cont->referencesCount() == 1) {
toInsert.push_back(ref);
int count = std::distance(cBeg, cEnd);
if (count > 1) {
for (; cBeg != cEnd; ++count, ++cBeg) {
if (cBeg->second == cont) {
delete cont;
contacts.erase(cBeg);
break;
}
}
} else { } else {
toInsert.push_back(cont); cont->removeReference(ref);
delete ref;
} }
} }
if (toInsert.size() > 0) { if (toInsert.size() > 0) {
Account* acc = accounts.find("account")->second; Account* acc = accounts.find("account")->second;
for (std::deque<Contact*>::size_type i = 0; i < toInsert.size(); ++i) { for (std::deque<Contact*>::size_type i = 0; i < toInsert.size(); ++i) {
Contact* cont = toInsert[i]; acc->appendChild(toInsert[i]); //TODO optimisation
acc->appendChild(cont); //TODO optimisation
} }
} }
@ -552,19 +537,18 @@ 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) void Models::Roster::changeContact(const QString& account, const QString& jid, const QMap<QString, QVariant>& data)
{ {
ElId id(account, jid); ElId id(account, jid);
std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound(id); std::map<ElId, Contact*>::iterator cItr = contacts.find(id);
std::multimap<ElId, Contact*>::iterator cEnd = contacts.upper_bound(id);
for (; cBeg != cEnd; ++cBeg) { if (cItr != contacts.end()) {
for (QMap<QString, QVariant>::const_iterator itr = data.begin(), end = data.end(); itr != end; ++itr) { for (QMap<QString, QVariant>::const_iterator itr = data.begin(), end = data.end(); itr != end; ++itr) {
cBeg->second->update(itr.key(), itr.value()); cItr->second->update(itr.key(), itr.value());
} }
} } else {
std::map<ElId, Room*>::iterator rItr = rooms.find(id);
std::map<ElId, Room*>::iterator rItr = rooms.find(id); if (rItr != rooms.end()) {
if (rItr != rooms.end()) { for (QMap<QString, QVariant>::const_iterator itr = data.begin(), end = data.end(); itr != end; ++itr) {
for (QMap<QString, QVariant>::const_iterator itr = data.begin(), end = data.end(); itr != end; ++itr) { rItr->second->update(itr.key(), itr.value());
rItr->second->update(itr.key(), itr.value()); }
} }
} }
} }
@ -572,18 +556,13 @@ 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) void Models::Roster::changeMessage(const QString& account, const QString& jid, const QString& id, const QMap<QString, QVariant>& data)
{ {
ElId elid(account, jid); ElId elid(account, jid);
std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound(elid); std::map<ElId, Contact*>::iterator cItr = contacts.find(elid);
std::multimap<ElId, Contact*>::iterator cEnd = contacts.upper_bound(elid);
for (; cBeg != cEnd; ++cBeg) { if (cItr != contacts.end()) {
for (QMap<QString, QVariant>::const_iterator itr = data.begin(), end = data.end(); itr != end; ++itr) { cItr->second->changeMessage(id, data);
cBeg->second->changeMessage(id, data); } else {
} std::map<ElId, Room*>::iterator rItr = rooms.find(elid);
} if (rItr != rooms.end()) {
std::map<ElId, Room*>::iterator rItr = rooms.find(elid);
if (rItr != rooms.end()) {
for (QMap<QString, QVariant>::const_iterator itr = data.begin(), end = data.end(); itr != end; ++itr) {
rItr->second->changeMessage(id, data); rItr->second->changeMessage(id, data);
} }
} }
@ -592,25 +571,26 @@ void Models::Roster::changeMessage(const QString& account, const QString& jid, c
void Models::Roster::removeContact(const QString& account, const QString& jid) void Models::Roster::removeContact(const QString& account, const QString& jid)
{ {
ElId id(account, jid); ElId id(account, jid);
std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound(id); std::map<ElId, Contact*>::iterator itr = contacts.find(id);
std::multimap<ElId, Contact*>::iterator cEnd = contacts.upper_bound(id);
std::multimap<ElId, Contact*>::iterator cpBeg = cBeg;
QSet<QString> toRemove; if (itr != contacts.end()) {
for (; cBeg != cEnd; ++cBeg) { Contact* contact = itr->second;
Contact* contact = cBeg->second;
Item* parent = contact->parentItem(); contacts.erase(itr);
if (parent->type == Item::group && parent->childCount() == 1) { delete contact;
toRemove.insert(parent->getName());
std::set<ElId> toRemove;
for (std::pair<ElId, Group*> pair : groups) {
if (pair.second->childCount() == 0) {
toRemove.insert(pair.first);
}
} }
parent->removeChild(contact->row()); for (const ElId& elId : toRemove) {
contact->deleteLater(); removeGroup(elId.account, elId.name);
} }
contacts.erase(cpBeg, cEnd); } else {
qDebug() << "An attempt to remove contact " << jid << " from account " << account <<" which doesn't exist there, skipping";
for (QSet<QString>::const_iterator itr = toRemove.begin(), end = toRemove.end(); itr != end; ++itr) {
removeGroup(account, *itr);
} }
} }
@ -619,6 +599,12 @@ void Models::Roster::removeContact(const QString& account, const QString& jid, c
ElId contactId(account, jid); ElId contactId(account, jid);
ElId groupId(account, group); ElId groupId(account, group);
std::map<ElId, Contact*>::iterator cItr = contacts.find(contactId);
if (cItr == contacts.end()) {
qDebug() << "An attempt to remove non existing contact " << jid << " from group " << group << " of account " << account <<", skipping";
return;
}
std::map<ElId, Group*>::iterator gItr = groups.find(groupId); std::map<ElId, Group*>::iterator gItr = groups.find(groupId);
if (gItr == groups.end()) { if (gItr == groups.end()) {
qDebug() << "An attempt to remove contact " << jid << " from non existing group " << group << " of account " << account <<", skipping"; qDebug() << "An attempt to remove contact " << jid << " from non existing group " << group << " of account " << account <<", skipping";
@ -626,57 +612,26 @@ void Models::Roster::removeContact(const QString& account, const QString& jid, c
} }
Account* acc = accounts.find(account)->second; //I assume the account is found, otherwise there will be no groups with that ElId; Account* acc = accounts.find(account)->second; //I assume the account is found, otherwise there will be no groups with that ElId;
Group* gr = gItr->second; Group* gr = gItr->second;
Contact* cont = 0; Contact* cont = cItr->second;
unsigned int entries(0); int contRow = gr->getContact(jid);
unsigned int ungroupped(0); if (contRow == -1) {
for (std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound(contactId), cEnd = contacts.upper_bound(contactId); cBeg != cEnd; ++cBeg) { qDebug() << "An attempt to remove contact " << jid << " of account " << account << " from group " << group <<", but there is no such contact in that group, skipping";
++entries; return;
Contact* elem = cBeg->second; }
if (elem->parentItem() == acc) { Reference* ref = static_cast<Reference*>(gr->child(contRow));
++ungroupped; gr->removeChild(contRow);
}
if (cont->referencesCount() == 1) {
qDebug() << "An attempt to remove last instance of contact" << jid << "from the group" << group << ", contact will be moved to ungrouped contacts of" << account;
acc->appendChild(ref);
} else {
cont->removeReference(ref);
delete ref;
} }
if (ungroupped == 0 && entries == 1) { if (gr->childCount() == 0) {
for (std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound(contactId), cEnd = contacts.upper_bound(contactId); cBeg != cEnd; ++cBeg) { removeGroup(account, group);
if (cBeg->second->parentItem() == gr) {
cont = cBeg->second;
break;
}
}
if (cont == 0) {
qDebug() << "An attempt to remove contact " << jid << " of account " << account << " from group " << group <<", but there is no such contact in that group, skipping";
return;
}
qDebug() << "An attempt to remove last instance of contact" << jid << "from the group" << group << ", contact will be moved to ungrouped contacts of" << account;
acc->appendChild(cont);
if (gr->childCount() == 0) {
removeGroup(account, group);
}
} else {
for (std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound(contactId), cEnd = contacts.upper_bound(contactId); cBeg != cEnd; ++cBeg) {
if (cBeg->second->parentItem() == gr) {
cont = cBeg->second;
contacts.erase(cBeg);
break;
}
}
if (cont == 0) {
qDebug() << "An attempt to remove contact" << jid << "of account" << account << "from group" << group <<", but there is no such contact in that group, skipping";
return;
}
gr->removeChild(cont->row());
cont->deleteLater();
if (gr->childCount() == 0) {
removeGroup(account, group);
}
} }
} }
@ -738,50 +693,46 @@ void Models::Roster::onChildRemoved()
void Models::Roster::addPresence(const QString& account, const QString& jid, const QString& name, const QMap<QString, QVariant>& data) void Models::Roster::addPresence(const QString& account, const QString& jid, const QString& name, const QMap<QString, QVariant>& data)
{ {
ElId contactId(account, jid); ElId contactId(account, jid);
std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound(contactId); std::map<ElId, Contact*>::iterator itr = contacts.find(contactId);
std::multimap<ElId, Contact*>::iterator cEnd = contacts.upper_bound(contactId); if (itr != contacts.end()) {
for (;cBeg != cEnd; ++cBeg) { itr->second->addPresence(name, data);
cBeg->second->addPresence(name, data);
} }
} }
void Models::Roster::removePresence(const QString& account, const QString& jid, const QString& name) void Models::Roster::removePresence(const QString& account, const QString& jid, const QString& name)
{ {
ElId contactId(account, jid); ElId contactId(account, jid);
std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound(contactId); std::map<ElId, Contact*>::iterator itr = contacts.find(contactId);
std::multimap<ElId, Contact*>::iterator cEnd = contacts.upper_bound(contactId); if (itr != contacts.end()) {
for (;cBeg != cEnd; ++cBeg) { itr->second->removePresence(name);
cBeg->second->removePresence(name);
} }
} }
void Models::Roster::addMessage(const QString& account, const Shared::Message& data) void Models::Roster::addMessage(const QString& account, const Shared::Message& data)
{ {
ElId id(account, data.getPenPalJid()); ElId id(account, data.getPenPalJid());
std::map<ElId, Contact*>::iterator itr = contacts.find(id);
std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound(id); if (itr != contacts.end()) {
std::multimap<ElId, Contact*>::iterator cEnd = contacts.upper_bound(id); itr->second->addMessage(data);
} else {
for (;cBeg != cEnd; ++cBeg) { std::map<ElId, Room*>::const_iterator rItr = rooms.find(id);
cBeg->second->addMessage(data); if (rItr != rooms.end()) {
} rItr->second->addMessage(data);
}
std::map<ElId, Room*>::const_iterator rItr = rooms.find(id);
if (rItr != rooms.end()) {
rItr->second->addMessage(data);
} }
} }
void Models::Roster::dropMessages(const QString& account, const QString& jid) void Models::Roster::dropMessages(const QString& account, const QString& jid)
{ {
ElId id(account, jid); ElId id(account, jid);
for (std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound(id), cEnd = contacts.upper_bound(id) ;cBeg != cEnd; ++cBeg) { std::map<ElId, Contact*>::iterator itr = contacts.find(id);
cBeg->second->dropMessages(); if (itr != contacts.end()) {
} itr->second->dropMessages();
} else {
std::map<ElId, Room*>::const_iterator rItr = rooms.find(id); std::map<ElId, Room*>::const_iterator rItr = rooms.find(id);
if (rItr != rooms.end()) { if (rItr != rooms.end()) {
rItr->second->dropMessages(); rItr->second->dropMessages();
}
} }
} }
@ -799,10 +750,10 @@ void Models::Roster::removeAccount(const QString& account)
accountsModel->removeAccount(index); accountsModel->removeAccount(index);
accounts.erase(itr); accounts.erase(itr);
std::multimap<ElId, Contact*>::const_iterator cItr = contacts.begin(); std::map<ElId, Contact*>::const_iterator cItr = contacts.begin();
while (cItr != contacts.end()) { while (cItr != contacts.end()) {
if (cItr->first.account == account) { if (cItr->first.account == account) {
std::multimap<ElId, Contact*>::const_iterator lItr = cItr; std::map<ElId, Contact*>::const_iterator lItr = cItr;
++cItr; ++cItr;
contacts.erase(lItr); contacts.erase(lItr);
} else { } else {
@ -838,7 +789,7 @@ void Models::Roster::removeAccount(const QString& account)
QString Models::Roster::getContactName(const QString& account, const QString& jid) QString Models::Roster::getContactName(const QString& account, const QString& jid)
{ {
ElId id(account, jid); ElId id(account, jid);
std::multimap<ElId, Contact*>::const_iterator cItr = contacts.find(id); std::map<ElId, Contact*>::const_iterator cItr = contacts.find(id);
QString name = ""; QString name = "";
if (cItr == contacts.end()) { if (cItr == contacts.end()) {
std::map<ElId, Room*>::const_iterator rItr = rooms.find(id); std::map<ElId, Room*>::const_iterator rItr = rooms.find(id);
@ -971,15 +922,14 @@ bool Models::Roster::groupHasContact(const QString& account, const QString& grou
if (gItr == groups.end()) { if (gItr == groups.end()) {
return false; return false;
} else { } else {
const Group* gr = gItr->second; return gItr->second->getContact(contact) != -1;
return gr->hasContact(contact);
} }
} }
QString Models::Roster::getContactIconPath(const QString& account, const QString& jid, const QString& resource) QString Models::Roster::getContactIconPath(const QString& account, const QString& jid, const QString& resource)
{ {
ElId id(account, jid); ElId id(account, jid);
std::multimap<ElId, Contact*>::const_iterator cItr = contacts.find(id); std::map<ElId, Contact*>::const_iterator cItr = contacts.find(id);
QString path = ""; QString path = "";
if (cItr == contacts.end()) { if (cItr == contacts.end()) {
std::map<ElId, Room*>::const_iterator rItr = rooms.find(id); std::map<ElId, Room*>::const_iterator rItr = rooms.find(id);

View File

@ -32,6 +32,7 @@
#include "contact.h" #include "contact.h"
#include "group.h" #include "group.h"
#include "room.h" #include "room.h"
#include "reference.h"
namespace Models namespace Models
{ {
@ -87,7 +88,7 @@ private:
Item* root; Item* root;
std::map<QString, Account*> accounts; std::map<QString, Account*> accounts;
std::map<ElId, Group*> groups; std::map<ElId, Group*> groups;
std::multimap<ElId, Contact*> contacts; std::map<ElId, Contact*> contacts;
std::map<ElId, Room*> rooms; std::map<ElId, Room*> rooms;
private slots: private slots:

View File

@ -305,6 +305,9 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item)
{ {
if (item.isValid()) { if (item.isValid()) {
Models::Item* node = static_cast<Models::Item*>(item.internalPointer()); Models::Item* node = static_cast<Models::Item*>(item.internalPointer());
if (node->type == Models::Item::reference) {
node = static_cast<Models::Reference*>(node)->dereference();
}
Models::Contact* contact = 0; Models::Contact* contact = 0;
Models::Room* room = 0; Models::Room* room = 0;
QString res; QString res;
@ -681,7 +684,9 @@ void Squawk::onRosterContextMenu(const QPoint& point)
QModelIndex index = m_ui->roster->indexAt(point); QModelIndex index = m_ui->roster->indexAt(point);
if (index.isValid()) { if (index.isValid()) {
Models::Item* item = static_cast<Models::Item*>(index.internalPointer()); Models::Item* item = static_cast<Models::Item*>(index.internalPointer());
if (item->type == Models::Item::reference) {
item = static_cast<Models::Reference*>(item)->dereference();
}
contextMenu->clear(); contextMenu->clear();
bool hasMenu = false; bool hasMenu = false;
bool active = item->getAccountConnectionState() == Shared::ConnectionState::connected; bool active = item->getAccountConnectionState() == Shared::ConnectionState::connected;
@ -1105,6 +1110,9 @@ void Squawk::onRosterSelectionChanged(const QModelIndex& current, const QModelIn
if (current.isValid()) { if (current.isValid()) {
Models::Item* node = static_cast<Models::Item*>(current.internalPointer()); Models::Item* node = static_cast<Models::Item*>(current.internalPointer());
if (node->type == Models::Item::reference) {
node = static_cast<Models::Reference*>(node)->dereference();
}
Models::Contact* contact = 0; Models::Contact* contact = 0;
Models::Room* room = 0; Models::Room* room = 0;
QString res; QString res;