2
Copyright 2009 Last.fm Ltd.
3
- Primarily authored by Max Howell, Jono Cole and Doug Mansell
5
This file is part of liblastfm.
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.
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.
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/>.
21
#include "InternetConnectionMonitor.h"
22
#include "linux/LNetworkConnectionMonitor.h"
23
#include "mac/MNetworkConnectionMonitor.h"
24
#include "win/WNetworkConnectionMonitor.h"
25
#include "NetworkConnectionMonitor.h"
30
class lastfm::InternetConnectionMonitorPrivate
34
NetworkConnectionMonitor* m_networkMonitor;
37
lastfm::InternetConnectionMonitor::InternetConnectionMonitor( QObject *parent )
39
, d( new InternetConnectionMonitorPrivate)
42
d->m_networkMonitor = createNetworkConnectionMonitor();
44
if ( d->m_networkMonitor )
46
connect( d->m_networkMonitor, SIGNAL( networkUp() ), this, SLOT( onNetworkUp() ) );
47
connect( d->m_networkMonitor, SIGNAL( networkDown() ), this, SLOT( onNetworkDown() ) );
50
connect( nam(), SIGNAL( finished( QNetworkReply* ) ), this, SLOT( onFinished( QNetworkReply* ) ) );
53
lastfm::InternetConnectionMonitor::~InternetConnectionMonitor()
58
bool lastfm::InternetConnectionMonitor::isDown() const
63
bool lastfm::InternetConnectionMonitor::isUp() const
69
lastfm::InternetConnectionMonitor::onFinished( QNetworkReply* reply )
71
if( reply->attribute( QNetworkRequest::SourceIsFromCacheAttribute).toBool() ) return;
73
switch( reply->error() )
75
case QNetworkReply::NoError:
80
emit connectivityChanged( d->m_up );
81
qDebug() << "Internet connection is reachable :)";
84
case QNetworkReply::HostNotFoundError:
85
case QNetworkReply::TimeoutError:
86
case QNetworkReply::ProxyConnectionRefusedError:
87
case QNetworkReply::ProxyConnectionClosedError:
88
case QNetworkReply::ProxyNotFoundError:
89
case QNetworkReply::ProxyTimeoutError:
90
case QNetworkReply::ProxyAuthenticationRequiredError:
95
emit connectivityChanged( d->m_up );
104
lastfm::InternetConnectionMonitor::onNetworkUp()
107
// We don't need to check on mac as the
108
// check is done as part of the reach api
112
emit connectivityChanged( d->m_up );
113
qDebug() << "Internet connection is reachable :)";
115
qDebug() << "Network seems to be up again. Let's try if there's internet connection!";
116
nam()->head( QNetworkRequest( QUrl( "http://www.last.fm/" ) ) );
121
lastfm::InternetConnectionMonitor::onNetworkDown()
123
qDebug() << "Internet is unreachable :(";
126
emit connectivityChanged( d->m_up );
129
lastfm::NetworkConnectionMonitor*
130
lastfm::InternetConnectionMonitor::createNetworkConnectionMonitor()
132
NetworkConnectionMonitor* ncm = 0;
135
ncm = new LNetworkConnectionMonitor( this );
136
#elif defined(Q_OS_WIN) && ! defined __MINGW32__
137
ncm = new WNetworkConnectionMonitor( this );
138
#elif defined(Q_OS_MAC)
139
ncm = new MNetworkConnectionMonitor( this );