/thirdparty/liblastfm2/src/ws/linux/LNetworkConnectionMonitor_linux.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 86 lines · 53 code · 12 blank · 21 comment · 11 complexity · 55835795ca7f12b9d9ef797be2c8a6c0 MD5 · raw file

  1. /*
  2. Copyright 2010 Last.fm Ltd.
  3. - Primarily authored by Max Howell, Jono Cole and Doug Mansell
  4. This file is part of liblastfm.
  5. liblastfm is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. liblastfm is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with liblastfm. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "LNetworkConnectionMonitor.h"
  17. #include <QDBusConnection>
  18. #include <QDBusInterface>
  19. #include <QDBusReply>
  20. LNetworkConnectionMonitor::LNetworkConnectionMonitor( QObject* parent ) :
  21. NetworkConnectionMonitor( parent )
  22. {
  23. m_nmInterface = new QDBusInterface( QString( "org.freedesktop.NetworkManager" ),
  24. QString( "/org/freedesktop/NetworkManager" ),
  25. QString( "org.freedesktop.NetworkManager" ),
  26. QDBusConnection::systemBus(),
  27. this );
  28. //get current connection state
  29. QDBusInterface* dbusInterface = new QDBusInterface( QString( "org.freedesktop.NetworkManager" ),
  30. QString( "/org/freedesktop/NetworkManager" ),
  31. QString( "org.freedesktop.DBus.Properties" ),
  32. QDBusConnection::systemBus(),
  33. this );
  34. QDBusReply<QVariant> reply = dbusInterface->call( "Get", "org.freedesktop.NetworkManager", "state" );
  35. if ( reply.isValid() )
  36. {
  37. if ( reply.value() == Connected )
  38. {
  39. setConnected( true );
  40. }
  41. else if ( reply.value() == Disconnected )
  42. {
  43. setConnected( false );
  44. }
  45. }
  46. else
  47. {
  48. qDebug() << "Error: " << reply.error();
  49. }
  50. delete dbusInterface;
  51. //connect network manager signals
  52. connect( m_nmInterface, SIGNAL( StateChange( uint ) ), this, SLOT( onStateChange( uint ) ) );
  53. }
  54. LNetworkConnectionMonitor::~LNetworkConnectionMonitor()
  55. {
  56. delete m_nmInterface;
  57. }
  58. void
  59. LNetworkConnectionMonitor::onStateChange( uint newState )
  60. {
  61. qDebug() << "Networkmanager state change!";
  62. if ( newState == Disconnected )
  63. {
  64. setConnected( false );
  65. }
  66. else if ( newState == Connected )
  67. {
  68. setConnected( true );
  69. }
  70. }