| 1 | /* |
| 2 | Copyright 2009 Last.fm Ltd. |
| 3 | - Primarily authored by Jono Cole and Michael Coffey |
| 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 "MNetworkConnectionMonitor.h" |
| 22 | #include "moc_MNetworkConnectionMonitor.cpp" |
| 23 | #include "../ws.h" |
| 24 | |
| 25 | #include <QPointer> |
| 26 | #include <SystemConfiguration/SCNetworkReachability.h> |
| 27 | |
| 28 | lastfm::MNetworkConnectionMonitor* context = 0; |
| 29 | |
| 30 | lastfm::MNetworkConnectionMonitor::MNetworkConnectionMonitor( QObject* parent ) : |
| 31 | NetworkConnectionMonitor( parent ) |
| 32 | { |
| 33 | context = this; |
| 34 | |
| 35 | SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithName( NULL, LASTFM_WS_HOSTNAME ); |
| 36 | SCNetworkReachabilityScheduleWithRunLoop( ref, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode ); |
| 37 | SCNetworkReachabilitySetCallback( ref, callback, NULL ); |
| 38 | CFRelease( ref ); |
| 39 | } |
| 40 | |
| 41 | lastfm::MNetworkConnectionMonitor::~MNetworkConnectionMonitor() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | |
| 46 | void |
| 47 | lastfm::MNetworkConnectionMonitor::callback( SCNetworkReachabilityRef target, |
| 48 | SCNetworkConnectionFlags flags, |
| 49 | void * ) |
| 50 | { |
| 51 | static bool up = true; |
| 52 | |
| 53 | // I couldn't find any diffinitive usage examples for these flags |
| 54 | // so I had to guess, since I can't test, eg. dial up :( |
| 55 | |
| 56 | bool b; |
| 57 | if (flags & kSCNetworkFlagsConnectionRequired) |
| 58 | b = false; |
| 59 | else |
| 60 | b = flags & (kSCNetworkFlagsReachable | kSCNetworkFlagsTransientConnection | kSCNetworkFlagsConnectionAutomatic); |
| 61 | |
| 62 | // basically, avoids telling everyone that we're up already on startup |
| 63 | if (up == b) |
| 64 | return; |
| 65 | |
| 66 | up = b; |
| 67 | |
| 68 | context->setConnected(b); |
| 69 | } |
| 70 |