/thirdparty/liblastfm2/src/ws/InternetConnectionMonitor.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 117 lines · 85 code · 11 blank · 21 comment · 5 complexity · bbd00c1a203d52c157e34f767c58fa41 MD5 · raw file

  1. /*
  2. Copyright 2009 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 "InternetConnectionMonitor.h"
  17. #include "linux/LNetworkConnectionMonitor.h"
  18. #include "mac/MNetworkConnectionMonitor.h"
  19. #include "win/WNetworkConnectionMonitor.h"
  20. #include "NetworkConnectionMonitor.h"
  21. #include "ws.h"
  22. lastfm::InternetConnectionMonitor::InternetConnectionMonitor( QObject *parent )
  23. : QObject( parent )
  24. , m_up( true )
  25. {
  26. m_networkMonitor = createNetworkConnectionMonitor();
  27. if ( m_networkMonitor )
  28. {
  29. connect( m_networkMonitor, SIGNAL( networkUp() ), this, SLOT( onNetworkUp() ) );
  30. connect( m_networkMonitor, SIGNAL( networkDown() ), this, SLOT( onNetworkDown() ) );
  31. }
  32. connect( lastfm::nam(), SIGNAL( finished( QNetworkReply* ) ), this, SLOT( onFinished( QNetworkReply* ) ) );
  33. }
  34. void
  35. lastfm::InternetConnectionMonitor::onFinished( QNetworkReply* reply )
  36. {
  37. if( reply->attribute( QNetworkRequest::SourceIsFromCacheAttribute).toBool() ) return;
  38. switch( reply->error() )
  39. {
  40. case QNetworkReply::NoError:
  41. if ( !m_up )
  42. {
  43. m_up = true;
  44. emit up();
  45. emit connectivityChanged( m_up );
  46. qDebug() << "Internet connection is reachable :)";
  47. }
  48. break;
  49. case QNetworkReply::HostNotFoundError:
  50. case QNetworkReply::TimeoutError:
  51. case QNetworkReply::ProxyConnectionRefusedError:
  52. case QNetworkReply::ProxyConnectionClosedError:
  53. case QNetworkReply::ProxyNotFoundError:
  54. case QNetworkReply::ProxyTimeoutError:
  55. case QNetworkReply::ProxyAuthenticationRequiredError:
  56. if ( m_up )
  57. {
  58. m_up = false;
  59. emit down();
  60. emit connectivityChanged( m_up );
  61. }
  62. break;
  63. default:
  64. break;
  65. }
  66. }
  67. void
  68. lastfm::InternetConnectionMonitor::onNetworkUp()
  69. {
  70. #ifdef Q_OS_MAC
  71. // We don't need to check on mac as the
  72. // check is done as part of the reach api
  73. m_up = true;
  74. emit up();
  75. emit connectivityChanged( m_up );
  76. qDebug() << "Internet connection is reachable :)";
  77. #else
  78. qDebug() << "Network seems to be up again. Let's try if there's internet connection!";
  79. lastfm::nam()->head( QNetworkRequest( QUrl( tr( "http://www.last.fm/" ) ) ) );
  80. #endif
  81. }
  82. void
  83. lastfm::InternetConnectionMonitor::onNetworkDown()
  84. {
  85. qDebug() << "Internet is unreachable :(";
  86. m_up = false;
  87. emit down();
  88. emit connectivityChanged( m_up );
  89. }
  90. NetworkConnectionMonitor*
  91. lastfm::InternetConnectionMonitor::createNetworkConnectionMonitor()
  92. {
  93. NetworkConnectionMonitor* ncm = 0;
  94. #ifdef Q_WS_X11
  95. ncm = new LNetworkConnectionMonitor( this );
  96. #elif defined(Q_WS_WIN)
  97. ncm = new WNetworkConnectionMonitor( this );
  98. #elif defined(Q_WS_MAC)
  99. ncm = new MNetworkConnectionMonitor( this );
  100. #endif
  101. return ncm;
  102. }