bump product version to 6.3.0.0.beta1
[LibreOffice.git] / shell / source / backends / desktopbe / desktopbackend.cxx
blob14b22b033fb1aeef3b2326354491b52d8ec691db
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/PropertyVetoException.hpp>
25 #include <com/sun/star/beans/UnknownPropertyException.hpp>
26 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <com/sun/star/beans/XPropertySetInfo.hpp>
29 #include <com/sun/star/beans/XVetoableChangeListener.hpp>
30 #include <com/sun/star/lang/IllegalArgumentException.hpp>
31 #include <com/sun/star/lang/WrappedTargetException.hpp>
32 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
33 #include <com/sun/star/lang/XServiceInfo.hpp>
34 #include <com/sun/star/uno/Any.hxx>
35 #include <com/sun/star/uno/Exception.hpp>
36 #include <com/sun/star/uno/Reference.hxx>
37 #include <com/sun/star/uno/RuntimeException.hpp>
38 #include <com/sun/star/uno/Sequence.hxx>
39 #include <com/sun/star/uno/XComponentContext.hpp>
40 #include <com/sun/star/uno/XCurrentContext.hpp>
41 #include <cppuhelper/factory.hxx>
42 #include <cppuhelper/implbase.hxx>
43 #include <cppuhelper/implementationentry.hxx>
44 #include <cppuhelper/weak.hxx>
45 #include <osl/file.hxx>
46 #include <osl/security.hxx>
47 #include <rtl/byteseq.hxx>
48 #include <rtl/string.h>
49 #include <rtl/textenc.h>
50 #include <rtl/ustring.h>
51 #include <rtl/ustrbuf.hxx>
52 #include <rtl/ustring.hxx>
53 #include <sal/types.h>
54 #include <uno/current_context.hxx>
55 #include <vcl/svapp.hxx>
57 namespace {
59 OUString getDefaultImplementationName() {
60 return OUString(
61 "com.sun.star.comp.configuration.backend.DesktopBackend");
64 css::uno::Sequence< OUString > getDefaultSupportedServiceNames() {
65 OUString name(
66 "com.sun.star.configuration.backend.DesktopBackend");
67 return css::uno::Sequence< OUString >(&name, 1);
70 class Default:
71 public cppu::WeakImplHelper<
72 css::lang::XServiceInfo, css::beans::XPropertySet >
74 public:
75 Default() {}
76 Default(const Default&) = delete;
77 Default& operator=(const Default&) = delete;
79 private:
80 virtual ~Default() override {}
82 virtual OUString SAL_CALL getImplementationName() override
83 { return getDefaultImplementationName(); }
85 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
86 { return ServiceName == getSupportedServiceNames()[0]; }
88 virtual css::uno::Sequence< OUString > SAL_CALL
89 getSupportedServiceNames() override
90 { return getDefaultSupportedServiceNames(); }
92 virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL
93 getPropertySetInfo() override
94 { return css::uno::Reference< css::beans::XPropertySetInfo >(); }
96 virtual void SAL_CALL setPropertyValue(
97 OUString const &, css::uno::Any const &) override;
99 virtual css::uno::Any SAL_CALL getPropertyValue(
100 OUString const & PropertyName) override;
102 virtual void SAL_CALL addPropertyChangeListener(
103 OUString const &,
104 css::uno::Reference< css::beans::XPropertyChangeListener > const &) override
107 virtual void SAL_CALL removePropertyChangeListener(
108 OUString const &,
109 css::uno::Reference< css::beans::XPropertyChangeListener > const &) override
112 virtual void SAL_CALL addVetoableChangeListener(
113 OUString const &,
114 css::uno::Reference< css::beans::XVetoableChangeListener > const &) override
117 virtual void SAL_CALL removeVetoableChangeListener(
118 OUString const &,
119 css::uno::Reference< css::beans::XVetoableChangeListener > const &) override
123 void Default::setPropertyValue(OUString const &, css::uno::Any const &)
125 throw css::lang::IllegalArgumentException(
126 "setPropertyValue not supported",
127 static_cast< cppu::OWeakObject * >(this), -1);
130 OUString xdg_user_dir_lookup (const char *type, bool bAllowHomeDir)
132 size_t nLenType = strlen(type);
133 char *config_home;
134 char *p;
135 bool bError = false;
137 osl::Security aSecurity;
138 oslFileHandle handle;
139 OUString aHomeDirURL;
140 OUString aDocumentsDirURL;
141 OUString aConfigFileURL;
142 OUStringBuffer aUserDirBuf;
144 if (!aSecurity.getHomeDir( aHomeDirURL ) )
146 osl::FileBase::getFileURLFromSystemPath("/tmp", aDocumentsDirURL);
147 return aDocumentsDirURL;
150 config_home = getenv ("XDG_CONFIG_HOME");
151 if (config_home == nullptr || config_home[0] == 0)
153 aConfigFileURL = aHomeDirURL + "/.config/user-dirs.dirs";
155 else
157 aConfigFileURL = OUString::createFromAscii(config_home) + "/user-dirs.dirs";
160 if(osl_File_E_None == osl_openFile(aConfigFileURL.pData, &handle, osl_File_OpenFlag_Read))
162 rtl::ByteSequence seq;
163 while (osl_File_E_None == osl_readLine(handle , reinterpret_cast<sal_Sequence **>(&seq)))
165 int relative = 0;
166 int len = seq.getLength();
167 seq.realloc(len + 1);
168 seq[len] = 0;
170 p = reinterpret_cast<char *>(seq.getArray());
171 while (*p == ' ' || *p == '\t')
172 p++;
173 if (strncmp (p, "XDG_", 4) != 0)
174 continue;
175 p += 4;
176 if (strncmp (p, OString(type, nLenType).toAsciiUpperCase().getStr(), nLenType) != 0)
177 continue;
178 p += nLenType;
179 if (strncmp (p, "_DIR", 4) != 0)
180 continue;
181 p += 4;
182 while (*p == ' ' || *p == '\t')
183 p++;
184 if (*p != '=')
185 continue;
186 p++;
187 while (*p == ' ' || *p == '\t')
188 p++;
189 if (*p != '"')
190 continue;
191 p++;
192 if (strncmp (p, "$HOME/", 6) == 0)
194 p += 6;
195 relative = 1;
197 else if (*p != '/')
198 continue;
199 if (relative)
201 aUserDirBuf = aHomeDirURL + "/";
203 else
205 aUserDirBuf.truncate();
207 while (*p && *p != '"')
209 if ((*p == '\\') && (*(p+1) != 0))
210 p++;
211 aUserDirBuf.append(static_cast<sal_Unicode>(*p++));
213 }//end of while
214 osl_closeFile(handle);
216 else
217 bError = true;
218 if (aUserDirBuf.getLength()>0 && !bError)
220 aDocumentsDirURL = aUserDirBuf.makeStringAndClear();
221 if ( bAllowHomeDir ||
222 (aDocumentsDirURL != aHomeDirURL && aDocumentsDirURL != aHomeDirURL + "/") )
224 osl::Directory aDocumentsDir( aDocumentsDirURL );
225 if( osl::FileBase::E_None == aDocumentsDir.open() )
226 return aDocumentsDirURL;
229 /* Use fallbacks historical compatibility if nothing else exists */
230 return aHomeDirURL + "/" + OUString::createFromAscii(type);
233 css::uno::Any xdgDirectoryIfExists(char const * type, bool bAllowHomeDir) {
234 auto url = xdg_user_dir_lookup(type, bAllowHomeDir);
235 return css::uno::Any(
236 osl::Directory(url).open() == osl::FileBase::E_None
237 ? css::beans::Optional<css::uno::Any>(true, css::uno::Any(url))
238 : css::beans::Optional<css::uno::Any>(false, css::uno::Any()));
241 css::uno::Any Default::getPropertyValue(OUString const & PropertyName)
243 if (PropertyName == "TemplatePathVariable")
245 // Never pick up the HOME directory as the default location of user's templates
246 return xdgDirectoryIfExists("Templates", false);
249 if (PropertyName == "WorkPathVariable")
251 return xdgDirectoryIfExists("Documents", true);
254 if ( PropertyName == "EnableATToolSupport" ||
255 PropertyName == "ExternalMailer" ||
256 PropertyName == "SourceViewFontHeight" ||
257 PropertyName == "SourceViewFontName" ||
258 PropertyName == "ooInetFTPProxyName" ||
259 PropertyName == "ooInetFTPProxyPort" ||
260 PropertyName == "ooInetHTTPProxyName" ||
261 PropertyName == "ooInetHTTPProxyPort" ||
262 PropertyName == "ooInetHTTPSProxyName" ||
263 PropertyName == "ooInetHTTPSProxyPort" ||
264 PropertyName == "ooInetNoProxy" ||
265 PropertyName == "ooInetProxyType" ||
266 PropertyName == "givenname" ||
267 PropertyName == "sn" )
269 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
272 throw css::beans::UnknownPropertyException(
273 PropertyName, static_cast< cppu::OWeakObject * >(this));
276 css::uno::Reference< css::uno::XInterface > createBackend(
277 css::uno::Reference< css::uno::XComponentContext > const & context,
278 OUString const & name)
280 try {
281 return css::uno::Reference< css::lang::XMultiComponentFactory >(
282 context->getServiceManager(), css::uno::UNO_SET_THROW)->
283 createInstanceWithContext(name, context);
284 } catch (css::uno::RuntimeException &) {
285 // Assuming these exceptions are real errors:
286 throw;
287 } catch (const css::uno::Exception & e) {
288 // Assuming these exceptions indicate that the service is not installed:
289 SAL_WARN("shell", "createInstance(" << name << ") failed with " << e);
290 return css::uno::Reference< css::uno::XInterface >();
294 css::uno::Reference< css::uno::XInterface > createInstance(
295 css::uno::Reference< css::uno::XComponentContext > const & context)
297 OUString desktop;
298 css::uno::Reference< css::uno::XCurrentContext > current(
299 css::uno::getCurrentContext());
300 if (current.is()) {
301 current->getValueByName("system.desktop-environment") >>= desktop;
304 // Fall back to the default if the specific backend is not available:
305 css::uno::Reference< css::uno::XInterface > backend;
306 if ( desktop == "KDE5" ) {
307 backend = createBackend(
308 context,
309 "com.sun.star.configuration.backend.KDE5Backend");
311 return backend.is()
312 ? backend : static_cast< cppu::OWeakObject * >(new Default);
315 static cppu::ImplementationEntry const services[] = {
316 { &createInstance, &getDefaultImplementationName,
317 &getDefaultSupportedServiceNames, &cppu::createSingleComponentFactory, nullptr,
318 0 },
319 { nullptr, nullptr, nullptr, nullptr, nullptr, 0 }
324 extern "C" SAL_DLLPUBLIC_EXPORT void * desktopbe1_component_getFactory(
325 char const * pImplName, void * pServiceManager, void * pRegistryKey)
327 return cppu::component_getFactoryHelper(
328 pImplName, pServiceManager, pRegistryKey, services);
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */