| 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> |
| 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> |
| 45 |
|
| 46 |
#include <osl/process.h> |
| 47 |
#include <osl/thread.h> |
| 48 |
|
| 49 |
#include "kfaccess.hxx" |
| 50 |
|
| 51 |
namespace |
| 52 |
{ |
| 53 |
class Service : public cppu::WeakImplHelper<css::lang::XServiceInfo, css::beans::XPropertySet> |
| 54 |
{ |
| 55 |
public: |
| 56 |
Service(); |
| 57 |
|
| 58 |
private: |
| 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 u"com.sun.star.comp.configuration.backend.KF5Backend"_ustr; |
| 68 |
} |
| 69 |
|
| 70 |
virtual sal_Bool SAL_CALL supportsService(OUString const& ServiceName) override |
| 71 |
{ |
| 72 |
return ServiceName == getSupportedServiceNames()[0]; |
| 73 |
} |
| 74 |
|
| 75 |
virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override |
| 76 |
{ |
| 77 |
return { u"com.sun.star.configuration.backend.KF5Backend"_ustr }; |
| 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 |
|
| 112 |
OString 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 |
|
| 129 |
OString 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 |
|
| 137 |
void readKDESettings(std::map<OUString, css::beans::Optional<css::uno::Any>>& rSettings) |
| 138 |
{ |
| 139 |
const std::vector<OUString> aKeys |
| 140 |
= { u"ExternalMailer"_ustr, u"SourceViewFontHeight"_ustr, u"SourceViewFontName"_ustr, |
| 141 |
u"WorkPathVariable"_ustr, u"ooInetHTTPProxyName"_ustr, u"ooInetHTTPProxyPort"_ustr, |
| 142 |
u"ooInetHTTPSProxyName"_ustr, u"ooInetHTTPSProxyPort"_ustr, u"ooInetNoProxy"_ustr, |
| 143 |
u"ooInetProxyType"_ustr }; |
| 144 |
|
| 145 |
for (const OUString& aKey : aKeys) |
| 146 |
{ |
| 147 |
css::beans::Optional<css::uno::Any> aValue = kfaccess::getValue(aKey); |
| 148 |
std::pair<OUString, css::beans::Optional<css::uno::Any>> elem |
| 149 |
= std::make_pair(aKey, aValue); |
| 150 |
rSettings.insert(elem); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// init the QApplication when we load the kfbackend 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) |
| 158 |
{ |
| 159 |
const auto aDisplay = getDisplayArg(); |
| 160 |
int nFakeArgc = aDisplay.isEmpty() ? 2 : 3; |
| 161 |
char** pFakeArgv = new char*[nFakeArgc]; |
| 162 |
|
| 163 |
pFakeArgv[0] = strdup(getExecutable().getStr()); |
| 164 |
pFakeArgv[1] = strdup("--nocrashhandler"); |
| 165 |
if (!aDisplay.isEmpty()) |
| 166 |
pFakeArgv[2] = strdup(aDisplay.getStr()); |
| 167 |
|
| 168 |
char* session_manager = nullptr; |
| 169 |
if (auto* session_manager_env = getenv("SESSION_MANAGER")) |
| 170 |
{ |
| 171 |
session_manager = strdup(session_manager_env); |
| 172 |
unsetenv("SESSION_MANAGER"); |
| 173 |
} |
| 174 |
|
| 175 |
{ |
| 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 |
| 179 |
|
| 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) |
| 183 |
free(pFakeArgv[i]); |
| 184 |
delete[] pFakeArgv; |
| 185 |
}); |
| 186 |
|
| 187 |
readKDESettings(rSettings); |
| 188 |
} |
| 189 |
|
| 190 |
if (session_manager != nullptr) |
| 191 |
{ |
| 192 |
// coverity[tainted_string] - trusted source for setenv |
| 193 |
setenv("SESSION_MANAGER", session_manager, 1); |
| 194 |
free(session_manager); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
Service::Service() |
| 199 |
{ |
| 200 |
if (Application::GetDesktopEnvironment() == u"PLASMA5") |
| 201 |
{ |
| 202 |
if (!qApp) // no qt event loop yet |
| 203 |
{ |
| 204 |
// so we start one and read KDE settings |
| 205 |
initQApp(m_KDESettings); |
| 206 |
} |
| 207 |
else // someone else (most likely kde/qt vclplug) has started qt event loop |
| 208 |
// all that is left to do is to read KDE settings |
| 209 |
readKDESettings(m_KDESettings); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
void Service::setPropertyValue(OUString const&, css::uno::Any const&) |
| 214 |
{ |
| 215 |
throw css::lang::IllegalArgumentException(u"setPropertyValue not supported"_ustr, getXWeak(), |
| 216 |
-1); |
| 217 |
} |
| 218 |
|
| 219 |
css::uno::Any Service::getPropertyValue(OUString const& PropertyName) |
| 220 |
{ |
| 221 |
if (PropertyName == "ExternalMailer" || PropertyName == "SourceViewFontHeight" |
| 222 |
|| PropertyName == "SourceViewFontName" || PropertyName == "WorkPathVariable" |
| 223 |
|| PropertyName == "ooInetHTTPProxyName" || PropertyName == "ooInetHTTPProxyPort" |
| 224 |
|| PropertyName == "ooInetHTTPSProxyName" || PropertyName == "ooInetHTTPSProxyPort" |
| 225 |
|| PropertyName == "ooInetNoProxy" || PropertyName == "ooInetProxyType") |
| 226 |
{ |
| 227 |
std::map<OUString, css::beans::Optional<css::uno::Any>>::iterator it |
| 228 |
= m_KDESettings.find(PropertyName); |
| 229 |
if (it != m_KDESettings.end()) |
| 230 |
return css::uno::Any(it->second); |
| 231 |
else |
| 232 |
return css::uno::Any(css::beans::Optional<css::uno::Any>()); |
| 233 |
} |
| 234 |
else if (PropertyName == "givenname" || PropertyName == "sn" |
| 235 |
|| PropertyName == "TemplatePathVariable") |
| 236 |
{ |
| 237 |
return css::uno::Any(css::beans::Optional<css::uno::Any>()); |
| 238 |
//TODO: obtain values from KDE? |
| 239 |
} |
| 240 |
throw css::beans::UnknownPropertyException(PropertyName, getXWeak()); |
| 241 |
} |
| 242 |
|
| 243 |
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* |
| 244 |
shell_kf5desktop_get_implementation(css::uno::XComponentContext*, |
| 245 |
css::uno::Sequence<css::uno::Any> const&) |
| 246 |
{ |
| 247 |
return cppu::acquire(new Service()); |
| 248 |
} |
| 249 |
} |
| 250 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |