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/ClientManager.hpp"
28#include "ores.qt/RecencyPulseManager.hpp"
29#include "ores.qt/RecencyTracker.hpp"
30#include "ores.logging/make_logger.hpp"
31#include "ores.iam/domain/account.hpp"
32#include "ores.iam/domain/login_info.hpp"
33
34namespace ores::qt {
35
42enum class LoginStatus {
43 Never, // Never logged in (no login_info or epoch last_login)
44 LongAgo, // Logged in more than 30 days ago
45 Recent, // Logged in within the last 30 days
46 Online // Currently logged in
47};
48
59 std::optional<iam::domain::login_info> loginInfo;
60};
61
68class ClientAccountModel final : public QAbstractTableModel {
69 Q_OBJECT
70
71private:
72 inline static std::string_view logger_name =
73 "ores.qt.client_account_model";
74
75 [[nodiscard]] static auto& lg() {
76 using namespace ores::logging;
77 static auto instance = make_logger(logger_name);
78 return instance;
79 }
80
81public:
82 explicit ClientAccountModel(ClientManager* clientManager,
83 QObject* parent = nullptr);
84 ~ClientAccountModel() override = default;
85
86 // QAbstractTableModel interface
87 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
88 int columnCount(const QModelIndex& parent = QModelIndex()) const override;
89 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
90 QVariant headerData(int section, Qt::Orientation orientation,
91 int role = Qt::DisplayRole) const override;
92
103 void refresh(bool replace = true);
104
114 void load_page(std::uint32_t offset, std::uint32_t limit);
115
121 bool canFetchMore(const QModelIndex& parent = QModelIndex()) const override;
122
129 void fetchMore(const QModelIndex& parent = QModelIndex()) override;
130
137 const AccountWithLoginInfo* getAccountWithLoginInfo(int row) const;
138
145 const iam::domain::account* getAccount(int row) const;
146
152 std::vector<AccountWithLoginInfo> getAccountsWithLoginInfo() const;
153
159 std::uint32_t page_size() const { return page_size_; }
160
166 void set_page_size(std::uint32_t size);
167
173 std::uint32_t total_available_count() const { return total_available_count_; }
174
175signals:
180
184 void loadError(const QString& error_message, const QString& details = {});
185
186private slots:
187 void onAccountsLoaded();
188 void onPulseStateChanged(bool isOn);
189 void onPulsingComplete();
190
191private:
201 QVariant recency_foreground_color(const std::string& username) const;
202
210 enum Column {
211 Username,
212 Email,
213 Status, // Login status: Never, LongAgo, Recent, Online
214 Locked,
215 Version,
216 RecordedBy,
217 RecordedAt,
218 ColumnCount // Must be last - represents total number of columns
219 };
220
227 static LoginStatus calculateLoginStatus(
228 const std::optional<iam::domain::login_info>& loginInfo);
229
230 struct FetchResult {
231 bool success;
232 std::vector<iam::domain::account> accounts;
233 std::vector<iam::domain::login_info> loginInfos;
234 std::uint32_t total_available_count;
235 QString error_message;
236 QString error_details;
237 };
238
239 using FutureWatcherResult = FetchResult;
240
241 ClientManager* clientManager_;
242 std::vector<AccountWithLoginInfo> accounts_;
243 QFutureWatcher<FutureWatcherResult>* watcher_;
244 std::uint32_t page_size_{100};
245 std::uint32_t total_available_count_{0};
246 bool is_fetching_{false};
247
248 // Recency highlighting
249 using AccountKeyExtractor = std::string(*)(const iam::domain::account&);
250 RecencyTracker<iam::domain::account, AccountKeyExtractor> recencyTracker_;
251 RecencyPulseManager* pulseManager_;
252
256 void fetch_accounts(std::uint32_t offset, std::uint32_t limit);
257};
258
259}
260
261#endif
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:35
LoginStatus
Enum representing account login status buckets.
Definition ClientAccountModel.hpp:42
Represents an account for an entity in the system.
Definition account.hpp:32
Composite structure combining account with its login status.
Definition ClientAccountModel.hpp:57
Model for displaying accounts fetched from the server via client.
Definition ClientAccountModel.hpp:68
bool canFetchMore(const QModelIndex &parent=QModelIndex()) const override
Check if more data can be fetched from the server.
Definition ClientAccountModel.cpp:377
void fetchMore(const QModelIndex &parent=QModelIndex()) override
Fetch the next page of data from the server.
Definition ClientAccountModel.cpp:389
std::vector< AccountWithLoginInfo > getAccountsWithLoginInfo() const
Get all accounts with their login info.
Definition ClientAccountModel.cpp:373
void set_page_size(std::uint32_t size)
Set the page size for pagination.
Definition ClientAccountModel.cpp:397
std::uint32_t total_available_count() const
Get the total number of records available on the server.
Definition ClientAccountModel.hpp:173
const AccountWithLoginInfo * getAccountWithLoginInfo(int row) const
Get account with login info at the specified row.
Definition ClientAccountModel.cpp:359
void dataLoaded()
Emitted when data has been successfully loaded.
const iam::domain::account * getAccount(int row) const
Get account at the specified row (for backward compatibility).
Definition ClientAccountModel.cpp:366
void refresh(bool replace=true)
Refresh account data from server asynchronously.
Definition ClientAccountModel.cpp:147
void load_page(std::uint32_t offset, std::uint32_t limit)
Load a specific page of account data.
Definition ClientAccountModel.cpp:179
void loadError(const QString &error_message, const QString &details={})
Emitted when an error occurs during data loading.
std::uint32_t page_size() const
Get the page size used for pagination.
Definition ClientAccountModel.hpp:159
Manages the lifecycle of the network client and IO context.
Definition ClientManager.hpp:90