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 "rtl/ustrbuf.hxx"
36 #define SEMI_COLON ';'
50 //------------------------------------------------------------------------
52 //------------------------------------------------------------------------
58 * Returns current proxy settings for selected service type (HTTP or
59 * FTP) as a C string (in the buffer specified by host and hostSize)
63 bool GetProxySetting(ServiceType sType, char *host, size_t hostSize, UInt16 *port)
66 CFDictionaryRef proxyDict;
67 CFNumberRef enableNum;
73 proxyDict = SCDynamicStoreCopyProxies(NULL);
78 CFStringRef proxiesEnable;
79 CFStringRef proxiesProxy;
80 CFStringRef proxiesPort;
84 case sHTTP : proxiesEnable = kSCPropNetProxiesHTTPEnable;
85 proxiesProxy = kSCPropNetProxiesHTTPProxy;
86 proxiesPort = kSCPropNetProxiesHTTPPort;
88 case sHTTPS: proxiesEnable = kSCPropNetProxiesHTTPSEnable;
89 proxiesProxy = kSCPropNetProxiesHTTPSProxy;
90 proxiesPort = kSCPropNetProxiesHTTPSPort;
92 default: proxiesEnable = kSCPropNetProxiesFTPEnable;
93 proxiesProxy = kSCPropNetProxiesFTPProxy;
94 proxiesPort = kSCPropNetProxiesFTPPort;
98 enableNum = (CFNumberRef) CFDictionaryGetValue( proxyDict,
101 result = (enableNum != NULL) && (CFGetTypeID(enableNum) == CFNumberGetTypeID());
104 result = CFNumberGetValue(enableNum, kCFNumberIntType, &enable) && (enable != 0);
106 // Proxy enabled -> get hostname
109 hostStr = (CFStringRef) CFDictionaryGetValue( proxyDict,
112 result = (hostStr != NULL) && (CFGetTypeID(hostStr) == CFStringGetTypeID());
116 result = CFStringGetCString(hostStr, host, (CFIndex) hostSize, kCFStringEncodingASCII);
121 portNum = (CFNumberRef) CFDictionaryGetValue( proxyDict,
124 result = (portNum != NULL) && (CFGetTypeID(portNum) == CFNumberGetTypeID());
128 CFRelease(proxyDict);
133 result = CFNumberGetValue(portNum, kCFNumberIntType, &portInt);
136 *port = (UInt16) portInt;
139 CFRelease(proxyDict);
150 } // end private namespace
152 //------------------------------------------------------------------------------
154 MacOSXBackend::MacOSXBackend()
158 //------------------------------------------------------------------------------
160 MacOSXBackend::~MacOSXBackend(void)
164 //------------------------------------------------------------------------------
166 MacOSXBackend* MacOSXBackend::createInstance()
168 return new MacOSXBackend;
171 // ---------------------------------------------------------------------------------------
173 rtl::OUString CFStringToOUString(const CFStringRef sOrig) {
176 CFIndex nStringLen = CFStringGetLength(sOrig)+1;
178 // Allocate a c string buffer
179 char sBuffer[nStringLen];
181 CFStringGetCString(sOrig, sBuffer, nStringLen, kCFStringEncodingASCII);
185 return rtl::OUString::createFromAscii((sal_Char*)sBuffer);
188 rtl::OUString GetOUString( NSString* pStr )
191 return rtl::OUString();
192 int nLen = [pStr length];
194 return rtl::OUString();
196 rtl::OUStringBuffer aBuf( nLen+1 );
197 aBuf.setLength( nLen );
198 [pStr getCharacters: const_cast<sal_Unicode*>(aBuf.getStr())];
199 return aBuf.makeStringAndClear();
202 void MacOSXBackend::setPropertyValue(
203 rtl::OUString const &, css::uno::Any const &)
205 css::beans::UnknownPropertyException, css::beans::PropertyVetoException,
206 css::lang::IllegalArgumentException, css::lang::WrappedTargetException,
207 css::uno::RuntimeException)
209 throw css::lang::IllegalArgumentException(
211 "setPropertyValue not supported"),
212 static_cast< cppu::OWeakObject * >(this), -1);
215 css::uno::Any MacOSXBackend::getPropertyValue(
216 rtl::OUString const & PropertyName)
218 css::beans::UnknownPropertyException, css::lang::WrappedTargetException,
219 css::uno::RuntimeException)
221 if ( PropertyName == "WorkPathVariable" )
223 rtl::OUString aDocDir;
224 NSArray* pPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, true );
225 if( pPaths && [pPaths count] > 0 )
227 aDocDir = GetOUString( [pPaths objectAtIndex: 0] );
229 rtl::OUString aDocURL;
230 if( aDocDir.getLength() > 0 &&
231 osl_getFileURLFromSystemPath( aDocDir.pData, &aDocURL.pData ) == osl_File_E_None )
233 return css::uno::makeAny(
234 css::beans::Optional< css::uno::Any >(
235 true, css::uno::makeAny( aDocURL ) ) );
239 OSL_TRACE( "user documents list contains empty file path or conversion failed" );
244 OSL_TRACE( "Got nil or empty list of user document directories" );
246 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
247 } else if ( PropertyName == "ooInetFTPProxyName" )
249 ProxyEntry aFtpProxy;
251 char host[MAXHOSTNAMELEN];
255 retVal = GetProxySetting(sFTP, host, 100, &port);
259 aFtpProxy.Server = rtl::OUString::createFromAscii( host );
263 if( aFtpProxy.Server.getLength() > 0 )
265 return css::uno::makeAny(
266 css::beans::Optional< css::uno::Any >(
267 true, uno::makeAny( aFtpProxy.Server ) ) );
269 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
270 } else if ( PropertyName == "ooInetFTPProxyPort" )
272 ProxyEntry aFtpProxy;
274 char host[MAXHOSTNAMELEN];
278 retVal = GetProxySetting(sFTP, host, 100, &port);
282 aFtpProxy.Port = port;
286 if( aFtpProxy.Port > 0 )
288 return css::uno::makeAny(
289 css::beans::Optional< css::uno::Any >(
290 true, uno::makeAny( aFtpProxy.Port ) ) );
292 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
293 } else if ( PropertyName == "ooInetHTTPProxyName" )
295 ProxyEntry aHttpProxy;
297 char host[MAXHOSTNAMELEN];
301 retVal = GetProxySetting(sHTTP, host, 100, &port);
305 aHttpProxy.Server = rtl::OUString::createFromAscii( host );
309 if( aHttpProxy.Server.getLength() > 0 )
311 return css::uno::makeAny(
312 css::beans::Optional< css::uno::Any >(
313 true, uno::makeAny( aHttpProxy.Server ) ) );
315 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
316 } else if ( PropertyName == "ooInetHTTPProxyPort" )
318 ProxyEntry aHttpProxy;
320 char host[MAXHOSTNAMELEN];
324 retVal = GetProxySetting(sHTTP, host, 100, &port);
328 aHttpProxy.Port = port;
332 if( aHttpProxy.Port > 0 )
334 return css::uno::makeAny(
335 css::beans::Optional< css::uno::Any >(
336 true, uno::makeAny( aHttpProxy.Port ) ) );
338 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
339 } else if ( PropertyName == "ooInetHTTPSProxyName" )
341 ProxyEntry aHttpsProxy;
343 char host[MAXHOSTNAMELEN];
347 retVal = GetProxySetting(sHTTPS, host, 100, &port);
351 aHttpsProxy.Server = rtl::OUString::createFromAscii( host );
355 if( aHttpsProxy.Server.getLength() > 0 )
357 return css::uno::makeAny(
358 css::beans::Optional< css::uno::Any >(
359 true, uno::makeAny( aHttpsProxy.Server ) ) );
361 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
362 } else if ( PropertyName == "ooInetHTTPSProxyPort" )
364 ProxyEntry aHttpsProxy;
366 char host[MAXHOSTNAMELEN];
370 retVal = GetProxySetting(sHTTPS, host, 100, &port);
374 aHttpsProxy.Port = port;
378 if( aHttpsProxy.Port > 0 )
380 return css::uno::makeAny(
381 css::beans::Optional< css::uno::Any >(
382 true, uno::makeAny( aHttpsProxy.Port ) ) );
384 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
385 } else if ( PropertyName == "ooInetProxyType" )
387 // override default for ProxyType, which is "0" meaning "No proxies".
388 sal_Int32 nProperties = 1;
389 return css::uno::makeAny(
390 css::beans::Optional< css::uno::Any >(
391 true, uno::makeAny( nProperties ) ) );
392 } else if ( PropertyName == "ooInetNoProxy" )
394 rtl::OUString aProxyBypassList;
396 CFArrayRef rExceptionsList;
397 CFDictionaryRef rProxyDict = SCDynamicStoreCopyProxies(NULL);
402 rExceptionsList = (CFArrayRef) CFDictionaryGetValue(rProxyDict, kSCPropNetProxiesExceptionsList);
406 for (CFIndex idx = 0; idx < CFArrayGetCount(rExceptionsList); idx++)
408 CFStringRef rException = (CFStringRef) CFArrayGetValueAtIndex(rExceptionsList, idx);
411 aProxyBypassList += rtl::OUString(";");
413 aProxyBypassList += CFStringToOUString(rException);
418 CFRelease(rProxyDict);
420 // fill proxy bypass list
421 if( aProxyBypassList.getLength() > 0 )
423 return css::uno::makeAny(
424 css::beans::Optional< css::uno::Any >(
426 uno::makeAny( aProxyBypassList.replace( SPACE, SEMI_COLON ) ) ) );
428 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
430 throw css::beans::UnknownPropertyException(
431 PropertyName, static_cast< cppu::OWeakObject * >(this));
435 //------------------------------------------------------------------------------
437 rtl::OUString SAL_CALL MacOSXBackend::getBackendName(void)
439 return rtl::OUString("com.sun.star.comp.configuration.backend.MacOSXBackend");
442 //------------------------------------------------------------------------------
444 rtl::OUString SAL_CALL MacOSXBackend::getImplementationName(void)
445 throw (uno::RuntimeException)
447 return getBackendName();
450 //------------------------------------------------------------------------------
452 uno::Sequence<rtl::OUString> SAL_CALL MacOSXBackend::getBackendServiceNames(void)
454 uno::Sequence<rtl::OUString> aServiceNameList(1);
455 aServiceNameList[0] = rtl::OUString( "com.sun.star.configuration.backend.MacOSXBackend");
457 return aServiceNameList;
460 //------------------------------------------------------------------------------
462 sal_Bool SAL_CALL MacOSXBackend::supportsService(const rtl::OUString& aServiceName)
463 throw (uno::RuntimeException)
465 uno::Sequence< rtl::OUString > const svc = getBackendServiceNames();
467 for(sal_Int32 i = 0; i < svc.getLength(); ++i )
468 if(svc[i] == aServiceName)
474 //------------------------------------------------------------------------------
476 uno::Sequence<rtl::OUString> SAL_CALL MacOSXBackend::getSupportedServiceNames(void)
477 throw (uno::RuntimeException)
479 return getBackendServiceNames();
482 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */