-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.h
More file actions
94 lines (80 loc) · 2.97 KB
/
Copy pathDataManager.h
File metadata and controls
94 lines (80 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once
#include <string>
#include <map>
#define g_data CDataManager::Instance()
// Which display row an item represents. The first three are the OpenCode Go usage
// windows (Rolling / Weekly / Monthly); Reset is an extra combined row showing the
// next reset countdown for all three windows.
enum class DisplayWindow
{
Rolling = 0,
Weekly = 1,
Monthly = 2,
Reset = 3,
Count = 4,
};
static const wchar_t* DisplayWindowShortLabel(DisplayWindow w)
{
switch (w)
{
case DisplayWindow::Weekly: return L"W";
case DisplayWindow::Monthly: return L"M";
case DisplayWindow::Reset: return L"RS";
default: return L"R";
}
}
struct SettingData
{
std::wstring api_key;
// Seconds between API fetches (background thread). Default 60, min 5.
int refresh_interval_seconds = 60;
// Whether each usage row (R/W/M) value also carries a compact reset countdown,
// e.g. "2% (4h)" vs "2%". (The dedicated Reset row always shows its countdowns.)
bool show_reset_time = true;
};
class CDataManager
{
private:
CDataManager();
~CDataManager();
public:
static CDataManager& Instance();
void LoadConfig(const std::wstring& config_dir);
void SaveConfig() const;
// Cached usage values, written by the background fetch thread and read by
// the display thread. All access goes through the lock so GetItemValueText
// (called frequently on the UI thread) never sees torn data.
void SetUsage(const struct OpenCodeGoUsage& usage);
void SetErrorMessage(const std::wstring& message);
void ClearError();
// Display label / value for one window (Rolling/Weekly/Monthly/Reset).
const wchar_t* GetDisplayLabel(DisplayWindow w) const;
const wchar_t* GetDisplayValue(DisplayWindow w) const;
// Graph value (0..1) for one window; -1 when unavailable.
float GetGraphValue(DisplayWindow w) const;
std::wstring GetError() const;
bool HasApiKey() const;
// Get a string resource by id (Win32 LoadStringW). Pointer valid until the
// next StringRes/GetDisplay* call on this thread.
const wchar_t* StringRes(UINT id) const;
public:
SettingData m_setting_data;
private:
static CDataManager m_instance;
std::wstring m_config_path;
mutable CRITICAL_SECTION m_lock;
mutable std::map<UINT, std::wstring> m_string_table;
// Buffers for GetDisplayLabel/GetDisplayValue — the returned pointers are only
// valid until the next call, which is the plugin text contract. One buffer per
// window so the three rows don't clobber each other.
mutable std::wstring m_label_buffer[static_cast<size_t>(DisplayWindow::Count)];
mutable std::wstring m_value_buffer[static_cast<size_t>(DisplayWindow::Count)];
int m_rolling_percent{ -1 };
int m_weekly_percent{ -1 };
int m_monthly_percent{ -1 };
bool m_has_usage{ false };
std::wstring m_error;
std::wstring m_rolling_reset;
std::wstring m_weekly_reset;
std::wstring m_monthly_reset;
};