LibreOffice Module shell (master) 1
macbackend.mm
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
21// For MAXHOSTNAMELEN constant
22#include <sys/param.h>
23
24#include <premac.h>
25#include <SystemConfiguration/SystemConfiguration.h>
26#include <Foundation/NSPathUtilities.h>
27#include <postmac.h>
28
29#include "macbackend.hxx"
30
31#include <com/sun/star/beans/Optional.hpp>
32#include <com/sun/star/uno/XComponentContext.hpp>
34#include <rtl/ustrbuf.hxx>
35#include <sal/log.hxx>
36#include <osl/diagnose.h>
37#include <osl/file.h>
38
39#define SPACE ' '
40#define SEMI_COLON ';'
41
42namespace
43{
44
45typedef enum {
46 sHTTP,
47 sHTTPS,
48 sFTP
49} ServiceType;
50
51/*
52 * Returns current proxy settings for selected service type (HTTP or
53 * FTP) as a C string (in the buffer specified by host and hostSize)
54 * and a port number.
55 */
56
57bool GetProxySetting(ServiceType sType, char *host, size_t hostSize, UInt16 *port)
58{
59 bool result;
60 CFDictionaryRef proxyDict;
61 CFNumberRef enableNum;
62 int enable;
63 CFStringRef hostStr;
64 CFNumberRef portNum;
65 int portInt;
66
67 proxyDict = SCDynamicStoreCopyProxies(nullptr);
68
69 if (!proxyDict)
70 return false;
71
72 CFStringRef proxiesEnable;
73 CFStringRef proxiesProxy;
74 CFStringRef proxiesPort;
75
76 switch ( sType )
77 {
78 case sHTTP : proxiesEnable = kSCPropNetProxiesHTTPEnable;
79 proxiesProxy = kSCPropNetProxiesHTTPProxy;
80 proxiesPort = kSCPropNetProxiesHTTPPort;
81 break;
82 case sHTTPS: proxiesEnable = kSCPropNetProxiesHTTPSEnable;
83 proxiesProxy = kSCPropNetProxiesHTTPSProxy;
84 proxiesPort = kSCPropNetProxiesHTTPSPort;
85 break;
86 default: proxiesEnable = kSCPropNetProxiesFTPEnable;
87 proxiesProxy = kSCPropNetProxiesFTPProxy;
88 proxiesPort = kSCPropNetProxiesFTPPort;
89 break;
90 }
91 // Proxy enabled?
92 enableNum = static_cast<CFNumberRef>(CFDictionaryGetValue( proxyDict,
93 proxiesEnable ));
94
95 result = (enableNum != nullptr) && (CFGetTypeID(enableNum) == CFNumberGetTypeID());
96
97 if (result)
98 result = CFNumberGetValue(enableNum, kCFNumberIntType, &enable) && (enable != 0);
99
100 // Proxy enabled -> get hostname
101 if (result)
102 {
103 hostStr = static_cast<CFStringRef>(CFDictionaryGetValue( proxyDict,
104 proxiesProxy ));
105
106 result = (hostStr != nullptr) && (CFGetTypeID(hostStr) == CFStringGetTypeID());
107 }
108
109 if (result)
110 result = CFStringGetCString(hostStr, host, static_cast<CFIndex>(hostSize), kCFStringEncodingASCII);
111
112 // Get proxy port
113 if (result)
114 {
115 portNum = static_cast<CFNumberRef>(CFDictionaryGetValue( proxyDict,
116 proxiesPort ));
117
118 result = (portNum != nullptr) && (CFGetTypeID(portNum) == CFNumberGetTypeID());
119 }
120 else
121 {
122 CFRelease(proxyDict);
123 return false;
124 }
125
126 if (result)
127 result = CFNumberGetValue(portNum, kCFNumberIntType, &portInt);
128
129 if (result)
130 *port = static_cast<UInt16>(portInt);
131
132 if (proxyDict)
133 CFRelease(proxyDict);
134
135 if (!result)
136 {
137 *host = 0;
138 *port = 0;
139 }
140
141 return result;
142}
143
144} // unnamed namespace
145
147{
148}
149
151{
152}
153
154static OUString CFStringToOUString(const CFStringRef sOrig) {
155 CFRetain(sOrig);
156
157 CFIndex nStringLen = CFStringGetLength(sOrig)+1;
158
159 // Allocate a c string buffer
160 char sBuffer[nStringLen];
161
162 CFStringGetCString(sOrig, sBuffer, nStringLen, kCFStringEncodingASCII);
163
164 CFRelease(sOrig);
165
166 return OUString::createFromAscii(sBuffer);
167}
168
169static OUString GetOUString( NSString* pStr )
170{
171 if( ! pStr )
172 return OUString();
173 int nLen = [pStr length];
174 if( nLen == 0 )
175 return OUString();
176
177 OUStringBuffer aBuf( nLen+1 );
178 aBuf.setLength( nLen );
179 [pStr getCharacters:
180 reinterpret_cast<unichar *>(const_cast<sal_Unicode*>(aBuf.getStr()))];
181 return aBuf.makeStringAndClear();
182}
183
185 OUString const &, css::uno::Any const &)
186{
187 throw css::lang::IllegalArgumentException(
188 "setPropertyValue not supported",
189 getXWeak(), -1);
190}
191
193 OUString const & PropertyName)
194{
195 if ( PropertyName == "WorkPathVariable" )
196 {
197 OUString aDocDir;
198 NSArray* pPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, true );
199 if( pPaths && [pPaths count] > 0 )
200 {
201 aDocDir = GetOUString( [pPaths objectAtIndex: 0] );
202
203 OUString aDocURL;
204 if( aDocDir.getLength() > 0 &&
205 osl_getFileURLFromSystemPath( aDocDir.pData, &aDocURL.pData ) == osl_File_E_None )
206 {
207 return css::uno::Any(
208 css::beans::Optional< css::uno::Any >(
209 true, css::uno::Any( aDocURL ) ) );
210 }
211 else
212 {
213 SAL_WARN("shell", "user documents list contains empty file path or conversion failed" );
214 }
215 }
216 else
217 {
218 SAL_WARN("shell", "Got nil or empty list of user document directories" );
219 }
220 return css::uno::Any(css::beans::Optional< css::uno::Any >());
221 } else if ( PropertyName == "ooInetFTPProxyName" )
222 {
223 char host[MAXHOSTNAMELEN];
224 UInt16 port;
225 bool retVal;
226
227 retVal = GetProxySetting(sFTP, host, 100, &port);
228
229 if (retVal)
230 {
231 auto const Server = OUString::createFromAscii( host );
232 if( Server.getLength() > 0 )
233 {
234 return css::uno::Any(
235 css::beans::Optional< css::uno::Any >(
236 true, uno::Any( Server ) ) );
237 }
238 }
239 return css::uno::Any(css::beans::Optional< css::uno::Any >());
240 } else if ( PropertyName == "ooInetFTPProxyPort" )
241 {
242 char host[MAXHOSTNAMELEN];
243 UInt16 port;
244 bool retVal;
245
246 retVal = GetProxySetting(sFTP, host, 100, &port);
247
248 if (retVal && port > 0)
249 {
250 return css::uno::Any(
251 css::beans::Optional< css::uno::Any >(
252 true, uno::Any( sal_Int32(port) ) ) );
253 }
254 return css::uno::Any(css::beans::Optional< css::uno::Any >());
255 } else if ( PropertyName == "ooInetHTTPProxyName" )
256 {
257 char host[MAXHOSTNAMELEN];
258 UInt16 port;
259 bool retVal;
260
261 retVal = GetProxySetting(sHTTP, host, 100, &port);
262
263 if (retVal)
264 {
265 auto const Server = OUString::createFromAscii( host );
266 if( Server.getLength() > 0 )
267 {
268 return css::uno::Any(
269 css::beans::Optional< css::uno::Any >(
270 true, uno::Any( Server ) ) );
271 }
272 }
273 return css::uno::Any(css::beans::Optional< css::uno::Any >());
274 } else if ( PropertyName == "ooInetHTTPProxyPort" )
275 {
276 char host[MAXHOSTNAMELEN];
277 UInt16 port;
278 bool retVal;
279
280 retVal = GetProxySetting(sHTTP, host, 100, &port);
281
282 if (retVal && port > 0)
283 {
284 return css::uno::Any(
285 css::beans::Optional< css::uno::Any >(
286 true, uno::Any( sal_Int32(port) ) ) );
287 }
288 return css::uno::Any(css::beans::Optional< css::uno::Any >());
289 } else if ( PropertyName == "ooInetHTTPSProxyName" )
290 {
291 char host[MAXHOSTNAMELEN];
292 UInt16 port;
293 bool retVal;
294
295 retVal = GetProxySetting(sHTTPS, host, 100, &port);
296
297 if (retVal)
298 {
299 auto const Server = OUString::createFromAscii( host );
300 if( Server.getLength() > 0 )
301 {
302 return css::uno::Any(
303 css::beans::Optional< css::uno::Any >(
304 true, uno::Any( Server ) ) );
305 }
306 }
307 return css::uno::Any(css::beans::Optional< css::uno::Any >());
308 } else if ( PropertyName == "ooInetHTTPSProxyPort" )
309 {
310 char host[MAXHOSTNAMELEN];
311 UInt16 port;
312 bool retVal;
313
314 retVal = GetProxySetting(sHTTPS, host, 100, &port);
315
316 if (retVal && port > 0)
317 {
318 return css::uno::Any(
319 css::beans::Optional< css::uno::Any >(
320 true, uno::Any( sal_Int32(port) ) ) );
321 }
322 return css::uno::Any(css::beans::Optional< css::uno::Any >());
323 } else if ( PropertyName == "ooInetProxyType" )
324 {
325 // override default for ProxyType, which is "0" meaning "No proxies".
326 return css::uno::Any(
327 css::beans::Optional< css::uno::Any >(
328 true, uno::Any( sal_Int32(1) ) ) );
329 } else if ( PropertyName == "ooInetNoProxy" )
330 {
331 OUString aProxyBypassList;
332
333 CFArrayRef rExceptionsList;
334 CFDictionaryRef rProxyDict = SCDynamicStoreCopyProxies(nullptr);
335
336 if (!rProxyDict)
337 rExceptionsList = nullptr;
338 else
339 rExceptionsList = static_cast<CFArrayRef>(CFDictionaryGetValue(rProxyDict, kSCPropNetProxiesExceptionsList));
340
341 if (rExceptionsList)
342 {
343 for (CFIndex idx = 0; idx < CFArrayGetCount(rExceptionsList); idx++)
344 {
345 CFStringRef rException = static_cast<CFStringRef>(CFArrayGetValueAtIndex(rExceptionsList, idx));
346
347 if (idx>0)
348 aProxyBypassList += ";";
349
350 aProxyBypassList += CFStringToOUString(rException);
351 }
352 }
353
354 if (rProxyDict)
355 CFRelease(rProxyDict);
356
357 // fill proxy bypass list
358 if( aProxyBypassList.getLength() > 0 )
359 {
360 return css::uno::Any(
361 css::beans::Optional< css::uno::Any >(
362 true,
363 uno::Any( aProxyBypassList.replace( SPACE, SEMI_COLON ) ) ) );
364 }
365 return css::uno::Any(css::beans::Optional< css::uno::Any >());
366 } else {
367 throw css::beans::UnknownPropertyException(
368 PropertyName, getXWeak());
369 }
370}
371
373{
374 return "com.sun.star.comp.configuration.backend.MacOSXBackend";
375}
376
377sal_Bool SAL_CALL MacOSXBackend::supportsService(const OUString& aServiceName)
378{
379 return cppu::supportsService(this, aServiceName);
380}
381
382uno::Sequence<OUString> SAL_CALL MacOSXBackend::getSupportedServiceNames(void)
383{
384 return { "com.sun.star.configuration.backend.MacOSXBackend" };
385}
386
387extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
389 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
390{
391 return cppu::acquire(new MacOSXBackend());
392}
393
394
395/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OptionalString sType
virtual OUString SAL_CALL getImplementationName() override
Definition: macbackend.mm:372
virtual ~MacOSXBackend() override
Destructor.
Definition: macbackend.mm:150
virtual sal_Bool SAL_CALL supportsService(const OUString &aServiceName) override
Definition: macbackend.mm:377
virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: macbackend.mm:382
virtual void SAL_CALL setPropertyValue(OUString const &, css::uno::Any const &) override
Definition: macbackend.mm:184
MacOSXBackend()
Service constructor from a service factory.
Definition: macbackend.mm:146
virtual css::uno::Any SAL_CALL getPropertyValue(OUString const &PropertyName) override
Definition: macbackend.mm:192
const sal_uInt16 idx[]
#define SAL_WARN(area, stream)
#define SEMI_COLON
Definition: macbackend.mm:40
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * shell_MacOSXBackend_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
Definition: macbackend.mm:388
#define SPACE
Definition: macbackend.mm:39
static OUString CFStringToOUString(const CFStringRef sOrig)
Definition: macbackend.mm:154
static OUString GetOUString(NSString *pStr)
Definition: macbackend.mm:169
aBuf
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_Bool
sal_uInt16 sal_Unicode
Any result