1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
21 #include <sal/log.hxx>
23 #include <com/sun/star/beans/Optional.hpp>
24 #include <com/sun/star/beans/UnknownPropertyException.hpp>
25 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <com/sun/star/beans/XPropertySetInfo.hpp>
28 #include <com/sun/star/beans/XVetoableChangeListener.hpp>
29 #include <com/sun/star/lang/IllegalArgumentException.hpp>
30 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/uno/Any.hxx>
33 #include <com/sun/star/uno/Exception.hpp>
34 #include <com/sun/star/uno/Reference.hxx>
35 #include <com/sun/star/uno/RuntimeException.hpp>
36 #include <com/sun/star/uno/Sequence.hxx>
37 #include <com/sun/star/uno/XComponentContext.hpp>
38 #include <com/sun/star/uno/XCurrentContext.hpp>
39 #include <cppuhelper/factory.hxx>
40 #include <cppuhelper/implbase.hxx>
41 #include <cppuhelper/implementationentry.hxx>
42 #include <cppuhelper/weak.hxx>
43 #include <osl/file.hxx>
44 #include <osl/security.hxx>
45 #include <rtl/byteseq.hxx>
46 #include <rtl/ustrbuf.hxx>
47 #include <rtl/ustring.hxx>
48 #include <sal/types.h>
49 #include <tools/diagnose_ex.h>
50 #include <uno/current_context.hxx>
55 public cppu::WeakImplHelper
<
56 css::lang::XServiceInfo
, css::beans::XPropertySet
>
60 Default(const Default
&) = delete;
61 Default
& operator=(const Default
&) = delete;
64 virtual ~Default() override
{}
66 virtual OUString SAL_CALL
getImplementationName() override
67 { return "com.sun.star.comp.configuration.backend.DesktopBackend"; }
69 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
) override
70 { return ServiceName
== getSupportedServiceNames()[0]; }
72 virtual css::uno::Sequence
< OUString
> SAL_CALL
73 getSupportedServiceNames() override
74 { return { "com.sun.star.configuration.backend.DesktopBackend" }; }
76 virtual css::uno::Reference
< css::beans::XPropertySetInfo
> SAL_CALL
77 getPropertySetInfo() override
78 { return css::uno::Reference
< css::beans::XPropertySetInfo
>(); }
80 virtual void SAL_CALL
setPropertyValue(
81 OUString
const &, css::uno::Any
const &) override
;
83 virtual css::uno::Any SAL_CALL
getPropertyValue(
84 OUString
const & PropertyName
) override
;
86 virtual void SAL_CALL
addPropertyChangeListener(
88 css::uno::Reference
< css::beans::XPropertyChangeListener
> const &) override
91 virtual void SAL_CALL
removePropertyChangeListener(
93 css::uno::Reference
< css::beans::XPropertyChangeListener
> const &) override
96 virtual void SAL_CALL
addVetoableChangeListener(
98 css::uno::Reference
< css::beans::XVetoableChangeListener
> const &) override
101 virtual void SAL_CALL
removeVetoableChangeListener(
103 css::uno::Reference
< css::beans::XVetoableChangeListener
> const &) override
107 void Default::setPropertyValue(OUString
const &, css::uno::Any
const &)
109 throw css::lang::IllegalArgumentException(
110 "setPropertyValue not supported",
111 static_cast< cppu::OWeakObject
* >(this), -1);
114 OUString
xdg_user_dir_lookup (const char *type
, bool bAllowHomeDir
)
116 size_t nLenType
= strlen(type
);
121 osl::Security aSecurity
;
122 oslFileHandle handle
;
123 OUString aHomeDirURL
;
124 OUString aDocumentsDirURL
;
125 OUString aConfigFileURL
;
126 OUStringBuffer aUserDirBuf
;
128 if (!aSecurity
.getHomeDir( aHomeDirURL
) )
130 osl::FileBase::getFileURLFromSystemPath("/tmp", aDocumentsDirURL
);
131 return aDocumentsDirURL
;
134 config_home
= getenv ("XDG_CONFIG_HOME");
135 if (config_home
== nullptr || config_home
[0] == 0)
137 aConfigFileURL
= aHomeDirURL
+ "/.config/user-dirs.dirs";
141 aConfigFileURL
= OUString::createFromAscii(config_home
) + "/user-dirs.dirs";
144 if(osl_File_E_None
== osl_openFile(aConfigFileURL
.pData
, &handle
, osl_File_OpenFlag_Read
))
146 rtl::ByteSequence seq
;
147 while (osl_File_E_None
== osl_readLine(handle
, reinterpret_cast<sal_Sequence
**>(&seq
)))
150 int len
= seq
.getLength();
151 seq
.realloc(len
+ 1);
154 p
= reinterpret_cast<char *>(seq
.getArray());
155 while (*p
== ' ' || *p
== '\t')
157 if (strncmp (p
, "XDG_", 4) != 0)
160 if (strncmp (p
, OString(type
, nLenType
).toAsciiUpperCase().getStr(), nLenType
) != 0)
163 if (strncmp (p
, "_DIR", 4) != 0)
166 while (*p
== ' ' || *p
== '\t')
171 while (*p
== ' ' || *p
== '\t')
176 if (strncmp (p
, "$HOME/", 6) == 0)
185 aUserDirBuf
= aHomeDirURL
+ "/";
189 aUserDirBuf
.truncate();
191 while (*p
&& *p
!= '"')
193 if ((*p
== '\\') && (*(p
+1) != 0))
195 aUserDirBuf
.append(static_cast<sal_Unicode
>(*p
++));
198 osl_closeFile(handle
);
202 if (aUserDirBuf
.getLength()>0 && !bError
)
204 aDocumentsDirURL
= aUserDirBuf
.makeStringAndClear();
205 if ( bAllowHomeDir
||
206 (aDocumentsDirURL
!= aHomeDirURL
&& aDocumentsDirURL
!= aHomeDirURL
+ "/") )
208 osl::Directory
aDocumentsDir( aDocumentsDirURL
);
209 if( osl::FileBase::E_None
== aDocumentsDir
.open() )
210 return aDocumentsDirURL
;
213 /* Use fallbacks historical compatibility if nothing else exists */
214 return aHomeDirURL
+ "/" + OUString::createFromAscii(type
);
217 css::uno::Any
xdgDirectoryIfExists(char const * type
, bool bAllowHomeDir
) {
218 auto url
= xdg_user_dir_lookup(type
, bAllowHomeDir
);
219 return css::uno::Any(
220 osl::Directory(url
).open() == osl::FileBase::E_None
221 ? css::beans::Optional
<css::uno::Any
>(true, css::uno::Any(url
))
222 : css::beans::Optional
<css::uno::Any
>(false, css::uno::Any()));
225 css::uno::Any
Default::getPropertyValue(OUString
const & PropertyName
)
227 if (PropertyName
== "TemplatePathVariable")
229 // Never pick up the HOME directory as the default location of user's templates
230 return xdgDirectoryIfExists("Templates", false);
233 if (PropertyName
== "WorkPathVariable")
235 return xdgDirectoryIfExists("Documents", true);
238 if ( PropertyName
== "EnableATToolSupport" ||
239 PropertyName
== "ExternalMailer" ||
240 PropertyName
== "SourceViewFontHeight" ||
241 PropertyName
== "SourceViewFontName" ||
242 PropertyName
== "ooInetFTPProxyName" ||
243 PropertyName
== "ooInetFTPProxyPort" ||
244 PropertyName
== "ooInetHTTPProxyName" ||
245 PropertyName
== "ooInetHTTPProxyPort" ||
246 PropertyName
== "ooInetHTTPSProxyName" ||
247 PropertyName
== "ooInetHTTPSProxyPort" ||
248 PropertyName
== "ooInetNoProxy" ||
249 PropertyName
== "ooInetProxyType" ||
250 PropertyName
== "givenname" ||
251 PropertyName
== "sn" )
253 return css::uno::makeAny(css::beans::Optional
< css::uno::Any
>());
256 throw css::beans::UnknownPropertyException(
257 PropertyName
, static_cast< cppu::OWeakObject
* >(this));
260 css::uno::Reference
< css::uno::XInterface
> createBackend(
261 css::uno::Reference
< css::uno::XComponentContext
> const & context
,
262 OUString
const & name
)
265 return css::uno::Reference
< css::lang::XMultiComponentFactory
>(
266 context
->getServiceManager(), css::uno::UNO_SET_THROW
)->
267 createInstanceWithContext(name
, context
);
268 } catch (css::uno::RuntimeException
&) {
269 // Assuming these exceptions are real errors:
271 } catch (const css::uno::Exception
&) {
272 // Assuming these exceptions indicate that the service is not installed:
273 TOOLS_WARN_EXCEPTION("shell", "createInstance(" << name
<< ") failed");
274 return css::uno::Reference
< css::uno::XInterface
>();
278 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
279 shell_DesktopBackend_get_implementation(
280 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
283 css::uno::Reference
< css::uno::XCurrentContext
> current(
284 css::uno::getCurrentContext());
286 current
->getValueByName("system.desktop-environment") >>= desktop
;
289 // Fall back to the default if the specific backend is not available:
290 css::uno::Reference
< css::uno::XInterface
> backend
;
291 if (desktop
== "PLASMA5")
292 backend
= createBackend(context
,
293 "com.sun.star.configuration.backend.KF5Backend");
295 backend
= static_cast< cppu::OWeakObject
* >(new Default
);
297 return backend
.get();
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */