PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 30ms app.codeStats 0ms

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

http://github.com/tomahawk-player/tomahawk
C++ | 117 lines | 85 code | 11 blank | 21 comment | 4 complexity | bbd00c1a203d52c157e34f767c58fa41 MD5 | raw file
  1/*
  2   Copyright 2009 Last.fm Ltd. 
  3      - Primarily authored by Max Howell, Jono Cole and Doug Mansell
  4
  5   This file is part of liblastfm.
  6
  7   liblastfm is free software: you can redistribute it and/or modify
  8   it under the terms of the GNU General Public License as published by
  9   the Free Software Foundation, either version 3 of the License, or
 10   (at your option) any later version.
 11
 12   liblastfm is distributed in the hope that it will be useful,
 13   but WITHOUT ANY WARRANTY; without even the implied warranty of
 14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15   GNU General Public License for more details.
 16
 17   You should have received a copy of the GNU General Public License
 18   along with liblastfm.  If not, see <http://www.gnu.org/licenses/>.
 19*/
 20
 21#include "InternetConnectionMonitor.h"
 22#include "linux/LNetworkConnectionMonitor.h"
 23#include "mac/MNetworkConnectionMonitor.h"
 24#include "win/WNetworkConnectionMonitor.h"
 25#include "NetworkConnectionMonitor.h"
 26#include "ws.h"
 27
 28lastfm::InternetConnectionMonitor::InternetConnectionMonitor( QObject *parent )
 29                                 : QObject( parent )
 30                                 , m_up( true )
 31{
 32    m_networkMonitor = createNetworkConnectionMonitor();
 33
 34    if ( m_networkMonitor )
 35    {
 36        connect( m_networkMonitor, SIGNAL( networkUp() ), this, SLOT( onNetworkUp() ) );
 37        connect( m_networkMonitor, SIGNAL( networkDown() ), this, SLOT( onNetworkDown() ) );
 38    }
 39
 40    connect( lastfm::nam(), SIGNAL( finished( QNetworkReply* ) ), this, SLOT( onFinished( QNetworkReply* ) ) );
 41}
 42
 43void
 44lastfm::InternetConnectionMonitor::onFinished( QNetworkReply* reply )
 45{
 46    if( reply->attribute( QNetworkRequest::SourceIsFromCacheAttribute).toBool() ) return;
 47
 48    switch( reply->error() )
 49    {
 50        case QNetworkReply::NoError:
 51            if ( !m_up )
 52            {
 53                m_up = true;
 54                emit up();
 55                emit connectivityChanged( m_up );
 56                qDebug() << "Internet connection is reachable :)";
 57            }
 58            break;
 59        case QNetworkReply::HostNotFoundError:
 60        case QNetworkReply::TimeoutError:
 61        case QNetworkReply::ProxyConnectionRefusedError:
 62        case QNetworkReply::ProxyConnectionClosedError:
 63        case QNetworkReply::ProxyNotFoundError:
 64        case QNetworkReply::ProxyTimeoutError:
 65        case QNetworkReply::ProxyAuthenticationRequiredError:
 66            if ( m_up )
 67            {
 68                m_up = false;
 69                emit down();
 70                emit connectivityChanged( m_up );
 71            }
 72            break;
 73        default:
 74            break;
 75    }
 76}
 77
 78void
 79lastfm::InternetConnectionMonitor::onNetworkUp()
 80{
 81#ifdef Q_OS_MAC
 82    // We don't need to check on mac as the
 83    // check is done as part of the reach api
 84        m_up = true;
 85    emit up();
 86    emit connectivityChanged( m_up );
 87    qDebug() << "Internet connection is reachable :)";
 88#else
 89    qDebug() << "Network seems to be up again. Let's try if there's internet connection!";
 90    lastfm::nam()->head( QNetworkRequest( QUrl( tr( "http://www.last.fm/" ) ) ) );
 91#endif
 92}
 93
 94void
 95lastfm::InternetConnectionMonitor::onNetworkDown()
 96{
 97    qDebug() << "Internet is unreachable :(";
 98    m_up = false;
 99    emit down();
100    emit connectivityChanged( m_up );
101}
102
103NetworkConnectionMonitor*
104lastfm::InternetConnectionMonitor::createNetworkConnectionMonitor()
105{
106    NetworkConnectionMonitor* ncm = 0;
107
108#ifdef Q_WS_X11
109    ncm = new LNetworkConnectionMonitor( this );
110#elif defined(Q_WS_WIN)
111    ncm = new WNetworkConnectionMonitor( this );
112#elif defined(Q_WS_MAC)
113    ncm = new MNetworkConnectionMonitor( this );
114#endif
115
116    return ncm;
117}