2020-08-03 13:27:42 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019-2020 by Savoir-faire Linux
|
|
|
|
* Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
|
|
|
|
* Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "accountlistmodel.h"
|
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
|
|
|
|
#include "globalinstances.h"
|
|
|
|
|
|
|
|
#include "lrcinstance.h"
|
|
|
|
#include "pixbufmanipulator.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2020-09-10 17:40:51 -04:00
|
|
|
AccountListModel::AccountListModel(QObject* parent)
|
2020-08-03 13:27:42 -04:00
|
|
|
: QAbstractListModel(parent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
AccountListModel::~AccountListModel() {}
|
|
|
|
|
|
|
|
int
|
2020-09-10 17:40:51 -04:00
|
|
|
AccountListModel::rowCount(const QModelIndex& parent) const
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
if (!parent.isValid()) {
|
|
|
|
/*
|
|
|
|
* Count.
|
|
|
|
*/
|
|
|
|
return LRCInstance::accountModel().getAccountList().size();
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* A valid QModelIndex returns 0 as no entry has sub-elements.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-09-10 17:40:51 -04:00
|
|
|
AccountListModel::columnCount(const QModelIndex& parent) const
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
/*
|
|
|
|
* Only need one column.
|
|
|
|
*/
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
2020-09-10 17:40:51 -04:00
|
|
|
AccountListModel::data(const QModelIndex& index, int role) const
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
auto accountList = LRCInstance::accountModel().getAccountList();
|
|
|
|
if (!index.isValid() || accountList.size() <= index.row()) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2020-09-10 17:40:51 -04:00
|
|
|
auto& accountInfo = LRCInstance::accountModel().getAccountInfo(accountList.at(index.row()));
|
2020-08-03 13:27:42 -04:00
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Role::Alias:
|
|
|
|
return QVariant(Utils::bestNameForAccount(accountInfo));
|
|
|
|
case Role::Username:
|
|
|
|
return QVariant(Utils::secondBestNameForAccount(accountInfo));
|
|
|
|
case Role::Type:
|
2020-09-10 17:40:51 -04:00
|
|
|
return QVariant(static_cast<int>(accountInfo.profileInfo.type));
|
2020-08-03 13:27:42 -04:00
|
|
|
case Role::Status:
|
2020-09-04 14:51:39 -04:00
|
|
|
return QVariant(static_cast<int>(accountInfo.status));
|
2020-08-03 13:27:42 -04:00
|
|
|
case Role::Picture:
|
|
|
|
return QString::fromLatin1(
|
|
|
|
Utils::QImageToByteArray(Utils::accountPhoto(accountInfo)).toBase64().data());
|
|
|
|
case Role::ID:
|
|
|
|
return QVariant(accountInfo.id);
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray>
|
|
|
|
AccountListModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roles;
|
|
|
|
roles[Alias] = "Alias";
|
|
|
|
roles[Username] = "Username";
|
|
|
|
roles[Picture] = "Picture";
|
|
|
|
roles[Type] = "Type";
|
|
|
|
roles[Status] = "Status";
|
|
|
|
roles[ID] = "ID";
|
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
2020-09-10 17:40:51 -04:00
|
|
|
AccountListModel::index(int row, int column, const QModelIndex& parent) const
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
if (column != 0) {
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (row >= 0 && row < rowCount()) {
|
|
|
|
return createIndex(row, column);
|
|
|
|
}
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
2020-09-10 17:40:51 -04:00
|
|
|
AccountListModel::parent(const QModelIndex& child) const
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
Q_UNUSED(child);
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags
|
2020-09-10 17:40:51 -04:00
|
|
|
AccountListModel::flags(const QModelIndex& index) const
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable;
|
|
|
|
if (!index.isValid()) {
|
|
|
|
return QAbstractItemModel::flags(index);
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AccountListModel::reset()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
endResetModel();
|
|
|
|
}
|