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 "lrcinstance.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2021-03-11 13:30:45 -05:00
|
|
|
#include "api/account.h"
|
|
|
|
#include "api/contact.h"
|
|
|
|
#include "api/conversation.h"
|
|
|
|
|
2020-09-10 17:40:51 -04:00
|
|
|
AccountListModel::AccountListModel(QObject* parent)
|
2021-03-11 13:30:45 -05:00
|
|
|
: AbstractListModelBase(parent)
|
2020-08-03 13:27:42 -04:00
|
|
|
{}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2021-03-11 13:30:45 -05:00
|
|
|
if (!parent.isValid() && lrcInstance_) {
|
2020-08-03 13:27:42 -04:00
|
|
|
/*
|
|
|
|
* Count.
|
|
|
|
*/
|
2021-03-11 13:30:45 -05:00
|
|
|
return lrcInstance_->accountModel().getAccountList().size();
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
{
|
2021-03-11 13:30:45 -05:00
|
|
|
auto accountList = lrcInstance_->accountModel().getAccountList();
|
2020-08-03 13:27:42 -04:00
|
|
|
if (!index.isValid() || accountList.size() <= index.row()) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2020-10-20 12:11:09 -04:00
|
|
|
auto accountId = accountList.at(index.row());
|
2021-03-11 13:30:45 -05:00
|
|
|
auto& accountInfo = lrcInstance_->accountModel().getAccountInfo(accountId);
|
2020-08-03 13:27:42 -04:00
|
|
|
|
2020-10-19 14:51:31 -04:00
|
|
|
// Since we are using image provider right now, image url representation should be unique to
|
|
|
|
// be able to use the image cache, account avatar will only be updated once PictureUid changed
|
2020-08-03 13:27:42 -04:00
|
|
|
switch (role) {
|
|
|
|
case Role::Alias:
|
2021-03-11 13:30:45 -05:00
|
|
|
return QVariant(lrcInstance_->accountModel().bestNameForAccount(accountId));
|
2020-08-03 13:27:42 -04:00
|
|
|
case Role::Username:
|
2021-03-11 13:30:45 -05:00
|
|
|
return QVariant(lrcInstance_->accountModel().bestIdForAccount(accountId));
|
2020-08-03 13:27:42 -04:00
|
|
|
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::ID:
|
|
|
|
return QVariant(accountInfo.id);
|
2020-10-19 14:51:31 -04:00
|
|
|
case Role::PictureUid:
|
|
|
|
return avatarUidMap_[accountInfo.id];
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray>
|
|
|
|
AccountListModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roles;
|
|
|
|
roles[Alias] = "Alias";
|
|
|
|
roles[Username] = "Username";
|
|
|
|
roles[Type] = "Type";
|
|
|
|
roles[Status] = "Status";
|
|
|
|
roles[ID] = "ID";
|
2020-10-19 14:51:31 -04:00
|
|
|
roles[PictureUid] = "PictureUid";
|
2020-08-03 13:27:42 -04:00
|
|
|
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();
|
2021-03-11 13:30:45 -05:00
|
|
|
fillAvatarUidMap(lrcInstance_->accountModel().getAccountList());
|
2020-08-03 13:27:42 -04:00
|
|
|
endResetModel();
|
|
|
|
}
|
2020-10-19 14:51:31 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
AccountListModel::updateAvatarUid(const QString& accountId)
|
|
|
|
{
|
|
|
|
avatarUidMap_[accountId] = Utils::generateUid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AccountListModel::fillAvatarUidMap(const QStringList& accountList)
|
|
|
|
{
|
|
|
|
if (accountList.size() == 0) {
|
|
|
|
avatarUidMap_.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avatarUidMap_.isEmpty() || accountList.size() != avatarUidMap_.size()) {
|
|
|
|
for (int i = 0; i < accountList.size(); ++i) {
|
|
|
|
if (!avatarUidMap_.contains(accountList.at(i)))
|
|
|
|
avatarUidMap_.insert(accountList.at(i), Utils::generateUid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|