merge the formfield patch from ooo-build
[ooovba.git] / shell / source / backends / macbe / macbelayer.cxx
blob22046770375f9e93f364cc7af20311b6e8eb0402
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: macbelayer.cxx,v $
10 * $Revision: 1.6 $
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>
37 #include <premac.h>
38 #include <SystemConfiguration/SystemConfiguration.h>
39 #include <Foundation/NSPathUtilities.h>
40 #include <postmac.h>
42 #include "macbelayer.hxx"
44 #include "rtl/ustrbuf.hxx"
45 #include "osl/file.h"
47 #define SPACE ' '
48 #define SEMI_COLON ';'
50 typedef struct
52 rtl::OUString Server;
53 sal_Int32 Port;
54 } ProxyEntry;
56 typedef enum {
57 sHTTP,
58 sHTTPS,
59 sFTP
60 } ServiceType;
62 //------------------------------------------------------------------------
63 // helper functions
64 //------------------------------------------------------------------------
66 namespace // private
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)
72 * and a port number.
75 bool GetProxySetting(ServiceType sType, char *host, size_t hostSize, UInt16 *port)
77 bool result;
78 CFDictionaryRef proxyDict;
79 CFNumberRef enableNum;
80 int enable;
81 CFStringRef hostStr;
82 CFNumberRef portNum;
83 int portInt;
85 proxyDict = SCDynamicStoreCopyProxies(NULL);
87 if (!proxyDict)
88 return false;
90 CFStringRef proxiesEnable;
91 CFStringRef proxiesProxy;
92 CFStringRef proxiesPort;
94 switch ( sType )
96 case sHTTP : proxiesEnable = kSCPropNetProxiesHTTPEnable;
97 proxiesProxy = kSCPropNetProxiesHTTPProxy;
98 proxiesPort = kSCPropNetProxiesHTTPPort;
99 break;
100 case sHTTPS: proxiesEnable = kSCPropNetProxiesHTTPSEnable;
101 proxiesProxy = kSCPropNetProxiesHTTPSProxy;
102 proxiesPort = kSCPropNetProxiesHTTPSPort;
103 break;
104 default: proxiesEnable = kSCPropNetProxiesFTPEnable;
105 proxiesProxy = kSCPropNetProxiesFTPProxy;
106 proxiesPort = kSCPropNetProxiesFTPPort;
107 break;
109 // Proxy enabled?
110 enableNum = (CFNumberRef) CFDictionaryGetValue( proxyDict,
111 proxiesEnable );
113 result = (enableNum != NULL) && (CFGetTypeID(enableNum) == CFNumberGetTypeID());
115 if (result)
116 result = CFNumberGetValue(enableNum, kCFNumberIntType, &enable) && (enable != 0);
118 // Proxy enabled -> get hostname
119 if (result)
121 hostStr = (CFStringRef) CFDictionaryGetValue( proxyDict,
122 proxiesProxy );
124 result = (hostStr != NULL) && (CFGetTypeID(hostStr) == CFStringGetTypeID());
127 if (result)
128 result = CFStringGetCString(hostStr, host, (CFIndex) hostSize, kCFStringEncodingASCII);
130 // Get proxy port
131 if (result)
133 portNum = (CFNumberRef) CFDictionaryGetValue( proxyDict,
134 proxiesPort );
136 result = (portNum != NULL) && (CFGetTypeID(portNum) == CFNumberGetTypeID());
138 else
140 CFRelease(proxyDict);
141 return false;
144 if (result)
145 result = CFNumberGetValue(portNum, kCFNumberIntType, &portInt);
147 if (result)
148 *port = (UInt16) portInt;
150 if (proxyDict)
151 CFRelease(proxyDict);
153 if (!result)
155 *host = 0;
156 *port = 0;
159 return result;
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));
179 else
181 OSL_TRACE("Could not retrieve ServiceManager");
186 //------------------------------------------------------------------------------
188 rtl::OUString CFStringToOUString(const CFStringRef sOrig) {
189 CFRetain(sOrig);
191 CFIndex nStringLen = CFStringGetLength(sOrig)+1;
193 // Allocate a c string buffer
194 char sBuffer[nStringLen];
196 CFStringGetCString(sOrig, sBuffer, nStringLen, kCFStringEncodingASCII);
198 CFRelease(sOrig);
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);
218 if (!rProxyDict)
219 rExceptionsList = false;
220 else
221 rExceptionsList = (CFArrayRef) CFDictionaryGetValue(rProxyDict, kSCPropNetProxiesExceptionsList);
223 if (rExceptionsList)
225 for (CFIndex idx = 0; idx < CFArrayGetCount(rExceptionsList); idx++)
227 CFStringRef rException = (CFStringRef) CFArrayGetValueAtIndex(rExceptionsList, idx);
229 if (idx>0)
230 aProxyBypassList += rtl::OUString::createFromAscii( ";" );
232 aProxyBypassList += CFStringToOUString(rException);
236 if (rProxyDict)
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];
267 UInt16 port;
268 bool retVal;
270 retVal = GetProxySetting(sHTTP, host, 100, &port);
272 if (retVal)
274 aHttpProxy.Server = rtl::OUString::createFromAscii( host );
275 aHttpProxy.Port = port;
278 retVal = GetProxySetting(sHTTPS, host, 100, &port);
280 if (retVal)
282 aHttpsProxy.Server = rtl::OUString::createFromAscii( host );
283 aHttpsProxy.Port = port;
286 retVal = GetProxySetting(sFTP, host, 100, &port);
288 if (retVal)
290 aFtpProxy.Server = rtl::OUString::createFromAscii( host );
291 aFtpProxy.Port = port;
294 // http proxy name
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 );
305 // http proxy port
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 );
316 // https proxy name
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 );
327 // https proxy port
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 );
338 // ftp proxy name
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 );
349 // ftp proxy port
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);
365 else
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) );
379 return aTimestamp;
382 //------------------------------------------------------------------------------
384 rtl::OUString GetOUString( NSString* pStr )
386 if( ! pStr )
387 return rtl::OUString();
388 int nLen = [pStr length];
389 if( nLen == 0 )
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);
428 else
430 OSL_TRACE( "user documents list contains empty file path or conversion failed" );
433 else
435 OSL_TRACE( "Got nil or empty list of user document directories" );
438 else
440 OSL_TRACE("Could not create com.sun.star.configuration.backend.LayerContentDescriber Service");