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 <unordered_set>
26#include <QFutureWatcher>
27#include <QAbstractTableModel>
28#include <QTimer>
29#include "ores.qt/ClientManager.hpp"
30#include "ores.utility/log/make_logger.hpp"
31#include "ores.accounts/domain/account.hpp"
32#include "ores.accounts/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<accounts::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::utility::log;
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
110 bool canFetchMore(const QModelIndex& parent = QModelIndex()) const override;
111
118 void fetchMore(const QModelIndex& parent = QModelIndex()) override;
119
126 const AccountWithLoginInfo* getAccountWithLoginInfo(int row) const;
127
134 const accounts::domain::account* getAccount(int row) const;
135
141 std::vector<AccountWithLoginInfo> getAccountsWithLoginInfo() const;
142
148 std::uint32_t page_size() const { return page_size_; }
149
155 void set_page_size(std::uint32_t size);
156
162 std::uint32_t total_available_count() const { return total_available_count_; }
163
164signals:
169
173 void loadError(const QString& error_message);
174
175private slots:
176 void onAccountsLoaded();
177 void onPulseTimerTimeout();
178
179private:
189 QVariant recency_foreground_color(const std::string& username) const;
190
194 void update_recent_accounts();
201 enum Column {
202 Username,
203 Email,
204 IsAdmin,
205 Status, // Login status: Never, LongAgo, Recent, Online
206 Locked,
207 Version,
208 RecordedBy,
209 RecordedAt,
210 ColumnCount // Must be last - represents total number of columns
211 };
212
219 static LoginStatus calculateLoginStatus(
220 const std::optional<accounts::domain::login_info>& loginInfo);
221
222 struct FetchResult {
223 bool success;
224 std::vector<accounts::domain::account> accounts;
225 std::vector<accounts::domain::login_info> loginInfos;
226 std::uint32_t total_available_count;
227 };
228
229 using FutureWatcherResult = FetchResult;
230
231 ClientManager* clientManager_;
232 std::vector<AccountWithLoginInfo> accounts_;
233 QFutureWatcher<FutureWatcherResult>* watcher_;
234 std::uint32_t page_size_{100};
235 std::uint32_t total_available_count_{0};
236 bool is_fetching_{false};
237
238 // Recency tracking for row coloring based on recorded_at date
239 std::unordered_set<std::string> recent_usernames_; // Usernames newer than last reload
240 QDateTime last_reload_time_; // Timestamp of last reload (for comparison)
241 QTimer* pulse_timer_;
242 mutable bool pulse_state_{false}; // Toggle for pulsing effect
243 mutable int pulse_count_{0};
244 static constexpr int max_pulse_cycles_{6}; // 6 pulses (3 seconds at 500ms interval)
245 static constexpr int pulse_interval_ms_{500}; // Pulse toggle interval in milliseconds
246};
247
248}
249
250#endif
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:35
LoginStatus
Enum representing account login status buckets.
Definition ClientAccountModel.hpp:42
Implements logging for ORE Studio.
Definition lifecycle_manager.hpp:30
Represents a user account in the system.
Definition account.hpp:31
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:359
void fetchMore(const QModelIndex &parent=QModelIndex()) override
Fetch the next page of data from the server.
Definition ClientAccountModel.cpp:371
std::vector< AccountWithLoginInfo > getAccountsWithLoginInfo() const
Get all accounts with their login info.
Definition ClientAccountModel.cpp:355
void set_page_size(std::uint32_t size)
Set the page size for pagination.
Definition ClientAccountModel.cpp:379
const accounts::domain::account * getAccount(int row) const
Get account at the specified row (for backward compatibility).
Definition ClientAccountModel.cpp:348
std::uint32_t total_available_count() const
Get the total number of records available on the server.
Definition ClientAccountModel.hpp:162
const AccountWithLoginInfo * getAccountWithLoginInfo(int row) const
Get account with login info at the specified row.
Definition ClientAccountModel.cpp:341
void dataLoaded()
Emitted when data has been successfully loaded.
void refresh(bool replace=true)
Refresh account data from server asynchronously.
Definition ClientAccountModel.cpp:143
std::uint32_t page_size() const
Get the page size used for pagination.
Definition ClientAccountModel.hpp:148
void loadError(const QString &error_message)
Emitted when an error occurs during data loading.
Manages the lifecycle of the network client and IO context.
Definition ClientManager.hpp:56