PageRenderTime 61ms CodeModel.GetById 19ms app.highlight 6ms RepoModel.GetById 21ms app.codeStats 1ms

/thirdparty/liblastfm2/src/ws/mac/ProxyDict.h

http://github.com/tomahawk-player/tomahawk
C++ Header | 75 lines | 37 code | 11 blank | 27 comment | 21 complexity | b0eeedaa5ad5e8871646f6081c479cab 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#include <SystemConfiguration/SystemConfiguration.h>
21
22
23struct ProxyDict
24{
25    ProxyDict();
26
27    int port;
28    QString host;
29    
30    bool isProxyEnabled() const { return port > 0 && host.size(); }
31};
32
33
34inline ProxyDict::ProxyDict() : port( 0 )
35{
36    // Get the dictionary.
37    CFDictionaryRef proxyDict = SCDynamicStoreCopyProxies( NULL );
38    bool result = (proxyDict != NULL);
39
40    // Get the enable flag.  This isn't a CFBoolean, but a CFNumber.
41    CFNumberRef enableNum;
42    int enable;
43    if (result) {
44        enableNum = (CFNumberRef) CFDictionaryGetValue( proxyDict, kSCPropNetProxiesHTTPEnable );
45        result = (enableNum != NULL) && (CFGetTypeID(enableNum) == CFNumberGetTypeID());
46    }
47    if (result)
48        result = CFNumberGetValue( enableNum, kCFNumberIntType, &enable ) && (enable != 0);
49
50    // Get the proxy host.  DNS names must be in ASCII.  If you 
51    // put a non-ASCII character  in the "Secure Web Proxy"
52    // field in the Network preferences panel, the CFStringGetCString
53    // function will fail and this function will return false.
54    CFStringRef hostStr;
55    if (result) {
56        hostStr = (CFStringRef) CFDictionaryGetValue( proxyDict, kSCPropNetProxiesHTTPProxy );
57        result = (hostStr != NULL) && (CFGetTypeID(hostStr) == CFStringGetTypeID());
58    }
59    if (result)
60        host = lastfm::CFStringToQString( hostStr );
61
62    // get the proxy port
63    CFNumberRef portNum;
64
65    if (result) {
66        portNum = (CFNumberRef) CFDictionaryGetValue( proxyDict, kSCPropNetProxiesHTTPPort );
67        result = (portNum != NULL) && (CFGetTypeID(portNum) == CFNumberGetTypeID());
68    }
69    if (result)
70        result = CFNumberGetValue( portNum, kCFNumberIntType, &port );
71
72    // clean up.
73    if (proxyDict != NULL)
74        CFRelease( proxyDict );
75}