look for java, javac and javadoc with the .exe suffix on windows
[LibreOffice.git] / shell / source / backends / desktopbe / desktopbackend.cxx
blobc9a9d8dafcd2a0e510b3fbe9c8169a8601e2a12f
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/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>
50 namespace {
52 class Default:
53 public cppu::WeakImplHelper<
54 css::lang::XServiceInfo, css::beans::XPropertySet >
56 public:
57 Default() {}
58 Default(const Default&) = delete;
59 Default& operator=(const Default&) = delete;
61 private:
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(
85 OUString const &,
86 css::uno::Reference< css::beans::XPropertyChangeListener > const &) override
89 virtual void SAL_CALL removePropertyChangeListener(
90 OUString const &,
91 css::uno::Reference< css::beans::XPropertyChangeListener > const &) override
94 virtual void SAL_CALL addVetoableChangeListener(
95 OUString const &,
96 css::uno::Reference< css::beans::XVetoableChangeListener > const &) override
99 virtual void SAL_CALL removeVetoableChangeListener(
100 OUString const &,
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 getXWeak(), -1);
112 OUString xdg_user_dir_lookup (const char *type, bool bAllowHomeDir)
114 size_t nLenType = strlen(type);
115 char *config_home;
116 char *p;
117 bool bError = false;
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";
137 else
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)))
147 int relative = 0;
148 int len = seq.getLength();
149 seq.realloc(len + 1);
150 seq[len] = 0;
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)
176 p += 6;
177 relative = 1;
179 else if (*p != '/')
180 continue;
181 if (relative)
183 aUserDirBuf = aHomeDirURL + "/";
185 else
187 aUserDirBuf.truncate();
189 while (*p && *p != '"')
191 if ((*p == '\\') && (*(p+1) != 0))
192 p++;
193 aUserDirBuf.append(static_cast<sal_Unicode>(*p++));
195 }//end of while
196 osl_closeFile(handle);
198 else
199 bError = true;
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 == "ooInetHTTPProxyName" ||
241 PropertyName == "ooInetHTTPProxyPort" ||
242 PropertyName == "ooInetHTTPSProxyName" ||
243 PropertyName == "ooInetHTTPSProxyPort" ||
244 PropertyName == "ooInetNoProxy" ||
245 PropertyName == "ooInetProxyType" ||
246 PropertyName == "givenname" ||
247 PropertyName == "sn" )
249 return css::uno::Any(css::beans::Optional< css::uno::Any >());
252 throw css::beans::UnknownPropertyException(
253 PropertyName, getXWeak());
256 css::uno::Reference< css::uno::XInterface > createBackend(
257 css::uno::Reference< css::uno::XComponentContext > const & context,
258 OUString const & name)
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 >();
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&)
278 OUString desktop;
279 css::uno::Reference< css::uno::XCurrentContext > current(
280 css::uno::getCurrentContext());
281 if (current.is()) {
282 current->getValueByName("system.desktop-environment") >>= desktop;
285 // Fall back to the default if the specific backend is not available:
286 css::uno::Reference< css::uno::XInterface > backend;
287 if (desktop == "PLASMA5")
288 backend = createBackend(context,
289 "com.sun.star.configuration.backend.KF5Backend");
290 if (!backend)
291 backend = getXWeak(new Default);
292 backend->acquire();
293 return backend.get();
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */