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 <cppuhelper/supportsservice.hxx>
33 #include "rtl/ustrbuf.hxx"
34 #include <osl/diagnose.h>
38 #define SEMI_COLON ';'
60 * Returns current proxy settings for selected service type (HTTP or
61 * FTP) as a C string (in the buffer specified by host and hostSize)
65 bool GetProxySetting(ServiceType sType, char *host, size_t hostSize, UInt16 *port)
68 CFDictionaryRef proxyDict;
69 CFNumberRef enableNum;
75 proxyDict = SCDynamicStoreCopyProxies(NULL);
80 CFStringRef proxiesEnable;
81 CFStringRef proxiesProxy;
82 CFStringRef proxiesPort;
86 case sHTTP : proxiesEnable = kSCPropNetProxiesHTTPEnable;
87 proxiesProxy = kSCPropNetProxiesHTTPProxy;
88 proxiesPort = kSCPropNetProxiesHTTPPort;
90 case sHTTPS: proxiesEnable = kSCPropNetProxiesHTTPSEnable;
91 proxiesProxy = kSCPropNetProxiesHTTPSProxy;
92 proxiesPort = kSCPropNetProxiesHTTPSPort;
94 default: proxiesEnable = kSCPropNetProxiesFTPEnable;
95 proxiesProxy = kSCPropNetProxiesFTPProxy;
96 proxiesPort = kSCPropNetProxiesFTPPort;
100 enableNum = static_cast<CFNumberRef>(CFDictionaryGetValue( proxyDict,
103 result = (enableNum != NULL) && (CFGetTypeID(enableNum) == CFNumberGetTypeID());
106 result = CFNumberGetValue(enableNum, kCFNumberIntType, &enable) && (enable != 0);
108 // Proxy enabled -> get hostname
111 hostStr = static_cast<CFStringRef>(CFDictionaryGetValue( proxyDict,
114 result = (hostStr != NULL) && (CFGetTypeID(hostStr) == CFStringGetTypeID());
118 result = CFStringGetCString(hostStr, host, (CFIndex) hostSize, kCFStringEncodingASCII);
123 portNum = static_cast<CFNumberRef>(CFDictionaryGetValue( proxyDict,
126 result = (portNum != NULL) && (CFGetTypeID(portNum) == CFNumberGetTypeID());
130 CFRelease(proxyDict);
135 result = CFNumberGetValue(portNum, kCFNumberIntType, &portInt);
138 *port = (UInt16) portInt;
141 CFRelease(proxyDict);
152 } // end private namespace
156 MacOSXBackend::MacOSXBackend()
162 MacOSXBackend::~MacOSXBackend(void)
168 MacOSXBackend* MacOSXBackend::createInstance()
170 return new MacOSXBackend;
175 rtl::OUString CFStringToOUString(const CFStringRef sOrig) {
178 CFIndex nStringLen = CFStringGetLength(sOrig)+1;
180 // Allocate a c string buffer
181 char sBuffer[nStringLen];
183 CFStringGetCString(sOrig, sBuffer, nStringLen, kCFStringEncodingASCII);
187 return rtl::OUString::createFromAscii((sal_Char*)sBuffer);
190 rtl::OUString GetOUString( NSString* pStr )
193 return rtl::OUString();
194 int nLen = [pStr length];
196 return rtl::OUString();
198 rtl::OUStringBuffer aBuf( nLen+1 );
199 aBuf.setLength( nLen );
200 [pStr getCharacters: const_cast<sal_Unicode*>(aBuf.getStr())];
201 return aBuf.makeStringAndClear();
204 void MacOSXBackend::setPropertyValue(
205 rtl::OUString const &, css::uno::Any const &)
207 css::beans::UnknownPropertyException, css::beans::PropertyVetoException,
208 css::lang::IllegalArgumentException, css::lang::WrappedTargetException,
209 css::uno::RuntimeException, std::exception)
211 throw css::lang::IllegalArgumentException(
213 "setPropertyValue not supported"),
214 static_cast< cppu::OWeakObject * >(this), -1);
217 css::uno::Any MacOSXBackend::getPropertyValue(
218 rtl::OUString const & PropertyName)
220 css::beans::UnknownPropertyException, css::lang::WrappedTargetException,
221 css::uno::RuntimeException, std::exception)
223 if ( PropertyName == "WorkPathVariable" )
225 rtl::OUString aDocDir;
226 NSArray* pPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, true );
227 if( pPaths && [pPaths count] > 0 )
229 aDocDir = GetOUString( [pPaths objectAtIndex: 0] );
231 rtl::OUString aDocURL;
232 if( aDocDir.getLength() > 0 &&
233 osl_getFileURLFromSystemPath( aDocDir.pData, &aDocURL.pData ) == osl_File_E_None )
235 return css::uno::makeAny(
236 css::beans::Optional< css::uno::Any >(
237 true, css::uno::makeAny( aDocURL ) ) );
241 OSL_TRACE( "user documents list contains empty file path or conversion failed" );
246 OSL_TRACE( "Got nil or empty list of user document directories" );
248 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
249 } else if ( PropertyName == "ooInetFTPProxyName" )
251 ProxyEntry aFtpProxy;
253 char host[MAXHOSTNAMELEN];
257 retVal = GetProxySetting(sFTP, host, 100, &port);
261 aFtpProxy.Server = rtl::OUString::createFromAscii( host );
265 if( aFtpProxy.Server.getLength() > 0 )
267 return css::uno::makeAny(
268 css::beans::Optional< css::uno::Any >(
269 true, uno::makeAny( aFtpProxy.Server ) ) );
271 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
272 } else if ( PropertyName == "ooInetFTPProxyPort" )
274 ProxyEntry aFtpProxy;
276 char host[MAXHOSTNAMELEN];
280 retVal = GetProxySetting(sFTP, host, 100, &port);
284 aFtpProxy.Port = port;
288 if( aFtpProxy.Port > 0 )
290 return css::uno::makeAny(
291 css::beans::Optional< css::uno::Any >(
292 true, uno::makeAny( aFtpProxy.Port ) ) );
294 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
295 } else if ( PropertyName == "ooInetHTTPProxyName" )
297 ProxyEntry aHttpProxy;
299 char host[MAXHOSTNAMELEN];
303 retVal = GetProxySetting(sHTTP, host, 100, &port);
307 aHttpProxy.Server = rtl::OUString::createFromAscii( host );
311 if( aHttpProxy.Server.getLength() > 0 )
313 return css::uno::makeAny(
314 css::beans::Optional< css::uno::Any >(
315 true, uno::makeAny( aHttpProxy.Server ) ) );
317 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
318 } else if ( PropertyName == "ooInetHTTPProxyPort" )
320 ProxyEntry aHttpProxy;
322 char host[MAXHOSTNAMELEN];
326 retVal = GetProxySetting(sHTTP, host, 100, &port);
330 aHttpProxy.Port = port;
334 if( aHttpProxy.Port > 0 )
336 return css::uno::makeAny(
337 css::beans::Optional< css::uno::Any >(
338 true, uno::makeAny( aHttpProxy.Port ) ) );
340 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
341 } else if ( PropertyName == "ooInetHTTPSProxyName" )
343 ProxyEntry aHttpsProxy;
345 char host[MAXHOSTNAMELEN];
349 retVal = GetProxySetting(sHTTPS, host, 100, &port);
353 aHttpsProxy.Server = rtl::OUString::createFromAscii( host );
357 if( aHttpsProxy.Server.getLength() > 0 )
359 return css::uno::makeAny(
360 css::beans::Optional< css::uno::Any >(
361 true, uno::makeAny( aHttpsProxy.Server ) ) );
363 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
364 } else if ( PropertyName == "ooInetHTTPSProxyPort" )
366 ProxyEntry aHttpsProxy;
368 char host[MAXHOSTNAMELEN];
372 retVal = GetProxySetting(sHTTPS, host, 100, &port);
376 aHttpsProxy.Port = port;
380 if( aHttpsProxy.Port > 0 )
382 return css::uno::makeAny(
383 css::beans::Optional< css::uno::Any >(
384 true, uno::makeAny( aHttpsProxy.Port ) ) );
386 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
387 } else if ( PropertyName == "ooInetProxyType" )
389 // override default for ProxyType, which is "0" meaning "No proxies".
390 sal_Int32 nProperties = 1;
391 return css::uno::makeAny(
392 css::beans::Optional< css::uno::Any >(
393 true, uno::makeAny( nProperties ) ) );
394 } else if ( PropertyName == "ooInetNoProxy" )
396 rtl::OUString aProxyBypassList;
398 CFArrayRef rExceptionsList;
399 CFDictionaryRef rProxyDict = SCDynamicStoreCopyProxies(NULL);
404 rExceptionsList = static_cast<CFArrayRef>(CFDictionaryGetValue(rProxyDict, kSCPropNetProxiesExceptionsList));
408 for (CFIndex idx = 0; idx < CFArrayGetCount(rExceptionsList); idx++)
410 CFStringRef rException = static_cast<CFStringRef>(CFArrayGetValueAtIndex(rExceptionsList, idx));
413 aProxyBypassList += rtl::OUString(";");
415 aProxyBypassList += CFStringToOUString(rException);
420 CFRelease(rProxyDict);
422 // fill proxy bypass list
423 if( aProxyBypassList.getLength() > 0 )
425 return css::uno::makeAny(
426 css::beans::Optional< css::uno::Any >(
428 uno::makeAny( aProxyBypassList.replace( SPACE, SEMI_COLON ) ) ) );
430 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
432 throw css::beans::UnknownPropertyException(
433 PropertyName, static_cast< cppu::OWeakObject * >(this));
439 rtl::OUString SAL_CALL MacOSXBackend::getBackendName(void)
441 return rtl::OUString("com.sun.star.comp.configuration.backend.MacOSXBackend");
446 rtl::OUString SAL_CALL MacOSXBackend::getImplementationName(void)
447 throw (uno::RuntimeException, std::exception)
449 return getBackendName();
452 uno::Sequence<rtl::OUString> SAL_CALL MacOSXBackend::getBackendServiceNames(void)
454 uno::Sequence<rtl::OUString> aServiceNameList(1);
455 aServiceNameList[0] = "com.sun.star.configuration.backend.MacOSXBackend";
457 return aServiceNameList;
460 sal_Bool SAL_CALL MacOSXBackend::supportsService(const rtl::OUString& aServiceName)
461 throw (uno::RuntimeException, std::exception)
463 return cppu::supportsService(this, aServiceName);
466 uno::Sequence<rtl::OUString> SAL_CALL MacOSXBackend::getSupportedServiceNames(void)
467 throw (uno::RuntimeException, std::exception)
469 return getBackendServiceNames();
472 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */