swift
rolelist.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QJsonValue>
7 
8 #include "misc/network/role.h"
9 
10 SWIFT_DEFINE_SEQUENCE_MIXINS(swift::misc::network, CRole, CRoleList)
11 
12 namespace swift::misc::network
13 {
14  bool CRoleList::hasRole(const QString &roleName) const
15  {
16  return this->contains(&CRole::getName, roleName.trimmed().toUpper());
17  }
18 
19  bool CRoleList::hasRole(const CRole &role) const { return hasRole(role.getName()); }
20 
21  bool CRoleList::hasAnyRole(const QStringList &roles) const
22  {
23  return std::any_of(roles.cbegin(), roles.cend(), [&](const QString &r) { return this->hasRole(r); });
24  }
25 
27 
28  QString CRoleList::namesAsString(const QString &separator) const
29  {
30  QStringList rolesString;
31  rolesString.reserve(size());
32  for (const CRole &role : (*this)) { rolesString.append(role.getName()); }
33  return rolesString.join(separator);
34  }
35 
37  {
38  CRoleList roles;
39  for (const QJsonValue &value : array) { roles.push_back(CRole::fromDatabaseJson(value.toObject())); }
40  return roles;
41  }
42 
43 } // namespace swift::misc::network
bool contains(const T &object) const
Return true if there is an element equal to given object. Uses the most efficient implementation avai...
Definition: range.h:109
size_type size() const
Returns number of elements in the sequence.
Definition: sequence.h:273
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
static CRole fromDatabaseJson(const QJsonObject &json)
Role from DB JSON.
Definition: role.cpp:61
const QString & getName() const
Name.
Definition: role.h:43
Value object encapsulating a list of servers.
Definition: rolelist.h:27
QString namesAsString(const QString &separator=", ") const
Roles as string.
Definition: rolelist.cpp:28
static CRoleList fromDatabaseJson(const QJsonArray &array)
From our database JSON format.
Definition: rolelist.cpp:36
bool hasRole(const QString &roleName) const
Has role?
Definition: rolelist.cpp:14
CRoleList()=default
Default constructor.
bool hasAnyRole(const QStringList &roles) const
Has any role?
Definition: rolelist.cpp:21
void append(QList< T > &&value)
QList< T >::const_iterator cbegin() const const
QList< T >::const_iterator cend() const const
void reserve(qsizetype size)
QString toUpper() const const
QString trimmed() const const
QString join(QChar separator) const const
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition: sequence.h:63