1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 // For MAXHOSTNAMELEN constant
22 #include <sys/param.h>
25 #include <SystemConfiguration/SystemConfiguration.h>
26 #include <Foundation/NSPathUtilities.h>
29 #include "macbackend.hxx"
31 #include <com/sun/star/beans/Optional.hpp>
32 #include <com/sun/star/uno/XComponentContext.hpp>
33 #include <cppuhelper/supportsservice.hxx>
34 #include <rtl/ustrbuf.hxx>
35 #include <sal/log.hxx>
36 #include <osl/diagnose.h>
40 #define SEMI_COLON ';'
52 * Returns current proxy settings for selected service type (HTTP or
53 * FTP) as a C string (in the buffer specified by host and hostSize)
57 bool GetProxySetting(ServiceType sType, char *host, size_t hostSize, UInt16 *port)
60 CFDictionaryRef proxyDict;
61 CFNumberRef enableNum;
67 proxyDict = SCDynamicStoreCopyProxies(nullptr);
72 CFStringRef proxiesEnable;
73 CFStringRef proxiesProxy;
74 CFStringRef proxiesPort;
78 case sHTTP : proxiesEnable = kSCPropNetProxiesHTTPEnable;
79 proxiesProxy = kSCPropNetProxiesHTTPProxy;
80 proxiesPort = kSCPropNetProxiesHTTPPort;
82 case sHTTPS: proxiesEnable = kSCPropNetProxiesHTTPSEnable;
83 proxiesProxy = kSCPropNetProxiesHTTPSProxy;
84 proxiesPort = kSCPropNetProxiesHTTPSPort;
86 default: proxiesEnable = kSCPropNetProxiesFTPEnable;
87 proxiesProxy = kSCPropNetProxiesFTPProxy;
88 proxiesPort = kSCPropNetProxiesFTPPort;
92 enableNum = static_cast<CFNumberRef>(CFDictionaryGetValue( proxyDict,
95 result = (enableNum != nullptr) && (CFGetTypeID(enableNum) == CFNumberGetTypeID());
98 result = CFNumberGetValue(enableNum, kCFNumberIntType, &enable) && (enable != 0);
100 // Proxy enabled -> get hostname
103 hostStr = static_cast<CFStringRef>(CFDictionaryGetValue( proxyDict,
106 result = (hostStr != nullptr) && (CFGetTypeID(hostStr) == CFStringGetTypeID());
110 result = CFStringGetCString(hostStr, host, static_cast<CFIndex>(hostSize), kCFStringEncodingASCII);
115 portNum = static_cast<CFNumberRef>(CFDictionaryGetValue( proxyDict,
118 result = (portNum != nullptr) && (CFGetTypeID(portNum) == CFNumberGetTypeID());
122 CFRelease(proxyDict);
127 result = CFNumberGetValue(portNum, kCFNumberIntType, &portInt);
130 *port = static_cast<UInt16>(portInt);
133 CFRelease(proxyDict);
144 } // unnamed namespace
146 MacOSXBackend::MacOSXBackend()
150 MacOSXBackend::~MacOSXBackend(void)
154 static OUString CFStringToOUString(const CFStringRef sOrig) {
157 CFIndex nStringLen = CFStringGetLength(sOrig)+1;
159 // Allocate a c string buffer
160 char sBuffer[nStringLen];
162 CFStringGetCString(sOrig, sBuffer, nStringLen, kCFStringEncodingASCII);
166 return OUString::createFromAscii(sBuffer);
169 static OUString GetOUString( NSString* pStr )
173 int nLen = [pStr length];
177 OUStringBuffer aBuf( nLen+1 );
178 aBuf.setLength( nLen );
180 reinterpret_cast<unichar *>(const_cast<sal_Unicode*>(aBuf.getStr()))];
181 return aBuf.makeStringAndClear();
184 void MacOSXBackend::setPropertyValue(
185 OUString const &, css::uno::Any const &)
187 throw css::lang::IllegalArgumentException(
188 "setPropertyValue not supported",
189 static_cast< cppu::OWeakObject * >(this), -1);
192 css::uno::Any MacOSXBackend::getPropertyValue(
193 OUString const & PropertyName)
195 if ( PropertyName == "WorkPathVariable" )
198 NSArray* pPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, true );
199 if( pPaths && [pPaths count] > 0 )
201 aDocDir = GetOUString( [pPaths objectAtIndex: 0] );
204 if( aDocDir.getLength() > 0 &&
205 osl_getFileURLFromSystemPath( aDocDir.pData, &aDocURL.pData ) == osl_File_E_None )
207 return css::uno::Any(
208 css::beans::Optional< css::uno::Any >(
209 true, css::uno::Any( aDocURL ) ) );
213 SAL_WARN("shell", "user documents list contains empty file path or conversion failed" );
218 SAL_WARN("shell", "Got nil or empty list of user document directories" );
220 return css::uno::Any(css::beans::Optional< css::uno::Any >());
221 } else if ( PropertyName == "ooInetFTPProxyName" )
223 char host[MAXHOSTNAMELEN];
227 retVal = GetProxySetting(sFTP, host, 100, &port);
231 auto const Server = OUString::createFromAscii( host );
232 if( Server.getLength() > 0 )
234 return css::uno::Any(
235 css::beans::Optional< css::uno::Any >(
236 true, uno::Any( Server ) ) );
239 return css::uno::Any(css::beans::Optional< css::uno::Any >());
240 } else if ( PropertyName == "ooInetFTPProxyPort" )
242 char host[MAXHOSTNAMELEN];
246 retVal = GetProxySetting(sFTP, host, 100, &port);
248 if (retVal && port > 0)
250 return css::uno::Any(
251 css::beans::Optional< css::uno::Any >(
252 true, uno::Any( sal_Int32(port) ) ) );
254 return css::uno::Any(css::beans::Optional< css::uno::Any >());
255 } else if ( PropertyName == "ooInetHTTPProxyName" )
257 char host[MAXHOSTNAMELEN];
261 retVal = GetProxySetting(sHTTP, host, 100, &port);
265 auto const Server = OUString::createFromAscii( host );
266 if( Server.getLength() > 0 )
268 return css::uno::Any(
269 css::beans::Optional< css::uno::Any >(
270 true, uno::Any( Server ) ) );
273 return css::uno::Any(css::beans::Optional< css::uno::Any >());
274 } else if ( PropertyName == "ooInetHTTPProxyPort" )
276 char host[MAXHOSTNAMELEN];
280 retVal = GetProxySetting(sHTTP, host, 100, &port);
282 if (retVal && port > 0)
284 return css::uno::Any(
285 css::beans::Optional< css::uno::Any >(
286 true, uno::Any( sal_Int32(port) ) ) );
288 return css::uno::Any(css::beans::Optional< css::uno::Any >());
289 } else if ( PropertyName == "ooInetHTTPSProxyName" )
291 char host[MAXHOSTNAMELEN];
295 retVal = GetProxySetting(sHTTPS, host, 100, &port);
299 auto const Server = OUString::createFromAscii( host );
300 if( Server.getLength() > 0 )
302 return css::uno::Any(
303 css::beans::Optional< css::uno::Any >(
304 true, uno::Any( Server ) ) );
307 return css::uno::Any(css::beans::Optional< css::uno::Any >());
308 } else if ( PropertyName == "ooInetHTTPSProxyPort" )
310 char host[MAXHOSTNAMELEN];
314 retVal = GetProxySetting(sHTTPS, host, 100, &port);
316 if (retVal && port > 0)
318 return css::uno::Any(
319 css::beans::Optional< css::uno::Any >(
320 true, uno::Any( sal_Int32(port) ) ) );
322 return css::uno::Any(css::beans::Optional< css::uno::Any >());
323 } else if ( PropertyName == "ooInetProxyType" )
325 // override default for ProxyType, which is "0" meaning "No proxies".
326 return css::uno::Any(
327 css::beans::Optional< css::uno::Any >(
328 true, uno::Any( sal_Int32(1) ) ) );
329 } else if ( PropertyName == "ooInetNoProxy" )
331 OUString aProxyBypassList;
333 CFArrayRef rExceptionsList;
334 CFDictionaryRef rProxyDict = SCDynamicStoreCopyProxies(nullptr);
337 rExceptionsList = nullptr;
339 rExceptionsList = static_cast<CFArrayRef>(CFDictionaryGetValue(rProxyDict, kSCPropNetProxiesExceptionsList));
343 for (CFIndex idx = 0; idx < CFArrayGetCount(rExceptionsList); idx++)
345 CFStringRef rException = static_cast<CFStringRef>(CFArrayGetValueAtIndex(rExceptionsList, idx));
348 aProxyBypassList += ";";
350 aProxyBypassList += CFStringToOUString(rException);
355 CFRelease(rProxyDict);
357 // fill proxy bypass list
358 if( aProxyBypassList.getLength() > 0 )
360 return css::uno::Any(
361 css::beans::Optional< css::uno::Any >(
363 uno::Any( aProxyBypassList.replace( SPACE, SEMI_COLON ) ) ) );
365 return css::uno::Any(css::beans::Optional< css::uno::Any >());
367 throw css::beans::UnknownPropertyException(
368 PropertyName, static_cast< cppu::OWeakObject * >(this));
372 OUString SAL_CALL MacOSXBackend::getImplementationName(void)
374 return "com.sun.star.comp.configuration.backend.MacOSXBackend";
377 sal_Bool SAL_CALL MacOSXBackend::supportsService(const OUString& aServiceName)
379 return cppu::supportsService(this, aServiceName);
382 uno::Sequence<OUString> SAL_CALL MacOSXBackend::getSupportedServiceNames(void)
384 return { "com.sun.star.configuration.backend.MacOSXBackend" };
387 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
388 shell_MacOSXBackend_get_implementation(
389 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
391 return cppu::acquire(new MacOSXBackend());
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */