Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / shell / source / backends / kde5be / kde5backend.cxx
blob0c0472b0233a3b8695e53f9772d284fd69bc4ee4
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>
22 #include <QtWidgets/QApplication>
24 #include <boost/noncopyable.hpp>
25 #include <com/sun/star/beans/Optional.hpp>
26 #include <com/sun/star/beans/PropertyVetoException.hpp>
27 #include <com/sun/star/beans/UnknownPropertyException.hpp>
28 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/beans/XPropertySetInfo.hpp>
31 #include <com/sun/star/beans/XVetoableChangeListener.hpp>
32 #include <com/sun/star/lang/IllegalArgumentException.hpp>
33 #include <com/sun/star/lang/WrappedTargetException.hpp>
34 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
35 #include <com/sun/star/lang/XServiceInfo.hpp>
36 #include <com/sun/star/uno/Any.hxx>
37 #include <com/sun/star/uno/Reference.hxx>
38 #include <com/sun/star/uno/RuntimeException.hpp>
39 #include <com/sun/star/uno/Sequence.hxx>
40 #include <com/sun/star/uno/XComponentContext.hpp>
41 #include <com/sun/star/uno/XCurrentContext.hpp>
42 #include <cppuhelper/factory.hxx>
43 #include <cppuhelper/implbase.hxx>
44 #include <cppuhelper/implementationentry.hxx>
45 #include <cppuhelper/weak.hxx>
46 #include <rtl/string.h>
47 #include <rtl/ustring.h>
48 #include <rtl/ustring.hxx>
49 #include <sal/types.h>
50 #include <uno/current_context.hxx>
52 #include <osl/process.h>
53 #include <osl/thread.h>
55 #include "kde5access.hxx"
57 namespace
59 OUString getServiceImplementationName()
61 return OUString("com.sun.star.comp.configuration.backend.KDE5Backend");
64 css::uno::Sequence<OUString> getServiceSupportedServiceNames()
66 OUString name("com.sun.star.configuration.backend.KDE5Backend");
67 return css::uno::Sequence<OUString>(&name, 1);
70 class Service : public cppu::WeakImplHelper<css::lang::XServiceInfo, css::beans::XPropertySet>,
71 private boost::noncopyable
73 public:
74 Service();
76 private:
77 virtual ~Service() override {}
79 virtual OUString SAL_CALL getImplementationName() override
81 return getServiceImplementationName();
84 virtual sal_Bool SAL_CALL supportsService(OUString const& ServiceName) override
86 return ServiceName == getSupportedServiceNames()[0];
89 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
91 return getServiceSupportedServiceNames();
94 virtual css::uno::Reference<css::beans::XPropertySetInfo> SAL_CALL getPropertySetInfo() override
96 return css::uno::Reference<css::beans::XPropertySetInfo>();
99 virtual void SAL_CALL setPropertyValue(OUString const&, css::uno::Any const&) override;
101 virtual css::uno::Any SAL_CALL getPropertyValue(OUString const& PropertyName) override;
103 virtual void SAL_CALL addPropertyChangeListener(
104 OUString const&, css::uno::Reference<css::beans::XPropertyChangeListener> const&) override
108 virtual void SAL_CALL removePropertyChangeListener(
109 OUString const&, css::uno::Reference<css::beans::XPropertyChangeListener> const&) override
113 virtual void SAL_CALL addVetoableChangeListener(
114 OUString const&, css::uno::Reference<css::beans::XVetoableChangeListener> const&) override
118 virtual void SAL_CALL removeVetoableChangeListener(
119 OUString const&, css::uno::Reference<css::beans::XVetoableChangeListener> const&) override
123 bool enabled_;
126 OString getDisplayArg()
128 OUString aParam;
129 const sal_uInt32 nParams = osl_getCommandArgCount();
130 for (sal_uInt32 nIdx = 0; nIdx < nParams; ++nIdx)
132 osl_getCommandArg(nIdx, &aParam.pData);
133 if (aParam != "-display")
134 continue;
136 ++nIdx;
137 osl_getCommandArg(nIdx, &aParam.pData);
138 return OUStringToOString(aParam, osl_getThreadTextEncoding());
140 return {};
143 OString getExecutable()
145 OUString aParam, aBin;
146 osl_getExecutableFile(&aParam.pData);
147 osl_getSystemPathFromFileURL(aParam.pData, &aBin.pData);
148 return OUStringToOString(aBin, osl_getThreadTextEncoding());
151 // init the QApplication when we load the kde5backend into a non-Qt vclplug (e.g. Gtk3KDE5)
152 // TODO: use a helper process to read these values without linking to Qt directly?
153 // TODO: share this code somehow with Qt5Instance.cxx?
154 void initQApp()
156 const auto aDisplay = getDisplayArg();
157 int nFakeArgc = aDisplay.isEmpty() ? 2 : 3;
158 char** pFakeArgv = new char*[nFakeArgc];
160 pFakeArgv[0] = strdup(getExecutable().getStr());
161 pFakeArgv[1] = strdup("--nocrashhandler");
162 if (aDisplay.isEmpty())
163 pFakeArgv[2] = strdup(aDisplay.getStr());
165 char* session_manager = nullptr;
166 if (auto* session_manager_env = getenv("SESSION_MANAGER"))
168 session_manager = strdup(session_manager_env);
169 unsetenv("SESSION_MANAGER");
172 auto app = new QApplication(nFakeArgc, pFakeArgv);
173 QObject::connect(app, &QObject::destroyed, app, [nFakeArgc, pFakeArgv]() {
174 for (int i = 0; i < nFakeArgc; ++i)
175 delete pFakeArgv[i];
176 delete[] pFakeArgv;
179 if (session_manager != nullptr)
181 // coverity[tainted_string] - trusted source for setenv
182 setenv("SESSION_MANAGER", session_manager, 1);
183 free(session_manager);
186 QApplication::setQuitOnLastWindowClosed(false);
189 Service::Service()
190 : enabled_(false)
192 css::uno::Reference<css::uno::XCurrentContext> context(css::uno::getCurrentContext());
193 if (context.is())
195 if (!qApp)
197 initQApp();
199 OUString desktop;
200 context->getValueByName("system.desktop-environment") >>= desktop;
201 enabled_ = desktop == "KDE5" && qApp != nullptr;
205 void Service::setPropertyValue(OUString const&, css::uno::Any const&)
207 throw css::lang::IllegalArgumentException("setPropertyValue not supported",
208 static_cast<cppu::OWeakObject*>(this), -1);
211 css::uno::Any Service::getPropertyValue(OUString const& PropertyName)
213 if (PropertyName == "EnableATToolSupport" || PropertyName == "ExternalMailer"
214 || PropertyName == "SourceViewFontHeight" || PropertyName == "SourceViewFontName"
215 || PropertyName == "WorkPathVariable" || PropertyName == "ooInetFTPProxyName"
216 || PropertyName == "ooInetFTPProxyPort" || PropertyName == "ooInetHTTPProxyName"
217 || PropertyName == "ooInetHTTPProxyPort" || PropertyName == "ooInetHTTPSProxyName"
218 || PropertyName == "ooInetHTTPSProxyPort" || PropertyName == "ooInetNoProxy"
219 || PropertyName == "ooInetProxyType")
221 return css::uno::makeAny(enabled_ ? kde5access::getValue(PropertyName)
222 : css::beans::Optional<css::uno::Any>());
224 else if (PropertyName == "givenname" || PropertyName == "sn"
225 || PropertyName == "TemplatePathVariable")
227 return css::uno::makeAny(css::beans::Optional<css::uno::Any>());
228 //TODO: obtain values from KDE?
230 throw css::beans::UnknownPropertyException(PropertyName, static_cast<cppu::OWeakObject*>(this));
233 css::uno::Reference<css::uno::XInterface>
234 createInstance(css::uno::Reference<css::uno::XComponentContext> const&)
236 return static_cast<cppu::OWeakObject*>(new Service);
239 static cppu::ImplementationEntry const services[]
240 = { { &createInstance, &getServiceImplementationName, &getServiceSupportedServiceNames,
241 &cppu::createSingleComponentFactory, nullptr, 0 },
242 { nullptr, nullptr, nullptr, nullptr, nullptr, 0 } };
245 extern "C" SAL_DLLPUBLIC_EXPORT void*
246 kde5be1_component_getFactory(char const* pImplName, void* pServiceManager, void* pRegistryKey)
248 return cppu::component_getFactoryHelper(pImplName, pServiceManager, pRegistryKey, services);
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */