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.
 
 
 
 

317 lines
11 KiB

  1. /* This file is part of KNemo
  2. Copyright (C) 2004, 2005 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 <unistd.h>
  17. #include <tqpixmap.h>
  18. #include <kdebug.h>
  19. #include <tdeglobal.h>
  20. #include <tdelocale.h>
  21. #include <kprocess.h>
  22. #include <tdepopupmenu.h>
  23. #include <kiconloader.h>
  24. #include <knotifyclient.h>
  25. #include "data.h"
  26. #include "interface.h"
  27. #include "knemodaemon.h"
  28. #include "interfaceicon.h"
  29. #include "interfacetray.h"
  30. #include "interfacemonitor.h"
  31. #include "interfacetooltip.h"
  32. const TQString InterfaceIcon::ICON_DISCONNECTED = "network_disconnected";
  33. const TQString InterfaceIcon::ICON_CONNECTED = "network_connected";
  34. const TQString InterfaceIcon::ICON_INCOMING = "network_incoming";
  35. const TQString InterfaceIcon::ICON_OUTGOING = "network_outgoing";
  36. const TQString InterfaceIcon::ICON_TRAFFIC = "network_traffic";
  37. const TQString InterfaceIcon::SUFFIX_PPP = "_ppp";
  38. const TQString InterfaceIcon::SUFFIX_LAN = "_lan";
  39. const TQString InterfaceIcon::SUFFIX_WLAN = "_wlan";
  40. InterfaceIcon::InterfaceIcon( Interface* interface )
  41. : TQObject(),
  42. mInterface( interface ),
  43. mTray( 0L )
  44. {
  45. }
  46. InterfaceIcon::~InterfaceIcon()
  47. {
  48. if ( mTray != 0L )
  49. delete mTray;
  50. }
  51. void InterfaceIcon::updateStatus( int status )
  52. {
  53. if ( mTray == 0L )
  54. return;
  55. // If the user wants something different than the default icons
  56. // append the correct suffix to the filename.
  57. TQString suffix;
  58. if ( mInterface->getSettings().iconSet == Interface::NETWORK )
  59. {
  60. suffix = SUFFIX_LAN;
  61. }
  62. else if ( mInterface->getSettings().iconSet == Interface::WIRELESS )
  63. {
  64. suffix = SUFFIX_WLAN;
  65. }
  66. else if ( mInterface->getSettings().iconSet == Interface::MODEM )
  67. {
  68. suffix = SUFFIX_PPP;
  69. }
  70. else
  71. {
  72. suffix = ""; // use standard icons
  73. }
  74. // Now set the correct icon depending on the status of the interface.
  75. if ( status == Interface::NOT_AVAILABLE ||
  76. status == Interface::NOT_EXISTING )
  77. {
  78. mTray->setPixmap( mTray->isShown() ? mTray->loadSizedIcon( ICON_DISCONNECTED + suffix, mTray->width() ) : mTray->loadIcon( ICON_DISCONNECTED + suffix ) );
  79. }
  80. else if ( ( status & Interface::RX_TRAFFIC ) &&
  81. ( status & Interface::TX_TRAFFIC ) )
  82. {
  83. mTray->setPixmap( mTray->isShown() ? mTray->loadSizedIcon( ICON_TRAFFIC + suffix, mTray->width() ) : mTray->loadIcon( ICON_TRAFFIC + suffix ) );
  84. }
  85. else if ( status & Interface::RX_TRAFFIC )
  86. {
  87. mTray->setPixmap( mTray->isShown() ? mTray->loadSizedIcon( ICON_INCOMING + suffix, mTray->width() ) : mTray->loadIcon( ICON_INCOMING + suffix ) );
  88. }
  89. else if ( status & Interface::TX_TRAFFIC )
  90. {
  91. mTray->setPixmap( mTray->isShown() ? mTray->loadSizedIcon( ICON_OUTGOING + suffix, mTray->width() ) : mTray->loadIcon( ICON_OUTGOING + suffix ) );
  92. }
  93. else
  94. {
  95. mTray->setPixmap( mTray->isShown() ? mTray->loadSizedIcon( ICON_CONNECTED + suffix, mTray->width() ) : mTray->loadIcon( ICON_CONNECTED + suffix ) );
  96. }
  97. }
  98. void InterfaceIcon::updateToolTip()
  99. {
  100. if ( mTray == 0L )
  101. return;
  102. TQString toolTip = mInterface->getSettings().alias;
  103. if ( toolTip == TQString() )
  104. toolTip = mInterface->getName();
  105. new InterfaceToolTip( mInterface, mTray );
  106. }
  107. void InterfaceIcon::updateMenu()
  108. {
  109. if ( mTray == 0L )
  110. return;
  111. // Remove all old entries.
  112. TDEPopupMenu* menu = mTray->contextMenu();
  113. int count = menu->count();
  114. for ( int i = 0; i < count - 6; i++ )
  115. menu->removeItemAt( 6 );
  116. InterfaceSettings& settings = mInterface->getSettings();
  117. // If the user wants statistics, add an entry to show them.
  118. if ( settings.activateStatistics )
  119. {
  120. menu->insertItem( i18n( "Open &Statistics" ), this,
  121. TQT_SIGNAL( statisticsSelected() ) );
  122. }
  123. // If the user wants custom commands, add them.
  124. if ( settings.customCommands )
  125. {
  126. menu->insertSeparator();
  127. TQValueVector<InterfaceCommand>::iterator it;
  128. for ( it = settings.commands.begin(); it != settings.commands.end(); it++ )
  129. (*it).id = menu->insertItem( (*it).menuText );
  130. }
  131. }
  132. void InterfaceIcon::resizeIcon()
  133. {
  134. updateTrayStatus(0, false);
  135. }
  136. void InterfaceIcon::updateTrayStatus( int previousState, bool notify )
  137. {
  138. bool interfaceExists = mInterface->getData().existing;
  139. bool interfaceAvailable = mInterface->getData().available;
  140. bool hideWhenNotExisting = mInterface->getSettings().hideWhenNotExisting;
  141. bool hideWhenNotAvailable = mInterface->getSettings().hideWhenNotAvailable;
  142. // notification 'interface not available'
  143. if ( !interfaceAvailable && mTray != 0L &&
  144. previousState == Interface::AVAILABLE && notify )
  145. {
  146. /* When KNemo is starting we don't show the change in connection
  147. * status as this would be annoying when KDE starts.
  148. */
  149. TQString title;
  150. if ( mInterface->getSettings().alias != TQString() )
  151. title = mInterface->getSettings().alias;
  152. else
  153. title = mInterface->getName();
  154. KNotifyClient::event( mTray->winId(), "knemo_disconnected",
  155. title + ":\n" + i18n( "Not connected." ) );
  156. /* Wait half a second before deleting the tray so that the call
  157. * to the notification daemon has a chance to run before the
  158. * winId gets invalid.
  159. */
  160. usleep( 500000 );
  161. }
  162. // notification 'interface not existing'
  163. if ( !interfaceExists && mTray != 0L &&
  164. previousState != Interface::UNKNOWN_STATE && notify )
  165. {
  166. /* When KNemo is starting we don't show the change in connection
  167. * status as this would be annoying when KDE starts.
  168. */
  169. TQString title;
  170. if ( mInterface->getSettings().alias != TQString() )
  171. title = mInterface->getSettings().alias;
  172. else
  173. title = mInterface->getName();
  174. KNotifyClient::event( mTray->winId(), "knemo_notexisting",
  175. title + ":\n" + i18n( "Not existing." ) );
  176. /* Wait half a second before deleting the tray so that the call
  177. * to the notification daemon has a chance to run before the
  178. * winId gets invalid.
  179. */
  180. usleep( 500000 );
  181. }
  182. /* Remove the icon if
  183. * - the interface is not available and the option to hide it is selected
  184. * - the interface does not exist, the option to hide it is selected
  185. * and the other option is not selected
  186. */
  187. if ( mTray != 0L &&
  188. ( ( !interfaceAvailable && hideWhenNotAvailable ) ||
  189. ( !interfaceExists && hideWhenNotExisting && !hideWhenNotAvailable ) ) )
  190. {
  191. delete mTray;
  192. mTray = 0L;
  193. }
  194. /* Create the icon if
  195. * - the interface is available
  196. * - the interface is not available and the option to hide it is not
  197. * selected and the interface does exist
  198. * - the interface does not exist and the option to hide it is not selected
  199. * and the other option is not selected
  200. */
  201. else if ( mTray == 0L &&
  202. ( interfaceAvailable ||
  203. ( !interfaceAvailable && !hideWhenNotAvailable && interfaceExists ) ||
  204. ( !interfaceExists && !hideWhenNotExisting && !hideWhenNotAvailable ) ) )
  205. {
  206. mTray = new InterfaceTray( mInterface->getName() );
  207. TQToolTip::add( mTray, mInterface->getName() );
  208. TDEPopupMenu* menu = mTray->contextMenu();
  209. connect( menu, TQT_SIGNAL( activated( int ) ),
  210. this, TQT_SLOT( menuActivated( int ) ) );
  211. connect( mTray, TQT_SIGNAL( leftClicked() ),
  212. mInterface, TQT_SLOT( showStatusDialog() ) );
  213. connect( mTray, TQT_SIGNAL( graphSelected( bool ) ),
  214. mInterface, TQT_SLOT( showSignalPlotter( bool ) ) );
  215. connect( mTray, TQT_SIGNAL( configSelected() ),
  216. this, TQT_SLOT( showConfigDialog() ) );
  217. connect( mTray, TQT_SIGNAL( iconResized() ),
  218. this, TQT_SLOT( resizeIcon() ) );
  219. updateStatus( mInterface->getState() );
  220. updateToolTip();
  221. updateMenu();
  222. mTray->show();
  223. }
  224. // notification 'interface available'
  225. if ( interfaceAvailable && mTray != 0L &&
  226. previousState != Interface::UNKNOWN_STATE && notify )
  227. {
  228. /* When KNemo is starting we don't show the change in connection
  229. * status as this would be annoying when KDE starts.
  230. */
  231. TQString title;
  232. if ( mInterface->getSettings().alias != TQString() )
  233. title = mInterface->getSettings().alias;
  234. else
  235. title = mInterface->getName();
  236. /* Wait half a second before calling the notification daemon
  237. * so that the winId of the tray is valid when used below.
  238. */
  239. usleep( 500000 );
  240. if ( mInterface->getData().wirelessDevice )
  241. {
  242. KNotifyClient::event( mTray->winId(), "knemo_connected",
  243. title + ":\n" + i18n( "Connection established to\n" ) +
  244. mInterface->getWirelessData().essid );
  245. }
  246. else
  247. {
  248. KNotifyClient::event( mTray->winId(), "knemo_connected",
  249. title + ":\n" + i18n( "Connection established." ) );
  250. }
  251. }
  252. }
  253. void InterfaceIcon::showConfigDialog()
  254. {
  255. KNemoDaemon::sSelectedInterface = mInterface->getName();
  256. TDEProcess process;
  257. process << "tdecmshell" << "kcm_knemo";
  258. process.start( TDEProcess::DontCare );
  259. }
  260. void InterfaceIcon::menuActivated( int id )
  261. {
  262. InterfaceSettings& settings = mInterface->getSettings();
  263. TQValueVector<InterfaceCommand>::iterator it;
  264. for ( it = settings.commands.begin(); it != settings.commands.end(); it++ )
  265. {
  266. if ( (*it).id == id )
  267. {
  268. TDEProcess process;
  269. if ( (*it).runAsRoot )
  270. {
  271. process << "tdesu";
  272. process << (*it).command;
  273. }
  274. else
  275. process << TQStringList::split( ' ', (*it).command );
  276. process.start( TDEProcess::DontCare );
  277. break;
  278. }
  279. }
  280. }
  281. #include "interfaceicon.moc"