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
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
//
// This file is part of the aMule Project.
//
// Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
// Copyright (c) 2005-2011 Dévai Tamás ( gonosztopi@amule.org )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
//

#ifndef STATTREE_H
#define STATTREE_H

/**
 * @file StatTree.h
 *
 * Interface to stat tree nodes.
 *
 * This file defines various classes representing
 * statistics tree nodes.
 */


#ifndef CLIENT_GUI
#	define VIRTUAL virtual
#else
#	define VIRTUAL
#endif


#include <list>			// Needed for std::list
#include <wx/string.h>		// Needed for wxString
#include <wx/thread.h>		// Needed for wxMutex
#include "Types.h"

#ifndef CLIENT_GUI

#include <wx/datetime.h>	// Needed for wxDateTime
#include "GetTickCount.h"	// Needed for GetTickCount64()


/**
 * Stat tree flags
 */
enum EStatTreeFlags
{
	stNone		= 0,		///< Nothing. Really.
	stSortChildren	= 1,		///< Childrens are sorted descending by their ID.
	stShowPercent	= 2,		/*!< Shows percentage compared to parent.
					 *   Counters only, whose parent is also counter! (khmm...)
					 */
	stHideIfZero	= 4,		///< Hides item (and children) if value is zero.
	stSortByValue	= 8,		/*!< Together with stSortChildren, sorts children by their value.
					 *   WARNING! This assumes that value-sorted children are
					 *   counters!
					 *   Sort-by-value works only on children with ID between 0x00000100-0x7fffffff.
					 *   @note CStatTreeItemBase::ReSortChildren() must be called to get sort order right.
					 */
	stCapChildren	= 16		///< Caps children list.
					/*!< Shows only top n children, where n is set by CStatisticsDlg::FillTree() to thePrefs::GetMaxClientVersions().
					 *   The list itself is not changed, only visibility is ignored on items
					 *   outside the top n visible.
					 *
					 *   On an EC request, visibility range can be set with the EC_TAG_STATTREE_CAPPING tag.
					 */
};

enum EValueType
{
	vtUnknown,
	vtString,
	vtInteger,
	vtFloat
};

/**
 * Display modes for Simple and Counter items
 */
enum EDisplayMode
{
	dmDefault,		///< Default display mode.
	dmTime,			///< Treat integer value as time in seconds.
	dmBytes			///< Treat integer value as bytes count.
};

#endif /* !CLIENT_GUI */


class CStatTreeItemBase;
typedef std::list<CStatTreeItemBase*>::iterator	StatTreeItemIterator;


class CECTag;

uint32_t NewStatTreeItemId();


/**
 * Base tree item class
 */
class CStatTreeItemBase
{
public:

#ifndef CLIENT_GUI
	/**
	 * Creates an item with a constant label.
	 *
	 * @param label	Visible text for the item.
	 * @param flags Flags to use.
	 */
	CStatTreeItemBase(const wxString &label, unsigned flags = stNone)
		: m_label(label), m_parent(NULL), m_flags(flags), m_id(0),
		m_visible_counter(0), m_uniqueid(NewStatTreeItemId())
		{}
#else
	/**
	 * Creates an item with a constant label.
	 *
	 * @param label	Visible text for the item.
	 */
	CStatTreeItemBase(const wxString &label, uint32_t uniqueid)
		: m_label(label), m_uniqueid(uniqueid)
		{}

	/**
	 * Creates an item (and the whole subtree) from an EC tag.
	 *
	 * @param tag EC tag containing a stat subtree.
	 */
	CStatTreeItemBase(const CECTag *tag);
#endif

	/**
	 * Deletes all children.
	 */
	VIRTUAL	~CStatTreeItemBase();

#ifndef CLIENT_GUI
	/**
	 * Adds a new child node.
	 *
	 * @param child child to add.
	 * @param id	an optional ID for the new item.
	 * @param skipOneLevel activates a trick to let the code work for aMule OSInfo and Version trees.
	 *
	 * @return the newly added item.
	 */
	CStatTreeItemBase *AddChild(
		CStatTreeItemBase* child,
		uint32_t id = 0,
		bool skipOneLevel = false);
#endif

	/**
	 * Check for children.
	 *
	 * @return true if this node has children, false otherwise.
	 */
	bool HasChildren() { wxMutexLocker lock(m_lock); return !m_children.empty(); }

