/thirdparty/liblastfm2/src/ws/mac/MNetworkConnectionMonitor_mac.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 70 lines · 33 code · 15 blank · 22 comment · 3 complexity · 436502edbc238e3c063aae18d864a082 MD5 · raw file

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