ORE Studio 0.0.4
Loading...
Searching...
No Matches
ClientRoleModel.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_QT_CLIENT_ROLE_MODEL_HPP
21#define ORES_QT_CLIENT_ROLE_MODEL_HPP
22
23#include <vector>
24#include <QSize>
25#include <QFutureWatcher>
26#include <QAbstractTableModel>
27#include "ores.qt/AbstractClientModel.hpp"
28#include "ores.qt/ClientManager.hpp"
29#include "ores.qt/ColumnMetadata.hpp"
30#include "ores.logging/make_logger.hpp"
31#include "ores.iam.api/domain/role.hpp"
32
33namespace ores::qt {
34
42 Q_OBJECT
43
44private:
45 inline static std::string_view logger_name =
46 "ores.qt.client_role_model";
47
48 [[nodiscard]] static auto& lg() {
49 using namespace ores::logging;
50 static auto instance = make_logger(logger_name);
51 return instance;
52 }
53
54public:
58 enum Column {
59 Name,
60 Description,
61 PermissionCount,
62 Version,
63 ModifiedBy,
64 RecordedAt,
65 ColumnCount
66 };
67
73 static constexpr std::size_t kColumnCount = std::size_t(ColumnCount);
74 static constexpr std::array<ColumnMetadata, kColumnCount> kColumns = {{
75 {
76 .column = Name,
77 .header = std::string_view("Name"),
79 .hidden_by_default = false,
80 .default_width = kColumnWidthAuto
81 },
82 {
83 .column = Description,
84 .header = std::string_view("Description"),
86 .hidden_by_default = false,
87 .default_width = kColumnWidthAuto
88 },
89 {
90 .column = PermissionCount,
91 .header = std::string_view("Permission Count"),
93 .hidden_by_default = false,
94 .default_width = 70
95 },
96 {
97 .column = Version,
98 .header = std::string_view("Version"),
100 .hidden_by_default = false,
101 .default_width = 70
102 },
103 {
104 .column = ModifiedBy,
105 .header = std::string_view("Modified By"),
107 .hidden_by_default = false,
108 .default_width = kColumnWidthAuto
109 },
110 {
111 .column = RecordedAt,
112 .header = std::string_view("Recorded At"),
114 .hidden_by_default = false,
115 .default_width = kColumnWidthAuto
116 }
117 }};
118
122 inline static const QSize kDefaultWindowSize = {700, 400};
123
127 static constexpr std::string_view kSettingsGroup = "RoleListWindow";
128
132 static std::vector<column_style> const& columnStyles() {
133 static std::vector<column_style> const kStylesVector = []() {
134 std::vector<column_style> result;
135 result.reserve(kColumnCount);
136 for (std::size_t i = 0; i < kColumnCount; ++i)
137 result.push_back(kColumns[i].style);
138 return result;
139 }();
140 return kStylesVector;
141 }
142
146 static QVector<int> defaultHiddenColumns() {
147 static QVector<int> const result =
148 ::ores::qt::defaultHiddenColumns<kColumnCount>(kColumns);
149 return result;
150 }
151
152 explicit ClientRoleModel(ClientManager* clientManager,
153 QObject* parent = nullptr);
154 ~ClientRoleModel() override = default;
155
156 // QAbstractTableModel interface
157 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
158 int columnCount(const QModelIndex& parent = QModelIndex()) const override;
159 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
160 QVariant headerData(int section, Qt::Orientation orientation,
161 int role = Qt::DisplayRole) const override;
162
166 void refresh();
167
174 const iam::domain::role* getRole(int row) const;
175
181 std::vector<iam::domain::role> getRoles() const;
182
183signals:
192private slots:
193 void onRolesLoaded();
194
195private:
196 struct FetchResult {
197 bool success;
198 std::vector<iam::domain::role> roles;
199 QString error_message;
200 QString error_details;
201 };
202
203 using FutureWatcherResult = FetchResult;
204
205 ClientManager* clientManager_;
206 std::vector<iam::domain::role> roles_;
207 QFutureWatcher<FutureWatcherResult>* watcher_;
208 bool is_fetching_{false};
209};
210
211}
212
213#endif
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:29
constexpr int kColumnWidthAuto
Sentinel value for column default width meaning "auto-size to contents".
Definition ColumnMetadata.hpp:37
@ mono_center
Monospace, centered.
@ text_left
Proportional font, left-aligned (default).
@ mono_left
Monospace, left-aligned.
Represents a named collection of permissions that can be assigned to accounts.
Definition role.hpp:41
Base class for all client-side entity models.
Definition AbstractClientModel.hpp:36
Manages the lifecycle of the NATS client and login state.
Definition ClientManager.hpp:109
Model for displaying roles fetched from the server via client.
Definition ClientRoleModel.hpp:41
static constexpr std::string_view kSettingsGroup
Settings group name for persisting window and column state.
Definition ClientRoleModel.hpp:127
static constexpr std::size_t kColumnCount
Column metadata: header text, style, visibility, and width.
Definition ClientRoleModel.hpp:73
static const QSize kDefaultWindowSize
Default window size for the role list window.
Definition ClientRoleModel.hpp:122
static std::vector< column_style > const & columnStyles()
Returns a static vector of column styles (built once per process).
Definition ClientRoleModel.hpp:132
void refresh()
Refresh role data from server asynchronously.
Definition ClientRoleModel.cpp:99
static QVector< int > defaultHiddenColumns()
Returns a static QVector of hidden column indices (built once per process).
Definition ClientRoleModel.hpp:146
std::vector< iam::domain::role > getRoles() const
Get all roles.
Definition ClientRoleModel.cpp:190
const iam::domain::role * getRole(int row) const
Get role at the specified row.
Definition ClientRoleModel.cpp:183
Column
Enumeration of table columns for type-safe column access.
Definition ClientRoleModel.hpp:58