/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. 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 <SystemConfiguration/SystemConfiguration.h>
  17. struct ProxyDict
  18. {
  19. ProxyDict();
  20. int port;
  21. QString host;
  22. bool isProxyEnabled() const { return port > 0 && host.size(); }
  23. };
  24. inline ProxyDict::ProxyDict() : port( 0 )
  25. {
  26. // Get the dictionary.
  27. CFDictionaryRef proxyDict = SCDynamicStoreCopyProxies( NULL );
  28. bool result = (proxyDict != NULL);
  29. // Get the enable flag. This isn't a CFBoolean, but a CFNumber.
  30. CFNumberRef enableNum;
  31. int enable;
  32. if (result) {
  33. enableNum = (CFNumberRef) CFDictionaryGetValue( proxyDict, kSCPropNetProxiesHTTPEnable );
  34. result = (enableNum != NULL) && (CFGetTypeID(enableNum) == CFNumberGetTypeID());
  35. }
  36. if (result)
  37. result = CFNumberGetValue( enableNum, kCFNumberIntType, &enable ) && (enable != 0);
  38. // Get the proxy host. DNS names must be in ASCII. If you
  39. // put a non-ASCII character in the "Secure Web Proxy"
  40. // field in the Network preferences panel, the CFStringGetCString
  41. // function will fail and this function will return false.
  42. CFStringRef hostStr;
  43. if (result) {
  44. hostStr = (CFStringRef) CFDictionaryGetValue( proxyDict, kSCPropNetProxiesHTTPProxy );
  45. result = (hostStr != NULL) && (CFGetTypeID(hostStr) == CFStringGetTypeID());
  46. }
  47. if (result)
  48. host = lastfm::CFStringToQString( hostStr );
  49. // get the proxy port
  50. CFNumberRef portNum;
  51. if (result) {
  52. portNum = (CFNumberRef) CFDictionaryGetValue( proxyDict, kSCPropNetProxiesHTTPPort );
  53. result = (portNum != NULL) && (CFGetTypeID(portNum) == CFNumberGetTypeID());
  54. }
  55. if (result)
  56. result = CFNumberGetValue( portNum, kCFNumberIntType, &port );
  57. // clean up.
  58. if (proxyDict != NULL)
  59. CFRelease( proxyDict );
  60. }