2019-09-01 19:46:12 +00:00
|
|
|
/*
|
|
|
|
* Squawk messenger.
|
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-03-30 20:13:13 +00:00
|
|
|
#include "accounts.h"
|
2019-04-02 15:46:18 +00:00
|
|
|
#include "../../global.h"
|
|
|
|
|
|
|
|
#include <QIcon>
|
2019-03-30 20:13:13 +00:00
|
|
|
|
|
|
|
std::deque<QString> Models::Accounts::columns = {
|
|
|
|
"name",
|
|
|
|
"server",
|
2019-05-24 14:46:34 +00:00
|
|
|
"state",
|
|
|
|
"error"
|
2019-03-30 20:13:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Models::Accounts::Accounts(QObject* parent):
|
|
|
|
QAbstractTableModel(parent),
|
|
|
|
accs()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Models::Accounts::~Accounts()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-03 18:15:36 +00:00
|
|
|
QVariant Models::Accounts::data (const QModelIndex& index, int role) const
|
2019-03-30 20:13:13 +00:00
|
|
|
{
|
|
|
|
QVariant answer;
|
|
|
|
switch (role) {
|
2019-04-03 18:15:36 +00:00
|
|
|
case Qt::DisplayRole:
|
|
|
|
answer = accs[index.row()]->data(index.column());
|
2019-03-30 20:13:13 +00:00
|
|
|
break;
|
2019-04-02 15:46:18 +00:00
|
|
|
case Qt::DecorationRole:
|
|
|
|
if (index.column() == 2) {
|
2019-07-01 13:53:01 +00:00
|
|
|
answer = Shared::connectionStateIcon(accs[index.row()]->getState());
|
2019-04-02 15:46:18 +00:00
|
|
|
}
|
|
|
|
break;
|
2019-03-30 20:13:13 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Models::Accounts::columnCount ( const QModelIndex& parent ) const
|
|
|
|
{
|
|
|
|
return columns.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Models::Accounts::rowCount ( const QModelIndex& parent ) const
|
|
|
|
{
|
|
|
|
return accs.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant Models::Accounts::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
|
|
|
|
return columns[section];
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-03 18:15:36 +00:00
|
|
|
void Models::Accounts::addAccount(Account* account)
|
2019-03-30 20:13:13 +00:00
|
|
|
{
|
|
|
|
beginInsertRows(QModelIndex(), accs.size(), accs.size());
|
2019-04-03 18:15:36 +00:00
|
|
|
accs.push_back(account);
|
2019-04-09 15:04:08 +00:00
|
|
|
connect(account, SIGNAL(childChanged(Models::Item*, int, int)), this, SLOT(onAccountChanged(Models::Item*, int, int)));
|
2019-03-30 20:13:13 +00:00
|
|
|
endInsertRows();
|
2019-06-15 15:29:15 +00:00
|
|
|
|
|
|
|
emit sizeChanged(accs.size());
|
2019-03-30 20:13:13 +00:00
|
|
|
}
|
2019-04-02 15:46:18 +00:00
|
|
|
|
2019-04-07 14:02:41 +00:00
|
|
|
void Models::Accounts::onAccountChanged(Item* item, int row, int col)
|
2019-04-02 15:46:18 +00:00
|
|
|
{
|
2019-04-07 14:02:41 +00:00
|
|
|
Account* acc = getAccount(row);
|
|
|
|
if (item != acc) {
|
|
|
|
return; //it means the signal is emitted by one of accounts' children, not exactly him, this model has no interest in that
|
|
|
|
}
|
2019-04-03 18:15:36 +00:00
|
|
|
|
2019-04-07 14:02:41 +00:00
|
|
|
if (col < columnCount(QModelIndex())) {
|
|
|
|
emit dataChanged(createIndex(row, col, this), createIndex(row, col, this));
|
2019-04-02 15:46:18 +00:00
|
|
|
}
|
2019-05-29 15:05:54 +00:00
|
|
|
emit changed();
|
2019-04-02 15:46:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 18:15:36 +00:00
|
|
|
Models::Account * Models::Accounts::getAccount(int index)
|
|
|
|
{
|
|
|
|
return accs[index];
|
|
|
|
}
|
2019-05-29 15:05:54 +00:00
|
|
|
|
|
|
|
void Models::Accounts::removeAccount(int index)
|
|
|
|
{
|
|
|
|
Account* account = accs[index];
|
|
|
|
beginRemoveRows(QModelIndex(), index, index);
|
|
|
|
disconnect(account, SIGNAL(childChanged(Models::Item*, int, int)), this, SLOT(onAccountChanged(Models::Item*, int, int)));
|
|
|
|
accs.erase(accs.begin() + index);
|
|
|
|
endRemoveRows();
|
2019-06-15 15:29:15 +00:00
|
|
|
|
|
|
|
emit sizeChanged(accs.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::deque<QString> Models::Accounts::getNames() const
|
|
|
|
{
|
|
|
|
std::deque<QString> res;
|
|
|
|
|
|
|
|
for (std::deque<Models::Account*>::const_iterator i = accs.begin(), end = accs.end(); i != end; ++i) {
|
|
|
|
res.push_back((*i)->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2019-05-29 15:05:54 +00:00
|
|
|
}
|