KNemo – network interfaces monitor for systray
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

309 行
10 KiB

  1. /* This file is part of KNemo
  2. Copyright (C) 2004, 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 <tqtimer.h>
  17. #include <tqlabel.h>
  18. #include <tqstring.h>
  19. #include <tqgroupbox.h>
  20. #include <tqdatetime.h>
  21. #include <tqtabwidget.h>
  22. #include <kdebug.h>
  23. #include <tdeconfig.h>
  24. #include <tdelocale.h>
  25. #include <tdeglobal.h>
  26. #include <kiconloader.h>
  27. #include <kactivelabel.h>
  28. #include <tdeio/global.h>
  29. #include "data.h"
  30. #include "interface.h"
  31. #include "interfacestatistics.h"
  32. #include "interfacestatusdialog.h"
  33. InterfaceStatusDialog::InterfaceStatusDialog( Interface* interface, TQWidget* parent, const char* name )
  34. : InterfaceStatusDlg( parent, name ),
  35. mPosInitialized( false ),
  36. mInterface( interface )
  37. {
  38. setIcon( SmallIcon( "knemo" ) );
  39. setCaption( interface->getName() + " " + i18n( "Interface Status" ) );
  40. updateDialog();
  41. if ( interface->getData().available )
  42. {
  43. enableNetworkGroups( 0 );
  44. }
  45. else
  46. {
  47. disableNetworkGroups( 0 );
  48. }
  49. if ( !interface->getData().wirelessDevice )
  50. {
  51. TQWidget* wirelessTab = tabWidget->page( 2 );
  52. tabWidget->removePage( wirelessTab );
  53. delete wirelessTab;
  54. }
  55. if ( !interface->getSettings().activateStatistics )
  56. {
  57. setStatisticsGroupEnabled( false );
  58. }
  59. // Restore window size and position.
  60. TDEConfig* config = new TDEConfig( "knemorc", false );
  61. if ( config->hasGroup( "Interface_" + mInterface->getName() ) )
  62. {
  63. config->setGroup( "Interface_" + mInterface->getName() );
  64. if ( config->hasKey( "StatusX" ) && config->hasKey( "StatusY" ) )
  65. {
  66. mPos.setX( config->readNumEntry( "StatusX" ) );
  67. mPos.setY( config->readNumEntry( "StatusY" ) );
  68. mPosInitialized = true;
  69. }
  70. if ( config->hasKey( "StatusWidth" ) && config->hasKey( "StatusHeight" ) )
  71. resize( config->readNumEntry( "StatusWidth" ),
  72. config->readNumEntry( "StatusHeight" ) );
  73. }
  74. delete config;
  75. statisticsChanged();
  76. mTimer = new TQTimer();
  77. connect( mTimer, TQT_SIGNAL( timeout() ), this, TQT_SLOT( updateDialog() ) );
  78. mTimer->start( 1000 );
  79. }
  80. InterfaceStatusDialog::~InterfaceStatusDialog()
  81. {
  82. mTimer->stop();
  83. delete mTimer;
  84. // Store window size and position.
  85. TDEConfig* config = new TDEConfig( "knemorc", false );
  86. if ( config->hasGroup( "Interface_" + mInterface->getName() ) )
  87. {
  88. config->setGroup( "Interface_" + mInterface->getName() );
  89. config->writeEntry( "StatusX", x() );
  90. config->writeEntry( "StatusY", y() );
  91. config->writeEntry( "StatusWidth", width() );
  92. config->writeEntry( "StatusHeight", height() );
  93. config->sync();
  94. }
  95. delete config;
  96. }
  97. void InterfaceStatusDialog::hide()
  98. {
  99. mPos = pos();
  100. mPosInitialized = true;
  101. TQDialog::hide();
  102. }
  103. void InterfaceStatusDialog::show()
  104. {
  105. TQDialog::show();
  106. /**
  107. * mPosInitialized should always be true, except when
  108. * starting KNemo for the very first time.
  109. */
  110. if ( mPosInitialized )
  111. move( mPos );
  112. }
  113. void InterfaceStatusDialog::setStatisticsGroupEnabled( bool enabled )
  114. {
  115. groupBoxStatistics->setEnabled( enabled );
  116. }
  117. void InterfaceStatusDialog::updateDialog()
  118. {
  119. InterfaceData& data = mInterface->getData();
  120. InterfaceSettings& settings = mInterface->getSettings();
  121. // connection tab
  122. textLabelInterface->setText( mInterface->getName() );
  123. textLabelAlias->setText( settings.alias );
  124. if ( data.available )
  125. {
  126. textLabelStatus->setText( i18n( "Connection established." ) );
  127. int upsecs = mInterface->getStartTime().secsTo( TQDateTime::currentDateTime() );
  128. int updays = upsecs / 86400; // don't use TQDateTime::daysTo() because
  129. // we only want complete days
  130. TQString uptime;
  131. if ( updays == 1 )
  132. uptime = "1 day, ";
  133. else if ( updays > 1 )
  134. uptime = TQString( "%1 days, " ).arg( updays );
  135. upsecs -= 86400 * updays; // we only want the seconds of today
  136. int hrs = upsecs / 3600;
  137. int mins = ( upsecs - hrs * 3600 ) / 60;
  138. int secs = upsecs - hrs * 3600 - mins * 60;
  139. TQString time;
  140. time.sprintf( "%02d:%02d:%02d", hrs, mins, secs );
  141. uptime += time;
  142. textLabelUptime->setText( uptime );
  143. }
  144. else if ( data.existing )
  145. {
  146. textLabelStatus->setText( i18n( "Not connected." ) );
  147. textLabelUptime->setText( "00:00:00" );
  148. }
  149. else
  150. {
  151. textLabelStatus->setText( i18n( "Not existing." ) );
  152. textLabelUptime->setText( "00:00:00" );
  153. }
  154. if ( data.available )
  155. {
  156. // ip tab
  157. textLabelIP->setText( data.ipAddress );
  158. textLabelSubnet->setText( data.subnetMask );
  159. if ( mInterface->getType() == Interface::ETHERNET )
  160. {
  161. variableLabel1->setText( i18n( "Broadcast Address:" ) );
  162. variableText1->setText( data.broadcastAddress );
  163. variableLabel2->setText( i18n( "Default Gateway:" ) );
  164. variableText2->setText( data.defaultGateway );
  165. variableLabel3->setText( i18n( "HW-Address:" ) );
  166. variableText3->setText( data.hwAddress );
  167. }
  168. else if ( mInterface->getType() == Interface::PPP )
  169. {
  170. variableLabel1->setText( i18n( "PtP-Address:" ) );
  171. variableText1->setText( data.ptpAddress );
  172. variableLabel2->setText( TQString() );
  173. variableText2->setText( TQString() );
  174. variableLabel3->setText( TQString() );
  175. variableText3->setText( TQString() );
  176. }
  177. else
  178. {
  179. // shouldn't happen
  180. variableLabel1->setText( TQString() );
  181. variableText1->setText( TQString() );
  182. variableLabel2->setText( TQString() );
  183. variableText2->setText( TQString() );
  184. variableLabel3->setText( TQString() );
  185. variableText3->setText( TQString() );
  186. }
  187. // traffic tab
  188. textLabelPacketsSend->setText( TQString::number( data.txPackets ) );
  189. textLabelPacketsReceived->setText( TQString::number( data.rxPackets ) );
  190. textLabelBytesSend->setText( data.txString );
  191. textLabelBytesReceived->setText( data.rxString );
  192. unsigned long bytesPerSecond = data.outgoingBytes / mInterface->getGeneralData().pollInterval;
  193. textLabelSpeedSend->setText( TDEIO::convertSize( bytesPerSecond ) + i18n( "/s" ) );
  194. bytesPerSecond = data.incomingBytes / mInterface->getGeneralData().pollInterval;
  195. textLabelSpeedReceived->setText( TDEIO::convertSize( bytesPerSecond ) + i18n( "/s" ) );
  196. if ( data.wirelessDevice )
  197. {
  198. WirelessData& wdata = mInterface->getWirelessData();
  199. // wireless tab
  200. textLabelESSID->setText( wdata.essid );
  201. textLabelAccessPoint->setText( wdata.accessPoint );
  202. textLabelNickName->setText( wdata.nickName );
  203. textLabelMode->setText( wdata.mode );
  204. textLabelFreqChannel->setText( wdata.frequency + " [" + wdata.channel + "]" );
  205. textLabelBitRate->setText( wdata.bitRate );
  206. textLabelLinkQuality->setText( wdata.linkQuality + "%" );
  207. if ( wdata.encryption == true )
  208. {
  209. textLabelEncryption->setText( i18n( "active" ) );
  210. }
  211. else
  212. {
  213. textLabelEncryption->setText( i18n( "off" ) );
  214. }
  215. }
  216. }
  217. }
  218. void InterfaceStatusDialog::enableNetworkGroups( int )
  219. {
  220. groupBoxIP->setEnabled( true );
  221. groupBoxCurrentConnection->setEnabled( true );
  222. }
  223. void InterfaceStatusDialog::disableNetworkGroups( int )
  224. {
  225. groupBoxIP->setEnabled( false );
  226. groupBoxCurrentConnection->setEnabled( false );
  227. // clear IP group
  228. textLabelIP->setText( TQString() );
  229. textLabelSubnet->setText( TQString() );
  230. variableText1->setText( TQString() );
  231. variableText2->setText( TQString() );
  232. variableText3->setText( TQString() );
  233. // clear current connection group
  234. textLabelPacketsSend->setText( TQString() );
  235. textLabelPacketsReceived->setText( TQString() );
  236. textLabelBytesSend->setText( TQString() );
  237. textLabelBytesReceived->setText( TQString() );
  238. textLabelSpeedSend->setText( TQString() );
  239. textLabelSpeedReceived->setText( TQString() );
  240. // clear wireless tab
  241. if ( mInterface->getData().wirelessDevice )
  242. {
  243. textLabelESSID->setText( TQString() );
  244. textLabelAccessPoint->setText( TQString() );
  245. textLabelNickName->setText( TQString() );
  246. textLabelMode->setText( TQString() );
  247. textLabelFreqChannel->setText( TQString() );
  248. textLabelBitRate->setText( TQString() );
  249. textLabelLinkQuality->setText( TQString() );
  250. textLabelEncryption->setText( TQString() );
  251. }
  252. }
  253. void InterfaceStatusDialog::statisticsChanged()
  254. {
  255. InterfaceStatistics* statistics = mInterface->getStatistics();
  256. if ( statistics == 0 )
  257. {
  258. return;
  259. }
  260. const StatisticEntry* entry = statistics->getCurrentDay();
  261. textLabelTodaySent->setText( TDEIO::convertSize( entry->txBytes ) );
  262. textLabelTodayReceived->setText( TDEIO::convertSize( entry->rxBytes ) );
  263. textLabelTodayTotal->setText( TDEIO::convertSize( entry->txBytes + entry->rxBytes ) );
  264. entry = statistics->getCurrentMonth();
  265. textLabelMonthSent->setText( TDEIO::convertSize( entry->txBytes ) );
  266. textLabelMonthReceived->setText( TDEIO::convertSize( entry->rxBytes ) );
  267. textLabelMonthTotal->setText( TDEIO::convertSize( entry->txBytes + entry->rxBytes ) );
  268. entry = statistics->getCurrentYear();
  269. textLabelYearSent->setText( TDEIO::convertSize( entry->txBytes ) );
  270. textLabelYearReceived->setText( TDEIO::convertSize( entry->rxBytes ) );
  271. textLabelYearTotal->setText( TDEIO::convertSize( entry->txBytes + entry->rxBytes ) );
  272. }
  273. #include "interfacestatusdialog.moc"