1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: macbelayer.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_shell.hxx"
34 // For MAXHOSTNAMELEN constant
35 #include <sys/param.h>
38 #include <SystemConfiguration/SystemConfiguration.h>
39 #include <Foundation/NSPathUtilities.h>
42 #include "macbelayer.hxx"
44 #include "rtl/ustrbuf.hxx"
48 #define SEMI_COLON ';'
62 //------------------------------------------------------------------------
64 //------------------------------------------------------------------------
70 * Returns current proxy settings for selected service type (HTTP or
71 * FTP) as a C string (in the buffer specified by host and hostSize)
75 bool GetProxySetting(ServiceType sType
, char *host
, size_t hostSize
, UInt16
*port
)
78 CFDictionaryRef proxyDict
;
79 CFNumberRef enableNum
;
85 proxyDict
= SCDynamicStoreCopyProxies(NULL
);
90 CFStringRef proxiesEnable
;
91 CFStringRef proxiesProxy
;
92 CFStringRef proxiesPort
;
96 case sHTTP
: proxiesEnable
= kSCPropNetProxiesHTTPEnable
;
97 proxiesProxy
= kSCPropNetProxiesHTTPProxy
;
98 proxiesPort
= kSCPropNetProxiesHTTPPort
;
100 case sHTTPS
: proxiesEnable
= kSCPropNetProxiesHTTPSEnable
;
101 proxiesProxy
= kSCPropNetProxiesHTTPSProxy
;
102 proxiesPort
= kSCPropNetProxiesHTTPSPort
;
104 default: proxiesEnable
= kSCPropNetProxiesFTPEnable
;
105 proxiesProxy
= kSCPropNetProxiesFTPProxy
;
106 proxiesPort
= kSCPropNetProxiesFTPPort
;
110 enableNum
= (CFNumberRef
) CFDictionaryGetValue( proxyDict
,
113 result
= (enableNum
!= NULL
) && (CFGetTypeID(enableNum
) == CFNumberGetTypeID());
116 result
= CFNumberGetValue(enableNum
, kCFNumberIntType
, &enable
) && (enable
!= 0);
118 // Proxy enabled -> get hostname
121 hostStr
= (CFStringRef
) CFDictionaryGetValue( proxyDict
,
124 result
= (hostStr
!= NULL
) && (CFGetTypeID(hostStr
) == CFStringGetTypeID());
128 result
= CFStringGetCString(hostStr
, host
, (CFIndex
) hostSize
, kCFStringEncodingASCII
);
133 portNum
= (CFNumberRef
) CFDictionaryGetValue( proxyDict
,
136 result
= (portNum
!= NULL
) && (CFGetTypeID(portNum
) == CFNumberGetTypeID());
140 CFRelease(proxyDict
);
145 result
= CFNumberGetValue(portNum
, kCFNumberIntType
, &portInt
);
148 *port
= (UInt16
) portInt
;
151 CFRelease(proxyDict
);
162 } // end private namespace
164 //------------------------------------------------------------------------------
166 MacOSXLayer::MacOSXLayer( const uno::Reference
<uno::XComponentContext
>& xContext
)
168 //Create instance of LayerContentDescriber Service
169 rtl::OUString
const k_sLayerDescriberService(
170 RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.configuration.backend.LayerDescriber"));
172 typedef uno::Reference
<backend::XLayerContentDescriber
> LayerDescriber
;
173 uno::Reference
< lang::XMultiComponentFactory
> xServiceManager
= xContext
->getServiceManager();
174 if( xServiceManager
.is() )
176 m_xLayerContentDescriber
= LayerDescriber::query(
177 xServiceManager
->createInstanceWithContext(k_sLayerDescriberService
, xContext
));
181 OSL_TRACE("Could not retrieve ServiceManager");
186 //------------------------------------------------------------------------------
188 rtl::OUString
CFStringToOUString(const CFStringRef sOrig
) {
191 CFIndex nStringLen
= CFStringGetLength(sOrig
)+1;
193 // Allocate a c string buffer
194 char sBuffer
[nStringLen
];
196 CFStringGetCString(sOrig
, sBuffer
, nStringLen
, kCFStringEncodingASCII
);
200 return rtl::OUString::createFromAscii((sal_Char
*)sBuffer
);
203 void SAL_CALL
MacOSXLayer::readData(
204 const uno::Reference
<backend::XLayerHandler
>& xHandler
)
205 throw ( backend::MalformedDataException
,
206 lang::NullPointerException
,
207 lang::WrappedTargetException
,
208 uno::RuntimeException
)
211 if (m_xLayerContentDescriber
.is())
213 rtl::OUString aProxyBypassList
;
215 CFArrayRef rExceptionsList
;
216 CFDictionaryRef rProxyDict
= SCDynamicStoreCopyProxies(NULL
);
219 rExceptionsList
= false;
221 rExceptionsList
= (CFArrayRef
) CFDictionaryGetValue(rProxyDict
, kSCPropNetProxiesExceptionsList
);
225 for (CFIndex idx
= 0; idx
< CFArrayGetCount(rExceptionsList
); idx
++)
227 CFStringRef rException
= (CFStringRef
) CFArrayGetValueAtIndex(rExceptionsList
, idx
);
230 aProxyBypassList
+= rtl::OUString::createFromAscii( ";" );
232 aProxyBypassList
+= CFStringToOUString(rException
);
237 CFRelease(rProxyDict
);
239 // override default for ProxyType, which is "0" meaning "No proxies".
240 // CAUTION: if you add properties, please increase the sequence size here !
241 uno::Sequence
<backend::PropertyInfo
> aPropInfoList(8);
242 sal_Int32 nProperties
= 1;
244 aPropInfoList
[0].Name
= rtl::OUString(
245 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetProxyType") );
246 aPropInfoList
[0].Type
= rtl::OUString(
247 RTL_CONSTASCII_USTRINGPARAM( "int" ) );
248 aPropInfoList
[0].Protected
= sal_False
;
249 aPropInfoList
[0].Value
= uno::makeAny( nProperties
);
251 // fill proxy bypass list
252 if( aProxyBypassList
.getLength() > 0 )
254 aPropInfoList
[nProperties
].Name
= rtl::OUString(
255 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetNoProxy") );
256 aPropInfoList
[nProperties
].Type
= rtl::OUString(
257 RTL_CONSTASCII_USTRINGPARAM( "string" ) );
258 aPropInfoList
[nProperties
].Protected
= sal_False
;
259 aPropInfoList
[nProperties
++].Value
= uno::makeAny( aProxyBypassList
.replace( SPACE
, SEMI_COLON
) );
262 ProxyEntry aHttpProxy
;
263 ProxyEntry aHttpsProxy
;
264 ProxyEntry aFtpProxy
;
266 char host
[MAXHOSTNAMELEN
];
270 retVal
= GetProxySetting(sHTTP
, host
, 100, &port
);
274 aHttpProxy
.Server
= rtl::OUString::createFromAscii( host
);
275 aHttpProxy
.Port
= port
;
278 retVal
= GetProxySetting(sHTTPS
, host
, 100, &port
);
282 aHttpsProxy
.Server
= rtl::OUString::createFromAscii( host
);
283 aHttpsProxy
.Port
= port
;
286 retVal
= GetProxySetting(sFTP
, host
, 100, &port
);
290 aFtpProxy
.Server
= rtl::OUString::createFromAscii( host
);
291 aFtpProxy
.Port
= port
;
295 if( aHttpProxy
.Server
.getLength() > 0 )
297 aPropInfoList
[nProperties
].Name
= rtl::OUString(
298 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetHTTPProxyName") );
299 aPropInfoList
[nProperties
].Type
= rtl::OUString(
300 RTL_CONSTASCII_USTRINGPARAM( "string" ) );
301 aPropInfoList
[nProperties
].Protected
= sal_False
;
302 aPropInfoList
[nProperties
++].Value
= uno::makeAny( aHttpProxy
.Server
);
306 if( aHttpProxy
.Port
> 0 )
308 aPropInfoList
[nProperties
].Name
= rtl::OUString(
309 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetHTTPProxyPort") );
310 aPropInfoList
[nProperties
].Type
= rtl::OUString(
311 RTL_CONSTASCII_USTRINGPARAM( "int" ) );
312 aPropInfoList
[nProperties
].Protected
= sal_False
;
313 aPropInfoList
[nProperties
++].Value
= uno::makeAny( aHttpProxy
.Port
);
317 if( aHttpsProxy
.Server
.getLength() > 0 )
319 aPropInfoList
[nProperties
].Name
= rtl::OUString(
320 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetHTTPSProxyName") );
321 aPropInfoList
[nProperties
].Type
= rtl::OUString(
322 RTL_CONSTASCII_USTRINGPARAM( "string" ) );
323 aPropInfoList
[nProperties
].Protected
= sal_False
;
324 aPropInfoList
[nProperties
++].Value
= uno::makeAny( aHttpsProxy
.Server
);
328 if( aHttpsProxy
.Port
> 0 )
330 aPropInfoList
[nProperties
].Name
= rtl::OUString(
331 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetHTTPSProxyPort") );
332 aPropInfoList
[nProperties
].Type
= rtl::OUString(
333 RTL_CONSTASCII_USTRINGPARAM( "int" ) );
334 aPropInfoList
[nProperties
].Protected
= sal_False
;
335 aPropInfoList
[nProperties
++].Value
= uno::makeAny( aHttpsProxy
.Port
);
339 if( aFtpProxy
.Server
.getLength() > 0 )
341 aPropInfoList
[nProperties
].Name
= rtl::OUString(
342 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetFTPProxyName") );
343 aPropInfoList
[nProperties
].Type
= rtl::OUString(
344 RTL_CONSTASCII_USTRINGPARAM( "string" ) );
345 aPropInfoList
[nProperties
].Protected
= sal_False
;
346 aPropInfoList
[nProperties
++].Value
= uno::makeAny( aFtpProxy
.Server
);
350 if( aFtpProxy
.Port
> 0 )
352 aPropInfoList
[nProperties
].Name
= rtl::OUString(
353 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetFTPProxyPort") );
354 aPropInfoList
[nProperties
].Type
= rtl::OUString(
355 RTL_CONSTASCII_USTRINGPARAM( "int" ) );
356 aPropInfoList
[nProperties
].Protected
= sal_False
;
357 aPropInfoList
[nProperties
++].Value
= uno::makeAny( aFtpProxy
.Port
);
360 // resize the property info list appropriately
361 aPropInfoList
.realloc(nProperties
);
363 m_xLayerContentDescriber
->describeLayer(xHandler
, aPropInfoList
);
367 OSL_TRACE("Could not create com.sun.star.configuration.backend.LayerContentDescriber Service");
371 //------------------------------------------------------------------------------
373 rtl::OUString SAL_CALL
MacOSXLayer::getTimestamp(void)
374 throw (uno::RuntimeException
)
376 // FIXME: Always new timestamp!?
377 rtl::OUString aTimestamp
= rtl::OUString::valueOf( time(NULL
) );
382 //------------------------------------------------------------------------------
384 rtl::OUString
GetOUString( NSString
* pStr
)
387 return rtl::OUString();
388 int nLen
= [pStr length
];
390 return rtl::OUString();
392 rtl::OUStringBuffer
aBuf( nLen
+1 );
393 aBuf
.setLength( nLen
);
394 [pStr getCharacters
: const_cast<sal_Unicode
*>(aBuf
.getStr())];
395 return aBuf
.makeStringAndClear();
398 void SAL_CALL
MacOSXPathLayer::readData(
399 const uno::Reference
<backend::XLayerHandler
>& i_xHandler
)
400 throw ( backend::MalformedDataException
,
401 lang::NullPointerException
,
402 lang::WrappedTargetException
,
403 uno::RuntimeException
)
405 if (m_xLayerContentDescriber
.is())
407 rtl::OUString aDocDir
;
408 NSArray
* pPaths
= NSSearchPathForDirectoriesInDomains( NSDocumentDirectory
, NSUserDomainMask
, true );
409 if( pPaths
&& [pPaths count
] > 0 )
411 aDocDir
= GetOUString( [pPaths objectAtIndex
: 0] );
413 rtl::OUString aDocURL
;
414 if( aDocDir
.getLength() > 0 &&
415 osl_getFileURLFromSystemPath( aDocDir
.pData
, &aDocURL
.pData
) == osl_File_E_None
)
417 uno::Sequence
<backend::PropertyInfo
> aPropInfoList(1);
419 aPropInfoList
[0].Name
= rtl::OUString(
420 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Office.Paths/Variables/Work" ) );
421 aPropInfoList
[0].Type
= rtl::OUString(
422 RTL_CONSTASCII_USTRINGPARAM( "string" ) );
423 aPropInfoList
[0].Protected
= sal_False
;
424 aPropInfoList
[0].Value
<<= aDocURL
;
426 m_xLayerContentDescriber
->describeLayer(i_xHandler
, aPropInfoList
);
430 OSL_TRACE( "user documents list contains empty file path or conversion failed" );
435 OSL_TRACE( "Got nil or empty list of user document directories" );
440 OSL_TRACE("Could not create com.sun.star.configuration.backend.LayerContentDescriber Service");