2017-07-03 09:30:10 +02:00
|
|
|
/*!
|
|
|
|
* \brief Model implementation for the chat.
|
|
|
|
*
|
2018-03-28 15:10:51 +02:00
|
|
|
* \copyright Copyright (c) 2015-2018 Governikus GmbH & Co. KG, Germany
|
2017-07-03 09:30:10 +02:00
|
|
|
*/
|
|
|
|
|
2017-12-20 14:54:05 +01:00
|
|
|
#include "ChatModel.h"
|
|
|
|
|
2017-07-03 09:30:10 +02:00
|
|
|
#include "asn1/AccessRoleAndRight.h"
|
|
|
|
#include "asn1/CVCertificate.h"
|
2017-09-15 10:23:30 +02:00
|
|
|
#include "AppSettings.h"
|
2017-12-20 14:54:05 +01:00
|
|
|
#include "context/SelfAuthContext.h"
|
|
|
|
|
2017-07-03 09:30:10 +02:00
|
|
|
|
|
|
|
using namespace governikus;
|
|
|
|
|
|
|
|
|
|
|
|
ChatModel::ChatModel(QObject* pParent)
|
|
|
|
: QAbstractListModel(pParent)
|
|
|
|
, mAuthContext()
|
|
|
|
, mAllRights()
|
|
|
|
, mOptionalRights()
|
|
|
|
, mSelectedRights()
|
|
|
|
, mFilterOptionalModel()
|
|
|
|
, mFilterRequiredModel()
|
|
|
|
{
|
|
|
|
resetContext(QSharedPointer<AuthContext>());
|
|
|
|
|
|
|
|
initFilterModel(mFilterOptionalModel, QStringLiteral("true"));
|
|
|
|
initFilterModel(mFilterRequiredModel, QStringLiteral("false"));
|
2017-12-20 14:54:05 +01:00
|
|
|
|
|
|
|
connect(&AppSettings::getInstance().getGeneralSettings(), &GeneralSettings::fireSettingsChanged, this, [this]()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
endResetModel();
|
|
|
|
});
|
2017-07-03 09:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ChatModel::initFilterModel(QSortFilterProxyModel& pModel, const QString& pFilter)
|
|
|
|
{
|
|
|
|
pModel.setSourceModel(this);
|
2017-07-03 09:33:28 +02:00
|
|
|
pModel.setFilterRole(ChatRoles::OPTIONAL_ROLE);
|
2017-07-03 09:30:10 +02:00
|
|
|
pModel.setFilterRegExp(pFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-03 09:33:28 +02:00
|
|
|
void ChatModel::resetContext(const QSharedPointer<AuthContext>& pContext)
|
2017-07-03 09:30:10 +02:00
|
|
|
{
|
|
|
|
mAuthContext = pContext;
|
|
|
|
|
2017-12-20 14:54:05 +01:00
|
|
|
if (pContext.objectCast<SelfAuthContext>())
|
2017-07-03 09:30:10 +02:00
|
|
|
{
|
|
|
|
/* nothing to do, access rights are static */
|
|
|
|
}
|
|
|
|
else if (!pContext.isNull() /* it's an AuthContext */)
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
|
|
|
|
mAllRights.clear();
|
|
|
|
mOptionalRights.clear();
|
|
|
|
mSelectedRights.clear();
|
|
|
|
|
|
|
|
endResetModel();
|
|
|
|
|
2017-07-03 09:33:28 +02:00
|
|
|
connect(mAuthContext.data(), &AuthContext::fireAuthenticationDataChanged, this, &ChatModel::onAuthenticationDataChanged);
|
2017-07-03 09:30:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* set static access rights according to selfAuthentication*/
|
|
|
|
Q_ASSERT(pContext.isNull());
|
|
|
|
|
|
|
|
beginResetModel();
|
|
|
|
|
|
|
|
mAllRights.clear();
|
|
|
|
mAllRights += AccessRight::READ_DG05;
|
|
|
|
mAllRights += AccessRight::READ_DG13;
|
|
|
|
mAllRights += AccessRight::READ_DG04;
|
|
|
|
mAllRights += AccessRight::READ_DG07;
|
|
|
|
mAllRights += AccessRight::READ_DG08;
|
|
|
|
mAllRights += AccessRight::READ_DG09;
|
|
|
|
mAllRights += AccessRight::READ_DG17;
|
|
|
|
mAllRights += AccessRight::READ_DG01;
|
|
|
|
mAllRights += AccessRight::READ_DG10;
|
|
|
|
mAllRights += AccessRight::READ_DG06;
|
|
|
|
mAllRights += AccessRight::READ_DG02;
|
|
|
|
mAllRights += AccessRight::READ_DG19;
|
|
|
|
|
|
|
|
mOptionalRights.clear();
|
|
|
|
mSelectedRights = mAllRights.toSet();
|
|
|
|
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-03 09:33:28 +02:00
|
|
|
void ChatModel::onAuthenticationDataChanged()
|
2017-07-03 09:30:10 +02:00
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
|
2017-07-03 09:33:28 +02:00
|
|
|
mAllRights.clear();
|
|
|
|
mOptionalRights.clear();
|
|
|
|
mSelectedRights.clear();
|
2017-07-03 09:30:10 +02:00
|
|
|
|
2017-07-03 09:33:28 +02:00
|
|
|
if (!mAuthContext->getRequiredAccessRights().isEmpty())
|
|
|
|
{
|
|
|
|
setOrderedAllRights(mAuthContext->getRequiredAccessRights());
|
|
|
|
mSelectedRights += mAuthContext->getRequiredAccessRights();
|
|
|
|
}
|
2017-07-03 09:30:10 +02:00
|
|
|
|
2017-07-03 09:33:28 +02:00
|
|
|
if (!mAuthContext->getOptionalAccessRights().isEmpty())
|
|
|
|
{
|
|
|
|
mOptionalRights += mAuthContext->getOptionalAccessRights();
|
|
|
|
setOrderedAllRights(mAuthContext->getOptionalAccessRights());
|
|
|
|
mSelectedRights += mAuthContext->getOptionalAccessRights();
|
|
|
|
}
|
2017-07-03 09:30:10 +02:00
|
|
|
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ChatModel::setOrderedAllRights(const QSet<AccessRight>& pAllRights)
|
|
|
|
{
|
|
|
|
for (auto right : AccessRoleAndRightsUtil::allDisplayedOrderedRights())
|
|
|
|
{
|
|
|
|
if (pAllRights.contains(right))
|
|
|
|
{
|
|
|
|
mAllRights += right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ChatModel::rowCount(const QModelIndex&) const
|
|
|
|
{
|
|
|
|
return mAllRights.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QVariant ChatModel::data(const QModelIndex& pIndex, int pRole) const
|
|
|
|
{
|
|
|
|
if (pIndex.isValid() && pIndex.row() < rowCount())
|
|
|
|
{
|
|
|
|
auto right = mAllRights.at(pIndex.row());
|
2017-07-03 09:33:28 +02:00
|
|
|
if (pRole == Qt::DisplayRole || pRole == NAME_ROLE)
|
2017-07-03 09:30:10 +02:00
|
|
|
{
|
|
|
|
QString displayText = AccessRoleAndRightsUtil::toDisplayText(right);
|
2017-07-03 09:33:28 +02:00
|
|
|
if (right == AccessRight::AGE_VERIFICATION)
|
2017-07-03 09:30:10 +02:00
|
|
|
{
|
2017-07-03 09:33:28 +02:00
|
|
|
displayText += QStringLiteral(" (%1)").arg(mAuthContext->getRequiredAge());
|
2017-07-03 09:30:10 +02:00
|
|
|
}
|
|
|
|
return displayText;
|
|
|
|
}
|
2017-07-03 09:33:28 +02:00
|
|
|
if (pRole == OPTIONAL_ROLE)
|
2017-07-03 09:30:10 +02:00
|
|
|
{
|
|
|
|
return mOptionalRights.contains(right);
|
|
|
|
}
|
2017-07-03 09:33:28 +02:00
|
|
|
if (pRole == SELECTED_ROLE)
|
2017-07-03 09:30:10 +02:00
|
|
|
{
|
|
|
|
return mSelectedRights.contains(right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QHash<int, QByteArray> ChatModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roles = QAbstractListModel::roleNames();
|
2017-07-03 09:33:28 +02:00
|
|
|
roles.insert(NAME_ROLE, "name");
|
|
|
|
roles.insert(OPTIONAL_ROLE, "optional");
|
|
|
|
roles.insert(SELECTED_ROLE, "selected");
|
2017-07-03 09:30:10 +02:00
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ChatModel::setData(const QModelIndex& pIndex, const QVariant& pValue, int pRole)
|
|
|
|
{
|
2017-07-03 09:33:28 +02:00
|
|
|
if (pRole == SELECTED_ROLE)
|
2017-07-03 09:30:10 +02:00
|
|
|
{
|
|
|
|
auto right = mAllRights.at(pIndex.row());
|
|
|
|
|
|
|
|
if (pValue.toBool()) // is selected?
|
|
|
|
{
|
|
|
|
if (mSelectedRights.contains(right))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mSelectedRights += right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!mSelectedRights.remove(right))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-03 09:33:28 +02:00
|
|
|
Q_EMIT dataChanged(pIndex, pIndex, QVector<int>({SELECTED_ROLE}));
|
2017-07-03 09:30:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ChatModel::transferAccessRights()
|
|
|
|
{
|
|
|
|
Q_ASSERT(mAuthContext);
|
|
|
|
|
|
|
|
mAuthContext->setEffectiveAccessRights(mSelectedRights);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QSortFilterProxyModel* ChatModel::getFilterOptionalModel()
|
|
|
|
{
|
|
|
|
return &mFilterOptionalModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QSortFilterProxyModel* ChatModel::getFilterRequiredModel()
|
|
|
|
{
|
|
|
|
return &mFilterRequiredModel;
|
|
|
|
}
|