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.
 
 
 
 
 
 

385 lines
11 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. #include <tqdom.h>
  17. #include <tqfile.h>
  18. #include <tqtimer.h>
  19. #include <tqstring.h>
  20. #include <tqdatetime.h>
  21. #include <kdebug.h>
  22. #include "interface.h"
  23. #include "interfacestatistics.h"
  24. InterfaceStatistics::InterfaceStatistics( Interface* interface )
  25. : TQObject(),
  26. mInterface( interface )
  27. {
  28. mDayStatistics.setAutoDelete( true );
  29. mMonthStatistics.setAutoDelete( true );
  30. mYearStatistics.setAutoDelete( true );
  31. initStatistics();
  32. mSaveTimer = new TQTimer();
  33. connect( mSaveTimer, TQT_SIGNAL( timeout() ), this, TQT_SLOT( saveStatistics() ) );
  34. mSaveTimer->start( mInterface->getGeneralData().saveInterval * 1000 );
  35. }
  36. InterfaceStatistics::~InterfaceStatistics()
  37. {
  38. mSaveTimer->stop();
  39. delete mSaveTimer;
  40. mDayStatistics.clear();
  41. mMonthStatistics.clear();
  42. mYearStatistics.clear();
  43. }
  44. void InterfaceStatistics::loadStatistics()
  45. {
  46. TQDomDocument doc( "statistics" );
  47. TQString dir = mInterface->getGeneralData().statisticsDir;
  48. TQFile file( dir + "/statistics_" + mInterface->getName() );
  49. if ( !file.open( IO_ReadOnly ) )
  50. return;
  51. if ( !doc.setContent( &file ) )
  52. {
  53. file.close();
  54. return;
  55. }
  56. file.close();
  57. mDayStatistics.clear();
  58. mMonthStatistics.clear();
  59. mYearStatistics.clear();
  60. TQDomElement root = doc.documentElement();
  61. TQDomNode n = root.namedItem( "days" );
  62. if ( !n.isNull() )
  63. {
  64. TQDomNode dayNode = n.firstChild();
  65. while ( !dayNode.isNull() )
  66. {
  67. TQDomElement day = dayNode.toElement();
  68. if ( !day.isNull() )
  69. {
  70. StatisticEntry* entry = new StatisticEntry();
  71. entry->day = day.attribute( "day" ).toInt();
  72. entry->month = day.attribute( "month" ).toInt();
  73. entry->year = day.attribute( "year" ).toInt();
  74. entry->rxBytes = (TQ_UINT64) day.attribute( "rxBytes" ).toDouble();
  75. entry->txBytes = (TQ_UINT64) day.attribute( "txBytes" ).toDouble();
  76. mDayStatistics.append( entry );
  77. }
  78. dayNode = dayNode.nextSibling();
  79. }
  80. mDayStatistics.sort();
  81. }
  82. n = root.namedItem( "months" );
  83. if ( !n.isNull() )
  84. {
  85. TQDomNode monthNode = n.firstChild();
  86. while ( !monthNode.isNull() )
  87. {
  88. TQDomElement month = monthNode.toElement();
  89. if ( !month.isNull() )
  90. {
  91. StatisticEntry* entry = new StatisticEntry();
  92. entry->day = 0;
  93. entry->month = month.attribute( "month" ).toInt();
  94. entry->year = month.attribute( "year" ).toInt();
  95. entry->rxBytes = (TQ_UINT64) month.attribute( "rxBytes" ).toDouble();
  96. entry->txBytes = (TQ_UINT64) month.attribute( "txBytes" ).toDouble();
  97. mMonthStatistics.append( entry );
  98. }
  99. monthNode = monthNode.nextSibling();
  100. }
  101. mMonthStatistics.sort();
  102. }
  103. n = root.namedItem( "years" );
  104. if ( !n.isNull() )
  105. {
  106. TQDomNode yearNode = n.firstChild();
  107. while ( !yearNode.isNull() )
  108. {
  109. TQDomElement year = yearNode.toElement();
  110. if ( !year.isNull() )
  111. {
  112. StatisticEntry* entry = new StatisticEntry();
  113. entry->day = 0;
  114. entry->month = 0;
  115. entry->year = year.attribute( "year" ).toInt();
  116. entry->rxBytes = (TQ_UINT64) year.attribute( "rxBytes" ).toDouble();
  117. entry->txBytes = (TQ_UINT64) year.attribute( "txBytes" ).toDouble();
  118. mYearStatistics.append( entry );
  119. }
  120. yearNode = yearNode.nextSibling();
  121. }
  122. mYearStatistics.sort();
  123. }
  124. initStatistics();
  125. }
  126. void InterfaceStatistics::saveStatistics()
  127. {
  128. TQDomDocument doc( "statistics" );
  129. TQDomElement root = doc.createElement( "statistics" );
  130. doc.appendChild( root );
  131. TQDomElement days = doc.createElement( "days" );
  132. StatisticEntry* iterator = mDayStatistics.first();
  133. while ( iterator )
  134. {
  135. TQDomElement day = doc.createElement( "day" );
  136. day.setAttribute( "day", iterator->day );
  137. day.setAttribute( "month", iterator->month );
  138. day.setAttribute( "year", iterator->year );
  139. day.setAttribute( "rxBytes", (double) iterator->rxBytes );
  140. day.setAttribute( "txBytes", (double) iterator->txBytes );
  141. days.appendChild( day );
  142. iterator = mDayStatistics.next();
  143. }
  144. root.appendChild( days );
  145. TQDomElement months = doc.createElement( "months" );
  146. iterator = mMonthStatistics.first();
  147. while ( iterator )
  148. {
  149. TQDomElement month = doc.createElement( "month" );
  150. month.setAttribute( "month", iterator->month );
  151. month.setAttribute( "year", iterator->year );
  152. month.setAttribute( "rxBytes", (double) iterator->rxBytes );
  153. month.setAttribute( "txBytes", (double) iterator->txBytes );
  154. months.appendChild( month );
  155. iterator = mMonthStatistics.next();
  156. }
  157. root.appendChild( months );
  158. TQDomElement years = doc.createElement( "years" );
  159. iterator = mYearStatistics.first();
  160. while ( iterator )
  161. {
  162. TQDomElement year = doc.createElement( "year" );
  163. year.setAttribute( "year", iterator->year );
  164. year.setAttribute( "rxBytes", (double) iterator->rxBytes );
  165. year.setAttribute( "txBytes", (double) iterator->txBytes );
  166. years.appendChild( year );
  167. iterator = mYearStatistics.next();
  168. }
  169. root.appendChild( years );
  170. TQString dir = mInterface->getGeneralData().statisticsDir;
  171. TQFile file( dir + "/statistics_" + mInterface->getName() );
  172. if ( !file.open( IO_WriteOnly ) )
  173. return;
  174. TQTextStream stream( &file );
  175. stream << doc.toString();
  176. file.close();
  177. }
  178. void InterfaceStatistics::configChanged()
  179. {
  180. // restart the timer with the new value
  181. mSaveTimer->changeInterval( mInterface->getGeneralData().saveInterval * 1000 );
  182. }
  183. const StatisticEntry* InterfaceStatistics::getCurrentDay() const
  184. {
  185. return mCurrentDay;
  186. }
  187. const StatisticEntry* InterfaceStatistics::getCurrentMonth() const
  188. {
  189. return mCurrentMonth;
  190. }
  191. const StatisticEntry* InterfaceStatistics::getCurrentYear() const
  192. {
  193. return mCurrentYear;
  194. }
  195. const StatisticsPtrList<StatisticEntry>& InterfaceStatistics::getDayStatistics() const
  196. {
  197. return mDayStatistics;
  198. }
  199. const StatisticsPtrList<StatisticEntry>& InterfaceStatistics::getMonthStatistics() const
  200. {
  201. return mMonthStatistics;
  202. }
  203. const StatisticsPtrList<StatisticEntry>& InterfaceStatistics::getYearStatistics() const
  204. {
  205. return mYearStatistics;
  206. }
  207. void InterfaceStatistics::addIncomingData( unsigned long data )
  208. {
  209. checkCurrentEntry();
  210. mCurrentDay->rxBytes += data;
  211. mCurrentMonth->rxBytes += data;
  212. mCurrentYear->rxBytes += data;
  213. emit currentEntryChanged();
  214. }
  215. void InterfaceStatistics::addOutgoingData( unsigned long data )
  216. {
  217. checkCurrentEntry();
  218. mCurrentDay->txBytes += data;
  219. mCurrentMonth->txBytes += data;
  220. mCurrentYear->txBytes += data;
  221. emit currentEntryChanged();
  222. }
  223. void InterfaceStatistics::clearDayStatistics()
  224. {
  225. mDayStatistics.clear();
  226. updateCurrentDay();
  227. }
  228. void InterfaceStatistics::clearMonthStatistics()
  229. {
  230. mMonthStatistics.clear();
  231. updateCurrentMonth();
  232. }
  233. void InterfaceStatistics::clearYearStatistics()
  234. {
  235. mYearStatistics.clear();
  236. updateCurrentYear();
  237. }
  238. void InterfaceStatistics::checkCurrentEntry()
  239. {
  240. if ( mCurrentDay->day != TQDate::currentDate().day() ||
  241. mCurrentDay->month != TQDate::currentDate().month() ||
  242. mCurrentDay->year != TQDate::currentDate().year() )
  243. {
  244. // current day has changed
  245. updateCurrentDay();
  246. if ( mCurrentMonth->month != TQDate::currentDate().month() ||
  247. mCurrentMonth->year != TQDate::currentDate().year() )
  248. {
  249. // current month has also changed
  250. updateCurrentMonth();
  251. }
  252. if ( mCurrentYear->year != TQDate::currentDate().year() )
  253. {
  254. // current year has also changed
  255. updateCurrentYear();
  256. }
  257. }
  258. }
  259. void InterfaceStatistics::initStatistics()
  260. {
  261. updateCurrentDay();
  262. updateCurrentMonth();
  263. updateCurrentYear();
  264. emit currentEntryChanged();
  265. }
  266. void InterfaceStatistics::updateCurrentDay()
  267. {
  268. mCurrentDay = mDayStatistics.first();
  269. while ( mCurrentDay )
  270. {
  271. if ( mCurrentDay->day == TQDate::currentDate().day() &&
  272. mCurrentDay->month == TQDate::currentDate().month() &&
  273. mCurrentDay->year == TQDate::currentDate().year() )
  274. {
  275. // found current day in list
  276. return;
  277. }
  278. mCurrentDay = mDayStatistics.next();
  279. }
  280. // the current day is not in the list
  281. mCurrentDay = new StatisticEntry();
  282. mCurrentDay->day = TQDate::currentDate().day();
  283. mCurrentDay->month = TQDate::currentDate().month();
  284. mCurrentDay->year = TQDate::currentDate().year();
  285. mCurrentDay->rxBytes = 0;
  286. mCurrentDay->txBytes = 0;
  287. mDayStatistics.append( mCurrentDay ); // TODO: insert at correct position
  288. emit dayStatisticsChanged();
  289. }
  290. void InterfaceStatistics::updateCurrentMonth()
  291. {
  292. mCurrentMonth = mMonthStatistics.first();
  293. while ( mCurrentMonth )
  294. {
  295. if ( mCurrentMonth->month == TQDate::currentDate().month() &&
  296. mCurrentMonth->year == TQDate::currentDate().year() )
  297. {
  298. // found current month in list
  299. return;
  300. }
  301. mCurrentMonth = mMonthStatistics.next();
  302. }
  303. // the current month is not in the list
  304. mCurrentMonth = new StatisticEntry();
  305. mCurrentMonth->day = 0;
  306. mCurrentMonth->month = TQDate::currentDate().month();
  307. mCurrentMonth->year = TQDate::currentDate().year();
  308. mCurrentMonth->rxBytes = 0;
  309. mCurrentMonth->txBytes = 0;
  310. mMonthStatistics.append( mCurrentMonth ); // TODO: insert at correct position
  311. emit monthStatisticsChanged();
  312. }
  313. void InterfaceStatistics::updateCurrentYear()
  314. {
  315. mCurrentYear = mYearStatistics.first();
  316. while ( mCurrentYear )
  317. {
  318. if ( mCurrentYear->year == TQDate::currentDate().year() )
  319. {
  320. // found current year in list
  321. return;
  322. }
  323. mCurrentYear = mYearStatistics.next();
  324. }
  325. // the current year is not in the list
  326. mCurrentYear = new StatisticEntry();
  327. mCurrentYear->day = 0;
  328. mCurrentYear->month = 0;
  329. mCurrentYear->year = TQDate::currentDate().year();
  330. mCurrentYear->rxBytes = 0;
  331. mCurrentYear->txBytes = 0;
  332. mYearStatistics.append( mCurrentYear ); // TODO: insert at correct position
  333. emit yearStatisticsChanged();
  334. }