Files
loongoffice/shell/source/sessioninstall/SyncDbusSessionHelper.cxx
Stephan Bergmann a93c3f2eb0 GError::message is UTF-8
This caused CppunitTest_sw_dialogs_test to fail for LO_TEST_LOCALE=or hitting

> sal/rtl/ustring.cxx:530: void rtl_uString_newFromAscii(rtl_uString**, const char*): Assertion `static_cast<unsigned char>(*pCharStr) < 0x80' failed.

at

> #4  0x00007fd18a844f74 in rtl_uString_newFromAscii(rtl_uString**, char const*) (ppThis=0x7ffcb73e6e90, pCharStr=0x6191180 "ସମୟ ସୀମା ପହଞ୍ଚିଯାଇଛି") at sal/rtl/ustring.cxx:530
> #5  0x00007fd1525af9c1 in rtl::OUString::createFromAscii(char const*) (value=0x6191180 "ସମୟ ସୀମା ପହଞ୍ଚିଯାଇଛି") at include/rtl/ustring.hxx:3328
> #6  0x00007fd1525ad026 in (anonymous namespace)::GErrorWrapper::~GErrorWrapper() (this=0x7ffcb73e6f38, __in_chrg=<optimized out>) at shell/source/sessioninstall/SyncDbusSessionHelper.cxx:35
> #7  0x00007fd1525ad6a5 in (anonymous namespace)::request(char const*, com::sun::uno::Sequence<rtl::OUString> const&, std::u16string_view) (method=0x7fd1525b481d "InstallFontconfigResources", resources=uno::Sequence of length 1 = {...}, interaction=u"hide-finished") at shell/source/sessioninstall/SyncDbusSessionHelper.cxx:91
> #8  0x00007fd1525ada9d in shell::sessioninstall::SyncDbusSessionHelper::InstallFontconfigResources(com::sun::uno::Sequence<rtl::OUString> const&, rtl::OUString const&) (this=0x5d22380, resources=uno::Sequence of length 1 = {...}, interaction="hide-finished") at shell/source/sessioninstall/SyncDbusSessionHelper.cxx:158
> #9  0x00007fd178670daa in psp::PrintFontManager::autoInstallFontLangSupport(Timer*) (this=0x2e3ed90) at vcl/unx/generic/fontmanager/fontconfig.cxx:962
> #10 0x00007fd178670cd9 in psp::PrintFontManager::LinkStubautoInstallFontLangSupport(void*, Timer*) (instance=0x2e3ed90, data=0x2e3efa0) at vcl/unx/generic/fontmanager/fontconfig.cxx:956
> #11 0x00007fd178341e03 in Link<Timer*, void>::Call(Timer*) const (this=0x2e3efc0, data=0x2e3efa0) at include/tools/link.hxx:111
[...]

(But probably better to use the lenient OUString-from-rtl_TextEncoding ctor
rather than OUString::fromUtf8, in case GError::message happens to not contain
proper UTF-8 after all, which would make the latter assert.)

Change-Id: I61fede4576988c5f7f35bb071ab3f2c0e7a15aa1
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122599
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2021-09-24 20:53:42 +02:00

213 lines
7.2 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "SyncDbusSessionHelper.hxx"
#include <cppuhelper/supportsservice.hxx>
#include <gio/gio.h>
#include <cstring>
#include <memory>
#include <string_view>
#include <vector>
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::uno;
namespace
{
struct GVariantDeleter { void operator()(GVariant* pV) { if (pV) g_variant_unref(pV); } };
struct GVariantBuilderDeleter { void operator()(GVariantBuilder* pVB) { g_variant_builder_unref(pVB); } };
template <typename T> struct GObjectDeleter { void operator()(T* pO) { g_object_unref(pO); } };
class GErrorWrapper
{
GError* m_pError;
public:
explicit GErrorWrapper() : m_pError(nullptr) {}
~GErrorWrapper() noexcept(false)
{
if(!m_pError)
return;
OUString sMsg(
m_pError->message, std::strlen(m_pError->message), RTL_TEXTENCODING_UTF8);
g_error_free(m_pError);
throw RuntimeException(sMsg);
}
GError*& getRef() { return m_pError; }
};
GDBusProxy* lcl_GetPackageKitProxy(std::u16string_view sInterface)
{
const OString sFullInterface = "org.freedesktop.PackageKit." + OUStringToOString(sInterface, RTL_TEXTENCODING_ASCII_US);
GDBusProxy* proxy = nullptr;
{
GErrorWrapper error;
proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE, nullptr,
"org.freedesktop.PackageKit",
"/org/freedesktop/PackageKit",
reinterpret_cast<const gchar*>(sFullInterface.getStr()),
nullptr,
&error.getRef());
}
if(!proxy)
throw RuntimeException("couldn't get a proxy!");
return proxy;
}
GVariant* pk_make_platform_data()
{
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
return g_variant_builder_end(&builder);
}
void request(
char const * method,
css::uno::Sequence<OUString> const & resources,
std::u16string_view interaction)
{
// Keep strings alive until after call to g_dbus_proxy_call_sync
std::vector<OString> resUtf8;
std::shared_ptr<GVariantBuilder> builder(
g_variant_builder_new(G_VARIANT_TYPE ("as")), GVariantBuilderDeleter());
for (auto & i: resources) {
auto s(OUStringToOString(i, RTL_TEXTENCODING_UTF8));
resUtf8.push_back(s);
g_variant_builder_add(builder.get(), "s", s.getStr());
}
auto iactUtf8(OUStringToOString(interaction, RTL_TEXTENCODING_UTF8));
std::shared_ptr<GDBusProxy> proxy(
lcl_GetPackageKitProxy(u"Modify2"), GObjectDeleter<GDBusProxy>());
GErrorWrapper error;
std::shared_ptr<GVariant> result(g_dbus_proxy_call_sync(
proxy.get(), method,
g_variant_new(
"(asss@a{sv})", builder.get(), iactUtf8.getStr(),
"libreoffice-startcenter.desktop", pk_make_platform_data()),
G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error.getRef()), GVariantDeleter());
}
}
namespace shell::sessioninstall
{
SyncDbusSessionHelper::SyncDbusSessionHelper(Reference<XComponentContext> const&)
{
#if !GLIB_CHECK_VERSION(2,36,0)
g_type_init ();
#endif
}
Sequence< OUString > SAL_CALL SyncDbusSessionHelper::getSupportedServiceNames()
{
return { "org.freedesktop.PackageKit.SyncDbusSessionHelper" };
}
OUString SAL_CALL SyncDbusSessionHelper::getImplementationName()
{
return "org.libreoffice.comp.shell.sessioninstall.SyncDbusSessionHelper";
}
sal_Bool SAL_CALL SyncDbusSessionHelper::supportsService(const OUString& aServiceName)
{
return cppu::supportsService(this, aServiceName);
}
void SyncDbusSessionHelper::InstallPackageFiles(
css::uno::Sequence<OUString> const & files,
OUString const & interaction)
{
request("InstallPackageFiles", files, interaction);
}
void SyncDbusSessionHelper::InstallProvideFiles(
css::uno::Sequence<OUString> const & files,
OUString const & interaction)
{
request("InstallProvideFiles", files, interaction);
}
void SyncDbusSessionHelper::InstallCatalogs(
css::uno::Sequence<OUString> const & files,
OUString const & interaction)
{
request("InstallCatalogs", files, interaction);
}
void SyncDbusSessionHelper::InstallPackageNames(
css::uno::Sequence<OUString> const & packages,
OUString const & interaction)
{
request("InstallPackageNames", packages, interaction);
}
void SyncDbusSessionHelper::InstallMimeTypes(
css::uno::Sequence<OUString> const & mimeTypes,
OUString const & interaction)
{
request("InstallMimeTypes", mimeTypes, interaction);
}
void SyncDbusSessionHelper::InstallFontconfigResources(
css::uno::Sequence<OUString> const & resources,
OUString const & interaction)
{
request("InstallFontconfigResources", resources, interaction);
}
void SyncDbusSessionHelper::InstallGStreamerResources(
css::uno::Sequence<OUString> const & resources,
OUString const & interaction)
{
request("InstallGStreamerResources", resources, interaction);
}
void SyncDbusSessionHelper::RemovePackageByFiles(
css::uno::Sequence<OUString> const & files,
OUString const & interaction)
{
request("RemovePackageByFiles", files, interaction);
}
void SyncDbusSessionHelper::InstallPrinterDrivers(
css::uno::Sequence<OUString> const & files,
OUString const & interaction)
{
request("InstallPrinterDrivers", files, interaction);
}
void SAL_CALL SyncDbusSessionHelper::IsInstalled( const OUString& sPackagename, const OUString& sInteraction, sal_Bool& o_isInstalled )
{
const OString sPackagenameAscii = OUStringToOString(sPackagename, RTL_TEXTENCODING_ASCII_US);
const OString sInteractionAscii = OUStringToOString(sInteraction, RTL_TEXTENCODING_ASCII_US);
std::shared_ptr<GDBusProxy> proxy(lcl_GetPackageKitProxy(u"Query"), GObjectDeleter<GDBusProxy>());
GErrorWrapper error;
std::shared_ptr<GVariant> result(g_dbus_proxy_call_sync (proxy.get(),
"IsInstalled",
g_variant_new ("(ss)",
sPackagenameAscii.getStr(),
sInteractionAscii.getStr()),
G_DBUS_CALL_FLAGS_NONE,
-1, /* timeout */
nullptr, /* cancellable */
&error.getRef()),GVariantDeleter());
if(result)
o_isInstalled = bool(g_variant_get_boolean(g_variant_get_child_value(result.get(),0)));
}
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
shell_sessioninstall_get_implementation(
css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
{
return cppu::acquire(new SyncDbusSessionHelper(context));
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */