Disabled context menu on items of not connected account, roster contacts group moving, bugfixes with roster contacts group moving ungrouping and copying
This commit is contained in:
parent
e4d1e21ea0
commit
415d56ba69
20 changed files with 314 additions and 36 deletions
|
@ -32,6 +32,15 @@ Models::AbstractParticipant::AbstractParticipant(Models::Item::Type p_type, cons
|
|||
}
|
||||
}
|
||||
|
||||
Models::AbstractParticipant::AbstractParticipant(const Models::AbstractParticipant& other):
|
||||
Item(other),
|
||||
availability(other.availability),
|
||||
lastActivity(other.lastActivity),
|
||||
status(other.status)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Models::AbstractParticipant::~AbstractParticipant()
|
||||
{
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ class AbstractParticipant : public Models::Item
|
|||
Q_OBJECT
|
||||
public:
|
||||
explicit AbstractParticipant(Type p_type, const QMap<QString, QVariant> &data, Item *parentItem = 0);
|
||||
AbstractParticipant(const AbstractParticipant& other);
|
||||
~AbstractParticipant();
|
||||
|
||||
virtual int columnCount() const override;
|
||||
|
|
|
@ -323,3 +323,28 @@ bool Models::Contact::columnInvolvedInDisplay(int col)
|
|||
{
|
||||
return Item::columnInvolvedInDisplay(col) && col == 1;
|
||||
}
|
||||
|
||||
Models::Contact * Models::Contact::copy() const
|
||||
{
|
||||
Contact* cnt = new Contact(*this);
|
||||
return cnt;
|
||||
}
|
||||
|
||||
Models::Contact::Contact(const Models::Contact& other):
|
||||
Item(other),
|
||||
jid(other.jid),
|
||||
availability(other.availability),
|
||||
state(other.state),
|
||||
presences(),
|
||||
messages(other.messages),
|
||||
childMessages(0)
|
||||
{
|
||||
for (const Presence* pres : other.presences) {
|
||||
Presence* pCopy = new Presence(*pres);
|
||||
presences.insert(pCopy->getName(), pCopy);
|
||||
Item::appendChild(pCopy);
|
||||
connect(pCopy, SIGNAL(childChanged(Models::Item*, int, int)), this, SLOT(refresh()));
|
||||
}
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ class Contact : public Item
|
|||
public:
|
||||
typedef std::deque<Shared::Message> Messages;
|
||||
Contact(const QString& p_jid, const QMap<QString, QVariant> &data, Item *parentItem = 0);
|
||||
Contact(const Contact& other);
|
||||
~Contact();
|
||||
|
||||
QString getJid() const;
|
||||
|
@ -59,6 +60,8 @@ public:
|
|||
void getMessages(Messages& container) const;
|
||||
QString getDisplayedName() const override;
|
||||
|
||||
Contact* copy() const;
|
||||
|
||||
protected:
|
||||
void _removeChild(int index) override;
|
||||
bool columnInvolvedInDisplay(int col) override;
|
||||
|
|
|
@ -103,3 +103,16 @@ unsigned int Models::Group::getOnlineContacts() const
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -36,6 +36,8 @@ public:
|
|||
|
||||
unsigned int getUnreadMessages() const;
|
||||
unsigned int getOnlineContacts() const;
|
||||
|
||||
bool hasContact(const QString& jid) const;
|
||||
|
||||
protected:
|
||||
void _removeChild(int index) override;
|
||||
|
|
|
@ -34,6 +34,15 @@ Models::Item::Item(Type p_type, const QMap<QString, QVariant> &p_data, Item *p_p
|
|||
}
|
||||
}
|
||||
|
||||
Models::Item::Item(const Models::Item& other):
|
||||
QObject(),
|
||||
type(other.type),
|
||||
name(other.name),
|
||||
childItems(),
|
||||
parent(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
Models::Item::~Item()
|
||||
{
|
||||
std::deque<Item*>::const_iterator itr = childItems.begin();
|
||||
|
@ -225,6 +234,24 @@ QString Models::Item::getAccountName() const
|
|||
return acc->getName();
|
||||
}
|
||||
|
||||
Shared::Availability Models::Item::getAccountAvailability() const
|
||||
{
|
||||
const Account* acc = static_cast<const Account*>(getParentAccount());
|
||||
if (acc == 0) {
|
||||
return Shared::offline;
|
||||
}
|
||||
return acc->getAvailability();
|
||||
}
|
||||
|
||||
Shared::ConnectionState Models::Item::getAccountConnectionState() const
|
||||
{
|
||||
const Account* acc = static_cast<const Account*>(getParentAccount());
|
||||
if (acc == 0) {
|
||||
return Shared::disconnected;
|
||||
}
|
||||
return acc->getState();
|
||||
}
|
||||
|
||||
QString Models::Item::getDisplayedName() const
|
||||
{
|
||||
return name;
|
||||
|
|
|
@ -25,6 +25,8 @@
|
|||
|
||||
#include <deque>
|
||||
|
||||
#include "../../global.h"
|
||||
|
||||
namespace Models {
|
||||
|
||||
class Item : public QObject{
|
||||
|
@ -41,6 +43,7 @@ class Item : public QObject{
|
|||
};
|
||||
|
||||
explicit Item(Type p_type, const QMap<QString, QVariant> &data, Item *parentItem = 0);
|
||||
Item(const Item& other);
|
||||
~Item();
|
||||
|
||||
signals:
|
||||
|
@ -71,6 +74,8 @@ class Item : public QObject{
|
|||
QString getAccountName() const;
|
||||
QString getAccountJid() const;
|
||||
QString getAccountResource() const;
|
||||
Shared::ConnectionState getAccountConnectionState() const;
|
||||
Shared::Availability getAccountAvailability() const;
|
||||
|
||||
const Type type;
|
||||
|
||||
|
|
|
@ -24,6 +24,13 @@ Models::Presence::Presence(const QMap<QString, QVariant>& data, Item* parentItem
|
|||
{
|
||||
}
|
||||
|
||||
Models::Presence::Presence(const Models::Presence& other):
|
||||
AbstractParticipant(other),
|
||||
messages(other.messages)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Models::Presence::~Presence()
|
||||
{
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ class Presence : public Models::AbstractParticipant
|
|||
public:
|
||||
typedef std::deque<Shared::Message> Messages;
|
||||
explicit Presence(const QMap<QString, QVariant> &data, Item *parentItem = 0);
|
||||
Presence(const Presence& other);
|
||||
~Presence();
|
||||
|
||||
int columnCount() const override;
|
||||
|
|
|
@ -383,50 +383,52 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons
|
|||
{
|
||||
Item* parent;
|
||||
Account* acc;
|
||||
Contact* contact;
|
||||
Contact* sample = 0;
|
||||
ElId id(account, jid);
|
||||
|
||||
{
|
||||
std::map<QString, Account*>::iterator itr = accounts.find(account);
|
||||
if (itr == accounts.end()) {
|
||||
qDebug() << "An attempt to add a contact " << jid << " to non existing account " << account << ", skipping";
|
||||
qDebug() << "An attempt to add a contact" << jid << "to non existing account" << account << ", skipping";
|
||||
return;
|
||||
}
|
||||
acc = itr->second;
|
||||
}
|
||||
|
||||
if (group == "") {
|
||||
std::multimap<ElId, Contact*>::iterator itr = contacts.lower_bound(id);
|
||||
std::multimap<ElId, Contact*>::iterator eItr = contacts.upper_bound(id);
|
||||
while (itr != eItr) {
|
||||
if (itr->second->parentItem() == acc) {
|
||||
qDebug() << "An attempt to add a contact " << jid << " ungrouped to non the account " << account << " for the second time, skipping";
|
||||
return;
|
||||
}
|
||||
itr++;
|
||||
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
|
||||
break; //so one iteration is enough
|
||||
}
|
||||
|
||||
if (group == "") { //this means this contact is already added somewhere and there is no sense to add it ungrouped
|
||||
if (sample != 0) {
|
||||
qDebug() << "An attempt to add a contact" << jid << "to the ungrouped contact set of account" << account << "for the second time, skipping";
|
||||
return;
|
||||
} else {
|
||||
parent = acc;
|
||||
}
|
||||
parent = acc;
|
||||
} else {
|
||||
std::map<ElId, Group*>::iterator itr = groups.find({account, group});
|
||||
if (itr == groups.end()) {
|
||||
qDebug() << "An attempt to add a contact " << jid << " to non existing group " << group << ", skipping";
|
||||
return;
|
||||
qDebug() << "An attempt to add a contact" << jid << "to non existing group" << group << ", adding group";
|
||||
addGroup(account, group);
|
||||
itr = groups.find({account, group});
|
||||
}
|
||||
|
||||
parent = itr->second;
|
||||
|
||||
for (int i = 0; i < parent->childCount(); ++i) {
|
||||
for (int i = 0; i < parent->childCount(); ++i) { //checking if the contact is already added to that group
|
||||
Item* item = parent->child(i);
|
||||
if (item->type == Item::contact) {
|
||||
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";
|
||||
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) {
|
||||
for (int i = 0; i < acc->childCount(); ++i) { //checking if that contact is among ugrouped
|
||||
Item* item = acc->child(i);
|
||||
if (item->type == Item::contact) {
|
||||
Contact* ca = static_cast<Contact*>(item);
|
||||
|
@ -440,7 +442,12 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons
|
|||
}
|
||||
|
||||
}
|
||||
contact = new Contact(jid, data);
|
||||
Contact* contact;
|
||||
if (sample == 0) {
|
||||
contact = new Contact(jid, data);
|
||||
} else {
|
||||
contact = sample->copy();
|
||||
}
|
||||
contacts.insert(std::make_pair(id, contact));
|
||||
parent->appendChild(contact);
|
||||
}
|
||||
|
@ -548,29 +555,59 @@ void Models::Roster::removeContact(const QString& account, const QString& jid, c
|
|||
qDebug() << "An attempt to remove contact " << jid << " from non existing group " << group << " of account " << account <<", skipping";
|
||||
return;
|
||||
}
|
||||
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;
|
||||
Contact* cont = 0;
|
||||
|
||||
std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound(contactId);
|
||||
std::multimap<ElId, Contact*>::iterator cEnd = contacts.upper_bound(contactId);
|
||||
for (;cBeg != cEnd; ++cBeg) {
|
||||
if (cBeg->second->parentItem() == gr) {
|
||||
cont = cBeg->second;
|
||||
contacts.erase(cBeg);
|
||||
break;
|
||||
unsigned int entries(0);
|
||||
unsigned int ungroupped(0);
|
||||
for (std::multimap<ElId, Contact*>::iterator cBeg = contacts.lower_bound(contactId), cEnd = contacts.upper_bound(contactId); cBeg != cEnd; ++cBeg) {
|
||||
++entries;
|
||||
Contact* elem = cBeg->second;
|
||||
if (elem->parentItem() == acc) {
|
||||
++ungroupped;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
if (ungroupped == 0 && entries == 1) {
|
||||
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;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -844,3 +881,27 @@ void Models::Roster::removeRoomParticipant(const QString& account, const QString
|
|||
itr->second->removeParticipant(name);
|
||||
}
|
||||
}
|
||||
|
||||
std::deque<QString> Models::Roster::groupList(const QString& account) const
|
||||
{
|
||||
std::deque<QString> answer;
|
||||
for (std::pair<ElId, Group*> pair : groups) {
|
||||
if (pair.first.account == account) {
|
||||
answer.push_back(pair.first.name);
|
||||
}
|
||||
}
|
||||
|
||||
return answer;
|
||||
}
|
||||
|
||||
bool Models::Roster::groupHasContact(const QString& account, const QString& group, const QString& contact) const
|
||||
{
|
||||
ElId grId({account, group});
|
||||
std::map<ElId, Group*>::const_iterator gItr = groups.find(grId);
|
||||
if (gItr == groups.end()) {
|
||||
return false;
|
||||
} else {
|
||||
const Group* gr = gItr->second;
|
||||
return gr->hasContact(contact);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -71,6 +71,9 @@ public:
|
|||
QModelIndex parent ( const QModelIndex& child ) const override;
|
||||
QModelIndex index ( int row, int column, const QModelIndex& parent ) const override;
|
||||
|
||||
std::deque<QString> groupList(const QString& account) const;
|
||||
bool groupHasContact(const QString& account, const QString& group, const QString& contactJID) const;
|
||||
|
||||
Accounts* accountsModel;
|
||||
|
||||
private:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue