Debian Codegraph @

shell/source/backends/desktopbe/desktopbackend.cxx

raw
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>
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>
49 #include <vcl/svapp.hxx>
50
51 namespace {
52
53 class Default:
54 public cppu::WeakImplHelper<
55 css::lang::XServiceInfo, css::beans::XPropertySet >
56 {
57 public:
58 Default() {}
59 Default(const Default&) = delete;
60 Default& operator=(const Default&) = delete;
61
62 private:
63 virtual ~Default() override {}
64
65 virtual OUString SAL_CALL getImplementationName() override
66 { return u"com.sun.star.comp.configuration.backend.DesktopBackend"_ustr; }
67
68 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
69 { return ServiceName == getSupportedServiceNames()[0]; }
70
71 virtual css::uno::Sequence< OUString > SAL_CALL
72 getSupportedServiceNames() override
73 { return { u"com.sun.star.configuration.backend.DesktopBackend"_ustr }; }
74
75 virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL
76 getPropertySetInfo() override
77 { return css::uno::Reference< css::beans::XPropertySetInfo >(); }
78
79 virtual void SAL_CALL setPropertyValue(
80 OUString const &, css::uno::Any const &) override;
81
82 virtual css::uno::Any SAL_CALL getPropertyValue(
83 OUString const & PropertyName) override;
84
85 virtual void SAL_CALL addPropertyChangeListener(
86 OUString const &,
87 css::uno::Reference< css::beans::XPropertyChangeListener > const &) override
88 {}
89
90 virtual void SAL_CALL removePropertyChangeListener(
91 OUString const &,
92 css::uno::Reference< css::beans::XPropertyChangeListener > const &) override
93 {}
94
95 virtual void SAL_CALL addVetoableChangeListener(
96 OUString const &,
97 css::uno::Reference< css::beans::XVetoableChangeListener > const &) override
98 {}
99
100 virtual void SAL_CALL removeVetoableChangeListener(
101 OUString const &,
102 css::uno::Reference< css::beans::XVetoableChangeListener > const &) override
103 {}
104 };
105
106 void Default::setPropertyValue(OUString const &, css::uno::Any const &)
107 {
108 throw css::lang::IllegalArgumentException(
109 u"setPropertyValue not supported"_ustr,
110 getXWeak(), -1);
111 }
112
113 OUString xdg_user_dir_lookup (const char *type, bool bAllowHomeDir)
114 {
115 size_t nLenType = strlen(type);
116 char *config_home;
117 char *p;
118 bool bError = false;
119
120 osl::Security aSecurity;
121 oslFileHandle handle;
122 OUString aHomeDirURL;
123 OUString aDocumentsDirURL;
124 OUString aConfigFileURL;
125 OUStringBuffer aUserDirBuf;
126
127 if (!aSecurity.getHomeDir( aHomeDirURL ) )
128 {
129 osl::FileBase::getFileURLFromSystemPath(u"/tmp"_ustr, aDocumentsDirURL);
130 return aDocumentsDirURL;
131 }
132
133 config_home = getenv ("XDG_CONFIG_HOME");
134 if (config_home == nullptr || config_home[0] == 0)
135 {
136 aConfigFileURL = aHomeDirURL + "/.config/user-dirs.dirs";
137 }
138 else
139 {
140 aConfigFileURL = OUString::createFromAscii(config_home) + "/user-dirs.dirs";
141 }
142
143 if(osl_File_E_None == osl_openFile(aConfigFileURL.pData, &handle, osl_File_OpenFlag_Read))
144 {
145 rtl::ByteSequence seq;
146 while (osl_File_E_None == osl_readLine(handle , reinterpret_cast<sal_Sequence **>(&seq)))
147 {
148 int relative = 0;
149 int len = seq.getLength();
150 seq.realloc(len + 1);
151 seq[len] = 0;
152
153 p = reinterpret_cast<char *>(seq.getArray());
154 while (*p == ' ' || *p == '\t')
155 p++;
156 if (strncmp (p, "XDG_", 4) != 0)
157 continue;
158 p += 4;
159 if (strncmp (p, OString(type, nLenType).toAsciiUpperCase().getStr(), nLenType) != 0)
160 continue;
161 p += nLenType;
162 if (strncmp (p, "_DIR", 4) != 0)
163 continue;
164 p += 4;
165 while (*p == ' ' || *p == '\t')
166 p++;
167 if (*p != '=')
168 continue;
169 p++;
170 while (*p == ' ' || *p == '\t')
171 p++;
172 if (*p != '"')
173 continue;
174 p++;
175 if (strncmp (p, "$HOME/", 6) == 0)
176 {
177 p += 6;
178 relative = 1;
179 }
180 else if (*p != '/')
181 continue;
182 if (relative)
183 {
184 aUserDirBuf = aHomeDirURL + "/";
185 }
186 else
187 {
188 aUserDirBuf.truncate();
189 }
190 while (*p && *p != '"')
191 {
192 if ((*p == '\\') && (*(p+1) != 0))
193 p++;
194 aUserDirBuf.append(static_cast<sal_Unicode>(*p++));
195 }
196 }//end of while
197 osl_closeFile(handle);
198 }
199 else
200 bError = true;
201 if (aUserDirBuf.getLength()>0 && !bError)
202 {
203 aDocumentsDirURL = aUserDirBuf.makeStringAndClear();
204 if ( bAllowHomeDir ||
205 (aDocumentsDirURL != aHomeDirURL && aDocumentsDirURL != Concat2View(aHomeDirURL + "/")) )
206 {
207 osl::Directory aDocumentsDir( aDocumentsDirURL );
208 if( osl::FileBase::E_None == aDocumentsDir.open() )
209 return aDocumentsDirURL;
210 }
211 }
212 /* Use fallbacks historical compatibility if nothing else exists */
213 return aHomeDirURL + "/" + OUString::createFromAscii(type);
214 }
215
216 css::uno::Any xdgDirectoryIfExists(char const * type, bool bAllowHomeDir) {
217 auto url = xdg_user_dir_lookup(type, bAllowHomeDir);
218 return css::uno::Any(
219 osl::Directory(url).open() == osl::FileBase::E_None
220 ? css::beans::Optional<css::uno::Any>(true, css::uno::Any(url))
221 : css::beans::Optional<css::uno::Any>(false, css::uno::Any()));
222 }
223
224 css::uno::Any Default::getPropertyValue(OUString const & PropertyName)
225 {
226 if (PropertyName == "TemplatePathVariable")
227 {
228 // Never pick up the HOME directory as the default location of user's templates
229 return xdgDirectoryIfExists("Templates", false);
230 }
231
232 if (PropertyName == "WorkPathVariable")
233 {
234 return xdgDirectoryIfExists("Documents", true);
235 }
236
237 if ( PropertyName == "ExternalMailer" ||
238 PropertyName == "SourceViewFontHeight" ||
239 PropertyName == "SourceViewFontName" ||
240 PropertyName == "ooInetHTTPProxyName" ||
241 PropertyName == "ooInetHTTPProxyPort" ||
242 PropertyName == "ooInetHTTPSProxyName" ||
243 PropertyName == "ooInetHTTPSProxyPort" ||
244 PropertyName == "ooInetNoProxy" ||
245 PropertyName == "ooInetProxyType" ||
246 PropertyName == "givenname" ||
247 PropertyName == "sn" )
248 {
249 return css::uno::Any(css::beans::Optional< css::uno::Any >());
250 }
251
252 throw css::beans::UnknownPropertyException(
253 PropertyName, getXWeak());
254 }
255
256 css::uno::Reference< css::uno::XInterface > createBackend(
257 css::uno::Reference< css::uno::XComponentContext > const & context,
258 OUString const & name)
259 {
260 try {
261 return css::uno::Reference< css::lang::XMultiComponentFactory >(
262 context->getServiceManager(), css::uno::UNO_SET_THROW)->
263 createInstanceWithContext(name, context);
264 } catch (css::uno::RuntimeException &) {
265 // Assuming these exceptions are real errors:
266 throw;
267 } catch (const css::uno::Exception &) {
268 // Assuming these exceptions indicate that the service is not installed:
269 TOOLS_WARN_EXCEPTION("shell", "createInstance(" << name << ") failed");
270 return css::uno::Reference< css::uno::XInterface >();
271 }
272 }
273
274 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
275 shell_DesktopBackend_get_implementation(
276 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
277 {
278 // Fall back to the default if the specific backend is not available:
279 css::uno::Reference< css::uno::XInterface > backend;
280 if (Application::GetDesktopEnvironment() == u"PLASMA5")
281 backend = createBackend(context,
282 u"com.sun.star.configuration.backend.KF5Backend"_ustr);
283 if (!backend)
284 backend = getXWeak(new Default);
285 backend->acquire();
286 return backend.get();
287 }
288
289 }
290
291
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */