Debian Codegraph @

shell/source/backends/macbe/macbackend.mm

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