	/**
	 * Check for visible children.
	 *
	 * @return true if this node has children and at least one of them is visible.
	 */
	bool HasVisibleChildren();

#ifndef CLIENT_GUI

	/**
	 * Check for a given child.
	 *
	 * @return true if this node has a child with the given ID.
	 */
	bool HasChildWithId(uint32_t id);

	/**
	 * Access a specific child.
	 *
	 * @return the child with the given ID, or NULL if not found.
	 */
	CStatTreeItemBase *GetChildById(uint32_t id);

	/**
	 * Get the first visible child.
	 *
	 * @param max_children The maximum number of children to show, when the stCapChildren flag is set. Otherwise it has no effect. (0 = unlimited)
	 *
	 * @return An iterator, that should be passed to GetNextVisibleChild() and IsAtEndOfList().
	 */
	StatTreeItemIterator GetFirstVisibleChild(uint32_t max_children);

	/**
	 * Get the next visible child.
	 */
	void GetNextVisibleChild(StatTreeItemIterator& it);

#else /* CLIENT_GUI */

	/**
	 * Get the first visible child.
	 *
	 * @return An iterator, that should be passed to GetNextVisibleChild() and IsAtEndOfList().
	 *
	 * @note On a remote list every item is visible.
	 */
	StatTreeItemIterator GetFirstVisibleChild() { return m_children.begin(); }

	/**
	 * Get the next visible child.
	 *
	 * @note On a remote list every item is visible.
	 */
	void GetNextVisibleChild(StatTreeItemIterator& it) { ++it; }

#endif /* !CLIENT_GUI / CLIENT_GUI */

	/**
	 * Check if we are past the end of child list.
	 */
	bool IsAtEndOfList(StatTreeItemIterator& it) { return it == m_children.end(); }<--- Parameter 'it' can be declared as reference to const

#ifndef CLIENT_GUI
	/**
	 * Resorts children for the stSortByValue flag.
	 */
	void ReSortChildren() { wxMutexLocker lock(m_lock); m_children.sort(ValueSort); }
#endif

#ifndef AMULE_DAEMON
#ifndef CLIENT_GUI
	/**
	 * Returns a string that will be displayed on the GUI tree.
	 */
	virtual	wxString GetDisplayString() const;<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class
#else
	/**
	 * Returns the associated text (GUI item label).
	 */
	const wxString& GetDisplayString() const { return m_label; }
#endif /* !CLIENT_GUI / CLIENT_GUI */

	/**
	 * Returns the mutex used to lock the child list of this node.
	 *
	 * This function is used by CStatisticsDlg to be able to lock the
	 * core tree while updating the GUI tree.
	 */
	wxMutex& GetLock() { return m_lock; }

	/**
	 * Returns the unique ID of this node.
	 */
	uint32_t GetUniqueId() const { return m_uniqueid; }
#endif /* !AMULE_DAEMON */

	/**
	 * Check whether this node is visible.
	 */
	VIRTUAL	bool IsVisible() const { return true; }<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class

#ifndef CLIENT_GUI
	/**
	 * Create an EC tag from this node (and children).
	 *
	 * @param max_children The maximum number of children to show, when the stCapChildren flag is set. Otherwise it has no effect. (0 = unlimited)
	 *
	 * @return A EC tag containing this node and all its children.
	 */
	virtual	CECTag *CreateECTag(uint32_t max_children);
#endif

protected:

#ifndef CLIENT_GUI
	/**
	 * Add values to the EC tag being generated.
	 *
	 * Should have a real implementation in children which have some value.
	 * The given parameter is the tag to which values should be added.
	 */
	virtual	void AddECValues(CECTag*) const {}<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class
#endif

	//! Unformatted and untranslated label of the node. Note: On remote gui it is already formatted and translated.
	const wxString m_label;
#ifndef CLIENT_GUI

	//! Parent of this node.
	CStatTreeItemBase *m_parent;

	//! Flags for the node.
	unsigned m_flags;
#endif

private:

#ifndef CLIENT_GUI
	//! Function used when sorting children by value.
	static bool ValueSort(const CStatTreeItemBase* a, const CStatTreeItemBase* b);

	//! ID of this node.
	uint32_t m_id;

	//! Counter to keep track of displayed visible items
	// (needed for the stCapChildren flag)
	uint32_t m_visible_counter;
#endif

	//! Unique ID of this node.
	uint32_t m_uniqueid;

	//! Children of this node.
	std::list<CStatTreeItemBase*>	m_children;

	//! Lock to protect list from simultaneous access.
	wxMutex m_lock;
};


//
// Anything below is only for core.
//
#ifndef CLIENT_GUI

/**
 * Simple tree item.
 *
 * This tree item has one value and nothing speciality. :)
 * The value might be an arbitrary integer or floating point type,
 * or a wxString string.
 *
 * The item is able to display value in different formats, see SetDisplayMode().
 *
 * @note that you have to specify the right format code on 'label', i.e.:
 * %s for string and integers with displayMode dmTime or dmBytes,
 * %u or similar for integers, and
 * %f or similar for floating point types.
 *
 * @note You have to call SetValue() after creation for non-integer values, otherwise
 * you'll get undesired results.
 */
class CStatTreeItemSimple : public CStatTreeItemBase
{
public:

	/**
	 * Constructor.
	 *
	 * @see CStatTreeItemBase::CStatTreeItemBase
	 */
	CStatTreeItemSimple(
		const wxString &label,
		unsigned flags = stNone,
		enum EDisplayMode displaymode = dmDefault)
	:
	CStatTreeItemBase(label, flags),
	m_valuetype(vtUnknown),
	m_displaymode(displaymode)
	{
		SetValue((uint64_t)0);
	}

	/**
	 * Sets the desired display mode of value.
	 */
	void SetDisplayMode(enum EDisplayMode mode)
	{
		m_displaymode = mode;
	}

	/**
	 * Sets an integer type value.
	 *
	 * @param value the value to be set.
	 */
	void SetValue(uint64_t value)
	{
		m_valuetype = vtInteger;
		m_intvalue = value;
	}

	/**
	 * Sets a floating point type value.
	 *
	 * @param value the value to be set.
	 */
	void SetValue(double value)
	{
		m_valuetype = vtFloat;
		m_floatvalue = value;
	}

	/**
	 * Sets a string type value.
	 *
	 * @param value the value to be set.
	 */
	void SetValue(const wxString& value)
	{
		m_valuetype = vtString;
		m_stringvalue = value;
	}

#ifndef AMULE_DAEMON
	/**
	 * @see CStatTreeItemBase::GetDisplayString()
	 */
	virtual	wxString GetDisplayString() const;<--- Function in derived class
#endif

	/**
	 * @see CStatTreeItemBase::IsVisible()
	 */
	virtual	bool IsVisible() const;<--- Function in derived class

protected:
	/**
	 * Add values to EC tag being generated.
	 *
	 * @param tag The tag to which values should be added.
	 *
	 * @see CStatTreeItemBase::AddECValues
	 */
	virtual	void AddECValues(CECTag *tag) const;<--- Function in derived class

	//! Type of the value.
	enum EValueType m_valuetype;
	//! Display mode of the value.
	enum EDisplayMode m_displaymode;
	//! Union to save space.
	union
	{
		uint64_t m_intvalue;	///< Integer value.
		double   m_floatvalue;	///< Floating point value.
	};
	wxString m_stringvalue;	///< String value.
};


/**
 * Counter-type tree item template.
 *
 * Able to show percentage compared to parent, hide itself
 * when value is zero, and nice functions for changing the value.
 * stShowPercent and stHideIfZero flags take effect only on
 * this node.
 */
template<typename _Tp>
class CStatTreeItemCounterTmpl : public CStatTreeItemBase
{
public:
	/**
	 * Constructor.
	 *
	 * @see CStatTreeItemBase::CStatTreeItemBase
	 */
	CStatTreeItemCounterTmpl(
		const wxString &label,
		unsigned flags = stNone)
	:
	CStatTreeItemBase(label, flags),
	m_value(0),
	m_displaymode(dmDefault) {}

	/**
	 * Retrieve counter value.
	 */
	_Tp GetValue() const { return m_value; }

	/**
	 * Retrieve counter value.
	 */
	operator _Tp() const { return m_value; }

	/**
	 * Set counter to given value.
	 */
	void SetValue(_Tp value) { m_value = value; }

	/**
	 * Set counter to given value.
	 */
	void operator=(_Tp value) { m_value = value; }

	/**
	 * Increase value by 1.
	 */
	void operator++() { ++m_value; }

	/**
	 * Decrease value by 1.
	 */
	void operator--() { --m_value; }

	/**
	 * Increase value by given amount.
	 */
	void operator+=(_Tp value) { m_value += value; }

	/**
	 * Decrease value by given amount.
	 */
	void operator-=(_Tp value) { m_value -= value; }

	/**
	 * Sets the desired display mode of value.
	 */
	void SetDisplayMode(enum EDisplayMode mode) { m_displaymode = mode; }

#ifndef AMULE_DAEMON
	/**
	 * @see CStatTreeItemBase::GetDisplayString()
	 */
	virtual	wxString GetDisplayString() const;<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Function in derived class
#endif

	/**
	 * @see CStatTreeItemBase::IsVisible()
	 */
	virtual	bool IsVisible() const<--- Virtual function in base class<--- Function in derived class
	{
		return (m_flags & stHideIfZero) ? (m_value != 0) : true;
	}

protected:
	/**
	 * Add values to EC tag being generated.
	 *
	 * @param tag The tag to which values should be added.
	 *
	 * @see CStatTreeItemBase::AddECValues
	 */
	virtual	void AddECValues(CECTag *tag) const;<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Function in derived class

	//! Actual value of the counter.
	_Tp m_value;

	//! Display mode of the value.
	enum EDisplayMode m_displaymode;
};

typedef CStatTreeItemCounterTmpl<uint64_t>	CStatTreeItemCounter;
typedef CStatTreeItemCounterTmpl<uint32_t>	CStatTreeItemNativeCounter;


/**
 * A counter, which does not display its value :P
 */
class CStatTreeItemHiddenCounter : public CStatTreeItemCounter
{
public:
	/**
	 * Constructor.
	 *
	 * @see CStatTreeItemCounter::CStatTreeItemCounter
	 */
	CStatTreeItemHiddenCounter(
		const wxString &label,
		unsigned flags = stNone)
	:
	CStatTreeItemCounter(label, flags) {}

#ifndef AMULE_DAEMON
	/**
	 * @see CStatTreeItemBase::GetDisplayString()
	 */
	virtual	wxString GetDisplayString() const<--- Function in derived class
	{
		return CStatTreeItemBase::GetDisplayString();
	}
#endif

	/**
	 * @see CStatTreeItemBase::IsVisible()
	 */
	virtual bool IsVisible() const { return true; }<--- Function in derived class

protected:
	//! Do nothing here.
	virtual	void AddECValues(CECTag*) const {}<--- Function in derived class
};


/**
 * Item for the session/total upload/download counter
 */
class CStatTreeItemUlDlCounter : public CStatTreeItemCounter
{
public:
	/**
	 * @param label     format text for item.
	 * @param totalfunc function that will return the totals.
	 */
	CStatTreeItemUlDlCounter(
		const wxString &label,
		uint64_t (*totalfunc)(),
		unsigned flags = stNone)
	:
	CStatTreeItemCounter(label, flags),
	m_totalfunc(totalfunc) {}

#ifndef AMULE_DAEMON
	/**
	 * @see CStatTreeBase::GetDisplayString()
	 */
	virtual	wxString GetDisplayString() const;<--- Function in derived class
#endif

protected:
	/**
	 * Add values to EC tag being generated.
	 *
	 * @param tag The tag to which values should be added.
	 *
	 * @see CStatTreeItemBase::AddECValues
	 */
	virtual	void AddECValues(CECTag *tag) const;<--- Function in derived class

	//! A function whose return value is the total (without current) value.
	uint64_t (*m_totalfunc)();
};


/**
 * Counter-like tree item which remembers its max value.
 *
 * Used for active connections counter, to be able to get peak connections.
 */
class CStatTreeItemCounterMax : public CStatTreeItemBase
{
public:
	/**
	 * @see CStatTreeItemBase::CStatTreeItemBase
	 */
	CStatTreeItemCounterMax(const wxString &label)
	:
	CStatTreeItemBase(label),
	m_value(0),
	m_max_value(0)
	{}

	/**
	 * Increase value
	 */
	void operator++()
	{
		if (++m_value > m_max_value) {
			m_max_value = m_value;
		}
	}

	/**
	 * Decrease value
	 */
	void operator--() { --m_value; }

	/**
	 * Retrieve actual value
	 */
	uint32_t GetValue() { return m_value; }

	/**
	 * Retrieve max value
	 */
	uint32_t GetMaxValue() { return m_max_value; }

#ifndef AMULE_DAEMON
	/**
	 * @see CStatTreeItemBase::GetDisplayString()
	 */
	virtual	wxString GetDisplayString() const;<--- Function in derived class
#endif

protected:
	/**
	 * Add values to EC tag being generated.
	 *
	 * @param tag The tag to which values should be added.
	 *
	 * @see CStatTreeItemBase::AddECValues
	 */
	virtual	void AddECValues(CECTag *tag) const;<--- Function in derived class

	//! Actual value of the counter.
	uint32_t m_value;

	//! Maximal value the counter has ever reached.
	uint32_t m_max_value;
};


/**
 * Tree item for counting packets
 */
class CStatTreeItemPackets : public CStatTreeItemBase
{
	friend class CStatTreeItemPacketTotals;

public:
	/**
	 * @see CStatTreeItemBase::CStatTreeItemBase
	 */
	CStatTreeItemPackets(const wxString &label)
	:
	CStatTreeItemBase(label, stNone),
	m_packets(0),
	m_bytes(0) {}

	/**
	 * Add a packet of size 'size'.
	 */
	void operator+=(long size)
	{
		++m_packets;
		m_bytes += size;
	}

#ifndef AMULE_DAEMON
	/**
	 * @see CStatTreeItemBase::GetDisplayString()
	 */
	virtual	wxString GetDisplayString() const;<--- Function in derived class<--- Virtual function in base class
#endif

protected:
	/**
	 * Add values to EC tag being generated.
	 *
	 * @param tag The tag to which values should be added.
	 *
	 * @see CStatTreeItemBase::AddECValues
	 */
	virtual	void AddECValues(CECTag *tag) const;<--- Function in derived class<--- Virtual function in base class

	//! Total number of packets.
	uint32_t m_packets;

	//! Total bytes in the packets.
	uint64_t m_bytes;
};


/**
 * Tree item for counting totals on packet counters.
 *
 * This item sums up a number of packet counters, plus adds its own values.
 */
class CStatTreeItemPacketTotals : public CStatTreeItemPackets
{
public:
	/**
	 * @see CStatTreeItemPackets::CStatTreeItemPackets
	 */
	CStatTreeItemPacketTotals(const wxString &label)
	:
	CStatTreeItemPackets(label) {}

	/**
	 * Adds a packet counter, whose values should be counted in the totals.
	 */
	void AddPacketCounter(CStatTreeItemPackets* counter)
	{
		m_counters.push_back(counter);
	}

#ifndef AMULE_DAEMON
	/**
	 * @see CStatTreeItemPackets::GetDisplayString()
	 */
	virtual	wxString GetDisplayString() const;<--- Function in derived class
#endif

protected:
	/**
	 * Add values to EC tag being generated.
	 *
	 * @param tag The tag to which values should be added.
	 *
	 * @see CStatTreeItemBase::AddECValues
	 */
	virtual	void AddECValues(CECTag *tag) const;<--- Function in derived class

	//! List of packet counters to sum.
	std::vector<CStatTreeItemPackets*> m_counters;
};


/**
 * Tree item for timer type nodes.
 */
class CStatTreeItemTimer : public CStatTreeItemBase
{
public:

	/**
	 * @see CStatTreeItemBase::CStatTreeItemBase
	 */
	CStatTreeItemTimer(
		const wxString &label,
		unsigned flags = stNone)
	:
	CStatTreeItemBase(label, flags),
	m_value(0) {}

	/**
	 * Sets timer start time (and thus starts timer).
	 */
	void SetStartTime(uint64_t value) { m_value = value; }

	/**
	 * Starts the timer if it's not running.
	 */
	void StartTimer() { if (!m_value) m_value = GetTickCount64(); }

	/**
	 * Stops the timer.
	 */
	void StopTimer() { m_value = 0; }

	/**
	 * Check whether the timer is running.
	 */
	bool IsRunning() const { return m_value != 0; }

	/**
	 * Reset timer unconditionally.
	 */
	void ResetTimer() { m_value = GetTickCount64(); }

	/**
	 * Get timer value.
	 */
	uint64_t GetTimerValue() const
	{
		return m_value ? GetTickCount64() - m_value : 0;
	}

	/**
	 * Get timer value (in ticks).
	 */
	operator uint64_t() const
	{
		return m_value ? GetTickCount64() - m_value : 0;
	}

	/**
	 * Get elapsed time in seconds.
	 */
	uint64_t GetTimerSeconds() const
	{
		return m_value ? (GetTickCount64() - m_value) / 1000 : 0;
	}

	/**
	 * Get start time of the timer.
	 */
	uint64_t GetTimerStart() const { return m_value; }

#ifndef AMULE_DAEMON
	/**
	 * @see CStatTreeItemBase::GetDisplayString()
	 */
	virtual	wxString GetDisplayString() const;<--- Function in derived class
#endif

	/**
	 * @see CStatTreeItemBase::IsVisible()
	 */
	virtual	bool IsVisible() const<--- Function in derived class
	{
		return (m_flags & stHideIfZero) ? m_value != 0 : true;
	}

protected:
	/**
	 * Add values to EC tag being generated.
	 *
	 * @param tag The tag to which values should be added.
	 *
	 * @see CStatTreeItemBase::AddECValues
	 */
	virtual	void AddECValues(CECTag *tag) const;<--- Function in derived class

	//! Tick count value when timer was started.
	uint64_t m_value;
};


/**
 * Tree item for shared files average size.
 *
 * Average is counted as dividend / divisor, if divisor is non-zero.
 */
class CStatTreeItemAverage : public CStatTreeItemBase
{
public:
	/**
	 * @see CStatTreeItemBase::CStatTreeItemBase
	 *
	 * @param dividend What to divide.
	 * @param divisor Divide by what.
	 */
	CStatTreeItemAverage(
		const wxString &label,
		const CStatTreeItemCounter *dividend,
		const CStatTreeItemCounter *divisor,
		enum EDisplayMode displaymode)
	:
	CStatTreeItemBase(label, stNone),
	m_dividend(dividend),
	m_divisor(divisor),
	m_displaymode(displaymode) {}

#ifndef AMULE_DAEMON
	/**
	 * @see CStatTreeItemBase::GetDisplayString()
	 */
	virtual	wxString GetDisplayString() const;<--- Function in derived class
#endif

	/**
	 * @see CStatTreeItemBase::IsVisible()
	 */
	virtual	bool IsVisible() const	{ return (*m_divisor) != 0; }<--- Function in derived class

protected:
	/**
	 * Add values to EC tag being generated.
	 *
	 * @param tag The tag to which values should be added.
	 *
	 * @see CStatTreeItemBase::AddECValues
	 */
	virtual	void AddECValues(CECTag *tag) const;<--- Function in derived class

	//! What to divide.
	const CStatTreeItemCounter *m_dividend;

	//! Divide by what.
	const CStatTreeItemCounter *m_divisor;

	//! Display mode.
	enum EDisplayMode m_displaymode;
};


/**
 * Tree item for average up/down speed.
 */
class CStatTreeItemAverageSpeed : public CStatTreeItemBase
{
public:

	/**
	 * @see CStatTreeItemBase::CStatTreeItemBase
	 *
	 * @param counter Session up/down counter.
	 * @param timer Session uptime timer.
	 */
	CStatTreeItemAverageSpeed(
		const wxString &label,
		const CStatTreeItemUlDlCounter *counter,
		const CStatTreeItemTimer *timer)
	:
	CStatTreeItemBase(label, stNone),
	m_counter(counter),
	m_timer(timer) {}

#ifndef AMULE_DAEMON
	/**
	 * @see CStatTreeItemBase::GetDisplayString()
	 */
	virtual	wxString GetDisplayString() const;<--- Function in derived class
#endif

protected:
	/**
	 * Add values to EC tag being generated.
	 *
	 * @param tag The tag to which values should be added.
	 *
	 * @see CStatTreeItemBase::AddECValues
	 */
	virtual	void AddECValues(CECTag *tag) const;<--- Function in derived class

	//! Session sent/received bytes counter.
	const CStatTreeItemUlDlCounter *m_counter;

	//! Session uptime.
	const CStatTreeItemTimer *m_timer;
};


/**
 * Tree item for displaying ratio between two counters.
 *
 * Ratio is counted as counter1:counter2.
 */
class CStatTreeItemRatio : public CStatTreeItemBase
{
public:
	/**
	 * @see CStatTreeItemBase::CStatTreeItemBase
	 *
	 * @param cnt1 First counter to use.
	 * @param cnt2 Second counter to use.
	 */
	CStatTreeItemRatio(
		const wxString &label,
		const CStatTreeItemCounter *cnt1,
		const CStatTreeItemCounter* cnt2,
		uint64_t (*totalfunc1)() = NULL,
		uint64_t (*totalfunc2)() = NULL)
	:
	CStatTreeItemBase(label, stNone),
	m_counter1(cnt1),
	m_counter2(cnt2),
	m_totalfunc1(totalfunc1),
	m_totalfunc2(totalfunc2){}

#ifndef AMULE_DAEMON
	/**
	 * @see CStatTreeItemBase::GetDisplayString()
	 */
	virtual	wxString GetDisplayString() const;<--- Function in derived class
#endif

protected:
	/**
	 * Add values to EC tag being generated.
	 *
	 * @param tag The tag to which values should be added.
	 *
	 * @see CStatTreeItemBase::AddECValues
	 */
	virtual	void AddECValues(CECTag *tag) const;<--- Function in derived class

	//! First counter.
	const CStatTreeItemCounter *m_counter1;

	//! Second counter.
	const CStatTreeItemCounter *m_counter2;

	//! A function for each whose return value is the total (without current) value.
	uint64_t (*m_totalfunc1)();
	uint64_t (*m_totalfunc2)();

private:
	//! Formatted String for display or EC
	wxString GetString() const;
};


/**
 * Special counter for reconnects.
 */
class CStatTreeItemReconnects : public CStatTreeItemNativeCounter {
public:
	/**
	 * @see CStatTreeItemBase::CStatTreeItemBase
	 */
	CStatTreeItemReconnects(const wxString &label)
	:
	CStatTreeItemNativeCounter(label, stNone) {}

#ifndef AMULE_DAEMON
	/**
	 * @see CStatTreeItemBase::GetDisplayString()
	 */
	virtual	wxString GetDisplayString() const;<--- Function in derived class
#endif

protected:
	/**
	 * Add values to EC tag being generated.
	 *
	 * @param tag The tag to which values should be added.
	 *
	 * @see CStatTreeItemBase::AddECValues
	 */
	virtual	void AddECValues(CECTag *tag) const;<--- Function in derived class
};

/**
 * Special item for Max Connection Limit Reached
 */
class CStatTreeItemMaxConnLimitReached : public CStatTreeItemBase
{
public:
	/**
	 * @see CStatTreeItemBase::CStatTreeItemBase
	 */
	CStatTreeItemMaxConnLimitReached(const wxString &label)
	:
	CStatTreeItemBase(label),
	m_count(0) {}

	/**
	 * Increase counter and save time.
	 */
	void operator++()
	{
		++m_count;
		m_time.SetToCurrent();
	}

#ifndef AMULE_DAEMON
	/**
	 * Returns a string to be displayed on GUI.
	 *
	 * For m_count == 0 it will display "Never",
	 * for other values it will display the counter value and the
	 * date & time of the event.
	 */
	virtual	wxString GetDisplayString() const;<--- Function in derived class
#endif

protected:
	/**
	 * Add values to EC tag being generated.
	 *
	 * @param tag The tag to which values should be added.
	 *
	 * @see CStatTreeItemBase::AddECValues
	 */
	virtual	void AddECValues(CECTag *tag) const;<--- Function in derived class

	//! Number of times max conn limit reached.
	uint32_t m_count;

	//! Last time when max conn limit reached.
	wxDateTime m_time;
};

/**
 * Special item for total client count
 */
class CStatTreeItemTotalClients : public CStatTreeItemBase
{
public:
	/**
	 * @see CStatTreeItemBase::CStatTreeItemBase
	 *
	 * @param known Counter that counts known clients.
	 * @param unknown Counter that counts unknown clients.
	 */
	CStatTreeItemTotalClients(
		const wxString &label,
		const CStatTreeItemCounter *known,
		const CStatTreeItemCounter *unknown)
	:
	CStatTreeItemBase(label),
	m_known(known),
	m_unknown(unknown) {}

#ifndef AMULE_DAEMON
	/**
	 * @see CStatTreeItemBase::GetDisplayString()
	 */
	virtual	wxString GetDisplayString() const;<--- Function in derived class
#endif

protected:
	/**
	 * Add values to EC tag being generated.
	 *
	 * @param tag The tag to which values should be added.
	 *
	 * @see CStatTreeItemBase::AddECValues
	 */
	virtual	void AddECValues(CECTag *tag) const;<--- Function in derived class

	//! Counter counting known clients.
	const CStatTreeItemCounter *m_known;

	//! Counter counting unknown clients.
	const CStatTreeItemCounter *m_unknown;
};

#endif /* !CLIENT_GUI */

#endif /* STATTREE_H */
// File_checked_for_headers