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/implbase.hxx>
40 #include <cppuhelper/weak.hxx>
41 #include <osl/file.hxx>
42 #include <osl/security.hxx>
43 #include <rtl/byteseq.hxx>
44 #include <rtl/ustrbuf.hxx>
45 #include <rtl/ustring.hxx>
46 #include <sal/types.h>
47 #include <comphelper/diagnose_ex.hxx>
48 #include <uno/current_context.hxx>
53 public cppu::WeakImplHelper
<
54 css::lang::XServiceInfo
, css::beans::XPropertySet
>
58 Default(const Default
&) = delete;
59 Default
& operator=(const Default
&) = delete;
62 virtual ~Default() override
{}
64 virtual OUString SAL_CALL
getImplementationName() override
65 { return "com.sun.star.comp.configuration.backend.DesktopBackend"; }
67 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
) override
68 { return ServiceName
== getSupportedServiceNames()[0]; }
70 virtual css::uno::Sequence
< OUString
> SAL_CALL
71 getSupportedServiceNames() override
72 { return { "com.sun.star.configuration.backend.DesktopBackend" }; }
74 virtual css::uno::Reference
< css::beans::XPropertySetInfo
> SAL_CALL
75 getPropertySetInfo() override
76 { return css::uno::Reference
< css::beans::XPropertySetInfo
>(); }
78 virtual void SAL_CALL
setPropertyValue(
79 OUString
const &, css::uno::Any
const &) override
;
81 virtual css::uno::Any SAL_CALL
getPropertyValue(
82 OUString
const & PropertyName
) override
;
84 virtual void SAL_CALL
addPropertyChangeListener(
86 css::uno::Reference
< css::beans::XPropertyChangeListener
> const &) override
89 virtual void SAL_CALL
removePropertyChangeListener(
91 css::uno::Reference
< css::beans::XPropertyChangeListener
> const &) override
94 virtual void SAL_CALL
addVetoableChangeListener(
96 css::uno::Reference
< css::beans::XVetoableChangeListener
> const &) override
99 virtual void SAL_CALL
removeVetoableChangeListener(
101 css::uno::Reference
< css::beans::XVetoableChangeListener
> const &) override
105 void Default::setPropertyValue(OUString
const &, css::uno::Any
const &)
107 throw css::lang::IllegalArgumentException(
108 "setPropertyValue not supported",
109 static_cast< cppu::OWeakObject
* >(this), -1);
112 OUString
xdg_user_dir_lookup (const char *type
, bool bAllowHomeDir
)
114 size_t nLenType
= strlen(type
);
119 osl::Security aSecurity
;
120 oslFileHandle handle
;
121 OUString aHomeDirURL
;
122 OUString aDocumentsDirURL
;
123 OUString aConfigFileURL
;
124 OUStringBuffer aUserDirBuf
;
126 if (!aSecurity
.getHomeDir( aHomeDirURL
) )
128 osl::FileBase::getFileURLFromSystemPath("/tmp", aDocumentsDirURL
);
129 return aDocumentsDirURL
;
132 config_home
= getenv ("XDG_CONFIG_HOME");
133 if (config_home
== nullptr || config_home
[0] == 0)
135 aConfigFileURL
= aHomeDirURL
+ "/.config/user-dirs.dirs";
139 aConfigFileURL
= OUString::createFromAscii(config_home
) + "/user-dirs.dirs";
142 if(osl_File_E_None
== osl_openFile(aConfigFileURL
.pData
, &handle
, osl_File_OpenFlag_Read
))
144 rtl::ByteSequence seq
;
145 while (osl_File_E_None
== osl_readLine(handle
, reinterpret_cast<sal_Sequence
**>(&seq
)))
148 int len
= seq
.getLength();
149 seq
.realloc(len
+ 1);
152 p
= reinterpret_cast<char *>(seq
.getArray());
153 while (*p
== ' ' || *p
== '\t')
155 if (strncmp (p
, "XDG_", 4) != 0)
158 if (strncmp (p
, OString(type
, nLenType
).toAsciiUpperCase().getStr(), nLenType
) != 0)
161 if (strncmp (p
, "_DIR", 4) != 0)
164 while (*p
== ' ' || *p
== '\t')
169 while (*p
== ' ' || *p
== '\t')
174 if (strncmp (p
, "$HOME/", 6) == 0)
183 aUserDirBuf
= aHomeDirURL
+ "/";
187 aUserDirBuf
.truncate();
189 while (*p
&& *p
!= '"')
191 if ((*p
== '\\') && (*(p
+1) != 0))
193 aUserDirBuf
.append(static_cast<sal_Unicode
>(*p
++));
196 osl_closeFile(handle
);
200 if (aUserDirBuf
.getLength()>0 && !bError
)
202 aDocumentsDirURL
= aUserDirBuf
.makeStringAndClear();
203 if ( bAllowHomeDir
||
204 (aDocumentsDirURL
!= aHomeDirURL
&& aDocumentsDirURL
!= Concat2View(aHomeDirURL
+ "/")) )
206 osl::Directory
aDocumentsDir( aDocumentsDirURL
);
207 if( osl::FileBase::E_None
== aDocumentsDir
.open() )
208 return aDocumentsDirURL
;
211 /* Use fallbacks historical compatibility if nothing else exists */
212 return aHomeDirURL
+ "/" + OUString::createFromAscii(type
);
215 css::uno::Any
xdgDirectoryIfExists(char const * type
, bool bAllowHomeDir
) {
216 auto url
= xdg_user_dir_lookup(type
, bAllowHomeDir
);
217 return css::uno::Any(
218 osl::Directory(url
).open() == osl::FileBase::E_None
219 ? css::beans::Optional
<css::uno::Any
>(true, css::uno::Any(url
))
220 : css::beans::Optional
<css::uno::Any
>(false, css::uno::Any()));
223 css::uno::Any
Default::getPropertyValue(OUString
const & PropertyName
)
225 if (PropertyName
== "TemplatePathVariable")
227 // Never pick up the HOME directory as the default location of user's templates
228 return xdgDirectoryIfExists("Templates", false);
231 if (PropertyName
== "WorkPathVariable")
233 return xdgDirectoryIfExists("Documents", true);
236 if ( PropertyName
== "EnableATToolSupport" ||
237 PropertyName
== "ExternalMailer" ||
238 PropertyName
== "SourceViewFontHeight" ||
239 PropertyName
== "SourceViewFontName" ||
240 PropertyName
== "ooInetFTPProxyName" ||
241 PropertyName
== "ooInetFTPProxyPort" ||
242 PropertyName
== "ooInetHTTPProxyName" ||
243 PropertyName
== "ooInetHTTPProxyPort" ||
244 PropertyName
== "ooInetHTTPSProxyName" ||
245 PropertyName
== "ooInetHTTPSProxyPort" ||
246 PropertyName
== "ooInetNoProxy" ||
247 PropertyName
== "ooInetProxyType" ||
248 PropertyName
== "givenname" ||
249 PropertyName
== "sn" )
251 return css::uno::Any(css::beans::Optional
< css::uno::Any
>());
254 throw css::beans::UnknownPropertyException(
255 PropertyName
, static_cast< cppu::OWeakObject
* >(this));
258 css::uno::Reference
< css::uno::XInterface
> createBackend(
259 css::uno::Reference
< css::uno::XComponentContext
> const & context
,
260 OUString
const & name
)
263 return css::uno::Reference
< css::lang::XMultiComponentFactory
>(
264 context
->getServiceManager(), css::uno::UNO_SET_THROW
)->
265 createInstanceWithContext(name
, context
);
266 } catch (css::uno::RuntimeException
&) {
267 // Assuming these exceptions are real errors:
269 } catch (const css::uno::Exception
&) {
270 // Assuming these exceptions indicate that the service is not installed:
271 TOOLS_WARN_EXCEPTION("shell", "createInstance(" << name
<< ") failed");
272 return css::uno::Reference
< css::uno::XInterface
>();
276 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
277 shell_DesktopBackend_get_implementation(
278 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
281 css::uno::Reference
< css::uno::XCurrentContext
> current(
282 css::uno::getCurrentContext());
284 current
->getValueByName("system.desktop-environment") >>= desktop
;
287 // Fall back to the default if the specific backend is not available:
288 css::uno::Reference
< css::uno::XInterface
> backend
;
289 if (desktop
== "PLASMA5")
290 backend
= createBackend(context
,
291 "com.sun.star.configuration.backend.KF5Backend");
293 backend
= static_cast< cppu::OWeakObject
* >(new Default
);
295 return backend
.get();
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */