LibreOffice Module shell (master) 1
desktopbackend.cxx
Go to the documentation of this file.
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#include <sal/log.hxx>
22
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>
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>
48#include <uno/current_context.hxx>
49
50namespace {
51
52class Default:
53 public cppu::WeakImplHelper<
54 css::lang::XServiceInfo, css::beans::XPropertySet >
55{
56public:
57 Default() {}
58 Default(const Default&) = delete;
59 Default& operator=(const Default&) = delete;
60
61private:
62 virtual ~Default() override {}
63
64 virtual OUString SAL_CALL getImplementationName() override
65 { return "com.sun.star.comp.configuration.backend.DesktopBackend"; }
66
67 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
68 { return ServiceName == getSupportedServiceNames()[0]; }
69
70 virtual css::uno::Sequence< OUString > SAL_CALL
72 { return { "com.sun.star.configuration.backend.DesktopBackend" }; }
73
74 virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL
75 getPropertySetInfo() override
76 { return css::uno::Reference< css::beans::XPropertySetInfo >(); }
77
78 virtual void SAL_CALL setPropertyValue(
79 OUString const &, css::uno::Any const &) override;
80
81 virtual css::uno::Any SAL_CALL getPropertyValue(
82 OUString const & PropertyName) override;
83
84 virtual void SAL_CALL addPropertyChangeListener(
85 OUString const &,
86 css::uno::Reference< css::beans::XPropertyChangeListener > const &) override
87 {}
88
89 virtual void SAL_CALL removePropertyChangeListener(
90 OUString const &,
91 css::uno::Reference< css::beans::XPropertyChangeListener > const &) override
92 {}
93
94 virtual void SAL_CALL addVetoableChangeListener(
95 OUString const &,
96 css::uno::Reference< css::beans::XVetoableChangeListener > const &) override
97 {}
98
99 virtual void SAL_CALL removeVetoableChangeListener(
100 OUString const &,
101 css::uno::Reference< css::beans::XVetoableChangeListener > const &) override
102 {}
103};
104
105void Default::setPropertyValue(OUString const &, css::uno::Any const &)
106{
107 throw css::lang::IllegalArgumentException(
108 "setPropertyValue not supported",
109 getXWeak(), -1);
110}
111
112OUString xdg_user_dir_lookup (const char *type, bool bAllowHomeDir)
113{
114 size_t nLenType = strlen(type);
115 char *config_home;
116 char *p;
117 bool bError = false;
118
119 osl::Security aSecurity;
120 oslFileHandle handle;
121 OUString aHomeDirURL;
122 OUString aDocumentsDirURL;
123 OUString aConfigFileURL;
124 OUStringBuffer aUserDirBuf;
125
126 if (!aSecurity.getHomeDir( aHomeDirURL ) )
127 {
128 osl::FileBase::getFileURLFromSystemPath("/tmp", aDocumentsDirURL);
129 return aDocumentsDirURL;
130 }
131
132 config_home = getenv ("XDG_CONFIG_HOME");
133 if (config_home == nullptr || config_home[0] == 0)
134 {
135 aConfigFileURL = aHomeDirURL + "/.config/user-dirs.dirs";
136 }
137 else
138 {
139 aConfigFileURL = OUString::createFromAscii(config_home) + "/user-dirs.dirs";
140 }
141
142 if(osl_File_E_None == osl_openFile(aConfigFileURL.pData, &handle, osl_File_OpenFlag_Read))
143 {
144 rtl::ByteSequence seq;
145 while (osl_File_E_None == osl_readLine(handle , reinterpret_cast<sal_Sequence **>(&seq)))
146 {
147 int relative = 0;
148 int len = seq.getLength();
149 seq.realloc(len + 1);
150 seq[len] = 0;
151
152 p = reinterpret_cast<char *>(seq.getArray());
153 while (*p == ' ' || *p == '\t')
154 p++;
155 if (strncmp (p, "XDG_", 4) != 0)
156 continue;
157 p += 4;
158 if (strncmp (p, OString(type, nLenType).toAsciiUpperCase().getStr(), nLenType) != 0)
159 continue;
160 p += nLenType;
161 if (strncmp (p, "_DIR", 4) != 0)
162 continue;
163 p += 4;
164 while (*p == ' ' || *p == '\t')
165 p++;
166 if (*p != '=')
167 continue;
168 p++;
169 while (*p == ' ' || *p == '\t')
170 p++;
171 if (*p != '"')
172 continue;
173 p++;
174 if (strncmp (p, "$HOME/", 6) == 0)
175 {
176 p += 6;
177 relative = 1;
178 }
179 else if (*p != '/')
180 continue;
181 if (relative)
182 {
183 aUserDirBuf = aHomeDirURL + "/";
184 }
185 else
186 {
187 aUserDirBuf.truncate();
188 }
189 while (*p && *p != '"')
190 {
191 if ((*p == '\\') && (*(p+1) != 0))
192 p++;
193 aUserDirBuf.append(static_cast<sal_Unicode>(*p++));
194 }
195 }//end of while
196 osl_closeFile(handle);
197 }
198 else
199 bError = true;
200 if (aUserDirBuf.getLength()>0 && !bError)
201 {
202 aDocumentsDirURL = aUserDirBuf.makeStringAndClear();
203 if ( bAllowHomeDir ||
204 (aDocumentsDirURL != aHomeDirURL && aDocumentsDirURL != Concat2View(aHomeDirURL + "/")) )
205 {
206 osl::Directory aDocumentsDir( aDocumentsDirURL );
207 if( osl::FileBase::E_None == aDocumentsDir.open() )
208 return aDocumentsDirURL;
209 }
210 }
211 /* Use fallbacks historical compatibility if nothing else exists */
212 return aHomeDirURL + "/" + OUString::createFromAscii(type);
213}
214
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()));
221}
222
223css::uno::Any Default::getPropertyValue(OUString const & PropertyName)
224{
225 if (PropertyName == "TemplatePathVariable")
226 {
227 // Never pick up the HOME directory as the default location of user's templates
228 return xdgDirectoryIfExists("Templates", false);
229 }
230
231 if (PropertyName == "WorkPathVariable")
232 {
233 return xdgDirectoryIfExists("Documents", true);
234 }
235
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" )
250 {
251 return css::uno::Any(css::beans::Optional< css::uno::Any >());
252 }
253
254 throw css::beans::UnknownPropertyException(
255 PropertyName, getXWeak());
256}
257
258css::uno::Reference< css::uno::XInterface > createBackend(
259 css::uno::Reference< css::uno::XComponentContext > const & context,
260 OUString const & name)
261{
262 try {
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:
268 throw;
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 >();
273 }
274}
275
276extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
277shell_DesktopBackend_get_implementation(
278 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
279{
280 OUString desktop;
281 css::uno::Reference< css::uno::XCurrentContext > current(
282 css::uno::getCurrentContext());
283 if (current.is()) {
284 current->getValueByName("system.desktop-environment") >>= desktop;
285 }
286
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");
292 if (!backend)
293 backend = getXWeak(new Default);
294 backend->acquire();
295 return backend.get();
296}
297
298}
299
300
301/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define TOOLS_WARN_EXCEPTION(area, stream)
void * p
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)
unsigned char sal_Bool
sal_uInt16 sal_Unicode