1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
24 #include <QtWidgets/QApplication>
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>
39 #include <cppuhelper/implbase.hxx>
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>
46 #include <osl/process.h>
47 #include <osl/thread.h>
49 #include "kf5access.hxx"
53 class Service
: public cppu::WeakImplHelper
<css::lang::XServiceInfo
, css::beans::XPropertySet
>
59 // noncopyable until we have good reasons...
60 Service(const Service
&) = delete;
61 Service
& operator=(const Service
&) = delete;
63 virtual ~Service() override
{}
65 virtual OUString SAL_CALL
getImplementationName() override
67 return "com.sun.star.comp.configuration.backend.KF5Backend";
70 virtual sal_Bool SAL_CALL
supportsService(OUString
const& ServiceName
) override
72 return ServiceName
== getSupportedServiceNames()[0];
75 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames() override
77 return { "com.sun.star.configuration.backend.KF5Backend" };
80 virtual css::uno::Reference
<css::beans::XPropertySetInfo
> SAL_CALL
getPropertySetInfo() override
82 return css::uno::Reference
<css::beans::XPropertySetInfo
>();
85 virtual void SAL_CALL
setPropertyValue(OUString
const&, css::uno::Any
const&) override
;
87 virtual css::uno::Any SAL_CALL
getPropertyValue(OUString
const& PropertyName
) override
;
89 virtual void SAL_CALL
addPropertyChangeListener(
90 OUString
const&, css::uno::Reference
<css::beans::XPropertyChangeListener
> const&) override
94 virtual void SAL_CALL
removePropertyChangeListener(
95 OUString
const&, css::uno::Reference
<css::beans::XPropertyChangeListener
> const&) override
99 virtual void SAL_CALL
addVetoableChangeListener(
100 OUString
const&, css::uno::Reference
<css::beans::XVetoableChangeListener
> const&) override
104 virtual void SAL_CALL
removeVetoableChangeListener(
105 OUString
const&, css::uno::Reference
<css::beans::XVetoableChangeListener
> const&) override
109 std::map
<OUString
, css::beans::Optional
<css::uno::Any
>> m_KDESettings
;
112 OString
getDisplayArg()
115 const sal_uInt32 nParams
= osl_getCommandArgCount();
116 for (sal_uInt32 nIdx
= 0; nIdx
< nParams
; ++nIdx
)
118 osl_getCommandArg(nIdx
, &aParam
.pData
);
119 if (aParam
!= "-display")
123 osl_getCommandArg(nIdx
, &aParam
.pData
);
124 return OUStringToOString(aParam
, osl_getThreadTextEncoding());
129 OString
getExecutable()
131 OUString aParam
, aBin
;
132 osl_getExecutableFile(&aParam
.pData
);
133 osl_getSystemPathFromFileURL(aParam
.pData
, &aBin
.pData
);
134 return OUStringToOString(aBin
, osl_getThreadTextEncoding());
137 void readKDESettings(std::map
<OUString
, css::beans::Optional
<css::uno::Any
>>& rSettings
)
139 const std::vector
<OUString
> aKeys
140 = { "EnableATToolSupport", "ExternalMailer", "SourceViewFontHeight",
141 "SourceViewFontName", "WorkPathVariable", "ooInetHTTPProxyName",
142 "ooInetHTTPProxyPort", "ooInetHTTPSProxyName", "ooInetHTTPSProxyPort",
143 "ooInetNoProxy", "ooInetProxyType" };
145 for (const OUString
& aKey
: aKeys
)
147 css::beans::Optional
<css::uno::Any
> aValue
= kf5access::getValue(aKey
);
148 std::pair
<OUString
, css::beans::Optional
<css::uno::Any
>> elem
149 = std::make_pair(aKey
, aValue
);
150 rSettings
.insert(elem
);
154 // init the QApplication when we load the kf5backend into a non-Qt vclplug (e.g. gtk3_kde5)
155 // TODO: use a helper process to read these values without linking to Qt directly?
156 // TODO: share this code somehow with Qt5Instance.cxx?
157 void initQApp(std::map
<OUString
, css::beans::Optional
<css::uno::Any
>>& rSettings
)
159 const auto aDisplay
= getDisplayArg();
160 int nFakeArgc
= aDisplay
.isEmpty() ? 2 : 3;
161 char** pFakeArgv
= new char*[nFakeArgc
];
163 pFakeArgv
[0] = strdup(getExecutable().getStr());
164 pFakeArgv
[1] = strdup("--nocrashhandler");
165 if (!aDisplay
.isEmpty())
166 pFakeArgv
[2] = strdup(aDisplay
.getStr());
168 char* session_manager
= nullptr;
169 if (auto* session_manager_env
= getenv("SESSION_MANAGER"))
171 session_manager
= strdup(session_manager_env
);
172 unsetenv("SESSION_MANAGER");
176 // rhbz#2047319 drop the SolarMutex during the execution of QApplication::init()
177 // https://invent.kde.org/qt/qt/qtwayland/-/merge_requests/24#note_383915
178 SolarMutexReleaser aReleaser
; // rhbz#2047319 drop the SolarMutex during the execution
180 std::unique_ptr
<QApplication
> app(new QApplication(nFakeArgc
, pFakeArgv
));
181 QObject::connect(app
.get(), &QObject::destroyed
, app
.get(), [nFakeArgc
, pFakeArgv
]() {
182 for (int i
= 0; i
< nFakeArgc
; ++i
)
187 readKDESettings(rSettings
);
190 if (session_manager
!= nullptr)
192 // coverity[tainted_string] - trusted source for setenv
193 setenv("SESSION_MANAGER", session_manager
, 1);
194 free(session_manager
);
200 css::uno::Reference
<css::uno::XCurrentContext
> context(css::uno::getCurrentContext());
205 context
->getValueByName("system.desktop-environment") >>= desktop
;
207 if (desktop
== "PLASMA5")
209 if (!qApp
) // no qt event loop yet
211 // so we start one and read KDE settings
212 initQApp(m_KDESettings
);
214 else // someone else (most likely kde/qt vclplug) has started qt event loop
215 // all that is left to do is to read KDE settings
216 readKDESettings(m_KDESettings
);
220 void Service::setPropertyValue(OUString
const&, css::uno::Any
const&)
222 throw css::lang::IllegalArgumentException("setPropertyValue not supported", getXWeak(), -1);
225 css::uno::Any
Service::getPropertyValue(OUString
const& PropertyName
)
227 if (PropertyName
== "EnableATToolSupport" || PropertyName
== "ExternalMailer"
228 || PropertyName
== "SourceViewFontHeight" || PropertyName
== "SourceViewFontName"
229 || PropertyName
== "WorkPathVariable" || PropertyName
== "ooInetHTTPProxyName"
230 || PropertyName
== "ooInetHTTPProxyPort" || PropertyName
== "ooInetHTTPSProxyName"
231 || PropertyName
== "ooInetHTTPSProxyPort" || PropertyName
== "ooInetNoProxy"
232 || PropertyName
== "ooInetProxyType")
234 std::map
<OUString
, css::beans::Optional
<css::uno::Any
>>::iterator it
235 = m_KDESettings
.find(PropertyName
);
236 if (it
!= m_KDESettings
.end())
237 return css::uno::Any(it
->second
);
239 return css::uno::Any(css::beans::Optional
<css::uno::Any
>());
241 else if (PropertyName
== "givenname" || PropertyName
== "sn"
242 || PropertyName
== "TemplatePathVariable")
244 return css::uno::Any(css::beans::Optional
<css::uno::Any
>());
245 //TODO: obtain values from KDE?
247 throw css::beans::UnknownPropertyException(PropertyName
, getXWeak());
250 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
251 shell_kf5desktop_get_implementation(css::uno::XComponentContext
*,
252 css::uno::Sequence
<css::uno::Any
> const&)
254 return cppu::acquire(new Service());
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */