KNemo – network interfaces monitor for systray
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

193 lines
5.4 KiB

  1. /* This file is part of KNemo
  2. Copyright (C) 2005, 2006 Percy Leonhardt <percy@eris23.de>
  3. KNemo is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of
  6. the License, or (at your option) any later version.
  7. KNemo is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. Boston, MA 02110-1301, USA.
  15. */
  16. #ifndef INTERFACESTATISTICS_H
  17. #define INTERFACESTATISTICS_H
  18. #include <tqobject.h>
  19. #include <tqptrlist.h>
  20. #include "global.h"
  21. class TQTimer;
  22. class Interface;
  23. template<class type>
  24. class StatisticsPtrList : public TQPtrList<type>
  25. {
  26. protected:
  27. virtual int compareItems ( TQPtrCollection::Item item1, TQPtrCollection::Item item2 )
  28. {
  29. StatisticEntry* entry1 = static_cast<StatisticEntry*>( item1 );
  30. StatisticEntry* entry2 = static_cast<StatisticEntry*>( item2 );
  31. if ( entry1->year > entry2->year )
  32. {
  33. return 1;
  34. }
  35. else if ( entry2->year > entry1->year )
  36. {
  37. return -1;
  38. } // ...here we know that years are the same...
  39. else if ( entry1->month > entry2->month )
  40. {
  41. return 1;
  42. }
  43. else if ( entry2->month > entry1->month )
  44. {
  45. return -1;
  46. } // ...here we know that months are the same...
  47. else if ( entry1->day > entry2->day )
  48. {
  49. return 1;
  50. }
  51. else if ( entry2->day > entry1->day )
  52. {
  53. return -1;
  54. } // ...here we know that dates are equal.
  55. return 0;
  56. };
  57. };
  58. /**
  59. * This class is able to collect transfered data for an interface,
  60. * store it in a file and deliver it on request.
  61. *
  62. * @short Statistics of transfered data for an interface
  63. * @author Percy Leonhardt <percy@eris23.de>
  64. */
  65. class InterfaceStatistics : public TQObject
  66. {
  67. Q_OBJECT
  68. public:
  69. /**
  70. * Default Constructor
  71. */
  72. InterfaceStatistics( Interface* interface );
  73. /**
  74. * Default Destructor
  75. */
  76. virtual ~InterfaceStatistics();
  77. /**
  78. * Load the statistics from a xml file
  79. */
  80. void loadStatistics();
  81. /**
  82. * Called from Interface::configChanged() when the user
  83. * changed the settings.
  84. */
  85. void configChanged();
  86. const StatisticEntry* getCurrentDay() const;
  87. const StatisticEntry* getCurrentMonth() const;
  88. const StatisticEntry* getCurrentYear() const;
  89. const StatisticsPtrList<StatisticEntry>& getDayStatistics() const;
  90. const StatisticsPtrList<StatisticEntry>& getMonthStatistics() const;
  91. const StatisticsPtrList<StatisticEntry>& getYearStatistics() const;
  92. signals:
  93. /**
  94. * The current entry has changed. There is only one signal
  95. * for day, month and year because if the day changes,
  96. * month and year also change.
  97. */
  98. void currentEntryChanged();
  99. /**
  100. * The list has changed i.e. there is a new day entry or the list was cleared
  101. */
  102. void dayStatisticsChanged();
  103. /**
  104. * The list has changed i.e. there is a new month entry or the list was cleared
  105. */
  106. void monthStatisticsChanged();
  107. /**
  108. * The list has changed i.e. there is a new year entry or the list was cleared
  109. */
  110. void yearStatisticsChanged();
  111. public slots:
  112. /**
  113. * Save the statistics to a xml file
  114. * (slot so it can be triggered by a timer signal)
  115. */
  116. void saveStatistics();
  117. /**
  118. * Add incoming data to the current day, month and year
  119. */
  120. void addIncomingData( unsigned long data );
  121. /**
  122. * Add outgoing data to the current day, month and year
  123. */
  124. void addOutgoingData( unsigned long data );
  125. /**
  126. * Clear all entries of the day statistics
  127. */
  128. void clearDayStatistics();
  129. /**
  130. * Clear all entries of the month statistics
  131. */
  132. void clearMonthStatistics();
  133. /**
  134. * Clear all entries of the year statistics
  135. */
  136. void clearYearStatistics();
  137. private:
  138. /**
  139. * Make sure the current entry corresponds with the current date
  140. */
  141. void checkCurrentEntry();
  142. /**
  143. * Fill the statistics with a current entry
  144. */
  145. void initStatistics();
  146. /**
  147. * Check if the current day is in the day statistics. If found set
  148. * mCurrentDay to the found entry else create a new one.
  149. */
  150. void updateCurrentDay();
  151. /**
  152. * Check if the current month is in the month statistics. If found set
  153. * mCurrentMonth to the found entry else create a new one.
  154. */
  155. void updateCurrentMonth();
  156. /**
  157. * Check if the current year is in the year statistics. If found set
  158. * mCurrentYear to the found entry else create a new one.
  159. */
  160. void updateCurrentYear();
  161. TQTimer* mSaveTimer;
  162. Interface* mInterface;
  163. StatisticEntry* mCurrentDay;
  164. StatisticEntry* mCurrentMonth;
  165. StatisticEntry* mCurrentYear;
  166. StatisticsPtrList<StatisticEntry> mDayStatistics;
  167. StatisticsPtrList<StatisticEntry> mMonthStatistics;
  168. StatisticsPtrList<StatisticEntry> mYearStatistics;
  169. };
  170. #endif // INTERFACESTATISTICS_H