玉兔远程控制 0.1.0-bate8
载入中...
搜索中...
未找到
Stats.cpp
1// Author: Kang Lin <kl222@126.com>
2#include "Stats.h"
3
4Q_DECLARE_METATYPE(CSecurityLevel::Levels)
5
6CStats::CStats(CParameterOperate *parent, const QString &szPrefix)
7 : CParameterOperate{parent}
8 , m_tmInterval(1)
9 , m_lastTime(QDateTime::currentDateTime())
10 , m_dbSendRate(0)
11 , m_dbReceiveRate(0)
12{
13 SetInterval();
14}
15
16QString CStats::Convertbytes(quint64 bytes)
17{
18 QString szBytes;
19 if((1 << 10) >= bytes)
20 szBytes = QString::number(bytes) + " " + tr("B");
21 else if((1 << 20) >= bytes)
22 szBytes = QString::number((qreal)bytes / (1 << 10), 'f', 2) + " " + tr("KB");
23 else if((1 << 30) >= bytes)
24 szBytes = QString::number((qreal)bytes / (1 << 20), 'f', 2) + " " + tr("MB");
25 else
26 szBytes = QString::number((qreal)bytes / (1 << 30), 'f', 2) + " " + tr("GB");
27 return szBytes;
28}
29
30QString CStats::TotalSends()
31{
32 return Convertbytes(GetTotalSends());
33}
34
35QString CStats::TotalReceives()
36{
37 return Convertbytes(GetTotalReceives());
38}
39
40quint64 CStats::GetTotalSends()
41{
42 return m_TotalSends;
43}
44
45quint64 CStats::GetTotalReceives()
46{
47 return m_TotalReceives;
48}
49
50void CStats::AddSends(quint64 size)
51{
52 m_TotalSends += size;
53 if(GetSendRate() <= 0)
54 emit sigDataChanged();
55}
56
57void CStats::AddReceives(quint64 size)
58{
59 m_TotalReceives += size;
60 if(GetReceiveRate() <= 0)
61 emit sigDataChanged();
62}
63
64QString CStats::SendRate()
65{
66 return Convertbytes(GetSendRate()) + "/" + tr("S");
67}
68
69QString CStats::ReceiveRate()
70{
71 return Convertbytes(GetReceiveRate()) + "/" + tr("S");
72}
73
75{
76 return m_dbSendRate;
77}
78
79double CStats::GetReceiveRate()
80{
81 return m_dbReceiveRate;
82}
83
85{
86 return m_tmInterval;
87}
88
89int CStats::SetInterval(int interval)
90{
91 if(m_tmInterval == interval)
92 return m_tmInterval;
93 int old = interval;
94 m_tmInterval = interval;
95 SetModified(true);
96 return old;
97}
98
100{
101 qint64 interval = m_lastTime.msecsTo(QDateTime::currentDateTime());
102
103 if(interval > 0) {
104 m_dbSendRate = (double)(m_TotalSends - m_lastSends) * 1000 / interval;
105 m_dbReceiveRate = (double)(m_TotalReceives - m_lastReceives) * 1000 / interval;
106 }
107
108 if(interval >= GetInterval() * 1000) {
109 m_lastSends = m_TotalSends;
110 m_lastReceives = m_TotalReceives;
111 m_lastTime = QDateTime::currentDateTime();
112 }
113}
114
115int CStats::OnLoad(QSettings &set)
116{
117 return 0;
118}
119
120int CStats::OnSave(QSettings &set)
121{
122 return 0;
123}
124
125CSecurityLevel::CSecurityLevel(Levels level, QObject* parent)
126 : QObject(parent)
127 , m_Level(level)
128{
129}
130
131CSecurityLevel::~CSecurityLevel()
132{}
133
134CSecurityLevel::Levels CSecurityLevel::GetLevel() const
135{
136 return m_Level;
137}
138
139QString CSecurityLevel::GetString() const
140{
141 return GetString(GetLevel());
142}
143
144QColor CSecurityLevel::GetColor() const
145{
146 return GetColor(GetLevel());
147}
148
149QString CSecurityLevel::GetUnicodeIcon() const
150{
151 return GetUnicodeIcon(GetLevel());
152}
153
154QIcon CSecurityLevel::GetIcon() const
155{
156 return GetIcon(GetLevel());
157}
158
159QString CSecurityLevel::GetString(const Levels &level)
160{
161 QString szSecurity;
162 if(Level::SecureChannel & level) {
163 if(!szSecurity.isEmpty())
164 szSecurity += " + ";
165 szSecurity += tr("Secure channel");
166 }
167 if(Level::Authentication & level) {
168 if(!szSecurity.isEmpty())
169 szSecurity += " + ";
170 szSecurity += tr("Authentication");
171 }
172 QString szNormal;
173 if(Level::Proxy & level) {
174 if(!szNormal.isEmpty())
175 szNormal += " + ";
176 szNormal += tr("Proxy");
177 }
178 if(Level::Gateway & level) {
179 if(!szNormal.isEmpty())
180 szNormal += " + ";
181 szNormal += tr("Gateway");
182 }
183 if(Level::Redirect & level) {
184 if(!szNormal.isEmpty())
185 szNormal += " + ";
186 szNormal += tr("Redirect");
187 }
188
189 if(level & Level::No)
190 return QString();
191
192 if(level == Level::SecureMask)
193 return tr("Secure") + ": " + szSecurity;
194 if(Level::SecureMask & level) {
195 QString szMsg = tr("Normal") + ": " + szSecurity;
196 if(!szNormal.isEmpty()) {
197 if(!szSecurity.isEmpty())
198 szMsg += " + ";
199 szMsg += szNormal;
200 }
201 return szMsg;
202 }
203 return tr("Risk");
204}
205
223QString CSecurityLevel::GetUnicodeIcon(const Levels &level)
224{
225 if(Level::No & level)
226 return QString();
227 if(level == Level::SecureMask)
228 return "🟢🛡🔐";
229 if(Level::SecureMask & level) {
230 QString s = "🟡";
231 if(Level::SecureChannel & level)
232 s += "🛡";
233 else if(Level::Authentication & level)
234 s+= "🔐";
235 return s;
236 }
237 return "🔴";
238}
239
240QColor CSecurityLevel::GetColor(const Levels &level)
241{
242 if(Level::No & level)
243 return QColor();
244 if(level == Level::SecureMask)
245 return Qt::GlobalColor::green;
246 if(Level::SecureMask & level)
247 return Qt::GlobalColor::yellow;
248 return Qt::GlobalColor::red;
249}
250
251QIcon CSecurityLevel::GetIcon(const Levels &level)
252{
253 if(Level::No & level)
254 return QIcon();
255 if(level == Level::SecureMask)
256 return QIcon::fromTheme("lock");
257 if(Level::SecureMask & level)
258 return QIcon::fromTheme("dialog-warning");
259 return QIcon::fromTheme("unlock");
260}
操作参数接口。仅在插件内有效。
int SetModified(bool bModified=true)
在设置参数时,如果有修改,则调用。
virtual void slotCalculating()
Calculating
Definition Stats.cpp:99
int SetInterval(int interval=1)
Set interval.
Definition Stats.cpp:89
void sigDataChanged()
当数据发生变化时触发,用于通知用户实时调用 slotCalculating 进行计算。
virtual double GetSendRate()
Send rate.
Definition Stats.cpp:74
int GetInterval()
Get interval.
Definition Stats.cpp:84