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>
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>
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 {}
65 {
return "com.sun.star.comp.configuration.backend.DesktopBackend"; }
70 virtual css::uno::Sequence< OUString > SAL_CALL
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 >(); }
79 OUString
const &, css::uno::Any
const &)
override;
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
105void Default::setPropertyValue(OUString
const &, css::uno::Any
const &)
107 throw css::lang::IllegalArgumentException(
108 "setPropertyValue not supported",
112OUString 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;
212 return aHomeDirURL +
"/" + OUString::createFromAscii(type);
215css::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()));
223css::uno::Any Default::getPropertyValue(OUString
const & PropertyName)
225 if (PropertyName ==
"TemplatePathVariable")
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, getXWeak());
258css::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 &) {
269 }
catch (
const css::uno::Exception &) {
272 return css::uno::Reference< css::uno::XInterface >();
276extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
277shell_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;
288 css::uno::Reference< css::uno::XInterface > backend;
290 backend = createBackend(context,
291 "com.sun.star.configuration.backend.KF5Backend");
293 backend = getXWeak(
new Default);
295 return backend.get();
#define TOOLS_WARN_EXCEPTION(area, stream)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
VBAHELPER_DLLPUBLIC bool setPropertyValue(css::uno::Sequence< css::beans::PropertyValue > &aProp, const OUString &aName, const css::uno::Any &aValue)
bool getPropertyValue(ValueType &rValue, css::uno::Reference< css::beans::XPropertySet > const &xPropSet, OUString const &propName)