Update ooo320-m1
[ooovba.git] / shell / source / backends / wininetbe / wininetlayer.cxx
blob0151eb5550d9dd12029730cffb755d63e6cb21f1
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: wininetlayer.cxx,v $
10 * $Revision: 1.7 $
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 #ifndef _WININETLAYER_HXX_
35 #include "wininetlayer.hxx"
36 #endif
38 #include <malloc.h>
40 #include <rtl/ustrbuf.hxx>
42 #define EQUAL_SIGN '='
43 #define COLON ':'
44 #define SPACE ' '
45 #define SEMI_COLON ';'
47 typedef struct
49 rtl::OUString Server;
50 rtl::OUString Port;
51 } ProxyEntry;
53 //------------------------------------------------------------------------
54 // helper functions
55 //------------------------------------------------------------------------
57 namespace // private
59 ProxyEntry ReadProxyEntry(const rtl::OUString& aProxy, sal_Int32& i)
61 ProxyEntry aProxyEntry;
63 aProxyEntry.Server = aProxy.getToken( 0, COLON, i );
64 if ( i > -1 )
65 aProxyEntry.Port = aProxy.getToken( 0, COLON, i );
67 return aProxyEntry;
70 ProxyEntry FindProxyEntry(const rtl::OUString& aProxyList, const rtl::OUString& aType)
72 sal_Int32 nIndex = 0;
76 // get the next token, e.g. ftp=server:port
77 rtl::OUString nextToken = aProxyList.getToken( 0, SPACE, nIndex );
79 // split the next token again into the parts separated
80 // through '=', e.g. ftp=server:port -> ftp and server:port
81 sal_Int32 i = 0;
82 if( nextToken.indexOf( EQUAL_SIGN ) > -1 )
84 if( aType.equals( nextToken.getToken( 0, EQUAL_SIGN, i ) ) )
85 return ReadProxyEntry(nextToken, i);
87 else if( aType.getLength() == 0)
88 return ReadProxyEntry(nextToken, i);
90 } while ( nIndex >= 0 );
92 return ProxyEntry();
95 } // end private namespace
97 //------------------------------------------------------------------------------
99 WinInetLayer::WinInetLayer( InternetQueryOption_Proc_T lpfnInternetQueryOption,
100 const uno::Reference<uno::XComponentContext>& xContext)
101 : m_lpfnInternetQueryOption(lpfnInternetQueryOption)
103 //Create instance of LayerContentDescriber Service
104 rtl::OUString const k_sLayerDescriberService(RTL_CONSTASCII_USTRINGPARAM(
105 "com.sun.star.comp.configuration.backend.LayerDescriber"));
107 typedef uno::Reference<backend::XLayerContentDescriber> LayerDescriber;
108 uno::Reference< lang::XMultiComponentFactory > xServiceManager = xContext->getServiceManager();
109 if( xServiceManager.is() )
111 m_xLayerContentDescriber = LayerDescriber::query(
112 xServiceManager->createInstanceWithContext(k_sLayerDescriberService, xContext));
114 else
116 OSL_TRACE("Could not retrieve ServiceManager");
121 //------------------------------------------------------------------------------
123 void SAL_CALL WinInetLayer::readData(
124 const uno::Reference<backend::XLayerHandler>& xHandler)
125 throw ( backend::MalformedDataException,
126 lang::NullPointerException,
127 lang::WrappedTargetException,
128 uno::RuntimeException)
131 if (m_xLayerContentDescriber.is() && m_lpfnInternetQueryOption)
133 LPINTERNET_PROXY_INFO lpi = NULL;
135 // query for the neccessary space
136 DWORD dwLength = 0;
137 BOOL bRet = m_lpfnInternetQueryOption(
138 NULL,
139 INTERNET_OPTION_PROXY,
140 (LPVOID)lpi,
141 &dwLength );
143 // allocate sufficient space on the heap
144 // insufficient space on the heap results
145 // in a stack overflow exception, we assume
146 // this never happens, because of the relatively
147 // small amount of memory we need
148 // _alloca is nice because it is fast and we don't
149 // have to free the allocated memory, it will be
150 // automatically done
151 lpi = reinterpret_cast< LPINTERNET_PROXY_INFO >(
152 _alloca( dwLength ) );
154 bRet = m_lpfnInternetQueryOption(
155 NULL,
156 INTERNET_OPTION_PROXY,
157 (LPVOID)lpi,
158 &dwLength );
160 // if a proxy is disabled, InternetQueryOption returns
161 // an empty proxy list, so we don't have to check if
162 // proxy is enabled or not
164 rtl::OUString aProxyList = rtl::OUString::createFromAscii( lpi->lpszProxy );
165 rtl::OUString aProxyBypassList = rtl::OUString::createFromAscii( lpi->lpszProxyBypass );
167 // override default for ProxyType, which is "0" meaning "No proxies".
168 uno::Sequence<backend::PropertyInfo> aPropInfoList(8);
169 sal_Int32 nProperties = 1;
171 aPropInfoList[0].Name = rtl::OUString(
172 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetProxyType") );
173 aPropInfoList[0].Type = rtl::OUString(
174 RTL_CONSTASCII_USTRINGPARAM( "int" ) );
175 aPropInfoList[0].Protected = sal_False;
176 aPropInfoList[0].Value = uno::makeAny( nProperties );
178 // fill proxy bypass list
179 if( aProxyBypassList.getLength() > 0 )
181 rtl::OUStringBuffer aReverseList;
182 sal_Int32 nIndex = 0;
185 rtl::OUString aToken = aProxyBypassList.getToken( 0, SPACE, nIndex );
186 if ( aProxyList.indexOf( aToken ) == -1 )
188 if ( aReverseList.getLength() )
190 aReverseList.insert( 0, sal_Unicode( SEMI_COLON ) );
191 aReverseList.insert( 0, aToken );
193 else
194 aReverseList = aToken;
197 while ( nIndex >= 0 );
199 aProxyBypassList = aReverseList.makeStringAndClear();
201 aPropInfoList[nProperties].Name = rtl::OUString(
202 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetNoProxy") );
203 aPropInfoList[nProperties].Type = rtl::OUString(
204 RTL_CONSTASCII_USTRINGPARAM( "string" ) );
205 aPropInfoList[nProperties].Protected = sal_False;
206 aPropInfoList[nProperties++].Value = uno::makeAny( aProxyBypassList.replace( SPACE, SEMI_COLON ) );
209 if( aProxyList.getLength() > 0 )
211 //-------------------------------------------------
212 // this implementation follows the algorithm
213 // of the internet explorer
214 // if there are type-dependent proxy settings
215 // and type independent proxy settings in the
216 // registry the internet explorer chooses the
217 // type independent proxy for all settings
218 // e.g. imagine the following registry entry
219 // ftp=server:port;http=server:port;server:port
220 // the last token server:port is type independent
221 // so the ie chooses this proxy server
223 // if there is no port specified for a type independent
224 // server the ie uses the port of an http server if
225 // there is one and it has a port
226 //-------------------------------------------------
228 ProxyEntry aTypeIndepProxy = FindProxyEntry( aProxyList, rtl::OUString());
229 ProxyEntry aHttpProxy = FindProxyEntry( aProxyList, rtl::OUString(
230 RTL_CONSTASCII_USTRINGPARAM( "http" ) ) );
231 ProxyEntry aHttpsProxy = FindProxyEntry( aProxyList, rtl::OUString(
232 RTL_CONSTASCII_USTRINGPARAM( "https" ) ) );
234 ProxyEntry aFtpProxy = FindProxyEntry( aProxyList, rtl::OUString(
235 RTL_CONSTASCII_USTRINGPARAM( "ftp" ) ) );
237 if( aTypeIndepProxy.Server.getLength() )
239 aHttpProxy.Server = aTypeIndepProxy.Server;
240 aHttpsProxy.Server = aTypeIndepProxy.Server;
241 aFtpProxy.Server = aTypeIndepProxy.Server;
243 if( aTypeIndepProxy.Port.getLength() )
245 aHttpProxy.Port = aTypeIndepProxy.Port;
246 aHttpsProxy.Port = aTypeIndepProxy.Port;
247 aFtpProxy.Port = aTypeIndepProxy.Port;
249 else
251 aFtpProxy.Port = aHttpProxy.Port;
252 aHttpsProxy.Port = aHttpProxy.Port;
256 // http proxy name
257 if( aHttpProxy.Server.getLength() > 0 )
259 aPropInfoList[nProperties].Name = rtl::OUString(
260 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetHTTPProxyName") );
261 aPropInfoList[nProperties].Type = rtl::OUString(
262 RTL_CONSTASCII_USTRINGPARAM( "string" ) );
263 aPropInfoList[nProperties].Protected = sal_False;
264 aPropInfoList[nProperties++].Value = uno::makeAny( aHttpProxy.Server );
267 // http proxy port
268 if( aHttpProxy.Port.getLength() > 0 )
270 aPropInfoList[nProperties].Name = rtl::OUString(
271 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetHTTPProxyPort") );
272 aPropInfoList[nProperties].Type = rtl::OUString(
273 RTL_CONSTASCII_USTRINGPARAM( "int" ) );
274 aPropInfoList[nProperties].Protected = sal_False;
275 aPropInfoList[nProperties++].Value = uno::makeAny( aHttpProxy.Port.toInt32() );
278 // https proxy name
279 if( aHttpsProxy.Server.getLength() > 0 )
281 aPropInfoList[nProperties].Name = rtl::OUString(
282 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetHTTPSProxyName") );
283 aPropInfoList[nProperties].Type = rtl::OUString(
284 RTL_CONSTASCII_USTRINGPARAM( "string" ) );
285 aPropInfoList[nProperties].Protected = sal_False;
286 aPropInfoList[nProperties++].Value = uno::makeAny( aHttpsProxy.Server );
289 // https proxy port
290 if( aHttpsProxy.Port.getLength() > 0 )
292 aPropInfoList[nProperties].Name = rtl::OUString(
293 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetHTTPSProxyPort") );
294 aPropInfoList[nProperties].Type = rtl::OUString(
295 RTL_CONSTASCII_USTRINGPARAM( "int" ) );
296 aPropInfoList[nProperties].Protected = sal_False;
297 aPropInfoList[nProperties++].Value = uno::makeAny( aHttpsProxy.Port.toInt32() );
300 // ftp proxy name
301 if( aFtpProxy.Server.getLength() > 0 )
303 aPropInfoList[nProperties].Name = rtl::OUString(
304 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetFTPProxyName") );
305 aPropInfoList[nProperties].Type = rtl::OUString(
306 RTL_CONSTASCII_USTRINGPARAM( "string" ) );
307 aPropInfoList[nProperties].Protected = sal_False;
308 aPropInfoList[nProperties++].Value = uno::makeAny( aFtpProxy.Server );
311 // ftp proxy port
312 if( aFtpProxy.Port.getLength() > 0 )
314 aPropInfoList[nProperties].Name = rtl::OUString(
315 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.Inet/Settings/ooInetFTPProxyPort") );
316 aPropInfoList[nProperties].Type = rtl::OUString(
317 RTL_CONSTASCII_USTRINGPARAM( "int" ) );
318 aPropInfoList[nProperties].Protected = sal_False;
319 aPropInfoList[nProperties++].Value = uno::makeAny( aFtpProxy.Port.toInt32() );
323 // resize the property info list appropriately
324 aPropInfoList.realloc(nProperties);
326 m_xLayerContentDescriber->describeLayer(xHandler, aPropInfoList);
328 else
330 OSL_TRACE("Could not create com.sun.star.configuration.backend.LayerContentDescriber Service");
334 //------------------------------------------------------------------------------
336 rtl::OUString SAL_CALL WinInetLayer::getTimestamp(void)
337 throw (uno::RuntimeException)
339 rtl::OUString aTimestamp;
341 if (m_lpfnInternetQueryOption)
343 LPINTERNET_PROXY_INFO lpi = NULL;
345 // query for the neccessary space
346 DWORD dwLength = 0;
347 BOOL bRet = m_lpfnInternetQueryOption(
348 NULL,
349 INTERNET_OPTION_PROXY,
350 (LPVOID)lpi,
351 &dwLength );
353 // allocate sufficient space on the heap
354 // insufficient space on the heap results
355 // in a stack overflow exception, we assume
356 // this never happens, because of the relatively
357 // small amount of memory we need
358 // _alloca is nice because it is fast and we don't
359 // have to free the allocated memory, it will be
360 // automatically done
361 lpi = reinterpret_cast< LPINTERNET_PROXY_INFO >(
362 _alloca( dwLength ) );
364 bRet = m_lpfnInternetQueryOption(
365 NULL,
366 INTERNET_OPTION_PROXY,
367 (LPVOID)lpi,
368 &dwLength );
370 aTimestamp = rtl::OUString::createFromAscii( lpi->lpszProxy );
371 aTimestamp += rtl::OUString::createFromAscii( lpi->lpszProxyBypass );
374 return aTimestamp;
377 //------------------------------------------------------------------------------