LibreOffice Module shell (master) 1
kf5backend.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
22#include <memory>
23
24#include <QtWidgets/QApplication>
25
26#include <com/sun/star/beans/Optional.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/XServiceInfo.hpp>
34#include <com/sun/star/uno/Any.hxx>
35#include <com/sun/star/uno/Reference.hxx>
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 <rtl/ustring.hxx>
42#include <sal/types.h>
43#include <uno/current_context.hxx>
44#include <vcl/svapp.hxx>
45
46#include <osl/process.h>
47#include <osl/thread.h>
48
49#include "kf5access.hxx"
50
51namespace
52{
53class Service : public cppu::WeakImplHelper<css::lang::XServiceInfo, css::beans::XPropertySet>
54{
55public:
56 Service();
57
58private:
59 // noncopyable until we have good reasons...
60 Service(const Service&) = delete;
61 Service& operator=(const Service&) = delete;
62
63 virtual ~Service() override {}
64
65 virtual OUString SAL_CALL getImplementationName() override
66 {
67 return "com.sun.star.comp.configuration.backend.KF5Backend";
68 }
69
70 virtual sal_Bool SAL_CALL supportsService(OUString const& ServiceName) override
71 {
73 }
74
75 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
76 {
77 return { "com.sun.star.configuration.backend.KF5Backend" };
78 }
79
80 virtual css::uno::Reference<css::beans::XPropertySetInfo> SAL_CALL getPropertySetInfo() override
81 {
82 return css::uno::Reference<css::beans::XPropertySetInfo>();
83 }
84
85 virtual void SAL_CALL setPropertyValue(OUString const&, css::uno::Any const&) override;
86
87 virtual css::uno::Any SAL_CALL getPropertyValue(OUString const& PropertyName) override;
88
89 virtual void SAL_CALL addPropertyChangeListener(
90 OUString const&, css::uno::Reference<css::beans::XPropertyChangeListener> const&) override
91 {
92 }
93
94 virtual void SAL_CALL removePropertyChangeListener(
95 OUString const&, css::uno::Reference<css::beans::XPropertyChangeListener> const&) override
96 {
97 }
98
99 virtual void SAL_CALL addVetoableChangeListener(
100 OUString const&, css::uno::Reference<css::beans::XVetoableChangeListener> const&) override
101 {
102 }
103
104 virtual void SAL_CALL removeVetoableChangeListener(
105 OUString const&, css::uno::Reference<css::beans::XVetoableChangeListener> const&) override
106 {
107 }
108
109 std::map<OUString, css::beans::Optional<css::uno::Any>> m_KDESettings;
110};
111
112OString getDisplayArg()
113{
114 OUString aParam;
115 const sal_uInt32 nParams = osl_getCommandArgCount();
116 for (sal_uInt32 nIdx = 0; nIdx < nParams; ++nIdx)
117 {
118 osl_getCommandArg(nIdx, &aParam.pData);
119 if (aParam != "-display")
120 continue;
121
122 ++nIdx;
123 osl_getCommandArg(nIdx, &aParam.pData);
124 return OUStringToOString(aParam, osl_getThreadTextEncoding());
125 }
126 return {};
127}
128
129OString getExecutable()
130{
131 OUString aParam, aBin;
132 osl_getExecutableFile(&aParam.pData);
133 osl_getSystemPathFromFileURL(aParam.pData, &aBin.pData);
134 return OUStringToOString(aBin, osl_getThreadTextEncoding());
135}
136
137void readKDESettings(std::map<OUString, css::beans::Optional<css::uno::Any>>& rSettings)
138{
139 const std::vector<OUString> aKeys
140 = { "EnableATToolSupport", "ExternalMailer", "SourceViewFontHeight",
141 "SourceViewFontName", "WorkPathVariable", "ooInetFTPProxyName",
142 "ooInetFTPProxyPort", "ooInetHTTPProxyName", "ooInetHTTPProxyPort",
143 "ooInetHTTPSProxyName", "ooInetHTTPSProxyPort", "ooInetNoProxy",
144 "ooInetProxyType" };
145
146 for (const OUString& aKey : aKeys)
147 {
148 css::beans::Optional<css::uno::Any> aValue = kf5access::getValue(aKey);
149 std::pair<OUString, css::beans::Optional<css::uno::Any>> elem
150 = std::make_pair(aKey, aValue);
151 rSettings.insert(elem);
152 }
153}
154
155// init the QApplication when we load the kf5backend into a non-Qt vclplug (e.g. gtk3_kde5)
156// TODO: use a helper process to read these values without linking to Qt directly?
157// TODO: share this code somehow with Qt5Instance.cxx?
158void initQApp(std::map<OUString, css::beans::Optional<css::uno::Any>>& rSettings)
159{
160 const auto aDisplay = getDisplayArg();
161 int nFakeArgc = aDisplay.isEmpty() ? 2 : 3;
162 char** pFakeArgv = new char*[nFakeArgc];
163
164 pFakeArgv[0] = strdup(getExecutable().getStr());
165 pFakeArgv[1] = strdup("--nocrashhandler");
166 if (!aDisplay.isEmpty())
167 pFakeArgv[2] = strdup(aDisplay.getStr());
168
169 char* session_manager = nullptr;
170 if (auto* session_manager_env = getenv("SESSION_MANAGER"))
171 {
172 session_manager = strdup(session_manager_env);
173 unsetenv("SESSION_MANAGER");
174 }
175
176 {
177 // rhbz#2047319 drop the SolarMutex during the execution of QApplication::init()
178 // https://invent.kde.org/qt/qt/qtwayland/-/merge_requests/24#note_383915
179 SolarMutexReleaser aReleaser; // rhbz#2047319 drop the SolarMutex during the execution
180
181 std::unique_ptr<QApplication> app(new QApplication(nFakeArgc, pFakeArgv));
182 QObject::connect(app.get(), &QObject::destroyed, app.get(), [nFakeArgc, pFakeArgv]() {
183 for (int i = 0; i < nFakeArgc; ++i)
184 free(pFakeArgv[i]);
185 delete[] pFakeArgv;
186 });
187
188 readKDESettings(rSettings);
189 }
190
191 if (session_manager != nullptr)
192 {
193 // coverity[tainted_string] - trusted source for setenv
194 setenv("SESSION_MANAGER", session_manager, 1);
195 free(session_manager);
196 }
197}
198
199Service::Service()
200{
201 css::uno::Reference<css::uno::XCurrentContext> context(css::uno::getCurrentContext());
202 if (!context.is())
203 return;
204
205 OUString desktop;
206 context->getValueByName("system.desktop-environment") >>= desktop;
207
208 if (desktop == "PLASMA5")
209 {
210 if (!qApp) // no qt event loop yet
211 {
212 // so we start one and read KDE settings
213 initQApp(m_KDESettings);
214 }
215 else // someone else (most likely kde/qt vclplug) has started qt event loop
216 // all that is left to do is to read KDE settings
217 readKDESettings(m_KDESettings);
218 }
219}
220
221void Service::setPropertyValue(OUString const&, css::uno::Any const&)
222{
223 throw css::lang::IllegalArgumentException("setPropertyValue not supported", getXWeak(), -1);
224}
225
226css::uno::Any Service::getPropertyValue(OUString const& PropertyName)
227{
228 if (PropertyName == "EnableATToolSupport" || PropertyName == "ExternalMailer"
229 || PropertyName == "SourceViewFontHeight" || PropertyName == "SourceViewFontName"
230 || PropertyName == "WorkPathVariable" || PropertyName == "ooInetFTPProxyName"
231 || PropertyName == "ooInetFTPProxyPort" || PropertyName == "ooInetHTTPProxyName"
232 || PropertyName == "ooInetHTTPProxyPort" || PropertyName == "ooInetHTTPSProxyName"
233 || PropertyName == "ooInetHTTPSProxyPort" || PropertyName == "ooInetNoProxy"
234 || PropertyName == "ooInetProxyType")
235 {
236 std::map<OUString, css::beans::Optional<css::uno::Any>>::iterator it
237 = m_KDESettings.find(PropertyName);
238 if (it != m_KDESettings.end())
239 return css::uno::Any(it->second);
240 else
241 return css::uno::Any(css::beans::Optional<css::uno::Any>());
242 }
243 else if (PropertyName == "givenname" || PropertyName == "sn"
244 || PropertyName == "TemplatePathVariable")
245 {
246 return css::uno::Any(css::beans::Optional<css::uno::Any>());
247 //TODO: obtain values from KDE?
248 }
249 throw css::beans::UnknownPropertyException(PropertyName, getXWeak());
250}
251
252extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
253shell_kf5desktop_get_implementation(css::uno::XComponentContext*,
254 css::uno::Sequence<css::uno::Any> const&)
255{
256 return cppu::acquire(new Service());
257}
258}
259/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
css::beans::Optional< css::uno::Any > getValue(std::u16string_view id)
Definition: kf5access.cxx:57
VBAHELPER_DLLPUBLIC bool setPropertyValue(css::uno::Sequence< css::beans::PropertyValue > &aProp, const OUString &aName, const css::uno::Any &aValue)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
bool getPropertyValue(ValueType &rValue, css::uno::Reference< css::beans::XPropertySet > const &xPropSet, OUString const &propName)
unsigned char sal_Bool