ORE Studio 0.0.4
Loading...
Searching...
No Matches
ClientAccountModel.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_ACCOUNT_MODEL_HPP
21#define ORES_QT_CLIENT_ACCOUNT_MODEL_HPP
22
23#include <vector>
24#include <optional>
25#include <QFutureWatcher>
26#include <QAbstractTableModel>
27#include "ores.qt/AbstractClientModel.hpp"
28#include "ores.qt/ClientManager.hpp"
29#include "ores.qt/RecencyPulseManager.hpp"
30#include "ores.qt/RecencyTracker.hpp"
31#include "ores.logging/make_logger.hpp"
32#include "ores.iam.api/domain/account.hpp"
33#include "ores.iam.api/domain/account_party.hpp"
34#include "ores.iam.api/domain/login_info.hpp"
35
36namespace ores::qt {
37
44enum class LoginStatus {
45 Never, // Never logged in (no login_info or epoch last_login)
46 LongAgo, // Logged in more than 30 days ago
47 Recent, // Logged in within the last 30 days
48 Online // Currently logged in
49};
50
61 std::optional<iam::domain::login_info> loginInfo;
62 int partyCount = 0;
63};
64
72 Q_OBJECT
73
74private:
75 inline static std::string_view logger_name =
76 "ores.qt.client_account_model";
77
78 [[nodiscard]] static auto& lg() {
79 using namespace ores::logging;
80 static auto instance = make_logger(logger_name);
81 return instance;
82 }
83
84public:
94 enum Column {
95 Username,
96 AccountType,
97 Email,
98 Parties, // Number of parties assigned to the account
99 Status, // Login status: Never, LongAgo, Recent, Online
100 Locked,
101 Version,
102 ModifiedBy,
103 RecordedAt,
104 ColumnCount // Must be last - represents total number of columns
105 };
106
107 explicit ClientAccountModel(ClientManager* clientManager,
108 QObject* parent = nullptr);
109 ~ClientAccountModel() override = default;
110
111 // QAbstractTableModel interface
112 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
113 int columnCount(const QModelIndex& parent = QModelIndex()) const override;
114 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
115 QVariant headerData(int section, Qt::Orientation orientation,
116 int role = Qt::DisplayRole) const override;
117
128 void refresh(bool replace = true);
129
139 void load_page(std::uint32_t offset, std::uint32_t limit);
140
147 const AccountWithLoginInfo* getAccountWithLoginInfo(int row) const;
148
155 const iam::domain::account* getAccount(int row) const;
156
162 std::vector<AccountWithLoginInfo> getAccountsWithLoginInfo() const;
163
169 std::uint32_t page_size() const { return page_size_; }
170
176 void set_page_size(std::uint32_t size);
177
183 std::uint32_t total_available_count() const { return total_available_count_; }
184
185signals:
194private slots:
195 void onAccountsLoaded();
196 void onPulseStateChanged(bool isOn);
197 void onPulsingComplete();
198
199private:
209 QVariant recency_foreground_color(const std::string& username) const;
210
217 static LoginStatus calculateLoginStatus(
218 const std::optional<iam::domain::login_info>& loginInfo);
219
220 struct FetchResult {
221 bool success;
222 std::vector<iam::domain::account> accounts;
223 std::vector<iam::domain::login_info> loginInfos;
224 std::vector<iam::domain::account_party> accountParties;
225 std::uint32_t total_available_count;
226 QString error_message;
227 QString error_details;
228 };
229
230 using FutureWatcherResult = FetchResult;
231
232 ClientManager* clientManager_;
233 std::vector<AccountWithLoginInfo> accounts_;
234 QFutureWatcher<FutureWatcherResult>* watcher_;
235 std::uint32_t page_size_{100};
236 std::uint32_t total_available_count_{0};
237 bool is_fetching_{false};
238
239 // Recency highlighting
240 using AccountKeyExtractor = std::string(*)(const iam::domain::account&);
241 RecencyTracker<iam::domain::account, AccountKeyExtractor> recencyTracker_;
242 RecencyPulseManager* pulseManager_;
243
247 void fetch_accounts(std::uint32_t offset, std::uint32_t limit);
248};
249
250}
251
252#endif
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:29
LoginStatus
Enum representing account login status buckets.
Definition ClientAccountModel.hpp:44
Represents an account for an entity in the system.
Definition account.hpp:34
Base class for all client-side entity models.
Definition AbstractClientModel.hpp:36
Composite structure combining account with its login status.
Definition ClientAccountModel.hpp:59
Model for displaying accounts fetched from the server via client.
Definition ClientAccountModel.hpp:71
std::vector< AccountWithLoginInfo > getAccountsWithLoginInfo() const
Get all accounts with their login info.
Definition ClientAccountModel.cpp:368
void set_page_size(std::uint32_t size)
Set the page size for pagination.
Definition ClientAccountModel.cpp:372
std::uint32_t total_available_count() const
Get the total number of records available on the server.
Definition ClientAccountModel.hpp:183
const AccountWithLoginInfo * getAccountWithLoginInfo(int row) const
Get account with login info at the specified row.
Definition ClientAccountModel.cpp:354
const iam::domain::account * getAccount(int row) const
Get account at the specified row (for backward compatibility).
Definition ClientAccountModel.cpp:361
void refresh(bool replace=true)
Refresh account data from server asynchronously.
Definition ClientAccountModel.cpp:150
void load_page(std::uint32_t offset, std::uint32_t limit)
Load a specific page of account data.
Definition ClientAccountModel.cpp:175
std::uint32_t page_size() const
Get the page size used for pagination.
Definition ClientAccountModel.hpp:169
Column
Enumeration of table columns for type-safe column access.
Definition ClientAccountModel.hpp:94
Manages the lifecycle of the NATS client and login state.
Definition ClientManager.hpp:109