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.
 
 
 
 
 
 

345 lines
14 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 <tqstrlist.h>
  18. #include <kdebug.h>
  19. #include <kglobal.h>
  20. #include <klocale.h>
  21. #include <kconfig.h>
  22. #include <kprocess.h>
  23. #include <kinstance.h>
  24. #include <kmessagebox.h>
  25. #include <kstandarddirs.h>
  26. #include "knemodaemon.h"
  27. #include "interface.h"
  28. #include "backendbase.h"
  29. #include "daemonregistry.h"
  30. TQString KNemoDaemon::sSelectedInterface = TQString();
  31. extern "C" {
  32. KDE_EXPORT KDEDModule *create_knemod(const TQCString &name) {
  33. return new KNemoDaemon(name);
  34. }
  35. }
  36. KNemoDaemon::KNemoDaemon( const TQCString& name )
  37. : KDEDModule( name ),
  38. mColorVLines( 0x04FB1D ),
  39. mColorHLines( 0x04FB1D ),
  40. mColorIncoming( 0x1889FF ),
  41. mColorOutgoing( 0xFF7F08 ),
  42. mColorBackground( 0x313031 ),
  43. mInstance( new KInstance( "knemo" ) ),
  44. mNotifyInstance( new KNotifyClient::Instance( mInstance ) )
  45. {
  46. KGlobal::locale()->insertCatalogue( "knemod" );
  47. readConfig();
  48. // select the backend from the config file
  49. KConfig* config = new KConfig( "knemorc", true );
  50. config->setGroup( "General" );
  51. mBackendName = config->readEntry( "Backend", "Nettools" );
  52. delete config;
  53. bool foundBackend = false;
  54. int i;
  55. for ( i = 0; DaemonRegistry[i].name != TQString(); i++ )
  56. {
  57. if ( DaemonRegistry[i].name == mBackendName )
  58. {
  59. foundBackend = true;
  60. break;
  61. }
  62. }
  63. if ( !foundBackend )
  64. {
  65. i = 0; // use the first backend (Nettools)
  66. }
  67. mBackend = ( *DaemonRegistry[i].function )( mInterfaceDict );
  68. mInterfaceDict.setAutoDelete( true );
  69. mPollTimer = new TQTimer();
  70. connect( mPollTimer, TQT_SIGNAL( timeout() ), this, TQT_SLOT( updateInterfaces() ) );
  71. mPollTimer->start( mGeneralData.pollInterval * 1000 );
  72. }
  73. KNemoDaemon::~KNemoDaemon()
  74. {
  75. mPollTimer->stop();
  76. delete mPollTimer;
  77. delete mBackend;
  78. delete mNotifyInstance;
  79. delete mInstance;
  80. TQDictIterator<Interface> it( mInterfaceDict );
  81. for ( ; it.current(); )
  82. {
  83. mInterfaceDict.remove( it.currentKey() );
  84. // 'remove' already advanced the iterator to the next item
  85. }
  86. }
  87. void KNemoDaemon::readConfig()
  88. {
  89. KConfig* config = new KConfig( "knemorc", true );
  90. config->setGroup( "General" );
  91. mGeneralData.pollInterval = config->readNumEntry( "PollInterval", 1 );
  92. mGeneralData.saveInterval = config->readNumEntry( "SaveInterval", 60 );
  93. mGeneralData.statisticsDir = config->readEntry( "StatisticsDir", KGlobal::dirs()->saveLocation( "data", "knemo/" ) );
  94. mGeneralData.toolTipContent = config->readNumEntry( "ToolTipContent", 2 );
  95. TQStrList list;
  96. int numEntries = config->readListEntry( "Interfaces", list );
  97. if ( numEntries == 0 )
  98. return;
  99. char* interface;
  100. for ( interface = list.first(); interface; interface = list.next() )
  101. {
  102. Interface* iface = new Interface( interface, mGeneralData, mPlotterSettings );
  103. TQString group( "Interface_" );
  104. group += interface;
  105. if ( config->hasGroup( group ) )
  106. {
  107. config->setGroup( group );
  108. InterfaceSettings& settings = iface->getSettings();
  109. settings.alias = config->readEntry( "Alias" );
  110. settings.iconSet = config->readNumEntry( "IconSet", 0 );
  111. settings.customCommands = config->readBoolEntry( "CustomCommands" );
  112. settings.hideWhenNotAvailable = config->readBoolEntry( "HideWhenNotAvailable" );
  113. settings.hideWhenNotExisting = config->readBoolEntry( "HideWhenNotExisting" );
  114. settings.activateStatistics = config->readBoolEntry( "ActivateStatistics" );
  115. settings.trafficThreshold = config->readNumEntry( "TrafficThreshold", 0 );
  116. if ( settings.customCommands )
  117. {
  118. int numCommands = config->readNumEntry( "NumCommands" );
  119. for ( int i = 0; i < numCommands; i++ )
  120. {
  121. TQString entry;
  122. InterfaceCommand cmd;
  123. entry = TQString( "RunAsRoot%1" ).arg( i + 1 );
  124. cmd.runAsRoot = config->readBoolEntry( entry );
  125. entry = TQString( "Command%1" ).arg( i + 1 );
  126. cmd.command = config->readEntry( entry );
  127. entry = TQString( "MenuText%1" ).arg( i + 1 );
  128. cmd.menuText = config->readEntry( entry );
  129. settings.commands.append( cmd );
  130. }
  131. }
  132. iface->configChanged(); // important to activate the statistics
  133. }
  134. mInterfaceDict.insert( interface, iface );
  135. }
  136. // load the settings for the plotter
  137. config->setGroup( "PlotterSettings" );
  138. mPlotterSettings.pixel = config->readNumEntry( "Pixel", 1 );
  139. mPlotterSettings.count = config->readNumEntry( "Count", 5 );
  140. mPlotterSettings.distance = config->readNumEntry( "Distance", 30 );
  141. mPlotterSettings.fontSize = config->readNumEntry( "FontSize", 8 );
  142. mPlotterSettings.minimumValue = config->readNumEntry( "MinimumValue", 0 );
  143. mPlotterSettings.maximumValue = config->readNumEntry( "MaximumValue", 1 );
  144. mPlotterSettings.labels = config->readBoolEntry( "Labels", true );
  145. mPlotterSettings.topBar = config->readBoolEntry( "TopBar", false );
  146. mPlotterSettings.showIncoming = config->readBoolEntry( "ShowIncoming", true );
  147. mPlotterSettings.showOutgoing = config->readBoolEntry( "ShowOutgoing", true );
  148. mPlotterSettings.verticalLines = config->readBoolEntry( "VerticalLines", true );
  149. mPlotterSettings.horizontalLines = config->readBoolEntry( "HorizontalLines", true );
  150. mPlotterSettings.automaticDetection = config->readBoolEntry( "AutomaticDetection", true );
  151. mPlotterSettings.verticalLinesScroll = config->readBoolEntry( "VerticalLinesScroll", true );
  152. mPlotterSettings.colorVLines = config->readColorEntry( "ColorVLines", &mColorVLines );
  153. mPlotterSettings.colorHLines = config->readColorEntry( "ColorHLines", &mColorHLines );
  154. mPlotterSettings.colorIncoming = config->readColorEntry( "ColorIncoming", &mColorIncoming );
  155. mPlotterSettings.colorOutgoing = config->readColorEntry( "ColorOutgoing", &mColorOutgoing );
  156. mPlotterSettings.colorBackground = config->readColorEntry( "ColorBackground", &mColorBackground );
  157. delete config;
  158. }
  159. void KNemoDaemon::reparseConfiguration()
  160. {
  161. // Read the settings from the config file.
  162. TQDict<InterfaceSettings> settingsDict;
  163. KConfig* config = new KConfig( "knemorc", false );
  164. config->setGroup( "General" );
  165. mGeneralData.pollInterval = config->readNumEntry( "PollInterval", 1 );
  166. mGeneralData.saveInterval = config->readNumEntry( "SaveInterval", 60 );
  167. mGeneralData.statisticsDir = config->readEntry( "StatisticsDir", KGlobal::dirs()->saveLocation( "data", "knemo/" ) );
  168. mGeneralData.toolTipContent = config->readNumEntry( "ToolTipContent", 2 );
  169. // restart the timer with the new interval
  170. mPollTimer->changeInterval( mGeneralData.pollInterval * 1000 );
  171. // select the backend from the config file
  172. TQString backend = config->readEntry( "Backend", "Nettools" );
  173. if ( mBackendName != backend )
  174. {
  175. bool foundBackend = false;
  176. mBackendName = backend;
  177. int i;
  178. for ( i = 0; DaemonRegistry[i].name != TQString(); i++ )
  179. {
  180. if ( DaemonRegistry[i].name == backend )
  181. {
  182. foundBackend = true;
  183. break;
  184. }
  185. }
  186. if ( foundBackend )
  187. {
  188. delete mBackend;
  189. mBackend = ( *DaemonRegistry[i].function )( mInterfaceDict );
  190. }
  191. }
  192. TQStrList list;
  193. int numEntries = config->readListEntry( "Interfaces", list );
  194. if ( numEntries == 0 )
  195. return;
  196. char* interface;
  197. for ( interface = list.first(); interface; interface = list.next() )
  198. {
  199. TQString group( "Interface_" );
  200. group += interface;
  201. InterfaceSettings* settings = new InterfaceSettings();
  202. if ( config->hasGroup( group ) )
  203. {
  204. config->setGroup( group );
  205. settings->alias = config->readEntry( "Alias" );
  206. settings->iconSet = config->readNumEntry( "IconSet", 0 );
  207. settings->customCommands = config->readBoolEntry( "CustomCommands" );
  208. settings->hideWhenNotAvailable = config->readBoolEntry( "HideWhenNotAvailable" );
  209. settings->hideWhenNotExisting = config->readBoolEntry( "HideWhenNotExisting" );
  210. settings->activateStatistics = config->readBoolEntry( "ActivateStatistics" );
  211. settings->trafficThreshold = config->readNumEntry( "TrafficThreshold", 0 );
  212. if ( settings->customCommands )
  213. {
  214. int numCommands = config->readNumEntry( "NumCommands" );
  215. for ( int i = 0; i < numCommands; i++ )
  216. {
  217. TQString entry;
  218. InterfaceCommand cmd;
  219. entry = TQString( "RunAsRoot%1" ).arg( i + 1 );
  220. cmd.runAsRoot = config->readBoolEntry( entry );
  221. entry = TQString( "Command%1" ).arg( i + 1 );
  222. cmd.command = config->readEntry( entry );
  223. entry = TQString( "MenuText%1" ).arg( i + 1 );
  224. cmd.menuText = config->readEntry( entry );
  225. settings->commands.append( cmd );
  226. }
  227. }
  228. }
  229. settingsDict.insert( interface, settings );
  230. }
  231. // load the settings for the plotter
  232. config->setGroup( "PlotterSettings" );
  233. mPlotterSettings.pixel = config->readNumEntry( "Pixel", 1 );
  234. mPlotterSettings.count = config->readNumEntry( "Count", 5 );
  235. mPlotterSettings.distance = config->readNumEntry( "Distance", 30 );
  236. mPlotterSettings.fontSize = config->readNumEntry( "FontSize", 8 );
  237. mPlotterSettings.minimumValue = config->readNumEntry( "MinimumValue", 0 );
  238. mPlotterSettings.maximumValue = config->readNumEntry( "MaximumValue", 1 );
  239. mPlotterSettings.labels = config->readBoolEntry( "Labels", true );
  240. mPlotterSettings.topBar = config->readBoolEntry( "TopBar", false );
  241. mPlotterSettings.showIncoming = config->readBoolEntry( "ShowIncoming", true );
  242. mPlotterSettings.showOutgoing = config->readBoolEntry( "ShowOutgoing", true );
  243. mPlotterSettings.verticalLines = config->readBoolEntry( "VerticalLines", true );
  244. mPlotterSettings.horizontalLines = config->readBoolEntry( "HorizontalLines", true );
  245. mPlotterSettings.automaticDetection = config->readBoolEntry( "AutomaticDetection", true );
  246. mPlotterSettings.verticalLinesScroll = config->readBoolEntry( "VerticalLinesScroll", true );
  247. mPlotterSettings.colorVLines = config->readColorEntry( "ColorVLines", &mColorVLines );
  248. mPlotterSettings.colorHLines = config->readColorEntry( "ColorHLines", &mColorHLines );
  249. mPlotterSettings.colorIncoming = config->readColorEntry( "ColorIncoming", &mColorIncoming );
  250. mPlotterSettings.colorOutgoing = config->readColorEntry( "ColorOutgoing", &mColorOutgoing );
  251. mPlotterSettings.colorBackground = config->readColorEntry( "ColorBackground", &mColorBackground );
  252. // Remove all interfaces that the user deleted from
  253. // the internal list.
  254. TQDictIterator<Interface> it( mInterfaceDict );
  255. for ( ; it.current(); )
  256. {
  257. if ( settingsDict.find( it.currentKey() ) == 0 )
  258. {
  259. config->deleteGroup( "Interface_" + it.currentKey() );
  260. mInterfaceDict.remove( it.currentKey() );
  261. // 'remove' already advanced the iterator to the next item
  262. }
  263. else
  264. ++it;
  265. }
  266. config->sync();
  267. delete config;
  268. // Update all other interfaces and add new ones.
  269. TQDictIterator<InterfaceSettings> setIt( settingsDict );
  270. for ( ; setIt.current(); ++setIt )
  271. {
  272. Interface* iface;
  273. if ( mInterfaceDict.find( setIt.currentKey() ) == 0 )
  274. {
  275. iface = new Interface( setIt.currentKey(), mGeneralData, mPlotterSettings );
  276. mInterfaceDict.insert( setIt.currentKey(), iface );
  277. }
  278. else
  279. iface = mInterfaceDict[setIt.currentKey()];
  280. InterfaceSettings& settings = iface->getSettings();
  281. settings.alias = setIt.current()->alias;
  282. settings.iconSet = setIt.current()->iconSet;
  283. settings.customCommands = setIt.current()->customCommands;
  284. settings.hideWhenNotAvailable = setIt.current()->hideWhenNotAvailable;
  285. settings.hideWhenNotExisting = setIt.current()->hideWhenNotExisting;
  286. settings.activateStatistics = setIt.current()->activateStatistics;
  287. settings.trafficThreshold = setIt.current()->trafficThreshold;
  288. settings.commands = setIt.current()->commands;
  289. iface->configChanged();
  290. }
  291. }
  292. TQString KNemoDaemon::getSelectedInterface()
  293. {
  294. // Reset the variable to avoid preselecting an interface when
  295. // the user opens the control center module from the control
  296. // center afterwards.
  297. TQString tmp = sSelectedInterface;
  298. sSelectedInterface = TQString();
  299. return tmp;
  300. }
  301. void KNemoDaemon::updateInterfaces()
  302. {
  303. mBackend->update();
  304. }
  305. #include "knemodaemon.moc"