2019-03-31 21:05:09 +00:00
|
|
|
#include "roster.h"
|
2019-04-03 21:23:51 +00:00
|
|
|
#include <QDebug>
|
2019-04-02 21:58:43 +00:00
|
|
|
#include <QIcon>
|
2019-03-31 21:05:09 +00:00
|
|
|
|
|
|
|
using namespace Models;
|
|
|
|
|
|
|
|
Models::Roster::Roster(QObject* parent):
|
|
|
|
QAbstractItemModel(parent),
|
2019-04-03 18:15:36 +00:00
|
|
|
accountsModel(new Accounts()),
|
|
|
|
root(new Item(Item::root, {{"name", "root"}})),
|
|
|
|
accounts(),
|
2019-04-03 21:23:51 +00:00
|
|
|
groups(),
|
|
|
|
contacts(),
|
2019-04-03 18:15:36 +00:00
|
|
|
elements()
|
2019-03-31 21:05:09 +00:00
|
|
|
{
|
2019-04-03 18:15:36 +00:00
|
|
|
connect(accountsModel,
|
|
|
|
SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&, const QVector<int>&)),
|
|
|
|
this,
|
|
|
|
SLOT(onAccountDataChanged(const QModelIndex&, const QModelIndex&, const QVector<int>&)));
|
2019-03-31 21:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Models::Roster::~Roster()
|
|
|
|
{
|
2019-04-03 18:15:36 +00:00
|
|
|
delete accountsModel;
|
2019-03-31 21:05:09 +00:00
|
|
|
delete root;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Roster::addAccount(const QMap<QString, QVariant>& data)
|
|
|
|
{
|
2019-04-02 21:58:43 +00:00
|
|
|
Account* acc = new Account(data, root);
|
2019-03-31 21:05:09 +00:00
|
|
|
beginInsertRows(QModelIndex(), root->childCount(), root->childCount());
|
|
|
|
root->appendChild(acc);
|
2019-04-03 15:09:29 +00:00
|
|
|
accounts.insert(std::make_pair(acc->getName(), acc));
|
2019-04-03 18:15:36 +00:00
|
|
|
accountsModel->addAccount(acc);
|
2019-03-31 21:05:09 +00:00
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant Models::Roster::data (const QModelIndex& index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid()) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant result;
|
|
|
|
|
2019-04-02 21:58:43 +00:00
|
|
|
Item *item = static_cast<Item*>(index.internalPointer());
|
2019-03-31 21:05:09 +00:00
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
{
|
|
|
|
result = item->data(index.column());
|
|
|
|
}
|
|
|
|
break;
|
2019-04-02 21:58:43 +00:00
|
|
|
case Qt::DecorationRole:
|
|
|
|
switch (item->type) {
|
|
|
|
case Item::account:{
|
2019-04-03 18:15:36 +00:00
|
|
|
Account* acc = static_cast<Account*>(item);
|
|
|
|
int state = acc->getState();
|
2019-04-02 21:58:43 +00:00
|
|
|
switch (state) {
|
|
|
|
case Shared::disconnected:
|
|
|
|
result = QIcon::fromTheme("im-user-offline");
|
|
|
|
break;
|
|
|
|
case Shared::connecting:
|
|
|
|
result = QIcon::fromTheme(Shared::ConnectionStateThemeIcons[state]);
|
|
|
|
break;
|
|
|
|
case Shared::connected:
|
|
|
|
result = QIcon::fromTheme("im-user-online");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2019-03-31 21:05:09 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Models::Roster::columnCount (const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
if (parent.isValid()) {
|
|
|
|
return static_cast<Item*>(parent.internalPointer())->columnCount();
|
|
|
|
} else {
|
|
|
|
return root->columnCount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 21:58:43 +00:00
|
|
|
void Models::Roster::updateAccount(const QString& account, const QString& field, const QVariant& value)
|
|
|
|
{
|
|
|
|
std::map<QString, Account*>::iterator itr = accounts.find(account);
|
|
|
|
if (itr != accounts.end()) {
|
|
|
|
Account* acc = itr->second;
|
2019-04-03 18:15:36 +00:00
|
|
|
acc->update(field, value);
|
2019-04-02 21:58:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-31 21:05:09 +00:00
|
|
|
Qt::ItemFlags Models::Roster::flags(const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
if (!index.isValid()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QAbstractItemModel::flags(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Models::Roster::rowCount (const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
Item *parentItem;
|
|
|
|
if (parent.column() > 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parent.isValid()) {
|
|
|
|
parentItem = root;
|
|
|
|
} else {
|
|
|
|
parentItem = static_cast<Item*>(parent.internalPointer());
|
|
|
|
}
|
|
|
|
|
|
|
|
return parentItem->childCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant Models::Roster::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex Models::Roster::parent (const QModelIndex& child) const
|
|
|
|
{
|
|
|
|
if (!child.isValid()) {
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
Item *childItem = static_cast<Item*>(child.internalPointer());
|
2019-04-02 21:58:43 +00:00
|
|
|
if (childItem == root) {
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
2019-03-31 21:05:09 +00:00
|
|
|
Item *parentItem = childItem->parentItem();
|
|
|
|
|
|
|
|
if (parentItem == root) {
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
return createIndex(parentItem->row(), 0, parentItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex Models::Roster::index (int row, int column, const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
if (!hasIndex(row, column, parent)) {
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
Item *parentItem;
|
|
|
|
|
|
|
|
if (!parent.isValid()) {
|
|
|
|
parentItem = root;
|
|
|
|
} else {
|
|
|
|
parentItem = static_cast<Item*>(parent.internalPointer());
|
|
|
|
}
|
|
|
|
|
|
|
|
Item *childItem = parentItem->child(row);
|
|
|
|
if (childItem) {
|
|
|
|
return createIndex(row, column, childItem);
|
|
|
|
} else {
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Models::Roster::ElId::ElId(const QString& p_account, const QString& p_name):
|
|
|
|
account(p_account),
|
|
|
|
name(p_name)
|
2019-04-03 18:15:36 +00:00
|
|
|
{}
|
2019-03-31 21:05:09 +00:00
|
|
|
|
|
|
|
bool Models::Roster::ElId::operator <(const Models::Roster::ElId& other) const
|
|
|
|
{
|
|
|
|
if (account == other.account) {
|
|
|
|
return name < other.name;
|
|
|
|
} else {
|
|
|
|
return account < other.account;
|
|
|
|
}
|
|
|
|
}
|
2019-04-02 21:58:43 +00:00
|
|
|
|
2019-04-03 18:15:36 +00:00
|
|
|
void Models::Roster::onAccountDataChanged(const QModelIndex& tl, const QModelIndex& br, const QVector<int>& roles)
|
|
|
|
{
|
|
|
|
if (tl.column() == 0) {
|
|
|
|
emit dataChanged(tl, br, roles);
|
|
|
|
} else if (tl.column() == 2) {
|
|
|
|
int row = tl.row();
|
|
|
|
Account* acc = accountsModel->getAccount(row);
|
|
|
|
emit dataChanged(createIndex(row, 0, acc), createIndex(br.row(), 0, acc), roles);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 21:23:51 +00:00
|
|
|
void Models::Roster::addGroup(const QString& account, const QString& name)
|
|
|
|
{
|
|
|
|
std::map<QString, Account*>::iterator itr = accounts.find(account);
|
|
|
|
if (itr != accounts.end()) {
|
|
|
|
Account* acc = itr->second;
|
|
|
|
Item* group = new Item(Item::group, {{"name", name}}, acc);
|
|
|
|
beginInsertRows(createIndex(acc->row(), 0, acc), acc->childCount(), acc->childCount());
|
|
|
|
acc->appendChild(group);
|
|
|
|
groups.insert(std::make_pair(name, group));
|
|
|
|
elements.insert({{account, name}, group});
|
|
|
|
endInsertRows();
|
|
|
|
} else {
|
|
|
|
qDebug() << "An attempt to add group " << name << " to non existing account " << account << ", skipping";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Roster::addContact(const QString& account, const QString& jid, const QString& name, const QString& group)
|
|
|
|
{
|
|
|
|
Item* parent;
|
|
|
|
if (group == "") {
|
|
|
|
std::map<QString, Account*>::iterator itr = accounts.find(account);
|
|
|
|
if (itr == accounts.end()) {
|
|
|
|
qDebug() << "An attempt to add a contact " << name << " to non existing account " << account << ", skipping";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
parent = itr->second;
|
|
|
|
} else {
|
|
|
|
std::map<QString, Item*>::iterator itr = groups.find(group);
|
|
|
|
if (itr == groups.end()) {
|
|
|
|
qDebug() << "An attempt to add a contact " << name << " to non existing group " << group << ", skipping";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
parent = itr->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString sName = name;
|
|
|
|
if (sName == "") {
|
|
|
|
sName = jid;
|
|
|
|
}
|
|
|
|
Item* contact = new Item(Item::contact, {{"name", sName}}, parent);
|
|
|
|
beginInsertRows(createIndex(parent->row(), 0, parent), parent->childCount(), parent->childCount());
|
|
|
|
parent->appendChild(contact);
|
|
|
|
contacts.insert(std::make_pair(jid, contact));
|
|
|
|
elements.insert({{account, jid}, contact});
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Roster::removeGroup(const QString& account, const QString& name)
|
|
|
|
{
|
|
|
|
}
|
2019-04-02 21:58:43 +00:00
|
|
|
|