2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (C) 2005-2008 Dévai Tamás ( gonosztopi@amule.org )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "OtherFunctions.h"
34 #include <common/Format.h> // Needed for CFormat
36 #define a_brackets_b(a,b) (a + wxT(" (") + b + wxT(")"))
38 #endif /* !AMULE_DAEMON */
40 #endif /* !EC_REMOTE */
42 #include <ec/cpp/ECTag.h> // Needed for CECTag
45 #include <ec/cpp/ECSpecialTags.h> // Needed for CEC_StatTree_Node_Tag
50 uint32_t NewStatTreeItemId()
52 static uint32_t lastid
= 0;
58 /* CStatTreeItemBase */
61 CStatTreeItemBase::CStatTreeItemBase(const CECTag
*tag
)
62 : m_label(((CEC_StatTree_Node_Tag
*)tag
)->GetDisplayString())
63 , m_uniqueid(tag
->GetTagByNameSafe(EC_TAG_STATTREE_NODEID
)->GetInt())
65 wxASSERT(tag
->GetTagName() == EC_TAG_STATTREE_NODE
);
67 for (int i
= 0; i
< tag
->GetTagCount(); ++i
) {
68 const CECTag
*tmp
= tag
->GetTagByIndex(i
);
69 if (tmp
->GetTagName() == EC_TAG_STATTREE_NODE
) {
70 m_children
.push_back(new CStatTreeItemBase(tmp
));
74 #endif /* EC_REMOTE */
76 CStatTreeItemBase::~CStatTreeItemBase()
78 for (std::list
<CStatTreeItemBase
*>::iterator it
= m_children
.begin();
79 it
!= m_children
.end(); ++it
) {
86 CStatTreeItemBase
* CStatTreeItemBase::AddChild(
87 CStatTreeItemBase
* child
,
91 wxMutexLocker
lock(m_lock
);
94 child
->m_parent
= m_parent
;
96 child
->m_parent
= this;
100 if (m_flags
& stSortChildren
) {
101 std::list
<CStatTreeItemBase
*>::iterator it
= m_children
.begin();
102 while (it
!= m_children
.end() && id
< (*it
)->m_id
) {
105 m_children
.insert(it
, child
);
107 m_children
.push_back(child
);
111 #endif /* !EC_REMOTE */
113 bool CStatTreeItemBase::HasVisibleChildren()
115 wxMutexLocker
lock(m_lock
);
117 for (std::list
<CStatTreeItemBase
*>::const_iterator it
= m_children
.begin();
118 it
!= m_children
.end(); ++it
) {
119 if ((*it
)->IsVisible()) {
127 bool CStatTreeItemBase::HasChildWithId(uint32_t id
)
129 wxMutexLocker
lock(m_lock
);
131 for (std::list
<CStatTreeItemBase
*>::const_iterator it
= m_children
.begin();
132 it
!= m_children
.end(); ++it
) {
133 if ((*it
)->m_id
== id
) {
140 CStatTreeItemBase
* CStatTreeItemBase::GetChildById(uint32_t id
)
142 wxMutexLocker
lock(m_lock
);
144 for (std::list
<CStatTreeItemBase
*>::const_iterator it
= m_children
.begin();
145 it
!= m_children
.end(); ++it
) {
146 if ((*it
)->m_id
== id
) {
152 #endif /* !EC_REMOTE */
154 // Note: these functions do not lock the list, because it is already locked at the time they're called
156 StatTreeItemIterator
CStatTreeItemBase::GetFirstVisibleChild(uint32_t max_children
)
158 StatTreeItemIterator it
= m_children
.begin();
159 m_visible_counter
= --max_children
;
160 while (it
!= m_children
.end() && !(*it
)->IsVisible()) ++it
;
164 void CStatTreeItemBase::GetNextVisibleChild(StatTreeItemIterator
& it
)
166 if (m_flags
& stCapChildren
) {
167 if (m_visible_counter
== 0) {
168 it
= m_children
.end();
174 if (it
!= m_children
.end()) ++it
;
175 while (it
!= m_children
.end() && !(*it
)->IsVisible()) ++it
;
180 // Anything below is only for core.
184 bool CStatTreeItemBase::ValueSort(CStatTreeItemBase
* a
, CStatTreeItemBase
* b
)
186 if (a
->m_id
< 0x00000100 || a
->m_id
> 0x7fffffff || b
->m_id
< 0x00000100 || b
->m_id
> 0x7fffffff) {
187 return a
->m_id
> b
->m_id
;
189 return ((CStatTreeItemCounter
*)a
)->GetValue() > ((CStatTreeItemCounter
*)b
)->GetValue();
194 wxString
CStatTreeItemBase::GetDisplayString() const
196 return wxGetTranslation(m_label
);
200 CECTag
*CStatTreeItemBase::CreateECTag(uint32_t max_children
)
203 wxMutexLocker
lock(m_lock
);
204 CECTag
*tag
= new CECTag(EC_TAG_STATTREE_NODE
, m_label
);
205 tag
->AddTag(CECTag(EC_TAG_STATTREE_NODEID
, m_uniqueid
));
207 m_visible_counter
= max_children
- 1;
208 for (std::list
<CStatTreeItemBase
*>::const_iterator it
= m_children
.begin();
209 it
!= m_children
.end(); ++it
) {
210 CECTag
*tmp
= (*it
)->CreateECTag(max_children
);
214 if (m_flags
& stCapChildren
) {
215 if (m_visible_counter
== 0) {
230 /* CStatTreeItemSimple */
233 wxString
CStatTreeItemSimple::GetDisplayString() const
235 switch (m_valuetype
) {
237 switch (m_displaymode
) {
238 case dmTime
: return CFormat(wxGetTranslation(m_label
)) % CastSecondsToHM(m_intvalue
);
239 case dmBytes
: return CFormat(wxGetTranslation(m_label
)) % CastItoXBytes(m_intvalue
);
240 default: return CFormat(wxGetTranslation(m_label
)) % m_intvalue
;
242 case vtFloat
: return wxString::Format(wxGetTranslation(m_label
), m_floatvalue
);
243 case vtString
: return CFormat(wxGetTranslation(m_label
)) % m_stringvalue
;
244 default: return wxGetTranslation(m_label
);
249 bool CStatTreeItemSimple::IsVisible() const
251 if (m_flags
& stHideIfZero
) {
252 switch (m_valuetype
) {
253 case vtInteger
: return m_intvalue
!= 0;
254 case vtFloat
: return m_floatvalue
!= 0.0;
255 case vtString
: return !m_stringvalue
.IsEmpty();
256 default: return false;
262 void CStatTreeItemSimple::AddECValues(CECTag
*tag
) const
264 switch (m_valuetype
) {
266 if (m_displaymode
== dmTime
) {
267 CECTag
value(EC_TAG_STAT_NODE_VALUE
, (uint32
)m_intvalue
);
268 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_TIME
));
271 CECTag
value(EC_TAG_STAT_NODE_VALUE
, (uint64
)m_intvalue
);
272 if (m_displaymode
== dmBytes
) {
273 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_BYTES
));
280 CECTag
value(EC_TAG_STAT_NODE_VALUE
, m_floatvalue
);
281 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_DOUBLE
));
286 CECTag
value(EC_TAG_STAT_NODE_VALUE
, m_stringvalue
);
287 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_STRING
));
297 /* CStatTreeItemCounterTmpl */
300 template<typename _Tp
>
301 wxString CStatTreeItemCounterTmpl
<_Tp
>::GetDisplayString() const
303 wxString my_label
= wxGetTranslation(m_label
);
304 // This is needed for client names, for example
305 if (my_label
== m_label
) {
306 if (m_label
.Right(4) == wxT(": %s")) {
307 my_label
= wxGetTranslation(
308 m_label
.Mid(0, m_label
.Length() - 4)) +
309 wxString(wxT(": %s"));
312 CFormat
label(my_label
);
313 if (m_displaymode
== dmBytes
) {
314 return label
% CastItoXBytes(m_value
);
316 wxString result
= CFormat(wxT("%u")) % m_value
;
317 if ((m_flags
& stShowPercent
) && m_parent
) {
318 result
.append(wxString::Format(wxT(" (%.2f%%)"),
319 ((double)m_value
/ ((CStatTreeItemCounterTmpl
<_Tp
>*)m_parent
)->m_value
) * 100.0));
321 return label
% result
;
326 template<typename _Tp
>
327 void CStatTreeItemCounterTmpl
<_Tp
>::AddECValues(CECTag
*tag
) const
329 CECTag
value(EC_TAG_STAT_NODE_VALUE
, (uint64
)m_value
);
330 if (m_displaymode
== dmBytes
) {
331 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_BYTES
));
333 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_ISTRING
));
334 if ((m_flags
& stShowPercent
) && m_parent
) {
335 CECTag
tmp(EC_TAG_STAT_NODE_VALUE
,
336 ((double)m_value
/ ((CStatTreeItemCounterTmpl
<_Tp
>*)m_parent
)->m_value
) * 100.0);
337 tmp
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_DOUBLE
));
344 /* CStatTreeItemUlDlCounter */
347 wxString
CStatTreeItemUlDlCounter::GetDisplayString() const
349 return CFormat(wxGetTranslation(m_label
)) %
350 a_brackets_b(CastItoXBytes(m_value
), CastItoXBytes(m_value
+ m_totalfunc()));
354 void CStatTreeItemUlDlCounter::AddECValues(CECTag
*tag
) const
356 CECTag
value(EC_TAG_STAT_NODE_VALUE
, m_value
);
357 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_BYTES
));
358 CECTag
tmp(EC_TAG_STAT_NODE_VALUE
, m_value
+ m_totalfunc());
359 tmp
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_BYTES
));
365 /* CStatTreeItemCounterMax */
368 wxString
CStatTreeItemCounterMax::GetDisplayString() const
370 return wxString::Format(wxGetTranslation(m_label
), m_value
);
374 void CStatTreeItemCounterMax::AddECValues(CECTag
*tag
) const
376 tag
->AddTag(CECTag(EC_TAG_STAT_NODE_VALUE
, (uint64
)m_value
));
380 /* CStatTreeItemPackets */
383 wxString
CStatTreeItemPackets::GetDisplayString() const
385 return CFormat(wxGetTranslation(m_label
)) %
386 a_brackets_b(CastItoXBytes(m_bytes
), CastItoIShort(m_packets
));
390 void CStatTreeItemPackets::AddECValues(CECTag
*tag
) const
392 CECTag
value(EC_TAG_STAT_NODE_VALUE
, m_bytes
);
393 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_BYTES
));
394 CECTag
tmp(EC_TAG_STAT_NODE_VALUE
, (uint64
)m_packets
);
395 tmp
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_ISHORT
));
401 /* CStatTreeItemPacketTotals */
404 wxString
CStatTreeItemPacketTotals::GetDisplayString() const
406 uint32_t tmp_packets
= m_packets
;
407 uint64_t tmp_bytes
= m_bytes
;
408 for (std::vector
<CStatTreeItemPackets
*>::const_iterator it
= m_counters
.begin();
409 it
!= m_counters
.end(); ++it
) {
410 tmp_packets
+= (*it
)->m_packets
;
411 tmp_bytes
+= (*it
)->m_bytes
;
414 return CFormat(wxGetTranslation(m_label
)) %
415 a_brackets_b(CastItoXBytes(tmp_bytes
), CastItoIShort(tmp_packets
));
419 void CStatTreeItemPacketTotals::AddECValues(CECTag
*tag
) const
421 uint32_t tmp_packets
= m_packets
;
422 uint64_t tmp_bytes
= m_bytes
;
423 for (std::vector
<CStatTreeItemPackets
*>::const_iterator it
= m_counters
.begin();
424 it
!= m_counters
.end(); ++it
) {
425 tmp_packets
+= (*it
)->m_packets
;
426 tmp_bytes
+= (*it
)->m_bytes
;
429 CECTag
value(EC_TAG_STAT_NODE_VALUE
, tmp_bytes
);
430 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_BYTES
));
431 CECTag
tmp(EC_TAG_STAT_NODE_VALUE
, (uint64
)tmp_packets
);
432 tmp
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_ISHORT
));
438 /* CStatTreeItemTimer */
441 wxString
CStatTreeItemTimer::GetDisplayString() const
443 return CFormat(wxGetTranslation(m_label
)) %
444 CastSecondsToHM(m_value
? GetTimerSeconds() : 0);
448 void CStatTreeItemTimer::AddECValues(CECTag
*tag
) const
450 CECTag
value(EC_TAG_STAT_NODE_VALUE
,
451 m_value
? (uint32
)GetTimerSeconds() : (uint32
)0);
452 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_TIME
));
457 /* CStatTreeItemAverage */
460 wxString
CStatTreeItemAverage::GetDisplayString() const
462 if ((*m_divisor
) != 0) {
463 switch (m_displaymode
) {
465 return CFormat(wxGetTranslation(m_label
)) %
466 CastItoXBytes((*m_dividend
)/(*m_divisor
));
468 return CFormat(wxGetTranslation(m_label
)) %
469 CastSecondsToHM((*m_dividend
)/(*m_divisor
));
471 return CFormat(wxGetTranslation(m_label
)) %
472 (CFormat(wxT("%u")) % ((uint64
)(*m_dividend
)/(*m_divisor
))).GetString();
475 return CFormat(wxGetTranslation(m_label
)) % wxT("-");
480 void CStatTreeItemAverage::AddECValues(CECTag
*tag
) const
482 if ((*m_divisor
) != 0) {
483 uint64 data
= (*m_dividend
)/(*m_divisor
);
484 if (m_displaymode
== dmTime
) {
485 CECTag
value(EC_TAG_STAT_NODE_VALUE
, (uint32
)data
);
486 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_TIME
));
489 CECTag
value(EC_TAG_STAT_NODE_VALUE
, data
);
490 if (m_displaymode
== dmBytes
) {
491 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_BYTES
));
496 CECTag
value(EC_TAG_STAT_NODE_VALUE
, wxString(wxT("-")));
497 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_STRING
));
503 /* CStatTreeItemAverageSpeed */
506 wxString
CStatTreeItemAverageSpeed::GetDisplayString() const
508 uint64 time
= m_timer
->GetTimerSeconds();
510 return CFormat(wxGetTranslation(m_label
)) % CastItoSpeed((*m_counter
)/time
);
512 return CFormat(wxGetTranslation(m_label
)) % CastItoSpeed(0);
517 void CStatTreeItemAverageSpeed::AddECValues(CECTag
*tag
) const
519 uint64 time
= m_timer
->GetTimerSeconds();
521 CECTag
value(EC_TAG_STAT_NODE_VALUE
, (uint32
)((*m_counter
)/time
));
522 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_SPEED
));
525 CECTag
value(EC_TAG_STAT_NODE_VALUE
, (uint32
)0);
526 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_SPEED
));
532 /* CStatTreeItemRatio */
535 wxString
CStatTreeItemRatio::GetDisplayString() const
537 if (m_counter1
->GetValue() && m_counter2
->GetValue()) {
538 if ((*m_counter2
) < (*m_counter1
)) {
539 return CFormat(wxGetTranslation(m_label
)) %
540 wxString::Format(wxT("%.2f : 1"),
541 (float)(*m_counter1
)/(*m_counter2
));
543 return CFormat(wxGetTranslation(m_label
)) %
544 wxString::Format(wxT("1 : %.2f"),
545 (float)(*m_counter2
)/(*m_counter1
));
548 return CFormat(wxGetTranslation(m_label
)) % _("Not available");
553 void CStatTreeItemRatio::AddECValues(CECTag
*tag
) const
556 if (m_counter1
->GetValue() && m_counter2
->GetValue()) {
557 if ((*m_counter2
) < (*m_counter1
)) {
558 result
= wxString::Format(wxT("%.2f : 1"), (float)(*m_counter1
)/(*m_counter2
));
560 result
= wxString::Format(wxT("1 : %.2f"), (float)(*m_counter2
)/(*m_counter1
));
563 result
= wxTRANSLATE("Not available");
566 CECTag
value(EC_TAG_STAT_NODE_VALUE
, result
);
567 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_STRING
));
572 /* CStatTreeItemReconnects */
575 wxString
CStatTreeItemReconnects::GetDisplayString() const
577 return CFormat(wxGetTranslation(m_label
)) % (m_value
? m_value
- 1 : 0);
581 void CStatTreeItemReconnects::AddECValues(CECTag
*tag
) const
583 tag
->AddTag(CECTag(EC_TAG_STAT_NODE_VALUE
,
584 m_value
? (uint64
)(m_value
- 1) : (uint64
)0));
587 /* CStatTreeItemMaxConnLimitReached */
590 wxString
CStatTreeItemMaxConnLimitReached::GetDisplayString() const
593 return CFormat(wxGetTranslation(m_label
)) %
594 (wxString::Format(wxT("%i : "), m_count
) +
595 m_time
.FormatISODate() +
597 m_time
.FormatISOTime());
599 return CFormat(wxGetTranslation(m_label
)) % _("Never");
604 void CStatTreeItemMaxConnLimitReached::AddECValues(CECTag
*tag
) const
608 result
= wxString::Format(wxT("%i : "), m_count
) +
609 m_time
.FormatISODate() +
611 m_time
.FormatISOTime();
613 result
= wxTRANSLATE("Never");
615 CECTag
value(EC_TAG_STAT_NODE_VALUE
, result
);
616 value
.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE
, (uint8
)EC_VALUE_STRING
));
620 /* CStatTreeItemTotalClients */
623 wxString
CStatTreeItemTotalClients::GetDisplayString() const
625 return CFormat(wxGetTranslation(m_label
)) %
626 (m_known
->GetValue() + m_unknown
->GetValue()) %
631 void CStatTreeItemTotalClients::AddECValues(CECTag
*tag
) const
633 CECTag
value1(EC_TAG_STAT_NODE_VALUE
,
634 (uint64
)(m_known
->GetValue() + m_unknown
->GetValue()));
636 CECTag
value2(EC_TAG_STAT_NODE_VALUE
,
637 (uint64
)m_known
->GetValue());
641 #endif /* !EC_REMOTE */
642 // File_checked_for_headers