PageRenderTime 223ms CodeModel.GetById 81ms app.highlight 4ms RepoModel.GetById 136ms app.codeStats 0ms

/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
 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
23#include <QPointer>
24#include <SystemConfiguration/SCNetworkReachability.h>
25
26#include "../ws.h"
27
28MNetworkConnectionMonitor* context = 0;
29
30MNetworkConnectionMonitor::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
41MNetworkConnectionMonitor::~MNetworkConnectionMonitor()
42{
43}
44
45
46void
47MNetworkConnectionMonitor::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