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 {
15 
16  bool CRoleList::hasRole(const QString &roleName) const
17  {
18  return this->contains(&CRole::getName, roleName.trimmed().toUpper());
19  }
20 
21  bool CRoleList::hasRole(const CRole &role) const { return hasRole(role.getName()); }
22 
23  bool CRoleList::hasAnyRole(const QStringList &roles) const
24  {
25  for (const QString &r : roles)
26  {
27  if (this->hasRole(r)) { return true; }
28  }
29  return false;
30  }
31 
33 
34  QString CRoleList::namesAsString(const QString &separator) const
35  {
36  QStringList rolesString;
37  rolesString.reserve(size());
38  for (const CRole &role : (*this)) { rolesString.append(role.getName()); }
39  return rolesString.join(separator);
40  }
41 
42  CRoleList CRoleList::fromDatabaseJson(const QJsonArray &array)
43  {
44  CRoleList roles;
45  for (const QJsonValue &value : array) { roles.push_back(CRole::fromDatabaseJson(value.toObject())); }
46  return roles;
47  }
48 
49 } // 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:34
CRoleList()
Default constructor.
Definition: rolelist.cpp:14
static CRoleList fromDatabaseJson(const QJsonArray &array)
From our database JSON format.
Definition: rolelist.cpp:42
bool hasRole(const QString &roleName) const
Has role?
Definition: rolelist.cpp:16
bool hasAnyRole(const QStringList &roles) const
Has any role?
Definition: rolelist.cpp:23
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition: sequence.h:63